Merge "Set initial focus on some special pages"
[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
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
263 return;
264 }
265
266 # At this point, $lines should contain an even number of elements.
267 $out .= Xml::openElement( 'table', array( 'class' => 'allpageslist' ) );
268 while ( count( $lines ) > 0 ) {
269 $inpoint = array_shift( $lines );
270 $outpoint = array_shift( $lines );
271 $out .= $this->showline( $inpoint, $outpoint, $namespace, $hideredirects );
272 }
273 $out .= Xml::closeElement( 'table' );
274 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
275
276 # Is there more?
277 if ( $this->including() ) {
278 $out2 = '';
279 } else {
280 if ( isset( $from ) || isset( $to ) ) {
281 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
282 '<tr>
283 <td>' .
284 $nsForm .
285 '</td>
286 <td class="mw-allpages-nav">' .
287 Linker::link( $this->getTitle(), $this->msg( 'allpages' )->escaped(),
288 array(), array(), 'known' ) .
289 "</td>
290 </tr>" .
291 Xml::closeElement( 'table' );
292 } else {
293 $out2 = $nsForm;
294 }
295 }
296 $output->addHTML( $out2 . $out );
297 }
298
299 /**
300 * Show a line of "ABC to DEF" ranges of articles
301 *
302 * @param string $inpoint lower limit of pagenames
303 * @param string $outpoint upper limit of pagenames
304 * @param $namespace Integer (Default NS_MAIN)
305 * @param bool $hideRedirects don't show redirects. Default: false
306 * @return string
307 */
308 function showline( $inpoint, $outpoint, $namespace = NS_MAIN, $hideRedirects = false ) {
309 // Use content language since page titles are considered to use content language
310 global $wgContLang;
311
312 $inpointf = str_replace( '_', ' ', $inpoint );
313 $outpointf = str_replace( '_', ' ', $outpoint );
314
315 // Don't let the length runaway
316 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength );
317 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength );
318
319 $queryParams = array(
320 'from' => $inpoint,
321 'to' => $outpoint,
322 );
323
324 if ( $namespace ) {
325 $queryParams['namespace'] = $namespace;
326 }
327 if ( $hideRedirects ) {
328 $queryParams['hideredirects'] = 1;
329 }
330
331 $url = $this->getTitle()->getLocalURL( $queryParams );
332 $inlink = Html::element( 'a', array( 'href' => $url ), $inpointf );
333 $outlink = Html::element( 'a', array( 'href' => $url ), $outpointf );
334
335 $out = $this->msg( 'alphaindexline' )->rawParams(
336 "$inlink</td><td>",
337 "</td><td>$outlink"
338 )->escaped();
339
340 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
341 }
342
343 /**
344 * @param int $namespace Namespace (Default NS_MAIN)
345 * @param string $from list all pages from this name (default FALSE)
346 * @param string $to list all pages to this name (default FALSE)
347 * @param bool $hideredirects dont show redirects (default FALSE)
348 */
349 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
350 $output = $this->getOutput();
351
352 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
353 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
354 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
355 $n = 0;
356
357 if ( !$fromList || !$toList ) {
358 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
359 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
360 // Show errormessage and reset to NS_MAIN
361 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
362 $namespace = NS_MAIN;
363 } else {
364 list( $namespace, $fromKey, $from ) = $fromList;
365 list( , $toKey, $to ) = $toList;
366
367 $dbr = wfGetDB( DB_SLAVE );
368 $conds = array(
369 'page_namespace' => $namespace,
370 'page_title >= ' . $dbr->addQuotes( $fromKey )
371 );
372
373 if ( $hideredirects ) {
374 $conds['page_is_redirect'] = 0;
375 }
376
377 if ( $toKey !== "" ) {
378 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
379 }
380
381 $res = $dbr->select( 'page',
382 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
383 $conds,
384 __METHOD__,
385 array(
386 'ORDER BY' => 'page_title',
387 'LIMIT' => $this->maxPerPage + 1,
388 'USE INDEX' => 'name_title',
389 )
390 );
391
392 if ( $res->numRows() > 0 ) {
393 $out = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
394 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
395 $t = Title::newFromRow( $s );
396 if ( $t ) {
397 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
398 Linker::link( $t ) .
399 ( $s->page_is_redirect ? '</div>' : '' );
400 } else {
401 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
402 }
403
404 if ( $n % 3 == 0 ) {
405 $out .= '<tr>';
406 }
407
408 $out .= "<td style=\"width:33%\">$link</td>";
409 $n++;
410 if ( $n % 3 == 0 ) {
411 $out .= "</tr>\n";
412 }
413 }
414
415 if ( ( $n % 3 ) != 0 ) {
416 $out .= "</tr>\n";
417 }
418 $out .= Xml::closeElement( 'table' );
419 } else {
420 $out = '';
421 }
422 }
423
424 if ( $this->including() ) {
425 $out2 = '';
426 } else {
427 if ( $from == '' ) {
428 // First chunk; no previous link.
429 $prevTitle = null;
430 } else {
431 # Get the last title from previous chunk
432 $dbr = wfGetDB( DB_SLAVE );
433 $res_prev = $dbr->select(
434 'page',
435 'page_title',
436 array( 'page_namespace' => $namespace, 'page_title < ' . $dbr->addQuotes( $from ) ),
437 __METHOD__,
438 array( 'ORDER BY' => 'page_title DESC',
439 'LIMIT' => $this->maxPerPage, 'OFFSET' => ( $this->maxPerPage - 1 )
440 )
441 );
442
443 # Get first title of previous complete chunk
444 if ( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
445 $pt = $dbr->fetchObject( $res_prev );
446 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
447 } else {
448 # The previous chunk is not complete, need to link to the very first title
449 # available in the database
450 $options = array( 'LIMIT' => 1 );
451 if ( !$dbr->implicitOrderby() ) {
452 $options['ORDER BY'] = 'page_title';
453 }
454 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
455 array( 'page_namespace' => $namespace ), __METHOD__, $options );
456 # Show the previous link if it s not the current requested chunk
457 if ( $from != $reallyFirstPage_title ) {
458 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
459 } else {
460 $prevTitle = null;
461 }
462 }
463 }
464
465 $self = $this->getTitle();
466
467 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
468 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
469 '<tr>
470 <td>' .
471 $nsForm .
472 '</td>
473 <td class="mw-allpages-nav">' .
474 Linker::link( $self, $this->msg( 'allpages' )->escaped() );
475
476 # Do we put a previous link ?
477 if ( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
478 $query = array( 'from' => $prevTitle->getText() );
479
480 if ( $namespace ) {
481 $query['namespace'] = $namespace;
482 }
483
484 if ( $hideredirects ) {
485 $query['hideredirects'] = $hideredirects;
486 }
487
488 $prevLink = Linker::linkKnown(
489 $self,
490 $this->msg( 'prevpage', $pt )->escaped(),
491 array(),
492 $query
493 );
494 $out2 = $this->getLanguage()->pipeList( array( $out2, $prevLink ) );
495 }
496
497 if ( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
498 # $s is the first link of the next chunk
499 $t = Title::makeTitle( $namespace, $s->page_title );
500 $query = array( 'from' => $t->getText() );
501
502 if ( $namespace ) {
503 $query['namespace'] = $namespace;
504 }
505
506 if ( $hideredirects ) {
507 $query['hideredirects'] = $hideredirects;
508 }
509
510 $nextLink = Linker::linkKnown(
511 $self,
512 $this->msg( 'nextpage', $t->getText() )->escaped(),
513 array(),
514 $query
515 );
516 $out2 = $this->getLanguage()->pipeList( array( $out2, $nextLink ) );
517 }
518 $out2 .= "</td></tr></table>";
519 }
520
521 $output->addHTML( $out2 . $out );
522
523 $links = array();
524 if ( isset( $prevLink ) ) {
525 $links[] = $prevLink;
526 }
527
528 if ( isset( $nextLink ) ) {
529 $links[] = $nextLink;
530 }
531
532 if ( count( $links ) ) {
533 $output->addHTML(
534 Html::element( 'hr' ) .
535 Html::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
536 $this->getLanguage()->pipeList( $links )
537 )
538 );
539 }
540 }
541
542 /**
543 * @param $ns Integer: the namespace of the article
544 * @param string $text the name of the article
545 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
546 */
547 protected function getNamespaceKeyAndText( $ns, $text ) {
548 if ( $text == '' ) {
549 # shortcut for common case
550 return array( $ns, '', '' );
551 }
552
553 $t = Title::makeTitleSafe( $ns, $text );
554 if ( $t && $t->isLocal() ) {
555 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
556 } elseif ( $t ) {
557 return null;
558 }
559
560 # try again, in case the problem was an empty pagename
561 $text = preg_replace( '/(#|$)/', 'X$1', $text );
562 $t = Title::makeTitleSafe( $ns, $text );
563 if ( $t && $t->isLocal() ) {
564 return array( $t->getNamespace(), '', '' );
565 } else {
566 return null;
567 }
568 }
569
570 protected function getGroupName() {
571 return 'pages';
572 }
573 }