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();
67 $from = $wgRequest->getVal( 'from', null );
68 $to = $wgRequest->getVal( 'to', null );
69 $namespace = $wgRequest->getInt( 'namespace' );
71 $namespaces = $wgContLang->getNamespaces();
74 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces) ) ) ?
75 wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
76 wfMsg( 'allarticles' )
80 $this->showChunk( $namespace, $par, $to );
81 } elseif( isset($from) && !isset($to) ) {
82 $this->showChunk( $namespace, $from, $to );
84 $this->showToplevel( $namespace, $from, $to );
89 * HTML for the top form
91 * @param $namespace Integer: a namespace constant (default NS_MAIN).
92 * @param $from String: dbKey we are starting listing at.
93 * @param $to String: dbKey we are ending listing at.
95 function namespaceForm( $namespace = NS_MAIN
, $from = '', $to = '' ) {
97 $t = $this->getTitle();
99 $out = Xml
::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
100 $out .= Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
101 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
102 $out .= Xml
::openElement( 'fieldset' );
103 $out .= Xml
::element( 'legend', null, wfMsg( 'allpages' ) );
104 $out .= Xml
::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
106 <td class='mw-label'>" .
107 Xml
::label( wfMsg( 'allpagesfrom' ), 'nsfrom' ) .
109 <td class='mw-input'>" .
110 Xml
::input( 'from', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
114 <td class='mw-label'>" .
115 Xml
::label( wfMsg( 'allpagesto' ), 'nsto' ) .
117 <td class='mw-input'>" .
118 Xml
::input( 'to', 30, str_replace('_',' ',$to), array( 'id' => 'nsto' ) ) .
122 <td class='mw-label'>" .
123 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
125 <td class='mw-input'>" .
126 Xml
::namespaceSelector( $namespace, null ) . ' ' .
127 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
130 $out .= Xml
::closeElement( 'table' );
131 $out .= Xml
::closeElement( 'fieldset' );
132 $out .= Xml
::closeElement( 'form' );
133 $out .= Xml
::closeElement( 'div' );
138 * @param $namespace Integer (default NS_MAIN)
139 * @param $from String: list all pages from this name
140 * @param $to String: list all pages to this name
142 function showToplevel( $namespace = NS_MAIN
, $from = '', $to = '' ) {
145 # TODO: Either make this *much* faster or cache the title index points
146 # in the querycache table.
148 $dbr = wfGetDB( DB_SLAVE
);
150 $where = array( 'page_namespace' => $namespace );
152 $from = Title
::makeTitleSafe( $namespace, $from );
153 $to = Title
::makeTitleSafe( $namespace, $to );
154 $from = ( $from && $from->isLocal() ) ?
$from->getDBkey() : null;
155 $to = ( $to && $to->isLocal() ) ?
$to->getDBkey() : null;
158 $where[] = 'page_title >= '.$dbr->addQuotes( $from );
160 $where[] = 'page_title <= '.$dbr->addQuotes( $to );
163 $key = wfMemcKey( 'allpages', 'ns', $namespace, $from, $to );
164 $lines = $wgMemc->get( $key );
166 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__
);
167 $maxPerSubpage = intval($count/$this->maxLineCount
);
168 $maxPerSubpage = max($maxPerSubpage,$this->maxPerPage
);
170 if( !is_array( $lines ) ) {
171 $options = array( 'LIMIT' => 1 );
172 $options['ORDER BY'] = 'page_title ASC';
173 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__
, $options );
174 $lastTitle = $firstTitle;
175 # This array is going to hold the page_titles in order.
176 $lines = array( $firstTitle );
177 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
180 // Fetch the last title of this chunk and the first of the next
181 $chunk = ( $lastTitle === false )
183 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
184 $res = $dbr->select( 'page', /* FROM */
185 'page_title', /* WHAT */
186 array_merge($where,$chunk),
188 array ('LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC')
191 if( $s = $dbr->fetchObject( $res ) ) {
192 array_push( $lines, $s->page_title
);
194 // Final chunk, but ended prematurely. Go back and find the end.
195 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
196 array_merge($where,$chunk),
198 array_push( $lines, $endTitle );
201 if( $s = $res->fetchObject() ) {
202 array_push( $lines, $s->page_title
);
203 $lastTitle = $s->page_title
;
205 // This was a final chunk and ended exactly at the limit.
206 // Rare but convenient!
211 $wgMemc->add( $key, $lines, 3600 );
214 // If there are only two or less sections, don't even display them.
215 // Instead, display the first section directly.
216 if( count( $lines ) <= 2 ) {
217 if( !empty($lines) ) {
218 $this->showChunk( $namespace, $from, $to );
220 $wgOut->addHTML( $this->namespaceForm( $namespace, $from, $to ) );
225 # At this point, $lines should contain an even number of elements.
226 $out .= Xml
::openElement( 'table', array( 'class' => 'allpageslist' ) );
227 while( count ( $lines ) > 0 ) {
228 $inpoint = array_shift( $lines );
229 $outpoint = array_shift( $lines );
230 $out .= $this->showline( $inpoint, $outpoint, $namespace );
232 $out .= Xml
::closeElement( 'table' );
233 $nsForm = $this->namespaceForm( $namespace, $from, $to );
236 if( $this->including() ) {
239 if( isset($from) ||
isset($to) ) {
241 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
246 <td class="mw-allpages-nav">' .
247 $wgUser->getSkin()->link( $this->getTitle(), wfMsgHtml ( 'allpages' ),
248 array(), array(), 'known' ) .
251 Xml
::closeElement( 'table' );
256 $wgOut->addHTML( $out2 . $out );
260 * Show a line of "ABC to DEF" ranges of articles
262 * @param $inpoint String: lower limit of pagenames
263 * @param $outpoint String: upper limit of pagenames
264 * @param $namespace Integer (Default NS_MAIN)
266 function showline( $inpoint, $outpoint, $namespace = NS_MAIN
) {
268 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
269 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
270 // Don't let the length runaway
271 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength
);
272 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength
);
274 $queryparams = $namespace ?
"namespace=$namespace&" : '';
275 $special = $this->getTitle();
276 $link = $special->escapeLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint) );
278 $out = wfMsgHtml( 'alphaindexline',
279 "<a href=\"$link\">$inpointf</a></td><td>",
280 "</td><td><a href=\"$link\">$outpointf</a>"
282 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
286 * @param $namespace Integer (Default NS_MAIN)
287 * @param $from String: list all pages from this name (default FALSE)
288 * @param $to String: list all pages to this name (default FALSE)
290 function showChunk( $namespace = NS_MAIN
, $from = false, $to = false ) {
291 global $wgOut, $wgUser, $wgContLang, $wgLang;
293 $sk = $wgUser->getSkin();
295 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
296 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
297 $namespaces = $wgContLang->getNamespaces();
300 if ( !$fromList ||
!$toList ) {
301 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
302 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
303 // Show errormessage and reset to NS_MAIN
304 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
305 $namespace = NS_MAIN
;
307 list( $namespace, $fromKey, $from ) = $fromList;
308 list( $namespace2, $toKey, $to ) = $toList;
310 $dbr = wfGetDB( DB_SLAVE
);
312 'page_namespace' => $namespace,
313 'page_title >= ' . $dbr->addQuotes( $fromKey )
315 if( $toKey !== "" ) {
316 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
319 $res = $dbr->select( 'page',
320 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
324 'ORDER BY' => 'page_title',
325 'LIMIT' => $this->maxPerPage +
1,
326 'USE INDEX' => 'name_title',
330 if( $res->numRows() > 0 ) {
331 $out = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
332 while( ( $n < $this->maxPerPage
) && ( $s = $res->fetchObject() ) ) {
333 $t = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
335 $link = ( $s->page_is_redirect ?
'<div class="allpagesredirect">' : '' ) .
336 $sk->linkKnown( $t, htmlspecialchars( $t->getText() ) ) .
337 ($s->page_is_redirect ?
'</div>' : '' );
339 $link = '[[' . htmlspecialchars( $s->page_title
) . ']]';
344 $out .= "<td style=\"width:33%\">$link</td>";
350 if( ($n %
3) != 0 ) {
353 $out .= Xml
::closeElement( 'table' );
359 if ( $this->including() ) {
363 // First chunk; no previous link.
366 # Get the last title from previous chunk
367 $dbr = wfGetDB( DB_SLAVE
);
368 $res_prev = $dbr->select(
371 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
373 array( 'ORDER BY' => 'page_title DESC',
374 'LIMIT' => $this->maxPerPage
, 'OFFSET' => ($this->maxPerPage
- 1 )
378 # Get first title of previous complete chunk
379 if( $dbr->numrows( $res_prev ) >= $this->maxPerPage
) {
380 $pt = $dbr->fetchObject( $res_prev );
381 $prevTitle = Title
::makeTitle( $namespace, $pt->page_title
);
383 # The previous chunk is not complete, need to link to the very first title
384 # available in the database
385 $options = array( 'LIMIT' => 1 );
386 if ( ! $dbr->implicitOrderby() ) {
387 $options['ORDER BY'] = 'page_title';
389 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
390 array( 'page_namespace' => $namespace ), __METHOD__
, $options );
391 # Show the previous link if it s not the current requested chunk
392 if( $from != $reallyFirstPage_title ) {
393 $prevTitle = Title
::makeTitle( $namespace, $reallyFirstPage_title );
400 $self = $this->getTitle();
402 $nsForm = $this->namespaceForm( $namespace, $from, $to );
403 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
408 <td class="mw-allpages-nav">' .
409 $sk->link( $self, wfMsgHtml ( 'allpages' ), array(), array(), 'known' );
411 # Do we put a previous link ?
412 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
413 $query = array( 'from' => $prevTitle->getText() );
416 $query['namespace'] = $namespace;
418 $prevLink = $sk->linkKnown(
420 htmlspecialchars( wfMsg( 'prevpage', $pt ) ),
424 $out2 = $wgLang->pipeList( array( $out2, $prevLink ) );
427 if( $n == $this->maxPerPage
&& $s = $res->fetchObject() ) {
428 # $s is the first link of the next chunk
429 $t = Title
::MakeTitle($namespace, $s->page_title
);
430 $query = array( 'from' => $t->getText() );
433 $query['namespace'] = $namespace;
435 $nextLink = $sk->linkKnown(
437 htmlspecialchars( wfMsg( 'nextpage', $t->getText() ) ),
441 $out2 = $wgLang->pipeList( array( $out2, $nextLink ) );
443 $out2 .= "</td></tr></table>";
446 $wgOut->addHTML( $out2 . $out );
447 if( isset($prevLink) or isset($nextLink) ) {
448 $wgOut->addHTML( '<hr /><p class="mw-allpages-nav">' );
449 if( isset( $prevLink ) ) {
450 $wgOut->addHTML( $prevLink );
452 if( isset( $prevLink ) && isset( $nextLink ) ) {
453 $wgOut->addHTML( wfMsgExt( 'pipe-separator' , 'escapenoentities' ) );
455 if( isset( $nextLink ) ) {
456 $wgOut->addHTML( $nextLink );
458 $wgOut->addHTML( '</p>' );
465 * @param $ns Integer: the namespace of the article
466 * @param $text String: the name of the article
467 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
471 function getNamespaceKeyAndText($ns, $text) {
473 return array( $ns, '', '' ); # shortcut for common case
475 $t = Title
::makeTitleSafe($ns, $text);
476 if ( $t && $t->isLocal() ) {
477 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
482 # try again, in case the problem was an empty pagename
483 $text = preg_replace('/(#|$)/', 'X$1', $text);
484 $t = Title
::makeTitleSafe($ns, $text);
485 if ( $t && $t->isLocal() ) {
486 return array( $t->getNamespace(), '', '' );