3 * Implements Special:Allpages
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Implements Special:Allpages
27 * @ingroup SpecialPage
29 class SpecialAllpages
extends IncludableSpecialPage
{
32 * Maximum number of pages to show on single subpage.
34 protected $maxPerPage = 345;
37 * Maximum number of pages to show on single index subpage.
39 protected $maxLineCount = 100;
42 * Maximum number of chars to show for an entry.
44 protected $maxPageLength = 70;
47 * Determines, which message describes the input field 'nsfrom'.
49 protected $nsfromMsg = 'allpagesfrom';
51 function __construct( $name = 'Allpages' ){
52 parent
::__construct( $name );
56 * Entry point : initialise variables and call subfunctions.
58 * @param $par String: becomes "FOO" when called like Special:Allpages/FOO (default NULL)
60 function execute( $par ) {
61 global $wgRequest, $wgOut, $wgContLang;
64 $this->outputHeader();
65 $wgOut->allowClickjacking();
68 $from = $wgRequest->getVal( 'from', null );
69 $to = $wgRequest->getVal( 'to', null );
70 $namespace = $wgRequest->getInt( 'namespace' );
72 $namespaces = $wgContLang->getNamespaces();
75 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces) ) ) ?
76 wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
77 wfMsg( 'allarticles' )
79 $wgOut->addModuleStyles( 'mediawiki.special' );
82 $this->showChunk( $namespace, $par, $to );
83 } elseif( isset($from) && !isset($to) ) {
84 $this->showChunk( $namespace, $from, $to );
86 $this->showToplevel( $namespace, $from, $to );
91 * HTML for the top form
93 * @param $namespace Integer: a namespace constant (default NS_MAIN).
94 * @param $from String: dbKey we are starting listing at.
95 * @param $to String: dbKey we are ending listing at.
97 function namespaceForm( $namespace = NS_MAIN
, $from = '', $to = '' ) {
99 $t = $this->getTitle();
101 $out = Xml
::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
102 $out .= Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
103 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
104 $out .= Xml
::openElement( 'fieldset' );
105 $out .= Xml
::element( 'legend', null, wfMsg( 'allpages' ) );
106 $out .= Xml
::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
108 <td class='mw-label'>" .
109 Xml
::label( wfMsg( 'allpagesfrom' ), 'nsfrom' ) .
111 <td class='mw-input'>" .
112 Xml
::input( 'from', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
116 <td class='mw-label'>" .
117 Xml
::label( wfMsg( 'allpagesto' ), 'nsto' ) .
119 <td class='mw-input'>" .
120 Xml
::input( 'to', 30, str_replace('_',' ',$to), array( 'id' => 'nsto' ) ) .
124 <td class='mw-label'>" .
125 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
127 <td class='mw-input'>" .
128 Xml
::namespaceSelector( $namespace, null ) . ' ' .
129 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
132 $out .= Xml
::closeElement( 'table' );
133 $out .= Xml
::closeElement( 'fieldset' );
134 $out .= Xml
::closeElement( 'form' );
135 $out .= Xml
::closeElement( 'div' );
140 * @param $namespace Integer (default NS_MAIN)
141 * @param $from String: list all pages from this name
142 * @param $to String: list all pages to this name
144 function showToplevel( $namespace = NS_MAIN
, $from = '', $to = '' ) {
147 # TODO: Either make this *much* faster or cache the title index points
148 # in the querycache table.
150 $dbr = wfGetDB( DB_SLAVE
);
152 $where = array( 'page_namespace' => $namespace );
154 $from = Title
::makeTitleSafe( $namespace, $from );
155 $to = Title
::makeTitleSafe( $namespace, $to );
156 $from = ( $from && $from->isLocal() ) ?
$from->getDBkey() : null;
157 $to = ( $to && $to->isLocal() ) ?
$to->getDBkey() : null;
160 $where[] = 'page_title >= '.$dbr->addQuotes( $from );
162 $where[] = 'page_title <= '.$dbr->addQuotes( $to );
165 $key = wfMemcKey( 'allpages', 'ns', $namespace, $from, $to );
166 $lines = $wgMemc->get( $key );
168 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__
);
169 $maxPerSubpage = intval($count/$this->maxLineCount
);
170 $maxPerSubpage = max($maxPerSubpage,$this->maxPerPage
);
172 if( !is_array( $lines ) ) {
173 $options = array( 'LIMIT' => 1 );
174 $options['ORDER BY'] = 'page_title ASC';
175 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__
, $options );
176 $lastTitle = $firstTitle;
177 # This array is going to hold the page_titles in order.
178 $lines = array( $firstTitle );
179 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
182 // Fetch the last title of this chunk and the first of the next
183 $chunk = ( $lastTitle === false )
185 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
186 $res = $dbr->select( 'page', /* FROM */
187 'page_title', /* WHAT */
188 array_merge($where,$chunk),
190 array ('LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC')
193 $s = $dbr->fetchObject( $res );
195 array_push( $lines, $s->page_title
);
197 // Final chunk, but ended prematurely. Go back and find the end.
198 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
199 array_merge($where,$chunk),
201 array_push( $lines, $endTitle );
204 $s = $res->fetchObject();
206 array_push( $lines, $s->page_title
);
207 $lastTitle = $s->page_title
;
209 // This was a final chunk and ended exactly at the limit.
210 // Rare but convenient!
215 $wgMemc->add( $key, $lines, 3600 );
218 // If there are only two or less sections, don't even display them.
219 // Instead, display the first section directly.
220 if( count( $lines ) <= 2 ) {
221 if( !empty($lines) ) {
222 $this->showChunk( $namespace, $from, $to );
224 $wgOut->addHTML( $this->namespaceForm( $namespace, $from, $to ) );
229 # At this point, $lines should contain an even number of elements.
230 $out .= Xml
::openElement( 'table', array( 'class' => 'allpageslist' ) );
231 while( count ( $lines ) > 0 ) {
232 $inpoint = array_shift( $lines );
233 $outpoint = array_shift( $lines );
234 $out .= $this->showline( $inpoint, $outpoint, $namespace );
236 $out .= Xml
::closeElement( 'table' );
237 $nsForm = $this->namespaceForm( $namespace, $from, $to );
240 if( $this->including() ) {
243 if( isset($from) ||
isset($to) ) {
245 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
250 <td class="mw-allpages-nav">' .
251 $wgUser->getSkin()->link( $this->getTitle(), wfMsgHtml ( 'allpages' ),
252 array(), array(), 'known' ) .
255 Xml
::closeElement( 'table' );
260 $wgOut->addHTML( $out2 . $out );
264 * Show a line of "ABC to DEF" ranges of articles
266 * @param $inpoint String: lower limit of pagenames
267 * @param $outpoint String: upper limit of pagenames
268 * @param $namespace Integer (Default NS_MAIN)
270 function showline( $inpoint, $outpoint, $namespace = NS_MAIN
) {
272 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
273 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
274 // Don't let the length runaway
275 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength
);
276 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength
);
278 $queryparams = $namespace ?
"namespace=$namespace&" : '';
279 $special = $this->getTitle();
280 $link = $special->escapeLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint) );
282 $out = wfMsgHtml( 'alphaindexline',
283 "<a href=\"$link\">$inpointf</a></td><td>",
284 "</td><td><a href=\"$link\">$outpointf</a>"
286 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
290 * @param $namespace Integer (Default NS_MAIN)
291 * @param $from String: list all pages from this name (default FALSE)
292 * @param $to String: list all pages to this name (default FALSE)
294 function showChunk( $namespace = NS_MAIN
, $from = false, $to = false ) {
295 global $wgOut, $wgUser, $wgContLang, $wgLang;
297 $sk = $wgUser->getSkin();
299 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
300 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
301 $namespaces = $wgContLang->getNamespaces();
304 if ( !$fromList ||
!$toList ) {
305 $out = wfMsgExt( 'allpagesbadtitle', 'parse' );
306 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
307 // Show errormessage and reset to NS_MAIN
308 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
309 $namespace = NS_MAIN
;
311 list( $namespace, $fromKey, $from ) = $fromList;
312 list( , $toKey, $to ) = $toList;
314 $dbr = wfGetDB( DB_SLAVE
);
316 'page_namespace' => $namespace,
317 'page_title >= ' . $dbr->addQuotes( $fromKey )
319 if( $toKey !== "" ) {
320 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
323 $res = $dbr->select( 'page',
324 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
328 'ORDER BY' => 'page_title',
329 'LIMIT' => $this->maxPerPage +
1,
330 'USE INDEX' => 'name_title',
334 if( $res->numRows() > 0 ) {
335 $out = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
336 while( ( $n < $this->maxPerPage
) && ( $s = $res->fetchObject() ) ) {
337 $t = Title
::newFromRow( $s );
339 $link = ( $s->page_is_redirect ?
'<div class="allpagesredirect">' : '' ) .
341 ($s->page_is_redirect ?
'</div>' : '' );
343 $link = '[[' . htmlspecialchars( $s->page_title
) . ']]';
348 $out .= "<td style=\"width:33%\">$link</td>";
354 if( ($n %
3) != 0 ) {
357 $out .= Xml
::closeElement( 'table' );
363 if ( $this->including() ) {
367 // First chunk; no previous link.
370 # Get the last title from previous chunk
371 $dbr = wfGetDB( DB_SLAVE
);
372 $res_prev = $dbr->select(
375 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
377 array( 'ORDER BY' => 'page_title DESC',
378 'LIMIT' => $this->maxPerPage
, 'OFFSET' => ($this->maxPerPage
- 1 )
382 # Get first title of previous complete chunk
383 if( $dbr->numrows( $res_prev ) >= $this->maxPerPage
) {
384 $pt = $dbr->fetchObject( $res_prev );
385 $prevTitle = Title
::makeTitle( $namespace, $pt->page_title
);
387 # The previous chunk is not complete, need to link to the very first title
388 # available in the database
389 $options = array( 'LIMIT' => 1 );
390 if ( ! $dbr->implicitOrderby() ) {
391 $options['ORDER BY'] = 'page_title';
393 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
394 array( 'page_namespace' => $namespace ), __METHOD__
, $options );
395 # Show the previous link if it s not the current requested chunk
396 if( $from != $reallyFirstPage_title ) {
397 $prevTitle = Title
::makeTitle( $namespace, $reallyFirstPage_title );
404 $self = $this->getTitle();
406 $nsForm = $this->namespaceForm( $namespace, $from, $to );
407 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
412 <td class="mw-allpages-nav">' .
413 $sk->link( $self, wfMsgHtml ( 'allpages' ) );
415 # Do we put a previous link ?
416 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
417 $query = array( 'from' => $prevTitle->getText() );
420 $query['namespace'] = $namespace;
422 $prevLink = $sk->linkKnown(
424 wfMessage( 'prevpage', $pt )->escaped(),
428 $out2 = $wgLang->pipeList( array( $out2, $prevLink ) );
431 if( $n == $this->maxPerPage
&& $s = $res->fetchObject() ) {
432 # $s is the first link of the next chunk
433 $t = Title
::MakeTitle($namespace, $s->page_title
);
434 $query = array( 'from' => $t->getText() );
437 $query['namespace'] = $namespace;
439 $nextLink = $sk->linkKnown(
441 wfMessage( 'nextpage', $t->getText() )->escaped(),
445 $out2 = $wgLang->pipeList( array( $out2, $nextLink ) );
447 $out2 .= "</td></tr></table>";
450 $wgOut->addHTML( $out2 . $out );
453 if ( isset( $prevLink ) ) $links[] = $prevLink;
454 if ( isset( $nextLink ) ) $links[] = $nextLink;
456 if ( count( $links ) ) {
458 Html
::element( 'hr' ) .
459 Html
::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
460 $wgLang->pipeList( $links )
467 * @param $ns Integer: the namespace of the article
468 * @param $text String: the name of the article
469 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
473 function getNamespaceKeyAndText($ns, $text) {
475 return array( $ns, '', '' ); # shortcut for common case
477 $t = Title
::makeTitleSafe($ns, $text);
478 if ( $t && $t->isLocal() ) {
479 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
484 # try again, in case the problem was an empty pagename
485 $text = preg_replace('/(#|$)/', 'X$1', $text);
486 $t = Title
::makeTitleSafe($ns, $text);
487 if ( $t && $t->isLocal() ) {
488 return array( $t->getNamespace(), '', '' );