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();
43 $wgOut->allowClickjacking();
46 $from = $wgRequest->getVal( 'from', null );
47 $to = $wgRequest->getVal( 'to', null );
48 $namespace = $wgRequest->getInt( 'namespace' );
50 $namespaces = $wgContLang->getNamespaces();
53 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces) ) ) ?
54 wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
55 wfMsg( 'allarticles' )
59 $this->showChunk( $namespace, $par, $to );
60 } elseif( isset($from) && !isset($to) ) {
61 $this->showChunk( $namespace, $from, $to );
63 $this->showToplevel( $namespace, $from, $to );
68 * HTML for the top form
69 * @param integer $namespace A namespace constant (default NS_MAIN).
70 * @param string $from dbKey we are starting listing at.
71 * @param string $to dbKey we are ending listing at.
73 function namespaceForm( $namespace = NS_MAIN, $from = '', $to = '' ) {
75 $t = $this->getTitle();
77 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
78 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
79 $out .= Xml::hidden( 'title', $t->getPrefixedText() );
80 $out .= Xml::openElement( 'fieldset' );
81 $out .= Xml::element( 'legend', null, wfMsg( 'allpages' ) );
82 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
84 <td class='mw-label'>" .
85 Xml::label( wfMsg( 'allpagesfrom' ), 'nsfrom' ) .
87 <td class='mw-input'>" .
88 Xml::input( 'from', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
92 <td class='mw-label'>" .
93 Xml::label( wfMsg( 'allpagesto' ), 'nsto' ) .
95 <td class='mw-input'>" .
96 Xml::input( 'to', 30, str_replace('_',' ',$to), array( 'id' => 'nsto' ) ) .
100 <td class='mw-label'>" .
101 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
103 <td class='mw-input'>" .
104 Xml::namespaceSelector( $namespace, null ) . ' ' .
105 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
108 $out .= Xml::closeElement( 'table' );
109 $out .= Xml::closeElement( 'fieldset' );
110 $out .= Xml::closeElement( 'form' );
111 $out .= Xml::closeElement( 'div' );
116 * @param integer $namespace (default NS_MAIN)
118 function showToplevel( $namespace = NS_MAIN, $from = '', $to = '' ) {
121 # TODO: Either make this *much* faster or cache the title index points
122 # in the querycache table.
124 $dbr = wfGetDB( DB_SLAVE );
126 $where = array( 'page_namespace' => $namespace );
128 $from = Title::makeTitleSafe( $namespace, $from );
129 $to = Title::makeTitleSafe( $namespace, $to );
130 $from = ( $from && $from->isLocal() ) ? $from->getDBkey() : null;
131 $to = ( $to && $to->isLocal() ) ? $to->getDBkey() : null;
134 $where[] = 'page_title >= '.$dbr->addQuotes( $from );
136 $where[] = 'page_title <= '.$dbr->addQuotes( $to );
139 $key = wfMemcKey( 'allpages', 'ns', $namespace, $from, $to );
140 $lines = $wgMemc->get( $key );
142 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__ );
143 $maxPerSubpage = intval($count/$this->maxLineCount);
144 $maxPerSubpage = max($maxPerSubpage,$this->maxPerPage);
146 if( !is_array( $lines ) ) {
147 $options = array( 'LIMIT' => 1 );
148 $options['ORDER BY'] = 'page_title ASC';
149 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__, $options );
150 $lastTitle = $firstTitle;
151 # This array is going to hold the page_titles in order.
152 $lines = array( $firstTitle );
153 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
156 // Fetch the last title of this chunk and the first of the next
157 $chunk = ( $lastTitle === false )
159 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
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, $from, $to );
196 $wgOut->addHTML( $this->namespaceForm( $namespace, $from, $to ) );
201 # At this point, $lines should contain an even number of elements.
202 $out .= Xml::openElement( 'table', array( 'class' => 'allpageslist' ) );
203 while( count ( $lines ) > 0 ) {
204 $inpoint = array_shift( $lines );
205 $outpoint = array_shift( $lines );
206 $out .= $this->showline( $inpoint, $outpoint, $namespace );
208 $out .= Xml::closeElement( 'table' );
209 $nsForm = $this->namespaceForm( $namespace, $from, $to );
212 if( $this->including() ) {
215 if( isset($from) || isset($to) ) {
217 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
222 <td class="mw-allpages-nav">' .
223 $wgUser->getSkin()->link( $this->getTitle(), wfMsgHtml ( 'allpages' ),
224 array(), array(), 'known' ) .
227 Xml::closeElement( 'table' );
232 $wgOut->addHTML( $out2 . $out );
236 * Show a line of "ABC to DEF" ranges of articles
237 * @param string $inpoint Lower limit of pagenames
238 * @param string $outpout Upper limit of pagenames
239 * @param integer $namespace (Default NS_MAIN)
241 function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
243 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
244 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
245 // Don't let the length runaway
246 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength );
247 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength );
249 $queryparams = $namespace ? "namespace=$namespace&" : '';
250 $special = $this->getTitle();
251 $link = $special->escapeLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint) );
253 $out = wfMsgHtml( 'alphaindexline',
254 "<a href=\"$link\">$inpointf</a></td><td>",
255 "</td><td><a href=\"$link\">$outpointf</a>"
257 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
261 * @param integer $namespace (Default NS_MAIN)
262 * @param string $from list all pages from this name (default FALSE)
263 * @param string $to list all pages to this name (default FALSE)
265 function showChunk( $namespace = NS_MAIN, $from = false, $to = false ) {
266 global $wgOut, $wgUser, $wgContLang, $wgLang;
268 $sk = $wgUser->getSkin();
270 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
271 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
272 $namespaces = $wgContLang->getNamespaces();
275 if ( !$fromList || !$toList ) {
276 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
277 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
278 // Show errormessage and reset to NS_MAIN
279 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
280 $namespace = NS_MAIN;
282 list( $namespace, $fromKey, $from ) = $fromList;
283 list( $namespace2, $toKey, $to ) = $toList;
285 $dbr = wfGetDB( DB_SLAVE );
287 'page_namespace' => $namespace,
288 'page_title >= ' . $dbr->addQuotes( $fromKey )
290 if( $toKey !== "" ) {
291 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
294 $res = $dbr->select( 'page',
295 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
299 'ORDER BY' => 'page_title',
300 'LIMIT' => $this->maxPerPage + 1,
301 'USE INDEX' => 'name_title',
305 if( $res->numRows() > 0 ) {
306 $out = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
307 while( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
308 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
310 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
311 $sk->linkKnown( $t, htmlspecialchars( $t->getText() ) ) .
312 ($s->page_is_redirect ? '</div>' : '' );
314 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
319 $out .= "<td width=\"33%\">$link</td>";
325 if( ($n % 3) != 0 ) {
328 $out .= Xml::closeElement( 'table' );
334 if ( $this->including() ) {
338 // First chunk; no previous link.
341 # Get the last title from previous chunk
342 $dbr = wfGetDB( DB_SLAVE );
343 $res_prev = $dbr->select(
346 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
348 array( 'ORDER BY' => 'page_title DESC',
349 'LIMIT' => $this->maxPerPage, 'OFFSET' => ($this->maxPerPage - 1 )
353 # Get first title of previous complete chunk
354 if( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
355 $pt = $dbr->fetchObject( $res_prev );
356 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
358 # The previous chunk is not complete, need to link to the very first title
359 # available in the database
360 $options = array( 'LIMIT' => 1 );
361 if ( ! $dbr->implicitOrderby() ) {
362 $options['ORDER BY'] = 'page_title';
364 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
365 array( 'page_namespace' => $namespace ), __METHOD__, $options );
366 # Show the previous link if it s not the current requested chunk
367 if( $from != $reallyFirstPage_title ) {
368 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
375 $self = $this->getTitle();
377 $nsForm = $this->namespaceForm( $namespace, $from, $to );
378 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
383 <td class="mw-allpages-nav">' .
384 $sk->link( $self, wfMsgHtml ( 'allpages' ), array(), array(), 'known' );
386 # Do we put a previous link ?
387 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
388 $query = array( 'from' => $prevTitle->getText() );
391 $query['namespace'] = $namespace;
393 $prevLink = $sk->linkKnown(
395 htmlspecialchars( wfMsg( 'prevpage', $pt ) ),
399 $out2 = $wgLang->pipeList( array( $out2, $prevLink ) );
402 if( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
403 # $s is the first link of the next chunk
404 $t = Title::MakeTitle($namespace, $s->page_title);
405 $query = array( 'from' => $t->getText() );
408 $query['namespace'] = $namespace;
410 $nextLink = $sk->linkKnown(
412 htmlspecialchars( wfMsg( 'nextpage', $t->getText() ) ),
416 $out2 = $wgLang->pipeList( array( $out2, $nextLink ) );
418 $out2 .= "</td></tr></table>";
421 $wgOut->addHTML( $out2 . $out );
422 if( isset($prevLink) or isset($nextLink) ) {
423 $wgOut->addHTML( '<hr /><p class="mw-allpages-nav">' );
424 if( isset( $prevLink ) ) {
425 $wgOut->addHTML( $prevLink );
427 if( isset( $prevLink ) && isset( $nextLink ) ) {
428 $wgOut->addHTML( wfMsgExt( 'pipe-separator' , 'escapenoentities' ) );
430 if( isset( $nextLink ) ) {
431 $wgOut->addHTML( $nextLink );
433 $wgOut->addHTML( '</p>' );
440 * @param int $ns the namespace of the article
441 * @param string $text the name of the article
442 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
446 function getNamespaceKeyAndText($ns, $text) {
448 return array( $ns, '', '' ); # shortcut for common case
450 $t = Title::makeTitleSafe($ns, $text);
451 if ( $t && $t->isLocal() ) {
452 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
457 # try again, in case the problem was an empty pagename
458 $text = preg_replace('/(#|$)/', 'X$1', $text);
459 $t = Title::makeTitleSafe($ns, $text);
460 if ( $t && $t->isLocal() ) {
461 return array( $t->getNamespace(), '', '' );