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 $target = $par ??
$request->getVal( 'target' );
55 if ( $request->getVal( 'contribs' ) == 'newbie' ||
$par === 'newbies' ) {
57 $this->opts
['contribs'] = 'newbie';
59 $this->opts
['contribs'] = 'user';
62 $this->opts
['deletedOnly'] = $request->getBool( 'deletedOnly' );
64 if ( !strlen( $target ) ) {
65 if ( !$this->including() ) {
66 $out->addHTML( $this->getForm() );
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 ( $this->opts
['contribs'] === 'newbie' ) {
82 $userObj = User
::newFromName( $target ); // hysterical raisins
83 $out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub' ) );
84 $out->setHTMLTitle( $this->msg(
86 $this->msg( 'sp-contributions-newbies-title' )->plain()
87 )->inContentLanguage() );
88 } elseif ( ExternalUserNames
::isExternal( $target ) ) {
89 $userObj = User
::newFromName( $target, false );
91 $out->addHTML( $this->getForm() );
95 $out->addSubtitle( $this->contributionsSub( $userObj ) );
96 $out->setHTMLTitle( $this->msg(
98 $this->msg( 'contributions-title', $target )->plain()
99 )->inContentLanguage() );
101 $nt = Title
::makeTitleSafe( NS_USER
, $target );
103 $out->addHTML( $this->getForm() );
106 $userObj = User
::newFromName( $nt->getText(), false );
108 $out->addHTML( $this->getForm() );
111 $id = $userObj->getId();
113 $target = $nt->getText();
114 $out->addSubtitle( $this->contributionsSub( $userObj ) );
115 $out->setHTMLTitle( $this->msg(
117 $this->msg( 'contributions-title', $target )->plain()
118 )->inContentLanguage() );
120 # For IP ranges, we want the contributionsSub, but not the skin-dependent
121 # links under 'Tools', which may include irrelevant links like 'Logs'.
122 if ( !IP
::isValidRange( $target ) ) {
123 $this->getSkin()->setRelevantUser( $userObj );
127 $ns = $request->getVal( 'namespace', null );
128 if ( $ns !== null && $ns !== '' ) {
129 $this->opts
['namespace'] = intval( $ns );
131 $this->opts
['namespace'] = '';
134 $this->opts
['associated'] = $request->getBool( 'associated' );
135 $this->opts
['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
136 $this->opts
['tagfilter'] = (string)$request->getVal( 'tagfilter' );
138 // Allows reverts to have the bot flag in recent changes. It is just here to
139 // be passed in the form at the top of the page
140 if ( $user->isAllowed( '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 $feedType = $request->getVal( 'feed' );
158 'action' => 'feedcontributions',
161 if ( $this->opts
['topOnly'] ) {
162 $feedParams['toponly'] = true;
164 if ( $this->opts
['newOnly'] ) {
165 $feedParams['newonly'] = true;
167 if ( $this->opts
['hideMinor'] ) {
168 $feedParams['hideminor'] = true;
170 if ( $this->opts
['deletedOnly'] ) {
171 $feedParams['deletedonly'] = true;
173 if ( $this->opts
['tagfilter'] !== '' ) {
174 $feedParams['tagfilter'] = $this->opts
['tagfilter'];
176 if ( $this->opts
['namespace'] !== '' ) {
177 $feedParams['namespace'] = $this->opts
['namespace'];
179 // Don't use year and month for the feed URL, but pass them on if
180 // we redirect to API (if $feedType is specified)
181 if ( $feedType && $this->opts
['year'] !== null ) {
182 $feedParams['year'] = $this->opts
['year'];
184 if ( $feedType && $this->opts
['month'] !== null ) {
185 $feedParams['month'] = $this->opts
['month'];
189 // Maintain some level of backwards compatibility
190 // If people request feeds using the old parameters, redirect to API
191 $feedParams['feedformat'] = $feedType;
192 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
194 $out->redirect( $url, '301' );
199 // Add RSS/atom links
200 $this->addFeedLinks( $feedParams );
202 if ( Hooks
::run( 'SpecialContributionsBeforeMainOutput', [ $id, $userObj, $this ] ) ) {
203 if ( !$this->including() ) {
204 $out->addHTML( $this->getForm() );
206 $pager = new ContribsPager( $this->getContext(), [
208 'contribs' => $this->opts
['contribs'],
209 'namespace' => $this->opts
['namespace'],
210 'tagfilter' => $this->opts
['tagfilter'],
211 'start' => $this->opts
['start'],
212 'end' => $this->opts
['end'],
213 'deletedOnly' => $this->opts
['deletedOnly'],
214 'topOnly' => $this->opts
['topOnly'],
215 'newOnly' => $this->opts
['newOnly'],
216 'hideMinor' => $this->opts
['hideMinor'],
217 'nsInvert' => $this->opts
['nsInvert'],
218 'associated' => $this->opts
['associated'],
221 if ( IP
::isValidRange( $target ) && !$pager->isQueryableRange( $target ) ) {
222 // Valid range, but outside CIDR limit.
223 $limits = $this->getConfig()->get( 'RangeContributionsCIDRLimit' );
224 $limit = $limits[ IP
::isIPv4( $target ) ?
'IPv4' : 'IPv6' ];
225 $out->addWikiMsg( 'sp-contributions-outofrange', $limit );
226 } elseif ( !$pager->getNumRows() ) {
227 $out->addWikiMsg( 'nocontribs', $target );
229 # Show a message about replica DB lag, if applicable
230 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
231 $lag = $lb->safeGetLag( $pager->getDatabase() );
233 $out->showLagWarning( $lag );
236 $output = $pager->getBody();
237 if ( !$this->including() ) {
238 $output = '<p>' . $pager->getNavigationBar() . '</p>' .
240 '<p>' . $pager->getNavigationBar() . '</p>';
242 $out->addHTML( $output );
245 $out->preventClickjacking( $pager->getPreventClickjacking() );
247 # Show the appropriate "footer" message - WHOIS tools, etc.
248 if ( $this->opts
['contribs'] == 'newbie' ) {
249 $message = 'sp-contributions-footer-newbies';
250 } elseif ( IP
::isValidRange( $target ) ) {
251 $message = 'sp-contributions-footer-anon-range';
252 } elseif ( IP
::isIPAddress( $target ) ) {
253 $message = 'sp-contributions-footer-anon';
254 } elseif ( $userObj->isAnon() ) {
255 // No message for non-existing users
258 $message = 'sp-contributions-footer';
262 if ( !$this->including() ) {
263 if ( !$this->msg( $message, $target )->isDisabled() ) {
265 "<div class='mw-contributions-footer'>\n$1\n</div>",
266 [ $message, $target ] );
274 * Generates the subheading with links
275 * @param User $userObj User object for the target
276 * @return string Appropriately-escaped HTML to be output literally
277 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
280 protected function contributionsSub( $userObj ) {
281 if ( $userObj->isAnon() ) {
282 // Show a warning message that the user being searched for doesn't exists.
283 // User::isIP returns true for IP address and usemod IPs like '123.123.123.xxx',
284 // but returns false for IP ranges. We don't want to suggest either of these are
285 // valid usernames which we would with the 'contributions-userdoesnotexist' message.
286 if ( !User
::isIP( $userObj->getName() ) && !$userObj->isIPRange() ) {
287 $this->getOutput()->wrapWikiMsg(
288 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
290 'contributions-userdoesnotexist',
291 wfEscapeWikiText( $userObj->getName() ),
294 if ( !$this->including() ) {
295 $this->getOutput()->setStatusCode( 404 );
298 $user = htmlspecialchars( $userObj->getName() );
300 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
302 $nt = $userObj->getUserPage();
303 $talk = $userObj->getTalkPage();
306 $tools = self
::getUserLinks( $this, $userObj );
307 $links = $this->getLanguage()->pipeList( $tools );
309 // Show a note if the user is blocked and display the last block log entry.
310 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
311 // and also this will display a totally irrelevant log entry as a current block.
312 if ( !$this->including() ) {
313 // For IP ranges you must give Block::newFromTarget the CIDR string and not a user object.
314 if ( $userObj->isIPRange() ) {
315 $block = Block
::newFromTarget( $userObj->getName(), $userObj->getName() );
317 $block = Block
::newFromTarget( $userObj, $userObj );
320 if ( !is_null( $block ) && $block->getType() != Block
::TYPE_AUTO
) {
321 if ( $block->getType() == Block
::TYPE_RANGE
) {
322 $nt = MWNamespace
::getCanonicalName( NS_USER
) . ':' . $block->getTarget();
325 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
326 LogEventsList
::showLogExtract(
333 'showIfEmpty' => false,
336 'sp-contributions-blocked-notice-anon' :
337 'sp-contributions-blocked-notice',
338 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
340 'offset' => '' # don't use WebRequest parameter offset
347 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
351 * Links to different places.
353 * @note This function is also called in DeletedContributionsPage
354 * @param SpecialPage $sp SpecialPage instance, for context
355 * @param User $target Target user object
358 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
359 $id = $target->getId();
360 $username = $target->getName();
361 $userpage = $target->getUserPage();
362 $talkpage = $target->getTalkPage();
364 $linkRenderer = $sp->getLinkRenderer();
366 # No talk pages for IP ranges.
367 if ( !IP
::isValidRange( $username ) ) {
368 $tools['user-talk'] = $linkRenderer->makeLink(
370 $sp->msg( 'sp-contributions-talk' )->text()
374 if ( ( $id !== null ) ||
( $id === null && IP
::isIPAddress( $username ) ) ) {
375 if ( $sp->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
376 if ( $target->isBlocked() && $target->getBlock()->getType() != Block
::TYPE_AUTO
) {
377 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
378 SpecialPage
::getTitleFor( 'Block', $username ),
379 $sp->msg( 'change-blocklink' )->text()
381 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
382 SpecialPage
::getTitleFor( 'Unblock', $username ),
383 $sp->msg( 'unblocklink' )->text()
385 } else { # User is not blocked
386 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
387 SpecialPage
::getTitleFor( 'Block', $username ),
388 $sp->msg( 'blocklink' )->text()
394 $tools['log-block'] = $linkRenderer->makeKnownLink(
395 SpecialPage
::getTitleFor( 'Log', 'block' ),
396 $sp->msg( 'sp-contributions-blocklog' )->text(),
398 [ 'page' => $userpage->getPrefixedText() ]
401 # Suppression log link (T61120)
402 if ( $sp->getUser()->isAllowed( 'suppressionlog' ) ) {
403 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
404 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
405 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
407 [ 'offender' => $username ]
412 # Don't show some links for IP ranges
413 if ( !IP
::isValidRange( $username ) ) {
415 $tools['uploads'] = $linkRenderer->makeKnownLink(
416 SpecialPage
::getTitleFor( 'Listfiles', $username ),
417 $sp->msg( 'sp-contributions-uploads' )->text()
421 $tools['logs'] = $linkRenderer->makeKnownLink(
422 SpecialPage
::getTitleFor( 'Log', $username ),
423 $sp->msg( 'sp-contributions-logs' )->text()
426 # Add link to deleted user contributions for priviledged users
427 if ( $sp->getUser()->isAllowed( 'deletedhistory' ) ) {
428 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
429 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
430 $sp->msg( 'sp-contributions-deleted', $username )->text()
435 # Add a link to change user rights for privileged users
436 $userrightsPage = new UserrightsPage();
437 $userrightsPage->setContext( $sp->getContext() );
438 if ( $userrightsPage->userCanChangeRights( $target ) ) {
439 $tools['userrights'] = $linkRenderer->makeKnownLink(
440 SpecialPage
::getTitleFor( 'Userrights', $username ),
441 $sp->msg( 'sp-contributions-userrights', $username )->text()
445 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
451 * Generates the namespace selector form with hidden attributes.
452 * @return string HTML fragment
454 protected function getForm() {
455 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
456 if ( !isset( $this->opts
['target'] ) ) {
457 $this->opts
['target'] = '';
459 $this->opts
['target'] = str_replace( '_', ' ', $this->opts
['target'] );
462 if ( !isset( $this->opts
['namespace'] ) ) {
463 $this->opts
['namespace'] = '';
466 if ( !isset( $this->opts
['nsInvert'] ) ) {
467 $this->opts
['nsInvert'] = '';
470 if ( !isset( $this->opts
['associated'] ) ) {
471 $this->opts
['associated'] = false;
474 if ( !isset( $this->opts
['contribs'] ) ) {
475 $this->opts
['contribs'] = 'user';
478 if ( !isset( $this->opts
['start'] ) ) {
479 $this->opts
['start'] = '';
482 if ( !isset( $this->opts
['end'] ) ) {
483 $this->opts
['end'] = '';
486 if ( $this->opts
['contribs'] == 'newbie' ) {
487 $this->opts
['target'] = '';
490 if ( !isset( $this->opts
['tagfilter'] ) ) {
491 $this->opts
['tagfilter'] = '';
494 if ( !isset( $this->opts
['topOnly'] ) ) {
495 $this->opts
['topOnly'] = false;
498 if ( !isset( $this->opts
['newOnly'] ) ) {
499 $this->opts
['newOnly'] = false;
502 if ( !isset( $this->opts
['hideMinor'] ) ) {
503 $this->opts
['hideMinor'] = false;
506 // Modules required only for the form
507 $this->getOutput()->addModules( [
508 'mediawiki.userSuggest',
509 'mediawiki.special.contributions',
511 $this->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
512 $this->getOutput()->enableOOUI();
514 $form = Html
::openElement(
518 'action' => wfScript(),
519 'class' => 'mw-contributions-form'
523 # Add hidden params for tracking except for parameters in $skipParameters
541 foreach ( $this->opts
as $name => $value ) {
542 if ( in_array( $name, $skipParameters ) ) {
545 $form .= "\t" . Html
::hidden( $name, $value ) . "\n";
548 $tagFilter = ChangeTags
::buildTagFilterSelector(
549 $this->opts
['tagfilter'], false, $this->getContext() );
552 $filterSelection = Html
::rawElement(
555 implode( "\u{00A0}", $tagFilter )
558 $filterSelection = Html
::rawElement( 'div', [], '' );
561 $labelNewbies = Xml
::radioLabel(
562 $this->msg( 'sp-contributions-newbies' )->text(),
566 $this->opts
['contribs'] == 'newbie',
567 [ 'class' => 'mw-input' ]
569 $labelUsername = Xml
::radioLabel(
570 $this->msg( 'sp-contributions-username' )->text(),
574 $this->opts
['contribs'] == 'user',
575 [ 'class' => 'mw-input' ]
577 $input = Html
::input(
579 $this->opts
['target'],
585 'mw-ui-input-inline',
586 'mw-autocomplete-user', // used by mediawiki.userSuggest
589 // Only autofocus if target hasn't been specified or in non-newbies mode
590 ( $this->opts
['contribs'] === 'newbie' ||
$this->opts
['target'] )
591 ?
[] : [ 'autofocus' => true ]
595 $targetSelection = Html
::rawElement(
598 $labelNewbies . '<br>' . $labelUsername . ' ' . $input . ' '
601 $namespaceSelection = Xml
::tags(
605 $this->msg( 'namespace' )->text(),
609 Html
::namespaceSelector(
610 [ 'selected' => $this->opts
['namespace'], 'all' => '' ],
612 'name' => 'namespace',
614 'class' => 'namespaceselector',
619 [ 'class' => 'mw-input-with-label' ],
621 $this->msg( 'invert' )->text(),
624 $this->opts
['nsInvert'],
626 'title' => $this->msg( 'tooltip-invert' )->text(),
627 'class' => 'mw-input'
631 Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' ],
633 $this->msg( 'namespace_association' )->text(),
636 $this->opts
['associated'],
638 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
639 'class' => 'mw-input'
647 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
648 $filters[] = Html
::rawElement(
650 [ 'class' => 'mw-input-with-label' ],
652 $this->msg( 'history-show-deleted' )->text(),
654 'mw-show-deleted-only',
655 $this->opts
['deletedOnly'],
656 [ 'class' => 'mw-input' ]
661 $filters[] = Html
::rawElement(
663 [ 'class' => 'mw-input-with-label' ],
665 $this->msg( 'sp-contributions-toponly' )->text(),
668 $this->opts
['topOnly'],
669 [ 'class' => 'mw-input' ]
672 $filters[] = Html
::rawElement(
674 [ 'class' => 'mw-input-with-label' ],
676 $this->msg( 'sp-contributions-newonly' )->text(),
679 $this->opts
['newOnly'],
680 [ 'class' => 'mw-input' ]
683 $filters[] = Html
::rawElement(
685 [ 'class' => 'mw-input-with-label' ],
687 $this->msg( 'sp-contributions-hideminor' )->text(),
689 'mw-hide-minor-edits',
690 $this->opts
['hideMinor'],
691 [ 'class' => 'mw-input' ]
696 'SpecialContributions::getForm::filters',
700 $extraOptions = Html
::rawElement(
703 implode( '', $filters )
706 $dateRangeSelection = Html
::rawElement(
709 Xml
::label( wfMessage( 'date-range-from' )->text(), 'mw-date-start' ) . ' ' .
710 new DateInputWidget( [
712 'id' => 'mw-date-start',
714 'value' => $this->opts
['start'],
715 'longDisplayFormat' => true,
717 Xml
::label( wfMessage( 'date-range-to' )->text(), 'mw-date-end' ) . ' ' .
718 new DateInputWidget( [
720 'id' => 'mw-date-end',
722 'value' => $this->opts
['end'],
723 'longDisplayFormat' => true,
727 $submit = Xml
::tags( 'div', [],
729 $this->msg( 'sp-contributions-submit' )->text(),
730 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
734 $form .= Xml
::fieldset(
735 $this->msg( 'sp-contributions-search' )->text(),
737 $namespaceSelection .
740 $dateRangeSelection .
742 [ 'class' => 'mw-contributions-table' ]
745 $explain = $this->msg( 'sp-contributions-explain' );
746 if ( !$explain->isBlank() ) {
747 $form .= "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>";
750 $form .= Xml
::closeElement( 'form' );
756 * Return an array of subpages beginning with $search that this special page will accept.
758 * @param string $search Prefix to search for
759 * @param int $limit Maximum number of results to return (usually 10)
760 * @param int $offset Number of results to skip (usually 0)
761 * @return string[] Matching subpages
763 public function prefixSearchSubpages( $search, $limit, $offset ) {
764 $user = User
::newFromName( $search );
766 // No prefix suggestion for invalid user
769 // Autocomplete subpage as user list - public to allow caching
770 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
773 protected function getGroupName() {