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 ( MediaWikiServices
::getInstance()
130 ->getPermissionManager()
131 ->userHasRight( $user, 'markbotedits' ) && $request->getBool( 'bot' )
133 $this->opts
['bot'] = '1';
136 $skip = $request->getText( 'offset' ) ||
$request->getText( 'dir' ) == 'prev';
137 # Offset overrides year/month selection
139 $this->opts
['year'] = $request->getVal( 'year' );
140 $this->opts
['month'] = $request->getVal( 'month' );
142 $this->opts
['start'] = $request->getVal( 'start' );
143 $this->opts
['end'] = $request->getVal( 'end' );
145 $this->opts
= ContribsPager
::processDateFilter( $this->opts
);
147 if ( $this->opts
['namespace'] < NS_MAIN
) {
148 $this->getOutput()->wrapWikiMsg(
149 "<div class=\"mw-negative-namespace-not-supported error\">\n\$1\n</div>",
150 [ 'negative-namespace-not-supported' ]
152 $out->addHTML( $this->getForm() );
156 $feedType = $request->getVal( 'feed' );
159 'action' => 'feedcontributions',
162 if ( $this->opts
['topOnly'] ) {
163 $feedParams['toponly'] = true;
165 if ( $this->opts
['newOnly'] ) {
166 $feedParams['newonly'] = true;
168 if ( $this->opts
['hideMinor'] ) {
169 $feedParams['hideminor'] = true;
171 if ( $this->opts
['deletedOnly'] ) {
172 $feedParams['deletedonly'] = true;
174 if ( $this->opts
['tagfilter'] !== '' ) {
175 $feedParams['tagfilter'] = $this->opts
['tagfilter'];
177 if ( $this->opts
['namespace'] !== '' ) {
178 $feedParams['namespace'] = $this->opts
['namespace'];
180 // Don't use year and month for the feed URL, but pass them on if
181 // we redirect to API (if $feedType is specified)
182 if ( $feedType && $this->opts
['year'] !== null ) {
183 $feedParams['year'] = $this->opts
['year'];
185 if ( $feedType && $this->opts
['month'] !== null ) {
186 $feedParams['month'] = $this->opts
['month'];
190 // Maintain some level of backwards compatibility
191 // If people request feeds using the old parameters, redirect to API
192 $feedParams['feedformat'] = $feedType;
193 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
195 $out->redirect( $url, '301' );
200 // Add RSS/atom links
201 $this->addFeedLinks( $feedParams );
203 if ( Hooks
::run( 'SpecialContributionsBeforeMainOutput', [ $id, $userObj, $this ] ) ) {
204 if ( !$this->including() ) {
205 $out->addHTML( $this->getForm() );
207 $pager = new ContribsPager( $this->getContext(), [
209 'namespace' => $this->opts
['namespace'],
210 'tagfilter' => $this->opts
['tagfilter'],
211 'start' => $this->opts
['start'],
212 'end' => $this->opts
['end'],
213 'deletedOnly' => $this->opts
['deletedOnly'],
214 'topOnly' => $this->opts
['topOnly'],
215 'newOnly' => $this->opts
['newOnly'],
216 'hideMinor' => $this->opts
['hideMinor'],
217 'nsInvert' => $this->opts
['nsInvert'],
218 'associated' => $this->opts
['associated'],
219 ], $this->getLinkRenderer() );
221 if ( IP
::isValidRange( $target ) && !$pager->isQueryableRange( $target ) ) {
222 // Valid range, but outside CIDR limit.
223 $limits = $this->getConfig()->get( 'RangeContributionsCIDRLimit' );
224 $limit = $limits[ IP
::isIPv4( $target ) ?
'IPv4' : 'IPv6' ];
225 $out->addWikiMsg( 'sp-contributions-outofrange', $limit );
226 } elseif ( !$pager->getNumRows() ) {
227 $out->addWikiMsg( 'nocontribs', $target );
229 # Show a message about replica DB lag, if applicable
230 $lag = $pager->getDatabase()->getSessionLagStatus()['lag'];
232 $out->showLagWarning( $lag );
235 $output = $pager->getBody();
236 if ( !$this->including() ) {
237 $output = $pager->getNavigationBar() .
239 $pager->getNavigationBar();
241 $out->addHTML( $output );
244 $out->preventClickjacking( $pager->getPreventClickjacking() );
246 # Show the appropriate "footer" message - WHOIS tools, etc.
247 if ( IP
::isValidRange( $target ) ) {
248 $message = 'sp-contributions-footer-anon-range';
249 } elseif ( IP
::isIPAddress( $target ) ) {
250 $message = 'sp-contributions-footer-anon';
251 } elseif ( $userObj->isAnon() ) {
252 // No message for non-existing users
255 $message = 'sp-contributions-footer';
258 if ( $message && !$this->including() && !$this->msg( $message, $target )->isDisabled() ) {
260 "<div class='mw-contributions-footer'>\n$1\n</div>",
261 [ $message, $target ] );
267 * Generates the subheading with links
268 * @param User $userObj User object for the target
269 * @return string Appropriately-escaped HTML to be output literally
270 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
273 protected function contributionsSub( $userObj ) {
274 if ( $userObj->isAnon() ) {
275 // Show a warning message that the user being searched for doesn't exist.
276 // User::isIP returns true for IP address and usemod IPs like '123.123.123.xxx',
277 // but returns false for IP ranges. We don't want to suggest either of these are
278 // valid usernames which we would with the 'contributions-userdoesnotexist' message.
279 if ( !User
::isIP( $userObj->getName() ) && !$userObj->isIPRange() ) {
280 $this->getOutput()->wrapWikiMsg(
281 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
283 'contributions-userdoesnotexist',
284 wfEscapeWikiText( $userObj->getName() ),
287 if ( !$this->including() ) {
288 $this->getOutput()->setStatusCode( 404 );
291 $user = htmlspecialchars( $userObj->getName() );
293 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
295 $nt = $userObj->getUserPage();
296 $talk = $userObj->getTalkPage();
299 $tools = self
::getUserLinks( $this, $userObj );
300 $links = Html
::openElement( 'span', [ 'class' => 'mw-changeslist-links' ] );
301 foreach ( $tools as $tool ) {
302 $links .= Html
::rawElement( 'span', [], $tool ) . ' ';
304 $links = trim( $links ) . Html
::closeElement( 'span' );
306 // Show a note if the user is blocked and display the last block log entry.
307 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
308 // and also this will display a totally irrelevant log entry as a current block.
309 if ( !$this->including() ) {
310 // For IP ranges you must give DatabaseBlock::newFromTarget the CIDR string
311 // and not a user object.
312 if ( $userObj->isIPRange() ) {
313 $block = DatabaseBlock
::newFromTarget( $userObj->getName(), $userObj->getName() );
315 $block = DatabaseBlock
::newFromTarget( $userObj, $userObj );
318 if ( !is_null( $block ) && $block->getType() != DatabaseBlock
::TYPE_AUTO
) {
319 if ( $block->getType() == DatabaseBlock
::TYPE_RANGE
) {
320 $nt = MediaWikiServices
::getInstance()->getNamespaceInfo()->
321 getCanonicalName( NS_USER
) . ':' . $block->getTarget();
324 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
325 LogEventsList
::showLogExtract(
332 'showIfEmpty' => false,
335 'sp-contributions-blocked-notice-anon' :
336 'sp-contributions-blocked-notice',
337 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
339 'offset' => '' # don't use WebRequest parameter offset
346 return Html
::rawElement( 'div', [ 'class' => 'mw-contributions-user-tools' ],
347 $this->msg( 'contributions-subtitle' )->rawParams( $user )->params( $userObj->getName() )
353 * Links to different places.
355 * @note This function is also called in DeletedContributionsPage
356 * @param SpecialPage $sp SpecialPage instance, for context
357 * @param User $target Target user object
360 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
361 $id = $target->getId();
362 $username = $target->getName();
363 $userpage = $target->getUserPage();
364 $talkpage = $target->getTalkPage();
365 $isIP = IP
::isValid( $username );
366 $isRange = IP
::isValidRange( $username );
368 $linkRenderer = $sp->getLinkRenderer();
371 # No talk pages for IP ranges.
373 $tools['user-talk'] = $linkRenderer->makeLink(
375 $sp->msg( 'sp-contributions-talk' )->text()
379 # Block / Change block / Unblock links
380 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
381 if ( $permissionManager->userHasRight( $sp->getUser(), 'block' ) ) {
382 if ( $target->getBlock() && $target->getBlock()->getType() != DatabaseBlock
::TYPE_AUTO
) {
383 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
384 SpecialPage
::getTitleFor( 'Block', $username ),
385 $sp->msg( 'change-blocklink' )->text()
387 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
388 SpecialPage
::getTitleFor( 'Unblock', $username ),
389 $sp->msg( 'unblocklink' )->text()
391 } else { # User is not blocked
392 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
393 SpecialPage
::getTitleFor( 'Block', $username ),
394 $sp->msg( 'blocklink' )->text()
400 $tools['log-block'] = $linkRenderer->makeKnownLink(
401 SpecialPage
::getTitleFor( 'Log', 'block' ),
402 $sp->msg( 'sp-contributions-blocklog' )->text(),
404 [ 'page' => $userpage->getPrefixedText() ]
407 # Suppression log link (T61120)
408 if ( $permissionManager->userHasRight( $sp->getUser(), 'suppressionlog' ) ) {
409 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
410 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
411 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
413 [ 'offender' => $username ]
417 # Don't show some links for IP ranges
419 # Uploads: hide if IPs cannot upload (T220674)
420 if ( !$isIP ||
$permissionManager->userHasRight( $target, 'upload' ) ) {
421 $tools['uploads'] = $linkRenderer->makeKnownLink(
422 SpecialPage
::getTitleFor( 'Listfiles', $username ),
423 $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
436 if ( $permissionManager->userHasRight( $sp->getUser(), '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
['start'] ) ) {
484 $this->opts
['start'] = '';
487 if ( !isset( $this->opts
['end'] ) ) {
488 $this->opts
['end'] = '';
491 if ( !isset( $this->opts
['tagfilter'] ) ) {
492 $this->opts
['tagfilter'] = '';
495 if ( !isset( $this->opts
['topOnly'] ) ) {
496 $this->opts
['topOnly'] = false;
499 if ( !isset( $this->opts
['newOnly'] ) ) {
500 $this->opts
['newOnly'] = false;
503 if ( !isset( $this->opts
['hideMinor'] ) ) {
504 $this->opts
['hideMinor'] = false;
507 // Modules required only for the form
508 $this->getOutput()->addModules( [
509 'mediawiki.userSuggest',
510 'mediawiki.special.contributions',
512 $this->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
513 $this->getOutput()->enableOOUI();
515 $form = Html
::openElement(
519 'action' => wfScript(),
520 'class' => 'mw-contributions-form'
524 # Add hidden params for tracking except for parameters in $skipParameters
542 foreach ( $this->opts
as $name => $value ) {
543 if ( in_array( $name, $skipParameters ) ) {
546 $form .= "\t" . Html
::hidden( $name, $value ) . "\n";
549 $tagFilter = ChangeTags
::buildTagFilterSelector(
550 $this->opts
['tagfilter'], false, $this->getContext() );
553 $filterSelection = Html
::rawElement(
556 implode( "\u{00A0}", $tagFilter )
559 $filterSelection = Html
::rawElement( 'div', [], '' );
562 $labelUsername = Xml
::label(
563 $this->msg( 'sp-contributions-username' )->text(),
564 'mw-target-user-or-ip'
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 ( MediaWikiServices
::getInstance()
637 ->getPermissionManager()
638 ->userHasRight( $this->getUser(), 'deletedhistory' )
640 $filters[] = Html
::rawElement(
642 [ 'class' => 'mw-input-with-label' ],
644 $this->msg( 'history-show-deleted' )->text(),
646 'mw-show-deleted-only',
647 $this->opts
['deletedOnly'],
648 [ 'class' => 'mw-input' ]
653 $filters[] = Html
::rawElement(
655 [ 'class' => 'mw-input-with-label' ],
657 $this->msg( 'sp-contributions-toponly' )->text(),
660 $this->opts
['topOnly'],
661 [ 'class' => 'mw-input' ]
664 $filters[] = Html
::rawElement(
666 [ 'class' => 'mw-input-with-label' ],
668 $this->msg( 'sp-contributions-newonly' )->text(),
671 $this->opts
['newOnly'],
672 [ 'class' => 'mw-input' ]
675 $filters[] = Html
::rawElement(
677 [ 'class' => 'mw-input-with-label' ],
679 $this->msg( 'sp-contributions-hideminor' )->text(),
681 'mw-hide-minor-edits',
682 $this->opts
['hideMinor'],
683 [ 'class' => 'mw-input' ]
688 'SpecialContributions::getForm::filters',
692 $extraOptions = Html
::rawElement(
695 implode( '', $filters )
698 $dateRangeSelection = Html
::rawElement(
701 Xml
::label( $this->msg( 'date-range-from' )->text(), 'mw-date-start' ) . ' ' .
702 new DateInputWidget( [
704 'id' => 'mw-date-start',
706 'value' => $this->opts
['start'],
707 'longDisplayFormat' => true,
709 Xml
::label( $this->msg( 'date-range-to' )->text(), 'mw-date-end' ) . ' ' .
710 new DateInputWidget( [
712 'id' => 'mw-date-end',
714 'value' => $this->opts
['end'],
715 'longDisplayFormat' => true,
719 $submit = Xml
::tags( 'div', [],
721 $this->msg( 'sp-contributions-submit' )->text(),
722 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
726 $form .= Xml
::fieldset(
727 $this->msg( 'sp-contributions-search' )->text(),
729 $namespaceSelection .
732 $dateRangeSelection .
734 [ 'class' => 'mw-contributions-table' ]
737 $explain = $this->msg( 'sp-contributions-explain' );
738 if ( !$explain->isBlank() ) {
739 $form .= Html
::rawElement(
740 'p', [ 'id' => 'mw-sp-contributions-explain' ], $explain->parse()
744 $form .= Xml
::closeElement( 'form' );
750 * Return an array of subpages beginning with $search that this special page will accept.
752 * @param string $search Prefix to search for
753 * @param int $limit Maximum number of results to return (usually 10)
754 * @param int $offset Number of results to skip (usually 0)
755 * @return string[] Matching subpages
757 public function prefixSearchSubpages( $search, $limit, $offset ) {
758 $user = User
::newFromName( $search );
760 // No prefix suggestion for invalid user
763 // Autocomplete subpage as user list - public to allow caching
764 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
767 protected function getGroupName() {