Fixed some @params documentation (includes/[specialpage|specials])
[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 && array_key_exists( $namespace, $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 int $namespace 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->getPageTitle();
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 int $namespace (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
220 return;
221 }
222
223 $maxPerSubpage = intval( $count / $this->maxLineCount );
224 $maxPerSubpage = max( $maxPerSubpage, $this->maxPerPage );
225
226 if ( !is_array( $lines ) ) {
227 $options = array( 'LIMIT' => 1 );
228 $options['ORDER BY'] = 'page_title ASC';
229 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__, $options );
230 $lastTitle = $firstTitle;
231 # This array is going to hold the page_titles in order.
232 $lines = array( $firstTitle );
233 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
234 $done = false;
235 while ( !$done ) {
236 // Fetch the last title of this chunk and the first of the next
237 $chunk = ( $lastTitle === false )
238 ? array()
239 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
240 $res = $dbr->select( 'page', /* FROM */
241 'page_title', /* WHAT */
242 array_merge( $where, $chunk ),
243 __METHOD__,
244 array( 'LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC' )
245 );
246
247 $s = $dbr->fetchObject( $res );
248 if ( $s ) {
249 array_push( $lines, $s->page_title );
250 } else {
251 // Final chunk, but ended prematurely. Go back and find the end.
252 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
253 array_merge( $where, $chunk ),
254 __METHOD__ );
255 array_push( $lines, $endTitle );
256 $done = true;
257 }
258
259 $s = $res->fetchObject();
260 if ( $s ) {
261 array_push( $lines, $s->page_title );
262 $lastTitle = $s->page_title;
263 } else {
264 // This was a final chunk and ended exactly at the limit.
265 // Rare but convenient!
266 $done = true;
267 }
268 $res->free();
269 }
270 $wgMemc->add( $key, $lines, 3600 );
271 }
272
273 // If there are only two or less sections, don't even display them.
274 // Instead, display the first section directly.
275 if ( count( $lines ) <= 2 ) {
276 if ( !empty( $lines ) ) {
277 $this->showChunk( $namespace, $from, $to, $hideredirects );
278 } else {
279 $output->addHTML( $this->namespaceForm( $namespace, $from, $to, $hideredirects ) );
280 }
281
282 return;
283 }
284
285 # At this point, $lines should contain an even number of elements.
286 $out .= Xml::openElement( 'table', array( 'class' => 'allpageslist' ) );
287 while ( count( $lines ) > 0 ) {
288 $inpoint = array_shift( $lines );
289 $outpoint = array_shift( $lines );
290 $out .= $this->showline( $inpoint, $outpoint, $namespace, $hideredirects );
291 }
292 $out .= Xml::closeElement( 'table' );
293 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
294
295 # Is there more?
296 if ( $this->including() ) {
297 $out2 = '';
298 } else {
299 if ( isset( $from ) || isset( $to ) ) {
300 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
301 '<tr>
302 <td>' .
303 $nsForm .
304 '</td>
305 <td class="mw-allpages-nav">' .
306 Linker::link( $this->getPageTitle(), $this->msg( 'allpages' )->escaped(),
307 array(), array(), 'known' ) .
308 "</td>
309 </tr>" .
310 Xml::closeElement( 'table' );
311 } else {
312 $out2 = $nsForm;
313 }
314 }
315 $output->addHTML( $out2 . $out );
316 }
317
318 /**
319 * Show a line of "ABC to DEF" ranges of articles
320 *
321 * @param string $inpoint Lower limit of pagenames
322 * @param string $outpoint Upper limit of pagenames
323 * @param int $namespace (Default NS_MAIN)
324 * @param bool $hideRedirects Don't show redirects. Default: false
325 * @return string
326 */
327 function showline( $inpoint, $outpoint, $namespace = NS_MAIN, $hideRedirects = false ) {
328 // Use content language since page titles are considered to use content language
329 global $wgContLang;
330
331 $inpointf = str_replace( '_', ' ', $inpoint );
332 $outpointf = str_replace( '_', ' ', $outpoint );
333
334 // Don't let the length runaway
335 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength );
336 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength );
337
338 $queryParams = array(
339 'from' => $inpoint,
340 'to' => $outpoint,
341 );
342
343 if ( $namespace ) {
344 $queryParams['namespace'] = $namespace;
345 }
346 if ( $hideRedirects ) {
347 $queryParams['hideredirects'] = 1;
348 }
349
350 $url = $this->getPageTitle()->getLocalURL( $queryParams );
351 $inlink = Html::element( 'a', array( 'href' => $url ), $inpointf );
352 $outlink = Html::element( 'a', array( 'href' => $url ), $outpointf );
353
354 $out = $this->msg( 'alphaindexline' )->rawParams(
355 "$inlink</td><td>",
356 "</td><td>$outlink"
357 )->escaped();
358
359 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
360 }
361
362 /**
363 * @param int $namespace Namespace (Default NS_MAIN)
364 * @param string $from List all pages from this name (default FALSE)
365 * @param string $to List all pages to this name (default FALSE)
366 * @param bool $hideredirects Dont show redirects (default FALSE)
367 */
368 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
369 $output = $this->getOutput();
370
371 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
372 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
373 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
374 $n = 0;
375
376 if ( !$fromList || !$toList ) {
377 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
378 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
379 // Show errormessage and reset to NS_MAIN
380 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
381 $namespace = NS_MAIN;
382 } else {
383 list( $namespace, $fromKey, $from ) = $fromList;
384 list( , $toKey, $to ) = $toList;
385
386 $dbr = wfGetDB( DB_SLAVE );
387 $conds = array(
388 'page_namespace' => $namespace,
389 'page_title >= ' . $dbr->addQuotes( $fromKey )
390 );
391
392 if ( $hideredirects ) {
393 $conds['page_is_redirect'] = 0;
394 }
395
396 if ( $toKey !== "" ) {
397 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
398 }
399
400 $res = $dbr->select( 'page',
401 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
402 $conds,
403 __METHOD__,
404 array(
405 'ORDER BY' => 'page_title',
406 'LIMIT' => $this->maxPerPage + 1,
407 'USE INDEX' => 'name_title',
408 )
409 );
410
411 if ( $res->numRows() > 0 ) {
412 $out = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
413 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
414 $t = Title::newFromRow( $s );
415 if ( $t ) {
416 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
417 Linker::link( $t ) .
418 ( $s->page_is_redirect ? '</div>' : '' );
419 } else {
420 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
421 }
422
423 if ( $n % 3 == 0 ) {
424 $out .= '<tr>';
425 }
426
427 $out .= "<td style=\"width:33%\">$link</td>";
428 $n++;
429 if ( $n % 3 == 0 ) {
430 $out .= "</tr>\n";
431 }
432 }
433
434 if ( ( $n % 3 ) != 0 ) {
435 $out .= "</tr>\n";
436 }
437 $out .= Xml::closeElement( 'table' );
438 } else {
439 $out = '';
440 }
441 }
442
443 if ( $this->including() ) {
444 $out2 = '';
445 } else {
446 if ( $from == '' ) {
447 // First chunk; no previous link.
448 $prevTitle = null;
449 } else {
450 # Get the last title from previous chunk
451 $dbr = wfGetDB( DB_SLAVE );
452 $res_prev = $dbr->select(
453 'page',
454 'page_title',
455 array( 'page_namespace' => $namespace, 'page_title < ' . $dbr->addQuotes( $from ) ),
456 __METHOD__,
457 array( 'ORDER BY' => 'page_title DESC',
458 'LIMIT' => $this->maxPerPage, 'OFFSET' => ( $this->maxPerPage - 1 )
459 )
460 );
461
462 # Get first title of previous complete chunk
463 if ( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
464 $pt = $dbr->fetchObject( $res_prev );
465 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
466 } else {
467 # The previous chunk is not complete, need to link to the very first title
468 # available in the database
469 $options = array( 'LIMIT' => 1 );
470 if ( !$dbr->implicitOrderby() ) {
471 $options['ORDER BY'] = 'page_title';
472 }
473 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
474 array( 'page_namespace' => $namespace ), __METHOD__, $options );
475 # Show the previous link if it s not the current requested chunk
476 if ( $from != $reallyFirstPage_title ) {
477 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
478 } else {
479 $prevTitle = null;
480 }
481 }
482 }
483
484 $self = $this->getPageTitle();
485
486 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
487 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
488 '<tr>
489 <td>' .
490 $nsForm .
491 '</td>
492 <td class="mw-allpages-nav">' .
493 Linker::link( $self, $this->msg( 'allpages' )->escaped() );
494
495 # Do we put a previous link ?
496 if ( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
497 $query = array( 'from' => $prevTitle->getText() );
498
499 if ( $namespace ) {
500 $query['namespace'] = $namespace;
501 }
502
503 if ( $hideredirects ) {
504 $query['hideredirects'] = $hideredirects;
505 }
506
507 $prevLink = Linker::linkKnown(
508 $self,
509 $this->msg( 'prevpage', $pt )->escaped(),
510 array(),
511 $query
512 );
513 $out2 = $this->getLanguage()->pipeList( array( $out2, $prevLink ) );
514 }
515
516 if ( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
517 # $s is the first link of the next chunk
518 $t = Title::makeTitle( $namespace, $s->page_title );
519 $query = array( 'from' => $t->getText() );
520
521 if ( $namespace ) {
522 $query['namespace'] = $namespace;
523 }
524
525 if ( $hideredirects ) {
526 $query['hideredirects'] = $hideredirects;
527 }
528
529 $nextLink = Linker::linkKnown(
530 $self,
531 $this->msg( 'nextpage', $t->getText() )->escaped(),
532 array(),
533 $query
534 );
535 $out2 = $this->getLanguage()->pipeList( array( $out2, $nextLink ) );
536 }
537 $out2 .= "</td></tr></table>";
538 }
539
540 $output->addHTML( $out2 . $out );
541
542 $links = array();
543 if ( isset( $prevLink ) ) {
544 $links[] = $prevLink;
545 }
546
547 if ( isset( $nextLink ) ) {
548 $links[] = $nextLink;
549 }
550
551 if ( count( $links ) ) {
552 $output->addHTML(
553 Html::element( 'hr' ) .
554 Html::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
555 $this->getLanguage()->pipeList( $links )
556 )
557 );
558 }
559 }
560
561 /**
562 * @param int $ns The namespace of the article
563 * @param string $text The name of the article
564 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
565 */
566 protected function getNamespaceKeyAndText( $ns, $text ) {
567 if ( $text == '' ) {
568 # shortcut for common case
569 return array( $ns, '', '' );
570 }
571
572 $t = Title::makeTitleSafe( $ns, $text );
573 if ( $t && $t->isLocal() ) {
574 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
575 } elseif ( $t ) {
576 return null;
577 }
578
579 # try again, in case the problem was an empty pagename
580 $text = preg_replace( '/(#|$)/', 'X$1', $text );
581 $t = Title::makeTitleSafe( $ns, $text );
582 if ( $t && $t->isLocal() ) {
583 return array( $t->getNamespace(), '', '' );
584 } else {
585 return null;
586 }
587 }
588
589 protected function getGroupName() {
590 return 'pages';
591 }
592 }