Merge "registration: Only allow one extension to set a specific config setting"
[lhc/web/wiklou.git] / includes / specials / SpecialAllPages.php
1 <?php
2 /**
3 * Implements Special:Allpages
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Implements Special:Allpages
26 *
27 * @ingroup SpecialPage
28 * @todo Rewrite using IndexPager
29 */
30 class SpecialAllPages extends IncludableSpecialPage {
31
32 /**
33 * Maximum number of pages to show on single subpage.
34 *
35 * @var int $maxPerPage
36 */
37 protected $maxPerPage = 345;
38
39 /**
40 * Determines, which message describes the input field 'nsfrom'.
41 *
42 * @var string $nsfromMsg
43 */
44 protected $nsfromMsg = 'allpagesfrom';
45
46 /**
47 * @param string $name Name of the special page, as seen in links and URLs (default: 'Allpages')
48 */
49 function __construct( $name = 'Allpages' ) {
50 parent::__construct( $name );
51 }
52
53 /**
54 * Entry point : initialise variables and call subfunctions.
55 *
56 * @param string $par Becomes "FOO" when called like Special:Allpages/FOO (default null)
57 */
58 function execute( $par ) {
59 $request = $this->getRequest();
60 $out = $this->getOutput();
61
62 $this->setHeaders();
63 $this->outputHeader();
64 $out->allowClickjacking();
65
66 # GET values
67 $from = $request->getVal( 'from', null );
68 $to = $request->getVal( 'to', null );
69 $namespace = $request->getInt( 'namespace' );
70
71 $miserMode = (bool)$this->getConfig()->get( 'MiserMode' );
72
73 // Redirects filter is disabled in MiserMode
74 $hideredirects = $request->getBool( 'hideredirects', false ) && !$miserMode;
75
76 $namespaces = $this->getLanguage()->getNamespaces();
77
78 $out->setPageTitle(
79 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) ) ?
80 $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
81 $this->msg( 'allarticles' )
82 );
83 $out->addModuleStyles( 'mediawiki.special' );
84
85 if ( $par !== null ) {
86 $this->showChunk( $namespace, $par, $to, $hideredirects );
87 } elseif ( $from !== null && $to === null ) {
88 $this->showChunk( $namespace, $from, $to, $hideredirects );
89 } else {
90 $this->showToplevel( $namespace, $from, $to, $hideredirects );
91 }
92 }
93
94 /**
95 * Outputs the HTMLForm used on this page
96 *
97 * @param int $namespace A namespace constant (default NS_MAIN).
98 * @param string $from DbKey we are starting listing at.
99 * @param string $to DbKey we are ending listing at.
100 * @param bool $hideRedirects Dont show redirects (default false)
101 */
102 protected function outputHTMLForm( $namespace = NS_MAIN,
103 $from = '', $to = '', $hideRedirects = false
104 ) {
105 $miserMode = (bool)$this->getConfig()->get( 'MiserMode' );
106 $fields = [
107 'from' => [
108 'type' => 'text',
109 'name' => 'from',
110 'id' => 'nsfrom',
111 'size' => 30,
112 'label-message' => 'allpagesfrom',
113 'default' => str_replace( '_', ' ', $from ),
114 ],
115 'to' => [
116 'type' => 'text',
117 'name' => 'to',
118 'id' => 'nsto',
119 'size' => 30,
120 'label-message' => 'allpagesto',
121 'default' => str_replace( '_', ' ', $to ),
122 ],
123 'namespace' => [
124 'type' => 'namespaceselect',
125 'name' => 'namespace',
126 'id' => 'namespace',
127 'label-message' => 'namespace',
128 'all' => null,
129 'value' => $namespace,
130 ],
131 'hideredirects' => [
132 'type' => 'check',
133 'name' => 'hideredirects',
134 'id' => 'hidredirects',
135 'label-message' => 'allpages-hide-redirects',
136 'value' => $hideRedirects,
137 ],
138 ];
139
140 if ( $miserMode ) {
141 unset( $fields['hideredirects'] );
142 }
143
144 $form = HTMLForm::factory( 'table', $fields, $this->getContext() );
145 $form->setMethod( 'get' )
146 ->setWrapperLegendMsg( 'allpages' )
147 ->setSubmitTextMsg( 'allpagessubmit' )
148 ->prepareForm()
149 ->displayForm( false );
150 }
151
152 /**
153 * @param int $namespace (default NS_MAIN)
154 * @param string $from List all pages from this name
155 * @param string $to List all pages to this name
156 * @param bool $hideredirects Dont show redirects (default false)
157 */
158 function showToplevel( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
159 $from = Title::makeTitleSafe( $namespace, $from );
160 $to = Title::makeTitleSafe( $namespace, $to );
161 $from = ( $from && $from->isLocal() ) ? $from->getDBkey() : null;
162 $to = ( $to && $to->isLocal() ) ? $to->getDBkey() : null;
163
164 $this->showChunk( $namespace, $from, $to, $hideredirects );
165 }
166
167 /**
168 * @param int $namespace Namespace (Default NS_MAIN)
169 * @param string $from List all pages from this name (default false)
170 * @param string $to List all pages to this name (default false)
171 * @param bool $hideredirects Dont show redirects (default false)
172 */
173 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
174 $output = $this->getOutput();
175
176 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
177 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
178 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
179 $n = 0;
180 $prevTitle = null;
181
182 if ( !$fromList || !$toList ) {
183 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
184 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
185 // Show errormessage and reset to NS_MAIN
186 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
187 $namespace = NS_MAIN;
188 } else {
189 list( $namespace, $fromKey, $from ) = $fromList;
190 list( , $toKey, $to ) = $toList;
191
192 $dbr = wfGetDB( DB_REPLICA );
193 $filterConds = [ 'page_namespace' => $namespace ];
194 if ( $hideredirects ) {
195 $filterConds['page_is_redirect'] = 0;
196 }
197
198 $conds = $filterConds;
199 $conds[] = 'page_title >= ' . $dbr->addQuotes( $fromKey );
200 if ( $toKey !== "" ) {
201 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
202 }
203
204 $res = $dbr->select( 'page',
205 [ 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ],
206 $conds,
207 __METHOD__,
208 [
209 'ORDER BY' => 'page_title',
210 'LIMIT' => $this->maxPerPage + 1,
211 'USE INDEX' => 'name_title',
212 ]
213 );
214
215 $linkRenderer = $this->getLinkRenderer();
216 if ( $res->numRows() > 0 ) {
217 $out = Html::openElement( 'ul', [ 'class' => 'mw-allpages-chunk' ] );
218
219 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
220 $t = Title::newFromRow( $s );
221 if ( $t ) {
222 $out .= '<li' .
223 ( $s->page_is_redirect ? ' class="allpagesredirect"' : '' ) .
224 '>' .
225 $linkRenderer->makeLink( $t ) .
226 "</li>\n";
227 } else {
228 $out .= '<li>[[' . htmlspecialchars( $s->page_title ) . "]]</li>\n";
229 }
230 $n++;
231 }
232 $out .= Html::closeElement( 'ul' );
233
234 if ( $res->numRows() > 2 ) {
235 // Only apply CSS column styles if there's more than 2 entries.
236 // Otherwise, rendering is broken as "mw-allpages-body"'s CSS column count is 3.
237 $out = Html::rawElement( 'div', [ 'class' => 'mw-allpages-body' ], $out );
238 }
239 } else {
240 $out = '';
241 }
242
243 if ( $fromKey !== '' && !$this->including() ) {
244 # Get the first title from previous chunk
245 $prevConds = $filterConds;
246 $prevConds[] = 'page_title < ' . $dbr->addQuotes( $fromKey );
247 $prevKey = $dbr->selectField(
248 'page',
249 'page_title',
250 $prevConds,
251 __METHOD__,
252 [ 'ORDER BY' => 'page_title DESC', 'OFFSET' => $this->maxPerPage - 1 ]
253 );
254
255 if ( $prevKey === false ) {
256 # The previous chunk is not complete, need to link to the very first title
257 # available in the database
258 $prevKey = $dbr->selectField(
259 'page',
260 'page_title',
261 $prevConds,
262 __METHOD__,
263 [ 'ORDER BY' => 'page_title' ]
264 );
265 }
266
267 if ( $prevKey !== false ) {
268 $prevTitle = Title::makeTitle( $namespace, $prevKey );
269 }
270 }
271 }
272
273 if ( $this->including() ) {
274 $output->addHTML( $out );
275 return;
276 }
277
278 $navLinks = [];
279 $self = $this->getPageTitle();
280
281 $linkRenderer = $this->getLinkRenderer();
282 // Generate a "previous page" link if needed
283 if ( $prevTitle ) {
284 $query = [ 'from' => $prevTitle->getText() ];
285
286 if ( $namespace ) {
287 $query['namespace'] = $namespace;
288 }
289
290 if ( $hideredirects ) {
291 $query['hideredirects'] = $hideredirects;
292 }
293
294 $navLinks[] = $linkRenderer->makeKnownLink(
295 $self,
296 $this->msg( 'prevpage', $prevTitle->getText() )->text(),
297 [],
298 $query
299 );
300
301 }
302
303 // Generate a "next page" link if needed
304 if ( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
305 # $s is the first link of the next chunk
306 $t = Title::makeTitle( $namespace, $s->page_title );
307 $query = [ 'from' => $t->getText() ];
308
309 if ( $namespace ) {
310 $query['namespace'] = $namespace;
311 }
312
313 if ( $hideredirects ) {
314 $query['hideredirects'] = $hideredirects;
315 }
316
317 $navLinks[] = $linkRenderer->makeKnownLink(
318 $self,
319 $this->msg( 'nextpage', $t->getText() )->text(),
320 [],
321 $query
322 );
323 }
324
325 $this->outputHTMLForm( $namespace, $from, $to, $hideredirects );
326
327 if ( count( $navLinks ) ) {
328 // Add pagination links
329 $pagination = Html::rawElement( 'div',
330 [ 'class' => 'mw-allpages-nav' ],
331 $this->getLanguage()->pipeList( $navLinks )
332 );
333
334 $output->addHTML( $pagination );
335 $out .= Html::element( 'hr' ) . $pagination; // Footer
336 }
337
338 $output->addHTML( $out );
339 }
340
341 /**
342 * @param int $ns The namespace of the article
343 * @param string $text The name of the article
344 * @return array|null [ int namespace, string dbkey, string pagename ] or null on error
345 */
346 protected function getNamespaceKeyAndText( $ns, $text ) {
347 if ( $text == '' ) {
348 # shortcut for common case
349 return [ $ns, '', '' ];
350 }
351
352 $t = Title::makeTitleSafe( $ns, $text );
353 if ( $t && $t->isLocal() ) {
354 return [ $t->getNamespace(), $t->getDBkey(), $t->getText() ];
355 } elseif ( $t ) {
356 return null;
357 }
358
359 # try again, in case the problem was an empty pagename
360 $text = preg_replace( '/(#|$)/', 'X$1', $text );
361 $t = Title::makeTitleSafe( $ns, $text );
362 if ( $t && $t->isLocal() ) {
363 return [ $t->getNamespace(), '', '' ];
364 } else {
365 return null;
366 }
367 }
368
369 /**
370 * Return an array of subpages beginning with $search that this special page will accept.
371 *
372 * @param string $search Prefix to search for
373 * @param int $limit Maximum number of results to return (usually 10)
374 * @param int $offset Number of results to skip (usually 0)
375 * @return string[] Matching subpages
376 */
377 public function prefixSearchSubpages( $search, $limit, $offset ) {
378 return $this->prefixSearchString( $search, $limit, $offset );
379 }
380
381 protected function getGroupName() {
382 return 'pages';
383 }
384 }