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 ) {
75 $request = $this->getRequest();
76 $out = $this->getOutput();
79 $this->outputHeader();
80 $out->allowClickjacking();
83 $from = $request->getVal( 'from', null );
84 $to = $request->getVal( 'to', null );
85 $namespace = $request->getInt( 'namespace' );
86 $hideredirects = $request->getBool( 'hideredirects', false );
88 $namespaces = $wgContLang->getNamespaces();
91 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) ) ?
92 $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
93 $this->msg( 'allarticles' )
95 $out->addModuleStyles( 'mediawiki.special' );
97 if ( $par !== null ) {
98 $this->showChunk( $namespace, $par, $to, $hideredirects );
99 } elseif ( $from !== null && $to === null ) {
100 $this->showChunk( $namespace, $from, $to, $hideredirects );
102 $this->showToplevel( $namespace, $from, $to, $hideredirects );
107 * HTML for the top form
109 * @param $namespace Integer: a namespace constant (default NS_MAIN).
110 * @param string $from dbKey we are starting listing at.
111 * @param string $to dbKey we are ending listing at.
112 * @param bool $hideredirects dont show redirects (default FALSE)
115 function namespaceForm( $namespace = NS_MAIN
, $from = '', $to = '', $hideredirects = false ) {
117 $t = $this->getTitle();
119 $out = Xml
::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
120 $out .= Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
121 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
122 $out .= Xml
::openElement( 'fieldset' );
123 $out .= Xml
::element( 'legend', null, $this->msg( 'allpages' )->text() );
124 $out .= Xml
::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
126 <td class='mw-label'>" .
127 Xml
::label( $this->msg( 'allpagesfrom' )->text(), 'nsfrom' ) .
129 <td class='mw-input'>" .
130 Xml
::input( 'from', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
134 <td class='mw-label'>" .
135 Xml
::label( $this->msg( 'allpagesto' )->text(), 'nsto' ) .
137 <td class='mw-input'>" .
138 Xml
::input( 'to', 30, str_replace( '_', ' ', $to ), array( 'id' => 'nsto' ) ) .
142 <td class='mw-label'>" .
143 Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
145 <td class='mw-input'>" .
146 Html
::namespaceSelector(
147 array( 'selected' => $namespace ),
148 array( 'name' => 'namespace', 'id' => 'namespace' )
151 $this->msg( 'allpages-hide-redirects' )->text(),
156 Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
159 $out .= Xml
::closeElement( 'table' );
160 $out .= Xml
::closeElement( 'fieldset' );
161 $out .= Xml
::closeElement( 'form' );
162 $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 ) );
265 # At this point, $lines should contain an even number of elements.
266 $out .= Xml
::openElement( 'table', array( 'class' => 'allpageslist' ) );
267 while ( count( $lines ) > 0 ) {
268 $inpoint = array_shift( $lines );
269 $outpoint = array_shift( $lines );
270 $out .= $this->showline( $inpoint, $outpoint, $namespace, $hideredirects );
272 $out .= Xml
::closeElement( 'table' );
273 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
276 if ( $this->including() ) {
279 if ( isset( $from ) ||
isset( $to ) ) {
280 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
285 <td class="mw-allpages-nav">' .
286 Linker
::link( $this->getTitle(), $this->msg( 'allpages' )->escaped(),
287 array(), array(), 'known' ) .
290 Xml
::closeElement( 'table' );
295 $output->addHTML( $out2 . $out );
299 * Show a line of "ABC to DEF" ranges of articles
301 * @param string $inpoint lower limit of pagenames
302 * @param string $outpoint upper limit of pagenames
303 * @param $namespace Integer (Default NS_MAIN)
304 * @param bool $hideRedirects don't show redirects. Default: false
307 function showline( $inpoint, $outpoint, $namespace = NS_MAIN
, $hideRedirects = false ) {
309 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
310 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
311 // Don't let the length runaway
312 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength
);
313 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength
);
315 $queryParams = array (
321 $queryParams['namespace'] = $namespace;
323 if ( $hideRedirects ) {
324 $queryParams['hideredirects'] = 1;
327 $link = htmlspecialchars(
328 $this->getTitle()->getLocalURL( $queryParams ) );
330 $out = $this->msg( 'alphaindexline' )->rawParams(
331 "<a href=\"$link\">$inpointf</a></td><td>",
332 "</td><td><a href=\"$link\">$outpointf</a>"
334 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
338 * @param $namespace Integer (Default NS_MAIN)
339 * @param string $from list all pages from this name (default FALSE)
340 * @param string $to list all pages to this name (default FALSE)
341 * @param bool $hideredirects dont show redirects (default FALSE)
343 function showChunk( $namespace = NS_MAIN
, $from = false, $to = false, $hideredirects = false ) {
345 $output = $this->getOutput();
347 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
348 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
349 $namespaces = $wgContLang->getNamespaces();
352 if ( !$fromList ||
!$toList ) {
353 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
354 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
355 // Show errormessage and reset to NS_MAIN
356 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
357 $namespace = NS_MAIN
;
359 list( $namespace, $fromKey, $from ) = $fromList;
360 list( , $toKey, $to ) = $toList;
362 $dbr = wfGetDB( DB_SLAVE
);
364 'page_namespace' => $namespace,
365 'page_title >= ' . $dbr->addQuotes( $fromKey )
368 if ( $hideredirects ) {
369 $conds['page_is_redirect'] = 0;
372 if ( $toKey !== "" ) {
373 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
376 $res = $dbr->select( 'page',
377 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
381 'ORDER BY' => 'page_title',
382 'LIMIT' => $this->maxPerPage +
1,
383 'USE INDEX' => 'name_title',
387 if ( $res->numRows() > 0 ) {
388 $out = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
389 while ( ( $n < $this->maxPerPage
) && ( $s = $res->fetchObject() ) ) {
390 $t = Title
::newFromRow( $s );
392 $link = ( $s->page_is_redirect ?
'<div class="allpagesredirect">' : '' ) .
394 ( $s->page_is_redirect ?
'</div>' : '' );
396 $link = '[[' . htmlspecialchars( $s->page_title
) . ']]';
403 $out .= "<td style=\"width:33%\">$link</td>";
410 if ( ( $n %
3 ) != 0 ) {
413 $out .= Xml
::closeElement( 'table' );
419 if ( $this->including() ) {
423 // First chunk; no previous link.
426 # Get the last title from previous chunk
427 $dbr = wfGetDB( DB_SLAVE
);
428 $res_prev = $dbr->select(
431 array( 'page_namespace' => $namespace, 'page_title < ' . $dbr->addQuotes( $from ) ),
433 array( 'ORDER BY' => 'page_title DESC',
434 'LIMIT' => $this->maxPerPage
, 'OFFSET' => ( $this->maxPerPage
- 1 )
438 # Get first title of previous complete chunk
439 if ( $dbr->numrows( $res_prev ) >= $this->maxPerPage
) {
440 $pt = $dbr->fetchObject( $res_prev );
441 $prevTitle = Title
::makeTitle( $namespace, $pt->page_title
);
443 # The previous chunk is not complete, need to link to the very first title
444 # available in the database
445 $options = array( 'LIMIT' => 1 );
446 if ( !$dbr->implicitOrderby() ) {
447 $options['ORDER BY'] = 'page_title';
449 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
450 array( 'page_namespace' => $namespace ), __METHOD__
, $options );
451 # Show the previous link if it s not the current requested chunk
452 if ( $from != $reallyFirstPage_title ) {
453 $prevTitle = Title
::makeTitle( $namespace, $reallyFirstPage_title );
460 $self = $this->getTitle();
462 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
463 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
468 <td class="mw-allpages-nav">' .
469 Linker
::link( $self, $this->msg( 'allpages' )->escaped() );
471 # Do we put a previous link ?
472 if ( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
473 $query = array( 'from' => $prevTitle->getText() );
476 $query['namespace'] = $namespace;
479 if ( $hideredirects ) {
480 $query['hideredirects'] = $hideredirects;
483 $prevLink = Linker
::linkKnown(
485 $this->msg( 'prevpage', $pt )->escaped(),
489 $out2 = $this->getLanguage()->pipeList( array( $out2, $prevLink ) );
492 if ( $n == $this->maxPerPage
&& $s = $res->fetchObject() ) {
493 # $s is the first link of the next chunk
494 $t = Title
::makeTitle( $namespace, $s->page_title
);
495 $query = array( 'from' => $t->getText() );
498 $query['namespace'] = $namespace;
501 if ( $hideredirects ) {
502 $query['hideredirects'] = $hideredirects;
505 $nextLink = Linker
::linkKnown(
507 $this->msg( 'nextpage', $t->getText() )->escaped(),
511 $out2 = $this->getLanguage()->pipeList( array( $out2, $nextLink ) );
513 $out2 .= "</td></tr></table>";
516 $output->addHTML( $out2 . $out );
519 if ( isset( $prevLink ) ) {
520 $links[] = $prevLink;
523 if ( isset( $nextLink ) ) {
524 $links[] = $nextLink;
527 if ( count( $links ) ) {
529 Html
::element( 'hr' ) .
530 Html
::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
531 $this->getLanguage()->pipeList( $links )
539 * @param $ns Integer: the namespace of the article
540 * @param string $text the name of the article
541 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
543 protected function getNamespaceKeyAndText( $ns, $text ) {
545 # shortcut for common case
546 return array( $ns, '', '' );
549 $t = Title
::makeTitleSafe( $ns, $text );
550 if ( $t && $t->isLocal() ) {
551 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
556 # try again, in case the problem was an empty pagename
557 $text = preg_replace( '/(#|$)/', 'X$1', $text );
558 $t = Title
::makeTitleSafe( $ns, $text );
559 if ( $t && $t->isLocal() ) {
560 return array( $t->getNamespace(), '', '' );
566 protected function getGroupName() {