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 = Html
::openElement( 'div', array( 'class' => 'mw-allpages-body' ) );
220 $out .= Html
::openElement( 'ul', array( 'class' => 'mw-allpages-chunk' ) );
222 while ( ( $n < $this->maxPerPage
) && ( $s = $res->fetchObject() ) ) {
223 $t = Title
::newFromRow( $s );
226 ( $s->page_is_redirect ?
' class="allpagesredirect"' : '' ) .
231 $out .= '<li>[[' . htmlspecialchars( $s->page_title
) . "]]</li>\n";
235 $out .= Html
::closeElement( 'ul' );
236 $out .= Html
::closeElement( 'div' );
241 if ( $fromKey !== '' && !$this->including() ) {
242 # Get the first title from previous chunk
243 $prevConds = $filterConds;
244 $prevConds[] = 'page_title < ' . $dbr->addQuotes( $fromKey );
245 $prevKey = $dbr->selectField(
250 array( 'ORDER BY' => 'page_title DESC', 'OFFSET' => $this->maxPerPage
- 1 )
253 if ( $prevKey === false ) {
254 # The previous chunk is not complete, need to link to the very first title
255 # available in the database
256 $prevKey = $dbr->selectField(
261 array( 'ORDER BY' => 'page_title' )
265 if ( $prevKey !== false ) {
266 $prevTitle = Title
::makeTitle( $namespace, $prevKey );
271 if ( $this->including() ) {
272 $output->addHTML( $out );
276 $self = $this->getPageTitle();
279 Linker
::link( $self, $this->msg( 'allpages' )->escaped() )
281 $bottomLinks = array();
283 # Do we put a previous link ?
285 $query = array( 'from' => $prevTitle->getText() );
288 $query['namespace'] = $namespace;
291 if ( $hideredirects ) {
292 $query['hideredirects'] = $hideredirects;
295 $prevLink = Linker
::linkKnown(
297 $this->msg( 'prevpage', $prevTitle->getText() )->escaped(),
301 $topLinks[] = $prevLink;
302 $bottomLinks[] = $prevLink;
305 if ( $n == $this->maxPerPage
&& $s = $res->fetchObject() ) {
306 # $s is the first link of the next chunk
307 $t = Title
::makeTitle( $namespace, $s->page_title
);
308 $query = array( 'from' => $t->getText() );
311 $query['namespace'] = $namespace;
314 if ( $hideredirects ) {
315 $query['hideredirects'] = $hideredirects;
318 $nextLink = Linker
::linkKnown(
320 $this->msg( 'nextpage', $t->getText() )->escaped(),
324 $topLinks[] = $nextLink;
325 $bottomLinks[] = $nextLink;
328 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
329 $out2 = Xml
::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
334 <td class="mw-allpages-nav">' .
335 $this->getLanguage()->pipeList( $topLinks ) .
336 '</td></tr></table>';
338 $output->addHTML( $out2 . $out );
340 if ( count( $bottomLinks ) ) {
342 Html
::element( 'hr' ) .
343 Html
::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
344 $this->getLanguage()->pipeList( $bottomLinks )
351 * @param int $ns The namespace of the article
352 * @param string $text The name of the article
353 * @return array( int namespace, string dbkey, string pagename ) or null on error
355 protected function getNamespaceKeyAndText( $ns, $text ) {
357 # shortcut for common case
358 return array( $ns, '', '' );
361 $t = Title
::makeTitleSafe( $ns, $text );
362 if ( $t && $t->isLocal() ) {
363 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
368 # try again, in case the problem was an empty pagename
369 $text = preg_replace( '/(#|$)/', 'X$1', $text );
370 $t = Title
::makeTitleSafe( $ns, $text );
371 if ( $t && $t->isLocal() ) {
372 return array( $t->getNamespace(), '', '' );
378 protected function getGroupName() {