In Special:RevisionDelete:
[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;
118
119 # TODO: Either make this *much* faster or cache the title index points
120 # in the querycache table.
121
122 $dbr = wfGetDB( DB_SLAVE );
123 $out = "";
124 $where = array( 'page_namespace' => $namespace );
125
126 $from = Title::makeTitleSafe( $namespace, $from );
127 $to = Title::makeTitleSafe( $namespace, $to );
128 $from = ( $from && $from->isLocal() ) ? $from->getDBkey() : null;
129 $to = ( $to && $to->isLocal() ) ? $to->getDBkey() : null;
130
131 if( isset($from) )
132 $where[] = 'page_title >= '.$dbr->addQuotes( $from );
133 if( isset($to) )
134 $where[] = 'page_title <= '.$dbr->addQuotes( $to );
135
136 global $wgMemc;
137 $key = wfMemcKey( 'allpages', 'ns', $namespace, $from, $to );
138 $lines = $wgMemc->get( $key );
139
140 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__ );
141 $maxPerSubpage = intval($count/$this->maxLineCount);
142 $maxPerSubpage = max($maxPerSubpage,$this->maxPerPage);
143
144 if( !is_array( $lines ) ) {
145 $options = array( 'LIMIT' => 1 );
146 $options['ORDER BY'] = 'page_title ASC';
147 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__, $options );
148 $lastTitle = $firstTitle;
149 # This array is going to hold the page_titles in order.
150 $lines = array( $firstTitle );
151 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
152 $done = false;
153 while( !$done ) {
154 // Fetch the last title of this chunk and the first of the next
155 $chunk = ( $lastTitle === false )
156 ? array()
157 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
158 $res = $dbr->select( 'page', /* FROM */
159 'page_title', /* WHAT */
160 array_merge($where,$chunk),
161 __METHOD__,
162 array ('LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC')
163 );
164
165 if( $s = $dbr->fetchObject( $res ) ) {
166 array_push( $lines, $s->page_title );
167 } else {
168 // Final chunk, but ended prematurely. Go back and find the end.
169 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
170 array_merge($where,$chunk),
171 __METHOD__ );
172 array_push( $lines, $endTitle );
173 $done = true;
174 }
175 if( $s = $res->fetchObject() ) {
176 array_push( $lines, $s->page_title );
177 $lastTitle = $s->page_title;
178 } else {
179 // This was a final chunk and ended exactly at the limit.
180 // Rare but convenient!
181 $done = true;
182 }
183 $res->free();
184 }
185 $wgMemc->add( $key, $lines, 3600 );
186 }
187
188 // If there are only two or less sections, don't even display them.
189 // Instead, display the first section directly.
190 if( count( $lines ) <= 2 ) {
191 if( !empty($lines) ) {
192 $this->showChunk( $namespace, $lines[0], $lines[count($lines)-1] );
193 } else {
194 $wgOut->addHTML( $this->namespaceForm( $namespace, $from, $to ) );
195 }
196 return;
197 }
198
199 # At this point, $lines should contain an even number of elements.
200 $out .= Xml::openElement( 'table', array( 'class' => 'allpageslist' ) );
201 while( count ( $lines ) > 0 ) {
202 $inpoint = array_shift( $lines );
203 $outpoint = array_shift( $lines );
204 $out .= $this->showline( $inpoint, $outpoint, $namespace );
205 }
206 $out .= Xml::closeElement( 'table' );
207 $nsForm = $this->namespaceForm( $namespace, $from, $to );
208
209 # Is there more?
210 if( $this->including() ) {
211 $out2 = '';
212 } else {
213 if( isset($from) || isset($to) ) {
214 global $wgUser;
215 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
216 '<tr>
217 <td>' .
218 $nsForm .
219 '</td>
220 <td class="mw-allpages-nav">' .
221 $wgUser->getSkin()->link( $this->getTitle(), wfMsgHtml ( 'allpages' ),
222 array(), array(), 'known' ) .
223 "</td>
224 </tr>" .
225 Xml::closeElement( 'table' );
226 } else {
227 $out2 = $nsForm;
228 }
229 }
230 $wgOut->addHTML( $out2 . $out );
231 }
232
233 /**
234 * Show a line of "ABC to DEF" ranges of articles
235 * @param string $inpoint Lower limit of pagenames
236 * @param string $outpout Upper limit of pagenames
237 * @param integer $namespace (Default NS_MAIN)
238 */
239 function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
240 global $wgContLang;
241 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
242 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
243 // Don't let the length runaway
244 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength );
245 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength );
246
247 $queryparams = $namespace ? "namespace=$namespace&" : '';
248 $special = $this->getTitle();
249 $link = $special->escapeLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint) );
250
251 $out = wfMsgHtml( 'alphaindexline',
252 "<a href=\"$link\">$inpointf</a></td><td>",
253 "</td><td><a href=\"$link\">$outpointf</a>"
254 );
255 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
256 }
257
258 /**
259 * @param integer $namespace (Default NS_MAIN)
260 * @param string $from list all pages from this name (default FALSE)
261 * @param string $to list all pages to this name (default FALSE)
262 */
263 function showChunk( $namespace = NS_MAIN, $from = false, $to = false ) {
264 global $wgOut, $wgUser, $wgContLang, $wgLang;
265
266 $sk = $wgUser->getSkin();
267
268 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
269 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
270 $namespaces = $wgContLang->getNamespaces();
271 $n = 0;
272
273 if ( !$fromList || !$toList ) {
274 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
275 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
276 // Show errormessage and reset to NS_MAIN
277 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
278 $namespace = NS_MAIN;
279 } else {
280 list( $namespace, $fromKey, $from ) = $fromList;
281 list( $namespace2, $toKey, $to ) = $toList;
282
283 $dbr = wfGetDB( DB_SLAVE );
284 $conds = array(
285 'page_namespace' => $namespace,
286 'page_title >= ' . $dbr->addQuotes( $fromKey )
287 );
288 if( $toKey !== "" ) {
289 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
290 }
291
292 $res = $dbr->select( 'page',
293 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
294 $conds,
295 __METHOD__,
296 array(
297 'ORDER BY' => 'page_title',
298 'LIMIT' => $this->maxPerPage + 1,
299 'USE INDEX' => 'name_title',
300 )
301 );
302
303 if( $res->numRows() > 0 ) {
304 $out = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
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>\n";
321 }
322 }
323 if( ($n % 3) != 0 ) {
324 $out .= "</tr>\n";
325 }
326 $out .= Xml::closeElement( '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 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
375 '<tr>
376 <td>' .
377 $nsForm .
378 '</td>
379 <td class="mw-allpages-nav">' .
380 $sk->link( $self, wfMsgHtml ( 'allpages' ), array(), array(), 'known' );
381
382 # Do we put a previous link ?
383 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
384 $q = 'from=' . $prevTitle->getPartialUrl()
385 . ( $namespace ? '&namespace=' . $namespace : '' );
386 $prevLink = $sk->makeKnownLinkObj( $self,
387 wfMsgHTML( 'prevpage', htmlspecialchars( $pt ) ), $q );
388 $out2 = $wgLang->pipeList( array( $out2, $prevLink ) );
389 }
390
391 if( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
392 # $s is the first link of the next chunk
393 $t = Title::MakeTitle($namespace, $s->page_title);
394 $q = 'from=' . $t->getPartialUrl()
395 . ( $namespace ? '&namespace=' . $namespace : '' );
396 $nextLink = $sk->makeKnownLinkObj( $self,
397 wfMsgHtml( 'nextpage', htmlspecialchars( $t->getText() ) ), $q );
398 $out2 = $wgLang->pipeList( array( $out2, $nextLink ) );
399 }
400 $out2 .= "</td></tr></table>";
401 }
402
403 $wgOut->addHTML( $out2 . $out );
404 if( isset($prevLink) or isset($nextLink) ) {
405 $wgOut->addHTML( '<hr /><p class="mw-allpages-nav">' );
406 if( isset( $prevLink ) ) {
407 $wgOut->addHTML( $prevLink );
408 }
409 if( isset( $prevLink ) && isset( $nextLink ) ) {
410 $wgOut->addHTML( wfMsgExt( 'pipe-separator' , 'escapenoentities' ) );
411 }
412 if( isset( $nextLink ) ) {
413 $wgOut->addHTML( $nextLink );
414 }
415 $wgOut->addHTML( '</p>' );
416
417 }
418
419 }
420
421 /**
422 * @param int $ns the namespace of the article
423 * @param string $text the name of the article
424 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
425 * @static (sort of)
426 * @access private
427 */
428 function getNamespaceKeyAndText($ns, $text) {
429 if ( $text == '' )
430 return array( $ns, '', '' ); # shortcut for common case
431
432 $t = Title::makeTitleSafe($ns, $text);
433 if ( $t && $t->isLocal() ) {
434 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
435 } else if ( $t ) {
436 return NULL;
437 }
438
439 # try again, in case the problem was an empty pagename
440 $text = preg_replace('/(#|$)/', 'X$1', $text);
441 $t = Title::makeTitleSafe($ns, $text);
442 if ( $t && $t->isLocal() ) {
443 return array( $t->getNamespace(), '', '' );
444 } else {
445 return NULL;
446 }
447 }
448 }