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