MediaWiki master
SpecialNewPages.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Specials;
25
26use HtmlArmor;
44
54 protected $opts;
56 protected $customFilters;
57
58 protected $showNavigation = false;
59
60 private LinkBatchFactory $linkBatchFactory;
61 private IContentHandlerFactory $contentHandlerFactory;
62 private GroupPermissionsLookup $groupPermissionsLookup;
63 private RevisionLookup $revisionLookup;
64 private NamespaceInfo $namespaceInfo;
65 private UserOptionsLookup $userOptionsLookup;
66 private RowCommentFormatter $rowCommentFormatter;
67 private ChangeTagsStore $changeTagsStore;
68
78 public function __construct(
79 LinkBatchFactory $linkBatchFactory,
80 IContentHandlerFactory $contentHandlerFactory,
81 GroupPermissionsLookup $groupPermissionsLookup,
82 RevisionLookup $revisionLookup,
83 NamespaceInfo $namespaceInfo,
84 UserOptionsLookup $userOptionsLookup,
85 RowCommentFormatter $rowCommentFormatter,
86 ChangeTagsStore $changeTagsStore
87 ) {
88 parent::__construct( 'Newpages' );
89 $this->linkBatchFactory = $linkBatchFactory;
90 $this->contentHandlerFactory = $contentHandlerFactory;
91 $this->groupPermissionsLookup = $groupPermissionsLookup;
92 $this->revisionLookup = $revisionLookup;
93 $this->namespaceInfo = $namespaceInfo;
94 $this->userOptionsLookup = $userOptionsLookup;
95 $this->rowCommentFormatter = $rowCommentFormatter;
96 $this->changeTagsStore = $changeTagsStore;
97 }
98
102 protected function setup( $par ) {
103 $opts = new FormOptions();
104 $this->opts = $opts; // bind
105 $opts->add( 'hideliu', false );
106 $opts->add(
107 'hidepatrolled',
108 $this->userOptionsLookup->getBoolOption( $this->getUser(), 'newpageshidepatrolled' )
109 );
110 $opts->add( 'hidebots', false );
111 $opts->add( 'hideredirs', true );
112 $opts->add(
113 'limit',
114 $this->userOptionsLookup->getIntOption( $this->getUser(), 'rclimit' )
115 );
116 $opts->add( 'offset', '' );
117 $opts->add( 'namespace', '0' );
118 $opts->add( 'username', '' );
119 $opts->add( 'feed', '' );
120 $opts->add( 'tagfilter', '' );
121 $opts->add( 'tagInvert', false );
122 $opts->add( 'invert', false );
123 $opts->add( 'associated', false );
124 $opts->add( 'size-mode', 'max' );
125 $opts->add( 'size', 0 );
126
127 $this->customFilters = [];
128 $this->getHookRunner()->onSpecialNewPagesFilters( $this, $this->customFilters );
129 // @phan-suppress-next-line PhanEmptyForeach False positive
130 foreach ( $this->customFilters as $key => $params ) {
131 $opts->add( $key, $params['default'] );
132 }
133
135 if ( $par ) {
136 $this->parseParams( $par );
137 }
138
139 $opts->validateIntBounds( 'limit', 0, 5000 );
140 }
141
145 protected function parseParams( $par ) {
146 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
147 foreach ( $bits as $bit ) {
148 $m = [];
149 if ( $bit === 'shownav' ) {
150 $this->showNavigation = true;
151 } elseif ( $bit === 'hideliu' ) {
152 $this->opts->setValue( 'hideliu', true );
153 } elseif ( $bit === 'hidepatrolled' ) {
154 $this->opts->setValue( 'hidepatrolled', true );
155 } elseif ( $bit === 'hidebots' ) {
156 $this->opts->setValue( 'hidebots', true );
157 } elseif ( $bit === 'showredirs' ) {
158 $this->opts->setValue( 'hideredirs', false );
159 } elseif ( is_numeric( $bit ) ) {
160 $this->opts->setValue( 'limit', intval( $bit ) );
161 } elseif ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
162 $this->opts->setValue( 'limit', intval( $m[1] ) );
163 } elseif ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
164 // PG offsets not just digits!
165 $this->opts->setValue( 'offset', intval( $m[1] ) );
166 } elseif ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
167 $this->opts->setValue( 'username', $m[1] );
168 } elseif ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
169 $ns = $this->getLanguage()->getNsIndex( $m[1] );
170 if ( $ns !== false ) {
171 $this->opts->setValue( 'namespace', $ns );
172 }
173 } else {
174 // T62424 try to interpret unrecognized parameters as a namespace
175 $ns = $this->getLanguage()->getNsIndex( $bit );
176 if ( $ns !== false ) {
177 $this->opts->setValue( 'namespace', $ns );
178 }
179 }
180 }
181 }
182
188 public function execute( $par ) {
189 $out = $this->getOutput();
190
191 $this->setHeaders();
192 $this->outputHeader();
193
194 $this->showNavigation = !$this->including(); // Maybe changed in setup
195 $this->setup( $par );
196
197 $this->addHelpLink( 'Help:New pages' );
198
199 if ( !$this->including() ) {
200 // Settings
201 $this->form();
202
203 $feedType = $this->opts->getValue( 'feed' );
204 if ( $feedType ) {
205 $this->feed( $feedType );
206
207 return;
208 }
209
210 $allValues = $this->opts->getAllValues();
211 unset( $allValues['feed'] );
212 $out->setFeedAppendQuery( wfArrayToCgi( $allValues ) );
213 }
214
215 $pager = $this->getNewPagesPager();
216 $pager->mLimit = $this->opts->getValue( 'limit' );
217 $pager->mOffset = $this->opts->getValue( 'offset' );
218
219 if ( $pager->getNumRows() ) {
220 $navigation = '';
221 if ( $this->showNavigation ) {
222 $navigation = $pager->getNavigationBar();
223 }
224 $out->addHTML( $navigation . $pager->getBody() . $navigation );
225 // Add styles for change tags
226 $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
227 } else {
228 $out->addWikiMsg( 'specialpage-empty' );
229 }
230 }
231
232 protected function filterLinks() {
233 // show/hide links
234 $showhide = [ $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() ];
235
236 // Option value -> message mapping
237 $filters = [
238 'hideliu' => 'newpages-showhide-registered',
239 'hidepatrolled' => 'newpages-showhide-patrolled',
240 'hidebots' => 'newpages-showhide-bots',
241 'hideredirs' => 'newpages-showhide-redirect'
242 ];
243 foreach ( $this->customFilters as $key => $params ) {
244 $filters[$key] = $params['msg'];
245 }
246
247 // Disable some if needed
248 if ( !$this->canAnonymousUsersCreatePages() ) {
249 unset( $filters['hideliu'] );
250 }
251 if ( !$this->getUser()->useNPPatrol() ) {
252 unset( $filters['hidepatrolled'] );
253 }
254
255 $links = [];
256 $changed = $this->opts->getChangedValues();
257 unset( $changed['offset'] ); // Reset offset if query type changes
258
259 // wfArrayToCgi(), called from LinkRenderer/Title, will not output null and false values
260 // to the URL, which would omit some options (T158504). Fix it by explicitly setting them
261 // to 0 or 1.
262 // Also do this only for boolean options, not eg. namespace or tagfilter
263 foreach ( $changed as $key => $value ) {
264 if ( array_key_exists( $key, $filters ) ) {
265 $changed[$key] = $changed[$key] ? '1' : '0';
266 }
267 }
268
269 $self = $this->getPageTitle();
270 $linkRenderer = $this->getLinkRenderer();
271 foreach ( $filters as $key => $msg ) {
272 $onoff = 1 - $this->opts->getValue( $key );
273 $link = $linkRenderer->makeLink(
274 $self,
275 new HtmlArmor( $showhide[$onoff] ),
276 [],
277 [ $key => $onoff ] + $changed
278 );
279 $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
280 }
281
282 return $this->getLanguage()->pipeList( $links );
283 }
284
285 protected function form() {
286 $out = $this->getOutput();
287
288 // Consume values
289 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
290 $namespace = $this->opts->consumeValue( 'namespace' );
291 $username = $this->opts->consumeValue( 'username' );
292 $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
293 $tagInvertVal = $this->opts->consumeValue( 'tagInvert' );
294 $nsinvert = $this->opts->consumeValue( 'invert' );
295 $nsassociated = $this->opts->consumeValue( 'associated' );
296
297 $size = $this->opts->consumeValue( 'size' );
298 $max = $this->opts->consumeValue( 'size-mode' ) === 'max';
299
300 // Check username input validity
301 $ut = Title::makeTitleSafe( NS_USER, $username );
302 $userText = $ut ? $ut->getText() : '';
303
304 $formDescriptor = [
305 'namespace' => [
306 'type' => 'namespaceselect',
307 'name' => 'namespace',
308 'label-message' => 'namespace',
309 'default' => $namespace,
310 ],
311 'nsinvert' => [
312 'type' => 'check',
313 'name' => 'invert',
314 'label-message' => 'invert',
315 'default' => $nsinvert,
316 'tooltip' => 'invert',
317 ],
318 'nsassociated' => [
319 'type' => 'check',
320 'name' => 'associated',
321 'label-message' => 'namespace_association',
322 'default' => $nsassociated,
323 'tooltip' => 'namespace_association',
324 ],
325 'tagFilter' => [
326 'type' => 'tagfilter',
327 'name' => 'tagfilter',
328 'label-message' => 'tag-filter',
329 'default' => $tagFilterVal,
330 ],
331 'tagInvert' => [
332 'type' => 'check',
333 'name' => 'tagInvert',
334 'label-message' => 'invert',
335 'hide-if' => [ '===', 'tagFilter', '' ],
336 'default' => $tagInvertVal,
337 ],
338 'username' => [
339 'type' => 'user',
340 'name' => 'username',
341 'label-message' => 'newpages-username',
342 'default' => $userText,
343 'id' => 'mw-np-username',
344 'size' => 30,
345 ],
346 'size' => [
347 'type' => 'sizefilter',
348 'name' => 'size',
349 'default' => ( $max ? -1 : 1 ) * $size,
350 ],
351 ];
352
353 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
354
355 // Store query values in hidden fields so that form submission doesn't lose them
356 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
357 $htmlForm->addHiddenField( $key, $value );
358 }
359
360 $htmlForm
361 ->setMethod( 'get' )
362 ->setFormIdentifier( 'newpagesform' )
363 // The form should be visible on each request (inclusive requests with submitted forms), so
364 // return always false here.
365 ->setSubmitCallback(
366 static function () {
367 return false;
368 }
369 )
370 ->setSubmitTextMsg( 'newpages-submit' )
371 ->setWrapperLegendMsg( 'newpages' )
372 ->addFooterHtml( Html::rawElement(
373 'div',
374 [],
375 $this->filterLinks()
376 ) )
377 ->show();
378 $out->addModuleStyles( 'mediawiki.special' );
379 }
380
381 private function getNewPagesPager() {
382 return new NewPagesPager(
383 $this->getContext(),
384 $this->getLinkRenderer(),
385 $this->groupPermissionsLookup,
386 $this->getHookContainer(),
387 $this->linkBatchFactory,
388 $this->namespaceInfo,
389 $this->changeTagsStore,
390 $this->rowCommentFormatter,
391 $this->contentHandlerFactory,
392 $this->opts,
393 );
394 }
395
401 protected function feed( $type ) {
402 if ( !$this->getConfig()->get( MainConfigNames::Feed ) ) {
403 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
404
405 return;
406 }
407
408 $feedClasses = $this->getConfig()->get( MainConfigNames::FeedClasses );
409 if ( !isset( $feedClasses[$type] ) ) {
410 $this->getOutput()->addWikiMsg( 'feed-invalid' );
411
412 return;
413 }
414
415 $feed = new $feedClasses[$type](
416 $this->feedTitle(),
417 $this->msg( 'tagline' )->text(),
418 $this->getPageTitle()->getFullURL()
419 );
420
421 $pager = $this->getNewPagesPager();
422 $limit = $this->opts->getValue( 'limit' );
423 $pager->mLimit = min( $limit, $this->getConfig()->get( MainConfigNames::FeedLimit ) );
424
425 $feed->outHeader();
426 if ( $pager->getNumRows() > 0 ) {
427 foreach ( $pager->mResult as $row ) {
428 $feed->outItem( $this->feedItem( $row ) );
429 }
430 }
431 $feed->outFooter();
432 }
433
434 protected function feedTitle() {
435 $desc = $this->getDescription()->text();
436 $code = $this->getConfig()->get( MainConfigNames::LanguageCode );
437 $sitename = $this->getConfig()->get( MainConfigNames::Sitename );
438
439 return "$sitename - $desc [$code]";
440 }
441
442 protected function feedItem( $row ) {
443 $title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title );
444 if ( $title ) {
445 $date = $row->rc_timestamp;
446 $comments = $title->getTalkPage()->getFullURL();
447
448 return new FeedItem(
449 $title->getPrefixedText(),
450 $this->feedItemDesc( $row ),
451 $title->getFullURL(),
452 $date,
453 $this->feedItemAuthor( $row ),
454 $comments
455 );
456 } else {
457 return null;
458 }
459 }
460
461 protected function feedItemAuthor( $row ) {
462 return $row->rc_user_text ?? '';
463 }
464
465 protected function feedItemDesc( $row ) {
466 $revisionRecord = $this->revisionLookup->getRevisionById( $row->rev_id );
467 if ( !$revisionRecord ) {
468 return '';
469 }
470
471 $content = $revisionRecord->getContent( SlotRecord::MAIN );
472 if ( $content === null ) {
473 return '';
474 }
475
476 // XXX: include content model/type in feed item?
477 $revUser = $revisionRecord->getUser();
478 $revUserText = $revUser ? $revUser->getName() : '';
479 $revComment = $revisionRecord->getComment();
480 $revCommentText = $revComment ? $revComment->text : '';
481 return '<p>' . htmlspecialchars( $revUserText ) .
482 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
483 htmlspecialchars( FeedItem::stripComment( $revCommentText ) ) .
484 "</p>\n<hr />\n<div>" .
485 nl2br( htmlspecialchars( $content->serialize() ) ) . "</div>";
486 }
487
488 private function canAnonymousUsersCreatePages() {
489 return $this->groupPermissionsLookup->groupHasPermission( '*', 'createpage' ) ||
490 $this->groupPermissionsLookup->groupHasPermission( '*', 'createtalk' );
491 }
492
493 protected function getGroupName() {
494 return 'changes';
495 }
496
497 protected function getCacheTTL() {
498 return 60 * 5;
499 }
500}
501
506class_alias( SpecialNewPages::class, 'SpecialNewpages' );
const NS_USER
Definition Defines.php:66
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
array $params
The job parameters.
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:30
Gateway class for change_tags table.
This is basically a CommentFormatter with a CommentStore dependency, allowing it to retrieve comment ...
A base class for outputting syndication feeds (e.g.
Definition FeedItem.php:40
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:206
Helper class to keep track of options when mixing links and form elements.
fetchValuesFromRequest(WebRequest $r, $optionKeys=null)
Fetch values for all options (or selected options) from the given WebRequest, making them available f...
add( $name, $default, $type=self::AUTO)
Add an option to be handled by this FormOptions instance.
validateIntBounds( $name, $min, $max)
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 FeedClasses
Name constant for the FeedClasses setting, for use with Config::get()
const Feed
Name constant for the Feed setting, for use with Config::get()
const Sitename
Name constant for the Sitename setting, for use with Config::get()
const LanguageCode
Name constant for the LanguageCode setting, for use with Config::get()
const FeedLimit
Name constant for the FeedLimit setting, for use with Config::get()
Value object representing a content slot associated with a page revision.
Shortcut to construct an includable special page.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getDescription()
Returns the name that goes in the <h1> in the special page itself, and also the name that will be l...
getUser()
Shortcut to get the User executing this instance.
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...
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
A special page that list newly created pages.
feed( $type)
Output a subscription feed listing recent edits to this page.
__construct(LinkBatchFactory $linkBatchFactory, IContentHandlerFactory $contentHandlerFactory, GroupPermissionsLookup $groupPermissionsLookup, RevisionLookup $revisionLookup, NamespaceInfo $namespaceInfo, UserOptionsLookup $userOptionsLookup, RowCommentFormatter $rowCommentFormatter, ChangeTagsStore $changeTagsStore)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
execute( $par)
Show a form for filtering namespace and username.
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Represents a title within MediaWiki.
Definition Title.php:78
Provides access to user options.
Service for looking up page revisions.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...