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