Add a way for packagers to override some installation details
[mediawiki.git] / includes / specials / SpecialAllpages.php
blob0f8b25570ae42a7e6ff840e758fa06b6dff86e7e
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Implements Special:Allpages
27 * @ingroup SpecialPage
29 class SpecialAllpages extends IncludableSpecialPage {
31 /**
32 * Maximum number of pages to show on single subpage.
34 * @var int $maxPerPage
36 protected $maxPerPage = 345;
38 /**
39 * Maximum number of pages to show on single index subpage.
41 * @var int $maxLineCount
43 protected $maxLineCount = 100;
45 /**
46 * Maximum number of chars to show for an entry.
48 * @var int $maxPageLength
50 protected $maxPageLength = 70;
52 /**
53 * Determines, which message describes the input field 'nsfrom'.
55 * @var string $nsfromMsg
57 protected $nsfromMsg = 'allpagesfrom';
59 /**
60 * Constructor
62 * @param $name string: name of the special page, as seen in links and URLs (default: 'Allpages')
64 function __construct( $name = 'Allpages' ){
65 parent::__construct( $name );
68 /**
69 * Entry point : initialise variables and call subfunctions.
71 * @param $par String: becomes "FOO" when called like Special:Allpages/FOO (default NULL)
73 function execute( $par ) {
74 global $wgContLang;
75 $request = $this->getRequest();
76 $out = $this->getOutput();
78 $this->setHeaders();
79 $this->outputHeader();
80 $out->allowClickjacking();
82 # GET values
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();
90 $out->setPageTitle(
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 );
101 } else {
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 $from String: dbKey we are starting listing at.
111 * @param $to String: dbKey we are ending listing at.
112 * @param $hideredirects Bool: dont show redirects (default FALSE)
113 * @return string
115 function namespaceForm( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
116 global $wgScript;
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' ) );
125 $out .= "<tr>
126 <td class='mw-label'>" .
127 Xml::label( $this->msg( 'allpagesfrom' )->text(), 'nsfrom' ) .
128 " </td>
129 <td class='mw-input'>" .
130 Xml::input( 'from', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
131 " </td>
132 </tr>
133 <tr>
134 <td class='mw-label'>" .
135 Xml::label( $this->msg( 'allpagesto' )->text(), 'nsto' ) .
136 " </td>
137 <td class='mw-input'>" .
138 Xml::input( 'to', 30, str_replace('_',' ',$to), array( 'id' => 'nsto' ) ) .
139 " </td>
140 </tr>
141 <tr>
142 <td class='mw-label'>" .
143 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
144 " </td>
145 <td class='mw-input'>" .
146 Html::namespaceSelector(
147 array( 'selected' => $namespace ),
148 array( 'name' => 'namespace', 'id' => 'namespace' )
149 ) . ' ' .
150 Xml::checkLabel(
151 $this->msg( 'allpages-hide-redirects' )->text(),
152 'hideredirects',
153 'hideredirects',
154 $hideredirects
155 ) . ' ' .
156 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
157 " </td>
158 </tr>";
159 $out .= Xml::closeElement( 'table' );
160 $out .= Xml::closeElement( 'fieldset' );
161 $out .= Xml::closeElement( 'form' );
162 $out .= Xml::closeElement( 'div' );
163 return $out;
167 * @param $namespace Integer (default NS_MAIN)
168 * @param $from String: list all pages from this name
169 * @param $to String: list all pages to this name
170 * @param $hideredirects Bool: 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 );
179 $out = "";
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 );
193 if( isset($to) )
194 $where[] = 'page_title <= '.$dbr->addQuotes( $to );
196 global $wgMemc;
197 $key = wfMemcKey( 'allpages', 'ns', $namespace, $from, $to );
198 $lines = $wgMemc->get( $key );
200 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__ );
201 $maxPerSubpage = intval($count/$this->maxLineCount);
202 $maxPerSubpage = max($maxPerSubpage,$this->maxPerPage);
204 if( !is_array( $lines ) ) {
205 $options = array( 'LIMIT' => 1 );
206 $options['ORDER BY'] = 'page_title ASC';
207 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__, $options );
208 $lastTitle = $firstTitle;
209 # This array is going to hold the page_titles in order.
210 $lines = array( $firstTitle );
211 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
212 $done = false;
213 while( !$done ) {
214 // Fetch the last title of this chunk and the first of the next
215 $chunk = ( $lastTitle === false )
216 ? array()
217 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
218 $res = $dbr->select( 'page', /* FROM */
219 'page_title', /* WHAT */
220 array_merge($where,$chunk),
221 __METHOD__,
222 array ('LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC')
225 $s = $dbr->fetchObject( $res );
226 if( $s ) {
227 array_push( $lines, $s->page_title );
228 } else {
229 // Final chunk, but ended prematurely. Go back and find the end.
230 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
231 array_merge($where,$chunk),
232 __METHOD__ );
233 array_push( $lines, $endTitle );
234 $done = true;
236 $s = $res->fetchObject();
237 if( $s ) {
238 array_push( $lines, $s->page_title );
239 $lastTitle = $s->page_title;
240 } else {
241 // This was a final chunk and ended exactly at the limit.
242 // Rare but convenient!
243 $done = true;
245 $res->free();
247 $wgMemc->add( $key, $lines, 3600 );
250 // If there are only two or less sections, don't even display them.
251 // Instead, display the first section directly.
252 if( count( $lines ) <= 2 ) {
253 if( !empty($lines) ) {
254 $this->showChunk( $namespace, $from, $to, $hideredirects );
255 } else {
256 $output->addHTML( $this->namespaceForm( $namespace, $from, $to, $hideredirects ) );
258 return;
261 # At this point, $lines should contain an even number of elements.
262 $out .= Xml::openElement( 'table', array( 'class' => 'allpageslist' ) );
263 while( count ( $lines ) > 0 ) {
264 $inpoint = array_shift( $lines );
265 $outpoint = array_shift( $lines );
266 $out .= $this->showline( $inpoint, $outpoint, $namespace, $hideredirects );
268 $out .= Xml::closeElement( 'table' );
269 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
271 # Is there more?
272 if( $this->including() ) {
273 $out2 = '';
274 } else {
275 if( isset($from) || isset($to) ) {
276 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
277 '<tr>
278 <td>' .
279 $nsForm .
280 '</td>
281 <td class="mw-allpages-nav">' .
282 Linker::link( $this->getTitle(), $this->msg( 'allpages' )->escaped(),
283 array(), array(), 'known' ) .
284 "</td>
285 </tr>" .
286 Xml::closeElement( 'table' );
287 } else {
288 $out2 = $nsForm;
291 $output->addHTML( $out2 . $out );
295 * Show a line of "ABC to DEF" ranges of articles
297 * @param $inpoint String: lower limit of pagenames
298 * @param $outpoint String: upper limit of pagenames
299 * @param $namespace Integer (Default NS_MAIN)
300 * @param $hideredirects Bool: dont show redirects (default FALSE)
301 * @return string
303 function showline( $inpoint, $outpoint, $namespace = NS_MAIN, $hideredirects ) {
304 global $wgContLang;
305 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
306 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
307 // Don't let the length runaway
308 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength );
309 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength );
311 $queryparams = $namespace ? "namespace=$namespace&" : '';
313 $queryhideredirects = array();
314 if ($hideredirects) {
315 $queryhideredirects[ 'hideredirects' ] = 1;
318 $special = $this->getTitle();
319 $link = htmlspecialchars( $special->getLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint), $queryhideredirects ) );
321 $out = $this->msg( 'alphaindexline' )->rawParams(
322 "<a href=\"$link\">$inpointf</a></td><td>",
323 "</td><td><a href=\"$link\">$outpointf</a>"
324 )->escaped();
325 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
329 * @param $namespace Integer (Default NS_MAIN)
330 * @param $from String: list all pages from this name (default FALSE)
331 * @param $to String: list all pages to this name (default FALSE)
332 * @param $hideredirects Bool: dont show redirects (default FALSE)
334 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
335 global $wgContLang;
336 $output = $this->getOutput();
338 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
339 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
340 $namespaces = $wgContLang->getNamespaces();
341 $n = 0;
343 if ( !$fromList || !$toList ) {
344 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
345 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
346 // Show errormessage and reset to NS_MAIN
347 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
348 $namespace = NS_MAIN;
349 } else {
350 list( $namespace, $fromKey, $from ) = $fromList;
351 list( , $toKey, $to ) = $toList;
353 $dbr = wfGetDB( DB_SLAVE );
354 $conds = array(
355 'page_namespace' => $namespace,
356 'page_title >= ' . $dbr->addQuotes( $fromKey )
359 if ( $hideredirects ) {
360 $conds[ 'page_is_redirect' ] = 0;
363 if( $toKey !== "" ) {
364 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
367 $res = $dbr->select( 'page',
368 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
369 $conds,
370 __METHOD__,
371 array(
372 'ORDER BY' => 'page_title',
373 'LIMIT' => $this->maxPerPage + 1,
374 'USE INDEX' => 'name_title',
378 if( $res->numRows() > 0 ) {
379 $out = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
380 while( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
381 $t = Title::newFromRow( $s );
382 if( $t ) {
383 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
384 Linker::link( $t ) .
385 ($s->page_is_redirect ? '</div>' : '' );
386 } else {
387 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
389 if( $n % 3 == 0 ) {
390 $out .= '<tr>';
392 $out .= "<td style=\"width:33%\">$link</td>";
393 $n++;
394 if( $n % 3 == 0 ) {
395 $out .= "</tr>\n";
398 if( ($n % 3) != 0 ) {
399 $out .= "</tr>\n";
401 $out .= Xml::closeElement( 'table' );
402 } else {
403 $out = '';
407 if ( $this->including() ) {
408 $out2 = '';
409 } else {
410 if( $from == '' ) {
411 // First chunk; no previous link.
412 $prevTitle = null;
413 } else {
414 # Get the last title from previous chunk
415 $dbr = wfGetDB( DB_SLAVE );
416 $res_prev = $dbr->select(
417 'page',
418 'page_title',
419 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
420 __METHOD__,
421 array( 'ORDER BY' => 'page_title DESC',
422 'LIMIT' => $this->maxPerPage, 'OFFSET' => ($this->maxPerPage - 1 )
426 # Get first title of previous complete chunk
427 if( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
428 $pt = $dbr->fetchObject( $res_prev );
429 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
430 } else {
431 # The previous chunk is not complete, need to link to the very first title
432 # available in the database
433 $options = array( 'LIMIT' => 1 );
434 if ( ! $dbr->implicitOrderby() ) {
435 $options['ORDER BY'] = 'page_title';
437 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
438 array( 'page_namespace' => $namespace ), __METHOD__, $options );
439 # Show the previous link if it s not the current requested chunk
440 if( $from != $reallyFirstPage_title ) {
441 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
442 } else {
443 $prevTitle = null;
448 $self = $this->getTitle();
450 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
451 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
452 '<tr>
453 <td>' .
454 $nsForm .
455 '</td>
456 <td class="mw-allpages-nav">' .
457 Linker::link( $self, $this->msg( 'allpages' )->escaped() );
459 # Do we put a previous link ?
460 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
461 $query = array( 'from' => $prevTitle->getText() );
463 if( $namespace )
464 $query['namespace'] = $namespace;
466 if( $hideredirects )
467 $query['hideredirects'] = $hideredirects;
469 $prevLink = Linker::linkKnown(
470 $self,
471 $this->msg( 'prevpage', $pt )->escaped(),
472 array(),
473 $query
475 $out2 = $this->getLanguage()->pipeList( array( $out2, $prevLink ) );
478 if( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
479 # $s is the first link of the next chunk
480 $t = Title::makeTitle($namespace, $s->page_title);
481 $query = array( 'from' => $t->getText() );
483 if( $namespace )
484 $query['namespace'] = $namespace;
486 if( $hideredirects )
487 $query['hideredirects'] = $hideredirects;
489 $nextLink = Linker::linkKnown(
490 $self,
491 $this->msg( 'nextpage', $t->getText() )->escaped(),
492 array(),
493 $query
495 $out2 = $this->getLanguage()->pipeList( array( $out2, $nextLink ) );
497 $out2 .= "</td></tr></table>";
500 $output->addHTML( $out2 . $out );
502 $links = array();
503 if ( isset( $prevLink ) ) $links[] = $prevLink;
504 if ( isset( $nextLink ) ) $links[] = $nextLink;
506 if ( count( $links ) ) {
507 $output->addHTML(
508 Html::element( 'hr' ) .
509 Html::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
510 $this->getLanguage()->pipeList( $links )
511 ) );
517 * @param $ns Integer: the namespace of the article
518 * @param $text String: the name of the article
519 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
521 protected function getNamespaceKeyAndText($ns, $text) {
522 if ( $text == '' )
523 return array( $ns, '', '' ); # shortcut for common case
525 $t = Title::makeTitleSafe($ns, $text);
526 if ( $t && $t->isLocal() ) {
527 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
528 } elseif ( $t ) {
529 return null;
532 # try again, in case the problem was an empty pagename
533 $text = preg_replace('/(#|$)/', 'X$1', $text);
534 $t = Title::makeTitleSafe($ns, $text);
535 if ( $t && $t->isLocal() ) {
536 return array( $t->getNamespace(), '', '' );
537 } else {
538 return null;