3393914f6eaa0e11206699dbe5b4374675b4dde9
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' )
81 $this->showChunk( $namespace, $par, $to );
82 } elseif( isset($from) && !isset($to) ) {
83 $this->showChunk( $namespace, $from, $to );
85 $this->showToplevel( $namespace, $from, $to );
90 * HTML for the top form
92 * @param $namespace Integer: a namespace constant (default NS_MAIN).
93 * @param $from String: dbKey we are starting listing at.
94 * @param $to String: dbKey we are ending listing at.
96 function namespaceForm( $namespace = NS_MAIN
, $from = '', $to = '' ) {
98 $t = $this->getTitle();
100 $out = Xml
::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
101 $out .= Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
102 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
103 $out .= Xml
::openElement( 'fieldset' );
104 $out .= Xml
::element( 'legend', null, wfMsg( 'allpages' ) );
105 $out .= Xml
::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
107 <td class='mw-label'>" .
108 Xml
::label( wfMsg( 'allpagesfrom' ), 'nsfrom' ) .
110 <td class='mw-input'>" .
111 Xml
::input( 'from', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
115 <td class='mw-label'>" .
116 Xml
::label( wfMsg( 'allpagesto' ), 'nsto' ) .
118 <td class='mw-input'>" .
119 Xml
::input( 'to', 30, str_replace('_',' ',$to), array( 'id' => 'nsto' ) ) .
123 <td class='mw-label'>" .
124 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
126 <td class='mw-input'>" .
127 Xml
::namespaceSelector( $namespace, null ) . ' ' .
128 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
131 $out .= Xml
::closeElement( 'table' );
132 $out .= Xml
::closeElement( 'fieldset' );
133 $out .= Xml
::closeElement( 'form' );
134 $out .= Xml
::closeElement( 'div' );
139 * @param $namespace Integer (default NS_MAIN)
140 * @param $from String: list all pages from this name
141 * @param $to String: list all pages to this name
143 function showToplevel( $namespace = NS_MAIN
, $from = '', $to = '' ) {
146 # TODO: Either make this *much* faster or cache the title index points
147 # in the querycache table.
149 $dbr = wfGetDB( DB_SLAVE
);
151 $where = array( 'page_namespace' => $namespace );
153 $from = Title
::makeTitleSafe( $namespace, $from );
154 $to = Title
::makeTitleSafe( $namespace, $to );
155 $from = ( $from && $from->isLocal() ) ?
$from->getDBkey() : null;
156 $to = ( $to && $to->isLocal() ) ?
$to->getDBkey() : null;
159 $where[] = 'page_title >= '.$dbr->addQuotes( $from );
161 $where[] = 'page_title <= '.$dbr->addQuotes( $to );
164 $key = wfMemcKey( 'allpages', 'ns', $namespace, $from, $to );
165 $lines = $wgMemc->get( $key );
167 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__
);
168 $maxPerSubpage = intval($count/$this->maxLineCount
);
169 $maxPerSubpage = max($maxPerSubpage,$this->maxPerPage
);
171 if( !is_array( $lines ) ) {
172 $options = array( 'LIMIT' => 1 );
173 $options['ORDER BY'] = 'page_title ASC';
174 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__
, $options );
175 $lastTitle = $firstTitle;
176 # This array is going to hold the page_titles in order.
177 $lines = array( $firstTitle );
178 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
181 // Fetch the last title of this chunk and the first of the next
182 $chunk = ( $lastTitle === false )
184 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
185 $res = $dbr->select( 'page', /* FROM */
186 'page_title', /* WHAT */
187 array_merge($where,$chunk),
189 array ('LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC')
192 $s = $dbr->fetchObject( $res );
194 array_push( $lines, $s->page_title
);
196 // Final chunk, but ended prematurely. Go back and find the end.
197 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
198 array_merge($where,$chunk),
200 array_push( $lines, $endTitle );
203 $s = $res->fetchObject();
205 array_push( $lines, $s->page_title
);
206 $lastTitle = $s->page_title
;
208 // This was a final chunk and ended exactly at the limit.
209 // Rare but convenient!
214 $wgMemc->add( $key, $lines, 3600 );
217 // If there are only two or less sections, don't even display them.
218 // Instead, display the first section directly.
219 if( count( $lines ) <= 2 ) {
220 if( !empty($lines) ) {
221 $this->showChunk( $namespace, $from, $to );
223 $wgOut->addHTML( $this->namespaceForm( $namespace, $from, $to ) );
228 # At this point, $lines should contain an even number of elements.
229 $out .= Xml
::openElement( 'table', array( 'class' => 'allpageslist' ) );
230 while( count ( $lines ) > 0 ) {
231 $inpoint = array_shift( $lines );
232 $outpoint = array_shift( $lines );
233 $out .= $this->showline( $inpoint, $outpoint, $namespace );
235 $out .= Xml
::closeElement( 'table' );
236 $nsForm = $this->namespaceForm( $namespace, $from, $to );
239 if( $this->including() ) {
242 if( isset($from) ||
isset($to) ) {
244 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
249 <td class="mw-allpages-nav">' .
250 $wgUser->getSkin()->link( $this->getTitle(), wfMsgHtml ( 'allpages' ),
251 array(), array(), 'known' ) .
254 Xml
::closeElement( 'table' );
259 $wgOut->addHTML( $out2 . $out );
263 * Show a line of "ABC to DEF" ranges of articles
265 * @param $inpoint String: lower limit of pagenames
266 * @param $outpoint String: upper limit of pagenames
267 * @param $namespace Integer (Default NS_MAIN)
269 function showline( $inpoint, $outpoint, $namespace = NS_MAIN
) {
271 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
272 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
273 // Don't let the length runaway
274 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength
);
275 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength
);
277 $queryparams = $namespace ?
"namespace=$namespace&" : '';
278 $special = $this->getTitle();
279 $link = $special->escapeLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint) );
281 $out = wfMsgHtml( 'alphaindexline',
282 "<a href=\"$link\">$inpointf</a></td><td>",
283 "</td><td><a href=\"$link\">$outpointf</a>"
285 return '<tr><td class="mw-allpages-alphaindexline">' . $out . '</td></tr>';
289 * @param $namespace Integer (Default NS_MAIN)
290 * @param $from String: list all pages from this name (default FALSE)
291 * @param $to String: list all pages to this name (default FALSE)
293 function showChunk( $namespace = NS_MAIN
, $from = false, $to = false ) {
294 global $wgOut, $wgUser, $wgContLang, $wgLang;
296 $sk = $wgUser->getSkin();
298 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
299 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
300 $namespaces = $wgContLang->getNamespaces();
303 if ( !$fromList ||
!$toList ) {
304 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
305 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
306 // Show errormessage and reset to NS_MAIN
307 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
308 $namespace = NS_MAIN
;
310 list( $namespace, $fromKey, $from ) = $fromList;
311 list( , $toKey, $to ) = $toList;
313 $dbr = wfGetDB( DB_SLAVE
);
315 'page_namespace' => $namespace,
316 'page_title >= ' . $dbr->addQuotes( $fromKey )
318 if( $toKey !== "" ) {
319 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
322 $res = $dbr->select( 'page',
323 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
327 'ORDER BY' => 'page_title',
328 'LIMIT' => $this->maxPerPage +
1,
329 'USE INDEX' => 'name_title',
333 if( $res->numRows() > 0 ) {
334 $out = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
335 while( ( $n < $this->maxPerPage
) && ( $s = $res->fetchObject() ) ) {
336 $t = Title
::newFromRow( $s );
338 $link = ( $s->page_is_redirect ?
'<div class="allpagesredirect">' : '' ) .
340 ($s->page_is_redirect ?
'</div>' : '' );
342 $link = '[[' . htmlspecialchars( $s->page_title
) . ']]';
347 $out .= "<td style=\"width:33%\">$link</td>";
353 if( ($n %
3) != 0 ) {
356 $out .= Xml
::closeElement( 'table' );
362 if ( $this->including() ) {
366 // First chunk; no previous link.
369 # Get the last title from previous chunk
370 $dbr = wfGetDB( DB_SLAVE
);
371 $res_prev = $dbr->select(
374 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
376 array( 'ORDER BY' => 'page_title DESC',
377 'LIMIT' => $this->maxPerPage
, 'OFFSET' => ($this->maxPerPage
- 1 )
381 # Get first title of previous complete chunk
382 if( $dbr->numrows( $res_prev ) >= $this->maxPerPage
) {
383 $pt = $dbr->fetchObject( $res_prev );
384 $prevTitle = Title
::makeTitle( $namespace, $pt->page_title
);
386 # The previous chunk is not complete, need to link to the very first title
387 # available in the database
388 $options = array( 'LIMIT' => 1 );
389 if ( ! $dbr->implicitOrderby() ) {
390 $options['ORDER BY'] = 'page_title';
392 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
393 array( 'page_namespace' => $namespace ), __METHOD__
, $options );
394 # Show the previous link if it s not the current requested chunk
395 if( $from != $reallyFirstPage_title ) {
396 $prevTitle = Title
::makeTitle( $namespace, $reallyFirstPage_title );
403 $self = $this->getTitle();
405 $nsForm = $this->namespaceForm( $namespace, $from, $to );
406 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
411 <td class="mw-allpages-nav">' .
412 $sk->link( $self, wfMsgHtml ( 'allpages' ), array(), array(), 'known' );
414 # Do we put a previous link ?
415 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
416 $query = array( 'from' => $prevTitle->getText() );
419 $query['namespace'] = $namespace;
421 $prevLink = $sk->linkKnown(
423 htmlspecialchars( wfMsg( 'prevpage', $pt ) ),
427 $out2 = $wgLang->pipeList( array( $out2, $prevLink ) );
430 if( $n == $this->maxPerPage
&& $s = $res->fetchObject() ) {
431 # $s is the first link of the next chunk
432 $t = Title
::MakeTitle($namespace, $s->page_title
);
433 $query = array( 'from' => $t->getText() );
436 $query['namespace'] = $namespace;
438 $nextLink = $sk->linkKnown(
440 htmlspecialchars( wfMsg( 'nextpage', $t->getText() ) ),
444 $out2 = $wgLang->pipeList( array( $out2, $nextLink ) );
446 $out2 .= "</td></tr></table>";
449 $wgOut->addHTML( $out2 . $out );
450 if( isset($prevLink) or isset($nextLink) ) {
451 $wgOut->addHTML( '<hr /><p class="mw-allpages-nav">' );
452 if( isset( $prevLink ) ) {
453 $wgOut->addHTML( $prevLink );
455 if( isset( $prevLink ) && isset( $nextLink ) ) {
456 $wgOut->addHTML( wfMsgExt( 'pipe-separator' , 'escapenoentities' ) );
458 if( isset( $nextLink ) ) {
459 $wgOut->addHTML( $nextLink );
461 $wgOut->addHTML( '</p>' );
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(), '', '' );