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 ) {
62 $request = $this->getRequest();
63 $out = $this->getOutput();
66 $this->outputHeader();
67 $out->allowClickjacking();
70 $from = $request->getVal( 'from', null );
71 $to = $request->getVal( 'to', null );
72 $namespace = $request->getInt( 'namespace' );
74 $namespaces = $wgContLang->getNamespaces();
77 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces) ) ) ?
78 wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
79 wfMsg( 'allarticles' )
81 $out->addModuleStyles( 'mediawiki.special' );
84 $this->showChunk( $namespace, $par, $to );
85 } elseif( isset($from) && !isset($to) ) {
86 $this->showChunk( $namespace, $from, $to );
88 $this->showToplevel( $namespace, $from, $to );
93 * HTML for the top form
95 * @param $namespace Integer: a namespace constant (default NS_MAIN).
96 * @param $from String: dbKey we are starting listing at.
97 * @param $to String: dbKey we are ending listing at.
99 function namespaceForm( $namespace = NS_MAIN
, $from = '', $to = '' ) {
101 $t = $this->getTitle();
103 $out = Xml
::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
104 $out .= Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
105 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
106 $out .= Xml
::openElement( 'fieldset' );
107 $out .= Xml
::element( 'legend', null, wfMsg( 'allpages' ) );
108 $out .= Xml
::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
110 <td class='mw-label'>" .
111 Xml
::label( wfMsg( 'allpagesfrom' ), 'nsfrom' ) .
113 <td class='mw-input'>" .
114 Xml
::input( 'from', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
118 <td class='mw-label'>" .
119 Xml
::label( wfMsg( 'allpagesto' ), 'nsto' ) .
121 <td class='mw-input'>" .
122 Xml
::input( 'to', 30, str_replace('_',' ',$to), array( 'id' => 'nsto' ) ) .
126 <td class='mw-label'>" .
127 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
129 <td class='mw-input'>" .
130 Xml
::namespaceSelector( $namespace, null ) . ' ' .
131 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
134 $out .= Xml
::closeElement( 'table' );
135 $out .= Xml
::closeElement( 'fieldset' );
136 $out .= Xml
::closeElement( 'form' );
137 $out .= Xml
::closeElement( 'div' );
142 * @param $namespace Integer (default NS_MAIN)
143 * @param $from String: list all pages from this name
144 * @param $to String: list all pages to this name
146 function showToplevel( $namespace = NS_MAIN
, $from = '', $to = '' ) {
147 $output = $this->getOutput();
149 # TODO: Either make this *much* faster or cache the title index points
150 # in the querycache table.
152 $dbr = wfGetDB( DB_SLAVE
);
154 $where = array( 'page_namespace' => $namespace );
156 $from = Title
::makeTitleSafe( $namespace, $from );
157 $to = Title
::makeTitleSafe( $namespace, $to );
158 $from = ( $from && $from->isLocal() ) ?
$from->getDBkey() : null;
159 $to = ( $to && $to->isLocal() ) ?
$to->getDBkey() : null;
162 $where[] = 'page_title >= '.$dbr->addQuotes( $from );
164 $where[] = 'page_title <= '.$dbr->addQuotes( $to );
167 $key = wfMemcKey( 'allpages', 'ns', $namespace, $from, $to );
168 $lines = $wgMemc->get( $key );
170 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__
);
171 $maxPerSubpage = intval($count/$this->maxLineCount
);
172 $maxPerSubpage = max($maxPerSubpage,$this->maxPerPage
);
174 if( !is_array( $lines ) ) {
175 $options = array( 'LIMIT' => 1 );
176 $options['ORDER BY'] = 'page_title ASC';
177 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__
, $options );
178 $lastTitle = $firstTitle;
179 # This array is going to hold the page_titles in order.
180 $lines = array( $firstTitle );
181 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
184 // Fetch the last title of this chunk and the first of the next
185 $chunk = ( $lastTitle === false )
187 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
188 $res = $dbr->select( 'page', /* FROM */
189 'page_title', /* WHAT */
190 array_merge($where,$chunk),
192 array ('LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC')
195 $s = $dbr->fetchObject( $res );
197 array_push( $lines, $s->page_title
);
199 // Final chunk, but ended prematurely. Go back and find the end.
200 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
201 array_merge($where,$chunk),
203 array_push( $lines, $endTitle );
206 $s = $res->fetchObject();
208 array_push( $lines, $s->page_title
);
209 $lastTitle = $s->page_title
;
211 // This was a final chunk and ended exactly at the limit.
212 // Rare but convenient!
217 $wgMemc->add( $key, $lines, 3600 );
220 // If there are only two or less sections, don't even display them.
221 // Instead, display the first section directly.
222 if( count( $lines ) <= 2 ) {
223 if( !empty($lines) ) {
224 $this->showChunk( $namespace, $from, $to );
226 $output->addHTML( $this->namespaceForm( $namespace, $from, $to ) );
231 # At this point, $lines should contain an even number of elements.
232 $out .= Xml
::openElement( 'table', array( 'class' => 'allpageslist' ) );
233 while( count ( $lines ) > 0 ) {
234 $inpoint = array_shift( $lines );
235 $outpoint = array_shift( $lines );
236 $out .= $this->showline( $inpoint, $outpoint, $namespace );
238 $out .= Xml
::closeElement( 'table' );
239 $nsForm = $this->namespaceForm( $namespace, $from, $to );
242 if( $this->including() ) {
245 if( isset($from) ||
isset($to) ) {
246 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
251 <td class="mw-allpages-nav">' .
252 $this->getSkin()->link( $this->getTitle(), wfMsgHtml ( 'allpages' ),
253 array(), array(), 'known' ) .
256 Xml
::closeElement( 'table' );
261 $output->addHTML( $out2 . $out );
265 * Show a line of "ABC to DEF" ranges of articles
267 * @param $inpoint String: lower limit of pagenames
268 * @param $outpoint String: upper limit of pagenames
269 * @param $namespace Integer (Default NS_MAIN)
271 function showline( $inpoint, $outpoint, $namespace = NS_MAIN
) {
273 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
274 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
275 // Don't let the length runaway
276 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength
);
277 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength
);
279 $queryparams = $namespace ?
"namespace=$namespace&" : '';
280 $special = $this->getTitle();
281 $link = $special->escapeLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint) );
283 $out = wfMsgHtml( 'alphaindexline',
284 "<a href=\"$link\">$inpointf</a></td><td>",
285 "</td><td><a href=\"$link\">$outpointf</a>"
287 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
291 * @param $namespace Integer (Default NS_MAIN)
292 * @param $from String: list all pages from this name (default FALSE)
293 * @param $to String: list all pages to this name (default FALSE)
295 function showChunk( $namespace = NS_MAIN
, $from = false, $to = false ) {
296 global $wgContLang, $wgLang;
297 $output = $this->getOutput();
298 $sk = $this->getSkin();
300 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
301 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
302 $namespaces = $wgContLang->getNamespaces();
305 if ( !$fromList ||
!$toList ) {
306 $out = wfMsgExt( 'allpagesbadtitle', 'parse' );
307 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
308 // Show errormessage and reset to NS_MAIN
309 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
310 $namespace = NS_MAIN
;
312 list( $namespace, $fromKey, $from ) = $fromList;
313 list( , $toKey, $to ) = $toList;
315 $dbr = wfGetDB( DB_SLAVE
);
317 'page_namespace' => $namespace,
318 'page_title >= ' . $dbr->addQuotes( $fromKey )
320 if( $toKey !== "" ) {
321 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
324 $res = $dbr->select( 'page',
325 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
329 'ORDER BY' => 'page_title',
330 'LIMIT' => $this->maxPerPage +
1,
331 'USE INDEX' => 'name_title',
335 if( $res->numRows() > 0 ) {
336 $out = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
337 while( ( $n < $this->maxPerPage
) && ( $s = $res->fetchObject() ) ) {
338 $t = Title
::newFromRow( $s );
340 $link = ( $s->page_is_redirect ?
'<div class="allpagesredirect">' : '' ) .
342 ($s->page_is_redirect ?
'</div>' : '' );
344 $link = '[[' . htmlspecialchars( $s->page_title
) . ']]';
349 $out .= "<td style=\"width:33%\">$link</td>";
355 if( ($n %
3) != 0 ) {
358 $out .= Xml
::closeElement( 'table' );
364 if ( $this->including() ) {
368 // First chunk; no previous link.
371 # Get the last title from previous chunk
372 $dbr = wfGetDB( DB_SLAVE
);
373 $res_prev = $dbr->select(
376 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
378 array( 'ORDER BY' => 'page_title DESC',
379 'LIMIT' => $this->maxPerPage
, 'OFFSET' => ($this->maxPerPage
- 1 )
383 # Get first title of previous complete chunk
384 if( $dbr->numrows( $res_prev ) >= $this->maxPerPage
) {
385 $pt = $dbr->fetchObject( $res_prev );
386 $prevTitle = Title
::makeTitle( $namespace, $pt->page_title
);
388 # The previous chunk is not complete, need to link to the very first title
389 # available in the database
390 $options = array( 'LIMIT' => 1 );
391 if ( ! $dbr->implicitOrderby() ) {
392 $options['ORDER BY'] = 'page_title';
394 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
395 array( 'page_namespace' => $namespace ), __METHOD__
, $options );
396 # Show the previous link if it s not the current requested chunk
397 if( $from != $reallyFirstPage_title ) {
398 $prevTitle = Title
::makeTitle( $namespace, $reallyFirstPage_title );
405 $self = $this->getTitle();
407 $nsForm = $this->namespaceForm( $namespace, $from, $to );
408 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
413 <td class="mw-allpages-nav">' .
414 $sk->link( $self, wfMsgHtml ( 'allpages' ) );
416 # Do we put a previous link ?
417 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
418 $query = array( 'from' => $prevTitle->getText() );
421 $query['namespace'] = $namespace;
423 $prevLink = $sk->linkKnown(
425 wfMessage( 'prevpage', $pt )->escaped(),
429 $out2 = $wgLang->pipeList( array( $out2, $prevLink ) );
432 if( $n == $this->maxPerPage
&& $s = $res->fetchObject() ) {
433 # $s is the first link of the next chunk
434 $t = Title
::MakeTitle($namespace, $s->page_title
);
435 $query = array( 'from' => $t->getText() );
438 $query['namespace'] = $namespace;
440 $nextLink = $sk->linkKnown(
442 wfMessage( 'nextpage', $t->getText() )->escaped(),
446 $out2 = $wgLang->pipeList( array( $out2, $nextLink ) );
448 $out2 .= "</td></tr></table>";
451 $output->addHTML( $out2 . $out );
454 if ( isset( $prevLink ) ) $links[] = $prevLink;
455 if ( isset( $nextLink ) ) $links[] = $nextLink;
457 if ( count( $links ) ) {
459 Html
::element( 'hr' ) .
460 Html
::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
461 $wgLang->pipeList( $links )
468 * @param $ns Integer: the namespace of the article
469 * @param $text String: the name of the article
470 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
474 function getNamespaceKeyAndText($ns, $text) {
476 return array( $ns, '', '' ); # shortcut for common case
478 $t = Title
::makeTitleSafe($ns, $text);
479 if ( $t && $t->isLocal() ) {
480 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
485 # try again, in case the problem was an empty pagename
486 $text = preg_replace('/(#|$)/', 'X$1', $text);
487 $t = Title
::makeTitleSafe($ns, $text);
488 if ( $t && $t->isLocal() ) {
489 return array( $t->getNamespace(), '', '' );