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 $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 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 'contribs' => $this->opts
['contribs'],
218 'namespace' => $this->opts
['namespace'],
219 'tagfilter' => $this->opts
['tagfilter'],
220 'start' => $this->opts
['start'],
221 'end' => $this->opts
['end'],
222 'deletedOnly' => $this->opts
['deletedOnly'],
223 'topOnly' => $this->opts
['topOnly'],
224 'newOnly' => $this->opts
['newOnly'],
225 'hideMinor' => $this->opts
['hideMinor'],
226 'nsInvert' => $this->opts
['nsInvert'],
227 'associated' => $this->opts
['associated'],
230 if ( IP
::isValidRange( $target ) && !$pager->isQueryableRange( $target ) ) {
231 // Valid range, but outside CIDR limit.
232 $limits = $this->getConfig()->get( 'RangeContributionsCIDRLimit' );
233 $limit = $limits[ IP
::isIPv4( $target ) ?
'IPv4' : 'IPv6' ];
234 $out->addWikiMsg( 'sp-contributions-outofrange', $limit );
235 } elseif ( !$pager->getNumRows() ) {
236 $out->addWikiMsg( 'nocontribs', $target );
238 # Show a message about replica DB lag, if applicable
239 $lag = $pager->getDatabase()->getSessionLagStatus()['lag'];
241 $out->showLagWarning( $lag );
244 $output = $pager->getBody();
245 if ( !$this->including() ) {
246 $output = '<p>' . $pager->getNavigationBar() . '</p>' .
248 '<p>' . $pager->getNavigationBar() . '</p>';
250 $out->addHTML( $output );
253 $out->preventClickjacking( $pager->getPreventClickjacking() );
255 # Show the appropriate "footer" message - WHOIS tools, etc.
256 if ( $this->opts
['contribs'] == 'newbie' ) {
257 $message = 'sp-contributions-footer-newbies';
258 } elseif ( IP
::isValidRange( $target ) ) {
259 $message = 'sp-contributions-footer-anon-range';
260 } elseif ( IP
::isIPAddress( $target ) ) {
261 $message = 'sp-contributions-footer-anon';
262 } elseif ( $userObj->isAnon() ) {
263 // No message for non-existing users
266 $message = 'sp-contributions-footer';
270 if ( !$this->including() ) {
271 if ( !$this->msg( $message, $target )->isDisabled() ) {
273 "<div class='mw-contributions-footer'>\n$1\n</div>",
274 [ $message, $target ] );
282 * Generates the subheading with links
283 * @param User $userObj User object for the target
284 * @return string Appropriately-escaped HTML to be output literally
285 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
288 protected function contributionsSub( $userObj ) {
289 if ( $userObj->isAnon() ) {
290 // Show a warning message that the user being searched for doesn't exists.
291 // User::isIP returns true for IP address and usemod IPs like '123.123.123.xxx',
292 // but returns false for IP ranges. We don't want to suggest either of these are
293 // valid usernames which we would with the 'contributions-userdoesnotexist' message.
294 if ( !User
::isIP( $userObj->getName() ) && !$userObj->isIPRange() ) {
295 $this->getOutput()->wrapWikiMsg(
296 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
298 'contributions-userdoesnotexist',
299 wfEscapeWikiText( $userObj->getName() ),
302 if ( !$this->including() ) {
303 $this->getOutput()->setStatusCode( 404 );
306 $user = htmlspecialchars( $userObj->getName() );
308 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
310 $nt = $userObj->getUserPage();
311 $talk = $userObj->getTalkPage();
314 $tools = self
::getUserLinks( $this, $userObj );
315 $links = $this->getLanguage()->pipeList( $tools );
317 // Show a note if the user is blocked and display the last block log entry.
318 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
319 // and also this will display a totally irrelevant log entry as a current block.
320 if ( !$this->including() ) {
321 // For IP ranges you must give Block::newFromTarget the CIDR string and not a user object.
322 if ( $userObj->isIPRange() ) {
323 $block = Block
::newFromTarget( $userObj->getName(), $userObj->getName() );
325 $block = Block
::newFromTarget( $userObj, $userObj );
328 if ( !is_null( $block ) && $block->getType() != Block
::TYPE_AUTO
) {
329 if ( $block->getType() == Block
::TYPE_RANGE
) {
330 $nt = MWNamespace
::getCanonicalName( NS_USER
) . ':' . $block->getTarget();
333 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
334 LogEventsList
::showLogExtract(
341 'showIfEmpty' => false,
344 'sp-contributions-blocked-notice-anon' :
345 'sp-contributions-blocked-notice',
346 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
348 'offset' => '' # don't use WebRequest parameter offset
355 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
359 * Links to different places.
361 * @note This function is also called in DeletedContributionsPage
362 * @param SpecialPage $sp SpecialPage instance, for context
363 * @param User $target Target user object
366 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
367 $id = $target->getId();
368 $username = $target->getName();
369 $userpage = $target->getUserPage();
370 $talkpage = $target->getTalkPage();
372 $linkRenderer = $sp->getLinkRenderer();
374 # No talk pages for IP ranges.
375 if ( !IP
::isValidRange( $username ) ) {
376 $tools['user-talk'] = $linkRenderer->makeLink(
378 $sp->msg( 'sp-contributions-talk' )->text()
382 if ( ( $id !== null ) ||
( $id === null && IP
::isIPAddress( $username ) ) ) {
383 if ( $sp->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
384 if ( $target->isBlocked() && $target->getBlock()->getType() != Block
::TYPE_AUTO
) {
385 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
386 SpecialPage
::getTitleFor( 'Block', $username ),
387 $sp->msg( 'change-blocklink' )->text()
389 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
390 SpecialPage
::getTitleFor( 'Unblock', $username ),
391 $sp->msg( 'unblocklink' )->text()
393 } else { # User is not blocked
394 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
395 SpecialPage
::getTitleFor( 'Block', $username ),
396 $sp->msg( 'blocklink' )->text()
402 $tools['log-block'] = $linkRenderer->makeKnownLink(
403 SpecialPage
::getTitleFor( 'Log', 'block' ),
404 $sp->msg( 'sp-contributions-blocklog' )->text(),
406 [ 'page' => $userpage->getPrefixedText() ]
409 # Suppression log link (T61120)
410 if ( $sp->getUser()->isAllowed( 'suppressionlog' ) ) {
411 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
412 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
413 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
415 [ 'offender' => $username ]
420 # Don't show some links for IP ranges
421 if ( !IP
::isValidRange( $username ) ) {
423 $tools['uploads'] = $linkRenderer->makeKnownLink(
424 SpecialPage
::getTitleFor( 'Listfiles', $username ),
425 $sp->msg( 'sp-contributions-uploads' )->text()
429 $tools['logs'] = $linkRenderer->makeKnownLink(
430 SpecialPage
::getTitleFor( 'Log', $username ),
431 $sp->msg( 'sp-contributions-logs' )->text()
434 # Add link to deleted user contributions for priviledged users
435 if ( $sp->getUser()->isAllowed( 'deletedhistory' ) ) {
436 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
437 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
438 $sp->msg( 'sp-contributions-deleted', $username )->text()
443 # Add a link to change user rights for privileged users
444 $userrightsPage = new UserrightsPage();
445 $userrightsPage->setContext( $sp->getContext() );
446 if ( $userrightsPage->userCanChangeRights( $target ) ) {
447 $tools['userrights'] = $linkRenderer->makeKnownLink(
448 SpecialPage
::getTitleFor( 'Userrights', $username ),
449 $sp->msg( 'sp-contributions-userrights', $username )->text()
453 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
459 * Generates the namespace selector form with hidden attributes.
460 * @return string HTML fragment
462 protected function getForm() {
463 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
464 if ( !isset( $this->opts
['target'] ) ) {
465 $this->opts
['target'] = '';
467 $this->opts
['target'] = str_replace( '_', ' ', $this->opts
['target'] );
470 if ( !isset( $this->opts
['namespace'] ) ) {
471 $this->opts
['namespace'] = '';
474 if ( !isset( $this->opts
['nsInvert'] ) ) {
475 $this->opts
['nsInvert'] = '';
478 if ( !isset( $this->opts
['associated'] ) ) {
479 $this->opts
['associated'] = false;
482 if ( !isset( $this->opts
['contribs'] ) ) {
483 $this->opts
['contribs'] = 'user';
486 if ( !isset( $this->opts
['start'] ) ) {
487 $this->opts
['start'] = '';
490 if ( !isset( $this->opts
['end'] ) ) {
491 $this->opts
['end'] = '';
494 if ( $this->opts
['contribs'] == 'newbie' ) {
495 $this->opts
['target'] = '';
498 if ( !isset( $this->opts
['tagfilter'] ) ) {
499 $this->opts
['tagfilter'] = '';
502 if ( !isset( $this->opts
['topOnly'] ) ) {
503 $this->opts
['topOnly'] = false;
506 if ( !isset( $this->opts
['newOnly'] ) ) {
507 $this->opts
['newOnly'] = false;
510 if ( !isset( $this->opts
['hideMinor'] ) ) {
511 $this->opts
['hideMinor'] = false;
514 // Modules required only for the form
515 $this->getOutput()->addModules( [
516 'mediawiki.userSuggest',
517 'mediawiki.special.contributions',
519 $this->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
520 $this->getOutput()->enableOOUI();
522 $form = Html
::openElement(
526 'action' => wfScript(),
527 'class' => 'mw-contributions-form'
531 # Add hidden params for tracking except for parameters in $skipParameters
549 foreach ( $this->opts
as $name => $value ) {
550 if ( in_array( $name, $skipParameters ) ) {
553 $form .= "\t" . Html
::hidden( $name, $value ) . "\n";
556 $tagFilter = ChangeTags
::buildTagFilterSelector(
557 $this->opts
['tagfilter'], false, $this->getContext() );
560 $filterSelection = Html
::rawElement(
563 implode( "\u{00A0}", $tagFilter )
566 $filterSelection = Html
::rawElement( 'div', [], '' );
569 $labelNewbies = Xml
::radioLabel(
570 $this->msg( 'sp-contributions-newbies' )->text(),
574 $this->opts
['contribs'] == 'newbie',
575 [ 'class' => 'mw-input' ]
577 $labelUsername = Xml
::radioLabel(
578 $this->msg( 'sp-contributions-username' )->text(),
582 $this->opts
['contribs'] == 'user',
583 [ 'class' => 'mw-input' ]
585 $input = Html
::input(
587 $this->opts
['target'],
593 'mw-ui-input-inline',
594 'mw-autocomplete-user', // used by mediawiki.userSuggest
597 // Only autofocus if target hasn't been specified or in non-newbies mode
598 ( $this->opts
['contribs'] === 'newbie' ||
$this->opts
['target'] )
599 ?
[] : [ 'autofocus' => true ]
603 $targetSelection = Html
::rawElement(
606 $labelNewbies . '<br>' . $labelUsername . ' ' . $input . ' '
609 $namespaceSelection = Xml
::tags(
613 $this->msg( 'namespace' )->text(),
617 Html
::namespaceSelector(
618 [ 'selected' => $this->opts
['namespace'], 'all' => '', 'in-user-lang' => true ],
620 'name' => 'namespace',
622 'class' => 'namespaceselector',
627 [ 'class' => 'mw-input-with-label' ],
629 $this->msg( 'invert' )->text(),
632 $this->opts
['nsInvert'],
634 'title' => $this->msg( 'tooltip-invert' )->text(),
635 'class' => 'mw-input'
639 Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' ],
641 $this->msg( 'namespace_association' )->text(),
644 $this->opts
['associated'],
646 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
647 'class' => 'mw-input'
655 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
656 $filters[] = Html
::rawElement(
658 [ 'class' => 'mw-input-with-label' ],
660 $this->msg( 'history-show-deleted' )->text(),
662 'mw-show-deleted-only',
663 $this->opts
['deletedOnly'],
664 [ 'class' => 'mw-input' ]
669 $filters[] = Html
::rawElement(
671 [ 'class' => 'mw-input-with-label' ],
673 $this->msg( 'sp-contributions-toponly' )->text(),
676 $this->opts
['topOnly'],
677 [ 'class' => 'mw-input' ]
680 $filters[] = Html
::rawElement(
682 [ 'class' => 'mw-input-with-label' ],
684 $this->msg( 'sp-contributions-newonly' )->text(),
687 $this->opts
['newOnly'],
688 [ 'class' => 'mw-input' ]
691 $filters[] = Html
::rawElement(
693 [ 'class' => 'mw-input-with-label' ],
695 $this->msg( 'sp-contributions-hideminor' )->text(),
697 'mw-hide-minor-edits',
698 $this->opts
['hideMinor'],
699 [ 'class' => 'mw-input' ]
704 'SpecialContributions::getForm::filters',
708 $extraOptions = Html
::rawElement(
711 implode( '', $filters )
714 $dateRangeSelection = Html
::rawElement(
717 Xml
::label( $this->msg( 'date-range-from' )->text(), 'mw-date-start' ) . ' ' .
718 new DateInputWidget( [
720 'id' => 'mw-date-start',
722 'value' => $this->opts
['start'],
723 'longDisplayFormat' => true,
725 Xml
::label( $this->msg( 'date-range-to' )->text(), 'mw-date-end' ) . ' ' .
726 new DateInputWidget( [
728 'id' => 'mw-date-end',
730 'value' => $this->opts
['end'],
731 'longDisplayFormat' => true,
735 $submit = Xml
::tags( 'div', [],
737 $this->msg( 'sp-contributions-submit' )->text(),
738 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
742 $form .= Xml
::fieldset(
743 $this->msg( 'sp-contributions-search' )->text(),
745 $namespaceSelection .
748 $dateRangeSelection .
750 [ 'class' => 'mw-contributions-table' ]
753 $explain = $this->msg( 'sp-contributions-explain' );
754 if ( !$explain->isBlank() ) {
755 $form .= Html
::rawElement(
756 'p', [ 'id' => 'mw-sp-contributions-explain' ], $explain->parse()
760 $form .= Xml
::closeElement( 'form' );
766 * Return an array of subpages beginning with $search that this special page will accept.
768 * @param string $search Prefix to search for
769 * @param int $limit Maximum number of results to return (usually 10)
770 * @param int $offset Number of results to skip (usually 0)
771 * @return string[] Matching subpages
773 public function prefixSearchSubpages( $search, $limit, $offset ) {
774 $user = User
::newFromName( $search );
776 // No prefix suggestion for invalid user
779 // Autocomplete subpage as user list - public to allow caching
780 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
783 protected function getGroupName() {