Fixed use of long memcached keys in AllPages.
[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 $name string: 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 $par String: 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 $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
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 $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)
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 if( isset($to) )
194 $where[] = 'page_title <= '.$dbr->addQuotes( $to );
195
196 global $wgMemc;
197 $key = wfMemcKey( 'allpages', 'ns', $namespace, sha1( $from ), sha1( $to ) );
198 $lines = $wgMemc->get( $key );
199
200 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__ );
201 $maxPerSubpage = intval( $count / $this->maxLineCount );
202 $maxPerSubpage = max( $maxPerSubpage, $this->maxPerPage );
203
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')
223 );
224
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;
235 }
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;
244 }
245 $res->free();
246 }
247 $wgMemc->add( $key, $lines, 3600 );
248 }
249
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 ) );
257 }
258 return;
259 }
260
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 );
267 }
268 $out .= Xml::closeElement( 'table' );
269 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
270
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;
289 }
290 }
291 $output->addHTML( $out2 . $out );
292 }
293
294 /**
295 * Show a line of "ABC to DEF" ranges of articles
296 *
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
302 */
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 );
310
311 $queryparams = $namespace ? "namespace=$namespace&" : '';
312
313 $queryhideredirects = array();
314 if ($hideredirects) {
315 $queryhideredirects[ 'hideredirects' ] = 1;
316 }
317
318 $special = $this->getTitle();
319 $link = htmlspecialchars( $special->getLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint), $queryhideredirects ) );
320
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>';
326 }
327
328 /**
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)
333 */
334 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
335 global $wgContLang;
336 $output = $this->getOutput();
337
338 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
339 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
340 $namespaces = $wgContLang->getNamespaces();
341 $n = 0;
342
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;
352
353 $dbr = wfGetDB( DB_SLAVE );
354 $conds = array(
355 'page_namespace' => $namespace,
356 'page_title >= ' . $dbr->addQuotes( $fromKey )
357 );
358
359 if ( $hideredirects ) {
360 $conds[ 'page_is_redirect' ] = 0;
361 }
362
363 if( $toKey !== "" ) {
364 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
365 }
366
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',
375 )
376 );
377
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 ) . ']]';
388 }
389 if( $n % 3 == 0 ) {
390 $out .= '<tr>';
391 }
392 $out .= "<td style=\"width:33%\">$link</td>";
393 $n++;
394 if( $n % 3 == 0 ) {
395 $out .= "</tr>\n";
396 }
397 }
398 if( ($n % 3) != 0 ) {
399 $out .= "</tr>\n";
400 }
401 $out .= Xml::closeElement( 'table' );
402 } else {
403 $out = '';
404 }
405 }
406
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 )
423 )
424 );
425
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';
436 }
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;
444 }
445 }
446 }
447
448 $self = $this->getTitle();
449
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() );
458
459 # Do we put a previous link ?
460 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
461 $query = array( 'from' => $prevTitle->getText() );
462
463 if( $namespace )
464 $query['namespace'] = $namespace;
465
466 if( $hideredirects )
467 $query['hideredirects'] = $hideredirects;
468
469 $prevLink = Linker::linkKnown(
470 $self,
471 $this->msg( 'prevpage', $pt )->escaped(),
472 array(),
473 $query
474 );
475 $out2 = $this->getLanguage()->pipeList( array( $out2, $prevLink ) );
476 }
477
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() );
482
483 if( $namespace )
484 $query['namespace'] = $namespace;
485
486 if( $hideredirects )
487 $query['hideredirects'] = $hideredirects;
488
489 $nextLink = Linker::linkKnown(
490 $self,
491 $this->msg( 'nextpage', $t->getText() )->escaped(),
492 array(),
493 $query
494 );
495 $out2 = $this->getLanguage()->pipeList( array( $out2, $nextLink ) );
496 }
497 $out2 .= "</td></tr></table>";
498 }
499
500 $output->addHTML( $out2 . $out );
501
502 $links = array();
503 if ( isset( $prevLink ) ) $links[] = $prevLink;
504 if ( isset( $nextLink ) ) $links[] = $nextLink;
505
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 ) );
512 }
513
514 }
515
516 /**
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
520 */
521 protected function getNamespaceKeyAndText($ns, $text) {
522 if ( $text == '' )
523 return array( $ns, '', '' ); # shortcut for common case
524
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;
530 }
531
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;
539 }
540 }
541 }