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 # Show a message about replica DB lag, if applicable
243 $lag = $pager->getDatabase()->getSessionLagStatus()['lag'];
245 $out->showLagWarning( $lag );
248 $output = $pager->getBody();
249 if ( !$this->including() ) {
250 $output = $pager->getNavigationBar() .
252 $pager->getNavigationBar();
254 $out->addHTML( $output );
257 $out->preventClickjacking( $pager->getPreventClickjacking() );
259 # Show the appropriate "footer" message - WHOIS tools, etc.
260 if ( IP
::isValidRange( $target ) ) {
261 $message = 'sp-contributions-footer-anon-range';
262 } elseif ( IP
::isIPAddress( $target ) ) {
263 $message = 'sp-contributions-footer-anon';
264 } elseif ( $userObj->isAnon() ) {
265 // No message for non-existing users
268 $message = 'sp-contributions-footer';
271 if ( $message && !$this->including() && !$this->msg( $message, $target )->isDisabled() ) {
273 "<div class='mw-contributions-footer'>\n$1\n</div>",
274 [ $message, $target ] );
280 * Generates the subheading with links
281 * @param User $userObj User object for the target
282 * @return string Appropriately-escaped HTML to be output literally
283 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
286 protected function contributionsSub( $userObj ) {
287 if ( $userObj->isAnon() ) {
288 // Show a warning message that the user being searched for doesn't exist.
289 // User::isIP returns true for IP address and usemod IPs like '123.123.123.xxx',
290 // but returns false for IP ranges. We don't want to suggest either of these are
291 // valid usernames which we would with the 'contributions-userdoesnotexist' message.
292 if ( !User
::isIP( $userObj->getName() ) && !$userObj->isIPRange() ) {
293 $this->getOutput()->wrapWikiMsg(
294 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
296 'contributions-userdoesnotexist',
297 wfEscapeWikiText( $userObj->getName() ),
300 if ( !$this->including() ) {
301 $this->getOutput()->setStatusCode( 404 );
304 $user = htmlspecialchars( $userObj->getName() );
306 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
308 $nt = $userObj->getUserPage();
309 $talk = $userObj->getTalkPage();
312 $tools = self
::getUserLinks( $this, $userObj );
313 $links = Html
::openElement( 'span', [ 'class' => 'mw-changeslist-links' ] );
314 foreach ( $tools as $tool ) {
315 $links .= Html
::rawElement( 'span', [], $tool ) . ' ';
317 $links = trim( $links ) . Html
::closeElement( 'span' );
319 // Show a note if the user is blocked and display the last block log entry.
320 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
321 // and also this will display a totally irrelevant log entry as a current block.
322 if ( !$this->including() ) {
323 // For IP ranges you must give DatabaseBlock::newFromTarget the CIDR string
324 // and not a user object.
325 if ( $userObj->isIPRange() ) {
326 $block = DatabaseBlock
::newFromTarget( $userObj->getName(), $userObj->getName() );
328 $block = DatabaseBlock
::newFromTarget( $userObj, $userObj );
331 if ( !is_null( $block ) && $block->getType() != DatabaseBlock
::TYPE_AUTO
) {
332 if ( $block->getType() == DatabaseBlock
::TYPE_RANGE
) {
333 $nt = MediaWikiServices
::getInstance()->getNamespaceInfo()->
334 getCanonicalName( NS_USER
) . ':' . $block->getTarget();
337 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
338 LogEventsList
::showLogExtract(
345 'showIfEmpty' => false,
348 'sp-contributions-blocked-notice-anon' :
349 'sp-contributions-blocked-notice',
350 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
352 'offset' => '' # don't use WebRequest parameter offset
359 return Html
::rawElement( 'div', [ 'class' => 'mw-contributions-user-tools' ],
360 $this->msg( 'contributions-subtitle' )->rawParams( $user )->params( $userObj->getName() )
366 * Links to different places.
368 * @note This function is also called in DeletedContributionsPage
369 * @param SpecialPage $sp SpecialPage instance, for context
370 * @param User $target Target user object
373 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
374 $id = $target->getId();
375 $username = $target->getName();
376 $userpage = $target->getUserPage();
377 $talkpage = $target->getTalkPage();
378 $isIP = IP
::isValid( $username );
379 $isRange = IP
::isValidRange( $username );
381 $linkRenderer = $sp->getLinkRenderer();
384 # No talk pages for IP ranges.
386 $tools['user-talk'] = $linkRenderer->makeLink(
388 $sp->msg( 'sp-contributions-talk' )->text()
392 # Block / Change block / Unblock links
393 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
394 if ( $permissionManager->userHasRight( $sp->getUser(), 'block' ) ) {
395 if ( $target->getBlock() && $target->getBlock()->getType() != DatabaseBlock
::TYPE_AUTO
) {
396 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
397 SpecialPage
::getTitleFor( 'Block', $username ),
398 $sp->msg( 'change-blocklink' )->text()
400 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
401 SpecialPage
::getTitleFor( 'Unblock', $username ),
402 $sp->msg( 'unblocklink' )->text()
404 } else { # User is not blocked
405 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
406 SpecialPage
::getTitleFor( 'Block', $username ),
407 $sp->msg( 'blocklink' )->text()
413 $tools['log-block'] = $linkRenderer->makeKnownLink(
414 SpecialPage
::getTitleFor( 'Log', 'block' ),
415 $sp->msg( 'sp-contributions-blocklog' )->text(),
417 [ 'page' => $userpage->getPrefixedText() ]
420 # Suppression log link (T61120)
421 if ( $permissionManager->userHasRight( $sp->getUser(), 'suppressionlog' ) ) {
422 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
423 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
424 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
426 [ 'offender' => $username ]
430 # Don't show some links for IP ranges
432 # Uploads: hide if IPs cannot upload (T220674)
433 if ( !$isIP ||
$permissionManager->userHasRight( $target, 'upload' ) ) {
434 $tools['uploads'] = $linkRenderer->makeKnownLink(
435 SpecialPage
::getTitleFor( 'Listfiles', $username ),
436 $sp->msg( 'sp-contributions-uploads' )->text()
442 $tools['logs'] = $linkRenderer->makeKnownLink(
443 SpecialPage
::getTitleFor( 'Log', $username ),
444 $sp->msg( 'sp-contributions-logs' )->text()
447 # Add link to deleted user contributions for priviledged users
449 if ( $permissionManager->userHasRight( $sp->getUser(), 'deletedhistory' ) ) {
450 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
451 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
452 $sp->msg( 'sp-contributions-deleted', $username )->text()
457 # Add a link to change user rights for privileged users
458 $userrightsPage = new UserrightsPage();
459 $userrightsPage->setContext( $sp->getContext() );
460 if ( $userrightsPage->userCanChangeRights( $target ) ) {
461 $tools['userrights'] = $linkRenderer->makeKnownLink(
462 SpecialPage
::getTitleFor( 'Userrights', $username ),
463 $sp->msg( 'sp-contributions-userrights', $username )->text()
467 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
473 * Generates the namespace selector form with hidden attributes.
474 * @param array $pagerOptions with keys contribs, user, deletedOnly, limit, target, topOnly,
475 * newOnly, hideMinor, namespace, associated, nsInvert, tagfilter, year, start, end
476 * @return string HTML fragment
478 protected function getForm( array $pagerOptions ) {
479 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
480 // Modules required only for the form
481 $this->getOutput()->addModules( [
482 'mediawiki.userSuggest',
483 'mediawiki.special.contributions',
485 $this->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
486 $this->getOutput()->enableOOUI();
489 # Add hidden params for tracking except for parameters in $skipParameters
506 foreach ( $this->opts
as $name => $value ) {
507 if ( in_array( $name, $skipParameters ) ) {
518 $target = $this->opts
['target'] ??
null;
519 $fields['target'] = [
521 'cssclass' => 'mw-autocomplete-user mw-ui-input-inline mw-input',
522 'default' => $target ?
523 str_replace( '_', ' ', $target ) : '' ,
524 'label' => $this->msg( 'sp-contributions-username' )->text(),
526 'id' => 'mw-target-user-or-ip',
528 'autofocus' => !$target,
529 'section' => 'contribs-top',
532 $ns = $this->opts
['namespace'] ??
null;
533 $fields['namespace'] = [
534 'type' => 'namespaceselect',
535 'label' => $this->msg( 'namespace' )->text(),
536 'name' => 'namespace',
537 'cssclass' => 'namespaceselector',
540 'section' => 'contribs-top',
542 $request = $this->getRequest();
543 $nsFilters = $request->getArray( 'wpfilters' );
544 $fields['nsFilters'] = [
545 'class' => 'HTMLMultiSelectField',
547 'name' => 'wpfilters',
549 // Only shown when namespaces are selected.
550 'cssclass' => $ns === '' ?
551 'contribs-ns-filters mw-input-with-label mw-input-hidden' :
552 'contribs-ns-filters mw-input-with-label',
553 // `contribs-ns-filters` class allows these fields to be toggled on/off by JavaScript.
554 // See resources/src/mediawiki.special.recentchanges.js
557 $this->msg( 'invert' )->text() => 'nsInvert',
558 $this->msg( 'namespace_association' )->text() => 'associated',
560 'default' => $nsFilters,
561 'section' => 'contribs-top',
563 $fields['tagfilter'] = [
564 'type' => 'tagfilter',
565 'cssclass' => 'mw-tagfilter-input',
567 'label-message' => [ 'tag-filter', 'parse' ],
568 'name' => 'tagfilter',
570 'section' => 'contribs-top',
573 if ( MediaWikiServices
::getInstance()
574 ->getPermissionManager()
575 ->userHasRight( $this->getUser(), 'deletedhistory' )
577 $fields['deletedOnly'] = [
579 'id' => 'mw-show-deleted-only',
580 'label' => $this->msg( 'history-show-deleted' )->text(),
581 'name' => 'deletedOnly',
582 'section' => 'contribs-top',
586 $fields['topOnly'] = [
588 'id' => 'mw-show-top-only',
589 'label' => $this->msg( 'sp-contributions-toponly' )->text(),
591 'section' => 'contribs-top',
593 $fields['newOnly'] = [
595 'id' => 'mw-show-new-only',
596 'label' => $this->msg( 'sp-contributions-newonly' )->text(),
598 'section' => 'contribs-top',
600 $fields['hideMinor'] = [
602 'cssclass' => 'mw-hide-minor-edits',
603 'id' => 'mw-show-new-only',
604 'label' => $this->msg( 'sp-contributions-hideminor' )->text(),
605 'name' => 'hideMinor',
606 'section' => 'contribs-top',
609 // Allow additions at this point to the filters.
612 'SpecialContributions::getForm::filters',
613 [ $this, &$rawFilters ]
615 foreach ( $rawFilters as $filter ) {
616 // Backwards compatibility support for previous hook function signature.
617 if ( is_string( $filter ) ) {
620 'default' => $filter,
622 'section' => 'contribs-top',
626 ' returning string[]',
630 // Preferred append method.
638 'id' => 'mw-date-start',
639 'label' => $this->msg( 'date-range-from' )->text(),
641 'section' => 'contribs-date',
646 'id' => 'mw-date-end',
647 'label' => $this->msg( 'date-range-to' )->text(),
649 'section' => 'contribs-date',
652 $htmlForm = HTMLForm
::factory( 'ooui', $fields, $this->getContext() );
655 // When offset is defined, the user is paging through results
656 // so we hide the form by default to allow users to focus on browsing
657 // rather than defining search parameters
658 ->setCollapsibleOptions(
659 ( $pagerOptions['target'] ??
null ) ||
660 ( $pagerOptions['start'] ??
null ) ||
661 ( $pagerOptions['end'] ??
null )
663 ->setAction( wfScript() )
664 ->setSubmitText( $this->msg( 'sp-contributions-submit' )->text() )
665 ->setWrapperLegend( $this->msg( 'sp-contributions-search' )->text() );
667 $explain = $this->msg( 'sp-contributions-explain' );
668 if ( !$explain->isBlank() ) {
669 $htmlForm->addFooterText( "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>" );
672 $htmlForm->loadData();
674 return $htmlForm->getHTML( false );
678 * Return an array of subpages beginning with $search that this special page will accept.
680 * @param string $search Prefix to search for
681 * @param int $limit Maximum number of results to return (usually 10)
682 * @param int $offset Number of results to skip (usually 0)
683 * @return string[] Matching subpages
685 public function prefixSearchSubpages( $search, $limit, $offset ) {
686 $user = User
::newFromName( $search );
688 // No prefix suggestion for invalid user
691 // Autocomplete subpage as user list - public to allow caching
692 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
695 protected function getGroupName() {