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 $this->opts
['deletedOnly'] = $request->getBool( 'deletedOnly' );
60 if ( !strlen( $target ) ) {
61 if ( !$this->including() ) {
62 $out->addHTML( $this->getForm() );
68 $user = $this->getUser();
70 $this->opts
['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
71 $this->opts
['target'] = $target;
72 $this->opts
['topOnly'] = $request->getBool( 'topOnly' );
73 $this->opts
['newOnly'] = $request->getBool( 'newOnly' );
74 $this->opts
['hideMinor'] = $request->getBool( 'hideMinor' );
77 if ( ExternalUserNames
::isExternal( $target ) ) {
78 $userObj = User
::newFromName( $target, false );
80 $out->addHTML( $this->getForm() );
84 $out->addSubtitle( $this->contributionsSub( $userObj ) );
85 $out->setHTMLTitle( $this->msg(
87 $this->msg( 'contributions-title', $target )->plain()
88 )->inContentLanguage() );
90 $nt = Title
::makeTitleSafe( NS_USER
, $target );
92 $out->addHTML( $this->getForm() );
95 $userObj = User
::newFromName( $nt->getText(), false );
97 $out->addHTML( $this->getForm() );
100 $id = $userObj->getId();
102 $target = $nt->getText();
103 $out->addSubtitle( $this->contributionsSub( $userObj ) );
104 $out->setHTMLTitle( $this->msg(
106 $this->msg( 'contributions-title', $target )->plain()
107 )->inContentLanguage() );
109 # For IP ranges, we want the contributionsSub, but not the skin-dependent
110 # links under 'Tools', which may include irrelevant links like 'Logs'.
111 if ( !IP
::isValidRange( $target ) ) {
112 $this->getSkin()->setRelevantUser( $userObj );
116 $ns = $request->getVal( 'namespace', null );
117 if ( $ns !== null && $ns !== '' ) {
118 $this->opts
['namespace'] = intval( $ns );
120 $this->opts
['namespace'] = '';
123 $this->opts
['associated'] = $request->getBool( 'associated' );
124 $this->opts
['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
125 $this->opts
['tagfilter'] = (string)$request->getVal( 'tagfilter' );
127 // Allows reverts to have the bot flag in recent changes. It is just here to
128 // be passed in the form at the top of the page
129 if ( $user->isAllowed( 'markbotedits' ) && $request->getBool( 'bot' ) ) {
130 $this->opts
['bot'] = '1';
133 $skip = $request->getText( 'offset' ) ||
$request->getText( 'dir' ) == 'prev';
134 # Offset overrides year/month selection
136 $this->opts
['year'] = $request->getVal( 'year' );
137 $this->opts
['month'] = $request->getVal( 'month' );
139 $this->opts
['start'] = $request->getVal( 'start' );
140 $this->opts
['end'] = $request->getVal( 'end' );
142 $this->opts
= ContribsPager
::processDateFilter( $this->opts
);
144 if ( $this->opts
['namespace'] < NS_MAIN
) {
145 $this->getOutput()->wrapWikiMsg(
146 "<div class=\"mw-negative-namespace-not-supported error\">\n\$1\n</div>",
147 [ 'negative-namespace-not-supported' ]
149 $out->addHTML( $this->getForm() );
153 $feedType = $request->getVal( 'feed' );
156 'action' => 'feedcontributions',
159 if ( $this->opts
['topOnly'] ) {
160 $feedParams['toponly'] = true;
162 if ( $this->opts
['newOnly'] ) {
163 $feedParams['newonly'] = true;
165 if ( $this->opts
['hideMinor'] ) {
166 $feedParams['hideminor'] = true;
168 if ( $this->opts
['deletedOnly'] ) {
169 $feedParams['deletedonly'] = true;
171 if ( $this->opts
['tagfilter'] !== '' ) {
172 $feedParams['tagfilter'] = $this->opts
['tagfilter'];
174 if ( $this->opts
['namespace'] !== '' ) {
175 $feedParams['namespace'] = $this->opts
['namespace'];
177 // Don't use year and month for the feed URL, but pass them on if
178 // we redirect to API (if $feedType is specified)
179 if ( $feedType && $this->opts
['year'] !== null ) {
180 $feedParams['year'] = $this->opts
['year'];
182 if ( $feedType && $this->opts
['month'] !== null ) {
183 $feedParams['month'] = $this->opts
['month'];
187 // Maintain some level of backwards compatibility
188 // If people request feeds using the old parameters, redirect to API
189 $feedParams['feedformat'] = $feedType;
190 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
192 $out->redirect( $url, '301' );
197 // Add RSS/atom links
198 $this->addFeedLinks( $feedParams );
200 if ( Hooks
::run( 'SpecialContributionsBeforeMainOutput', [ $id, $userObj, $this ] ) ) {
201 if ( !$this->including() ) {
202 $out->addHTML( $this->getForm() );
204 $pager = new ContribsPager( $this->getContext(), [
206 // Temporary, until newbie feature is fully removed from ContribsPager
207 'contribs' => 'user',
208 'namespace' => $this->opts
['namespace'],
209 'tagfilter' => $this->opts
['tagfilter'],
210 'start' => $this->opts
['start'],
211 'end' => $this->opts
['end'],
212 'deletedOnly' => $this->opts
['deletedOnly'],
213 'topOnly' => $this->opts
['topOnly'],
214 'newOnly' => $this->opts
['newOnly'],
215 'hideMinor' => $this->opts
['hideMinor'],
216 'nsInvert' => $this->opts
['nsInvert'],
217 'associated' => $this->opts
['associated'],
220 if ( IP
::isValidRange( $target ) && !$pager->isQueryableRange( $target ) ) {
221 // Valid range, but outside CIDR limit.
222 $limits = $this->getConfig()->get( 'RangeContributionsCIDRLimit' );
223 $limit = $limits[ IP
::isIPv4( $target ) ?
'IPv4' : 'IPv6' ];
224 $out->addWikiMsg( 'sp-contributions-outofrange', $limit );
225 } elseif ( !$pager->getNumRows() ) {
226 $out->addWikiMsg( 'nocontribs', $target );
228 # Show a message about replica DB lag, if applicable
229 $lag = $pager->getDatabase()->getSessionLagStatus()['lag'];
231 $out->showLagWarning( $lag );
234 $output = $pager->getBody();
235 if ( !$this->including() ) {
236 $output = $pager->getNavigationBar() .
238 $pager->getNavigationBar();
240 $out->addHTML( $output );
243 $out->preventClickjacking( $pager->getPreventClickjacking() );
245 # Show the appropriate "footer" message - WHOIS tools, etc.
246 if ( IP
::isValidRange( $target ) ) {
247 $message = 'sp-contributions-footer-anon-range';
248 } elseif ( IP
::isIPAddress( $target ) ) {
249 $message = 'sp-contributions-footer-anon';
250 } elseif ( $userObj->isAnon() ) {
251 // No message for non-existing users
254 $message = 'sp-contributions-footer';
257 if ( $message && !$this->including() && !$this->msg( $message, $target )->isDisabled() ) {
259 "<div class='mw-contributions-footer'>\n$1\n</div>",
260 [ $message, $target ] );
266 * Generates the subheading with links
267 * @param User $userObj User object for the target
268 * @return string Appropriately-escaped HTML to be output literally
269 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
272 protected function contributionsSub( $userObj ) {
273 if ( $userObj->isAnon() ) {
274 // Show a warning message that the user being searched for doesn't exist.
275 // User::isIP returns true for IP address and usemod IPs like '123.123.123.xxx',
276 // but returns false for IP ranges. We don't want to suggest either of these are
277 // valid usernames which we would with the 'contributions-userdoesnotexist' message.
278 if ( !User
::isIP( $userObj->getName() ) && !$userObj->isIPRange() ) {
279 $this->getOutput()->wrapWikiMsg(
280 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
282 'contributions-userdoesnotexist',
283 wfEscapeWikiText( $userObj->getName() ),
286 if ( !$this->including() ) {
287 $this->getOutput()->setStatusCode( 404 );
290 $user = htmlspecialchars( $userObj->getName() );
292 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
294 $nt = $userObj->getUserPage();
295 $talk = $userObj->getTalkPage();
298 $tools = self
::getUserLinks( $this, $userObj );
299 $links = Html
::openElement( 'span', [ 'class' => 'mw-changeslist-links' ] );
300 foreach ( $tools as $tool ) {
301 $links .= Html
::rawElement( 'span', [], $tool ) . ' ';
303 $links = trim( $links ) . Html
::closeElement( 'span' );
305 // Show a note if the user is blocked and display the last block log entry.
306 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
307 // and also this will display a totally irrelevant log entry as a current block.
308 if ( !$this->including() ) {
309 // For IP ranges you must give DatabaseBlock::newFromTarget the CIDR string
310 // and not a user object.
311 if ( $userObj->isIPRange() ) {
312 $block = DatabaseBlock
::newFromTarget( $userObj->getName(), $userObj->getName() );
314 $block = DatabaseBlock
::newFromTarget( $userObj, $userObj );
317 if ( !is_null( $block ) && $block->getType() != DatabaseBlock
::TYPE_AUTO
) {
318 if ( $block->getType() == DatabaseBlock
::TYPE_RANGE
) {
319 $nt = MediaWikiServices
::getInstance()->getNamespaceInfo()->
320 getCanonicalName( NS_USER
) . ':' . $block->getTarget();
323 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
324 LogEventsList
::showLogExtract(
331 'showIfEmpty' => false,
334 'sp-contributions-blocked-notice-anon' :
335 'sp-contributions-blocked-notice',
336 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
338 'offset' => '' # don't use WebRequest parameter offset
345 return Html
::rawElement( 'div', [ 'class' => 'mw-contributions-user-tools' ],
346 $this->msg( 'contributions-subtitle' )->rawParams( $user )->params( $userObj->getName() )
352 * Links to different places.
354 * @note This function is also called in DeletedContributionsPage
355 * @param SpecialPage $sp SpecialPage instance, for context
356 * @param User $target Target user object
359 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
360 $id = $target->getId();
361 $username = $target->getName();
362 $userpage = $target->getUserPage();
363 $talkpage = $target->getTalkPage();
364 $isIP = IP
::isValid( $username );
365 $isRange = IP
::isValidRange( $username );
367 $linkRenderer = $sp->getLinkRenderer();
369 # No talk pages for IP ranges.
371 $tools['user-talk'] = $linkRenderer->makeLink(
373 $sp->msg( 'sp-contributions-talk' )->text()
377 if ( $sp->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
378 if ( $target->getBlock() && $target->getBlock()->getType() != DatabaseBlock
::TYPE_AUTO
) {
379 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
380 SpecialPage
::getTitleFor( 'Block', $username ),
381 $sp->msg( 'change-blocklink' )->text()
383 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
384 SpecialPage
::getTitleFor( 'Unblock', $username ),
385 $sp->msg( 'unblocklink' )->text()
387 } else { # User is not blocked
388 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
389 SpecialPage
::getTitleFor( 'Block', $username ),
390 $sp->msg( 'blocklink' )->text()
396 $tools['log-block'] = $linkRenderer->makeKnownLink(
397 SpecialPage
::getTitleFor( 'Log', 'block' ),
398 $sp->msg( 'sp-contributions-blocklog' )->text(),
400 [ 'page' => $userpage->getPrefixedText() ]
403 # Suppression log link (T61120)
404 if ( $sp->getUser()->isAllowed( 'suppressionlog' ) ) {
405 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
406 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
407 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
409 [ 'offender' => $username ]
413 # Don't show some links for IP ranges
415 # Uploads: hide if IPs cannot upload (T220674)
416 if ( !$isIP ||
$target->isAllowed( 'upload' ) ) {
417 $tools['uploads'] = $linkRenderer->makeKnownLink(
418 SpecialPage
::getTitleFor( 'Listfiles', $username ),
419 $sp->msg( 'sp-contributions-uploads' )->text()
425 $tools['logs'] = $linkRenderer->makeKnownLink(
426 SpecialPage
::getTitleFor( 'Log', $username ),
427 $sp->msg( 'sp-contributions-logs' )->text()
430 # Add link to deleted user contributions for priviledged users
432 if ( $sp->getUser()->isAllowed( 'deletedhistory' ) ) {
433 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
434 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
435 $sp->msg( 'sp-contributions-deleted', $username )->text()
440 # Add a link to change user rights for privileged users
441 $userrightsPage = new UserrightsPage();
442 $userrightsPage->setContext( $sp->getContext() );
443 if ( $userrightsPage->userCanChangeRights( $target ) ) {
444 $tools['userrights'] = $linkRenderer->makeKnownLink(
445 SpecialPage
::getTitleFor( 'Userrights', $username ),
446 $sp->msg( 'sp-contributions-userrights', $username )->text()
450 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
456 * Generates the namespace selector form with hidden attributes.
457 * @return string HTML fragment
459 protected function getForm() {
460 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
461 if ( !isset( $this->opts
['target'] ) ) {
462 $this->opts
['target'] = '';
464 $this->opts
['target'] = str_replace( '_', ' ', $this->opts
['target'] );
467 if ( !isset( $this->opts
['namespace'] ) ) {
468 $this->opts
['namespace'] = '';
471 if ( !isset( $this->opts
['nsInvert'] ) ) {
472 $this->opts
['nsInvert'] = '';
475 if ( !isset( $this->opts
['associated'] ) ) {
476 $this->opts
['associated'] = false;
479 if ( !isset( $this->opts
['start'] ) ) {
480 $this->opts
['start'] = '';
483 if ( !isset( $this->opts
['end'] ) ) {
484 $this->opts
['end'] = '';
487 if ( !isset( $this->opts
['tagfilter'] ) ) {
488 $this->opts
['tagfilter'] = '';
491 if ( !isset( $this->opts
['topOnly'] ) ) {
492 $this->opts
['topOnly'] = false;
495 if ( !isset( $this->opts
['newOnly'] ) ) {
496 $this->opts
['newOnly'] = false;
499 if ( !isset( $this->opts
['hideMinor'] ) ) {
500 $this->opts
['hideMinor'] = false;
503 // Modules required only for the form
504 $this->getOutput()->addModules( [
505 'mediawiki.userSuggest',
506 'mediawiki.special.contributions',
508 $this->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
509 $this->getOutput()->enableOOUI();
511 $form = Html
::openElement(
515 'action' => wfScript(),
516 'class' => 'mw-contributions-form'
520 # Add hidden params for tracking except for parameters in $skipParameters
538 foreach ( $this->opts
as $name => $value ) {
539 if ( in_array( $name, $skipParameters ) ) {
542 $form .= "\t" . Html
::hidden( $name, $value ) . "\n";
545 $tagFilter = ChangeTags
::buildTagFilterSelector(
546 $this->opts
['tagfilter'], false, $this->getContext() );
549 $filterSelection = Html
::rawElement(
552 implode( "\u{00A0}", $tagFilter )
555 $filterSelection = Html
::rawElement( 'div', [], '' );
558 $labelUsername = Xml
::radioLabel(
559 $this->msg( 'sp-contributions-username' )->text(),
564 [ 'class' => 'mw-input' ]
566 $input = Html
::input(
568 $this->opts
['target'],
571 'id' => 'mw-target-user-or-ip',
575 'mw-ui-input-inline',
576 'mw-autocomplete-user', // used by mediawiki.userSuggest
579 // Only autofocus if target hasn't been specified
580 $this->opts
['target'] ?
[] : [ 'autofocus' => true ]
584 $targetSelection = Html
::rawElement(
587 $labelUsername . ' ' . $input . ' '
590 $hidden = $this->opts
['namespace'] === '' ?
' mw-input-hidden' : '';
591 $namespaceSelection = Xml
::tags(
595 $this->msg( 'namespace' )->text(),
598 Html
::namespaceSelector(
599 [ 'selected' => $this->opts
['namespace'], 'all' => '', 'in-user-lang' => true ],
601 'name' => 'namespace',
603 'class' => 'namespaceselector',
608 [ 'class' => 'mw-input-with-label' . $hidden ],
610 $this->msg( 'invert' )->text(),
613 $this->opts
['nsInvert'],
615 'title' => $this->msg( 'tooltip-invert' )->text(),
616 'class' => 'mw-input'
620 Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' . $hidden ],
622 $this->msg( 'namespace_association' )->text(),
625 $this->opts
['associated'],
627 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
628 'class' => 'mw-input'
636 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
637 $filters[] = Html
::rawElement(
639 [ 'class' => 'mw-input-with-label' ],
641 $this->msg( 'history-show-deleted' )->text(),
643 'mw-show-deleted-only',
644 $this->opts
['deletedOnly'],
645 [ 'class' => 'mw-input' ]
650 $filters[] = Html
::rawElement(
652 [ 'class' => 'mw-input-with-label' ],
654 $this->msg( 'sp-contributions-toponly' )->text(),
657 $this->opts
['topOnly'],
658 [ 'class' => 'mw-input' ]
661 $filters[] = Html
::rawElement(
663 [ 'class' => 'mw-input-with-label' ],
665 $this->msg( 'sp-contributions-newonly' )->text(),
668 $this->opts
['newOnly'],
669 [ 'class' => 'mw-input' ]
672 $filters[] = Html
::rawElement(
674 [ 'class' => 'mw-input-with-label' ],
676 $this->msg( 'sp-contributions-hideminor' )->text(),
678 'mw-hide-minor-edits',
679 $this->opts
['hideMinor'],
680 [ 'class' => 'mw-input' ]
685 'SpecialContributions::getForm::filters',
689 $extraOptions = Html
::rawElement(
692 implode( '', $filters )
695 $dateRangeSelection = Html
::rawElement(
698 Xml
::label( $this->msg( 'date-range-from' )->text(), 'mw-date-start' ) . ' ' .
699 new DateInputWidget( [
701 'id' => 'mw-date-start',
703 'value' => $this->opts
['start'],
704 'longDisplayFormat' => true,
706 Xml
::label( $this->msg( 'date-range-to' )->text(), 'mw-date-end' ) . ' ' .
707 new DateInputWidget( [
709 'id' => 'mw-date-end',
711 'value' => $this->opts
['end'],
712 'longDisplayFormat' => true,
716 $submit = Xml
::tags( 'div', [],
718 $this->msg( 'sp-contributions-submit' )->text(),
719 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
723 $form .= Xml
::fieldset(
724 $this->msg( 'sp-contributions-search' )->text(),
726 $namespaceSelection .
729 $dateRangeSelection .
731 [ 'class' => 'mw-contributions-table' ]
734 $explain = $this->msg( 'sp-contributions-explain' );
735 if ( !$explain->isBlank() ) {
736 $form .= Html
::rawElement(
737 'p', [ 'id' => 'mw-sp-contributions-explain' ], $explain->parse()
741 $form .= Xml
::closeElement( 'form' );
747 * Return an array of subpages beginning with $search that this special page will accept.
749 * @param string $search Prefix to search for
750 * @param int $limit Maximum number of results to return (usually 10)
751 * @param int $offset Number of results to skip (usually 0)
752 * @return string[] Matching subpages
754 public function prefixSearchSubpages( $search, $limit, $offset ) {
755 $user = User
::newFromName( $search );
757 // No prefix suggestion for invalid user
760 // Autocomplete subpage as user list - public to allow caching
761 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
764 protected function getGroupName() {