Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
68.29% |
196 / 287 |
|
20.00% |
3 / 15 |
CRAP | |
0.00% |
0 / 1 |
SpecialNewPages | |
68.53% |
196 / 286 |
|
20.00% |
3 / 15 |
162.83 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
setup | |
93.94% |
31 / 33 |
|
0.00% |
0 / 1 |
4.00 | |||
parseParams | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
210 | |||
execute | |
88.00% |
22 / 25 |
|
0.00% |
0 / 1 |
5.04 | |||
filterLinks | |
96.77% |
30 / 31 |
|
0.00% |
0 / 1 |
8 | |||
form | |
98.78% |
81 / 82 |
|
0.00% |
0 / 1 |
4 | |||
getNewPagesPager | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
feed | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 | |||
feedTitle | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
feedItem | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
feedItemAuthor | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
feedItemDesc | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
30 | |||
canAnonymousUsersCreatePages | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getCacheTTL | |
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\Cache\LinkBatchFactory; |
24 | use MediaWiki\ChangeTags\ChangeTagsStore; |
25 | use MediaWiki\CommentFormatter\RowCommentFormatter; |
26 | use MediaWiki\Content\IContentHandlerFactory; |
27 | use MediaWiki\Feed\FeedItem; |
28 | use MediaWiki\Html\FormOptions; |
29 | use MediaWiki\Html\Html; |
30 | use MediaWiki\HTMLForm\HTMLForm; |
31 | use MediaWiki\MainConfigNames; |
32 | use MediaWiki\Pager\NewPagesPager; |
33 | use MediaWiki\Permissions\GroupPermissionsLookup; |
34 | use MediaWiki\Revision\RevisionLookup; |
35 | use MediaWiki\Revision\SlotRecord; |
36 | use MediaWiki\SpecialPage\IncludableSpecialPage; |
37 | use MediaWiki\Title\NamespaceInfo; |
38 | use MediaWiki\Title\Title; |
39 | use MediaWiki\User\Options\UserOptionsLookup; |
40 | use MediaWiki\User\TempUser\TempUserConfig; |
41 | use Wikimedia\HtmlArmor\HtmlArmor; |
42 | |
43 | /** |
44 | * List of newly created pages |
45 | * |
46 | * @see SpecialRecentChanges |
47 | * @see SpecialNewFiles |
48 | * @ingroup SpecialPage |
49 | */ |
50 | class SpecialNewPages extends IncludableSpecialPage { |
51 | /** |
52 | * @var FormOptions |
53 | */ |
54 | protected $opts; |
55 | /** @var array[] */ |
56 | protected $customFilters; |
57 | |
58 | /** @var bool */ |
59 | protected $showNavigation = false; |
60 | |
61 | private LinkBatchFactory $linkBatchFactory; |
62 | private IContentHandlerFactory $contentHandlerFactory; |
63 | private GroupPermissionsLookup $groupPermissionsLookup; |
64 | private RevisionLookup $revisionLookup; |
65 | private NamespaceInfo $namespaceInfo; |
66 | private UserOptionsLookup $userOptionsLookup; |
67 | private RowCommentFormatter $rowCommentFormatter; |
68 | private ChangeTagsStore $changeTagsStore; |
69 | private TempUserConfig $tempUserConfig; |
70 | |
71 | public function __construct( |
72 | LinkBatchFactory $linkBatchFactory, |
73 | IContentHandlerFactory $contentHandlerFactory, |
74 | GroupPermissionsLookup $groupPermissionsLookup, |
75 | RevisionLookup $revisionLookup, |
76 | NamespaceInfo $namespaceInfo, |
77 | UserOptionsLookup $userOptionsLookup, |
78 | RowCommentFormatter $rowCommentFormatter, |
79 | ChangeTagsStore $changeTagsStore, |
80 | TempUserConfig $tempUserConfig |
81 | ) { |
82 | parent::__construct( 'Newpages' ); |
83 | $this->linkBatchFactory = $linkBatchFactory; |
84 | $this->contentHandlerFactory = $contentHandlerFactory; |
85 | $this->groupPermissionsLookup = $groupPermissionsLookup; |
86 | $this->revisionLookup = $revisionLookup; |
87 | $this->namespaceInfo = $namespaceInfo; |
88 | $this->userOptionsLookup = $userOptionsLookup; |
89 | $this->rowCommentFormatter = $rowCommentFormatter; |
90 | $this->changeTagsStore = $changeTagsStore; |
91 | $this->tempUserConfig = $tempUserConfig; |
92 | } |
93 | |
94 | /** |
95 | * @param string|null $par |
96 | */ |
97 | protected function setup( $par ) { |
98 | $opts = new FormOptions(); |
99 | $this->opts = $opts; // bind |
100 | $opts->add( 'hideliu', false ); |
101 | $opts->add( |
102 | 'hidepatrolled', |
103 | $this->userOptionsLookup->getBoolOption( $this->getUser(), 'newpageshidepatrolled' ) |
104 | ); |
105 | $opts->add( 'hidebots', false ); |
106 | $opts->add( 'hideredirs', true ); |
107 | $opts->add( |
108 | 'limit', |
109 | $this->userOptionsLookup->getIntOption( $this->getUser(), 'rclimit' ) |
110 | ); |
111 | $opts->add( 'offset', '' ); |
112 | $opts->add( 'namespace', '0' ); |
113 | $opts->add( 'username', '' ); |
114 | $opts->add( 'feed', '' ); |
115 | $opts->add( 'tagfilter', '' ); |
116 | $opts->add( 'tagInvert', false ); |
117 | $opts->add( 'invert', false ); |
118 | $opts->add( 'associated', false ); |
119 | $opts->add( 'size-mode', 'max' ); |
120 | $opts->add( 'size', 0 ); |
121 | |
122 | $this->customFilters = []; |
123 | $this->getHookRunner()->onSpecialNewPagesFilters( $this, $this->customFilters ); |
124 | // @phan-suppress-next-line PhanEmptyForeach False positive |
125 | foreach ( $this->customFilters as $key => $params ) { |
126 | $opts->add( $key, $params['default'] ); |
127 | } |
128 | |
129 | $opts->fetchValuesFromRequest( $this->getRequest() ); |
130 | if ( $par ) { |
131 | $this->parseParams( $par ); |
132 | } |
133 | |
134 | // The hideliu option is only available when anonymous users can create pages, as if specified when they |
135 | // cannot create pages it always would produce no results. Therefore, if anon users cannot create pages |
136 | // then set hideliu as false overriding the value provided by the user. |
137 | if ( !$this->canAnonymousUsersCreatePages() ) { |
138 | $opts->setValue( 'hideliu', false, true ); |
139 | } |
140 | |
141 | $opts->validateIntBounds( 'limit', 0, 5000 ); |
142 | } |
143 | |
144 | /** |
145 | * @param string $par |
146 | */ |
147 | protected function parseParams( $par ) { |
148 | $bits = preg_split( '/\s*,\s*/', trim( $par ) ); |
149 | foreach ( $bits as $bit ) { |
150 | $m = []; |
151 | if ( $bit === 'shownav' ) { |
152 | $this->showNavigation = true; |
153 | } elseif ( $bit === 'hideliu' ) { |
154 | $this->opts->setValue( 'hideliu', true ); |
155 | } elseif ( $bit === 'hidepatrolled' ) { |
156 | $this->opts->setValue( 'hidepatrolled', true ); |
157 | } elseif ( $bit === 'hidebots' ) { |
158 | $this->opts->setValue( 'hidebots', true ); |
159 | } elseif ( $bit === 'showredirs' ) { |
160 | $this->opts->setValue( 'hideredirs', false ); |
161 | } elseif ( is_numeric( $bit ) ) { |
162 | $this->opts->setValue( 'limit', intval( $bit ) ); |
163 | } elseif ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) { |
164 | $this->opts->setValue( 'limit', intval( $m[1] ) ); |
165 | } elseif ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) { |
166 | // PG offsets not just digits! |
167 | $this->opts->setValue( 'offset', intval( $m[1] ) ); |
168 | } elseif ( preg_match( '/^username=(.*)$/', $bit, $m ) ) { |
169 | $this->opts->setValue( 'username', $m[1] ); |
170 | } elseif ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) { |
171 | $ns = $this->getLanguage()->getNsIndex( $m[1] ); |
172 | if ( $ns !== false ) { |
173 | $this->opts->setValue( 'namespace', $ns ); |
174 | } |
175 | } else { |
176 | // T62424 try to interpret unrecognized parameters as a namespace |
177 | $ns = $this->getLanguage()->getNsIndex( $bit ); |
178 | if ( $ns !== false ) { |
179 | $this->opts->setValue( 'namespace', $ns ); |
180 | } |
181 | } |
182 | } |
183 | } |
184 | |
185 | /** |
186 | * Show a form for filtering namespace and username |
187 | * |
188 | * @param string|null $par |
189 | */ |
190 | public function execute( $par ) { |
191 | $out = $this->getOutput(); |
192 | |
193 | $this->setHeaders(); |
194 | $this->outputHeader(); |
195 | |
196 | $this->showNavigation = !$this->including(); // Maybe changed in setup |
197 | $this->setup( $par ); |
198 | |
199 | $this->addHelpLink( 'Help:New pages' ); |
200 | |
201 | if ( !$this->including() ) { |
202 | // Settings |
203 | $this->form(); |
204 | |
205 | $feedType = $this->opts->getValue( 'feed' ); |
206 | if ( $feedType ) { |
207 | $this->feed( $feedType ); |
208 | |
209 | return; |
210 | } |
211 | |
212 | $allValues = $this->opts->getAllValues(); |
213 | unset( $allValues['feed'] ); |
214 | $out->setFeedAppendQuery( wfArrayToCgi( $allValues ) ); |
215 | } |
216 | |
217 | $pager = $this->getNewPagesPager(); |
218 | $pager->mLimit = $this->opts->getValue( 'limit' ); |
219 | $pager->mOffset = $this->opts->getValue( 'offset' ); |
220 | |
221 | if ( $pager->getNumRows() ) { |
222 | $navigation = ''; |
223 | if ( $this->showNavigation ) { |
224 | $navigation = $pager->getNavigationBar(); |
225 | } |
226 | $out->addHTML( $navigation . $pager->getBody() . $navigation ); |
227 | // Add styles for change tags |
228 | $out->addModuleStyles( 'mediawiki.interface.helpers.styles' ); |
229 | } else { |
230 | $out->addWikiMsg( 'specialpage-empty' ); |
231 | } |
232 | } |
233 | |
234 | protected function filterLinks() { |
235 | // show/hide links |
236 | $showhide = [ $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() ]; |
237 | |
238 | // Option value -> message mapping |
239 | $filters = [ |
240 | 'hideliu' => 'newpages-showhide-registered', |
241 | 'hidepatrolled' => 'newpages-showhide-patrolled', |
242 | 'hidebots' => 'newpages-showhide-bots', |
243 | 'hideredirs' => 'newpages-showhide-redirect' |
244 | ]; |
245 | foreach ( $this->customFilters as $key => $params ) { |
246 | $filters[$key] = $params['msg']; |
247 | } |
248 | |
249 | // Disable some if needed |
250 | if ( !$this->canAnonymousUsersCreatePages() ) { |
251 | unset( $filters['hideliu'] ); |
252 | } |
253 | if ( !$this->getUser()->useNPPatrol() ) { |
254 | unset( $filters['hidepatrolled'] ); |
255 | } |
256 | |
257 | $links = []; |
258 | $changed = $this->opts->getChangedValues(); |
259 | unset( $changed['offset'] ); // Reset offset if query type changes |
260 | |
261 | // wfArrayToCgi(), called from LinkRenderer/Title, will not output null and false values |
262 | // to the URL, which would omit some options (T158504). Fix it by explicitly setting them |
263 | // to 0 or 1. |
264 | // Also do this only for boolean options, not eg. namespace or tagfilter |
265 | foreach ( $changed as $key => $value ) { |
266 | if ( array_key_exists( $key, $filters ) ) { |
267 | $changed[$key] = $changed[$key] ? '1' : '0'; |
268 | } |
269 | } |
270 | |
271 | $self = $this->getPageTitle(); |
272 | $linkRenderer = $this->getLinkRenderer(); |
273 | foreach ( $filters as $key => $msg ) { |
274 | $onoff = 1 - $this->opts->getValue( $key ); |
275 | $link = $linkRenderer->makeLink( |
276 | $self, |
277 | new HtmlArmor( $showhide[$onoff] ), |
278 | [], |
279 | [ $key => $onoff ] + $changed |
280 | ); |
281 | $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped(); |
282 | } |
283 | |
284 | return $this->getLanguage()->pipeList( $links ); |
285 | } |
286 | |
287 | protected function form() { |
288 | $out = $this->getOutput(); |
289 | |
290 | // Consume values |
291 | $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW |
292 | $namespace = $this->opts->consumeValue( 'namespace' ); |
293 | $username = $this->opts->consumeValue( 'username' ); |
294 | $tagFilterVal = $this->opts->consumeValue( 'tagfilter' ); |
295 | $tagInvertVal = $this->opts->consumeValue( 'tagInvert' ); |
296 | $nsinvert = $this->opts->consumeValue( 'invert' ); |
297 | $nsassociated = $this->opts->consumeValue( 'associated' ); |
298 | |
299 | $size = $this->opts->consumeValue( 'size' ); |
300 | $max = $this->opts->consumeValue( 'size-mode' ) === 'max'; |
301 | |
302 | // Check username input validity |
303 | $ut = Title::makeTitleSafe( NS_USER, $username ); |
304 | $userText = $ut ? $ut->getText() : ''; |
305 | |
306 | $formDescriptor = [ |
307 | 'namespace' => [ |
308 | 'type' => 'namespaceselect', |
309 | 'name' => 'namespace', |
310 | 'label-message' => 'namespace', |
311 | 'default' => $namespace, |
312 | ], |
313 | 'nsinvert' => [ |
314 | 'type' => 'check', |
315 | 'name' => 'invert', |
316 | 'label-message' => 'invert', |
317 | 'default' => $nsinvert, |
318 | 'tooltip' => 'invert', |
319 | ], |
320 | 'nsassociated' => [ |
321 | 'type' => 'check', |
322 | 'name' => 'associated', |
323 | 'label-message' => 'namespace_association', |
324 | 'default' => $nsassociated, |
325 | 'tooltip' => 'namespace_association', |
326 | ], |
327 | 'tagFilter' => [ |
328 | 'type' => 'tagfilter', |
329 | 'name' => 'tagfilter', |
330 | 'label-message' => 'tag-filter', |
331 | 'default' => $tagFilterVal, |
332 | ], |
333 | 'tagInvert' => [ |
334 | 'type' => 'check', |
335 | 'name' => 'tagInvert', |
336 | 'label-message' => 'invert', |
337 | 'hide-if' => [ '===', 'tagFilter', '' ], |
338 | 'default' => $tagInvertVal, |
339 | ], |
340 | 'username' => [ |
341 | 'type' => 'user', |
342 | 'name' => 'username', |
343 | 'label-message' => 'newpages-username', |
344 | 'default' => $userText, |
345 | 'id' => 'mw-np-username', |
346 | 'size' => 30, |
347 | ], |
348 | 'size' => [ |
349 | 'type' => 'sizefilter', |
350 | 'name' => 'size', |
351 | 'default' => ( $max ? -1 : 1 ) * $size, |
352 | ], |
353 | ]; |
354 | |
355 | $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() ); |
356 | |
357 | // Store query values in hidden fields so that form submission doesn't lose them |
358 | foreach ( $this->opts->getUnconsumedValues() as $key => $value ) { |
359 | $htmlForm->addHiddenField( $key, $value ); |
360 | } |
361 | |
362 | $htmlForm |
363 | ->setMethod( 'get' ) |
364 | ->setFormIdentifier( 'newpagesform' ) |
365 | // The form should be visible on each request (inclusive requests with submitted forms), so |
366 | // return always false here. |
367 | ->setSubmitCallback( |
368 | static function () { |
369 | return false; |
370 | } |
371 | ) |
372 | ->setSubmitTextMsg( 'newpages-submit' ) |
373 | ->setWrapperLegendMsg( 'newpages' ) |
374 | ->addFooterHtml( Html::rawElement( |
375 | 'div', |
376 | [], |
377 | $this->filterLinks() |
378 | ) ) |
379 | ->show(); |
380 | $out->addModuleStyles( 'mediawiki.special' ); |
381 | } |
382 | |
383 | private function getNewPagesPager(): NewPagesPager { |
384 | return new NewPagesPager( |
385 | $this->getContext(), |
386 | $this->getLinkRenderer(), |
387 | $this->groupPermissionsLookup, |
388 | $this->getHookContainer(), |
389 | $this->linkBatchFactory, |
390 | $this->namespaceInfo, |
391 | $this->changeTagsStore, |
392 | $this->rowCommentFormatter, |
393 | $this->contentHandlerFactory, |
394 | $this->tempUserConfig, |
395 | $this->opts, |
396 | ); |
397 | } |
398 | |
399 | /** |
400 | * Output a subscription feed listing recent edits to this page. |
401 | * |
402 | * @param string $type |
403 | */ |
404 | protected function feed( $type ) { |
405 | if ( !$this->getConfig()->get( MainConfigNames::Feed ) ) { |
406 | $this->getOutput()->addWikiMsg( 'feed-unavailable' ); |
407 | |
408 | return; |
409 | } |
410 | |
411 | $feedClasses = $this->getConfig()->get( MainConfigNames::FeedClasses ); |
412 | if ( !isset( $feedClasses[$type] ) ) { |
413 | $this->getOutput()->addWikiMsg( 'feed-invalid' ); |
414 | |
415 | return; |
416 | } |
417 | |
418 | $feed = new $feedClasses[$type]( |
419 | $this->feedTitle(), |
420 | $this->msg( 'tagline' )->text(), |
421 | $this->getPageTitle()->getFullURL() |
422 | ); |
423 | |
424 | $pager = $this->getNewPagesPager(); |
425 | $limit = $this->opts->getValue( 'limit' ); |
426 | $pager->mLimit = min( $limit, $this->getConfig()->get( MainConfigNames::FeedLimit ) ); |
427 | |
428 | $feed->outHeader(); |
429 | if ( $pager->getNumRows() > 0 ) { |
430 | foreach ( $pager->mResult as $row ) { |
431 | $feed->outItem( $this->feedItem( $row ) ); |
432 | } |
433 | } |
434 | $feed->outFooter(); |
435 | } |
436 | |
437 | protected function feedTitle() { |
438 | $desc = $this->getDescription()->text(); |
439 | $code = $this->getConfig()->get( MainConfigNames::LanguageCode ); |
440 | $sitename = $this->getConfig()->get( MainConfigNames::Sitename ); |
441 | |
442 | return "$sitename - $desc [$code]"; |
443 | } |
444 | |
445 | protected function feedItem( $row ) { |
446 | $title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title ); |
447 | if ( $title ) { |
448 | $date = $row->rc_timestamp; |
449 | $comments = $title->getTalkPage()->getFullURL(); |
450 | |
451 | return new FeedItem( |
452 | $title->getPrefixedText(), |
453 | $this->feedItemDesc( $row ), |
454 | $title->getFullURL(), |
455 | $date, |
456 | $this->feedItemAuthor( $row ), |
457 | $comments |
458 | ); |
459 | } else { |
460 | return null; |
461 | } |
462 | } |
463 | |
464 | protected function feedItemAuthor( $row ) { |
465 | return $row->rc_user_text ?? ''; |
466 | } |
467 | |
468 | protected function feedItemDesc( $row ) { |
469 | $revisionRecord = $this->revisionLookup->getRevisionById( $row->rev_id ); |
470 | if ( !$revisionRecord ) { |
471 | return ''; |
472 | } |
473 | |
474 | $content = $revisionRecord->getContent( SlotRecord::MAIN ); |
475 | if ( $content === null ) { |
476 | return ''; |
477 | } |
478 | |
479 | // XXX: include content model/type in feed item? |
480 | $revUser = $revisionRecord->getUser(); |
481 | $revUserText = $revUser ? $revUser->getName() : ''; |
482 | $revComment = $revisionRecord->getComment(); |
483 | $revCommentText = $revComment ? $revComment->text : ''; |
484 | return '<p>' . htmlspecialchars( $revUserText ) . |
485 | $this->msg( 'colon-separator' )->inContentLanguage()->escaped() . |
486 | htmlspecialchars( FeedItem::stripComment( $revCommentText ) ) . |
487 | "</p>\n<hr />\n<div>" . |
488 | nl2br( htmlspecialchars( $content->serialize() ) ) . "</div>"; |
489 | } |
490 | |
491 | /** |
492 | * @return bool Whether any users classed anonymous can create pages (when temporary accounts are enabled, then |
493 | * this definition includes temporary accounts). |
494 | */ |
495 | private function canAnonymousUsersCreatePages(): bool { |
496 | // Get all the groups which anon users can be in. |
497 | $anonGroups = [ '*' ]; |
498 | if ( $this->tempUserConfig->isKnown() ) { |
499 | $anonGroups[] = 'temp'; |
500 | } |
501 | // Check if any of the groups have the createpage or createtalk right. |
502 | foreach ( $anonGroups as $group ) { |
503 | $anonUsersCanCreatePages = $this->groupPermissionsLookup->groupHasPermission( $group, 'createpage' ) || |
504 | $this->groupPermissionsLookup->groupHasPermission( $group, 'createtalk' ); |
505 | if ( $anonUsersCanCreatePages ) { |
506 | return true; |
507 | } |
508 | } |
509 | return false; |
510 | } |
511 | |
512 | protected function getGroupName() { |
513 | return 'changes'; |
514 | } |
515 | |
516 | protected function getCacheTTL() { |
517 | return 60 * 5; |
518 | } |
519 | } |
520 | |
521 | /** |
522 | * Retain the old class name for backwards compatibility. |
523 | * @deprecated since 1.41 |
524 | */ |
525 | class_alias( SpecialNewPages::class, 'SpecialNewpages' ); |