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