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\MediaWikiServices
;
25 use MediaWiki\Widget\DateInputWidget
;
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( [
46 'mediawiki.special.changeslist',
48 $this->addHelpLink( 'Help:User contributions' );
51 $request = $this->getRequest();
53 if ( $par !== null ) {
56 $target = $request->getVal( 'target' );
59 if ( $request->getVal( 'contribs' ) == 'newbie' ||
$par === 'newbies' ) {
61 $this->opts
['contribs'] = 'newbie';
63 $this->opts
['contribs'] = 'user';
66 $this->opts
['deletedOnly'] = $request->getBool( 'deletedOnly' );
68 if ( !strlen( $target ) ) {
69 if ( !$this->including() ) {
70 $out->addHTML( $this->getForm() );
76 $user = $this->getUser();
78 $this->opts
['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
79 $this->opts
['target'] = $target;
80 $this->opts
['topOnly'] = $request->getBool( 'topOnly' );
81 $this->opts
['newOnly'] = $request->getBool( 'newOnly' );
82 $this->opts
['hideMinor'] = $request->getBool( 'hideMinor' );
85 if ( $this->opts
['contribs'] === 'newbie' ) {
86 $userObj = User
::newFromName( $target ); // hysterical raisins
87 $out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub' ) );
88 $out->setHTMLTitle( $this->msg(
90 $this->msg( 'sp-contributions-newbies-title' )->plain()
91 )->inContentLanguage() );
92 } elseif ( ExternalUserNames
::isExternal( $target ) ) {
93 $userObj = User
::newFromName( $target, false );
95 $out->addHTML( $this->getForm() );
99 $out->addSubtitle( $this->contributionsSub( $userObj ) );
100 $out->setHTMLTitle( $this->msg(
102 $this->msg( 'contributions-title', $target )->plain()
103 )->inContentLanguage() );
105 $nt = Title
::makeTitleSafe( NS_USER
, $target );
107 $out->addHTML( $this->getForm() );
110 $userObj = User
::newFromName( $nt->getText(), false );
112 $out->addHTML( $this->getForm() );
115 $id = $userObj->getId();
117 $target = $nt->getText();
118 $out->addSubtitle( $this->contributionsSub( $userObj ) );
119 $out->setHTMLTitle( $this->msg(
121 $this->msg( 'contributions-title', $target )->plain()
122 )->inContentLanguage() );
124 # For IP ranges, we want the contributionsSub, but not the skin-dependent
125 # links under 'Tools', which may include irrelevant links like 'Logs'.
126 if ( !IP
::isValidRange( $target ) ) {
127 $this->getSkin()->setRelevantUser( $userObj );
131 $ns = $request->getVal( 'namespace', null );
132 if ( $ns !== null && $ns !== '' ) {
133 $this->opts
['namespace'] = intval( $ns );
135 $this->opts
['namespace'] = '';
138 $this->opts
['associated'] = $request->getBool( 'associated' );
139 $this->opts
['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
140 $this->opts
['tagfilter'] = (string)$request->getVal( 'tagfilter' );
142 // Allows reverts to have the bot flag in recent changes. It is just here to
143 // be passed in the form at the top of the page
144 if ( $user->isAllowed( 'markbotedits' ) && $request->getBool( 'bot' ) ) {
145 $this->opts
['bot'] = '1';
148 $skip = $request->getText( 'offset' ) ||
$request->getText( 'dir' ) == 'prev';
149 # Offset overrides year/month selection
151 $this->opts
['year'] = $request->getVal( 'year' );
152 $this->opts
['month'] = $request->getVal( 'month' );
154 $this->opts
['start'] = $request->getVal( 'start' );
155 $this->opts
['end'] = $request->getVal( 'end' );
157 $this->opts
= ContribsPager
::processDateFilter( $this->opts
);
159 $feedType = $request->getVal( 'feed' );
162 'action' => 'feedcontributions',
165 if ( $this->opts
['topOnly'] ) {
166 $feedParams['toponly'] = true;
168 if ( $this->opts
['newOnly'] ) {
169 $feedParams['newonly'] = true;
171 if ( $this->opts
['hideMinor'] ) {
172 $feedParams['hideminor'] = true;
174 if ( $this->opts
['deletedOnly'] ) {
175 $feedParams['deletedonly'] = true;
177 if ( $this->opts
['tagfilter'] !== '' ) {
178 $feedParams['tagfilter'] = $this->opts
['tagfilter'];
180 if ( $this->opts
['namespace'] !== '' ) {
181 $feedParams['namespace'] = $this->opts
['namespace'];
183 // Don't use year and month for the feed URL, but pass them on if
184 // we redirect to API (if $feedType is specified)
185 if ( $feedType && $this->opts
['year'] !== null ) {
186 $feedParams['year'] = $this->opts
['year'];
188 if ( $feedType && $this->opts
['month'] !== null ) {
189 $feedParams['month'] = $this->opts
['month'];
193 // Maintain some level of backwards compatibility
194 // If people request feeds using the old parameters, redirect to API
195 $feedParams['feedformat'] = $feedType;
196 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
198 $out->redirect( $url, '301' );
203 // Add RSS/atom links
204 $this->addFeedLinks( $feedParams );
206 if ( Hooks
::run( 'SpecialContributionsBeforeMainOutput', [ $id, $userObj, $this ] ) ) {
207 if ( !$this->including() ) {
208 $out->addHTML( $this->getForm() );
210 $pager = new ContribsPager( $this->getContext(), [
212 'contribs' => $this->opts
['contribs'],
213 'namespace' => $this->opts
['namespace'],
214 'tagfilter' => $this->opts
['tagfilter'],
215 'start' => $this->opts
['start'],
216 'end' => $this->opts
['end'],
217 'deletedOnly' => $this->opts
['deletedOnly'],
218 'topOnly' => $this->opts
['topOnly'],
219 'newOnly' => $this->opts
['newOnly'],
220 'hideMinor' => $this->opts
['hideMinor'],
221 'nsInvert' => $this->opts
['nsInvert'],
222 'associated' => $this->opts
['associated'],
225 if ( IP
::isValidRange( $target ) && !$pager->isQueryableRange( $target ) ) {
226 // Valid range, but outside CIDR limit.
227 $limits = $this->getConfig()->get( 'RangeContributionsCIDRLimit' );
228 $limit = $limits[ IP
::isIPv4( $target ) ?
'IPv4' : 'IPv6' ];
229 $out->addWikiMsg( 'sp-contributions-outofrange', $limit );
230 } elseif ( !$pager->getNumRows() ) {
231 $out->addWikiMsg( 'nocontribs', $target );
233 // @todo We just want a wiki ID here, not a "DB domain", but
234 // current status of MediaWiki conflates the two. See T235955.
235 $poolKey = WikiMap
::getCurrentWikiDbDomain() . ':SpecialContributions:';
236 if ( $this->getUser()->isAnon() ) {
237 $poolKey .= 'a:' . $this->getUser()->getName();
239 $poolKey .= 'u:' . $this->getUser()->getId();
241 $work = new PoolCounterWorkViaCallback( 'SpecialContributions', $poolKey, [
242 'doWork' => function () use ( $pager, $out ) {
243 # Show a message about replica DB lag, if applicable
244 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
245 $lag = $lb->safeGetLag( $pager->getDatabase() );
247 $out->showLagWarning( $lag );
250 $output = $pager->getBody();
251 if ( !$this->including() ) {
252 $output = '<p>' . $pager->getNavigationBar() . '</p>' .
254 '<p>' . $pager->getNavigationBar() . '</p>';
256 $out->addHTML( $output );
258 'error' => function () use ( $out ) {
259 $msg = $this->getUser()->isAnon()
260 ?
'sp-contributions-concurrency-ip'
261 : 'sp-contributions-concurrency-user';
262 $out->wrapWikiMsg( "<div class='errorbox'>\n$1\n</div>", $msg );
268 $out->preventClickjacking( $pager->getPreventClickjacking() );
270 # Show the appropriate "footer" message - WHOIS tools, etc.
271 if ( $this->opts
['contribs'] == 'newbie' ) {
272 $message = 'sp-contributions-footer-newbies';
273 } elseif ( IP
::isValidRange( $target ) ) {
274 $message = 'sp-contributions-footer-anon-range';
275 } elseif ( IP
::isIPAddress( $target ) ) {
276 $message = 'sp-contributions-footer-anon';
277 } elseif ( $userObj->isAnon() ) {
278 // No message for non-existing users
281 $message = 'sp-contributions-footer';
285 if ( !$this->including() ) {
286 if ( !$this->msg( $message, $target )->isDisabled() ) {
288 "<div class='mw-contributions-footer'>\n$1\n</div>",
289 [ $message, $target ] );
297 * Generates the subheading with links
298 * @param User $userObj User object for the target
299 * @return string Appropriately-escaped HTML to be output literally
300 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
303 protected function contributionsSub( $userObj ) {
304 if ( $userObj->isAnon() ) {
305 // Show a warning message that the user being searched for doesn't exists.
306 // User::isIP returns true for IP address and usemod IPs like '123.123.123.xxx',
307 // but returns false for IP ranges. We don't want to suggest either of these are
308 // valid usernames which we would with the 'contributions-userdoesnotexist' message.
309 if ( !User
::isIP( $userObj->getName() ) && !$userObj->isIPRange() ) {
310 $this->getOutput()->wrapWikiMsg(
311 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
313 'contributions-userdoesnotexist',
314 wfEscapeWikiText( $userObj->getName() ),
317 if ( !$this->including() ) {
318 $this->getOutput()->setStatusCode( 404 );
321 $user = htmlspecialchars( $userObj->getName() );
323 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
325 $nt = $userObj->getUserPage();
326 $talk = $userObj->getTalkPage();
329 $tools = self
::getUserLinks( $this, $userObj );
330 $links = $this->getLanguage()->pipeList( $tools );
332 // Show a note if the user is blocked and display the last block log entry.
333 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
334 // and also this will display a totally irrelevant log entry as a current block.
335 if ( !$this->including() ) {
336 // For IP ranges you must give Block::newFromTarget the CIDR string and not a user object.
337 if ( $userObj->isIPRange() ) {
338 $block = Block
::newFromTarget( $userObj->getName(), $userObj->getName() );
340 $block = Block
::newFromTarget( $userObj, $userObj );
343 if ( !is_null( $block ) && $block->getType() != Block
::TYPE_AUTO
) {
344 if ( $block->getType() == Block
::TYPE_RANGE
) {
345 $nt = MWNamespace
::getCanonicalName( NS_USER
) . ':' . $block->getTarget();
348 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
349 LogEventsList
::showLogExtract(
356 'showIfEmpty' => false,
359 'sp-contributions-blocked-notice-anon' :
360 'sp-contributions-blocked-notice',
361 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
363 'offset' => '' # don't use WebRequest parameter offset
370 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
374 * Links to different places.
376 * @note This function is also called in DeletedContributionsPage
377 * @param SpecialPage $sp SpecialPage instance, for context
378 * @param User $target Target user object
381 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
382 $id = $target->getId();
383 $username = $target->getName();
384 $userpage = $target->getUserPage();
385 $talkpage = $target->getTalkPage();
387 $linkRenderer = $sp->getLinkRenderer();
389 # No talk pages for IP ranges.
390 if ( !IP
::isValidRange( $username ) ) {
391 $tools['user-talk'] = $linkRenderer->makeLink(
393 $sp->msg( 'sp-contributions-talk' )->text()
397 if ( ( $id !== null ) ||
( $id === null && IP
::isIPAddress( $username ) ) ) {
398 if ( $sp->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
399 if ( $target->isBlocked() && $target->getBlock()->getType() != Block
::TYPE_AUTO
) {
400 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
401 SpecialPage
::getTitleFor( 'Block', $username ),
402 $sp->msg( 'change-blocklink' )->text()
404 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
405 SpecialPage
::getTitleFor( 'Unblock', $username ),
406 $sp->msg( 'unblocklink' )->text()
408 } else { # User is not blocked
409 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
410 SpecialPage
::getTitleFor( 'Block', $username ),
411 $sp->msg( 'blocklink' )->text()
417 $tools['log-block'] = $linkRenderer->makeKnownLink(
418 SpecialPage
::getTitleFor( 'Log', 'block' ),
419 $sp->msg( 'sp-contributions-blocklog' )->text(),
421 [ 'page' => $userpage->getPrefixedText() ]
424 # Suppression log link (T61120)
425 if ( $sp->getUser()->isAllowed( 'suppressionlog' ) ) {
426 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
427 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
428 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
430 [ 'offender' => $username ]
435 # Don't show some links for IP ranges
436 if ( !IP
::isValidRange( $username ) ) {
438 $tools['uploads'] = $linkRenderer->makeKnownLink(
439 SpecialPage
::getTitleFor( 'Listfiles', $username ),
440 $sp->msg( 'sp-contributions-uploads' )->text()
444 $tools['logs'] = $linkRenderer->makeKnownLink(
445 SpecialPage
::getTitleFor( 'Log', $username ),
446 $sp->msg( 'sp-contributions-logs' )->text()
449 # Add link to deleted user contributions for priviledged users
450 if ( $sp->getUser()->isAllowed( 'deletedhistory' ) ) {
451 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
452 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
453 $sp->msg( 'sp-contributions-deleted', $username )->text()
458 # Add a link to change user rights for privileged users
459 $userrightsPage = new UserrightsPage();
460 $userrightsPage->setContext( $sp->getContext() );
461 if ( $userrightsPage->userCanChangeRights( $target ) ) {
462 $tools['userrights'] = $linkRenderer->makeKnownLink(
463 SpecialPage
::getTitleFor( 'Userrights', $username ),
464 $sp->msg( 'sp-contributions-userrights', $username )->text()
468 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
474 * Generates the namespace selector form with hidden attributes.
475 * @return string HTML fragment
477 protected function getForm() {
478 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
479 if ( !isset( $this->opts
['target'] ) ) {
480 $this->opts
['target'] = '';
482 $this->opts
['target'] = str_replace( '_', ' ', $this->opts
['target'] );
485 if ( !isset( $this->opts
['namespace'] ) ) {
486 $this->opts
['namespace'] = '';
489 if ( !isset( $this->opts
['nsInvert'] ) ) {
490 $this->opts
['nsInvert'] = '';
493 if ( !isset( $this->opts
['associated'] ) ) {
494 $this->opts
['associated'] = false;
497 if ( !isset( $this->opts
['contribs'] ) ) {
498 $this->opts
['contribs'] = 'user';
501 if ( !isset( $this->opts
['start'] ) ) {
502 $this->opts
['start'] = '';
505 if ( !isset( $this->opts
['end'] ) ) {
506 $this->opts
['end'] = '';
509 if ( $this->opts
['contribs'] == 'newbie' ) {
510 $this->opts
['target'] = '';
513 if ( !isset( $this->opts
['tagfilter'] ) ) {
514 $this->opts
['tagfilter'] = '';
517 if ( !isset( $this->opts
['topOnly'] ) ) {
518 $this->opts
['topOnly'] = false;
521 if ( !isset( $this->opts
['newOnly'] ) ) {
522 $this->opts
['newOnly'] = false;
525 if ( !isset( $this->opts
['hideMinor'] ) ) {
526 $this->opts
['hideMinor'] = false;
529 // Modules required only for the form
530 $this->getOutput()->addModules( [
531 'mediawiki.userSuggest',
532 'mediawiki.special.contributions',
534 $this->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
535 $this->getOutput()->enableOOUI();
537 $form = Html
::openElement(
541 'action' => wfScript(),
542 'class' => 'mw-contributions-form'
546 # Add hidden params for tracking except for parameters in $skipParameters
564 foreach ( $this->opts
as $name => $value ) {
565 if ( in_array( $name, $skipParameters ) ) {
568 $form .= "\t" . Html
::hidden( $name, $value ) . "\n";
571 $tagFilter = ChangeTags
::buildTagFilterSelector(
572 $this->opts
['tagfilter'], false, $this->getContext() );
575 $filterSelection = Html
::rawElement(
578 implode( ' ', $tagFilter )
581 $filterSelection = Html
::rawElement( 'div', [], '' );
584 $labelNewbies = Xml
::radioLabel(
585 $this->msg( 'sp-contributions-newbies' )->text(),
589 $this->opts
['contribs'] == 'newbie',
590 [ 'class' => 'mw-input' ]
592 $labelUsername = Xml
::radioLabel(
593 $this->msg( 'sp-contributions-username' )->text(),
597 $this->opts
['contribs'] == 'user',
598 [ 'class' => 'mw-input' ]
600 $input = Html
::input(
602 $this->opts
['target'],
608 'mw-ui-input-inline',
609 'mw-autocomplete-user', // used by mediawiki.userSuggest
612 // Only autofocus if target hasn't been specified or in non-newbies mode
613 ( $this->opts
['contribs'] === 'newbie' ||
$this->opts
['target'] )
614 ?
[] : [ 'autofocus' => true ]
618 $targetSelection = Html
::rawElement(
621 $labelNewbies . '<br>' . $labelUsername . ' ' . $input . ' '
624 $namespaceSelection = Xml
::tags(
628 $this->msg( 'namespace' )->text(),
632 Html
::namespaceSelector(
633 [ 'selected' => $this->opts
['namespace'], 'all' => '' ],
635 'name' => 'namespace',
637 'class' => 'namespaceselector',
642 [ 'class' => 'mw-input-with-label' ],
644 $this->msg( 'invert' )->text(),
647 $this->opts
['nsInvert'],
649 'title' => $this->msg( 'tooltip-invert' )->text(),
650 'class' => 'mw-input'
654 Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' ],
656 $this->msg( 'namespace_association' )->text(),
659 $this->opts
['associated'],
661 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
662 'class' => 'mw-input'
670 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
671 $filters[] = Html
::rawElement(
673 [ 'class' => 'mw-input-with-label' ],
675 $this->msg( 'history-show-deleted' )->text(),
677 'mw-show-deleted-only',
678 $this->opts
['deletedOnly'],
679 [ 'class' => 'mw-input' ]
684 $filters[] = Html
::rawElement(
686 [ 'class' => 'mw-input-with-label' ],
688 $this->msg( 'sp-contributions-toponly' )->text(),
691 $this->opts
['topOnly'],
692 [ 'class' => 'mw-input' ]
695 $filters[] = Html
::rawElement(
697 [ 'class' => 'mw-input-with-label' ],
699 $this->msg( 'sp-contributions-newonly' )->text(),
702 $this->opts
['newOnly'],
703 [ 'class' => 'mw-input' ]
706 $filters[] = Html
::rawElement(
708 [ 'class' => 'mw-input-with-label' ],
710 $this->msg( 'sp-contributions-hideminor' )->text(),
712 'mw-hide-minor-edits',
713 $this->opts
['hideMinor'],
714 [ 'class' => 'mw-input' ]
719 'SpecialContributions::getForm::filters',
723 $extraOptions = Html
::rawElement(
726 implode( '', $filters )
729 $dateRangeSelection = Html
::rawElement(
732 Xml
::label( wfMessage( 'date-range-from' )->text(), 'mw-date-start' ) . ' ' .
733 new DateInputWidget( [
735 'id' => 'mw-date-start',
737 'value' => $this->opts
['start'],
738 'longDisplayFormat' => true,
740 Xml
::label( wfMessage( 'date-range-to' )->text(), 'mw-date-end' ) . ' ' .
741 new DateInputWidget( [
743 'id' => 'mw-date-end',
745 'value' => $this->opts
['end'],
746 'longDisplayFormat' => true,
750 $submit = Xml
::tags( 'div', [],
752 $this->msg( 'sp-contributions-submit' )->text(),
753 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
757 $form .= Xml
::fieldset(
758 $this->msg( 'sp-contributions-search' )->text(),
760 $namespaceSelection .
763 $dateRangeSelection .
765 [ 'class' => 'mw-contributions-table' ]
768 $explain = $this->msg( 'sp-contributions-explain' );
769 if ( !$explain->isBlank() ) {
770 $form .= "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>";
773 $form .= Xml
::closeElement( 'form' );
779 * Return an array of subpages beginning with $search that this special page will accept.
781 * @param string $search Prefix to search for
782 * @param int $limit Maximum number of results to return (usually 10)
783 * @param int $offset Number of results to skip (usually 0)
784 * @return string[] Matching subpages
786 public function prefixSearchSubpages( $search, $limit, $offset ) {
787 $user = User
::newFromName( $search );
789 // No prefix suggestion for invalid user
792 // Autocomplete subpage as user list - public to allow caching
793 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
796 protected function getGroupName() {