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