In Special:AllPages, limit the size of hierarchical lists
[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 */
29 class SpecialAllpages extends IncludableSpecialPage {
30
31 /**
32 * Maximum number of pages to show on single subpage.
33 *
34 * @var int $maxPerPage
35 */
36 protected $maxPerPage = 345;
37
38 /**
39 * Maximum number of pages to show on single index subpage.
40 *
41 * @var int $maxLineCount
42 */
43 protected $maxLineCount = 100;
44
45 /**
46 * Maximum number of chars to show for an entry.
47 *
48 * @var int $maxPageLength
49 */
50 protected $maxPageLength = 70;
51
52 /**
53 * Maximum number of pages in a hierarchical ("top level") list.
54 *
55 * Traversal of the entire page list by spidering the top levels is thought
56 * to require O(N^3) DB CPU time where N is the number of pages on the wiki.
57 * See bug 56840. If this limit is exceeded, the behaviour becomes like a
58 * simple alphabetic pager.
59 */
60 protected $maxTopLevelPages = 50000;
61
62 /**
63 * Determines, which message describes the input field 'nsfrom'.
64 *
65 * @var string $nsfromMsg
66 */
67 protected $nsfromMsg = 'allpagesfrom';
68
69 /**
70 * Constructor
71 *
72 * @param string $name name of the special page, as seen in links and URLs (default: 'Allpages')
73 */
74 function __construct( $name = 'Allpages' ) {
75 parent::__construct( $name );
76 }
77
78 /**
79 * Entry point : initialise variables and call subfunctions.
80 *
81 * @param string $par becomes "FOO" when called like Special:Allpages/FOO (default NULL)
82 */
83 function execute( $par ) {
84 $request = $this->getRequest();
85 $out = $this->getOutput();
86
87 $this->setHeaders();
88 $this->outputHeader();
89 $out->allowClickjacking();
90
91 # GET values
92 $from = $request->getVal( 'from', null );
93 $to = $request->getVal( 'to', null );
94 $namespace = $request->getInt( 'namespace' );
95 $hideredirects = $request->getBool( 'hideredirects', false );
96
97 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
98
99 $out->setPageTitle(
100 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) ) ?
101 $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
102 $this->msg( 'allarticles' )
103 );
104 $out->addModuleStyles( 'mediawiki.special' );
105
106 if ( $par !== null ) {
107 $this->showChunk( $namespace, $par, $to, $hideredirects );
108 } elseif ( $from !== null && $to === null ) {
109 $this->showChunk( $namespace, $from, $to, $hideredirects );
110 } else {
111 $this->showToplevel( $namespace, $from, $to, $hideredirects );
112 }
113 }
114
115 /**
116 * HTML for the top form
117 *
118 * @param $namespace Integer: a namespace constant (default NS_MAIN).
119 * @param string $from dbKey we are starting listing at.
120 * @param string $to dbKey we are ending listing at.
121 * @param bool $hideredirects dont show redirects (default FALSE)
122 * @return string
123 */
124 function namespaceForm( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
125 global $wgScript;
126 $t = $this->getTitle();
127
128 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
129 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
130 $out .= Html::hidden( 'title', $t->getPrefixedText() );
131 $out .= Xml::openElement( 'fieldset' );
132 $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
133 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
134 $out .= "<tr>
135 <td class='mw-label'>" .
136 Xml::label( $this->msg( 'allpagesfrom' )->text(), 'nsfrom' ) .
137 " </td>
138 <td class='mw-input'>" .
139 Xml::input( 'from', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
140 " </td>
141 </tr>
142 <tr>
143 <td class='mw-label'>" .
144 Xml::label( $this->msg( 'allpagesto' )->text(), 'nsto' ) .
145 " </td>
146 <td class='mw-input'>" .
147 Xml::input( 'to', 30, str_replace( '_', ' ', $to ), array( 'id' => 'nsto' ) ) .
148 " </td>
149 </tr>
150 <tr>
151 <td class='mw-label'>" .
152 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
153 " </td>
154 <td class='mw-input'>" .
155 Html::namespaceSelector(
156 array( 'selected' => $namespace ),
157 array( 'name' => 'namespace', 'id' => 'namespace' )
158 ) . ' ' .
159 Xml::checkLabel(
160 $this->msg( 'allpages-hide-redirects' )->text(),
161 'hideredirects',
162 'hideredirects',
163 $hideredirects
164 ) . ' ' .
165 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
166 " </td>
167 </tr>";
168 $out .= Xml::closeElement( 'table' );
169 $out .= Xml::closeElement( 'fieldset' );
170 $out .= Xml::closeElement( 'form' );
171 $out .= Xml::closeElement( 'div' );
172
173 return $out;
174 }
175
176 /**
177 * @param $namespace Integer (default NS_MAIN)
178 * @param string $from list all pages from this name
179 * @param string $to list all pages to this name
180 * @param bool $hideredirects dont show redirects (default FALSE)
181 */
182 function showToplevel( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
183 $output = $this->getOutput();
184
185 # TODO: Either make this *much* faster or cache the title index points
186 # in the querycache table.
187
188 $dbr = wfGetDB( DB_SLAVE );
189 $out = "";
190 $where = array( 'page_namespace' => $namespace );
191
192 if ( $hideredirects ) {
193 $where['page_is_redirect'] = 0;
194 }
195
196 $from = Title::makeTitleSafe( $namespace, $from );
197 $to = Title::makeTitleSafe( $namespace, $to );
198 $from = ( $from && $from->isLocal() ) ? $from->getDBkey() : null;
199 $to = ( $to && $to->isLocal() ) ? $to->getDBkey() : null;
200
201 if ( isset( $from ) ) {
202 $where[] = 'page_title >= ' . $dbr->addQuotes( $from );
203 }
204
205 if ( isset( $to ) ) {
206 $where[] = 'page_title <= ' . $dbr->addQuotes( $to );
207 }
208
209 global $wgMemc;
210 $key = wfMemcKey( 'allpages', 'ns', $namespace, sha1( $from ), sha1( $to ) );
211 $lines = $wgMemc->get( $key );
212
213 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__ );
214
215 // Don't show a hierarchical list if the number of pages is very large,
216 // since generating it will cause a lot of scanning
217 if ( $count > $this->maxTopLevelPages ) {
218 $this->showChunk( $namespace, $from, $to, $hideredirects );
219 return;
220 }
221
222 $maxPerSubpage = intval( $count / $this->maxLineCount );
223 $maxPerSubpage = max( $maxPerSubpage, $this->maxPerPage );
224
225 if ( !is_array( $lines ) ) {
226 $options = array( 'LIMIT' => 1 );
227 $options['ORDER BY'] = 'page_title ASC';
228 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__, $options );
229 $lastTitle = $firstTitle;
230 # This array is going to hold the page_titles in order.
231 $lines = array( $firstTitle );
232 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
233 $done = false;
234 while ( !$done ) {
235 // Fetch the last title of this chunk and the first of the next
236 $chunk = ( $lastTitle === false )
237 ? array()
238 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
239 $res = $dbr->select( 'page', /* FROM */
240 'page_title', /* WHAT */
241 array_merge( $where, $chunk ),
242 __METHOD__,
243 array( 'LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC' )
244 );
245
246 $s = $dbr->fetchObject( $res );
247 if ( $s ) {
248 array_push( $lines, $s->page_title );
249 } else {
250 // Final chunk, but ended prematurely. Go back and find the end.
251 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
252 array_merge( $where, $chunk ),
253 __METHOD__ );
254 array_push( $lines, $endTitle );
255 $done = true;
256 }
257
258 $s = $res->fetchObject();
259 if ( $s ) {
260 array_push( $lines, $s->page_title );
261 $lastTitle = $s->page_title;
262 } else {
263 // This was a final chunk and ended exactly at the limit.
264 // Rare but convenient!
265 $done = true;
266 }
267 $res->free();
268 }
269 $wgMemc->add( $key, $lines, 3600 );
270 }
271
272 // If there are only two or less sections, don't even display them.
273 // Instead, display the first section directly.
274 if ( count( $lines ) <= 2 ) {
275 if ( !empty( $lines ) ) {
276 $this->showChunk( $namespace, $from, $to, $hideredirects );
277 } else {
278 $output->addHTML( $this->namespaceForm( $namespace, $from, $to, $hideredirects ) );
279 }
280
281 return;
282 }
283
284 # At this point, $lines should contain an even number of elements.
285 $out .= Xml::openElement( 'table', array( 'class' => 'allpageslist' ) );
286 while ( count( $lines ) > 0 ) {
287 $inpoint = array_shift( $lines );
288 $outpoint = array_shift( $lines );
289 $out .= $this->showline( $inpoint, $outpoint, $namespace, $hideredirects );
290 }
291 $out .= Xml::closeElement( 'table' );
292 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
293
294 # Is there more?
295 if ( $this->including() ) {
296 $out2 = '';
297 } else {
298 if ( isset( $from ) || isset( $to ) ) {
299 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
300 '<tr>
301 <td>' .
302 $nsForm .
303 '</td>
304 <td class="mw-allpages-nav">' .
305 Linker::link( $this->getTitle(), $this->msg( 'allpages' )->escaped(),
306 array(), array(), 'known' ) .
307 "</td>
308 </tr>" .
309 Xml::closeElement( 'table' );
310 } else {
311 $out2 = $nsForm;
312 }
313 }
314 $output->addHTML( $out2 . $out );
315 }
316
317 /**
318 * Show a line of "ABC to DEF" ranges of articles
319 *
320 * @param string $inpoint lower limit of pagenames
321 * @param string $outpoint upper limit of pagenames
322 * @param $namespace Integer (Default NS_MAIN)
323 * @param bool $hideRedirects don't show redirects. Default: false
324 * @return string
325 */
326 function showline( $inpoint, $outpoint, $namespace = NS_MAIN, $hideRedirects = false ) {
327 // Use content language since page titles are considered to use content language
328 global $wgContLang;
329
330 $inpointf = str_replace( '_', ' ', $inpoint );
331 $outpointf = str_replace( '_', ' ', $outpoint );
332
333 // Don't let the length runaway
334 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength );
335 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength );
336
337 $queryParams = array(
338 'from' => $inpoint,
339 'to' => $outpoint,
340 );
341
342 if ( $namespace ) {
343 $queryParams['namespace'] = $namespace;
344 }
345 if ( $hideRedirects ) {
346 $queryParams['hideredirects'] = 1;
347 }
348
349 $url = $this->getTitle()->getLocalURL( $queryParams );
350 $inlink = Html::element( 'a', array( 'href' => $url ), $inpointf );
351 $outlink = Html::element( 'a', array( 'href' => $url ), $outpointf );
352
353 $out = $this->msg( 'alphaindexline' )->rawParams(
354 "$inlink</td><td>",
355 "</td><td>$outlink"
356 )->escaped();
357
358 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
359 }
360
361 /**
362 * @param int $namespace Namespace (Default NS_MAIN)
363 * @param string $from list all pages from this name (default FALSE)
364 * @param string $to list all pages to this name (default FALSE)
365 * @param bool $hideredirects dont show redirects (default FALSE)
366 */
367 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
368 $output = $this->getOutput();
369
370 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
371 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
372 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
373 $n = 0;
374
375 if ( !$fromList || !$toList ) {
376 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
377 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
378 // Show errormessage and reset to NS_MAIN
379 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
380 $namespace = NS_MAIN;
381 } else {
382 list( $namespace, $fromKey, $from ) = $fromList;
383 list( , $toKey, $to ) = $toList;
384
385 $dbr = wfGetDB( DB_SLAVE );
386 $conds = array(
387 'page_namespace' => $namespace,
388 'page_title >= ' . $dbr->addQuotes( $fromKey )
389 );
390
391 if ( $hideredirects ) {
392 $conds['page_is_redirect'] = 0;
393 }
394
395 if ( $toKey !== "" ) {
396 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
397 }
398
399 $res = $dbr->select( 'page',
400 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
401 $conds,
402 __METHOD__,
403 array(
404 'ORDER BY' => 'page_title',
405 'LIMIT' => $this->maxPerPage + 1,
406 'USE INDEX' => 'name_title',
407 )
408 );
409
410 if ( $res->numRows() > 0 ) {
411 $out = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
412 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
413 $t = Title::newFromRow( $s );
414 if ( $t ) {
415 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
416 Linker::link( $t ) .
417 ( $s->page_is_redirect ? '</div>' : '' );
418 } else {
419 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
420 }
421
422 if ( $n % 3 == 0 ) {
423 $out .= '<tr>';
424 }
425
426 $out .= "<td style=\"width:33%\">$link</td>";
427 $n++;
428 if ( $n % 3 == 0 ) {
429 $out .= "</tr>\n";
430 }
431 }
432
433 if ( ( $n % 3 ) != 0 ) {
434 $out .= "</tr>\n";
435 }
436 $out .= Xml::closeElement( 'table' );
437 } else {
438 $out = '';
439 }
440 }
441
442 if ( $this->including() ) {
443 $out2 = '';
444 } else {
445 if ( $from == '' ) {
446 // First chunk; no previous link.
447 $prevTitle = null;
448 } else {
449 # Get the last title from previous chunk
450 $dbr = wfGetDB( DB_SLAVE );
451 $res_prev = $dbr->select(
452 'page',
453 'page_title',
454 array( 'page_namespace' => $namespace, 'page_title < ' . $dbr->addQuotes( $from ) ),
455 __METHOD__,
456 array( 'ORDER BY' => 'page_title DESC',
457 'LIMIT' => $this->maxPerPage, 'OFFSET' => ( $this->maxPerPage - 1 )
458 )
459 );
460
461 # Get first title of previous complete chunk
462 if ( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
463 $pt = $dbr->fetchObject( $res_prev );
464 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
465 } else {
466 # The previous chunk is not complete, need to link to the very first title
467 # available in the database
468 $options = array( 'LIMIT' => 1 );
469 if ( !$dbr->implicitOrderby() ) {
470 $options['ORDER BY'] = 'page_title';
471 }
472 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
473 array( 'page_namespace' => $namespace ), __METHOD__, $options );
474 # Show the previous link if it s not the current requested chunk
475 if ( $from != $reallyFirstPage_title ) {
476 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
477 } else {
478 $prevTitle = null;
479 }
480 }
481 }
482
483 $self = $this->getTitle();
484
485 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
486 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
487 '<tr>
488 <td>' .
489 $nsForm .
490 '</td>
491 <td class="mw-allpages-nav">' .
492 Linker::link( $self, $this->msg( 'allpages' )->escaped() );
493
494 # Do we put a previous link ?
495 if ( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
496 $query = array( 'from' => $prevTitle->getText() );
497
498 if ( $namespace ) {
499 $query['namespace'] = $namespace;
500 }
501
502 if ( $hideredirects ) {
503 $query['hideredirects'] = $hideredirects;
504 }
505
506 $prevLink = Linker::linkKnown(
507 $self,
508 $this->msg( 'prevpage', $pt )->escaped(),
509 array(),
510 $query
511 );
512 $out2 = $this->getLanguage()->pipeList( array( $out2, $prevLink ) );
513 }
514
515 if ( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
516 # $s is the first link of the next chunk
517 $t = Title::makeTitle( $namespace, $s->page_title );
518 $query = array( 'from' => $t->getText() );
519
520 if ( $namespace ) {
521 $query['namespace'] = $namespace;
522 }
523
524 if ( $hideredirects ) {
525 $query['hideredirects'] = $hideredirects;
526 }
527
528 $nextLink = Linker::linkKnown(
529 $self,
530 $this->msg( 'nextpage', $t->getText() )->escaped(),
531 array(),
532 $query
533 );
534 $out2 = $this->getLanguage()->pipeList( array( $out2, $nextLink ) );
535 }
536 $out2 .= "</td></tr></table>";
537 }
538
539 $output->addHTML( $out2 . $out );
540
541 $links = array();
542 if ( isset( $prevLink ) ) {
543 $links[] = $prevLink;
544 }
545
546 if ( isset( $nextLink ) ) {
547 $links[] = $nextLink;
548 }
549
550 if ( count( $links ) ) {
551 $output->addHTML(
552 Html::element( 'hr' ) .
553 Html::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
554 $this->getLanguage()->pipeList( $links )
555 )
556 );
557 }
558 }
559
560 /**
561 * @param $ns Integer: the namespace of the article
562 * @param string $text the name of the article
563 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
564 */
565 protected function getNamespaceKeyAndText( $ns, $text ) {
566 if ( $text == '' ) {
567 # shortcut for common case
568 return array( $ns, '', '' );
569 }
570
571 $t = Title::makeTitleSafe( $ns, $text );
572 if ( $t && $t->isLocal() ) {
573 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
574 } elseif ( $t ) {
575 return null;
576 }
577
578 # try again, in case the problem was an empty pagename
579 $text = preg_replace( '/(#|$)/', 'X$1', $text );
580 $t = Title::makeTitleSafe( $ns, $text );
581 if ( $t && $t->isLocal() ) {
582 return array( $t->getNamespace(), '', '' );
583 } else {
584 return null;
585 }
586 }
587
588 protected function getGroupName() {
589 return 'pages';
590 }
591 }