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 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 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
240 $lag = $lb->safeGetLag( $pager->getDatabase() );
242 $out->showLagWarning( $lag );
245 $output = $pager->getBody();
246 if ( !$this->including() ) {
247 $output = '<p>' . $pager->getNavigationBar() . '</p>' .
249 '<p>' . $pager->getNavigationBar() . '</p>';
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';
271 if ( !$this->including() ) {
272 if ( !$this->msg( $message, $target )->isDisabled() ) {
274 "<div class='mw-contributions-footer'>\n$1\n</div>",
275 [ $message, $target ] );
283 * Generates the subheading with links
284 * @param User $userObj User object for the target
285 * @return string Appropriately-escaped HTML to be output literally
286 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
289 protected function contributionsSub( $userObj ) {
290 if ( $userObj->isAnon() ) {
291 // Show a warning message that the user being searched for doesn't exists.
292 // User::isIP returns true for IP address and usemod IPs like '123.123.123.xxx',
293 // but returns false for IP ranges. We don't want to suggest either of these are
294 // valid usernames which we would with the 'contributions-userdoesnotexist' message.
295 if ( !User
::isIP( $userObj->getName() ) && !$userObj->isIPRange() ) {
296 $this->getOutput()->wrapWikiMsg(
297 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
299 'contributions-userdoesnotexist',
300 wfEscapeWikiText( $userObj->getName() ),
303 if ( !$this->including() ) {
304 $this->getOutput()->setStatusCode( 404 );
307 $user = htmlspecialchars( $userObj->getName() );
309 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
311 $nt = $userObj->getUserPage();
312 $talk = $userObj->getTalkPage();
315 $tools = self
::getUserLinks( $this, $userObj );
316 $links = $this->getLanguage()->pipeList( $tools );
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 $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
360 * Links to different places.
362 * @note This function is also called in DeletedContributionsPage
363 * @param SpecialPage $sp SpecialPage instance, for context
364 * @param User $target Target user object
367 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
368 $id = $target->getId();
369 $username = $target->getName();
370 $userpage = $target->getUserPage();
371 $talkpage = $target->getTalkPage();
373 $linkRenderer = $sp->getLinkRenderer();
375 # No talk pages for IP ranges.
376 if ( !IP
::isValidRange( $username ) ) {
377 $tools['user-talk'] = $linkRenderer->makeLink(
379 $sp->msg( 'sp-contributions-talk' )->text()
383 if ( ( $id !== null ) ||
( $id === null && IP
::isIPAddress( $username ) ) ) {
384 if ( $sp->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
385 if ( $target->isBlocked() && $target->getBlock()->getType() != Block
::TYPE_AUTO
) {
386 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
387 SpecialPage
::getTitleFor( 'Block', $username ),
388 $sp->msg( 'change-blocklink' )->text()
390 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
391 SpecialPage
::getTitleFor( 'Unblock', $username ),
392 $sp->msg( 'unblocklink' )->text()
394 } else { # User is not blocked
395 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
396 SpecialPage
::getTitleFor( 'Block', $username ),
397 $sp->msg( 'blocklink' )->text()
403 $tools['log-block'] = $linkRenderer->makeKnownLink(
404 SpecialPage
::getTitleFor( 'Log', 'block' ),
405 $sp->msg( 'sp-contributions-blocklog' )->text(),
407 [ 'page' => $userpage->getPrefixedText() ]
410 # Suppression log link (T61120)
411 if ( $sp->getUser()->isAllowed( 'suppressionlog' ) ) {
412 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
413 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
414 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
416 [ 'offender' => $username ]
421 # Don't show some links for IP ranges
422 if ( !IP
::isValidRange( $username ) ) {
424 $tools['uploads'] = $linkRenderer->makeKnownLink(
425 SpecialPage
::getTitleFor( 'Listfiles', $username ),
426 $sp->msg( 'sp-contributions-uploads' )->text()
430 $tools['logs'] = $linkRenderer->makeKnownLink(
431 SpecialPage
::getTitleFor( 'Log', $username ),
432 $sp->msg( 'sp-contributions-logs' )->text()
435 # Add link to deleted user contributions for priviledged users
436 if ( $sp->getUser()->isAllowed( 'deletedhistory' ) ) {
437 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
438 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
439 $sp->msg( 'sp-contributions-deleted', $username )->text()
444 # Add a link to change user rights for privileged users
445 $userrightsPage = new UserrightsPage();
446 $userrightsPage->setContext( $sp->getContext() );
447 if ( $userrightsPage->userCanChangeRights( $target ) ) {
448 $tools['userrights'] = $linkRenderer->makeKnownLink(
449 SpecialPage
::getTitleFor( 'Userrights', $username ),
450 $sp->msg( 'sp-contributions-userrights', $username )->text()
454 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
460 * Generates the namespace selector form with hidden attributes.
461 * @return string HTML fragment
463 protected function getForm() {
464 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
465 if ( !isset( $this->opts
['target'] ) ) {
466 $this->opts
['target'] = '';
468 $this->opts
['target'] = str_replace( '_', ' ', $this->opts
['target'] );
471 if ( !isset( $this->opts
['namespace'] ) ) {
472 $this->opts
['namespace'] = '';
475 if ( !isset( $this->opts
['nsInvert'] ) ) {
476 $this->opts
['nsInvert'] = '';
479 if ( !isset( $this->opts
['associated'] ) ) {
480 $this->opts
['associated'] = false;
483 if ( !isset( $this->opts
['contribs'] ) ) {
484 $this->opts
['contribs'] = 'user';
487 if ( !isset( $this->opts
['start'] ) ) {
488 $this->opts
['start'] = '';
491 if ( !isset( $this->opts
['end'] ) ) {
492 $this->opts
['end'] = '';
495 if ( $this->opts
['contribs'] == 'newbie' ) {
496 $this->opts
['target'] = '';
499 if ( !isset( $this->opts
['tagfilter'] ) ) {
500 $this->opts
['tagfilter'] = '';
503 if ( !isset( $this->opts
['topOnly'] ) ) {
504 $this->opts
['topOnly'] = false;
507 if ( !isset( $this->opts
['newOnly'] ) ) {
508 $this->opts
['newOnly'] = false;
511 if ( !isset( $this->opts
['hideMinor'] ) ) {
512 $this->opts
['hideMinor'] = false;
515 // Modules required only for the form
516 $this->getOutput()->addModules( [
517 'mediawiki.userSuggest',
518 'mediawiki.special.contributions',
520 $this->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
521 $this->getOutput()->enableOOUI();
523 $form = Html
::openElement(
527 'action' => wfScript(),
528 'class' => 'mw-contributions-form'
532 # Add hidden params for tracking except for parameters in $skipParameters
550 foreach ( $this->opts
as $name => $value ) {
551 if ( in_array( $name, $skipParameters ) ) {
554 $form .= "\t" . Html
::hidden( $name, $value ) . "\n";
557 $tagFilter = ChangeTags
::buildTagFilterSelector(
558 $this->opts
['tagfilter'], false, $this->getContext() );
561 $filterSelection = Html
::rawElement(
564 implode( "\u{00A0}", $tagFilter )
567 $filterSelection = Html
::rawElement( 'div', [], '' );
570 $labelNewbies = Xml
::radioLabel(
571 $this->msg( 'sp-contributions-newbies' )->text(),
575 $this->opts
['contribs'] == 'newbie',
576 [ 'class' => 'mw-input' ]
578 $labelUsername = Xml
::radioLabel(
579 $this->msg( 'sp-contributions-username' )->text(),
583 $this->opts
['contribs'] == 'user',
584 [ 'class' => 'mw-input' ]
586 $input = Html
::input(
588 $this->opts
['target'],
594 'mw-ui-input-inline',
595 'mw-autocomplete-user', // used by mediawiki.userSuggest
598 // Only autofocus if target hasn't been specified or in non-newbies mode
599 ( $this->opts
['contribs'] === 'newbie' ||
$this->opts
['target'] )
600 ?
[] : [ 'autofocus' => true ]
604 $targetSelection = Html
::rawElement(
607 $labelNewbies . '<br>' . $labelUsername . ' ' . $input . ' '
610 $namespaceSelection = Xml
::tags(
614 $this->msg( 'namespace' )->text(),
618 Html
::namespaceSelector(
619 [ 'selected' => $this->opts
['namespace'], 'all' => '', 'in-user-lang' => true ],
621 'name' => 'namespace',
623 'class' => 'namespaceselector',
628 [ 'class' => 'mw-input-with-label' ],
630 $this->msg( 'invert' )->text(),
633 $this->opts
['nsInvert'],
635 'title' => $this->msg( 'tooltip-invert' )->text(),
636 'class' => 'mw-input'
640 Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' ],
642 $this->msg( 'namespace_association' )->text(),
645 $this->opts
['associated'],
647 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
648 'class' => 'mw-input'
656 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
657 $filters[] = Html
::rawElement(
659 [ 'class' => 'mw-input-with-label' ],
661 $this->msg( 'history-show-deleted' )->text(),
663 'mw-show-deleted-only',
664 $this->opts
['deletedOnly'],
665 [ 'class' => 'mw-input' ]
670 $filters[] = Html
::rawElement(
672 [ 'class' => 'mw-input-with-label' ],
674 $this->msg( 'sp-contributions-toponly' )->text(),
677 $this->opts
['topOnly'],
678 [ 'class' => 'mw-input' ]
681 $filters[] = Html
::rawElement(
683 [ 'class' => 'mw-input-with-label' ],
685 $this->msg( 'sp-contributions-newonly' )->text(),
688 $this->opts
['newOnly'],
689 [ 'class' => 'mw-input' ]
692 $filters[] = Html
::rawElement(
694 [ 'class' => 'mw-input-with-label' ],
696 $this->msg( 'sp-contributions-hideminor' )->text(),
698 'mw-hide-minor-edits',
699 $this->opts
['hideMinor'],
700 [ 'class' => 'mw-input' ]
705 'SpecialContributions::getForm::filters',
709 $extraOptions = Html
::rawElement(
712 implode( '', $filters )
715 $dateRangeSelection = Html
::rawElement(
718 Xml
::label( $this->msg( 'date-range-from' )->text(), 'mw-date-start' ) . ' ' .
719 new DateInputWidget( [
721 'id' => 'mw-date-start',
723 'value' => $this->opts
['start'],
724 'longDisplayFormat' => true,
726 Xml
::label( $this->msg( 'date-range-to' )->text(), 'mw-date-end' ) . ' ' .
727 new DateInputWidget( [
729 'id' => 'mw-date-end',
731 'value' => $this->opts
['end'],
732 'longDisplayFormat' => true,
736 $submit = Xml
::tags( 'div', [],
738 $this->msg( 'sp-contributions-submit' )->text(),
739 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
743 $form .= Xml
::fieldset(
744 $this->msg( 'sp-contributions-search' )->text(),
746 $namespaceSelection .
749 $dateRangeSelection .
751 [ 'class' => 'mw-contributions-table' ]
754 $explain = $this->msg( 'sp-contributions-explain' );
755 if ( !$explain->isBlank() ) {
756 $form .= Html
::rawElement(
757 'p', [ 'id' => 'mw-sp-contributions-explain' ], $explain->parse()
761 $form .= Xml
::closeElement( 'form' );
767 * Return an array of subpages beginning with $search that this special page will accept.
769 * @param string $search Prefix to search for
770 * @param int $limit Maximum number of results to return (usually 10)
771 * @param int $offset Number of results to skip (usually 0)
772 * @return string[] Matching subpages
774 public function prefixSearchSubpages( $search, $limit, $offset ) {
775 $user = User
::newFromName( $search );
777 // No prefix suggestion for invalid user
780 // Autocomplete subpage as user list - public to allow caching
781 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
784 protected function getGroupName() {