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