Got rid of some global $wgContLang usage in SpecialAllpages
[lhc/web/wiklou.git] / includes / specials / SpecialAllpages.php
1 <?php
2 /**
3 * Implements Special:Allpages
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Implements Special:Allpages
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialAllpages extends IncludableSpecialPage {
30
31 /**
32 * Maximum number of pages to show on single subpage.
33 *
34 * @var int $maxPerPage
35 */
36 protected $maxPerPage = 345;
37
38 /**
39 * Maximum number of pages to show on single index subpage.
40 *
41 * @var int $maxLineCount
42 */
43 protected $maxLineCount = 100;
44
45 /**
46 * Maximum number of chars to show for an entry.
47 *
48 * @var int $maxPageLength
49 */
50 protected $maxPageLength = 70;
51
52 /**
53 * Determines, which message describes the input field 'nsfrom'.
54 *
55 * @var string $nsfromMsg
56 */
57 protected $nsfromMsg = 'allpagesfrom';
58
59 /**
60 * Constructor
61 *
62 * @param string $name name of the special page, as seen in links and URLs (default: 'Allpages')
63 */
64 function __construct( $name = 'Allpages' ) {
65 parent::__construct( $name );
66 }
67
68 /**
69 * Entry point : initialise variables and call subfunctions.
70 *
71 * @param string $par becomes "FOO" when called like Special:Allpages/FOO (default NULL)
72 */
73 function execute( $par ) {
74 $request = $this->getRequest();
75 $out = $this->getOutput();
76
77 $this->setHeaders();
78 $this->outputHeader();
79 $out->allowClickjacking();
80
81 # GET values
82 $from = $request->getVal( 'from', null );
83 $to = $request->getVal( 'to', null );
84 $namespace = $request->getInt( 'namespace' );
85 $hideredirects = $request->getBool( 'hideredirects', false );
86
87 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
88
89 $out->setPageTitle(
90 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) ) ?
91 $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
92 $this->msg( 'allarticles' )
93 );
94 $out->addModuleStyles( 'mediawiki.special' );
95
96 if ( $par !== null ) {
97 $this->showChunk( $namespace, $par, $to, $hideredirects );
98 } elseif ( $from !== null && $to === null ) {
99 $this->showChunk( $namespace, $from, $to, $hideredirects );
100 } else {
101 $this->showToplevel( $namespace, $from, $to, $hideredirects );
102 }
103 }
104
105 /**
106 * HTML for the top form
107 *
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)
112 * @return string
113 */
114 function namespaceForm( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
115 global $wgScript;
116 $t = $this->getTitle();
117
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' ) );
124 $out .= "<tr>
125 <td class='mw-label'>" .
126 Xml::label( $this->msg( 'allpagesfrom' )->text(), 'nsfrom' ) .
127 " </td>
128 <td class='mw-input'>" .
129 Xml::input( 'from', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
130 " </td>
131 </tr>
132 <tr>
133 <td class='mw-label'>" .
134 Xml::label( $this->msg( 'allpagesto' )->text(), 'nsto' ) .
135 " </td>
136 <td class='mw-input'>" .
137 Xml::input( 'to', 30, str_replace( '_', ' ', $to ), array( 'id' => 'nsto' ) ) .
138 " </td>
139 </tr>
140 <tr>
141 <td class='mw-label'>" .
142 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
143 " </td>
144 <td class='mw-input'>" .
145 Html::namespaceSelector(
146 array( 'selected' => $namespace ),
147 array( 'name' => 'namespace', 'id' => 'namespace' )
148 ) . ' ' .
149 Xml::checkLabel(
150 $this->msg( 'allpages-hide-redirects' )->text(),
151 'hideredirects',
152 'hideredirects',
153 $hideredirects
154 ) . ' ' .
155 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
156 " </td>
157 </tr>";
158 $out .= Xml::closeElement( 'table' );
159 $out .= Xml::closeElement( 'fieldset' );
160 $out .= Xml::closeElement( 'form' );
161 $out .= Xml::closeElement( 'div' );
162 return $out;
163 }
164
165 /**
166 * @param $namespace Integer (default NS_MAIN)
167 * @param string $from list all pages from this name
168 * @param string $to list all pages to this name
169 * @param bool $hideredirects dont show redirects (default FALSE)
170 */
171 function showToplevel( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
172 $output = $this->getOutput();
173
174 # TODO: Either make this *much* faster or cache the title index points
175 # in the querycache table.
176
177 $dbr = wfGetDB( DB_SLAVE );
178 $out = "";
179 $where = array( 'page_namespace' => $namespace );
180
181 if ( $hideredirects ) {
182 $where['page_is_redirect'] = 0;
183 }
184
185 $from = Title::makeTitleSafe( $namespace, $from );
186 $to = Title::makeTitleSafe( $namespace, $to );
187 $from = ( $from && $from->isLocal() ) ? $from->getDBkey() : null;
188 $to = ( $to && $to->isLocal() ) ? $to->getDBkey() : null;
189
190 if ( isset( $from ) ) {
191 $where[] = 'page_title >= ' . $dbr->addQuotes( $from );
192 }
193
194 if ( isset( $to ) ) {
195 $where[] = 'page_title <= ' . $dbr->addQuotes( $to );
196 }
197
198 global $wgMemc;
199 $key = wfMemcKey( 'allpages', 'ns', $namespace, sha1( $from ), sha1( $to ) );
200 $lines = $wgMemc->get( $key );
201
202 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__ );
203 $maxPerSubpage = intval( $count / $this->maxLineCount );
204 $maxPerSubpage = max( $maxPerSubpage, $this->maxPerPage );
205
206 if ( !is_array( $lines ) ) {
207 $options = array( 'LIMIT' => 1 );
208 $options['ORDER BY'] = 'page_title ASC';
209 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__, $options );
210 $lastTitle = $firstTitle;
211 # This array is going to hold the page_titles in order.
212 $lines = array( $firstTitle );
213 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
214 $done = false;
215 while ( !$done ) {
216 // Fetch the last title of this chunk and the first of the next
217 $chunk = ( $lastTitle === false )
218 ? array()
219 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
220 $res = $dbr->select( 'page', /* FROM */
221 'page_title', /* WHAT */
222 array_merge( $where, $chunk ),
223 __METHOD__,
224 array( 'LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC' )
225 );
226
227 $s = $dbr->fetchObject( $res );
228 if ( $s ) {
229 array_push( $lines, $s->page_title );
230 } else {
231 // Final chunk, but ended prematurely. Go back and find the end.
232 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
233 array_merge( $where, $chunk ),
234 __METHOD__ );
235 array_push( $lines, $endTitle );
236 $done = true;
237 }
238
239 $s = $res->fetchObject();
240 if ( $s ) {
241 array_push( $lines, $s->page_title );
242 $lastTitle = $s->page_title;
243 } else {
244 // This was a final chunk and ended exactly at the limit.
245 // Rare but convenient!
246 $done = true;
247 }
248 $res->free();
249 }
250 $wgMemc->add( $key, $lines, 3600 );
251 }
252
253 // If there are only two or less sections, don't even display them.
254 // Instead, display the first section directly.
255 if ( count( $lines ) <= 2 ) {
256 if ( !empty( $lines ) ) {
257 $this->showChunk( $namespace, $from, $to, $hideredirects );
258 } else {
259 $output->addHTML( $this->namespaceForm( $namespace, $from, $to, $hideredirects ) );
260 }
261 return;
262 }
263
264 # At this point, $lines should contain an even number of elements.
265 $out .= Xml::openElement( 'table', array( 'class' => 'allpageslist' ) );
266 while ( count( $lines ) > 0 ) {
267 $inpoint = array_shift( $lines );
268 $outpoint = array_shift( $lines );
269 $out .= $this->showline( $inpoint, $outpoint, $namespace, $hideredirects );
270 }
271 $out .= Xml::closeElement( 'table' );
272 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
273
274 # Is there more?
275 if ( $this->including() ) {
276 $out2 = '';
277 } else {
278 if ( isset( $from ) || isset( $to ) ) {
279 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
280 '<tr>
281 <td>' .
282 $nsForm .
283 '</td>
284 <td class="mw-allpages-nav">' .
285 Linker::link( $this->getTitle(), $this->msg( 'allpages' )->escaped(),
286 array(), array(), 'known' ) .
287 "</td>
288 </tr>" .
289 Xml::closeElement( 'table' );
290 } else {
291 $out2 = $nsForm;
292 }
293 }
294 $output->addHTML( $out2 . $out );
295 }
296
297 /**
298 * Show a line of "ABC to DEF" ranges of articles
299 *
300 * @param string $inpoint lower limit of pagenames
301 * @param string $outpoint upper limit of pagenames
302 * @param $namespace Integer (Default NS_MAIN)
303 * @param bool $hideRedirects don't show redirects. Default: false
304 * @return string
305 */
306 function showline( $inpoint, $outpoint, $namespace = NS_MAIN, $hideRedirects = false ) {
307 // Use content language since page titles are considered to use content language
308 global $wgContLang;
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 );
314
315 $queryParams = array(
316 'from' => $inpoint,
317 'to' => $outpoint,
318 );
319
320 if( $namespace ) {
321 $queryParams['namespace'] = $namespace;
322 }
323 if ( $hideRedirects ) {
324 $queryParams['hideredirects'] = 1;
325 }
326
327 $link = htmlspecialchars(
328 $this->getTitle()->getLocalURL( $queryParams ) );
329
330 $out = $this->msg( 'alphaindexline' )->rawParams(
331 "<a href=\"$link\">$inpointf</a></td><td>",
332 "</td><td><a href=\"$link\">$outpointf</a>"
333 )->escaped();
334 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
335 }
336
337 /**
338 * @param int $namespace Namespace (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)
342 */
343 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
344 $output = $this->getOutput();
345
346 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
347 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
348 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
349 $n = 0;
350
351 if ( !$fromList || !$toList ) {
352 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
353 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
354 // Show errormessage and reset to NS_MAIN
355 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
356 $namespace = NS_MAIN;
357 } else {
358 list( $namespace, $fromKey, $from ) = $fromList;
359 list( , $toKey, $to ) = $toList;
360
361 $dbr = wfGetDB( DB_SLAVE );
362 $conds = array(
363 'page_namespace' => $namespace,
364 'page_title >= ' . $dbr->addQuotes( $fromKey )
365 );
366
367 if ( $hideredirects ) {
368 $conds['page_is_redirect'] = 0;
369 }
370
371 if ( $toKey !== "" ) {
372 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
373 }
374
375 $res = $dbr->select( 'page',
376 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
377 $conds,
378 __METHOD__,
379 array(
380 'ORDER BY' => 'page_title',
381 'LIMIT' => $this->maxPerPage + 1,
382 'USE INDEX' => 'name_title',
383 )
384 );
385
386 if ( $res->numRows() > 0 ) {
387 $out = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
388 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
389 $t = Title::newFromRow( $s );
390 if ( $t ) {
391 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
392 Linker::link( $t ) .
393 ( $s->page_is_redirect ? '</div>' : '' );
394 } else {
395 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
396 }
397
398 if ( $n % 3 == 0 ) {
399 $out .= '<tr>';
400 }
401
402 $out .= "<td style=\"width:33%\">$link</td>";
403 $n++;
404 if ( $n % 3 == 0 ) {
405 $out .= "</tr>\n";
406 }
407 }
408
409 if ( ( $n % 3 ) != 0 ) {
410 $out .= "</tr>\n";
411 }
412 $out .= Xml::closeElement( 'table' );
413 } else {
414 $out = '';
415 }
416 }
417
418 if ( $this->including() ) {
419 $out2 = '';
420 } else {
421 if ( $from == '' ) {
422 // First chunk; no previous link.
423 $prevTitle = null;
424 } else {
425 # Get the last title from previous chunk
426 $dbr = wfGetDB( DB_SLAVE );
427 $res_prev = $dbr->select(
428 'page',
429 'page_title',
430 array( 'page_namespace' => $namespace, 'page_title < ' . $dbr->addQuotes( $from ) ),
431 __METHOD__,
432 array( 'ORDER BY' => 'page_title DESC',
433 'LIMIT' => $this->maxPerPage, 'OFFSET' => ( $this->maxPerPage - 1 )
434 )
435 );
436
437 # Get first title of previous complete chunk
438 if ( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
439 $pt = $dbr->fetchObject( $res_prev );
440 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
441 } else {
442 # The previous chunk is not complete, need to link to the very first title
443 # available in the database
444 $options = array( 'LIMIT' => 1 );
445 if ( !$dbr->implicitOrderby() ) {
446 $options['ORDER BY'] = 'page_title';
447 }
448 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
449 array( 'page_namespace' => $namespace ), __METHOD__, $options );
450 # Show the previous link if it s not the current requested chunk
451 if ( $from != $reallyFirstPage_title ) {
452 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
453 } else {
454 $prevTitle = null;
455 }
456 }
457 }
458
459 $self = $this->getTitle();
460
461 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
462 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
463 '<tr>
464 <td>' .
465 $nsForm .
466 '</td>
467 <td class="mw-allpages-nav">' .
468 Linker::link( $self, $this->msg( 'allpages' )->escaped() );
469
470 # Do we put a previous link ?
471 if ( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
472 $query = array( 'from' => $prevTitle->getText() );
473
474 if ( $namespace ) {
475 $query['namespace'] = $namespace;
476 }
477
478 if ( $hideredirects ) {
479 $query['hideredirects'] = $hideredirects;
480 }
481
482 $prevLink = Linker::linkKnown(
483 $self,
484 $this->msg( 'prevpage', $pt )->escaped(),
485 array(),
486 $query
487 );
488 $out2 = $this->getLanguage()->pipeList( array( $out2, $prevLink ) );
489 }
490
491 if ( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
492 # $s is the first link of the next chunk
493 $t = Title::makeTitle( $namespace, $s->page_title );
494 $query = array( 'from' => $t->getText() );
495
496 if ( $namespace ) {
497 $query['namespace'] = $namespace;
498 }
499
500 if ( $hideredirects ) {
501 $query['hideredirects'] = $hideredirects;
502 }
503
504 $nextLink = Linker::linkKnown(
505 $self,
506 $this->msg( 'nextpage', $t->getText() )->escaped(),
507 array(),
508 $query
509 );
510 $out2 = $this->getLanguage()->pipeList( array( $out2, $nextLink ) );
511 }
512 $out2 .= "</td></tr></table>";
513 }
514
515 $output->addHTML( $out2 . $out );
516
517 $links = array();
518 if ( isset( $prevLink ) ) {
519 $links[] = $prevLink;
520 }
521
522 if ( isset( $nextLink ) ) {
523 $links[] = $nextLink;
524 }
525
526 if ( count( $links ) ) {
527 $output->addHTML(
528 Html::element( 'hr' ) .
529 Html::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
530 $this->getLanguage()->pipeList( $links )
531 )
532 );
533 }
534
535 }
536
537 /**
538 * @param $ns Integer: the namespace of the article
539 * @param string $text the name of the article
540 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
541 */
542 protected function getNamespaceKeyAndText( $ns, $text ) {
543 if ( $text == '' ) {
544 # shortcut for common case
545 return array( $ns, '', '' );
546 }
547
548 $t = Title::makeTitleSafe( $ns, $text );
549 if ( $t && $t->isLocal() ) {
550 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
551 } elseif ( $t ) {
552 return null;
553 }
554
555 # try again, in case the problem was an empty pagename
556 $text = preg_replace( '/(#|$)/', 'X$1', $text );
557 $t = Title::makeTitleSafe( $ns, $text );
558 if ( $t && $t->isLocal() ) {
559 return array( $t->getNamespace(), '', '' );
560 } else {
561 return null;
562 }
563 }
564
565 protected function getGroupName() {
566 return 'pages';
567 }
568 }