Fix bug that brakes the 'jquery.tabIndex > firstTabIndex' test in IE8/IE9. Some value...
[mediawiki.git] / includes / specials / SpecialAllpages.php
blobec049ac55a0f8cca53a3e90b68c51f781db6db05
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Implements Special:Allpages
27 * @ingroup SpecialPage
29 class SpecialAllpages extends IncludableSpecialPage {
31 /**
32 * Maximum number of pages to show on single subpage.
34 protected $maxPerPage = 345;
36 /**
37 * Maximum number of pages to show on single index subpage.
39 protected $maxLineCount = 100;
41 /**
42 * Maximum number of chars to show for an entry.
44 protected $maxPageLength = 70;
46 /**
47 * Determines, which message describes the input field 'nsfrom'.
49 protected $nsfromMsg = 'allpagesfrom';
51 function __construct( $name = 'Allpages' ){
52 parent::__construct( $name );
55 /**
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 $wgContLang;
62 $request = $this->getRequest();
63 $out = $this->getOutput();
65 $this->setHeaders();
66 $this->outputHeader();
67 $out->allowClickjacking();
69 # GET values
70 $from = $request->getVal( 'from', null );
71 $to = $request->getVal( 'to', null );
72 $namespace = $request->getInt( 'namespace' );
74 $namespaces = $wgContLang->getNamespaces();
76 $out->setPagetitle(
77 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces) ) ) ?
78 wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
79 wfMsg( 'allarticles' )
81 $out->addModuleStyles( 'mediawiki.special' );
83 if( isset($par) ) {
84 $this->showChunk( $namespace, $par, $to );
85 } elseif( isset($from) && !isset($to) ) {
86 $this->showChunk( $namespace, $from, $to );
87 } else {
88 $this->showToplevel( $namespace, $from, $to );
92 /**
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 = '' ) {
100 global $wgScript;
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' ) );
109 $out .= "<tr>
110 <td class='mw-label'>" .
111 Xml::label( wfMsg( 'allpagesfrom' ), 'nsfrom' ) .
112 " </td>
113 <td class='mw-input'>" .
114 Xml::input( 'from', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
115 " </td>
116 </tr>
117 <tr>
118 <td class='mw-label'>" .
119 Xml::label( wfMsg( 'allpagesto' ), 'nsto' ) .
120 " </td>
121 <td class='mw-input'>" .
122 Xml::input( 'to', 30, str_replace('_',' ',$to), array( 'id' => 'nsto' ) ) .
123 " </td>
124 </tr>
125 <tr>
126 <td class='mw-label'>" .
127 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
128 " </td>
129 <td class='mw-input'>" .
130 Xml::namespaceSelector( $namespace, null ) . ' ' .
131 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
132 " </td>
133 </tr>";
134 $out .= Xml::closeElement( 'table' );
135 $out .= Xml::closeElement( 'fieldset' );
136 $out .= Xml::closeElement( 'form' );
137 $out .= Xml::closeElement( 'div' );
138 return $out;
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 );
153 $out = "";
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;
161 if( isset($from) )
162 $where[] = 'page_title >= '.$dbr->addQuotes( $from );
163 if( isset($to) )
164 $where[] = 'page_title <= '.$dbr->addQuotes( $to );
166 global $wgMemc;
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.
182 $done = false;
183 while( !$done ) {
184 // Fetch the last title of this chunk and the first of the next
185 $chunk = ( $lastTitle === false )
186 ? array()
187 : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
188 $res = $dbr->select( 'page', /* FROM */
189 'page_title', /* WHAT */
190 array_merge($where,$chunk),
191 __METHOD__,
192 array ('LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC')
195 $s = $dbr->fetchObject( $res );
196 if( $s ) {
197 array_push( $lines, $s->page_title );
198 } else {
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),
202 __METHOD__ );
203 array_push( $lines, $endTitle );
204 $done = true;
206 $s = $res->fetchObject();
207 if( $s ) {
208 array_push( $lines, $s->page_title );
209 $lastTitle = $s->page_title;
210 } else {
211 // This was a final chunk and ended exactly at the limit.
212 // Rare but convenient!
213 $done = true;
215 $res->free();
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 );
225 } else {
226 $output->addHTML( $this->namespaceForm( $namespace, $from, $to ) );
228 return;
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 );
241 # Is there more?
242 if( $this->including() ) {
243 $out2 = '';
244 } else {
245 if( isset($from) || isset($to) ) {
246 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
247 '<tr>
248 <td>' .
249 $nsForm .
250 '</td>
251 <td class="mw-allpages-nav">' .
252 $this->getSkin()->link( $this->getTitle(), wfMsgHtml ( 'allpages' ),
253 array(), array(), 'known' ) .
254 "</td>
255 </tr>" .
256 Xml::closeElement( 'table' );
257 } else {
258 $out2 = $nsForm;
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 ) {
272 global $wgContLang;
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();
303 $n = 0;
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;
311 } else {
312 list( $namespace, $fromKey, $from ) = $fromList;
313 list( , $toKey, $to ) = $toList;
315 $dbr = wfGetDB( DB_SLAVE );
316 $conds = array(
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' ),
326 $conds,
327 __METHOD__,
328 array(
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 );
339 if( $t ) {
340 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
341 $sk->link( $t ) .
342 ($s->page_is_redirect ? '</div>' : '' );
343 } else {
344 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
346 if( $n % 3 == 0 ) {
347 $out .= '<tr>';
349 $out .= "<td style=\"width:33%\">$link</td>";
350 $n++;
351 if( $n % 3 == 0 ) {
352 $out .= "</tr>\n";
355 if( ($n % 3) != 0 ) {
356 $out .= "</tr>\n";
358 $out .= Xml::closeElement( 'table' );
359 } else {
360 $out = '';
364 if ( $this->including() ) {
365 $out2 = '';
366 } else {
367 if( $from == '' ) {
368 // First chunk; no previous link.
369 $prevTitle = null;
370 } else {
371 # Get the last title from previous chunk
372 $dbr = wfGetDB( DB_SLAVE );
373 $res_prev = $dbr->select(
374 'page',
375 'page_title',
376 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
377 __METHOD__,
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 );
387 } else {
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 );
399 } else {
400 $prevTitle = null;
405 $self = $this->getTitle();
407 $nsForm = $this->namespaceForm( $namespace, $from, $to );
408 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
409 '<tr>
410 <td>' .
411 $nsForm .
412 '</td>
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() );
420 if( $namespace )
421 $query['namespace'] = $namespace;
423 $prevLink = $sk->linkKnown(
424 $self,
425 wfMessage( 'prevpage', $pt )->escaped(),
426 array(),
427 $query
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() );
437 if( $namespace )
438 $query['namespace'] = $namespace;
440 $nextLink = $sk->linkKnown(
441 $self,
442 wfMessage( 'nextpage', $t->getText() )->escaped(),
443 array(),
444 $query
446 $out2 = $wgLang->pipeList( array( $out2, $nextLink ) );
448 $out2 .= "</td></tr></table>";
451 $output->addHTML( $out2 . $out );
453 $links = array();
454 if ( isset( $prevLink ) ) $links[] = $prevLink;
455 if ( isset( $nextLink ) ) $links[] = $nextLink;
457 if ( count( $links ) ) {
458 $output->addHTML(
459 Html::element( 'hr' ) .
460 Html::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
461 $wgLang->pipeList( $links )
462 ) );
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
471 * @static (sort of)
472 * @access private
474 function getNamespaceKeyAndText($ns, $text) {
475 if ( $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() );
481 } else if ( $t ) {
482 return null;
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(), '', '' );
490 } else {
491 return null;