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