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 = 200;
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();
51 $wgOut->setPagetitle( ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces) ) ) ?
52 wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
53 wfMsg( 'allarticles' )
57 $this->showChunk( $namespace, $par, $to );
58 } elseif( isset($from) && !isset($to) ) {
59 $this->showChunk( $namespace, $from, $to );
61 $this->showToplevel( $namespace, $from, $to );
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.
71 function namespaceForm( $namespace = NS_MAIN
, $from = '', $to = '' ) {
73 $t = $this->getTitle();
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' ) );
82 <td class='mw-label'>" .
83 Xml
::label( wfMsg( 'allpagesfrom' ), 'nsfrom' ) .
85 <td class='mw-input'>" .
86 Xml
::input( 'from', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
90 <td class='mw-label'>" .
91 Xml
::label( wfMsg( 'allpagesto' ), 'nsto' ) .
93 <td class='mw-input'>" .
94 Xml
::input( 'to', 30, str_replace('_',' ',$to), array( 'id' => 'nsto' ) ) .
98 <td class='mw-label'>" .
99 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
101 <td class='mw-input'>" .
102 Xml
::namespaceSelector( $namespace, null ) . ' ' .
103 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
106 $out .= Xml
::closeElement( 'table' );
107 $out .= Xml
::closeElement( 'fieldset' );
108 $out .= Xml
::closeElement( 'form' );
109 $out .= Xml
::closeElement( 'div' );
114 * @param integer $namespace (default NS_MAIN)
116 function showToplevel( $namespace = NS_MAIN
, $from = '', $to = '' ) {
117 global $wgOut, $wgContLang;
118 $align = $wgContLang->isRtl() ?
'left' : 'right';
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 = is_null( $lastTitle )
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),
164 array ('LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC')
167 if( $s = $dbr->fetchObject( $res ) ) {
168 array_push( $lines, $s->page_title
);
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),
174 array_push( $lines, $endTitle );
177 if( $s = $res->fetchObject() ) {
178 array_push( $lines, $s->page_title
);
179 $lastTitle = $s->page_title
;
181 // This was a final chunk and ended exactly at the limit.
182 // Rare but convenient!
187 $wgMemc->add( $key, $lines, 3600 );
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] );
196 $wgOut->addHtml( $this->namespaceForm( $namespace, $from, $to ) );
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 );
209 $nsForm = $this->namespaceForm( $namespace, $from, $to );
212 if( $this->including() ) {
215 if( isset($from) ||
isset($to) ) {
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 />";
223 $out2 = $nsForm . '<hr />';
226 $wgOut->addHtml( $out2 . $out );
231 * @param string $from
232 * @param integer $namespace (Default NS_MAIN)
234 function showline( $inpoint, $outpoint, $namespace = NS_MAIN
) {
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
, '...' );
243 $queryparams = $namespace ?
"namespace=$namespace&" : '';
244 $special = $this->getTitle();
245 $link = $special->escapeLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint) );
247 $out = wfMsgHtml( 'alphaindexline',
248 "<a href=\"$link\">$inpointf</a></td><td>",
249 "</td><td><a href=\"$link\">$outpointf</a>"
251 return '<tr><td align="' . $align . '">'.$out.'</td></tr>';
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)
259 function showChunk( $namespace = NS_MAIN
, $from = false, $to = false ) {
260 global $wgOut, $wgUser, $wgContLang;
262 $sk = $wgUser->getSkin();
264 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
265 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
266 $namespaces = $wgContLang->getNamespaces();
267 $align = $wgContLang->isRtl() ?
'left' : 'right';
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
;
278 list( $namespace, $fromKey, $from ) = $fromList;
279 list( $namespace2, $toKey, $to ) = $toList;
281 $dbr = wfGetDB( DB_SLAVE
);
283 'page_namespace' => $namespace,
284 'page_title >= ' . $dbr->addQuotes( $fromKey )
286 if( $toKey !== "" ) {
287 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
290 $res = $dbr->select( 'page',
291 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
295 'ORDER BY' => 'page_title',
296 'LIMIT' => $this->maxPerPage +
1,
297 'USE INDEX' => 'name_title',
301 if( $res->numRows() > 0 ) {
302 $out = '<table style="background: inherit;" border="0" width="100%">';
304 while( ( $n < $this->maxPerPage
) && ( $s = $res->fetchObject() ) ) {
305 $t = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
307 $link = ( $s->page_is_redirect ?
'<div class="allpagesredirect">' : '' ) .
308 $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
309 ($s->page_is_redirect ?
'</div>' : '' );
311 $link = '[[' . htmlspecialchars( $s->page_title
) . ']]';
316 $out .= "<td width=\"33%\">$link</td>";
322 if( ($n %
3) != 0 ) {
331 if ( $this->including() ) {
335 // First chunk; no previous link.
338 # Get the last title from previous chunk
339 $dbr = wfGetDB( DB_SLAVE
);
340 $res_prev = $dbr->select(
343 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
345 array( 'ORDER BY' => 'page_title DESC', 'LIMIT' => $this->maxPerPage
, 'OFFSET' => ($this->maxPerPage
- 1 ) )
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
);
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';
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 );
370 $self = $this->getTitle();
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' ) );
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 .= ' | ' . $prevLink;
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 .= ' | ' . $nextLink;
397 $out2 .= "</td></tr></table><hr />";
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 );
406 if( isset( $prevLink ) && isset( $nextLink ) ) {
407 $wgOut->addHTML( ' | ' );
409 if( isset( $nextLink ) ) {
410 $wgOut->addHTML( $nextLink );
412 $wgOut->addHTML( '</p>' );
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
425 function getNamespaceKeyAndText($ns, $text) {
427 return array( $ns, '', '' ); # shortcut for common case
429 $t = Title
::makeTitleSafe($ns, $text);
430 if ( $t && $t->isLocal() ) {
431 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
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(), '', '' );