3 * Implements Special:Allpages
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.
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.
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
21 * @ingroup SpecialPage
25 * Implements Special:Allpages
27 * @ingroup SpecialPage
29 class SpecialAllpages
extends IncludableSpecialPage
{
32 * Maximum number of pages to show on single subpage.
34 * @var int $maxPerPage
36 protected $maxPerPage = 345;
39 * Maximum number of pages to show on single index subpage.
41 * @var int $maxLineCount
43 protected $maxLineCount = 100;
46 * Maximum number of chars to show for an entry.
48 * @var int $maxPageLength
50 protected $maxPageLength = 70;
53 * Maximum number of pages in a hierarchical ("top level") list.
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.
60 protected $maxTopLevelPages = 50000;
63 * Determines, which message describes the input field 'nsfrom'.
65 * @var string $nsfromMsg
67 protected $nsfromMsg = 'allpagesfrom';
72 * @param string $name name of the special page, as seen in links and URLs (default: 'Allpages')
74 function __construct( $name = 'Allpages' ) {
75 parent
::__construct( $name );
79 * Entry point : initialise variables and call subfunctions.
81 * @param string $par becomes "FOO" when called like Special:Allpages/FOO (default NULL)
83 function execute( $par ) {
84 $request = $this->getRequest();
85 $out = $this->getOutput();
88 $this->outputHeader();
89 $out->allowClickjacking();
92 $from = $request->getVal( 'from', null );
93 $to = $request->getVal( 'to', null );
94 $namespace = $request->getInt( 'namespace' );
95 $hideredirects = $request->getBool( 'hideredirects', false );
97 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
100 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) ) ?
101 $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
102 $this->msg( 'allarticles' )
104 $out->addModuleStyles( 'mediawiki.special' );
106 if ( $par !== null ) {
107 $this->showChunk( $namespace, $par, $to, $hideredirects );
108 } elseif ( $from !== null && $to === null ) {
109 $this->showChunk( $namespace, $from, $to, $hideredirects );
111 $this->showToplevel( $namespace, $from, $to, $hideredirects );
116 * HTML for the top form
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)
124 function namespaceForm( $namespace = NS_MAIN
, $from = '', $to = '', $hideredirects = false ) {
126 $t = $this->getTitle();
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' ) );
135 <td class='mw-label'>" .
136 Xml
::label( $this->msg( 'allpagesfrom' )->text(), 'nsfrom' ) .
138 <td class='mw-input'>" .
139 Xml
::input( 'from', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
143 <td class='mw-label'>" .
144 Xml
::label( $this->msg( 'allpagesto' )->text(), 'nsto' ) .
146 <td class='mw-input'>" .
147 Xml
::input( 'to', 30, str_replace( '_', ' ', $to ), array( 'id' => 'nsto' ) ) .
151 <td class='mw-label'>" .
152 Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
154 <td class='mw-input'>" .
155 Html
::namespaceSelector(
156 array( 'selected' => $namespace ),
157 array( 'name' => 'namespace', 'id' => 'namespace' )
160 $this->msg( 'allpages-hide-redirects' )->text(),
165 Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
168 $out .= Xml
::closeElement( 'table' );
169 $out .= Xml
::closeElement( 'fieldset' );
170 $out .= Xml
::closeElement( 'form' );
171 $out .= Xml
::closeElement( 'div' );
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)
182 function showToplevel( $namespace = NS_MAIN
, $from = '', $to = '', $hideredirects = false ) {
183 $output = $this->getOutput();
185 # TODO: Either make this *much* faster or cache the title index points
186 # in the querycache table.
188 $dbr = wfGetDB( DB_SLAVE
);
190 $where = array( 'page_namespace' => $namespace );
192 if ( $hideredirects ) {
193 $where['page_is_redirect'] = 0;
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;
201 if ( isset( $from ) ) {
202 $where[] = 'page_title >= ' . $dbr->addQuotes( $from );
205 if ( isset( $to ) ) {
206 $where[] = 'page_title <= ' . $dbr->addQuotes( $to );
210 $key = wfMemcKey( 'allpages', 'ns', $namespace, sha1( $from ), sha1( $to ) );
211 $lines = $wgMemc->get( $key );
213 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__
);
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 );
222 $maxPerSubpage = intval( $count / $this->maxLineCount
);
223 $maxPerSubpage = max( $maxPerSubpage, $this->maxPerPage
);
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.
235 // Fetch the last title of this chunk and the first of the next
236 $chunk = ( $lastTitle === false )
238 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
239 $res = $dbr->select( 'page', /* FROM */
240 'page_title', /* WHAT */
241 array_merge( $where, $chunk ),
243 array( 'LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC' )
246 $s = $dbr->fetchObject( $res );
248 array_push( $lines, $s->page_title
);
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 ),
254 array_push( $lines, $endTitle );
258 $s = $res->fetchObject();
260 array_push( $lines, $s->page_title
);
261 $lastTitle = $s->page_title
;
263 // This was a final chunk and ended exactly at the limit.
264 // Rare but convenient!
269 $wgMemc->add( $key, $lines, 3600 );
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 );
278 $output->addHTML( $this->namespaceForm( $namespace, $from, $to, $hideredirects ) );
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 );
291 $out .= Xml
::closeElement( 'table' );
292 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
295 if ( $this->including() ) {
298 if ( isset( $from ) ||
isset( $to ) ) {
299 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
304 <td class="mw-allpages-nav">' .
305 Linker
::link( $this->getTitle(), $this->msg( 'allpages' )->escaped(),
306 array(), array(), 'known' ) .
309 Xml
::closeElement( 'table' );
314 $output->addHTML( $out2 . $out );
318 * Show a line of "ABC to DEF" ranges of articles
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
326 function showline( $inpoint, $outpoint, $namespace = NS_MAIN
, $hideRedirects = false ) {
327 // Use content language since page titles are considered to use content language
330 $inpointf = str_replace( '_', ' ', $inpoint );
331 $outpointf = str_replace( '_', ' ', $outpoint );
333 // Don't let the length runaway
334 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength
);
335 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength
);
337 $queryParams = array(
343 $queryParams['namespace'] = $namespace;
345 if ( $hideRedirects ) {
346 $queryParams['hideredirects'] = 1;
349 $url = $this->getTitle()->getLocalURL( $queryParams );
350 $inlink = Html
::element( 'a', array( 'href' => $url ), $inpointf );
351 $outlink = Html
::element( 'a', array( 'href' => $url ), $outpointf );
353 $out = $this->msg( 'alphaindexline' )->rawParams(
358 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
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)
367 function showChunk( $namespace = NS_MAIN
, $from = false, $to = false, $hideredirects = false ) {
368 $output = $this->getOutput();
370 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
371 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
372 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
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
;
382 list( $namespace, $fromKey, $from ) = $fromList;
383 list( , $toKey, $to ) = $toList;
385 $dbr = wfGetDB( DB_SLAVE
);
387 'page_namespace' => $namespace,
388 'page_title >= ' . $dbr->addQuotes( $fromKey )
391 if ( $hideredirects ) {
392 $conds['page_is_redirect'] = 0;
395 if ( $toKey !== "" ) {
396 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
399 $res = $dbr->select( 'page',
400 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
404 'ORDER BY' => 'page_title',
405 'LIMIT' => $this->maxPerPage +
1,
406 'USE INDEX' => 'name_title',
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 );
415 $link = ( $s->page_is_redirect ?
'<div class="allpagesredirect">' : '' ) .
417 ( $s->page_is_redirect ?
'</div>' : '' );
419 $link = '[[' . htmlspecialchars( $s->page_title
) . ']]';
426 $out .= "<td style=\"width:33%\">$link</td>";
433 if ( ( $n %
3 ) != 0 ) {
436 $out .= Xml
::closeElement( 'table' );
442 if ( $this->including() ) {
446 // First chunk; no previous link.
449 # Get the last title from previous chunk
450 $dbr = wfGetDB( DB_SLAVE
);
451 $res_prev = $dbr->select(
454 array( 'page_namespace' => $namespace, 'page_title < ' . $dbr->addQuotes( $from ) ),
456 array( 'ORDER BY' => 'page_title DESC',
457 'LIMIT' => $this->maxPerPage
, 'OFFSET' => ( $this->maxPerPage
- 1 )
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
);
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';
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 );
483 $self = $this->getTitle();
485 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
486 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
491 <td class="mw-allpages-nav">' .
492 Linker
::link( $self, $this->msg( 'allpages' )->escaped() );
494 # Do we put a previous link ?
495 if ( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
496 $query = array( 'from' => $prevTitle->getText() );
499 $query['namespace'] = $namespace;
502 if ( $hideredirects ) {
503 $query['hideredirects'] = $hideredirects;
506 $prevLink = Linker
::linkKnown(
508 $this->msg( 'prevpage', $pt )->escaped(),
512 $out2 = $this->getLanguage()->pipeList( array( $out2, $prevLink ) );
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() );
521 $query['namespace'] = $namespace;
524 if ( $hideredirects ) {
525 $query['hideredirects'] = $hideredirects;
528 $nextLink = Linker
::linkKnown(
530 $this->msg( 'nextpage', $t->getText() )->escaped(),
534 $out2 = $this->getLanguage()->pipeList( array( $out2, $nextLink ) );
536 $out2 .= "</td></tr></table>";
539 $output->addHTML( $out2 . $out );
542 if ( isset( $prevLink ) ) {
543 $links[] = $prevLink;
546 if ( isset( $nextLink ) ) {
547 $links[] = $nextLink;
550 if ( count( $links ) ) {
552 Html
::element( 'hr' ) .
553 Html
::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
554 $this->getLanguage()->pipeList( $links )
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
565 protected function getNamespaceKeyAndText( $ns, $text ) {
567 # shortcut for common case
568 return array( $ns, '', '' );
571 $t = Title
::makeTitleSafe( $ns, $text );
572 if ( $t && $t->isLocal() ) {
573 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
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(), '', '' );
588 protected function getGroupName() {