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';
47 * @param string $name Name of the special page, as seen in links and URLs (default: 'Allpages')
49 function __construct( $name = 'Allpages' ) {
50 parent
::__construct( $name );
54 * Entry point : initialise variables and call subfunctions.
56 * @param string $par Becomes "FOO" when called like Special:Allpages/FOO (default null)
58 function execute( $par ) {
59 $request = $this->getRequest();
60 $out = $this->getOutput();
63 $this->outputHeader();
64 $out->allowClickjacking();
67 $from = $request->getVal( 'from', null );
68 $to = $request->getVal( 'to', null );
69 $namespace = $request->getInt( 'namespace' );
71 $miserMode = (bool)$this->getConfig()->get( 'MiserMode' );
73 // Redirects filter is disabled in MiserMode
74 $hideredirects = $request->getBool( 'hideredirects', false ) && !$miserMode;
76 $namespaces = $this->getLanguage()->getNamespaces();
79 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) ) ?
80 $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
81 $this->msg( 'allarticles' )
83 $out->addModuleStyles( 'mediawiki.special' );
85 if ( $par !== null ) {
86 $this->showChunk( $namespace, $par, $to, $hideredirects );
87 } elseif ( $from !== null && $to === null ) {
88 $this->showChunk( $namespace, $from, $to, $hideredirects );
90 $this->showToplevel( $namespace, $from, $to, $hideredirects );
95 * Outputs the HTMLForm used on this page
97 * @param int $namespace A namespace constant (default NS_MAIN).
98 * @param string $from DbKey we are starting listing at.
99 * @param string $to DbKey we are ending listing at.
100 * @param bool $hideRedirects Dont show redirects (default false)
102 protected function outputHTMLForm( $namespace = NS_MAIN
,
103 $from = '', $to = '', $hideRedirects = false
105 $miserMode = (bool)$this->getConfig()->get( 'MiserMode' );
112 'label-message' => 'allpagesfrom',
113 'default' => str_replace( '_', ' ', $from ),
120 'label-message' => 'allpagesto',
121 'default' => str_replace( '_', ' ', $to ),
124 'type' => 'namespaceselect',
125 'name' => 'namespace',
127 'label-message' => 'namespace',
129 'value' => $namespace,
133 'name' => 'hideredirects',
134 'id' => 'hidredirects',
135 'label-message' => 'allpages-hide-redirects',
136 'value' => $hideRedirects,
141 unset( $fields['hideredirects'] );
144 $form = HTMLForm
::factory( 'table', $fields, $this->getContext() );
145 $form->setMethod( 'get' )
146 ->setWrapperLegendMsg( 'allpages' )
147 ->setSubmitTextMsg( 'allpagessubmit' )
149 ->displayForm( false );
153 * @param int $namespace (default NS_MAIN)
154 * @param string $from List all pages from this name
155 * @param string $to List all pages to this name
156 * @param bool $hideredirects Dont show redirects (default false)
158 function showToplevel( $namespace = NS_MAIN
, $from = '', $to = '', $hideredirects = false ) {
159 $from = Title
::makeTitleSafe( $namespace, $from );
160 $to = Title
::makeTitleSafe( $namespace, $to );
161 $from = ( $from && $from->isLocal() ) ?
$from->getDBkey() : null;
162 $to = ( $to && $to->isLocal() ) ?
$to->getDBkey() : null;
164 $this->showChunk( $namespace, $from, $to, $hideredirects );
168 * @param int $namespace Namespace (Default NS_MAIN)
169 * @param string $from List all pages from this name (default false)
170 * @param string $to List all pages to this name (default false)
171 * @param bool $hideredirects Dont show redirects (default false)
173 function showChunk( $namespace = NS_MAIN
, $from = false, $to = false, $hideredirects = false ) {
174 $output = $this->getOutput();
176 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
177 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
178 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
182 if ( !$fromList ||
!$toList ) {
183 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
184 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
185 // Show errormessage and reset to NS_MAIN
186 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
187 $namespace = NS_MAIN
;
189 list( $namespace, $fromKey, $from ) = $fromList;
190 list( , $toKey, $to ) = $toList;
192 $dbr = wfGetDB( DB_REPLICA
);
193 $filterConds = [ 'page_namespace' => $namespace ];
194 if ( $hideredirects ) {
195 $filterConds['page_is_redirect'] = 0;
198 $conds = $filterConds;
199 $conds[] = 'page_title >= ' . $dbr->addQuotes( $fromKey );
200 if ( $toKey !== "" ) {
201 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
204 $res = $dbr->select( 'page',
205 [ 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ],
209 'ORDER BY' => 'page_title',
210 'LIMIT' => $this->maxPerPage +
1,
211 'USE INDEX' => 'name_title',
215 $linkRenderer = $this->getLinkRenderer();
216 if ( $res->numRows() > 0 ) {
217 $out = Html
::openElement( 'ul', [ 'class' => 'mw-allpages-chunk' ] );
219 while ( ( $n < $this->maxPerPage
) && ( $s = $res->fetchObject() ) ) {
220 $t = Title
::newFromRow( $s );
223 ( $s->page_is_redirect ?
' class="allpagesredirect"' : '' ) .
225 $linkRenderer->makeLink( $t ) .
228 $out .= '<li>[[' . htmlspecialchars( $s->page_title
) . "]]</li>\n";
232 $out .= Html
::closeElement( 'ul' );
234 if ( $res->numRows() > 2 ) {
235 // Only apply CSS column styles if there's more than 2 entries.
236 // Otherwise, rendering is broken as "mw-allpages-body"'s CSS column count is 3.
237 $out = Html
::rawElement( 'div', [ 'class' => 'mw-allpages-body' ], $out );
243 if ( $fromKey !== '' && !$this->including() ) {
244 # Get the first title from previous chunk
245 $prevConds = $filterConds;
246 $prevConds[] = 'page_title < ' . $dbr->addQuotes( $fromKey );
247 $prevKey = $dbr->selectField(
252 [ 'ORDER BY' => 'page_title DESC', 'OFFSET' => $this->maxPerPage
- 1 ]
255 if ( $prevKey === false ) {
256 # The previous chunk is not complete, need to link to the very first title
257 # available in the database
258 $prevKey = $dbr->selectField(
263 [ 'ORDER BY' => 'page_title' ]
267 if ( $prevKey !== false ) {
268 $prevTitle = Title
::makeTitle( $namespace, $prevKey );
273 if ( $this->including() ) {
274 $output->addHTML( $out );
279 $self = $this->getPageTitle();
281 $linkRenderer = $this->getLinkRenderer();
282 // Generate a "previous page" link if needed
284 $query = [ 'from' => $prevTitle->getText() ];
287 $query['namespace'] = $namespace;
290 if ( $hideredirects ) {
291 $query['hideredirects'] = $hideredirects;
294 $navLinks[] = $linkRenderer->makeKnownLink(
296 $this->msg( 'prevpage', $prevTitle->getText() )->text(),
303 // Generate a "next page" link if needed
304 if ( $n == $this->maxPerPage
&& $s = $res->fetchObject() ) {
305 # $s is the first link of the next chunk
306 $t = Title
::makeTitle( $namespace, $s->page_title
);
307 $query = [ 'from' => $t->getText() ];
310 $query['namespace'] = $namespace;
313 if ( $hideredirects ) {
314 $query['hideredirects'] = $hideredirects;
317 $navLinks[] = $linkRenderer->makeKnownLink(
319 $this->msg( 'nextpage', $t->getText() )->text(),
325 $this->outputHTMLForm( $namespace, $from, $to, $hideredirects );
327 if ( count( $navLinks ) ) {
328 // Add pagination links
329 $pagination = Html
::rawElement( 'div',
330 [ 'class' => 'mw-allpages-nav' ],
331 $this->getLanguage()->pipeList( $navLinks )
334 $output->addHTML( $pagination );
335 $out .= Html
::element( 'hr' ) . $pagination; // Footer
338 $output->addHTML( $out );
342 * @param int $ns The namespace of the article
343 * @param string $text The name of the article
344 * @return array|null [ int namespace, string dbkey, string pagename ] or null on error
346 protected function getNamespaceKeyAndText( $ns, $text ) {
348 # shortcut for common case
349 return [ $ns, '', '' ];
352 $t = Title
::makeTitleSafe( $ns, $text );
353 if ( $t && $t->isLocal() ) {
354 return [ $t->getNamespace(), $t->getDBkey(), $t->getText() ];
359 # try again, in case the problem was an empty pagename
360 $text = preg_replace( '/(#|$)/', 'X$1', $text );
361 $t = Title
::makeTitleSafe( $ns, $text );
362 if ( $t && $t->isLocal() ) {
363 return [ $t->getNamespace(), '', '' ];
370 * Return an array of subpages beginning with $search that this special page will accept.
372 * @param string $search Prefix to search for
373 * @param int $limit Maximum number of results to return (usually 10)
374 * @param int $offset Number of results to skip (usually 0)
375 * @return string[] Matching subpages
377 public function prefixSearchSubpages( $search, $limit, $offset ) {
378 return $this->prefixSearchString( $search, $limit, $offset );
381 protected function getGroupName() {