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 'mediawiki.interface.helpers.styles',
47 'mediawiki.special.changeslist',
49 $out->addModules( 'mediawiki.special.recentchanges' );
50 $this->addHelpLink( 'Help:User contributions' );
53 $request = $this->getRequest();
55 $target = $par ??
$request->getVal( 'target' );
57 $this->opts
['deletedOnly'] = $request->getBool( 'deletedOnly' );
59 if ( !strlen( $target ) ) {
60 if ( !$this->including() ) {
61 $out->addHTML( $this->getForm() );
67 $user = $this->getUser();
69 $this->opts
['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
70 $this->opts
['target'] = $target;
71 $this->opts
['topOnly'] = $request->getBool( 'topOnly' );
72 $this->opts
['newOnly'] = $request->getBool( 'newOnly' );
73 $this->opts
['hideMinor'] = $request->getBool( 'hideMinor' );
76 if ( ExternalUserNames
::isExternal( $target ) ) {
77 $userObj = User
::newFromName( $target, false );
79 $out->addHTML( $this->getForm() );
83 $out->addSubtitle( $this->contributionsSub( $userObj ) );
84 $out->setHTMLTitle( $this->msg(
86 $this->msg( 'contributions-title', $target )->plain()
87 )->inContentLanguage() );
89 $nt = Title
::makeTitleSafe( NS_USER
, $target );
91 $out->addHTML( $this->getForm() );
94 $userObj = User
::newFromName( $nt->getText(), false );
96 $out->addHTML( $this->getForm() );
99 $id = $userObj->getId();
101 $target = $nt->getText();
102 $out->addSubtitle( $this->contributionsSub( $userObj ) );
103 $out->setHTMLTitle( $this->msg(
105 $this->msg( 'contributions-title', $target )->plain()
106 )->inContentLanguage() );
108 # For IP ranges, we want the contributionsSub, but not the skin-dependent
109 # links under 'Tools', which may include irrelevant links like 'Logs'.
110 if ( !IP
::isValidRange( $target ) ) {
111 $this->getSkin()->setRelevantUser( $userObj );
115 $ns = $request->getVal( 'namespace', null );
116 if ( $ns !== null && $ns !== '' && $ns !== 'all' ) {
117 $this->opts
['namespace'] = intval( $ns );
119 $this->opts
['namespace'] = '';
122 // Backwards compatibility: Before using OOUI form the old HTML form had
123 // fields for nsInvert and associated. These have now been replaced with the
124 // wpFilters query string parameters. These are retained to keep old URIs working.
125 $this->opts
['associated'] = $request->getBool( 'associated' );
126 $this->opts
['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
127 $nsFilters = $request->getArray( 'wpfilters', null );
128 if ( $nsFilters !== null ) {
129 $this->opts
['associated'] = in_array( 'associated', $nsFilters );
130 $this->opts
['nsInvert'] = in_array( 'nsInvert', $nsFilters );
133 $this->opts
['tagfilter'] = (string)$request->getVal( 'tagfilter' );
135 // Allows reverts to have the bot flag in recent changes. It is just here to
136 // be passed in the form at the top of the page
137 if ( MediaWikiServices
::getInstance()
138 ->getPermissionManager()
139 ->userHasRight( $user, 'markbotedits' ) && $request->getBool( 'bot' )
141 $this->opts
['bot'] = '1';
144 $skip = $request->getText( 'offset' ) ||
$request->getText( 'dir' ) == 'prev';
145 # Offset overrides year/month selection
147 $this->opts
['year'] = $request->getVal( 'year' );
148 $this->opts
['month'] = $request->getVal( 'month' );
150 $this->opts
['start'] = $request->getVal( 'start' );
151 $this->opts
['end'] = $request->getVal( 'end' );
153 $this->opts
= ContribsPager
::processDateFilter( $this->opts
);
155 if ( $this->opts
['namespace'] < NS_MAIN
) {
156 $this->getOutput()->wrapWikiMsg(
157 "<div class=\"mw-negative-namespace-not-supported error\">\n\$1\n</div>",
158 [ 'negative-namespace-not-supported' ]
160 $out->addHTML( $this->getForm() );
164 $feedType = $request->getVal( 'feed' );
167 'action' => 'feedcontributions',
170 if ( $this->opts
['topOnly'] ) {
171 $feedParams['toponly'] = true;
173 if ( $this->opts
['newOnly'] ) {
174 $feedParams['newonly'] = true;
176 if ( $this->opts
['hideMinor'] ) {
177 $feedParams['hideminor'] = true;
179 if ( $this->opts
['deletedOnly'] ) {
180 $feedParams['deletedonly'] = true;
182 if ( $this->opts
['tagfilter'] !== '' ) {
183 $feedParams['tagfilter'] = $this->opts
['tagfilter'];
185 if ( $this->opts
['namespace'] !== '' ) {
186 $feedParams['namespace'] = $this->opts
['namespace'];
188 // Don't use year and month for the feed URL, but pass them on if
189 // we redirect to API (if $feedType is specified)
190 if ( $feedType && $this->opts
['year'] !== null ) {
191 $feedParams['year'] = $this->opts
['year'];
193 if ( $feedType && $this->opts
['month'] !== null ) {
194 $feedParams['month'] = $this->opts
['month'];
198 // Maintain some level of backwards compatibility
199 // If people request feeds using the old parameters, redirect to API
200 $feedParams['feedformat'] = $feedType;
201 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
203 $out->redirect( $url, '301' );
208 // Add RSS/atom links
209 $this->addFeedLinks( $feedParams );
211 if ( Hooks
::run( 'SpecialContributionsBeforeMainOutput', [ $id, $userObj, $this ] ) ) {
212 if ( !$this->including() ) {
213 $out->addHTML( $this->getForm() );
215 $pager = new ContribsPager( $this->getContext(), [
217 'namespace' => $this->opts
['namespace'],
218 'tagfilter' => $this->opts
['tagfilter'],
219 'start' => $this->opts
['start'],
220 'end' => $this->opts
['end'],
221 'deletedOnly' => $this->opts
['deletedOnly'],
222 'topOnly' => $this->opts
['topOnly'],
223 'newOnly' => $this->opts
['newOnly'],
224 'hideMinor' => $this->opts
['hideMinor'],
225 'nsInvert' => $this->opts
['nsInvert'],
226 'associated' => $this->opts
['associated'],
227 ], $this->getLinkRenderer() );
229 if ( IP
::isValidRange( $target ) && !$pager->isQueryableRange( $target ) ) {
230 // Valid range, but outside CIDR limit.
231 $limits = $this->getConfig()->get( 'RangeContributionsCIDRLimit' );
232 $limit = $limits[ IP
::isIPv4( $target ) ?
'IPv4' : 'IPv6' ];
233 $out->addWikiMsg( 'sp-contributions-outofrange', $limit );
234 } elseif ( !$pager->getNumRows() ) {
235 $out->addWikiMsg( 'nocontribs', $target );
237 # Show a message about replica DB lag, if applicable
238 $lag = $pager->getDatabase()->getSessionLagStatus()['lag'];
240 $out->showLagWarning( $lag );
243 $output = $pager->getBody();
244 if ( !$this->including() ) {
245 $output = $pager->getNavigationBar() .
247 $pager->getNavigationBar();
249 $out->addHTML( $output );
252 $out->preventClickjacking( $pager->getPreventClickjacking() );
254 # Show the appropriate "footer" message - WHOIS tools, etc.
255 if ( IP
::isValidRange( $target ) ) {
256 $message = 'sp-contributions-footer-anon-range';
257 } elseif ( IP
::isIPAddress( $target ) ) {
258 $message = 'sp-contributions-footer-anon';
259 } elseif ( $userObj->isAnon() ) {
260 // No message for non-existing users
263 $message = 'sp-contributions-footer';
266 if ( $message && !$this->including() && !$this->msg( $message, $target )->isDisabled() ) {
268 "<div class='mw-contributions-footer'>\n$1\n</div>",
269 [ $message, $target ] );
275 * Generates the subheading with links
276 * @param User $userObj User object for the target
277 * @return string Appropriately-escaped HTML to be output literally
278 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
281 protected function contributionsSub( $userObj ) {
282 if ( $userObj->isAnon() ) {
283 // Show a warning message that the user being searched for doesn't exist.
284 // User::isIP returns true for IP address and usemod IPs like '123.123.123.xxx',
285 // but returns false for IP ranges. We don't want to suggest either of these are
286 // valid usernames which we would with the 'contributions-userdoesnotexist' message.
287 if ( !User
::isIP( $userObj->getName() ) && !$userObj->isIPRange() ) {
288 $this->getOutput()->wrapWikiMsg(
289 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
291 'contributions-userdoesnotexist',
292 wfEscapeWikiText( $userObj->getName() ),
295 if ( !$this->including() ) {
296 $this->getOutput()->setStatusCode( 404 );
299 $user = htmlspecialchars( $userObj->getName() );
301 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
303 $nt = $userObj->getUserPage();
304 $talk = $userObj->getTalkPage();
307 $tools = self
::getUserLinks( $this, $userObj );
308 $links = Html
::openElement( 'span', [ 'class' => 'mw-changeslist-links' ] );
309 foreach ( $tools as $tool ) {
310 $links .= Html
::rawElement( 'span', [], $tool ) . ' ';
312 $links = trim( $links ) . Html
::closeElement( 'span' );
314 // Show a note if the user is blocked and display the last block log entry.
315 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
316 // and also this will display a totally irrelevant log entry as a current block.
317 if ( !$this->including() ) {
318 // For IP ranges you must give DatabaseBlock::newFromTarget the CIDR string
319 // and not a user object.
320 if ( $userObj->isIPRange() ) {
321 $block = DatabaseBlock
::newFromTarget( $userObj->getName(), $userObj->getName() );
323 $block = DatabaseBlock
::newFromTarget( $userObj, $userObj );
326 if ( !is_null( $block ) && $block->getType() != DatabaseBlock
::TYPE_AUTO
) {
327 if ( $block->getType() == DatabaseBlock
::TYPE_RANGE
) {
328 $nt = MediaWikiServices
::getInstance()->getNamespaceInfo()->
329 getCanonicalName( NS_USER
) . ':' . $block->getTarget();
332 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
333 LogEventsList
::showLogExtract(
340 'showIfEmpty' => false,
343 'sp-contributions-blocked-notice-anon' :
344 'sp-contributions-blocked-notice',
345 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
347 'offset' => '' # don't use WebRequest parameter offset
354 return Html
::rawElement( 'div', [ 'class' => 'mw-contributions-user-tools' ],
355 $this->msg( 'contributions-subtitle' )->rawParams( $user )->params( $userObj->getName() )
361 * Links to different places.
363 * @note This function is also called in DeletedContributionsPage
364 * @param SpecialPage $sp SpecialPage instance, for context
365 * @param User $target Target user object
368 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
369 $id = $target->getId();
370 $username = $target->getName();
371 $userpage = $target->getUserPage();
372 $talkpage = $target->getTalkPage();
373 $isIP = IP
::isValid( $username );
374 $isRange = IP
::isValidRange( $username );
376 $linkRenderer = $sp->getLinkRenderer();
379 # No talk pages for IP ranges.
381 $tools['user-talk'] = $linkRenderer->makeLink(
383 $sp->msg( 'sp-contributions-talk' )->text()
387 # Block / Change block / Unblock links
388 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
389 if ( $permissionManager->userHasRight( $sp->getUser(), 'block' ) ) {
390 if ( $target->getBlock() && $target->getBlock()->getType() != DatabaseBlock
::TYPE_AUTO
) {
391 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
392 SpecialPage
::getTitleFor( 'Block', $username ),
393 $sp->msg( 'change-blocklink' )->text()
395 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
396 SpecialPage
::getTitleFor( 'Unblock', $username ),
397 $sp->msg( 'unblocklink' )->text()
399 } else { # User is not blocked
400 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
401 SpecialPage
::getTitleFor( 'Block', $username ),
402 $sp->msg( 'blocklink' )->text()
408 $tools['log-block'] = $linkRenderer->makeKnownLink(
409 SpecialPage
::getTitleFor( 'Log', 'block' ),
410 $sp->msg( 'sp-contributions-blocklog' )->text(),
412 [ 'page' => $userpage->getPrefixedText() ]
415 # Suppression log link (T61120)
416 if ( $permissionManager->userHasRight( $sp->getUser(), 'suppressionlog' ) ) {
417 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
418 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
419 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
421 [ 'offender' => $username ]
425 # Don't show some links for IP ranges
427 # Uploads: hide if IPs cannot upload (T220674)
428 if ( !$isIP ||
$permissionManager->userHasRight( $target, 'upload' ) ) {
429 $tools['uploads'] = $linkRenderer->makeKnownLink(
430 SpecialPage
::getTitleFor( 'Listfiles', $username ),
431 $sp->msg( 'sp-contributions-uploads' )->text()
437 $tools['logs'] = $linkRenderer->makeKnownLink(
438 SpecialPage
::getTitleFor( 'Log', $username ),
439 $sp->msg( 'sp-contributions-logs' )->text()
442 # Add link to deleted user contributions for priviledged users
444 if ( $permissionManager->userHasRight( $sp->getUser(), 'deletedhistory' ) ) {
445 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
446 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
447 $sp->msg( 'sp-contributions-deleted', $username )->text()
452 # Add a link to change user rights for privileged users
453 $userrightsPage = new UserrightsPage();
454 $userrightsPage->setContext( $sp->getContext() );
455 if ( $userrightsPage->userCanChangeRights( $target ) ) {
456 $tools['userrights'] = $linkRenderer->makeKnownLink(
457 SpecialPage
::getTitleFor( 'Userrights', $username ),
458 $sp->msg( 'sp-contributions-userrights', $username )->text()
462 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
468 * Generates the namespace selector form with hidden attributes.
469 * @return string HTML fragment
471 protected function getForm() {
472 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
473 // Modules required only for the form
474 $this->getOutput()->addModules( [
475 'mediawiki.userSuggest',
476 'mediawiki.special.contributions',
478 $this->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
479 $this->getOutput()->enableOOUI();
482 # Add hidden params for tracking except for parameters in $skipParameters
499 foreach ( $this->opts
as $name => $value ) {
500 if ( in_array( $name, $skipParameters ) ) {
511 $target = $this->opts
['target'] ??
null;
512 $fields['target'] = [
514 'cssclass' => 'mw-autocomplete-user mw-ui-input-inline mw-input',
515 'default' => $target ?
516 str_replace( '_', ' ', $target ) : '' ,
517 'label' => $this->msg( 'sp-contributions-username' )->text(),
519 'id' => 'mw-target-user-or-ip',
521 'autofocus' => !$target,
522 'section' => 'contribs-top',
525 $ns = $this->opts
['namespace'] ??
null;
526 $fields['namespace'] = [
527 'type' => 'namespaceselect',
528 'label' => $this->msg( 'namespace' )->text(),
529 'name' => 'namespace',
530 'cssclass' => 'namespaceselector',
533 'section' => 'contribs-top',
535 $request = $this->getRequest();
536 $nsFilters = $request->getArray( 'wpfilters' );
537 $fields['nsFilters'] = [
538 'class' => 'HTMLMultiSelectField',
540 'name' => 'wpfilters',
542 // Only shown when namespaces are selected.
543 'cssclass' => $ns === '' ?
544 'contribs-ns-filters mw-input-with-label mw-input-hidden' :
545 'contribs-ns-filters mw-input-with-label',
546 // `contribs-ns-filters` class allows these fields to be toggled on/off by JavaScript.
547 // See resources/src/mediawiki.special.recentchanges.js
550 $this->msg( 'invert' )->text() => 'nsInvert',
551 $this->msg( 'namespace_association' )->text() => 'associated',
553 'default' => $nsFilters,
554 'section' => 'contribs-top',
556 $fields['tagfilter'] = [
557 'type' => 'tagfilter',
558 'cssclass' => 'mw-tagfilter-input',
560 'label-message' => [ 'tag-filter', 'parse' ],
561 'name' => 'tagfilter',
563 'section' => 'contribs-top',
566 if ( MediaWikiServices
::getInstance()
567 ->getPermissionManager()
568 ->userHasRight( $this->getUser(), 'deletedhistory' )
570 $fields['deletedOnly'] = [
572 'id' => 'mw-show-deleted-only',
573 'label' => $this->msg( 'history-show-deleted' )->text(),
574 'name' => 'deletedOnly',
575 'section' => 'contribs-top',
579 $fields['topOnly'] = [
581 'id' => 'mw-show-top-only',
582 'label' => $this->msg( 'sp-contributions-toponly' )->text(),
584 'section' => 'contribs-top',
586 $fields['newOnly'] = [
588 'id' => 'mw-show-new-only',
589 'label' => $this->msg( 'sp-contributions-newonly' )->text(),
591 'section' => 'contribs-top',
593 $fields['hideMinor'] = [
595 'cssclass' => 'mw-hide-minor-edits',
596 'id' => 'mw-show-new-only',
597 'label' => $this->msg( 'sp-contributions-hideminor' )->text(),
598 'name' => 'hideMinor',
599 'section' => 'contribs-top',
602 // Allow additions at this point to the filters.
605 'SpecialContributions::getForm::filters',
606 [ $this, &$rawFilters ]
608 foreach ( $rawFilters as $filter ) {
609 // Backwards compatibility support for previous hook function signature.
610 if ( is_string( $filter ) ) {
613 'default' => $filter,
615 'section' => 'contribs-top',
619 ' returning string[]',
623 // Preferred append method.
631 'id' => 'mw-date-start',
632 'label' => $this->msg( 'date-range-from' )->text(),
634 'section' => 'contribs-date',
639 'id' => 'mw-date-end',
640 'label' => $this->msg( 'date-range-to' )->text(),
642 'section' => 'contribs-date',
645 $htmlForm = HTMLForm
::factory( 'ooui', $fields, $this->getContext() );
648 ->setAction( wfScript() )
649 ->setSubmitText( $this->msg( 'sp-contributions-submit' )->text() )
650 ->setWrapperLegend( $this->msg( 'sp-contributions-search' )->text() );
652 $explain = $this->msg( 'sp-contributions-explain' );
653 if ( !$explain->isBlank() ) {
654 $htmlForm->addFooterText( "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>" );
657 $htmlForm->loadData();
659 return $htmlForm->getHTML( false );
663 * Return an array of subpages beginning with $search that this special page will accept.
665 * @param string $search Prefix to search for
666 * @param int $limit Maximum number of results to return (usually 10)
667 * @param int $offset Number of results to skip (usually 0)
668 * @return string[] Matching subpages
670 public function prefixSearchSubpages( $search, $limit, $offset ) {
671 $user = User
::newFromName( $search );
673 // No prefix suggestion for invalid user
676 // Autocomplete subpage as user list - public to allow caching
677 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
680 protected function getGroupName() {