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 protected $maxPerPage = 345;
37 * Maximum number of pages to show on single index subpage.
39 protected $maxLineCount = 100;
42 * Maximum number of chars to show for an entry.
44 protected $maxPageLength = 70;
47 * Determines, which message describes the input field 'nsfrom'.
49 protected $nsfromMsg = 'allpagesfrom';
51 function __construct( $name = 'Allpages' ){
52 parent
::__construct( $name );
56 * Entry point : initialise variables and call subfunctions.
58 * @param $par String: becomes "FOO" when called like Special:Allpages/FOO (default NULL)
60 function execute( $par ) {
62 $request = $this->getRequest();
63 $out = $this->getOutput();
66 $this->outputHeader();
67 $out->allowClickjacking();
70 $from = $request->getVal( 'from', null );
71 $to = $request->getVal( 'to', null );
72 $namespace = $request->getInt( 'namespace' );
74 $namespaces = $wgContLang->getNamespaces();
77 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces) ) ) ?
78 $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
79 $this->msg( 'allarticles' )
81 $out->addModuleStyles( 'mediawiki.special' );
84 $this->showChunk( $namespace, $par, $to );
85 } elseif( $from !== null && $to === null ) {
86 $this->showChunk( $namespace, $from, $to );
88 $this->showToplevel( $namespace, $from, $to );
93 * HTML for the top form
95 * @param $namespace Integer: a namespace constant (default NS_MAIN).
96 * @param $from String: dbKey we are starting listing at.
97 * @param $to String: dbKey we are ending listing at.
100 function namespaceForm( $namespace = NS_MAIN
, $from = '', $to = '' ) {
102 $t = $this->getTitle();
104 $out = Xml
::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
105 $out .= Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
106 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
107 $out .= Xml
::openElement( 'fieldset' );
108 $out .= Xml
::element( 'legend', null, $this->msg( 'allpages' )->text() );
109 $out .= Xml
::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
111 <td class='mw-label'>" .
112 Xml
::label( $this->msg( 'allpagesfrom' )->text(), 'nsfrom' ) .
114 <td class='mw-input'>" .
115 Xml
::input( 'from', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
119 <td class='mw-label'>" .
120 Xml
::label( $this->msg( 'allpagesto' )->text(), 'nsto' ) .
122 <td class='mw-input'>" .
123 Xml
::input( 'to', 30, str_replace('_',' ',$to), array( 'id' => 'nsto' ) ) .
127 <td class='mw-label'>" .
128 Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
130 <td class='mw-input'>" .
131 Html
::namespaceSelector(
132 array( 'selected' => $namespace ),
133 array( 'name' => 'namespace', 'id' => 'namespace' )
135 Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
138 $out .= Xml
::closeElement( 'table' );
139 $out .= Xml
::closeElement( 'fieldset' );
140 $out .= Xml
::closeElement( 'form' );
141 $out .= Xml
::closeElement( 'div' );
146 * @param $namespace Integer (default NS_MAIN)
147 * @param $from String: list all pages from this name
148 * @param $to String: list all pages to this name
150 function showToplevel( $namespace = NS_MAIN
, $from = '', $to = '' ) {
151 $output = $this->getOutput();
153 # TODO: Either make this *much* faster or cache the title index points
154 # in the querycache table.
156 $dbr = wfGetDB( DB_SLAVE
);
158 $where = array( 'page_namespace' => $namespace );
160 $from = Title
::makeTitleSafe( $namespace, $from );
161 $to = Title
::makeTitleSafe( $namespace, $to );
162 $from = ( $from && $from->isLocal() ) ?
$from->getDBkey() : null;
163 $to = ( $to && $to->isLocal() ) ?
$to->getDBkey() : null;
166 $where[] = 'page_title >= '.$dbr->addQuotes( $from );
168 $where[] = 'page_title <= '.$dbr->addQuotes( $to );
171 $key = wfMemcKey( 'allpages', 'ns', $namespace, $from, $to );
172 $lines = $wgMemc->get( $key );
174 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__
);
175 $maxPerSubpage = intval($count/$this->maxLineCount
);
176 $maxPerSubpage = max($maxPerSubpage,$this->maxPerPage
);
178 if( !is_array( $lines ) ) {
179 $options = array( 'LIMIT' => 1 );
180 $options['ORDER BY'] = 'page_title ASC';
181 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__
, $options );
182 $lastTitle = $firstTitle;
183 # This array is going to hold the page_titles in order.
184 $lines = array( $firstTitle );
185 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
188 // Fetch the last title of this chunk and the first of the next
189 $chunk = ( $lastTitle === false )
191 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
192 $res = $dbr->select( 'page', /* FROM */
193 'page_title', /* WHAT */
194 array_merge($where,$chunk),
196 array ('LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC')
199 $s = $dbr->fetchObject( $res );
201 array_push( $lines, $s->page_title
);
203 // Final chunk, but ended prematurely. Go back and find the end.
204 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
205 array_merge($where,$chunk),
207 array_push( $lines, $endTitle );
210 $s = $res->fetchObject();
212 array_push( $lines, $s->page_title
);
213 $lastTitle = $s->page_title
;
215 // This was a final chunk and ended exactly at the limit.
216 // Rare but convenient!
221 $wgMemc->add( $key, $lines, 3600 );
224 // If there are only two or less sections, don't even display them.
225 // Instead, display the first section directly.
226 if( count( $lines ) <= 2 ) {
227 if( !empty($lines) ) {
228 $this->showChunk( $namespace, $from, $to );
230 $output->addHTML( $this->namespaceForm( $namespace, $from, $to ) );
235 # At this point, $lines should contain an even number of elements.
236 $out .= Xml
::openElement( 'table', array( 'class' => 'allpageslist' ) );
237 while( count ( $lines ) > 0 ) {
238 $inpoint = array_shift( $lines );
239 $outpoint = array_shift( $lines );
240 $out .= $this->showline( $inpoint, $outpoint, $namespace );
242 $out .= Xml
::closeElement( 'table' );
243 $nsForm = $this->namespaceForm( $namespace, $from, $to );
246 if( $this->including() ) {
249 if( isset($from) ||
isset($to) ) {
250 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
255 <td class="mw-allpages-nav">' .
256 Linker
::link( $this->getTitle(), $this->msg( 'allpages' )->escaped(),
257 array(), array(), 'known' ) .
260 Xml
::closeElement( 'table' );
265 $output->addHTML( $out2 . $out );
269 * Show a line of "ABC to DEF" ranges of articles
271 * @param $inpoint String: lower limit of pagenames
272 * @param $outpoint String: upper limit of pagenames
273 * @param $namespace Integer (Default NS_MAIN)
276 function showline( $inpoint, $outpoint, $namespace = NS_MAIN
) {
278 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
279 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
280 // Don't let the length runaway
281 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength
);
282 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength
);
284 $queryparams = $namespace ?
"namespace=$namespace&" : '';
285 $special = $this->getTitle();
286 $link = htmlspecialchars( $special->getLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint) ) );
288 $out = $this->msg( 'alphaindexline' )->rawParams(
289 "<a href=\"$link\">$inpointf</a></td><td>",
290 "</td><td><a href=\"$link\">$outpointf</a>"
292 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
296 * @param $namespace Integer (Default NS_MAIN)
297 * @param $from String: list all pages from this name (default FALSE)
298 * @param $to String: list all pages to this name (default FALSE)
300 function showChunk( $namespace = NS_MAIN
, $from = false, $to = false ) {
302 $output = $this->getOutput();
304 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
305 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
306 $namespaces = $wgContLang->getNamespaces();
309 if ( !$fromList ||
!$toList ) {
310 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
311 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
312 // Show errormessage and reset to NS_MAIN
313 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
314 $namespace = NS_MAIN
;
316 list( $namespace, $fromKey, $from ) = $fromList;
317 list( , $toKey, $to ) = $toList;
319 $dbr = wfGetDB( DB_SLAVE
);
321 'page_namespace' => $namespace,
322 'page_title >= ' . $dbr->addQuotes( $fromKey )
324 if( $toKey !== "" ) {
325 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
328 $res = $dbr->select( 'page',
329 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
333 'ORDER BY' => 'page_title',
334 'LIMIT' => $this->maxPerPage +
1,
335 'USE INDEX' => 'name_title',
339 if( $res->numRows() > 0 ) {
340 $out = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
341 while( ( $n < $this->maxPerPage
) && ( $s = $res->fetchObject() ) ) {
342 $t = Title
::newFromRow( $s );
344 $link = ( $s->page_is_redirect ?
'<div class="allpagesredirect">' : '' ) .
346 ($s->page_is_redirect ?
'</div>' : '' );
348 $link = '[[' . htmlspecialchars( $s->page_title
) . ']]';
353 $out .= "<td style=\"width:33%\">$link</td>";
359 if( ($n %
3) != 0 ) {
362 $out .= Xml
::closeElement( 'table' );
368 if ( $this->including() ) {
372 // First chunk; no previous link.
375 # Get the last title from previous chunk
376 $dbr = wfGetDB( DB_SLAVE
);
377 $res_prev = $dbr->select(
380 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
382 array( 'ORDER BY' => 'page_title DESC',
383 'LIMIT' => $this->maxPerPage
, 'OFFSET' => ($this->maxPerPage
- 1 )
387 # Get first title of previous complete chunk
388 if( $dbr->numrows( $res_prev ) >= $this->maxPerPage
) {
389 $pt = $dbr->fetchObject( $res_prev );
390 $prevTitle = Title
::makeTitle( $namespace, $pt->page_title
);
392 # The previous chunk is not complete, need to link to the very first title
393 # available in the database
394 $options = array( 'LIMIT' => 1 );
395 if ( ! $dbr->implicitOrderby() ) {
396 $options['ORDER BY'] = 'page_title';
398 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
399 array( 'page_namespace' => $namespace ), __METHOD__
, $options );
400 # Show the previous link if it s not the current requested chunk
401 if( $from != $reallyFirstPage_title ) {
402 $prevTitle = Title
::makeTitle( $namespace, $reallyFirstPage_title );
409 $self = $this->getTitle();
411 $nsForm = $this->namespaceForm( $namespace, $from, $to );
412 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
417 <td class="mw-allpages-nav">' .
418 Linker
::link( $self, $this->msg( 'allpages' )->escaped() );
420 # Do we put a previous link ?
421 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
422 $query = array( 'from' => $prevTitle->getText() );
425 $query['namespace'] = $namespace;
427 $prevLink = Linker
::linkKnown(
429 $this->msg( 'prevpage', $pt )->escaped(),
433 $out2 = $this->getLanguage()->pipeList( array( $out2, $prevLink ) );
436 if( $n == $this->maxPerPage
&& $s = $res->fetchObject() ) {
437 # $s is the first link of the next chunk
438 $t = Title
::MakeTitle($namespace, $s->page_title
);
439 $query = array( 'from' => $t->getText() );
442 $query['namespace'] = $namespace;
444 $nextLink = Linker
::linkKnown(
446 $this->msg( 'nextpage', $t->getText() )->escaped(),
450 $out2 = $this->getLanguage()->pipeList( array( $out2, $nextLink ) );
452 $out2 .= "</td></tr></table>";
455 $output->addHTML( $out2 . $out );
458 if ( isset( $prevLink ) ) $links[] = $prevLink;
459 if ( isset( $nextLink ) ) $links[] = $nextLink;
461 if ( count( $links ) ) {
463 Html
::element( 'hr' ) .
464 Html
::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
465 $this->getLanguage()->pipeList( $links )
472 * @param $ns Integer: the namespace of the article
473 * @param $text String: the name of the article
474 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
476 protected function getNamespaceKeyAndText($ns, $text) {
478 return array( $ns, '', '' ); # shortcut for common case
480 $t = Title
::makeTitleSafe($ns, $text);
481 if ( $t && $t->isLocal() ) {
482 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
487 # try again, in case the problem was an empty pagename
488 $text = preg_replace('/(#|$)/', 'X$1', $text);
489 $t = Title
::makeTitleSafe($ns, $text);
490 if ( $t && $t->isLocal() ) {
491 return array( $t->getNamespace(), '', '' );