Some escaping fixes and related readability changes
[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 = 100;
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(
52 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces) ) ) ?
53 wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
54 wfMsg( 'allarticles' )
55 );
56
57 if( isset($par) ) {
58 $this->showChunk( $namespace, $par, $to );
59 } elseif( isset($from) && !isset($to) ) {
60 $this->showChunk( $namespace, $from, $to );
61 } else {
62 $this->showToplevel( $namespace, $from, $to );
63 }
64 }
65
66 /**
67 * HTML for the top form
68 * @param integer $namespace A namespace constant (default NS_MAIN).
69 * @param string $from dbKey we are starting listing at.
70 * @param string $to dbKey we are ending listing at.
71 */
72 function namespaceForm( $namespace = NS_MAIN, $from = '', $to = '' ) {
73 global $wgScript;
74 $t = $this->getTitle();
75
76 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
77 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
78 $out .= Xml::hidden( 'title', $t->getPrefixedText() );
79 $out .= Xml::openElement( 'fieldset' );
80 $out .= Xml::element( 'legend', null, wfMsg( 'allpages' ) );
81 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
82 $out .= "<tr>
83 <td class='mw-label'>" .
84 Xml::label( wfMsg( 'allpagesfrom' ), 'nsfrom' ) .
85 " </td>
86 <td class='mw-input'>" .
87 Xml::input( 'from', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
88 " </td>
89 </tr>
90 <tr>
91 <td class='mw-label'>" .
92 Xml::label( wfMsg( 'allpagesto' ), 'nsto' ) .
93 " </td>
94 <td class='mw-input'>" .
95 Xml::input( 'to', 30, str_replace('_',' ',$to), array( 'id' => 'nsto' ) ) .
96 " </td>
97 </tr>
98 <tr>
99 <td class='mw-label'>" .
100 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
101 " </td>
102 <td class='mw-input'>" .
103 Xml::namespaceSelector( $namespace, null ) . ' ' .
104 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
105 " </td>
106 </tr>";
107 $out .= Xml::closeElement( 'table' );
108 $out .= Xml::closeElement( 'fieldset' );
109 $out .= Xml::closeElement( 'form' );
110 $out .= Xml::closeElement( 'div' );
111 return $out;
112 }
113
114 /**
115 * @param integer $namespace (default NS_MAIN)
116 */
117 function showToplevel( $namespace = NS_MAIN, $from = '', $to = '' ) {
118 global $wgOut;
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, $from, $to );
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 .= Xml::openElement( 'table', array( 'class' => 'allpageslist' ) );
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 .= Xml::closeElement( '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 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
217 '<tr>
218 <td>' .
219 $nsForm .
220 '</td>
221 <td class="mw-allpages-nav">' .
222 $wgUser->getSkin()->link( $this->getTitle(), wfMsgHtml ( 'allpages' ),
223 array(), array(), 'known' ) .
224 "</td>
225 </tr>" .
226 Xml::closeElement( 'table' );
227 } else {
228 $out2 = $nsForm;
229 }
230 }
231 $wgOut->addHTML( $out2 . $out );
232 }
233
234 /**
235 * Show a line of "ABC to DEF" ranges of articles
236 * @param string $inpoint Lower limit of pagenames
237 * @param string $outpout Upper limit of pagenames
238 * @param integer $namespace (Default NS_MAIN)
239 */
240 function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
241 global $wgContLang;
242 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
243 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
244 // Don't let the length runaway
245 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength );
246 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength );
247
248 $queryparams = $namespace ? "namespace=$namespace&" : '';
249 $special = $this->getTitle();
250 $link = $special->escapeLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint) );
251
252 $out = wfMsgHtml( 'alphaindexline',
253 "<a href=\"$link\">$inpointf</a></td><td>",
254 "</td><td><a href=\"$link\">$outpointf</a>"
255 );
256 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
257 }
258
259 /**
260 * @param integer $namespace (Default NS_MAIN)
261 * @param string $from list all pages from this name (default FALSE)
262 * @param string $to list all pages to this name (default FALSE)
263 */
264 function showChunk( $namespace = NS_MAIN, $from = false, $to = false ) {
265 global $wgOut, $wgUser, $wgContLang, $wgLang;
266
267 $sk = $wgUser->getSkin();
268
269 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
270 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
271 $namespaces = $wgContLang->getNamespaces();
272 $n = 0;
273
274 if ( !$fromList || !$toList ) {
275 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
276 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
277 // Show errormessage and reset to NS_MAIN
278 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
279 $namespace = NS_MAIN;
280 } else {
281 list( $namespace, $fromKey, $from ) = $fromList;
282 list( $namespace2, $toKey, $to ) = $toList;
283
284 $dbr = wfGetDB( DB_SLAVE );
285 $conds = array(
286 'page_namespace' => $namespace,
287 'page_title >= ' . $dbr->addQuotes( $fromKey )
288 );
289 if( $toKey !== "" ) {
290 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
291 }
292
293 $res = $dbr->select( 'page',
294 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
295 $conds,
296 __METHOD__,
297 array(
298 'ORDER BY' => 'page_title',
299 'LIMIT' => $this->maxPerPage + 1,
300 'USE INDEX' => 'name_title',
301 )
302 );
303
304 if( $res->numRows() > 0 ) {
305 $out = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
306 while( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
307 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
308 if( $t ) {
309 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
310 $sk->linkKnown( $t, htmlspecialchars( $t->getText() ) ) .
311 ($s->page_is_redirect ? '</div>' : '' );
312 } else {
313 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
314 }
315 if( $n % 3 == 0 ) {
316 $out .= '<tr>';
317 }
318 $out .= "<td width=\"33%\">$link</td>";
319 $n++;
320 if( $n % 3 == 0 ) {
321 $out .= "</tr>\n";
322 }
323 }
324 if( ($n % 3) != 0 ) {
325 $out .= "</tr>\n";
326 }
327 $out .= Xml::closeElement( 'table' );
328 } else {
329 $out = '';
330 }
331 }
332
333 if ( $this->including() ) {
334 $out2 = '';
335 } else {
336 if( $from == '' ) {
337 // First chunk; no previous link.
338 $prevTitle = null;
339 } else {
340 # Get the last title from previous chunk
341 $dbr = wfGetDB( DB_SLAVE );
342 $res_prev = $dbr->select(
343 'page',
344 'page_title',
345 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
346 __METHOD__,
347 array( 'ORDER BY' => 'page_title DESC',
348 'LIMIT' => $this->maxPerPage, 'OFFSET' => ($this->maxPerPage - 1 )
349 )
350 );
351
352 # Get first title of previous complete chunk
353 if( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
354 $pt = $dbr->fetchObject( $res_prev );
355 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
356 } else {
357 # The previous chunk is not complete, need to link to the very first title
358 # available in the database
359 $options = array( 'LIMIT' => 1 );
360 if ( ! $dbr->implicitOrderby() ) {
361 $options['ORDER BY'] = 'page_title';
362 }
363 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
364 array( 'page_namespace' => $namespace ), __METHOD__, $options );
365 # Show the previous link if it s not the current requested chunk
366 if( $from != $reallyFirstPage_title ) {
367 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
368 } else {
369 $prevTitle = null;
370 }
371 }
372 }
373
374 $self = $this->getTitle();
375
376 $nsForm = $this->namespaceForm( $namespace, $from, $to );
377 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
378 '<tr>
379 <td>' .
380 $nsForm .
381 '</td>
382 <td class="mw-allpages-nav">' .
383 $sk->link( $self, wfMsgHtml ( 'allpages' ), array(), array(), 'known' );
384
385 # Do we put a previous link ?
386 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
387 $query = array( 'from' => $prevTitle->getText() );
388
389 if( $namespace )
390 $query['namespace'] = $namespace;
391
392 $prevLink = $sk->linkKnown(
393 $self,
394 htmlspecialchars( wfMsg( 'prevpage', $pt ) ),
395 array(),
396 $query
397 );
398 $out2 = $wgLang->pipeList( array( $out2, $prevLink ) );
399 }
400
401 if( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
402 # $s is the first link of the next chunk
403 $t = Title::MakeTitle($namespace, $s->page_title);
404 $query = array( 'from' => $t->getText() );
405
406 if( $namespace )
407 $query['namespace'] = $namespace;
408
409 $nextLink = $sk->linkKnown(
410 $self,
411 htmlspecialchars( wfMsg( 'nextpage', $t->getText() ) ),
412 array(),
413 $query
414 );
415 $out2 = $wgLang->pipeList( array( $out2, $nextLink ) );
416 }
417 $out2 .= "</td></tr></table>";
418 }
419
420 $wgOut->addHTML( $out2 . $out );
421 if( isset($prevLink) or isset($nextLink) ) {
422 $wgOut->addHTML( '<hr /><p class="mw-allpages-nav">' );
423 if( isset( $prevLink ) ) {
424 $wgOut->addHTML( $prevLink );
425 }
426 if( isset( $prevLink ) && isset( $nextLink ) ) {
427 $wgOut->addHTML( wfMsgExt( 'pipe-separator' , 'escapenoentities' ) );
428 }
429 if( isset( $nextLink ) ) {
430 $wgOut->addHTML( $nextLink );
431 }
432 $wgOut->addHTML( '</p>' );
433
434 }
435
436 }
437
438 /**
439 * @param int $ns the namespace of the article
440 * @param string $text the name of the article
441 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
442 * @static (sort of)
443 * @access private
444 */
445 function getNamespaceKeyAndText($ns, $text) {
446 if ( $text == '' )
447 return array( $ns, '', '' ); # shortcut for common case
448
449 $t = Title::makeTitleSafe($ns, $text);
450 if ( $t && $t->isLocal() ) {
451 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
452 } else if ( $t ) {
453 return NULL;
454 }
455
456 # try again, in case the problem was an empty pagename
457 $text = preg_replace('/(#|$)/', 'X$1', $text);
458 $t = Title::makeTitleSafe($ns, $text);
459 if ( $t && $t->isLocal() ) {
460 return array( $t->getNamespace(), '', '' );
461 } else {
462 return NULL;
463 }
464 }
465 }