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