487d44d3b80796408290f9b43ae4ab9f56d29c6e
[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 $s = $dbr->fetchObject( $res );
192 if( $s ) {
193 array_push( $lines, $s->page_title );
194 } else {
195 // Final chunk, but ended prematurely. Go back and find the end.
196 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
197 array_merge($where,$chunk),
198 __METHOD__ );
199 array_push( $lines, $endTitle );
200 $done = true;
201 }
202 $s = $res->fetchObject();
203 if( $s ) {
204 array_push( $lines, $s->page_title );
205 $lastTitle = $s->page_title;
206 } else {
207 // This was a final chunk and ended exactly at the limit.
208 // Rare but convenient!
209 $done = true;
210 }
211 $res->free();
212 }
213 $wgMemc->add( $key, $lines, 3600 );
214 }
215
216 // If there are only two or less sections, don't even display them.
217 // Instead, display the first section directly.
218 if( count( $lines ) <= 2 ) {
219 if( !empty($lines) ) {
220 $this->showChunk( $namespace, $from, $to );
221 } else {
222 $wgOut->addHTML( $this->namespaceForm( $namespace, $from, $to ) );
223 }
224 return;
225 }
226
227 # At this point, $lines should contain an even number of elements.
228 $out .= Xml::openElement( 'table', array( 'class' => 'allpageslist' ) );
229 while( count ( $lines ) > 0 ) {
230 $inpoint = array_shift( $lines );
231 $outpoint = array_shift( $lines );
232 $out .= $this->showline( $inpoint, $outpoint, $namespace );
233 }
234 $out .= Xml::closeElement( 'table' );
235 $nsForm = $this->namespaceForm( $namespace, $from, $to );
236
237 # Is there more?
238 if( $this->including() ) {
239 $out2 = '';
240 } else {
241 if( isset($from) || isset($to) ) {
242 global $wgUser;
243 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
244 '<tr>
245 <td>' .
246 $nsForm .
247 '</td>
248 <td class="mw-allpages-nav">' .
249 $wgUser->getSkin()->link( $this->getTitle(), wfMsgHtml ( 'allpages' ),
250 array(), array(), 'known' ) .
251 "</td>
252 </tr>" .
253 Xml::closeElement( 'table' );
254 } else {
255 $out2 = $nsForm;
256 }
257 }
258 $wgOut->addHTML( $out2 . $out );
259 }
260
261 /**
262 * Show a line of "ABC to DEF" ranges of articles
263 *
264 * @param $inpoint String: lower limit of pagenames
265 * @param $outpoint String: upper limit of pagenames
266 * @param $namespace Integer (Default NS_MAIN)
267 */
268 function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
269 global $wgContLang;
270 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
271 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
272 // Don't let the length runaway
273 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength );
274 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength );
275
276 $queryparams = $namespace ? "namespace=$namespace&" : '';
277 $special = $this->getTitle();
278 $link = $special->escapeLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint) );
279
280 $out = wfMsgHtml( 'alphaindexline',
281 "<a href=\"$link\">$inpointf</a></td><td>",
282 "</td><td><a href=\"$link\">$outpointf</a>"
283 );
284 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
285 }
286
287 /**
288 * @param $namespace Integer (Default NS_MAIN)
289 * @param $from String: list all pages from this name (default FALSE)
290 * @param $to String: list all pages to this name (default FALSE)
291 */
292 function showChunk( $namespace = NS_MAIN, $from = false, $to = false ) {
293 global $wgOut, $wgUser, $wgContLang, $wgLang;
294
295 $sk = $wgUser->getSkin();
296
297 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
298 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
299 $namespaces = $wgContLang->getNamespaces();
300 $n = 0;
301
302 if ( !$fromList || !$toList ) {
303 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
304 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
305 // Show errormessage and reset to NS_MAIN
306 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
307 $namespace = NS_MAIN;
308 } else {
309 list( $namespace, $fromKey, $from ) = $fromList;
310 list( $namespace2, $toKey, $to ) = $toList;
311
312 $dbr = wfGetDB( DB_SLAVE );
313 $conds = array(
314 'page_namespace' => $namespace,
315 'page_title >= ' . $dbr->addQuotes( $fromKey )
316 );
317 if( $toKey !== "" ) {
318 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
319 }
320
321 $res = $dbr->select( 'page',
322 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
323 $conds,
324 __METHOD__,
325 array(
326 'ORDER BY' => 'page_title',
327 'LIMIT' => $this->maxPerPage + 1,
328 'USE INDEX' => 'name_title',
329 )
330 );
331
332 if( $res->numRows() > 0 ) {
333 $out = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
334 while( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
335 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
336 if( $t ) {
337 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
338 $sk->linkKnown( $t, htmlspecialchars( $t->getText() ) ) .
339 ($s->page_is_redirect ? '</div>' : '' );
340 } else {
341 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
342 }
343 if( $n % 3 == 0 ) {
344 $out .= '<tr>';
345 }
346 $out .= "<td style=\"width:33%\">$link</td>";
347 $n++;
348 if( $n % 3 == 0 ) {
349 $out .= "</tr>\n";
350 }
351 }
352 if( ($n % 3) != 0 ) {
353 $out .= "</tr>\n";
354 }
355 $out .= Xml::closeElement( 'table' );
356 } else {
357 $out = '';
358 }
359 }
360
361 if ( $this->including() ) {
362 $out2 = '';
363 } else {
364 if( $from == '' ) {
365 // First chunk; no previous link.
366 $prevTitle = null;
367 } else {
368 # Get the last title from previous chunk
369 $dbr = wfGetDB( DB_SLAVE );
370 $res_prev = $dbr->select(
371 'page',
372 'page_title',
373 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
374 __METHOD__,
375 array( 'ORDER BY' => 'page_title DESC',
376 'LIMIT' => $this->maxPerPage, 'OFFSET' => ($this->maxPerPage - 1 )
377 )
378 );
379
380 # Get first title of previous complete chunk
381 if( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
382 $pt = $dbr->fetchObject( $res_prev );
383 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
384 } else {
385 # The previous chunk is not complete, need to link to the very first title
386 # available in the database
387 $options = array( 'LIMIT' => 1 );
388 if ( ! $dbr->implicitOrderby() ) {
389 $options['ORDER BY'] = 'page_title';
390 }
391 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
392 array( 'page_namespace' => $namespace ), __METHOD__, $options );
393 # Show the previous link if it s not the current requested chunk
394 if( $from != $reallyFirstPage_title ) {
395 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
396 } else {
397 $prevTitle = null;
398 }
399 }
400 }
401
402 $self = $this->getTitle();
403
404 $nsForm = $this->namespaceForm( $namespace, $from, $to );
405 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
406 '<tr>
407 <td>' .
408 $nsForm .
409 '</td>
410 <td class="mw-allpages-nav">' .
411 $sk->link( $self, wfMsgHtml ( 'allpages' ), array(), array(), 'known' );
412
413 # Do we put a previous link ?
414 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
415 $query = array( 'from' => $prevTitle->getText() );
416
417 if( $namespace )
418 $query['namespace'] = $namespace;
419
420 $prevLink = $sk->linkKnown(
421 $self,
422 htmlspecialchars( wfMsg( 'prevpage', $pt ) ),
423 array(),
424 $query
425 );
426 $out2 = $wgLang->pipeList( array( $out2, $prevLink ) );
427 }
428
429 if( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
430 # $s is the first link of the next chunk
431 $t = Title::MakeTitle($namespace, $s->page_title);
432 $query = array( 'from' => $t->getText() );
433
434 if( $namespace )
435 $query['namespace'] = $namespace;
436
437 $nextLink = $sk->linkKnown(
438 $self,
439 htmlspecialchars( wfMsg( 'nextpage', $t->getText() ) ),
440 array(),
441 $query
442 );
443 $out2 = $wgLang->pipeList( array( $out2, $nextLink ) );
444 }
445 $out2 .= "</td></tr></table>";
446 }
447
448 $wgOut->addHTML( $out2 . $out );
449 if( isset($prevLink) or isset($nextLink) ) {
450 $wgOut->addHTML( '<hr /><p class="mw-allpages-nav">' );
451 if( isset( $prevLink ) ) {
452 $wgOut->addHTML( $prevLink );
453 }
454 if( isset( $prevLink ) && isset( $nextLink ) ) {
455 $wgOut->addHTML( wfMsgExt( 'pipe-separator' , 'escapenoentities' ) );
456 }
457 if( isset( $nextLink ) ) {
458 $wgOut->addHTML( $nextLink );
459 }
460 $wgOut->addHTML( '</p>' );
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 * @static (sort of)
471 * @access private
472 */
473 function getNamespaceKeyAndText($ns, $text) {
474 if ( $text == '' )
475 return array( $ns, '', '' ); # shortcut for common case
476
477 $t = Title::makeTitleSafe($ns, $text);
478 if ( $t && $t->isLocal() ) {
479 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
480 } else if ( $t ) {
481 return null;
482 }
483
484 # try again, in case the problem was an empty pagename
485 $text = preg_replace('/(#|$)/', 'X$1', $text);
486 $t = Title::makeTitleSafe($ns, $text);
487 if ( $t && $t->isLocal() ) {
488 return array( $t->getNamespace(), '', '' );
489 } else {
490 return null;
491 }
492 }
493 }