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