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
;
26 use MediaWiki\Widget\DateInputWidget
;
29 * Special:Contributions, show user contributions in a paged list
31 * @ingroup SpecialPage
33 class SpecialContributions
extends IncludableSpecialPage
{
36 public function __construct() {
37 parent
::__construct( 'Contributions' );
40 public function execute( $par ) {
42 $this->outputHeader();
43 $out = $this->getOutput();
44 // Modules required for viewing the list of contributions (also when included on other pages)
45 $out->addModuleStyles( [
46 'mediawiki.interface.helpers.styles',
48 'mediawiki.special.changeslist',
50 $out->addModules( 'mediawiki.special.recentchanges' );
51 $this->addHelpLink( 'Help:User contributions' );
54 $request = $this->getRequest();
56 $target = $par ??
$request->getVal( 'target' );
58 if ( $request->getVal( 'contribs' ) == 'newbie' ||
$par === 'newbies' ) {
60 $this->opts
['contribs'] = 'newbie';
62 $this->opts
['contribs'] = 'user';
65 $this->opts
['deletedOnly'] = $request->getBool( 'deletedOnly' );
67 if ( !strlen( $target ) ) {
68 if ( !$this->including() ) {
69 $out->addHTML( $this->getForm() );
75 $user = $this->getUser();
77 $this->opts
['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
78 $this->opts
['target'] = $target;
79 $this->opts
['topOnly'] = $request->getBool( 'topOnly' );
80 $this->opts
['newOnly'] = $request->getBool( 'newOnly' );
81 $this->opts
['hideMinor'] = $request->getBool( 'hideMinor' );
84 if ( $this->opts
['contribs'] === 'newbie' ) {
85 $userObj = User
::newFromName( $target ); // hysterical raisins
86 $out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub' ) );
87 $out->setHTMLTitle( $this->msg(
89 $this->msg( 'sp-contributions-newbies-title' )->plain()
90 )->inContentLanguage() );
91 } elseif ( ExternalUserNames
::isExternal( $target ) ) {
92 $userObj = User
::newFromName( $target, false );
94 $out->addHTML( $this->getForm() );
98 $out->addSubtitle( $this->contributionsSub( $userObj ) );
99 $out->setHTMLTitle( $this->msg(
101 $this->msg( 'contributions-title', $target )->plain()
102 )->inContentLanguage() );
104 $nt = Title
::makeTitleSafe( NS_USER
, $target );
106 $out->addHTML( $this->getForm() );
109 $userObj = User
::newFromName( $nt->getText(), false );
111 $out->addHTML( $this->getForm() );
114 $id = $userObj->getId();
116 $target = $nt->getText();
117 $out->addSubtitle( $this->contributionsSub( $userObj ) );
118 $out->setHTMLTitle( $this->msg(
120 $this->msg( 'contributions-title', $target )->plain()
121 )->inContentLanguage() );
123 # For IP ranges, we want the contributionsSub, but not the skin-dependent
124 # links under 'Tools', which may include irrelevant links like 'Logs'.
125 if ( !IP
::isValidRange( $target ) ) {
126 $this->getSkin()->setRelevantUser( $userObj );
130 $ns = $request->getVal( 'namespace', null );
131 if ( $ns !== null && $ns !== '' ) {
132 $this->opts
['namespace'] = intval( $ns );
134 $this->opts
['namespace'] = '';
137 $this->opts
['associated'] = $request->getBool( 'associated' );
138 $this->opts
['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
139 $this->opts
['tagfilter'] = (string)$request->getVal( 'tagfilter' );
141 // Allows reverts to have the bot flag in recent changes. It is just here to
142 // be passed in the form at the top of the page
143 if ( $user->isAllowed( 'markbotedits' ) && $request->getBool( 'bot' ) ) {
144 $this->opts
['bot'] = '1';
147 $skip = $request->getText( 'offset' ) ||
$request->getText( 'dir' ) == 'prev';
148 # Offset overrides year/month selection
150 $this->opts
['year'] = $request->getVal( 'year' );
151 $this->opts
['month'] = $request->getVal( 'month' );
153 $this->opts
['start'] = $request->getVal( 'start' );
154 $this->opts
['end'] = $request->getVal( 'end' );
156 $this->opts
= ContribsPager
::processDateFilter( $this->opts
);
158 if ( $this->opts
['namespace'] < NS_MAIN
) {
159 $this->getOutput()->wrapWikiMsg(
160 "<div class=\"mw-negative-namespace-not-supported error\">\n\$1\n</div>",
161 [ 'negative-namespace-not-supported' ]
163 $out->addHTML( $this->getForm() );
167 $feedType = $request->getVal( 'feed' );
170 'action' => 'feedcontributions',
173 if ( $this->opts
['topOnly'] ) {
174 $feedParams['toponly'] = true;
176 if ( $this->opts
['newOnly'] ) {
177 $feedParams['newonly'] = true;
179 if ( $this->opts
['hideMinor'] ) {
180 $feedParams['hideminor'] = true;
182 if ( $this->opts
['deletedOnly'] ) {
183 $feedParams['deletedonly'] = true;
185 if ( $this->opts
['tagfilter'] !== '' ) {
186 $feedParams['tagfilter'] = $this->opts
['tagfilter'];
188 if ( $this->opts
['namespace'] !== '' ) {
189 $feedParams['namespace'] = $this->opts
['namespace'];
191 // Don't use year and month for the feed URL, but pass them on if
192 // we redirect to API (if $feedType is specified)
193 if ( $feedType && $this->opts
['year'] !== null ) {
194 $feedParams['year'] = $this->opts
['year'];
196 if ( $feedType && $this->opts
['month'] !== null ) {
197 $feedParams['month'] = $this->opts
['month'];
201 // Maintain some level of backwards compatibility
202 // If people request feeds using the old parameters, redirect to API
203 $feedParams['feedformat'] = $feedType;
204 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
206 $out->redirect( $url, '301' );
211 // Add RSS/atom links
212 $this->addFeedLinks( $feedParams );
214 if ( Hooks
::run( 'SpecialContributionsBeforeMainOutput', [ $id, $userObj, $this ] ) ) {
215 if ( !$this->including() ) {
216 $out->addHTML( $this->getForm() );
218 $pager = new ContribsPager( $this->getContext(), [
220 'contribs' => $this->opts
['contribs'],
221 'namespace' => $this->opts
['namespace'],
222 'tagfilter' => $this->opts
['tagfilter'],
223 'start' => $this->opts
['start'],
224 'end' => $this->opts
['end'],
225 'deletedOnly' => $this->opts
['deletedOnly'],
226 'topOnly' => $this->opts
['topOnly'],
227 'newOnly' => $this->opts
['newOnly'],
228 'hideMinor' => $this->opts
['hideMinor'],
229 'nsInvert' => $this->opts
['nsInvert'],
230 'associated' => $this->opts
['associated'],
233 if ( IP
::isValidRange( $target ) && !$pager->isQueryableRange( $target ) ) {
234 // Valid range, but outside CIDR limit.
235 $limits = $this->getConfig()->get( 'RangeContributionsCIDRLimit' );
236 $limit = $limits[ IP
::isIPv4( $target ) ?
'IPv4' : 'IPv6' ];
237 $out->addWikiMsg( 'sp-contributions-outofrange', $limit );
238 } elseif ( !$pager->getNumRows() ) {
239 $out->addWikiMsg( 'nocontribs', $target );
241 # Show a message about replica DB lag, if applicable
242 $lag = $pager->getDatabase()->getSessionLagStatus()['lag'];
244 $out->showLagWarning( $lag );
247 $output = $pager->getBody();
248 if ( !$this->including() ) {
249 $output = $pager->getNavigationBar() .
251 $pager->getNavigationBar();
253 $out->addHTML( $output );
256 $out->preventClickjacking( $pager->getPreventClickjacking() );
258 # Show the appropriate "footer" message - WHOIS tools, etc.
259 if ( $this->opts
['contribs'] == 'newbie' ) {
260 $message = 'sp-contributions-footer-newbies';
261 } elseif ( IP
::isValidRange( $target ) ) {
262 $message = 'sp-contributions-footer-anon-range';
263 } elseif ( IP
::isIPAddress( $target ) ) {
264 $message = 'sp-contributions-footer-anon';
265 } elseif ( $userObj->isAnon() ) {
266 // No message for non-existing users
269 $message = 'sp-contributions-footer';
272 if ( $message && !$this->including() && !$this->msg( $message, $target )->isDisabled() ) {
274 "<div class='mw-contributions-footer'>\n$1\n</div>",
275 [ $message, $target ] );
281 * Generates the subheading with links
282 * @param User $userObj User object for the target
283 * @return string Appropriately-escaped HTML to be output literally
284 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
287 protected function contributionsSub( $userObj ) {
288 if ( $userObj->isAnon() ) {
289 // Show a warning message that the user being searched for doesn't exist.
290 // User::isIP returns true for IP address and usemod IPs like '123.123.123.xxx',
291 // but returns false for IP ranges. We don't want to suggest either of these are
292 // valid usernames which we would with the 'contributions-userdoesnotexist' message.
293 if ( !User
::isIP( $userObj->getName() ) && !$userObj->isIPRange() ) {
294 $this->getOutput()->wrapWikiMsg(
295 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
297 'contributions-userdoesnotexist',
298 wfEscapeWikiText( $userObj->getName() ),
301 if ( !$this->including() ) {
302 $this->getOutput()->setStatusCode( 404 );
305 $user = htmlspecialchars( $userObj->getName() );
307 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
309 $nt = $userObj->getUserPage();
310 $talk = $userObj->getTalkPage();
313 $tools = self
::getUserLinks( $this, $userObj );
314 $links = Html
::openElement( 'span', [ 'class' => 'mw-changeslist-links' ] );
315 foreach ( $tools as $tool ) {
316 $links .= Html
::rawElement( 'span', [], $tool ) . ' ';
318 $links = trim( $links ) . Html
::closeElement( 'span' );
320 // Show a note if the user is blocked and display the last block log entry.
321 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
322 // and also this will display a totally irrelevant log entry as a current block.
323 if ( !$this->including() ) {
324 // For IP ranges you must give DatabaseBlock::newFromTarget the CIDR string
325 // and not a user object.
326 if ( $userObj->isIPRange() ) {
327 $block = DatabaseBlock
::newFromTarget( $userObj->getName(), $userObj->getName() );
329 $block = DatabaseBlock
::newFromTarget( $userObj, $userObj );
332 if ( !is_null( $block ) && $block->getType() != DatabaseBlock
::TYPE_AUTO
) {
333 if ( $block->getType() == DatabaseBlock
::TYPE_RANGE
) {
334 $nt = MediaWikiServices
::getInstance()->getNamespaceInfo()->
335 getCanonicalName( NS_USER
) . ':' . $block->getTarget();
338 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
339 LogEventsList
::showLogExtract(
346 'showIfEmpty' => false,
349 'sp-contributions-blocked-notice-anon' :
350 'sp-contributions-blocked-notice',
351 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
353 'offset' => '' # don't use WebRequest parameter offset
360 return Html
::rawElement( 'div', [ 'class' => 'mw-contributions-user-tools' ],
361 $this->msg( 'contributions-subtitle' )->rawParams( $user )->params( $userObj->getName() )
367 * Links to different places.
369 * @note This function is also called in DeletedContributionsPage
370 * @param SpecialPage $sp SpecialPage instance, for context
371 * @param User $target Target user object
374 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
375 $id = $target->getId();
376 $username = $target->getName();
377 $userpage = $target->getUserPage();
378 $talkpage = $target->getTalkPage();
379 $isIP = IP
::isValid( $username );
380 $isRange = IP
::isValidRange( $username );
382 $linkRenderer = $sp->getLinkRenderer();
384 # No talk pages for IP ranges.
386 $tools['user-talk'] = $linkRenderer->makeLink(
388 $sp->msg( 'sp-contributions-talk' )->text()
392 if ( $sp->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
393 if ( $target->getBlock() && $target->getBlock()->getType() != DatabaseBlock
::TYPE_AUTO
) {
394 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
395 SpecialPage
::getTitleFor( 'Block', $username ),
396 $sp->msg( 'change-blocklink' )->text()
398 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
399 SpecialPage
::getTitleFor( 'Unblock', $username ),
400 $sp->msg( 'unblocklink' )->text()
402 } else { # User is not blocked
403 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
404 SpecialPage
::getTitleFor( 'Block', $username ),
405 $sp->msg( 'blocklink' )->text()
411 $tools['log-block'] = $linkRenderer->makeKnownLink(
412 SpecialPage
::getTitleFor( 'Log', 'block' ),
413 $sp->msg( 'sp-contributions-blocklog' )->text(),
415 [ 'page' => $userpage->getPrefixedText() ]
418 # Suppression log link (T61120)
419 if ( $sp->getUser()->isAllowed( 'suppressionlog' ) ) {
420 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
421 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
422 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
424 [ 'offender' => $username ]
428 # Don't show some links for IP ranges
430 # Uploads: hide if IPs cannot upload (T220674)
431 if ( !$isIP ||
$target->isAllowed( 'upload' ) ) {
432 $tools['uploads'] = $linkRenderer->makeKnownLink(
433 SpecialPage
::getTitleFor( 'Listfiles', $username ),
434 $sp->msg( 'sp-contributions-uploads' )->text()
440 $tools['logs'] = $linkRenderer->makeKnownLink(
441 SpecialPage
::getTitleFor( 'Log', $username ),
442 $sp->msg( 'sp-contributions-logs' )->text()
445 # Add link to deleted user contributions for priviledged users
447 if ( $sp->getUser()->isAllowed( 'deletedhistory' ) ) {
448 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
449 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
450 $sp->msg( 'sp-contributions-deleted', $username )->text()
455 # Add a link to change user rights for privileged users
456 $userrightsPage = new UserrightsPage();
457 $userrightsPage->setContext( $sp->getContext() );
458 if ( $userrightsPage->userCanChangeRights( $target ) ) {
459 $tools['userrights'] = $linkRenderer->makeKnownLink(
460 SpecialPage
::getTitleFor( 'Userrights', $username ),
461 $sp->msg( 'sp-contributions-userrights', $username )->text()
465 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
471 * Generates the namespace selector form with hidden attributes.
472 * @return string HTML fragment
474 protected function getForm() {
475 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
476 if ( !isset( $this->opts
['target'] ) ) {
477 $this->opts
['target'] = '';
479 $this->opts
['target'] = str_replace( '_', ' ', $this->opts
['target'] );
482 if ( !isset( $this->opts
['namespace'] ) ) {
483 $this->opts
['namespace'] = '';
486 if ( !isset( $this->opts
['nsInvert'] ) ) {
487 $this->opts
['nsInvert'] = '';
490 if ( !isset( $this->opts
['associated'] ) ) {
491 $this->opts
['associated'] = false;
494 if ( !isset( $this->opts
['contribs'] ) ) {
495 $this->opts
['contribs'] = 'user';
498 if ( !isset( $this->opts
['start'] ) ) {
499 $this->opts
['start'] = '';
502 if ( !isset( $this->opts
['end'] ) ) {
503 $this->opts
['end'] = '';
506 if ( $this->opts
['contribs'] == 'newbie' ) {
507 $this->opts
['target'] = '';
510 if ( !isset( $this->opts
['tagfilter'] ) ) {
511 $this->opts
['tagfilter'] = '';
514 if ( !isset( $this->opts
['topOnly'] ) ) {
515 $this->opts
['topOnly'] = false;
518 if ( !isset( $this->opts
['newOnly'] ) ) {
519 $this->opts
['newOnly'] = false;
522 if ( !isset( $this->opts
['hideMinor'] ) ) {
523 $this->opts
['hideMinor'] = false;
526 // Modules required only for the form
527 $this->getOutput()->addModules( [
528 'mediawiki.userSuggest',
529 'mediawiki.special.contributions',
531 $this->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
532 $this->getOutput()->enableOOUI();
534 $form = Html
::openElement(
538 'action' => wfScript(),
539 'class' => 'mw-contributions-form'
543 # Add hidden params for tracking except for parameters in $skipParameters
561 foreach ( $this->opts
as $name => $value ) {
562 if ( in_array( $name, $skipParameters ) ) {
565 $form .= "\t" . Html
::hidden( $name, $value ) . "\n";
568 $tagFilter = ChangeTags
::buildTagFilterSelector(
569 $this->opts
['tagfilter'], false, $this->getContext() );
572 $filterSelection = Html
::rawElement(
575 implode( "\u{00A0}", $tagFilter )
578 $filterSelection = Html
::rawElement( 'div', [], '' );
581 $labelNewbies = Xml
::radioLabel(
582 $this->msg( 'sp-contributions-newbies' )->text(),
586 $this->opts
['contribs'] == 'newbie',
587 [ 'class' => 'mw-input' ]
589 $labelUsername = Xml
::radioLabel(
590 $this->msg( 'sp-contributions-username' )->text(),
594 $this->opts
['contribs'] == 'user',
595 [ 'class' => 'mw-input' ]
597 $input = Html
::input(
599 $this->opts
['target'],
602 'id' => 'mw-target-user-or-ip',
606 'mw-ui-input-inline',
607 'mw-autocomplete-user', // used by mediawiki.userSuggest
610 // Only autofocus if target hasn't been specified or in non-newbies mode
611 ( $this->opts
['contribs'] === 'newbie' ||
$this->opts
['target'] )
612 ?
[] : [ 'autofocus' => true ]
616 $targetSelection = Html
::rawElement(
619 $labelNewbies . '<br>' . $labelUsername . ' ' . $input . ' '
622 $hidden = $this->opts
['namespace'] === '' ?
' mw-input-hidden' : '';
623 $namespaceSelection = Xml
::tags(
627 $this->msg( 'namespace' )->text(),
630 Html
::namespaceSelector(
631 [ 'selected' => $this->opts
['namespace'], 'all' => '', 'in-user-lang' => true ],
633 'name' => 'namespace',
635 'class' => 'namespaceselector',
640 [ 'class' => 'mw-input-with-label' . $hidden ],
642 $this->msg( 'invert' )->text(),
645 $this->opts
['nsInvert'],
647 'title' => $this->msg( 'tooltip-invert' )->text(),
648 'class' => 'mw-input'
652 Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' . $hidden ],
654 $this->msg( 'namespace_association' )->text(),
657 $this->opts
['associated'],
659 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
660 'class' => 'mw-input'
668 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
669 $filters[] = Html
::rawElement(
671 [ 'class' => 'mw-input-with-label' ],
673 $this->msg( 'history-show-deleted' )->text(),
675 'mw-show-deleted-only',
676 $this->opts
['deletedOnly'],
677 [ 'class' => 'mw-input' ]
682 $filters[] = Html
::rawElement(
684 [ 'class' => 'mw-input-with-label' ],
686 $this->msg( 'sp-contributions-toponly' )->text(),
689 $this->opts
['topOnly'],
690 [ 'class' => 'mw-input' ]
693 $filters[] = Html
::rawElement(
695 [ 'class' => 'mw-input-with-label' ],
697 $this->msg( 'sp-contributions-newonly' )->text(),
700 $this->opts
['newOnly'],
701 [ 'class' => 'mw-input' ]
704 $filters[] = Html
::rawElement(
706 [ 'class' => 'mw-input-with-label' ],
708 $this->msg( 'sp-contributions-hideminor' )->text(),
710 'mw-hide-minor-edits',
711 $this->opts
['hideMinor'],
712 [ 'class' => 'mw-input' ]
717 'SpecialContributions::getForm::filters',
721 $extraOptions = Html
::rawElement(
724 implode( '', $filters )
727 $dateRangeSelection = Html
::rawElement(
730 Xml
::label( $this->msg( 'date-range-from' )->text(), 'mw-date-start' ) . ' ' .
731 new DateInputWidget( [
733 'id' => 'mw-date-start',
735 'value' => $this->opts
['start'],
736 'longDisplayFormat' => true,
738 Xml
::label( $this->msg( 'date-range-to' )->text(), 'mw-date-end' ) . ' ' .
739 new DateInputWidget( [
741 'id' => 'mw-date-end',
743 'value' => $this->opts
['end'],
744 'longDisplayFormat' => true,
748 $submit = Xml
::tags( 'div', [],
750 $this->msg( 'sp-contributions-submit' )->text(),
751 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
755 $form .= Xml
::fieldset(
756 $this->msg( 'sp-contributions-search' )->text(),
758 $namespaceSelection .
761 $dateRangeSelection .
763 [ 'class' => 'mw-contributions-table' ]
766 $explain = $this->msg( 'sp-contributions-explain' );
767 if ( !$explain->isBlank() ) {
768 $form .= Html
::rawElement(
769 'p', [ 'id' => 'mw-sp-contributions-explain' ], $explain->parse()
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() {