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();
103 $out = Xml
::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
104 $out .= Xml
::openElement(
106 array( 'method' => 'get', 'action' => $this->getConfig()->get( 'Script' ) )
108 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
109 $out .= Xml
::openElement( 'fieldset' );
110 $out .= Xml
::element( 'legend', null, $this->msg( 'allpages' )->text() );
111 $out .= Xml
::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
113 <td class='mw-label'>" .
114 Xml
::label( $this->msg( 'allpagesfrom' )->text(), 'nsfrom' ) .
116 <td class='mw-input'>" .
117 Xml
::input( 'from', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
121 <td class='mw-label'>" .
122 Xml
::label( $this->msg( 'allpagesto' )->text(), 'nsto' ) .
124 <td class='mw-input'>" .
125 Xml
::input( 'to', 30, str_replace( '_', ' ', $to ), array( 'id' => 'nsto' ) ) .
129 <td class='mw-label'>" .
130 Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
132 <td class='mw-input'>" .
133 Html
::namespaceSelector(
134 array( 'selected' => $namespace ),
135 array( 'name' => 'namespace', 'id' => 'namespace' )
138 $this->msg( 'allpages-hide-redirects' )->text(),
143 Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
146 $out .= Xml
::closeElement( 'table' );
147 $out .= Xml
::closeElement( 'fieldset' );
148 $out .= Xml
::closeElement( 'form' );
149 $out .= Xml
::closeElement( 'div' );
154 * @param int $namespace (default NS_MAIN)
155 * @param string $from List all pages from this name
156 * @param string $to List all pages to this name
157 * @param bool $hideredirects Dont show redirects (default false)
159 function showToplevel( $namespace = NS_MAIN
, $from = '', $to = '', $hideredirects = false ) {
160 $from = Title
::makeTitleSafe( $namespace, $from );
161 $to = Title
::makeTitleSafe( $namespace, $to );
162 $from = ( $from && $from->isLocal() ) ?
$from->getDBkey() : null;
163 $to = ( $to && $to->isLocal() ) ?
$to->getDBkey() : null;
165 $this->showChunk( $namespace, $from, $to, $hideredirects );
169 * @param int $namespace Namespace (Default NS_MAIN)
170 * @param string $from List all pages from this name (default false)
171 * @param string $to List all pages to this name (default false)
172 * @param bool $hideredirects Dont show redirects (default false)
174 function showChunk( $namespace = NS_MAIN
, $from = false, $to = false, $hideredirects = false ) {
175 $output = $this->getOutput();
177 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
178 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
179 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
183 if ( !$fromList ||
!$toList ) {
184 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
185 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
186 // Show errormessage and reset to NS_MAIN
187 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
188 $namespace = NS_MAIN
;
190 list( $namespace, $fromKey, $from ) = $fromList;
191 list( , $toKey, $to ) = $toList;
193 $dbr = wfGetDB( DB_SLAVE
);
194 $filterConds = array( 'page_namespace' => $namespace );
195 if ( $hideredirects ) {
196 $filterConds['page_is_redirect'] = 0;
199 $conds = $filterConds;
200 $conds[] = 'page_title >= ' . $dbr->addQuotes( $fromKey );
201 if ( $toKey !== "" ) {
202 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
205 $res = $dbr->select( 'page',
206 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
210 'ORDER BY' => 'page_title',
211 'LIMIT' => $this->maxPerPage +
1,
212 'USE INDEX' => 'name_title',
216 if ( $res->numRows() > 0 ) {
217 $out = Html
::openElement( 'div', array( 'class' => 'mw-allpages-body' ) );
218 $out .= Html
::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 .= Html
::closeElement( 'ul' );
234 $out .= Html
::closeElement( 'div' );
239 if ( $fromKey !== '' && !$this->including() ) {
240 # Get the first title from previous chunk
241 $prevConds = $filterConds;
242 $prevConds[] = 'page_title < ' . $dbr->addQuotes( $fromKey );
243 $prevKey = $dbr->selectField(
248 array( 'ORDER BY' => 'page_title DESC', 'OFFSET' => $this->maxPerPage
- 1 )
251 if ( $prevKey === false ) {
252 # The previous chunk is not complete, need to link to the very first title
253 # available in the database
254 $prevKey = $dbr->selectField(
259 array( 'ORDER BY' => 'page_title' )
263 if ( $prevKey !== false ) {
264 $prevTitle = Title
::makeTitle( $namespace, $prevKey );
269 if ( $this->including() ) {
270 $output->addHTML( $out );
275 $self = $this->getPageTitle();
277 // Generate a "previous page" link if needed
279 $query = array( 'from' => $prevTitle->getText() );
282 $query['namespace'] = $namespace;
285 if ( $hideredirects ) {
286 $query['hideredirects'] = $hideredirects;
289 $navLinks[] = Linker
::linkKnown(
291 $this->msg( 'prevpage', $prevTitle->getText() )->escaped(),
298 // Generate a "next page" link if needed
299 if ( $n == $this->maxPerPage
&& $s = $res->fetchObject() ) {
300 # $s is the first link of the next chunk
301 $t = Title
::makeTitle( $namespace, $s->page_title
);
302 $query = array( 'from' => $t->getText() );
305 $query['namespace'] = $namespace;
308 if ( $hideredirects ) {
309 $query['hideredirects'] = $hideredirects;
312 $navLinks[] = Linker
::linkKnown(
314 $this->msg( 'nextpage', $t->getText() )->escaped(),
320 $topOut = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
322 if ( count( $navLinks ) ) {
323 // Add pagination links
324 $pagination = Html
::rawElement( 'div',
325 array( 'class' => 'mw-allpages-nav' ),
326 $this->getLanguage()->pipeList( $navLinks )
329 $topOut .= $pagination;
330 $out .= Html
::element( 'hr' ) . $pagination; // Footer
333 $output->addHTML( $topOut . $out );
337 * @param int $ns The namespace of the article
338 * @param string $text The name of the article
339 * @return array( int namespace, string dbkey, string pagename ) or null on error
341 protected function getNamespaceKeyAndText( $ns, $text ) {
343 # shortcut for common case
344 return array( $ns, '', '' );
347 $t = Title
::makeTitleSafe( $ns, $text );
348 if ( $t && $t->isLocal() ) {
349 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
354 # try again, in case the problem was an empty pagename
355 $text = preg_replace( '/(#|$)/', 'X$1', $text );
356 $t = Title
::makeTitleSafe( $ns, $text );
357 if ( $t && $t->isLocal() ) {
358 return array( $t->getNamespace(), '', '' );
364 protected function getGroupName() {