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\Widget\DateInputWidget
;
27 * Special:Contributions, show user contributions in a paged list
29 * @ingroup SpecialPage
31 class SpecialContributions
extends IncludableSpecialPage
{
34 public function __construct() {
35 parent
::__construct( 'Contributions' );
38 public function execute( $par ) {
40 $this->outputHeader();
41 $out = $this->getOutput();
42 // Modules required for viewing the list of contributions (also when included on other pages)
43 $out->addModuleStyles( [
44 'mediawiki.interface.helpers.styles',
46 'mediawiki.special.changeslist',
48 $out->addModules( 'mediawiki.special.recentchanges' );
49 $this->addHelpLink( 'Help:User contributions' );
52 $request = $this->getRequest();
54 $target = $par ??
$request->getVal( 'target' );
56 if ( $request->getVal( 'contribs' ) == 'newbie' ||
$par === 'newbies' ) {
58 $this->opts
['contribs'] = 'newbie';
60 $this->opts
['contribs'] = 'user';
63 $this->opts
['deletedOnly'] = $request->getBool( 'deletedOnly' );
65 if ( !strlen( $target ) ) {
66 if ( !$this->including() ) {
67 $out->addHTML( $this->getForm() );
73 $user = $this->getUser();
75 $this->opts
['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
76 $this->opts
['target'] = $target;
77 $this->opts
['topOnly'] = $request->getBool( 'topOnly' );
78 $this->opts
['newOnly'] = $request->getBool( 'newOnly' );
79 $this->opts
['hideMinor'] = $request->getBool( 'hideMinor' );
82 if ( $this->opts
['contribs'] === 'newbie' ) {
83 $userObj = User
::newFromName( $target ); // hysterical raisins
84 $out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub' ) );
85 $out->setHTMLTitle( $this->msg(
87 $this->msg( 'sp-contributions-newbies-title' )->plain()
88 )->inContentLanguage() );
89 } elseif ( ExternalUserNames
::isExternal( $target ) ) {
90 $userObj = User
::newFromName( $target, false );
92 $out->addHTML( $this->getForm() );
96 $out->addSubtitle( $this->contributionsSub( $userObj ) );
97 $out->setHTMLTitle( $this->msg(
99 $this->msg( 'contributions-title', $target )->plain()
100 )->inContentLanguage() );
102 $nt = Title
::makeTitleSafe( NS_USER
, $target );
104 $out->addHTML( $this->getForm() );
107 $userObj = User
::newFromName( $nt->getText(), false );
109 $out->addHTML( $this->getForm() );
112 $id = $userObj->getId();
114 $target = $nt->getText();
115 $out->addSubtitle( $this->contributionsSub( $userObj ) );
116 $out->setHTMLTitle( $this->msg(
118 $this->msg( 'contributions-title', $target )->plain()
119 )->inContentLanguage() );
121 # For IP ranges, we want the contributionsSub, but not the skin-dependent
122 # links under 'Tools', which may include irrelevant links like 'Logs'.
123 if ( !IP
::isValidRange( $target ) ) {
124 $this->getSkin()->setRelevantUser( $userObj );
128 $ns = $request->getVal( 'namespace', null );
129 if ( $ns !== null && $ns !== '' ) {
130 $this->opts
['namespace'] = intval( $ns );
132 $this->opts
['namespace'] = '';
135 $this->opts
['associated'] = $request->getBool( 'associated' );
136 $this->opts
['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
137 $this->opts
['tagfilter'] = (string)$request->getVal( 'tagfilter' );
139 // Allows reverts to have the bot flag in recent changes. It is just here to
140 // be passed in the form at the top of the page
141 if ( $user->isAllowed( 'markbotedits' ) && $request->getBool( 'bot' ) ) {
142 $this->opts
['bot'] = '1';
145 $skip = $request->getText( 'offset' ) ||
$request->getText( 'dir' ) == 'prev';
146 # Offset overrides year/month selection
148 $this->opts
['year'] = $request->getVal( 'year' );
149 $this->opts
['month'] = $request->getVal( 'month' );
151 $this->opts
['start'] = $request->getVal( 'start' );
152 $this->opts
['end'] = $request->getVal( 'end' );
154 $this->opts
= ContribsPager
::processDateFilter( $this->opts
);
156 if ( $this->opts
['namespace'] < NS_MAIN
) {
157 $this->getOutput()->wrapWikiMsg(
158 "<div class=\"mw-negative-namespace-not-supported error\">\n\$1\n</div>",
159 [ 'negative-namespace-not-supported' ]
161 $out->addHTML( $this->getForm() );
165 $feedType = $request->getVal( 'feed' );
168 'action' => 'feedcontributions',
171 if ( $this->opts
['topOnly'] ) {
172 $feedParams['toponly'] = true;
174 if ( $this->opts
['newOnly'] ) {
175 $feedParams['newonly'] = true;
177 if ( $this->opts
['hideMinor'] ) {
178 $feedParams['hideminor'] = true;
180 if ( $this->opts
['deletedOnly'] ) {
181 $feedParams['deletedonly'] = true;
183 if ( $this->opts
['tagfilter'] !== '' ) {
184 $feedParams['tagfilter'] = $this->opts
['tagfilter'];
186 if ( $this->opts
['namespace'] !== '' ) {
187 $feedParams['namespace'] = $this->opts
['namespace'];
189 // Don't use year and month for the feed URL, but pass them on if
190 // we redirect to API (if $feedType is specified)
191 if ( $feedType && $this->opts
['year'] !== null ) {
192 $feedParams['year'] = $this->opts
['year'];
194 if ( $feedType && $this->opts
['month'] !== null ) {
195 $feedParams['month'] = $this->opts
['month'];
199 // Maintain some level of backwards compatibility
200 // If people request feeds using the old parameters, redirect to API
201 $feedParams['feedformat'] = $feedType;
202 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
204 $out->redirect( $url, '301' );
209 // Add RSS/atom links
210 $this->addFeedLinks( $feedParams );
212 if ( Hooks
::run( 'SpecialContributionsBeforeMainOutput', [ $id, $userObj, $this ] ) ) {
213 if ( !$this->including() ) {
214 $out->addHTML( $this->getForm() );
216 $pager = new ContribsPager( $this->getContext(), [
218 'contribs' => $this->opts
['contribs'],
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'],
231 if ( IP
::isValidRange( $target ) && !$pager->isQueryableRange( $target ) ) {
232 // Valid range, but outside CIDR limit.
233 $limits = $this->getConfig()->get( 'RangeContributionsCIDRLimit' );
234 $limit = $limits[ IP
::isIPv4( $target ) ?
'IPv4' : 'IPv6' ];
235 $out->addWikiMsg( 'sp-contributions-outofrange', $limit );
236 } elseif ( !$pager->getNumRows() ) {
237 $out->addWikiMsg( 'nocontribs', $target );
239 # Show a message about replica DB lag, if applicable
240 $lag = $pager->getDatabase()->getSessionLagStatus()['lag'];
242 $out->showLagWarning( $lag );
245 $output = $pager->getBody();
246 if ( !$this->including() ) {
247 $output = $pager->getNavigationBar() .
249 $pager->getNavigationBar();
251 $out->addHTML( $output );
254 $out->preventClickjacking( $pager->getPreventClickjacking() );
256 # Show the appropriate "footer" message - WHOIS tools, etc.
257 if ( $this->opts
['contribs'] == 'newbie' ) {
258 $message = 'sp-contributions-footer-newbies';
259 } elseif ( IP
::isValidRange( $target ) ) {
260 $message = 'sp-contributions-footer-anon-range';
261 } elseif ( IP
::isIPAddress( $target ) ) {
262 $message = 'sp-contributions-footer-anon';
263 } elseif ( $userObj->isAnon() ) {
264 // No message for non-existing users
267 $message = 'sp-contributions-footer';
270 if ( $message && !$this->including() && !$this->msg( $message, $target )->isDisabled() ) {
272 "<div class='mw-contributions-footer'>\n$1\n</div>",
273 [ $message, $target ] );
279 * Generates the subheading with links
280 * @param User $userObj User object for the target
281 * @return string Appropriately-escaped HTML to be output literally
282 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
285 protected function contributionsSub( $userObj ) {
286 if ( $userObj->isAnon() ) {
287 // Show a warning message that the user being searched for doesn't exists.
288 // User::isIP returns true for IP address and usemod IPs like '123.123.123.xxx',
289 // but returns false for IP ranges. We don't want to suggest either of these are
290 // valid usernames which we would with the 'contributions-userdoesnotexist' message.
291 if ( !User
::isIP( $userObj->getName() ) && !$userObj->isIPRange() ) {
292 $this->getOutput()->wrapWikiMsg(
293 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
295 'contributions-userdoesnotexist',
296 wfEscapeWikiText( $userObj->getName() ),
299 if ( !$this->including() ) {
300 $this->getOutput()->setStatusCode( 404 );
303 $user = htmlspecialchars( $userObj->getName() );
305 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
307 $nt = $userObj->getUserPage();
308 $talk = $userObj->getTalkPage();
311 $tools = self
::getUserLinks( $this, $userObj );
312 $links = Html
::openElement( 'span', [ 'class' => 'mw-changeslist-links' ] );
313 foreach ( $tools as $tool ) {
314 $links .= Html
::rawElement( 'span', [], $tool ) . ' ';
316 $links = trim( $links ) . Html
::closeElement( 'span' );
318 // Show a note if the user is blocked and display the last block log entry.
319 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
320 // and also this will display a totally irrelevant log entry as a current block.
321 if ( !$this->including() ) {
322 // For IP ranges you must give Block::newFromTarget the CIDR string and not a user object.
323 if ( $userObj->isIPRange() ) {
324 $block = Block
::newFromTarget( $userObj->getName(), $userObj->getName() );
326 $block = Block
::newFromTarget( $userObj, $userObj );
329 if ( !is_null( $block ) && $block->getType() != Block
::TYPE_AUTO
) {
330 if ( $block->getType() == Block
::TYPE_RANGE
) {
331 $nt = MWNamespace
::getCanonicalName( NS_USER
) . ':' . $block->getTarget();
334 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
335 LogEventsList
::showLogExtract(
342 'showIfEmpty' => false,
345 'sp-contributions-blocked-notice-anon' :
346 'sp-contributions-blocked-notice',
347 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
349 'offset' => '' # don't use WebRequest parameter offset
356 return Html
::rawElement( 'div', [ 'class' => 'mw-contributions-user-tools' ],
357 $this->msg( 'contributions-subtitle' )->rawParams( $user )->params( $userObj->getName() )
363 * Links to different places.
365 * @note This function is also called in DeletedContributionsPage
366 * @param SpecialPage $sp SpecialPage instance, for context
367 * @param User $target Target user object
370 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
371 $id = $target->getId();
372 $username = $target->getName();
373 $userpage = $target->getUserPage();
374 $talkpage = $target->getTalkPage();
375 $isIP = IP
::isValid( $username );
376 $isRange = IP
::isValidRange( $username );
378 $linkRenderer = $sp->getLinkRenderer();
380 # No talk pages for IP ranges.
382 $tools['user-talk'] = $linkRenderer->makeLink(
384 $sp->msg( 'sp-contributions-talk' )->text()
388 if ( $sp->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
389 if ( $target->getBlock() && $target->getBlock()->getType() != Block
::TYPE_AUTO
) {
390 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
391 SpecialPage
::getTitleFor( 'Block', $username ),
392 $sp->msg( 'change-blocklink' )->text()
394 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
395 SpecialPage
::getTitleFor( 'Unblock', $username ),
396 $sp->msg( 'unblocklink' )->text()
398 } else { # User is not blocked
399 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
400 SpecialPage
::getTitleFor( 'Block', $username ),
401 $sp->msg( 'blocklink' )->text()
407 $tools['log-block'] = $linkRenderer->makeKnownLink(
408 SpecialPage
::getTitleFor( 'Log', 'block' ),
409 $sp->msg( 'sp-contributions-blocklog' )->text(),
411 [ 'page' => $userpage->getPrefixedText() ]
414 # Suppression log link (T61120)
415 if ( $sp->getUser()->isAllowed( 'suppressionlog' ) ) {
416 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
417 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
418 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
420 [ 'offender' => $username ]
424 # Don't show some links for IP ranges
426 # Uploads: hide if IPs cannot upload (T220674)
427 if ( !$isIP ||
$target->isAllowed( 'upload' ) ) {
428 $tools['uploads'] = $linkRenderer->makeKnownLink(
429 SpecialPage
::getTitleFor( 'Listfiles', $username ),
430 $sp->msg( 'sp-contributions-uploads' )->text()
436 $tools['logs'] = $linkRenderer->makeKnownLink(
437 SpecialPage
::getTitleFor( 'Log', $username ),
438 $sp->msg( 'sp-contributions-logs' )->text()
441 # Add link to deleted user contributions for priviledged users
443 if ( $sp->getUser()->isAllowed( 'deletedhistory' ) ) {
444 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
445 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
446 $sp->msg( 'sp-contributions-deleted', $username )->text()
451 # Add a link to change user rights for privileged users
452 $userrightsPage = new UserrightsPage();
453 $userrightsPage->setContext( $sp->getContext() );
454 if ( $userrightsPage->userCanChangeRights( $target ) ) {
455 $tools['userrights'] = $linkRenderer->makeKnownLink(
456 SpecialPage
::getTitleFor( 'Userrights', $username ),
457 $sp->msg( 'sp-contributions-userrights', $username )->text()
461 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
467 * Generates the namespace selector form with hidden attributes.
468 * @return string HTML fragment
470 protected function getForm() {
471 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
472 if ( !isset( $this->opts
['target'] ) ) {
473 $this->opts
['target'] = '';
475 $this->opts
['target'] = str_replace( '_', ' ', $this->opts
['target'] );
478 if ( !isset( $this->opts
['namespace'] ) ) {
479 $this->opts
['namespace'] = '';
482 if ( !isset( $this->opts
['nsInvert'] ) ) {
483 $this->opts
['nsInvert'] = '';
486 if ( !isset( $this->opts
['associated'] ) ) {
487 $this->opts
['associated'] = false;
490 if ( !isset( $this->opts
['contribs'] ) ) {
491 $this->opts
['contribs'] = 'user';
494 if ( !isset( $this->opts
['start'] ) ) {
495 $this->opts
['start'] = '';
498 if ( !isset( $this->opts
['end'] ) ) {
499 $this->opts
['end'] = '';
502 if ( $this->opts
['contribs'] == 'newbie' ) {
503 $this->opts
['target'] = '';
506 if ( !isset( $this->opts
['tagfilter'] ) ) {
507 $this->opts
['tagfilter'] = '';
510 if ( !isset( $this->opts
['topOnly'] ) ) {
511 $this->opts
['topOnly'] = false;
514 if ( !isset( $this->opts
['newOnly'] ) ) {
515 $this->opts
['newOnly'] = false;
518 if ( !isset( $this->opts
['hideMinor'] ) ) {
519 $this->opts
['hideMinor'] = false;
522 // Modules required only for the form
523 $this->getOutput()->addModules( [
524 'mediawiki.userSuggest',
525 'mediawiki.special.contributions',
527 $this->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
528 $this->getOutput()->enableOOUI();
530 $form = Html
::openElement(
534 'action' => wfScript(),
535 'class' => 'mw-contributions-form'
539 # Add hidden params for tracking except for parameters in $skipParameters
557 foreach ( $this->opts
as $name => $value ) {
558 if ( in_array( $name, $skipParameters ) ) {
561 $form .= "\t" . Html
::hidden( $name, $value ) . "\n";
564 $tagFilter = ChangeTags
::buildTagFilterSelector(
565 $this->opts
['tagfilter'], false, $this->getContext() );
568 $filterSelection = Html
::rawElement(
571 implode( "\u{00A0}", $tagFilter )
574 $filterSelection = Html
::rawElement( 'div', [], '' );
577 $labelNewbies = Xml
::radioLabel(
578 $this->msg( 'sp-contributions-newbies' )->text(),
582 $this->opts
['contribs'] == 'newbie',
583 [ 'class' => 'mw-input' ]
585 $labelUsername = Xml
::radioLabel(
586 $this->msg( 'sp-contributions-username' )->text(),
590 $this->opts
['contribs'] == 'user',
591 [ 'class' => 'mw-input' ]
593 $input = Html
::input(
595 $this->opts
['target'],
598 'id' => 'mw-target-user-or-ip',
602 'mw-ui-input-inline',
603 'mw-autocomplete-user', // used by mediawiki.userSuggest
606 // Only autofocus if target hasn't been specified or in non-newbies mode
607 ( $this->opts
['contribs'] === 'newbie' ||
$this->opts
['target'] )
608 ?
[] : [ 'autofocus' => true ]
612 $targetSelection = Html
::rawElement(
615 $labelNewbies . '<br>' . $labelUsername . ' ' . $input . ' '
618 $hidden = $this->opts
['namespace'] === '' ?
' mw-input-hidden' : '';
619 $namespaceSelection = Xml
::tags(
623 $this->msg( 'namespace' )->text(),
627 Html
::namespaceSelector(
628 [ 'selected' => $this->opts
['namespace'], 'all' => '', 'in-user-lang' => true ],
630 'name' => 'namespace',
632 'class' => 'namespaceselector',
637 [ 'class' => 'mw-input-with-label' . $hidden ],
639 $this->msg( 'invert' )->text(),
642 $this->opts
['nsInvert'],
644 'title' => $this->msg( 'tooltip-invert' )->text(),
645 'class' => 'mw-input'
649 Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' . $hidden ],
651 $this->msg( 'namespace_association' )->text(),
654 $this->opts
['associated'],
656 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
657 'class' => 'mw-input'
665 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
666 $filters[] = Html
::rawElement(
668 [ 'class' => 'mw-input-with-label' ],
670 $this->msg( 'history-show-deleted' )->text(),
672 'mw-show-deleted-only',
673 $this->opts
['deletedOnly'],
674 [ 'class' => 'mw-input' ]
679 $filters[] = Html
::rawElement(
681 [ 'class' => 'mw-input-with-label' ],
683 $this->msg( 'sp-contributions-toponly' )->text(),
686 $this->opts
['topOnly'],
687 [ 'class' => 'mw-input' ]
690 $filters[] = Html
::rawElement(
692 [ 'class' => 'mw-input-with-label' ],
694 $this->msg( 'sp-contributions-newonly' )->text(),
697 $this->opts
['newOnly'],
698 [ 'class' => 'mw-input' ]
701 $filters[] = Html
::rawElement(
703 [ 'class' => 'mw-input-with-label' ],
705 $this->msg( 'sp-contributions-hideminor' )->text(),
707 'mw-hide-minor-edits',
708 $this->opts
['hideMinor'],
709 [ 'class' => 'mw-input' ]
714 'SpecialContributions::getForm::filters',
718 $extraOptions = Html
::rawElement(
721 implode( '', $filters )
724 $dateRangeSelection = Html
::rawElement(
727 Xml
::label( $this->msg( 'date-range-from' )->text(), 'mw-date-start' ) . ' ' .
728 new DateInputWidget( [
730 'id' => 'mw-date-start',
732 'value' => $this->opts
['start'],
733 'longDisplayFormat' => true,
735 Xml
::label( $this->msg( 'date-range-to' )->text(), 'mw-date-end' ) . ' ' .
736 new DateInputWidget( [
738 'id' => 'mw-date-end',
740 'value' => $this->opts
['end'],
741 'longDisplayFormat' => true,
745 $submit = Xml
::tags( 'div', [],
747 $this->msg( 'sp-contributions-submit' )->text(),
748 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
752 $form .= Xml
::fieldset(
753 $this->msg( 'sp-contributions-search' )->text(),
755 $namespaceSelection .
758 $dateRangeSelection .
760 [ 'class' => 'mw-contributions-table' ]
763 $explain = $this->msg( 'sp-contributions-explain' );
764 if ( !$explain->isBlank() ) {
765 $form .= Html
::rawElement(
766 'p', [ 'id' => 'mw-sp-contributions-explain' ], $explain->parse()
770 $form .= Xml
::closeElement( 'form' );
776 * Return an array of subpages beginning with $search that this special page will accept.
778 * @param string $search Prefix to search for
779 * @param int $limit Maximum number of results to return (usually 10)
780 * @param int $offset Number of results to skip (usually 0)
781 * @return string[] Matching subpages
783 public function prefixSearchSubpages( $search, $limit, $offset ) {
784 $user = User
::newFromName( $search );
786 // No prefix suggestion for invalid user
789 // Autocomplete subpage as user list - public to allow caching
790 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
793 protected function getGroupName() {