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