MediaWiki master
SpecialAllPages.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
35
43
49 protected $maxPerPage = 345;
50
56 protected $nsfromMsg = 'allpagesfrom';
57
58 private IConnectionProvider $dbProvider;
59 private SearchEngineFactory $searchEngineFactory;
60 private PageStore $pageStore;
61
62 public function __construct(
63 IConnectionProvider $dbProvider = null,
64 SearchEngineFactory $searchEngineFactory = null,
65 PageStore $pageStore = null
66 ) {
67 parent::__construct( 'Allpages' );
68 // This class is extended and therefore falls back to global state - T265309
70 $this->dbProvider = $dbProvider ?? $services->getConnectionProvider();
71 $this->searchEngineFactory = $searchEngineFactory ?? $services->getSearchEngineFactory();
72 $this->pageStore = $pageStore ?? $services->getPageStore();
73 }
74
80 public function execute( $par ) {
81 $request = $this->getRequest();
82 $out = $this->getOutput();
83
84 $this->setHeaders();
85 $this->outputHeader();
86 $out->setPreventClickjacking( false );
87
88 # GET values
89 $from = $request->getVal( 'from', null );
90 $to = $request->getVal( 'to', null );
91 $namespace = $request->getInt( 'namespace' );
92
93 $miserMode = (bool)$this->getConfig()->get( MainConfigNames::MiserMode );
94
95 // Redirects filter is disabled in MiserMode
96 $hideredirects = $request->getBool( 'hideredirects', false ) && !$miserMode;
97
98 $namespaces = $this->getLanguage()->getNamespaces();
99
100 $out->setPageTitleMsg(
101 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) ) ?
102 $this->msg( 'allinnamespace' )->plaintextParams( str_replace( '_', ' ', $namespaces[$namespace] ) ) :
103 $this->msg( 'allarticles' )
104 );
105 $out->addModuleStyles( 'mediawiki.special' );
106
107 if ( $par !== null ) {
108 $this->showChunk( $namespace, $par, $to, $hideredirects );
109 } elseif ( $from !== null && $to === null ) {
110 $this->showChunk( $namespace, $from, $to, $hideredirects );
111 } else {
112 $this->showToplevel( $namespace, $from, $to, $hideredirects );
113 }
114 }
115
124 protected function outputHTMLForm( $namespace = NS_MAIN,
125 $from = '', $to = '', $hideRedirects = false
126 ) {
127 $miserMode = (bool)$this->getConfig()->get( MainConfigNames::MiserMode );
128 $formDescriptor = [
129 'from' => [
130 'type' => 'text',
131 'name' => 'from',
132 'id' => 'nsfrom',
133 'size' => 30,
134 'label-message' => 'allpagesfrom',
135 'default' => str_replace( '_', ' ', $from ),
136 ],
137 'to' => [
138 'type' => 'text',
139 'name' => 'to',
140 'id' => 'nsto',
141 'size' => 30,
142 'label-message' => 'allpagesto',
143 'default' => str_replace( '_', ' ', $to ),
144 ],
145 'namespace' => [
146 'type' => 'namespaceselect',
147 'name' => 'namespace',
148 'id' => 'namespace',
149 'label-message' => 'namespace',
150 'all' => null,
151 'default' => $namespace,
152 ],
153 'hideredirects' => [
154 'type' => 'check',
155 'name' => 'hideredirects',
156 'id' => 'hidredirects',
157 'label-message' => 'allpages-hide-redirects',
158 'value' => $hideRedirects,
159 ],
160 ];
161
162 if ( $miserMode ) {
163 unset( $formDescriptor['hideredirects'] );
164 }
165
166 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
167 $htmlForm
168 ->setMethod( 'get' )
169 ->setTitle( $this->getPageTitle() ) // Remove subpage
170 ->setWrapperLegendMsg( 'allpages' )
171 ->setSubmitTextMsg( 'allpagessubmit' )
172 ->prepareForm()
173 ->displayForm( false );
174 }
175
182 private function showToplevel(
183 $namespace = NS_MAIN, $from = null, $to = null, $hideredirects = false
184 ) {
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;
189
190 $this->showChunk( $namespace, $from, $to, $hideredirects );
191 }
192
199 private function showChunk(
200 $namespace = NS_MAIN, $from = null, $to = null, $hideredirects = false
201 ) {
202 $output = $this->getOutput();
203
204 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
205 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
206 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
207 $n = 0;
208 $prevTitle = null;
209
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;
216 } else {
217 [ $namespace, $fromKey, $from ] = $fromList;
218 [ , $toKey, $to ] = $toList;
219
220 $dbr = $this->dbProvider->getReplicaDatabase();
221 $filterConds = [ 'page_namespace' => $namespace ];
222 if ( $hideredirects ) {
223 $filterConds['page_is_redirect'] = 0;
224 }
225
226 $conds = $filterConds;
227 $conds[] = $dbr->expr( 'page_title', '>=', $fromKey );
228 if ( $toKey !== "" ) {
229 $conds[] = $dbr->expr( 'page_title', '<=', $toKey );
230 }
231
232 $res = $this->pageStore->newSelectQueryBuilder()
233 ->where( $conds )
234 ->caller( __METHOD__ )
235 ->orderBy( 'page_title' )
236 ->limit( $this->maxPerPage + 1 )
237 ->useIndex( 'page_name_title' )
238 ->fetchPageRecords();
239
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.
244 $pages = iterator_to_array( $res );
245
246 $linkRenderer = $this->getLinkRenderer();
247 if ( count( $pages ) > 0 ) {
248 $out = Html::openElement( 'ul', [ 'class' => 'mw-allpages-chunk' ] );
249
250 while ( $n < $this->maxPerPage && $n < count( $pages ) ) {
251 $page = $pages[$n];
252 $attributes = $page->isRedirect() ? [ 'class' => 'allpagesredirect' ] : [];
253
254 $out .= Html::rawElement( 'li', $attributes, $linkRenderer->makeKnownLink( $page ) ) . "\n";
255 $n++;
256 }
257 $out .= Html::closeElement( 'ul' );
258
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 );
263 }
264 } else {
265 $out = '';
266 }
267
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' )
274 ->from( 'page' )
275 ->where( $prevConds )
276 ->orderBy( 'page_title', SelectQueryBuilder::SORT_DESC )
277 ->offset( $this->maxPerPage - 1 )
278 ->caller( __METHOD__ )->fetchField();
279
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' )
285 ->from( 'page' )
286 ->where( $prevConds )
287 ->orderBy( 'page_title' )
288 ->caller( __METHOD__ )->fetchField();
289 }
290
291 if ( $prevKey !== false ) {
292 $prevTitle = Title::makeTitle( $namespace, $prevKey );
293 }
294 }
295 }
296
297 if ( $this->including() ) {
298 $output->addHTML( $out );
299 return;
300 }
301
302 $navLinks = [];
303 $self = $this->getPageTitle();
304
305 $linkRenderer = $this->getLinkRenderer();
306 // Generate a "previous page" link if needed
307 if ( $prevTitle ) {
308 $query = [ 'from' => $prevTitle->getText() ];
309
310 if ( $namespace ) {
311 $query['namespace'] = $namespace;
312 }
313
314 if ( $hideredirects ) {
315 $query['hideredirects'] = $hideredirects;
316 }
317
318 $navLinks[] = $linkRenderer->makeKnownLink(
319 $self,
320 $this->msg( 'prevpage', $prevTitle->getText() )->text(),
321 [],
322 $query
323 );
324
325 }
326
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() ];
332
333 if ( $namespace ) {
334 $query['namespace'] = $namespace;
335 }
336
337 if ( $hideredirects ) {
338 $query['hideredirects'] = $hideredirects;
339 }
340
341 $navLinks[] = $linkRenderer->makeKnownLink(
342 $self,
343 $this->msg( 'nextpage', $t->getText() )->text(),
344 [],
345 $query
346 );
347 }
348
349 $this->outputHTMLForm( $namespace, $from, $to, $hideredirects );
350
351 if ( count( $navLinks ) ) {
352 // Add pagination links
353 $pagination = Html::rawElement( 'div',
354 [ 'class' => 'mw-allpages-nav' ],
355 $this->getLanguage()->pipeList( $navLinks )
356 );
357
358 $output->addHTML( $pagination );
359 $out .= Html::element( 'hr' ) . $pagination; // Footer
360 }
361
362 $output->addHTML( $out );
363 }
364
370 protected function getNamespaceKeyAndText( $ns, $text ) {
371 if ( $text == '' ) {
372 # shortcut for common case
373 return [ $ns, '', '' ];
374 }
375
376 $t = Title::makeTitleSafe( $ns, $text );
377 if ( $t && $t->isLocal() ) {
378 return [ $t->getNamespace(), $t->getDBkey(), $t->getText() ];
379 } elseif ( $t ) {
380 return null;
381 }
382
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(), '', '' ];
388 } else {
389 return null;
390 }
391 }
392
401 public function prefixSearchSubpages( $search, $limit, $offset ) {
402 return $this->prefixSearchString( $search, $limit, $offset, $this->searchEngineFactory );
403 }
404
405 protected function getGroupName() {
406 return 'pages';
407 }
408}
409
411class_alias( SpecialAllPages::class, 'SpecialAllPages' );
const NS_MAIN
Definition Defines.php:65
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:208
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
A class containing constants representing the names of configuration variables.
const MiserMode
Name constant for the MiserMode setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Shortcut to construct an includable special page.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
prefixSearchString( $search, $limit, $offset, SearchEngineFactory $searchEngineFactory=null)
Perform a regular substring search for prefixSearchSubpages.
getPageTitle( $subpage=false)
Get a self-referential title object.
getConfig()
Shortcut to get main config object.
getContext()
Gets the context this SpecialPage is executed in.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
including( $x=null)
Whether the special page is being evaluated via transclusion.
getLanguage()
Shortcut to get user's language.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
Implements Special:Allpages.
string $nsfromMsg
Determines, which message describes the input field 'nsfrom'.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
int $maxPerPage
Maximum number of pages to show on single subpage.
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
execute( $par)
Entry point : initialise variables and call subfunctions.
outputHTMLForm( $namespace=NS_MAIN, $from='', $to='', $hideRedirects=false)
Outputs the HTMLForm used on this page.
__construct(IConnectionProvider $dbProvider=null, SearchEngineFactory $searchEngineFactory=null, PageStore $pageStore=null)
Represents the target of a wiki link.
Represents a title within MediaWiki.
Definition Title.php:79
Factory class for SearchEngine.
Build SELECT queries with a fluent interface.
Data record representing a page that currently exists as an editable page on a wiki.
Provide primary and replica IDatabase connections.
element(SerializerNode $parent, SerializerNode $node, $contents)