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