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 )
133 Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
136 $out .= Xml
::closeElement( 'table' );
137 $out .= Xml
::closeElement( 'fieldset' );
138 $out .= Xml
::closeElement( 'form' );
139 $out .= Xml
::closeElement( 'div' );
144 * @param $namespace Integer (default NS_MAIN)
145 * @param $from String: list all pages from this name
146 * @param $to String: list all pages to this name
148 function showToplevel( $namespace = NS_MAIN
, $from = '', $to = '' ) {
149 $output = $this->getOutput();
151 # TODO: Either make this *much* faster or cache the title index points
152 # in the querycache table.
154 $dbr = wfGetDB( DB_SLAVE
);
156 $where = array( 'page_namespace' => $namespace );
158 $from = Title
::makeTitleSafe( $namespace, $from );
159 $to = Title
::makeTitleSafe( $namespace, $to );
160 $from = ( $from && $from->isLocal() ) ?
$from->getDBkey() : null;
161 $to = ( $to && $to->isLocal() ) ?
$to->getDBkey() : null;
164 $where[] = 'page_title >= '.$dbr->addQuotes( $from );
166 $where[] = 'page_title <= '.$dbr->addQuotes( $to );
169 $key = wfMemcKey( 'allpages', 'ns', $namespace, $from, $to );
170 $lines = $wgMemc->get( $key );
172 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__
);
173 $maxPerSubpage = intval($count/$this->maxLineCount
);
174 $maxPerSubpage = max($maxPerSubpage,$this->maxPerPage
);
176 if( !is_array( $lines ) ) {
177 $options = array( 'LIMIT' => 1 );
178 $options['ORDER BY'] = 'page_title ASC';
179 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__
, $options );
180 $lastTitle = $firstTitle;
181 # This array is going to hold the page_titles in order.
182 $lines = array( $firstTitle );
183 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
186 // Fetch the last title of this chunk and the first of the next
187 $chunk = ( $lastTitle === false )
189 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
190 $res = $dbr->select( 'page', /* FROM */
191 'page_title', /* WHAT */
192 array_merge($where,$chunk),
194 array ('LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC')
197 $s = $dbr->fetchObject( $res );
199 array_push( $lines, $s->page_title
);
201 // Final chunk, but ended prematurely. Go back and find the end.
202 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
203 array_merge($where,$chunk),
205 array_push( $lines, $endTitle );
208 $s = $res->fetchObject();
210 array_push( $lines, $s->page_title
);
211 $lastTitle = $s->page_title
;
213 // This was a final chunk and ended exactly at the limit.
214 // Rare but convenient!
219 $wgMemc->add( $key, $lines, 3600 );
222 // If there are only two or less sections, don't even display them.
223 // Instead, display the first section directly.
224 if( count( $lines ) <= 2 ) {
225 if( !empty($lines) ) {
226 $this->showChunk( $namespace, $from, $to );
228 $output->addHTML( $this->namespaceForm( $namespace, $from, $to ) );
233 # At this point, $lines should contain an even number of elements.
234 $out .= Xml
::openElement( 'table', array( 'class' => 'allpageslist' ) );
235 while( count ( $lines ) > 0 ) {
236 $inpoint = array_shift( $lines );
237 $outpoint = array_shift( $lines );
238 $out .= $this->showline( $inpoint, $outpoint, $namespace );
240 $out .= Xml
::closeElement( 'table' );
241 $nsForm = $this->namespaceForm( $namespace, $from, $to );
244 if( $this->including() ) {
247 if( isset($from) ||
isset($to) ) {
248 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
253 <td class="mw-allpages-nav">' .
254 Linker
::link( $this->getTitle(), $this->msg( 'allpages' )->escaped(),
255 array(), array(), 'known' ) .
258 Xml
::closeElement( 'table' );
263 $output->addHTML( $out2 . $out );
267 * Show a line of "ABC to DEF" ranges of articles
269 * @param $inpoint String: lower limit of pagenames
270 * @param $outpoint String: upper limit of pagenames
271 * @param $namespace Integer (Default NS_MAIN)
273 function showline( $inpoint, $outpoint, $namespace = NS_MAIN
) {
275 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
276 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
277 // Don't let the length runaway
278 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength
);
279 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength
);
281 $queryparams = $namespace ?
"namespace=$namespace&" : '';
282 $special = $this->getTitle();
283 $link = htmlspecialchars( $special->getLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint) ) );
285 $out = $this->msg( 'alphaindexline' )->rawParams(
286 "<a href=\"$link\">$inpointf</a></td><td>",
287 "</td><td><a href=\"$link\">$outpointf</a>"
289 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
293 * @param $namespace Integer (Default NS_MAIN)
294 * @param $from String: list all pages from this name (default FALSE)
295 * @param $to String: list all pages to this name (default FALSE)
297 function showChunk( $namespace = NS_MAIN
, $from = false, $to = false ) {
299 $output = $this->getOutput();
301 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
302 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
303 $namespaces = $wgContLang->getNamespaces();
306 if ( !$fromList ||
!$toList ) {
307 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
308 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
309 // Show errormessage and reset to NS_MAIN
310 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
311 $namespace = NS_MAIN
;
313 list( $namespace, $fromKey, $from ) = $fromList;
314 list( , $toKey, $to ) = $toList;
316 $dbr = wfGetDB( DB_SLAVE
);
318 'page_namespace' => $namespace,
319 'page_title >= ' . $dbr->addQuotes( $fromKey )
321 if( $toKey !== "" ) {
322 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
325 $res = $dbr->select( 'page',
326 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
330 'ORDER BY' => 'page_title',
331 'LIMIT' => $this->maxPerPage +
1,
332 'USE INDEX' => 'name_title',
336 if( $res->numRows() > 0 ) {
337 $out = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
338 while( ( $n < $this->maxPerPage
) && ( $s = $res->fetchObject() ) ) {
339 $t = Title
::newFromRow( $s );
341 $link = ( $s->page_is_redirect ?
'<div class="allpagesredirect">' : '' ) .
343 ($s->page_is_redirect ?
'</div>' : '' );
345 $link = '[[' . htmlspecialchars( $s->page_title
) . ']]';
350 $out .= "<td style=\"width:33%\">$link</td>";
356 if( ($n %
3) != 0 ) {
359 $out .= Xml
::closeElement( 'table' );
365 if ( $this->including() ) {
369 // First chunk; no previous link.
372 # Get the last title from previous chunk
373 $dbr = wfGetDB( DB_SLAVE
);
374 $res_prev = $dbr->select(
377 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
379 array( 'ORDER BY' => 'page_title DESC',
380 'LIMIT' => $this->maxPerPage
, 'OFFSET' => ($this->maxPerPage
- 1 )
384 # Get first title of previous complete chunk
385 if( $dbr->numrows( $res_prev ) >= $this->maxPerPage
) {
386 $pt = $dbr->fetchObject( $res_prev );
387 $prevTitle = Title
::makeTitle( $namespace, $pt->page_title
);
389 # The previous chunk is not complete, need to link to the very first title
390 # available in the database
391 $options = array( 'LIMIT' => 1 );
392 if ( ! $dbr->implicitOrderby() ) {
393 $options['ORDER BY'] = 'page_title';
395 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
396 array( 'page_namespace' => $namespace ), __METHOD__
, $options );
397 # Show the previous link if it s not the current requested chunk
398 if( $from != $reallyFirstPage_title ) {
399 $prevTitle = Title
::makeTitle( $namespace, $reallyFirstPage_title );
406 $self = $this->getTitle();
408 $nsForm = $this->namespaceForm( $namespace, $from, $to );
409 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
414 <td class="mw-allpages-nav">' .
415 Linker
::link( $self, $this->msg( 'allpages' )->escaped() );
417 # Do we put a previous link ?
418 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
419 $query = array( 'from' => $prevTitle->getText() );
422 $query['namespace'] = $namespace;
424 $prevLink = Linker
::linkKnown(
426 $this->msg( 'prevpage', $pt )->escaped(),
430 $out2 = $this->getLanguage()->pipeList( array( $out2, $prevLink ) );
433 if( $n == $this->maxPerPage
&& $s = $res->fetchObject() ) {
434 # $s is the first link of the next chunk
435 $t = Title
::MakeTitle($namespace, $s->page_title
);
436 $query = array( 'from' => $t->getText() );
439 $query['namespace'] = $namespace;
441 $nextLink = Linker
::linkKnown(
443 $this->msg( 'nextpage', $t->getText() )->escaped(),
447 $out2 = $this->getLanguage()->pipeList( array( $out2, $nextLink ) );
449 $out2 .= "</td></tr></table>";
452 $output->addHTML( $out2 . $out );
455 if ( isset( $prevLink ) ) $links[] = $prevLink;
456 if ( isset( $nextLink ) ) $links[] = $nextLink;
458 if ( count( $links ) ) {
460 Html
::element( 'hr' ) .
461 Html
::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
462 $this->getLanguage()->pipeList( $links )
469 * @param $ns Integer: the namespace of the article
470 * @param $text String: the name of the article
471 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
473 protected 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(), '', '' );