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
28 * @todo Rewrite using IndexPager
30 class SpecialAllPages
extends IncludableSpecialPage
{
33 * Maximum number of pages to show on single subpage.
35 * @var int $maxPerPage
37 protected $maxPerPage = 345;
40 * Determines, which message describes the input field 'nsfrom'.
42 * @var string $nsfromMsg
44 protected $nsfromMsg = 'allpagesfrom';
49 * @param string $name Name of the special page, as seen in links and URLs (default: 'Allpages')
51 function __construct( $name = 'Allpages' ) {
52 parent
::__construct( $name );
56 * Entry point : initialise variables and call subfunctions.
58 * @param string $par Becomes "FOO" when called like Special:Allpages/FOO (default null)
60 function execute( $par ) {
61 $request = $this->getRequest();
62 $out = $this->getOutput();
65 $this->outputHeader();
66 $out->allowClickjacking();
69 $from = $request->getVal( 'from', null );
70 $to = $request->getVal( 'to', null );
71 $namespace = $request->getInt( 'namespace' );
72 $hideredirects = $request->getBool( 'hideredirects', false );
74 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
77 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) ) ?
78 $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
79 $this->msg( 'allarticles' )
81 $out->addModuleStyles( 'mediawiki.special' );
83 if ( $par !== null ) {
84 $this->showChunk( $namespace, $par, $to, $hideredirects );
85 } elseif ( $from !== null && $to === null ) {
86 $this->showChunk( $namespace, $from, $to, $hideredirects );
88 $this->showToplevel( $namespace, $from, $to, $hideredirects );
93 * HTML for the top form
95 * @param int $namespace A namespace constant (default NS_MAIN).
96 * @param string $from DbKey we are starting listing at.
97 * @param string $to DbKey we are ending listing at.
98 * @param bool $hideredirects Dont show redirects (default false)
101 function namespaceForm( $namespace = NS_MAIN
, $from = '', $to = '', $hideredirects = false ) {
102 $t = $this->getPageTitle();
104 $out = Xml
::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
105 $out .= Xml
::openElement(
107 array( 'method' => 'get', 'action' => $this->getConfig()->get( 'Script' ) )
109 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
110 $out .= Xml
::openElement( 'fieldset' );
111 $out .= Xml
::element( 'legend', null, $this->msg( 'allpages' )->text() );
112 $out .= Xml
::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
114 <td class='mw-label'>" .
115 Xml
::label( $this->msg( 'allpagesfrom' )->text(), 'nsfrom' ) .
117 <td class='mw-input'>" .
118 Xml
::input( 'from', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
122 <td class='mw-label'>" .
123 Xml
::label( $this->msg( 'allpagesto' )->text(), 'nsto' ) .
125 <td class='mw-input'>" .
126 Xml
::input( 'to', 30, str_replace( '_', ' ', $to ), array( 'id' => 'nsto' ) ) .
130 <td class='mw-label'>" .
131 Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
133 <td class='mw-input'>" .
134 Html
::namespaceSelector(
135 array( 'selected' => $namespace ),
136 array( 'name' => 'namespace', 'id' => 'namespace' )
139 $this->msg( 'allpages-hide-redirects' )->text(),
144 Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
147 $out .= Xml
::closeElement( 'table' );
148 $out .= Xml
::closeElement( 'fieldset' );
149 $out .= Xml
::closeElement( 'form' );
150 $out .= Xml
::closeElement( 'div' );
156 * @param int $namespace (default NS_MAIN)
157 * @param string $from List all pages from this name
158 * @param string $to List all pages to this name
159 * @param bool $hideredirects Dont show redirects (default false)
161 function showToplevel( $namespace = NS_MAIN
, $from = '', $to = '', $hideredirects = false ) {
162 $from = Title
::makeTitleSafe( $namespace, $from );
163 $to = Title
::makeTitleSafe( $namespace, $to );
164 $from = ( $from && $from->isLocal() ) ?
$from->getDBkey() : null;
165 $to = ( $to && $to->isLocal() ) ?
$to->getDBkey() : null;
167 $this->showChunk( $namespace, $from, $to, $hideredirects );
171 * @param int $namespace Namespace (Default NS_MAIN)
172 * @param string $from List all pages from this name (default false)
173 * @param string $to List all pages to this name (default false)
174 * @param bool $hideredirects Dont show redirects (default false)
176 function showChunk( $namespace = NS_MAIN
, $from = false, $to = false, $hideredirects = false ) {
177 $output = $this->getOutput();
179 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
180 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
181 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
185 if ( !$fromList ||
!$toList ) {
186 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
187 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
188 // Show errormessage and reset to NS_MAIN
189 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
190 $namespace = NS_MAIN
;
192 list( $namespace, $fromKey, $from ) = $fromList;
193 list( , $toKey, $to ) = $toList;
195 $dbr = wfGetDB( DB_SLAVE
);
196 $filterConds = array( 'page_namespace' => $namespace );
197 if ( $hideredirects ) {
198 $filterConds['page_is_redirect'] = 0;
201 $conds = $filterConds;
202 $conds[] = 'page_title >= ' . $dbr->addQuotes( $fromKey );
203 if ( $toKey !== "" ) {
204 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
207 $res = $dbr->select( 'page',
208 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
212 'ORDER BY' => 'page_title',
213 'LIMIT' => $this->maxPerPage +
1,
214 'USE INDEX' => 'name_title',
218 if ( $res->numRows() > 0 ) {
219 $out = Xml
::openElement( 'ul', array( 'class' => 'mw-allpages-chunk' ) );
220 while ( ( $n < $this->maxPerPage
) && ( $s = $res->fetchObject() ) ) {
221 $t = Title
::newFromRow( $s );
224 ( $s->page_is_redirect ?
' class="allpagesredirect"' : '' ) .
229 $out .= '<li>[[' . htmlspecialchars( $s->page_title
) . "]]</li>\n";
233 $out .= Xml
::closeElement( 'ul' );
238 if ( $fromKey !== '' && !$this->including() ) {
239 # Get the first title from previous chunk
240 $prevConds = $filterConds;
241 $prevConds[] = 'page_title < ' . $dbr->addQuotes( $fromKey );
242 $prevKey = $dbr->selectField(
247 array( 'ORDER BY' => 'page_title DESC', 'OFFSET' => $this->maxPerPage
- 1 )
250 if ( $prevKey === false ) {
251 # The previous chunk is not complete, need to link to the very first title
252 # available in the database
253 $prevKey = $dbr->selectField(
258 array( 'ORDER BY' => 'page_title' )
262 if ( $prevKey !== false ) {
263 $prevTitle = Title
::makeTitle( $namespace, $prevKey );
268 if ( $this->including() ) {
269 $output->addHTML( $out );
273 $self = $this->getPageTitle();
276 Linker
::link( $self, $this->msg( 'allpages' )->escaped() )
278 $bottomLinks = array();
280 # Do we put a previous link ?
282 $query = array( 'from' => $prevTitle->getText() );
285 $query['namespace'] = $namespace;
288 if ( $hideredirects ) {
289 $query['hideredirects'] = $hideredirects;
292 $prevLink = Linker
::linkKnown(
294 $this->msg( 'prevpage', $prevTitle->getText() )->escaped(),
298 $topLinks[] = $prevLink;
299 $bottomLinks[] = $prevLink;
302 if ( $n == $this->maxPerPage
&& $s = $res->fetchObject() ) {
303 # $s is the first link of the next chunk
304 $t = Title
::makeTitle( $namespace, $s->page_title
);
305 $query = array( 'from' => $t->getText() );
308 $query['namespace'] = $namespace;
311 if ( $hideredirects ) {
312 $query['hideredirects'] = $hideredirects;
315 $nextLink = Linker
::linkKnown(
317 $this->msg( 'nextpage', $t->getText() )->escaped(),
321 $topLinks[] = $nextLink;
322 $bottomLinks[] = $nextLink;
325 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
326 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
331 <td class="mw-allpages-nav">' .
332 $this->getLanguage()->pipeList( $topLinks ) .
333 '</td></tr></table>';
335 $output->addHTML( $out2 . $out );
337 if ( count( $bottomLinks ) ) {
339 Html
::element( 'hr' ) .
340 Html
::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
341 $this->getLanguage()->pipeList( $bottomLinks )
348 * @param int $ns The namespace of the article
349 * @param string $text The name of the article
350 * @return array( int namespace, string dbkey, string pagename ) or null on error
352 protected function getNamespaceKeyAndText( $ns, $text ) {
354 # shortcut for common case
355 return array( $ns, '', '' );
358 $t = Title
::makeTitleSafe( $ns, $text );
359 if ( $t && $t->isLocal() ) {
360 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
365 # try again, in case the problem was an empty pagename
366 $text = preg_replace( '/(#|$)/', 'X$1', $text );
367 $t = Title
::makeTitleSafe( $ns, $text );
368 if ( $t && $t->isLocal() ) {
369 return array( $t->getNamespace(), '', '' );
375 protected function getGroupName() {