4 * Implements Special:Allpages
7 class SpecialAllpages
extends IncludableSpecialPage
{
10 * Maximum number of pages to show on single subpage.
12 protected $maxPerPage = 345;
15 * Maximum number of pages to show on single index subpage.
17 protected $maxLineCount = 100;
20 * Maximum number of chars to show for an entry.
22 protected $maxPageLength = 70;
25 * Determines, which message describes the input field 'nsfrom'.
27 protected $nsfromMsg = 'allpagesfrom';
29 function __construct( $name = 'Allpages' ){
30 parent
::__construct( $name );
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.
38 function execute( $par ) {
39 global $wgRequest, $wgOut, $wgContLang;
42 $this->outputHeader();
45 $from = $wgRequest->getVal( 'from', null );
46 $to = $wgRequest->getVal( 'to', null );
47 $namespace = $wgRequest->getInt( 'namespace' );
49 $namespaces = $wgContLang->getNamespaces();
52 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces) ) ) ?
53 wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
54 wfMsg( 'allarticles' )
58 $this->showChunk( $namespace, $par, $to );
59 } elseif( isset($from) && !isset($to) ) {
60 $this->showChunk( $namespace, $from, $to );
62 $this->showToplevel( $namespace, $from, $to );
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.
72 function namespaceForm( $namespace = NS_MAIN
, $from = '', $to = '' ) {
74 $t = $this->getTitle();
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' ) );
83 <td class='mw-label'>" .
84 Xml
::label( wfMsg( 'allpagesfrom' ), 'nsfrom' ) .
86 <td class='mw-input'>" .
87 Xml
::input( 'from', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
91 <td class='mw-label'>" .
92 Xml
::label( wfMsg( 'allpagesto' ), 'nsto' ) .
94 <td class='mw-input'>" .
95 Xml
::input( 'to', 30, str_replace('_',' ',$to), array( 'id' => 'nsto' ) ) .
99 <td class='mw-label'>" .
100 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
102 <td class='mw-input'>" .
103 Xml
::namespaceSelector( $namespace, null ) . ' ' .
104 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
107 $out .= Xml
::closeElement( 'table' );
108 $out .= Xml
::closeElement( 'fieldset' );
109 $out .= Xml
::closeElement( 'form' );
110 $out .= Xml
::closeElement( 'div' );
115 * @param integer $namespace (default NS_MAIN)
117 function showToplevel( $namespace = NS_MAIN
, $from = '', $to = '' ) {
120 # TODO: Either make this *much* faster or cache the title index points
121 # in the querycache table.
123 $dbr = wfGetDB( DB_SLAVE
);
125 $where = array( 'page_namespace' => $namespace );
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;
133 $where[] = 'page_title >= '.$dbr->addQuotes( $from );
135 $where[] = 'page_title <= '.$dbr->addQuotes( $to );
138 $key = wfMemcKey( 'allpages', 'ns', $namespace, $from, $to );
139 $lines = $wgMemc->get( $key );
141 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__
);
142 $maxPerSubpage = intval($count/$this->maxLineCount
);
143 $maxPerSubpage = max($maxPerSubpage,$this->maxPerPage
);
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.
155 // Fetch the last title of this chunk and the first of the next
156 $chunk = ( $lastTitle === false )
158 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
159 $res = $dbr->select( 'page', /* FROM */
160 'page_title', /* WHAT */
161 array_merge($where,$chunk),
163 array ('LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC')
166 if( $s = $dbr->fetchObject( $res ) ) {
167 array_push( $lines, $s->page_title
);
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),
173 array_push( $lines, $endTitle );
176 if( $s = $res->fetchObject() ) {
177 array_push( $lines, $s->page_title
);
178 $lastTitle = $s->page_title
;
180 // This was a final chunk and ended exactly at the limit.
181 // Rare but convenient!
186 $wgMemc->add( $key, $lines, 3600 );
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 );
195 $wgOut->addHTML( $this->namespaceForm( $namespace, $from, $to ) );
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 );
207 $out .= Xml
::closeElement( 'table' );
208 $nsForm = $this->namespaceForm( $namespace, $from, $to );
211 if( $this->including() ) {
214 if( isset($from) ||
isset($to) ) {
216 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
221 <td class="mw-allpages-nav">' .
222 $wgUser->getSkin()->link( $this->getTitle(), wfMsgHtml ( 'allpages' ),
223 array(), array(), 'known' ) .
226 Xml
::closeElement( 'table' );
231 $wgOut->addHTML( $out2 . $out );
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)
240 function showline( $inpoint, $outpoint, $namespace = NS_MAIN
) {
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
);
248 $queryparams = $namespace ?
"namespace=$namespace&" : '';
249 $special = $this->getTitle();
250 $link = $special->escapeLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint) );
252 $out = wfMsgHtml( 'alphaindexline',
253 "<a href=\"$link\">$inpointf</a></td><td>",
254 "</td><td><a href=\"$link\">$outpointf</a>"
256 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
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)
264 function showChunk( $namespace = NS_MAIN
, $from = false, $to = false ) {
265 global $wgOut, $wgUser, $wgContLang, $wgLang;
267 $sk = $wgUser->getSkin();
269 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
270 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
271 $namespaces = $wgContLang->getNamespaces();
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
;
281 list( $namespace, $fromKey, $from ) = $fromList;
282 list( $namespace2, $toKey, $to ) = $toList;
284 $dbr = wfGetDB( DB_SLAVE
);
286 'page_namespace' => $namespace,
287 'page_title >= ' . $dbr->addQuotes( $fromKey )
289 if( $toKey !== "" ) {
290 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
293 $res = $dbr->select( 'page',
294 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
298 'ORDER BY' => 'page_title',
299 'LIMIT' => $this->maxPerPage +
1,
300 'USE INDEX' => 'name_title',
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
);
309 $link = ( $s->page_is_redirect ?
'<div class="allpagesredirect">' : '' ) .
310 $sk->linkKnown( $t, htmlspecialchars( $t->getText() ) ) .
311 ($s->page_is_redirect ?
'</div>' : '' );
313 $link = '[[' . htmlspecialchars( $s->page_title
) . ']]';
318 $out .= "<td width=\"33%\">$link</td>";
324 if( ($n %
3) != 0 ) {
327 $out .= Xml
::closeElement( 'table' );
333 if ( $this->including() ) {
337 // First chunk; no previous link.
340 # Get the last title from previous chunk
341 $dbr = wfGetDB( DB_SLAVE
);
342 $res_prev = $dbr->select(
345 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
347 array( 'ORDER BY' => 'page_title DESC',
348 'LIMIT' => $this->maxPerPage
, 'OFFSET' => ($this->maxPerPage
- 1 )
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
);
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';
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 );
374 $self = $this->getTitle();
376 $nsForm = $this->namespaceForm( $namespace, $from, $to );
377 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
382 <td class="mw-allpages-nav">' .
383 $sk->link( $self, wfMsgHtml ( 'allpages' ), array(), array(), 'known' );
385 # Do we put a previous link ?
386 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
387 $query = array( 'from' => $prevTitle->getText() );
390 $query['namespace'] = $namespace;
392 $prevLink = $sk->linkKnown(
394 htmlspecialchars( wfMsg( 'prevpage', $pt ) ),
398 $out2 = $wgLang->pipeList( array( $out2, $prevLink ) );
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() );
407 $query['namespace'] = $namespace;
409 $nextLink = $sk->linkKnown(
411 htmlspecialchars( wfMsg( 'nextpage', $t->getText() ) ),
415 $out2 = $wgLang->pipeList( array( $out2, $nextLink ) );
417 $out2 .= "</td></tr></table>";
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 );
426 if( isset( $prevLink ) && isset( $nextLink ) ) {
427 $wgOut->addHTML( wfMsgExt( 'pipe-separator' , 'escapenoentities' ) );
429 if( isset( $nextLink ) ) {
430 $wgOut->addHTML( $nextLink );
432 $wgOut->addHTML( '</p>' );
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
445 function getNamespaceKeyAndText($ns, $text) {
447 return array( $ns, '', '' ); # shortcut for common case
449 $t = Title
::makeTitleSafe($ns, $text);
450 if ( $t && $t->isLocal() ) {
451 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
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(), '', '' );