3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Specials
;
23 use MediaWiki\Html\Html
;
24 use MediaWiki\HTMLForm\HTMLForm
;
25 use MediaWiki\MainConfigNames
;
26 use MediaWiki\MediaWikiServices
;
27 use MediaWiki\Page\ExistingPageRecord
;
28 use MediaWiki\Page\PageStore
;
29 use MediaWiki\SpecialPage\IncludableSpecialPage
;
30 use MediaWiki\Title\Title
;
31 use MediaWiki\Title\TitleValue
;
32 use SearchEngineFactory
;
33 use Wikimedia\Rdbms\IConnectionProvider
;
34 use Wikimedia\Rdbms\SelectQueryBuilder
;
37 * Implements Special:Allpages
39 * @ingroup SpecialPage
40 * @todo Rewrite using IndexPager
42 class SpecialAllPages
extends IncludableSpecialPage
{
45 * Maximum number of pages to show on single subpage.
49 protected $maxPerPage = 345;
52 * Determines, which message describes the input field 'nsfrom'.
56 protected $nsfromMsg = 'allpagesfrom';
58 private IConnectionProvider
$dbProvider;
59 private SearchEngineFactory
$searchEngineFactory;
60 private PageStore
$pageStore;
62 public function __construct(
63 ?IConnectionProvider
$dbProvider = null,
64 ?SearchEngineFactory
$searchEngineFactory = null,
65 ?PageStore
$pageStore = null
67 parent
::__construct( 'Allpages' );
68 // This class is extended and therefore falls back to global state - T265309
69 $services = MediaWikiServices
::getInstance();
70 $this->dbProvider
= $dbProvider ??
$services->getConnectionProvider();
71 $this->searchEngineFactory
= $searchEngineFactory ??
$services->getSearchEngineFactory();
72 $this->pageStore
= $pageStore ??
$services->getPageStore();
76 * Entry point : initialise variables and call subfunctions.
78 * @param string|null $par Becomes "FOO" when called like Special:Allpages/FOO
80 public function execute( $par ) {
81 $request = $this->getRequest();
82 $out = $this->getOutput();
85 $this->outputHeader();
86 $out->getMetadata()->setPreventClickjacking( false );
89 $from = $request->getVal( 'from', null );
90 $to = $request->getVal( 'to', null );
91 $namespace = $request->getInt( 'namespace' );
93 $miserMode = (bool)$this->getConfig()->get( MainConfigNames
::MiserMode
);
95 // Redirects filter is disabled in MiserMode
96 $hideredirects = $request->getBool( 'hideredirects', false ) && !$miserMode;
98 $namespaces = $this->getLanguage()->getNamespaces();
100 $out->setPageTitleMsg(
101 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) ) ?
102 $this->msg( 'allinnamespace' )->plaintextParams( str_replace( '_', ' ', $namespaces[$namespace] ) ) :
103 $this->msg( 'allarticles' )
105 $out->addModuleStyles( 'mediawiki.special' );
107 if ( $par !== null ) {
108 $this->showChunk( $namespace, $par, $to, $hideredirects );
109 } elseif ( $from !== null && $to === null ) {
110 $this->showChunk( $namespace, $from, $to, $hideredirects );
112 $this->showToplevel( $namespace, $from, $to, $hideredirects );
117 * Outputs the HTMLForm used on this page
119 * @param int $namespace A namespace constant (default NS_MAIN).
120 * @param string $from DbKey we are starting listing at.
121 * @param string $to DbKey we are ending listing at.
122 * @param bool $hideRedirects Don't show redirects (default false)
124 protected function outputHTMLForm( $namespace = NS_MAIN
,
125 $from = '', $to = '', $hideRedirects = false
127 $miserMode = (bool)$this->getConfig()->get( MainConfigNames
::MiserMode
);
134 'label-message' => 'allpagesfrom',
135 'default' => str_replace( '_', ' ', $from ),
142 'label-message' => 'allpagesto',
143 'default' => str_replace( '_', ' ', $to ),
146 'type' => 'namespaceselect',
147 'name' => 'namespace',
149 'label-message' => 'namespace',
151 'default' => $namespace,
155 'name' => 'hideredirects',
156 'id' => 'hidredirects',
157 'label-message' => 'allpages-hide-redirects',
158 'value' => $hideRedirects,
163 unset( $formDescriptor['hideredirects'] );
166 $htmlForm = HTMLForm
::factory( 'ooui', $formDescriptor, $this->getContext() );
169 ->setTitle( $this->getPageTitle() ) // Remove subpage
170 ->setWrapperLegendMsg( 'allpages' )
171 ->setSubmitTextMsg( 'allpagessubmit' )
173 ->displayForm( false );
177 * @param int $namespace (default NS_MAIN)
178 * @param string|null $from List all pages from this name
179 * @param string|null $to List all pages to this name
180 * @param bool $hideredirects Don't show redirects (default false)
182 private function showToplevel(
183 $namespace = NS_MAIN
, $from = null, $to = null, $hideredirects = false
185 $from = $from ? Title
::makeTitleSafe( $namespace, $from ) : null;
186 $to = $to ? Title
::makeTitleSafe( $namespace, $to ) : null;
187 $from = ( $from && $from->isLocal() ) ?
$from->getDBkey() : null;
188 $to = ( $to && $to->isLocal() ) ?
$to->getDBkey() : null;
190 $this->showChunk( $namespace, $from, $to, $hideredirects );
194 * @param int $namespace Namespace (Default NS_MAIN)
195 * @param string|null $from List all pages from this name (default null)
196 * @param string|null $to List all pages to this name (default null)
197 * @param bool $hideredirects Don't show redirects (default false)
199 private function showChunk(
200 $namespace = NS_MAIN
, $from = null, $to = null, $hideredirects = false
202 $output = $this->getOutput();
204 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
205 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
206 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
210 if ( !$fromList ||
!$toList ) {
211 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
212 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
213 // Show errormessage and reset to NS_MAIN
214 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
215 $namespace = NS_MAIN
;
217 [ $namespace, $fromKey, $from ] = $fromList;
218 [ , $toKey, $to ] = $toList;
220 $dbr = $this->dbProvider
->getReplicaDatabase();
221 $filterConds = [ 'page_namespace' => $namespace ];
222 if ( $hideredirects ) {
223 $filterConds['page_is_redirect'] = 0;
226 $conds = $filterConds;
227 $conds[] = $dbr->expr( 'page_title', '>=', $fromKey );
228 if ( $toKey !== "" ) {
229 $conds[] = $dbr->expr( 'page_title', '<=', $toKey );
232 $res = $this->pageStore
->newSelectQueryBuilder()
234 ->caller( __METHOD__
)
235 ->orderBy( 'page_title' )
236 ->limit( $this->maxPerPage +
1 )
237 ->useIndex( 'page_name_title' )
238 ->fetchPageRecords();
240 // Eagerly fetch the set of pages to be displayed and warm up LinkCache (T328174).
241 // Note that we can't use fetchPageRecordArray() here as that returns an array keyed
242 // by page IDs; we need a simple sequence.
243 /** @var ExistingPageRecord[] $pages */
244 $pages = iterator_to_array( $res );
246 $linkRenderer = $this->getLinkRenderer();
247 if ( count( $pages ) > 0 ) {
248 $out = Html
::openElement( 'ul', [ 'class' => 'mw-allpages-chunk' ] );
250 while ( $n < $this->maxPerPage
&& $n < count( $pages ) ) {
252 $attributes = $page->isRedirect() ?
[ 'class' => 'allpagesredirect' ] : [];
254 $out .= Html
::rawElement( 'li', $attributes, $linkRenderer->makeKnownLink( $page ) ) . "\n";
257 $out .= Html
::closeElement( 'ul' );
259 if ( count( $pages ) > 2 ) {
260 // Only apply CSS column styles if there's more than 2 entries.
261 // Otherwise, rendering is broken as "mw-allpages-body"'s CSS column count is 3.
262 $out = Html
::rawElement( 'div', [ 'class' => 'mw-allpages-body' ], $out );
268 if ( $fromKey !== '' && !$this->including() ) {
269 # Get the first title from previous chunk
270 $prevConds = $filterConds;
271 $prevConds[] = $dbr->expr( 'page_title', '<', $fromKey );
272 $prevKey = $dbr->newSelectQueryBuilder()
273 ->select( 'page_title' )
275 ->where( $prevConds )
276 ->orderBy( 'page_title', SelectQueryBuilder
::SORT_DESC
)
277 ->offset( $this->maxPerPage
- 1 )
278 ->caller( __METHOD__
)->fetchField();
280 if ( $prevKey === false ) {
281 # The previous chunk is not complete, need to link to the very first title
282 # available in the database
283 $prevKey = $dbr->newSelectQueryBuilder()
284 ->select( 'page_title' )
286 ->where( $prevConds )
287 ->orderBy( 'page_title' )
288 ->caller( __METHOD__
)->fetchField();
291 if ( $prevKey !== false ) {
292 $prevTitle = Title
::makeTitle( $namespace, $prevKey );
297 if ( $this->including() ) {
298 $output->addHTML( $out );
303 $self = $this->getPageTitle();
305 $linkRenderer = $this->getLinkRenderer();
306 // Generate a "previous page" link if needed
308 $query = [ 'from' => $prevTitle->getText() ];
311 $query['namespace'] = $namespace;
314 if ( $hideredirects ) {
315 $query['hideredirects'] = $hideredirects;
318 $navLinks[] = $linkRenderer->makeKnownLink(
320 $this->msg( 'prevpage', $prevTitle->getText() )->text(),
327 // Generate a "next page" link if needed
328 if ( $n === $this->maxPerPage
&& isset( $pages[$n] ) ) {
329 # $t is the first link of the next chunk
330 $t = TitleValue
::newFromPage( $pages[$n] );
331 $query = [ 'from' => $t->getText() ];
334 $query['namespace'] = $namespace;
337 if ( $hideredirects ) {
338 $query['hideredirects'] = $hideredirects;
341 $navLinks[] = $linkRenderer->makeKnownLink(
343 $this->msg( 'nextpage', $t->getText() )->text(),
349 $this->outputHTMLForm( $namespace, $from ??
'', $to ??
'', $hideredirects );
351 if ( count( $navLinks ) ) {
352 // Add pagination links
353 $pagination = Html
::rawElement( 'div',
354 [ 'class' => 'mw-allpages-nav' ],
355 $this->getLanguage()->pipeList( $navLinks )
358 $output->addHTML( $pagination );
359 $out .= Html
::element( 'hr' ) . $pagination; // Footer
362 $output->addHTML( $out );
366 * @param int $ns The namespace of the article
367 * @param string $text The name of the article
368 * @return array|null [ int namespace, string dbkey, string pagename ] or null on error
370 protected function getNamespaceKeyAndText( $ns, $text ) {
372 # shortcut for common case
373 return [ $ns, '', '' ];
376 $t = Title
::makeTitleSafe( $ns, $text );
377 if ( $t && $t->isLocal() ) {
378 return [ $t->getNamespace(), $t->getDBkey(), $t->getText() ];
383 # try again, in case the problem was an empty pagename
384 $text = preg_replace( '/(#|$)/', 'X$1', $text );
385 $t = Title
::makeTitleSafe( $ns, $text );
386 if ( $t && $t->isLocal() ) {
387 return [ $t->getNamespace(), '', '' ];
394 * Return an array of subpages beginning with $search that this special page will accept.
396 * @param string $search Prefix to search for
397 * @param int $limit Maximum number of results to return (usually 10)
398 * @param int $offset Number of results to skip (usually 0)
399 * @return string[] Matching subpages
401 public function prefixSearchSubpages( $search, $limit, $offset ) {
402 return $this->prefixSearchString( $search, $limit, $offset, $this->searchEngineFactory
);
405 protected function getGroupName() {
410 /** @deprecated class alias since 1.41 */
411 class_alias( SpecialAllPages
::class, 'SpecialAllPages' );