3 * Implements Special:Contributions
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
24 use MediaWiki\Block\DatabaseBlock
;
25 use MediaWiki\MediaWikiServices
;
28 * Special:Contributions, show user contributions in a paged list
30 * @ingroup SpecialPage
32 class SpecialContributions
extends IncludableSpecialPage
{
35 public function __construct() {
36 parent
::__construct( 'Contributions' );
39 public function execute( $par ) {
41 $this->outputHeader();
42 $out = $this->getOutput();
43 // Modules required for viewing the list of contributions (also when included on other pages)
44 $out->addModuleStyles( [
45 'jquery.makeCollapsible.styles',
46 'mediawiki.interface.helpers.styles',
48 'mediawiki.special.changeslist',
51 'mediawiki.special.recentchanges',
52 // Certain skins e.g. Minerva might have disabled this module.
53 'mediawiki.page.ready'
55 $this->addHelpLink( 'Help:User contributions' );
58 $request = $this->getRequest();
60 $target = $par ??
$request->getVal( 'target' );
62 $this->opts
['deletedOnly'] = $request->getBool( 'deletedOnly' );
64 if ( !strlen( $target ) ) {
65 if ( !$this->including() ) {
66 $out->addHTML( $this->getForm( $this->opts
) );
72 $user = $this->getUser();
74 $this->opts
['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
75 $this->opts
['target'] = $target;
76 $this->opts
['topOnly'] = $request->getBool( 'topOnly' );
77 $this->opts
['newOnly'] = $request->getBool( 'newOnly' );
78 $this->opts
['hideMinor'] = $request->getBool( 'hideMinor' );
81 if ( ExternalUserNames
::isExternal( $target ) ) {
82 $userObj = User
::newFromName( $target, false );
84 $out->addHTML( $this->getForm( $this->opts
) );
88 $out->addSubtitle( $this->contributionsSub( $userObj ) );
89 $out->setHTMLTitle( $this->msg(
91 $this->msg( 'contributions-title', $target )->plain()
92 )->inContentLanguage() );
94 $nt = Title
::makeTitleSafe( NS_USER
, $target );
96 $out->addHTML( $this->getForm( $this->opts
) );
99 $userObj = User
::newFromName( $nt->getText(), false );
101 $out->addHTML( $this->getForm( $this->opts
) );
104 $id = $userObj->getId();
106 $target = $nt->getText();
107 $out->addSubtitle( $this->contributionsSub( $userObj ) );
108 $out->setHTMLTitle( $this->msg(
110 $this->msg( 'contributions-title', $target )->plain()
111 )->inContentLanguage() );
113 # For IP ranges, we want the contributionsSub, but not the skin-dependent
114 # links under 'Tools', which may include irrelevant links like 'Logs'.
115 if ( !IP
::isValidRange( $target ) ) {
116 $this->getSkin()->setRelevantUser( $userObj );
120 $ns = $request->getVal( 'namespace', null );
121 if ( $ns !== null && $ns !== '' && $ns !== 'all' ) {
122 $this->opts
['namespace'] = intval( $ns );
124 $this->opts
['namespace'] = '';
127 // Backwards compatibility: Before using OOUI form the old HTML form had
128 // fields for nsInvert and associated. These have now been replaced with the
129 // wpFilters query string parameters. These are retained to keep old URIs working.
130 $this->opts
['associated'] = $request->getBool( 'associated' );
131 $this->opts
['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
132 $nsFilters = $request->getArray( 'wpfilters', null );
133 if ( $nsFilters !== null ) {
134 $this->opts
['associated'] = in_array( 'associated', $nsFilters );
135 $this->opts
['nsInvert'] = in_array( 'nsInvert', $nsFilters );
138 $this->opts
['tagfilter'] = (string)$request->getVal( 'tagfilter' );
140 // Allows reverts to have the bot flag in recent changes. It is just here to
141 // be passed in the form at the top of the page
142 if ( MediaWikiServices
::getInstance()
143 ->getPermissionManager()
144 ->userHasRight( $user, 'markbotedits' ) && $request->getBool( 'bot' )
146 $this->opts
['bot'] = '1';
149 $skip = $request->getText( 'offset' ) ||
$request->getText( 'dir' ) == 'prev';
150 # Offset overrides year/month selection
152 $this->opts
['year'] = $request->getVal( 'year' );
153 $this->opts
['month'] = $request->getVal( 'month' );
155 $this->opts
['start'] = $request->getVal( 'start' );
156 $this->opts
['end'] = $request->getVal( 'end' );
158 $this->opts
= ContribsPager
::processDateFilter( $this->opts
);
160 if ( $this->opts
['namespace'] < NS_MAIN
) {
161 $this->getOutput()->wrapWikiMsg(
162 "<div class=\"mw-negative-namespace-not-supported error\">\n\$1\n</div>",
163 [ 'negative-namespace-not-supported' ]
165 $out->addHTML( $this->getForm( $this->opts
) );
169 $feedType = $request->getVal( 'feed' );
172 'action' => 'feedcontributions',
175 if ( $this->opts
['topOnly'] ) {
176 $feedParams['toponly'] = true;
178 if ( $this->opts
['newOnly'] ) {
179 $feedParams['newonly'] = true;
181 if ( $this->opts
['hideMinor'] ) {
182 $feedParams['hideminor'] = true;
184 if ( $this->opts
['deletedOnly'] ) {
185 $feedParams['deletedonly'] = true;
187 if ( $this->opts
['tagfilter'] !== '' ) {
188 $feedParams['tagfilter'] = $this->opts
['tagfilter'];
190 if ( $this->opts
['namespace'] !== '' ) {
191 $feedParams['namespace'] = $this->opts
['namespace'];
193 // Don't use year and month for the feed URL, but pass them on if
194 // we redirect to API (if $feedType is specified)
195 if ( $feedType && $this->opts
['year'] !== null ) {
196 $feedParams['year'] = $this->opts
['year'];
198 if ( $feedType && $this->opts
['month'] !== null ) {
199 $feedParams['month'] = $this->opts
['month'];
203 // Maintain some level of backwards compatibility
204 // If people request feeds using the old parameters, redirect to API
205 $feedParams['feedformat'] = $feedType;
206 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
208 $out->redirect( $url, '301' );
213 // Add RSS/atom links
214 $this->addFeedLinks( $feedParams );
216 if ( Hooks
::run( 'SpecialContributionsBeforeMainOutput', [ $id, $userObj, $this ] ) ) {
217 $pager = new ContribsPager( $this->getContext(), [
219 'namespace' => $this->opts
['namespace'],
220 'tagfilter' => $this->opts
['tagfilter'],
221 'start' => $this->opts
['start'],
222 'end' => $this->opts
['end'],
223 'deletedOnly' => $this->opts
['deletedOnly'],
224 'topOnly' => $this->opts
['topOnly'],
225 'newOnly' => $this->opts
['newOnly'],
226 'hideMinor' => $this->opts
['hideMinor'],
227 'nsInvert' => $this->opts
['nsInvert'],
228 'associated' => $this->opts
['associated'],
229 ], $this->getLinkRenderer() );
230 if ( !$this->including() ) {
231 $out->addHTML( $this->getForm( $this->opts
) );
234 if ( IP
::isValidRange( $target ) && !$pager->isQueryableRange( $target ) ) {
235 // Valid range, but outside CIDR limit.
236 $limits = $this->getConfig()->get( 'RangeContributionsCIDRLimit' );
237 $limit = $limits[ IP
::isIPv4( $target ) ?
'IPv4' : 'IPv6' ];
238 $out->addWikiMsg( 'sp-contributions-outofrange', $limit );
239 } elseif ( !$pager->getNumRows() ) {
240 $out->addWikiMsg( 'nocontribs', $target );
242 // @todo We just want a wiki ID here, not a "DB domain", but
243 // current status of MediaWiki conflates the two. See T235955.
244 $poolKey = WikiMap
::getCurrentWikiDbDomain() . ':SpecialContributions:';
245 if ( $this->getUser()->isAnon() ) {
246 $poolKey .= 'a:' . $this->getUser()->getName();
248 $poolKey .= 'u:' . $this->getUser()->getId();
250 $work = new PoolCounterWorkViaCallback( 'SpecialContributions', $poolKey, [
251 'doWork' => function () use ( $pager, $out ) {
252 # Show a message about replica DB lag, if applicable
253 $lag = $pager->getDatabase()->getSessionLagStatus()['lag'];
255 $out->showLagWarning( $lag );
258 $output = $pager->getBody();
259 if ( !$this->including() ) {
260 $output = $pager->getNavigationBar() .
262 $pager->getNavigationBar();
264 $out->addHTML( $output );
266 'error' => function () use ( $out ) {
267 $msg = $this->getUser()->isAnon()
268 ?
'sp-contributions-concurrency-ip'
269 : 'sp-contributions-concurrency-user';
270 $out->wrapWikiMsg( "<div class='errorbox'>\n$1\n</div>", $msg );
276 $out->preventClickjacking( $pager->getPreventClickjacking() );
278 # Show the appropriate "footer" message - WHOIS tools, etc.
279 if ( IP
::isValidRange( $target ) ) {
280 $message = 'sp-contributions-footer-anon-range';
281 } elseif ( IP
::isIPAddress( $target ) ) {
282 $message = 'sp-contributions-footer-anon';
283 } elseif ( $userObj->isAnon() ) {
284 // No message for non-existing users
287 $message = 'sp-contributions-footer';
290 if ( $message && !$this->including() && !$this->msg( $message, $target )->isDisabled() ) {
292 "<div class='mw-contributions-footer'>\n$1\n</div>",
293 [ $message, $target ] );
299 * Generates the subheading with links
300 * @param User $userObj User object for the target
301 * @return string Appropriately-escaped HTML to be output literally
302 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
305 protected function contributionsSub( $userObj ) {
306 if ( $userObj->isAnon() ) {
307 // Show a warning message that the user being searched for doesn't exist.
308 // User::isIP returns true for IP address and usemod IPs like '123.123.123.xxx',
309 // but returns false for IP ranges. We don't want to suggest either of these are
310 // valid usernames which we would with the 'contributions-userdoesnotexist' message.
311 if ( !User
::isIP( $userObj->getName() ) && !$userObj->isIPRange() ) {
312 $this->getOutput()->wrapWikiMsg(
313 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
315 'contributions-userdoesnotexist',
316 wfEscapeWikiText( $userObj->getName() ),
319 if ( !$this->including() ) {
320 $this->getOutput()->setStatusCode( 404 );
323 $user = htmlspecialchars( $userObj->getName() );
325 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
327 $nt = $userObj->getUserPage();
328 $talk = $userObj->getTalkPage();
331 $tools = self
::getUserLinks( $this, $userObj );
332 $links = Html
::openElement( 'span', [ 'class' => 'mw-changeslist-links' ] );
333 foreach ( $tools as $tool ) {
334 $links .= Html
::rawElement( 'span', [], $tool ) . ' ';
336 $links = trim( $links ) . Html
::closeElement( 'span' );
338 // Show a note if the user is blocked and display the last block log entry.
339 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
340 // and also this will display a totally irrelevant log entry as a current block.
341 if ( !$this->including() ) {
342 // For IP ranges you must give DatabaseBlock::newFromTarget the CIDR string
343 // and not a user object.
344 if ( $userObj->isIPRange() ) {
345 $block = DatabaseBlock
::newFromTarget( $userObj->getName(), $userObj->getName() );
347 $block = DatabaseBlock
::newFromTarget( $userObj, $userObj );
350 if ( !is_null( $block ) && $block->getType() != DatabaseBlock
::TYPE_AUTO
) {
351 if ( $block->getType() == DatabaseBlock
::TYPE_RANGE
) {
352 $nt = MediaWikiServices
::getInstance()->getNamespaceInfo()->
353 getCanonicalName( NS_USER
) . ':' . $block->getTarget();
356 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
357 LogEventsList
::showLogExtract(
364 'showIfEmpty' => false,
367 'sp-contributions-blocked-notice-anon' :
368 'sp-contributions-blocked-notice',
369 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
371 'offset' => '' # don't use WebRequest parameter offset
378 return Html
::rawElement( 'div', [ 'class' => 'mw-contributions-user-tools' ],
379 $this->msg( 'contributions-subtitle' )->rawParams( $user )->params( $userObj->getName() )
385 * Links to different places.
387 * @note This function is also called in DeletedContributionsPage
388 * @param SpecialPage $sp SpecialPage instance, for context
389 * @param User $target Target user object
392 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
393 $id = $target->getId();
394 $username = $target->getName();
395 $userpage = $target->getUserPage();
396 $talkpage = $target->getTalkPage();
397 $isIP = IP
::isValid( $username );
398 $isRange = IP
::isValidRange( $username );
400 $linkRenderer = $sp->getLinkRenderer();
403 # No talk pages for IP ranges.
405 $tools['user-talk'] = $linkRenderer->makeLink(
407 $sp->msg( 'sp-contributions-talk' )->text()
411 # Block / Change block / Unblock links
412 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
413 if ( $permissionManager->userHasRight( $sp->getUser(), 'block' ) ) {
414 if ( $target->getBlock() && $target->getBlock()->getType() != DatabaseBlock
::TYPE_AUTO
) {
415 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
416 SpecialPage
::getTitleFor( 'Block', $username ),
417 $sp->msg( 'change-blocklink' )->text()
419 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
420 SpecialPage
::getTitleFor( 'Unblock', $username ),
421 $sp->msg( 'unblocklink' )->text()
423 } else { # User is not blocked
424 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
425 SpecialPage
::getTitleFor( 'Block', $username ),
426 $sp->msg( 'blocklink' )->text()
432 $tools['log-block'] = $linkRenderer->makeKnownLink(
433 SpecialPage
::getTitleFor( 'Log', 'block' ),
434 $sp->msg( 'sp-contributions-blocklog' )->text(),
436 [ 'page' => $userpage->getPrefixedText() ]
439 # Suppression log link (T61120)
440 if ( $permissionManager->userHasRight( $sp->getUser(), 'suppressionlog' ) ) {
441 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
442 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
443 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
445 [ 'offender' => $username ]
449 # Don't show some links for IP ranges
451 # Uploads: hide if IPs cannot upload (T220674)
452 if ( !$isIP ||
$permissionManager->userHasRight( $target, 'upload' ) ) {
453 $tools['uploads'] = $linkRenderer->makeKnownLink(
454 SpecialPage
::getTitleFor( 'Listfiles', $username ),
455 $sp->msg( 'sp-contributions-uploads' )->text()
461 $tools['logs'] = $linkRenderer->makeKnownLink(
462 SpecialPage
::getTitleFor( 'Log', $username ),
463 $sp->msg( 'sp-contributions-logs' )->text()
466 # Add link to deleted user contributions for priviledged users
468 if ( $permissionManager->userHasRight( $sp->getUser(), 'deletedhistory' ) ) {
469 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
470 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
471 $sp->msg( 'sp-contributions-deleted', $username )->text()
476 # Add a link to change user rights for privileged users
477 $userrightsPage = new UserrightsPage();
478 $userrightsPage->setContext( $sp->getContext() );
479 if ( $userrightsPage->userCanChangeRights( $target ) ) {
480 $tools['userrights'] = $linkRenderer->makeKnownLink(
481 SpecialPage
::getTitleFor( 'Userrights', $username ),
482 $sp->msg( 'sp-contributions-userrights', $username )->text()
486 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
492 * Generates the namespace selector form with hidden attributes.
493 * @param array $pagerOptions with keys contribs, user, deletedOnly, limit, target, topOnly,
494 * newOnly, hideMinor, namespace, associated, nsInvert, tagfilter, year, start, end
495 * @return string HTML fragment
497 protected function getForm( array $pagerOptions ) {
498 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
499 // Modules required only for the form
500 $this->getOutput()->addModules( [
501 'mediawiki.userSuggest',
502 'mediawiki.special.contributions',
504 $this->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
505 $this->getOutput()->enableOOUI();
508 # Add hidden params for tracking except for parameters in $skipParameters
525 foreach ( $this->opts
as $name => $value ) {
526 if ( in_array( $name, $skipParameters ) ) {
537 $target = $this->opts
['target'] ??
null;
538 $fields['target'] = [
540 'cssclass' => 'mw-autocomplete-user mw-ui-input-inline mw-input',
541 'default' => $target ?
542 str_replace( '_', ' ', $target ) : '' ,
543 'label' => $this->msg( 'sp-contributions-username' )->text(),
545 'id' => 'mw-target-user-or-ip',
547 'autofocus' => !$target,
548 'section' => 'contribs-top',
551 $ns = $this->opts
['namespace'] ??
null;
552 $fields['namespace'] = [
553 'type' => 'namespaceselect',
554 'label' => $this->msg( 'namespace' )->text(),
555 'name' => 'namespace',
556 'cssclass' => 'namespaceselector',
559 'section' => 'contribs-top',
561 $request = $this->getRequest();
562 $nsFilters = $request->getArray( 'wpfilters' );
563 $fields['nsFilters'] = [
564 'class' => 'HTMLMultiSelectField',
566 'name' => 'wpfilters',
568 // Only shown when namespaces are selected.
569 'cssclass' => $ns === '' ?
570 'contribs-ns-filters mw-input-with-label mw-input-hidden' :
571 'contribs-ns-filters mw-input-with-label',
572 // `contribs-ns-filters` class allows these fields to be toggled on/off by JavaScript.
573 // See resources/src/mediawiki.special.recentchanges.js
576 $this->msg( 'invert' )->text() => 'nsInvert',
577 $this->msg( 'namespace_association' )->text() => 'associated',
579 'default' => $nsFilters,
580 'section' => 'contribs-top',
582 $fields['tagfilter'] = [
583 'type' => 'tagfilter',
584 'cssclass' => 'mw-tagfilter-input',
586 'label-message' => [ 'tag-filter', 'parse' ],
587 'name' => 'tagfilter',
589 'section' => 'contribs-top',
592 if ( MediaWikiServices
::getInstance()
593 ->getPermissionManager()
594 ->userHasRight( $this->getUser(), 'deletedhistory' )
596 $fields['deletedOnly'] = [
598 'id' => 'mw-show-deleted-only',
599 'label' => $this->msg( 'history-show-deleted' )->text(),
600 'name' => 'deletedOnly',
601 'section' => 'contribs-top',
605 $fields['topOnly'] = [
607 'id' => 'mw-show-top-only',
608 'label' => $this->msg( 'sp-contributions-toponly' )->text(),
610 'section' => 'contribs-top',
612 $fields['newOnly'] = [
614 'id' => 'mw-show-new-only',
615 'label' => $this->msg( 'sp-contributions-newonly' )->text(),
617 'section' => 'contribs-top',
619 $fields['hideMinor'] = [
621 'cssclass' => 'mw-hide-minor-edits',
622 'id' => 'mw-show-new-only',
623 'label' => $this->msg( 'sp-contributions-hideminor' )->text(),
624 'name' => 'hideMinor',
625 'section' => 'contribs-top',
628 // Allow additions at this point to the filters.
631 'SpecialContributions::getForm::filters',
632 [ $this, &$rawFilters ]
634 foreach ( $rawFilters as $filter ) {
635 // Backwards compatibility support for previous hook function signature.
636 if ( is_string( $filter ) ) {
639 'default' => $filter,
641 'section' => 'contribs-top',
645 ' returning string[]',
649 // Preferred append method.
657 'id' => 'mw-date-start',
658 'label' => $this->msg( 'date-range-from' )->text(),
660 'section' => 'contribs-date',
665 'id' => 'mw-date-end',
666 'label' => $this->msg( 'date-range-to' )->text(),
668 'section' => 'contribs-date',
671 $htmlForm = HTMLForm
::factory( 'ooui', $fields, $this->getContext() );
674 // When offset is defined, the user is paging through results
675 // so we hide the form by default to allow users to focus on browsing
676 // rather than defining search parameters
677 ->setCollapsibleOptions(
678 ( $pagerOptions['target'] ??
null ) ||
679 ( $pagerOptions['start'] ??
null ) ||
680 ( $pagerOptions['end'] ??
null )
682 ->setAction( wfScript() )
683 ->setSubmitText( $this->msg( 'sp-contributions-submit' )->text() )
684 ->setWrapperLegend( $this->msg( 'sp-contributions-search' )->text() );
686 $explain = $this->msg( 'sp-contributions-explain' );
687 if ( !$explain->isBlank() ) {
688 $htmlForm->addFooterText( "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>" );
691 $htmlForm->loadData();
693 return $htmlForm->getHTML( false );
697 * Return an array of subpages beginning with $search that this special page will accept.
699 * @param string $search Prefix to search for
700 * @param int $limit Maximum number of results to return (usually 10)
701 * @param int $offset Number of results to skip (usually 0)
702 * @return string[] Matching subpages
704 public function prefixSearchSubpages( $search, $limit, $offset ) {
705 $user = User
::newFromName( $search );
707 // No prefix suggestion for invalid user
710 // Autocomplete subpage as user list - public to allow caching
711 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
714 protected function getGroupName() {