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