Merge "backport release notes from Ia6efdf931b0b6be7556fc16c040aa7493845cdfc"
[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 global $wgContLang;
75 $request = $this->getRequest();
76 $out = $this->getOutput();
77
78 $this->setHeaders();
79 $this->outputHeader();
80 $out->allowClickjacking();
81
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 );
87
88 $namespaces = $wgContLang->getNamespaces();
89
90 $out->setPageTitle(
91 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) ) ?
92 $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
93 $this->msg( 'allarticles' )
94 );
95 $out->addModuleStyles( 'mediawiki.special' );
96
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 );
103 }
104 }
105
106 /**
107 * HTML for the top form
108 *
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)
113 * @return string
114 */
115 function namespaceForm( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
116 global $wgScript;
117 $t = $this->getTitle();
118
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;
164 }
165
166 /**
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)
171 */
172 function showToplevel( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
173 $output = $this->getOutput();
174
175 # TODO: Either make this *much* faster or cache the title index points
176 # in the querycache table.
177
178 $dbr = wfGetDB( DB_SLAVE );
179 $out = "";
180 $where = array( 'page_namespace' => $namespace );
181
182 if ( $hideredirects ) {
183 $where['page_is_redirect'] = 0;
184 }
185
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;
190
191 if ( isset( $from ) ) {
192 $where[] = 'page_title >= ' . $dbr->addQuotes( $from );
193 }
194
195 if ( isset( $to ) ) {
196 $where[] = 'page_title <= ' . $dbr->addQuotes( $to );
197 }
198
199 global $wgMemc;
200 $key = wfMemcKey( 'allpages', 'ns', $namespace, sha1( $from ), sha1( $to ) );
201 $lines = $wgMemc->get( $key );
202
203 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__ );
204 $maxPerSubpage = intval( $count / $this->maxLineCount );
205 $maxPerSubpage = max( $maxPerSubpage, $this->maxPerPage );
206
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.
215 $done = false;
216 while ( !$done ) {
217 // Fetch the last title of this chunk and the first of the next
218 $chunk = ( $lastTitle === false )
219 ? array()
220 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
221 $res = $dbr->select( 'page', /* FROM */
222 'page_title', /* WHAT */
223 array_merge( $where, $chunk ),
224 __METHOD__,
225 array( 'LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC' )
226 );
227
228 $s = $dbr->fetchObject( $res );
229 if ( $s ) {
230 array_push( $lines, $s->page_title );
231 } else {
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 ),
235 __METHOD__ );
236 array_push( $lines, $endTitle );
237 $done = true;
238 }
239
240 $s = $res->fetchObject();
241 if ( $s ) {
242 array_push( $lines, $s->page_title );
243 $lastTitle = $s->page_title;
244 } else {
245 // This was a final chunk and ended exactly at the limit.
246 // Rare but convenient!
247 $done = true;
248 }
249 $res->free();
250 }
251 $wgMemc->add( $key, $lines, 3600 );
252 }
253
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 );
259 } else {
260 $output->addHTML( $this->namespaceForm( $namespace, $from, $to, $hideredirects ) );
261 }
262 return;
263 }
264
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 );
271 }
272 $out .= Xml::closeElement( 'table' );
273 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
274
275 # Is there more?
276 if ( $this->including() ) {
277 $out2 = '';
278 } else {
279 if ( isset( $from ) || isset( $to ) ) {
280 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
281 '<tr>
282 <td>' .
283 $nsForm .
284 '</td>
285 <td class="mw-allpages-nav">' .
286 Linker::link( $this->getTitle(), $this->msg( 'allpages' )->escaped(),
287 array(), array(), 'known' ) .
288 "</td>
289 </tr>" .
290 Xml::closeElement( 'table' );
291 } else {
292 $out2 = $nsForm;
293 }
294 }
295 $output->addHTML( $out2 . $out );
296 }
297
298 /**
299 * Show a line of "ABC to DEF" ranges of articles
300 *
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
305 * @return string
306 */
307 function showline( $inpoint, $outpoint, $namespace = NS_MAIN, $hideRedirects = false ) {
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 $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)
342 */
343 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
344 global $wgContLang;
345 $output = $this->getOutput();
346
347 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
348 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
349 $namespaces = $wgContLang->getNamespaces();
350 $n = 0;
351
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;
358 } else {
359 list( $namespace, $fromKey, $from ) = $fromList;
360 list( , $toKey, $to ) = $toList;
361
362 $dbr = wfGetDB( DB_SLAVE );
363 $conds = array(
364 'page_namespace' => $namespace,
365 'page_title >= ' . $dbr->addQuotes( $fromKey )
366 );
367
368 if ( $hideredirects ) {
369 $conds['page_is_redirect'] = 0;
370 }
371
372 if ( $toKey !== "" ) {
373 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
374 }
375
376 $res = $dbr->select( 'page',
377 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
378 $conds,
379 __METHOD__,
380 array(
381 'ORDER BY' => 'page_title',
382 'LIMIT' => $this->maxPerPage + 1,
383 'USE INDEX' => 'name_title',
384 )
385 );
386
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 );
391 if ( $t ) {
392 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
393 Linker::link( $t ) .
394 ( $s->page_is_redirect ? '</div>' : '' );
395 } else {
396 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
397 }
398
399 if ( $n % 3 == 0 ) {
400 $out .= '<tr>';
401 }
402
403 $out .= "<td style=\"width:33%\">$link</td>";
404 $n++;
405 if ( $n % 3 == 0 ) {
406 $out .= "</tr>\n";
407 }
408 }
409
410 if ( ( $n % 3 ) != 0 ) {
411 $out .= "</tr>\n";
412 }
413 $out .= Xml::closeElement( 'table' );
414 } else {
415 $out = '';
416 }
417 }
418
419 if ( $this->including() ) {
420 $out2 = '';
421 } else {
422 if ( $from == '' ) {
423 // First chunk; no previous link.
424 $prevTitle = null;
425 } else {
426 # Get the last title from previous chunk
427 $dbr = wfGetDB( DB_SLAVE );
428 $res_prev = $dbr->select(
429 'page',
430 'page_title',
431 array( 'page_namespace' => $namespace, 'page_title < ' . $dbr->addQuotes( $from ) ),
432 __METHOD__,
433 array( 'ORDER BY' => 'page_title DESC',
434 'LIMIT' => $this->maxPerPage, 'OFFSET' => ( $this->maxPerPage - 1 )
435 )
436 );
437
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 );
442 } else {
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';
448 }
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 );
454 } else {
455 $prevTitle = null;
456 }
457 }
458 }
459
460 $self = $this->getTitle();
461
462 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
463 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
464 '<tr>
465 <td>' .
466 $nsForm .
467 '</td>
468 <td class="mw-allpages-nav">' .
469 Linker::link( $self, $this->msg( 'allpages' )->escaped() );
470
471 # Do we put a previous link ?
472 if ( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
473 $query = array( 'from' => $prevTitle->getText() );
474
475 if ( $namespace ) {
476 $query['namespace'] = $namespace;
477 }
478
479 if ( $hideredirects ) {
480 $query['hideredirects'] = $hideredirects;
481 }
482
483 $prevLink = Linker::linkKnown(
484 $self,
485 $this->msg( 'prevpage', $pt )->escaped(),
486 array(),
487 $query
488 );
489 $out2 = $this->getLanguage()->pipeList( array( $out2, $prevLink ) );
490 }
491
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() );
496
497 if ( $namespace ) {
498 $query['namespace'] = $namespace;
499 }
500
501 if ( $hideredirects ) {
502 $query['hideredirects'] = $hideredirects;
503 }
504
505 $nextLink = Linker::linkKnown(
506 $self,
507 $this->msg( 'nextpage', $t->getText() )->escaped(),
508 array(),
509 $query
510 );
511 $out2 = $this->getLanguage()->pipeList( array( $out2, $nextLink ) );
512 }
513 $out2 .= "</td></tr></table>";
514 }
515
516 $output->addHTML( $out2 . $out );
517
518 $links = array();
519 if ( isset( $prevLink ) ) {
520 $links[] = $prevLink;
521 }
522
523 if ( isset( $nextLink ) ) {
524 $links[] = $nextLink;
525 }
526
527 if ( count( $links ) ) {
528 $output->addHTML(
529 Html::element( 'hr' ) .
530 Html::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
531 $this->getLanguage()->pipeList( $links )
532 )
533 );
534 }
535
536 }
537
538 /**
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
542 */
543 protected function getNamespaceKeyAndText( $ns, $text ) {
544 if ( $text == '' ) {
545 # shortcut for common case
546 return array( $ns, '', '' );
547 }
548
549 $t = Title::makeTitleSafe( $ns, $text );
550 if ( $t && $t->isLocal() ) {
551 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
552 } elseif ( $t ) {
553 return null;
554 }
555
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(), '', '' );
561 } else {
562 return null;
563 }
564 }
565
566 protected function getGroupName() {
567 return 'pages';
568 }
569 }