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 * Determines, which message describes the input field 'nsfrom'.
55 * @var string $nsfromMsg
57 protected $nsfromMsg = 'allpagesfrom';
62 * @param string $name name of the special page, as seen in links and URLs (default: 'Allpages')
64 function __construct( $name = 'Allpages' ) {
65 parent
::__construct( $name );
69 * Entry point : initialise variables and call subfunctions.
71 * @param string $par becomes "FOO" when called like Special:Allpages/FOO (default NULL)
73 function execute( $par ) {
74 $request = $this->getRequest();
75 $out = $this->getOutput();
78 $this->outputHeader();
79 $out->allowClickjacking();
82 $from = $request->getVal( 'from', null );
83 $to = $request->getVal( 'to', null );
84 $namespace = $request->getInt( 'namespace' );
85 $hideredirects = $request->getBool( 'hideredirects', false );
87 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
90 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) ) ?
91 $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
92 $this->msg( 'allarticles' )
94 $out->addModuleStyles( 'mediawiki.special' );
96 if ( $par !== null ) {
97 $this->showChunk( $namespace, $par, $to, $hideredirects );
98 } elseif ( $from !== null && $to === null ) {
99 $this->showChunk( $namespace, $from, $to, $hideredirects );
101 $this->showToplevel( $namespace, $from, $to, $hideredirects );
106 * HTML for the top form
108 * @param $namespace Integer: a namespace constant (default NS_MAIN).
109 * @param string $from dbKey we are starting listing at.
110 * @param string $to dbKey we are ending listing at.
111 * @param bool $hideredirects dont show redirects (default FALSE)
114 function namespaceForm( $namespace = NS_MAIN
, $from = '', $to = '', $hideredirects = false ) {
116 $t = $this->getTitle();
118 $out = Xml
::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
119 $out .= Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
120 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
121 $out .= Xml
::openElement( 'fieldset' );
122 $out .= Xml
::element( 'legend', null, $this->msg( 'allpages' )->text() );
123 $out .= Xml
::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
125 <td class='mw-label'>" .
126 Xml
::label( $this->msg( 'allpagesfrom' )->text(), 'nsfrom' ) .
128 <td class='mw-input'>" .
129 Xml
::input( 'from', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
133 <td class='mw-label'>" .
134 Xml
::label( $this->msg( 'allpagesto' )->text(), 'nsto' ) .
136 <td class='mw-input'>" .
137 Xml
::input( 'to', 30, str_replace( '_', ' ', $to ), array( 'id' => 'nsto' ) ) .
141 <td class='mw-label'>" .
142 Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
144 <td class='mw-input'>" .
145 Html
::namespaceSelector(
146 array( 'selected' => $namespace ),
147 array( 'name' => 'namespace', 'id' => 'namespace' )
150 $this->msg( 'allpages-hide-redirects' )->text(),
155 Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
158 $out .= Xml
::closeElement( 'table' );
159 $out .= Xml
::closeElement( 'fieldset' );
160 $out .= Xml
::closeElement( 'form' );
161 $out .= Xml
::closeElement( 'div' );
167 * @param $namespace Integer (default NS_MAIN)
168 * @param string $from list all pages from this name
169 * @param string $to list all pages to this name
170 * @param bool $hideredirects dont show redirects (default FALSE)
172 function showToplevel( $namespace = NS_MAIN
, $from = '', $to = '', $hideredirects = false ) {
173 $output = $this->getOutput();
175 # TODO: Either make this *much* faster or cache the title index points
176 # in the querycache table.
178 $dbr = wfGetDB( DB_SLAVE
);
180 $where = array( 'page_namespace' => $namespace );
182 if ( $hideredirects ) {
183 $where['page_is_redirect'] = 0;
186 $from = Title
::makeTitleSafe( $namespace, $from );
187 $to = Title
::makeTitleSafe( $namespace, $to );
188 $from = ( $from && $from->isLocal() ) ?
$from->getDBkey() : null;
189 $to = ( $to && $to->isLocal() ) ?
$to->getDBkey() : null;
191 if ( isset( $from ) ) {
192 $where[] = 'page_title >= ' . $dbr->addQuotes( $from );
195 if ( isset( $to ) ) {
196 $where[] = 'page_title <= ' . $dbr->addQuotes( $to );
200 $key = wfMemcKey( 'allpages', 'ns', $namespace, sha1( $from ), sha1( $to ) );
201 $lines = $wgMemc->get( $key );
203 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__
);
204 $maxPerSubpage = intval( $count / $this->maxLineCount
);
205 $maxPerSubpage = max( $maxPerSubpage, $this->maxPerPage
);
207 if ( !is_array( $lines ) ) {
208 $options = array( 'LIMIT' => 1 );
209 $options['ORDER BY'] = 'page_title ASC';
210 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__
, $options );
211 $lastTitle = $firstTitle;
212 # This array is going to hold the page_titles in order.
213 $lines = array( $firstTitle );
214 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
217 // Fetch the last title of this chunk and the first of the next
218 $chunk = ( $lastTitle === false )
220 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
221 $res = $dbr->select( 'page', /* FROM */
222 'page_title', /* WHAT */
223 array_merge( $where, $chunk ),
225 array( 'LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC' )
228 $s = $dbr->fetchObject( $res );
230 array_push( $lines, $s->page_title
);
232 // Final chunk, but ended prematurely. Go back and find the end.
233 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
234 array_merge( $where, $chunk ),
236 array_push( $lines, $endTitle );
240 $s = $res->fetchObject();
242 array_push( $lines, $s->page_title
);
243 $lastTitle = $s->page_title
;
245 // This was a final chunk and ended exactly at the limit.
246 // Rare but convenient!
251 $wgMemc->add( $key, $lines, 3600 );
254 // If there are only two or less sections, don't even display them.
255 // Instead, display the first section directly.
256 if ( count( $lines ) <= 2 ) {
257 if ( !empty( $lines ) ) {
258 $this->showChunk( $namespace, $from, $to, $hideredirects );
260 $output->addHTML( $this->namespaceForm( $namespace, $from, $to, $hideredirects ) );
266 # At this point, $lines should contain an even number of elements.
267 $out .= Xml
::openElement( 'table', array( 'class' => 'allpageslist' ) );
268 while ( count( $lines ) > 0 ) {
269 $inpoint = array_shift( $lines );
270 $outpoint = array_shift( $lines );
271 $out .= $this->showline( $inpoint, $outpoint, $namespace, $hideredirects );
273 $out .= Xml
::closeElement( 'table' );
274 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
277 if ( $this->including() ) {
280 if ( isset( $from ) ||
isset( $to ) ) {
281 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
286 <td class="mw-allpages-nav">' .
287 Linker
::link( $this->getTitle(), $this->msg( 'allpages' )->escaped(),
288 array(), array(), 'known' ) .
291 Xml
::closeElement( 'table' );
296 $output->addHTML( $out2 . $out );
300 * Show a line of "ABC to DEF" ranges of articles
302 * @param string $inpoint lower limit of pagenames
303 * @param string $outpoint upper limit of pagenames
304 * @param $namespace Integer (Default NS_MAIN)
305 * @param bool $hideRedirects don't show redirects. Default: false
308 function showline( $inpoint, $outpoint, $namespace = NS_MAIN
, $hideRedirects = false ) {
309 // Use content language since page titles are considered to use content language
311 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
312 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
313 // Don't let the length runaway
314 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength
);
315 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength
);
317 $queryParams = array(
323 $queryParams['namespace'] = $namespace;
325 if ( $hideRedirects ) {
326 $queryParams['hideredirects'] = 1;
329 $link = htmlspecialchars(
330 $this->getTitle()->getLocalURL( $queryParams ) );
332 $out = $this->msg( 'alphaindexline' )->rawParams(
333 "<a href=\"$link\">$inpointf</a></td><td>",
334 "</td><td><a href=\"$link\">$outpointf</a>"
337 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
341 * @param int $namespace Namespace (Default NS_MAIN)
342 * @param string $from list all pages from this name (default FALSE)
343 * @param string $to list all pages to this name (default FALSE)
344 * @param bool $hideredirects dont show redirects (default FALSE)
346 function showChunk( $namespace = NS_MAIN
, $from = false, $to = false, $hideredirects = false ) {
347 $output = $this->getOutput();
349 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
350 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
351 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
354 if ( !$fromList ||
!$toList ) {
355 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
356 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
357 // Show errormessage and reset to NS_MAIN
358 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
359 $namespace = NS_MAIN
;
361 list( $namespace, $fromKey, $from ) = $fromList;
362 list( , $toKey, $to ) = $toList;
364 $dbr = wfGetDB( DB_SLAVE
);
366 'page_namespace' => $namespace,
367 'page_title >= ' . $dbr->addQuotes( $fromKey )
370 if ( $hideredirects ) {
371 $conds['page_is_redirect'] = 0;
374 if ( $toKey !== "" ) {
375 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
378 $res = $dbr->select( 'page',
379 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
383 'ORDER BY' => 'page_title',
384 'LIMIT' => $this->maxPerPage +
1,
385 'USE INDEX' => 'name_title',
389 if ( $res->numRows() > 0 ) {
390 $out = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
391 while ( ( $n < $this->maxPerPage
) && ( $s = $res->fetchObject() ) ) {
392 $t = Title
::newFromRow( $s );
394 $link = ( $s->page_is_redirect ?
'<div class="allpagesredirect">' : '' ) .
396 ( $s->page_is_redirect ?
'</div>' : '' );
398 $link = '[[' . htmlspecialchars( $s->page_title
) . ']]';
405 $out .= "<td style=\"width:33%\">$link</td>";
412 if ( ( $n %
3 ) != 0 ) {
415 $out .= Xml
::closeElement( 'table' );
421 if ( $this->including() ) {
425 // First chunk; no previous link.
428 # Get the last title from previous chunk
429 $dbr = wfGetDB( DB_SLAVE
);
430 $res_prev = $dbr->select(
433 array( 'page_namespace' => $namespace, 'page_title < ' . $dbr->addQuotes( $from ) ),
435 array( 'ORDER BY' => 'page_title DESC',
436 'LIMIT' => $this->maxPerPage
, 'OFFSET' => ( $this->maxPerPage
- 1 )
440 # Get first title of previous complete chunk
441 if ( $dbr->numrows( $res_prev ) >= $this->maxPerPage
) {
442 $pt = $dbr->fetchObject( $res_prev );
443 $prevTitle = Title
::makeTitle( $namespace, $pt->page_title
);
445 # The previous chunk is not complete, need to link to the very first title
446 # available in the database
447 $options = array( 'LIMIT' => 1 );
448 if ( !$dbr->implicitOrderby() ) {
449 $options['ORDER BY'] = 'page_title';
451 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
452 array( 'page_namespace' => $namespace ), __METHOD__
, $options );
453 # Show the previous link if it s not the current requested chunk
454 if ( $from != $reallyFirstPage_title ) {
455 $prevTitle = Title
::makeTitle( $namespace, $reallyFirstPage_title );
462 $self = $this->getTitle();
464 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
465 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
470 <td class="mw-allpages-nav">' .
471 Linker
::link( $self, $this->msg( 'allpages' )->escaped() );
473 # Do we put a previous link ?
474 if ( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
475 $query = array( 'from' => $prevTitle->getText() );
478 $query['namespace'] = $namespace;
481 if ( $hideredirects ) {
482 $query['hideredirects'] = $hideredirects;
485 $prevLink = Linker
::linkKnown(
487 $this->msg( 'prevpage', $pt )->escaped(),
491 $out2 = $this->getLanguage()->pipeList( array( $out2, $prevLink ) );
494 if ( $n == $this->maxPerPage
&& $s = $res->fetchObject() ) {
495 # $s is the first link of the next chunk
496 $t = Title
::makeTitle( $namespace, $s->page_title
);
497 $query = array( 'from' => $t->getText() );
500 $query['namespace'] = $namespace;
503 if ( $hideredirects ) {
504 $query['hideredirects'] = $hideredirects;
507 $nextLink = Linker
::linkKnown(
509 $this->msg( 'nextpage', $t->getText() )->escaped(),
513 $out2 = $this->getLanguage()->pipeList( array( $out2, $nextLink ) );
515 $out2 .= "</td></tr></table>";
518 $output->addHTML( $out2 . $out );
521 if ( isset( $prevLink ) ) {
522 $links[] = $prevLink;
525 if ( isset( $nextLink ) ) {
526 $links[] = $nextLink;
529 if ( count( $links ) ) {
531 Html
::element( 'hr' ) .
532 Html
::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
533 $this->getLanguage()->pipeList( $links )
540 * @param $ns Integer: the namespace of the article
541 * @param string $text the name of the article
542 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
544 protected function getNamespaceKeyAndText( $ns, $text ) {
546 # shortcut for common case
547 return array( $ns, '', '' );
550 $t = Title
::makeTitleSafe( $ns, $text );
551 if ( $t && $t->isLocal() ) {
552 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
557 # try again, in case the problem was an empty pagename
558 $text = preg_replace( '/(#|$)/', 'X$1', $text );
559 $t = Title
::makeTitleSafe( $ns, $text );
560 if ( $t && $t->isLocal() ) {
561 return array( $t->getNamespace(), '', '' );
567 protected function getGroupName() {