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 $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
79 $this->msg( 'allarticles' )
81 $out->addModuleStyles( 'mediawiki.special' );
84 $this->showChunk( $namespace, $par, $to );
85 } elseif( $from !== null && $to === null ) {
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, $this->msg( 'allpages' )->text() );
108 $out .= Xml
::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
110 <td class='mw-label'>" .
111 Xml
::label( $this->msg( 'allpagesfrom' )->text(), '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( $this->msg( 'allpagesto' )->text(), '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( $this->msg( 'namespace' )->text(), 'namespace' ) .
129 <td class='mw-input'>" .
130 Html
::namespaceSelector(
131 array( 'selected' => $namespace ),
132 array( 'name' => 'namespace', 'id' => 'namespace' )
134 Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
137 $out .= Xml
::closeElement( 'table' );
138 $out .= Xml
::closeElement( 'fieldset' );
139 $out .= Xml
::closeElement( 'form' );
140 $out .= Xml
::closeElement( 'div' );
145 * @param $namespace Integer (default NS_MAIN)
146 * @param $from String: list all pages from this name
147 * @param $to String: list all pages to this name
149 function showToplevel( $namespace = NS_MAIN
, $from = '', $to = '' ) {
150 $output = $this->getOutput();
152 # TODO: Either make this *much* faster or cache the title index points
153 # in the querycache table.
155 $dbr = wfGetDB( DB_SLAVE
);
157 $where = array( 'page_namespace' => $namespace );
159 $from = Title
::makeTitleSafe( $namespace, $from );
160 $to = Title
::makeTitleSafe( $namespace, $to );
161 $from = ( $from && $from->isLocal() ) ?
$from->getDBkey() : null;
162 $to = ( $to && $to->isLocal() ) ?
$to->getDBkey() : null;
165 $where[] = 'page_title >= '.$dbr->addQuotes( $from );
167 $where[] = 'page_title <= '.$dbr->addQuotes( $to );
170 $key = wfMemcKey( 'allpages', 'ns', $namespace, $from, $to );
171 $lines = $wgMemc->get( $key );
173 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__
);
174 $maxPerSubpage = intval($count/$this->maxLineCount
);
175 $maxPerSubpage = max($maxPerSubpage,$this->maxPerPage
);
177 if( !is_array( $lines ) ) {
178 $options = array( 'LIMIT' => 1 );
179 $options['ORDER BY'] = 'page_title ASC';
180 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__
, $options );
181 $lastTitle = $firstTitle;
182 # This array is going to hold the page_titles in order.
183 $lines = array( $firstTitle );
184 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
187 // Fetch the last title of this chunk and the first of the next
188 $chunk = ( $lastTitle === false )
190 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
191 $res = $dbr->select( 'page', /* FROM */
192 'page_title', /* WHAT */
193 array_merge($where,$chunk),
195 array ('LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC')
198 $s = $dbr->fetchObject( $res );
200 array_push( $lines, $s->page_title
);
202 // Final chunk, but ended prematurely. Go back and find the end.
203 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
204 array_merge($where,$chunk),
206 array_push( $lines, $endTitle );
209 $s = $res->fetchObject();
211 array_push( $lines, $s->page_title
);
212 $lastTitle = $s->page_title
;
214 // This was a final chunk and ended exactly at the limit.
215 // Rare but convenient!
220 $wgMemc->add( $key, $lines, 3600 );
223 // If there are only two or less sections, don't even display them.
224 // Instead, display the first section directly.
225 if( count( $lines ) <= 2 ) {
226 if( !empty($lines) ) {
227 $this->showChunk( $namespace, $from, $to );
229 $output->addHTML( $this->namespaceForm( $namespace, $from, $to ) );
234 # At this point, $lines should contain an even number of elements.
235 $out .= Xml
::openElement( 'table', array( 'class' => 'allpageslist' ) );
236 while( count ( $lines ) > 0 ) {
237 $inpoint = array_shift( $lines );
238 $outpoint = array_shift( $lines );
239 $out .= $this->showline( $inpoint, $outpoint, $namespace );
241 $out .= Xml
::closeElement( 'table' );
242 $nsForm = $this->namespaceForm( $namespace, $from, $to );
245 if( $this->including() ) {
248 if( isset($from) ||
isset($to) ) {
249 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
254 <td class="mw-allpages-nav">' .
255 Linker
::link( $this->getTitle(), $this->msg( 'allpages' )->escaped(),
256 array(), array(), 'known' ) .
259 Xml
::closeElement( 'table' );
264 $output->addHTML( $out2 . $out );
268 * Show a line of "ABC to DEF" ranges of articles
270 * @param $inpoint String: lower limit of pagenames
271 * @param $outpoint String: upper limit of pagenames
272 * @param $namespace Integer (Default NS_MAIN)
274 function showline( $inpoint, $outpoint, $namespace = NS_MAIN
) {
276 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
277 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
278 // Don't let the length runaway
279 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength
);
280 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength
);
282 $queryparams = $namespace ?
"namespace=$namespace&" : '';
283 $special = $this->getTitle();
284 $link = htmlspecialchars( $special->getLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint) ) );
286 $out = $this->msg( 'alphaindexline' )->rawParams(
287 "<a href=\"$link\">$inpointf</a></td><td>",
288 "</td><td><a href=\"$link\">$outpointf</a>"
290 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
294 * @param $namespace Integer (Default NS_MAIN)
295 * @param $from String: list all pages from this name (default FALSE)
296 * @param $to String: list all pages to this name (default FALSE)
298 function showChunk( $namespace = NS_MAIN
, $from = false, $to = false ) {
300 $output = $this->getOutput();
302 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
303 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
304 $namespaces = $wgContLang->getNamespaces();
307 if ( !$fromList ||
!$toList ) {
308 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
309 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
310 // Show errormessage and reset to NS_MAIN
311 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
312 $namespace = NS_MAIN
;
314 list( $namespace, $fromKey, $from ) = $fromList;
315 list( , $toKey, $to ) = $toList;
317 $dbr = wfGetDB( DB_SLAVE
);
319 'page_namespace' => $namespace,
320 'page_title >= ' . $dbr->addQuotes( $fromKey )
322 if( $toKey !== "" ) {
323 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
326 $res = $dbr->select( 'page',
327 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
331 'ORDER BY' => 'page_title',
332 'LIMIT' => $this->maxPerPage +
1,
333 'USE INDEX' => 'name_title',
337 if( $res->numRows() > 0 ) {
338 $out = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
339 while( ( $n < $this->maxPerPage
) && ( $s = $res->fetchObject() ) ) {
340 $t = Title
::newFromRow( $s );
342 $link = ( $s->page_is_redirect ?
'<div class="allpagesredirect">' : '' ) .
344 ($s->page_is_redirect ?
'</div>' : '' );
346 $link = '[[' . htmlspecialchars( $s->page_title
) . ']]';
351 $out .= "<td style=\"width:33%\">$link</td>";
357 if( ($n %
3) != 0 ) {
360 $out .= Xml
::closeElement( 'table' );
366 if ( $this->including() ) {
370 // First chunk; no previous link.
373 # Get the last title from previous chunk
374 $dbr = wfGetDB( DB_SLAVE
);
375 $res_prev = $dbr->select(
378 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
380 array( 'ORDER BY' => 'page_title DESC',
381 'LIMIT' => $this->maxPerPage
, 'OFFSET' => ($this->maxPerPage
- 1 )
385 # Get first title of previous complete chunk
386 if( $dbr->numrows( $res_prev ) >= $this->maxPerPage
) {
387 $pt = $dbr->fetchObject( $res_prev );
388 $prevTitle = Title
::makeTitle( $namespace, $pt->page_title
);
390 # The previous chunk is not complete, need to link to the very first title
391 # available in the database
392 $options = array( 'LIMIT' => 1 );
393 if ( ! $dbr->implicitOrderby() ) {
394 $options['ORDER BY'] = 'page_title';
396 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
397 array( 'page_namespace' => $namespace ), __METHOD__
, $options );
398 # Show the previous link if it s not the current requested chunk
399 if( $from != $reallyFirstPage_title ) {
400 $prevTitle = Title
::makeTitle( $namespace, $reallyFirstPage_title );
407 $self = $this->getTitle();
409 $nsForm = $this->namespaceForm( $namespace, $from, $to );
410 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
415 <td class="mw-allpages-nav">' .
416 Linker
::link( $self, $this->msg( 'allpages' )->escaped() );
418 # Do we put a previous link ?
419 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
420 $query = array( 'from' => $prevTitle->getText() );
423 $query['namespace'] = $namespace;
425 $prevLink = Linker
::linkKnown(
427 $this->msg( 'prevpage', $pt )->escaped(),
431 $out2 = $this->getLanguage()->pipeList( array( $out2, $prevLink ) );
434 if( $n == $this->maxPerPage
&& $s = $res->fetchObject() ) {
435 # $s is the first link of the next chunk
436 $t = Title
::MakeTitle($namespace, $s->page_title
);
437 $query = array( 'from' => $t->getText() );
440 $query['namespace'] = $namespace;
442 $nextLink = Linker
::linkKnown(
444 $this->msg( 'nextpage', $t->getText() )->escaped(),
448 $out2 = $this->getLanguage()->pipeList( array( $out2, $nextLink ) );
450 $out2 .= "</td></tr></table>";
453 $output->addHTML( $out2 . $out );
456 if ( isset( $prevLink ) ) $links[] = $prevLink;
457 if ( isset( $nextLink ) ) $links[] = $nextLink;
459 if ( count( $links ) ) {
461 Html
::element( 'hr' ) .
462 Html
::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
463 $this->getLanguage()->pipeList( $links )
470 * @param $ns Integer: the namespace of the article
471 * @param $text String: the name of the article
472 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
474 protected 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(), '', '' );