Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 191 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
SpecialAllPages | |
0.00% |
0 / 190 |
|
0.00% |
0 / 8 |
2550 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
56 | |||
outputHTMLForm | |
0.00% |
0 / 44 |
|
0.00% |
0 / 1 |
6 | |||
showToplevel | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
56 | |||
showChunk | |
0.00% |
0 / 100 |
|
0.00% |
0 / 1 |
600 | |||
getNamespaceKeyAndText | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
56 | |||
prefixSearchSubpages | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
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. |
7 | * |
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. |
12 | * |
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 |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\Specials; |
22 | |
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; |
35 | |
36 | /** |
37 | * Implements Special:Allpages |
38 | * |
39 | * @ingroup SpecialPage |
40 | * @todo Rewrite using IndexPager |
41 | */ |
42 | class SpecialAllPages extends IncludableSpecialPage { |
43 | |
44 | /** |
45 | * Maximum number of pages to show on single subpage. |
46 | * |
47 | * @var int |
48 | */ |
49 | protected $maxPerPage = 345; |
50 | |
51 | /** |
52 | * Determines, which message describes the input field 'nsfrom'. |
53 | * |
54 | * @var string |
55 | */ |
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 |
69 | $services = MediaWikiServices::getInstance(); |
70 | $this->dbProvider = $dbProvider ?? $services->getConnectionProvider(); |
71 | $this->searchEngineFactory = $searchEngineFactory ?? $services->getSearchEngineFactory(); |
72 | $this->pageStore = $pageStore ?? $services->getPageStore(); |
73 | } |
74 | |
75 | /** |
76 | * Entry point : initialise variables and call subfunctions. |
77 | * |
78 | * @param string|null $par Becomes "FOO" when called like Special:Allpages/FOO |
79 | */ |
80 | public function execute( $par ) { |
81 | $request = $this->getRequest(); |
82 | $out = $this->getOutput(); |
83 | |
84 | $this->setHeaders(); |
85 | $this->outputHeader(); |
86 | $out->getMetadata()->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 | |
116 | /** |
117 | * Outputs the HTMLForm used on this page |
118 | * |
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) |
123 | */ |
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 | |
176 | /** |
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) |
181 | */ |
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 | |
193 | /** |
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) |
198 | */ |
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. |
243 | /** @var ExistingPageRecord[] $pages */ |
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 | |
365 | /** |
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 |
369 | */ |
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 | |
393 | /** |
394 | * Return an array of subpages beginning with $search that this special page will accept. |
395 | * |
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 |
400 | */ |
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 | |
410 | /** @deprecated class alias since 1.41 */ |
411 | class_alias( SpecialAllPages::class, 'SpecialAllPages' ); |