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 'namespace' => $this->opts
['namespace'],
207 'tagfilter' => $this->opts
['tagfilter'],
208 'start' => $this->opts
['start'],
209 'end' => $this->opts
['end'],
210 'deletedOnly' => $this->opts
['deletedOnly'],
211 'topOnly' => $this->opts
['topOnly'],
212 'newOnly' => $this->opts
['newOnly'],
213 'hideMinor' => $this->opts
['hideMinor'],
214 'nsInvert' => $this->opts
['nsInvert'],
215 'associated' => $this->opts
['associated'],
218 if ( IP
::isValidRange( $target ) && !$pager->isQueryableRange( $target ) ) {
219 // Valid range, but outside CIDR limit.
220 $limits = $this->getConfig()->get( 'RangeContributionsCIDRLimit' );
221 $limit = $limits[ IP
::isIPv4( $target ) ?
'IPv4' : 'IPv6' ];
222 $out->addWikiMsg( 'sp-contributions-outofrange', $limit );
223 } elseif ( !$pager->getNumRows() ) {
224 $out->addWikiMsg( 'nocontribs', $target );
226 # Show a message about replica DB lag, if applicable
227 $lag = $pager->getDatabase()->getSessionLagStatus()['lag'];
229 $out->showLagWarning( $lag );
232 $output = $pager->getBody();
233 if ( !$this->including() ) {
234 $output = $pager->getNavigationBar() .
236 $pager->getNavigationBar();
238 $out->addHTML( $output );
241 $out->preventClickjacking( $pager->getPreventClickjacking() );
243 # Show the appropriate "footer" message - WHOIS tools, etc.
244 if ( IP
::isValidRange( $target ) ) {
245 $message = 'sp-contributions-footer-anon-range';
246 } elseif ( IP
::isIPAddress( $target ) ) {
247 $message = 'sp-contributions-footer-anon';
248 } elseif ( $userObj->isAnon() ) {
249 // No message for non-existing users
252 $message = 'sp-contributions-footer';
255 if ( $message && !$this->including() && !$this->msg( $message, $target )->isDisabled() ) {
257 "<div class='mw-contributions-footer'>\n$1\n</div>",
258 [ $message, $target ] );
264 * Generates the subheading with links
265 * @param User $userObj User object for the target
266 * @return string Appropriately-escaped HTML to be output literally
267 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
270 protected function contributionsSub( $userObj ) {
271 if ( $userObj->isAnon() ) {
272 // Show a warning message that the user being searched for doesn't exist.
273 // User::isIP returns true for IP address and usemod IPs like '123.123.123.xxx',
274 // but returns false for IP ranges. We don't want to suggest either of these are
275 // valid usernames which we would with the 'contributions-userdoesnotexist' message.
276 if ( !User
::isIP( $userObj->getName() ) && !$userObj->isIPRange() ) {
277 $this->getOutput()->wrapWikiMsg(
278 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
280 'contributions-userdoesnotexist',
281 wfEscapeWikiText( $userObj->getName() ),
284 if ( !$this->including() ) {
285 $this->getOutput()->setStatusCode( 404 );
288 $user = htmlspecialchars( $userObj->getName() );
290 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
292 $nt = $userObj->getUserPage();
293 $talk = $userObj->getTalkPage();
296 $tools = self
::getUserLinks( $this, $userObj );
297 $links = Html
::openElement( 'span', [ 'class' => 'mw-changeslist-links' ] );
298 foreach ( $tools as $tool ) {
299 $links .= Html
::rawElement( 'span', [], $tool ) . ' ';
301 $links = trim( $links ) . Html
::closeElement( 'span' );
303 // Show a note if the user is blocked and display the last block log entry.
304 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
305 // and also this will display a totally irrelevant log entry as a current block.
306 if ( !$this->including() ) {
307 // For IP ranges you must give DatabaseBlock::newFromTarget the CIDR string
308 // and not a user object.
309 if ( $userObj->isIPRange() ) {
310 $block = DatabaseBlock
::newFromTarget( $userObj->getName(), $userObj->getName() );
312 $block = DatabaseBlock
::newFromTarget( $userObj, $userObj );
315 if ( !is_null( $block ) && $block->getType() != DatabaseBlock
::TYPE_AUTO
) {
316 if ( $block->getType() == DatabaseBlock
::TYPE_RANGE
) {
317 $nt = MediaWikiServices
::getInstance()->getNamespaceInfo()->
318 getCanonicalName( NS_USER
) . ':' . $block->getTarget();
321 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
322 LogEventsList
::showLogExtract(
329 'showIfEmpty' => false,
332 'sp-contributions-blocked-notice-anon' :
333 'sp-contributions-blocked-notice',
334 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
336 'offset' => '' # don't use WebRequest parameter offset
343 return Html
::rawElement( 'div', [ 'class' => 'mw-contributions-user-tools' ],
344 $this->msg( 'contributions-subtitle' )->rawParams( $user )->params( $userObj->getName() )
350 * Links to different places.
352 * @note This function is also called in DeletedContributionsPage
353 * @param SpecialPage $sp SpecialPage instance, for context
354 * @param User $target Target user object
357 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
358 $id = $target->getId();
359 $username = $target->getName();
360 $userpage = $target->getUserPage();
361 $talkpage = $target->getTalkPage();
362 $isIP = IP
::isValid( $username );
363 $isRange = IP
::isValidRange( $username );
365 $linkRenderer = $sp->getLinkRenderer();
367 # No talk pages for IP ranges.
369 $tools['user-talk'] = $linkRenderer->makeLink(
371 $sp->msg( 'sp-contributions-talk' )->text()
375 if ( $sp->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
376 if ( $target->getBlock() && $target->getBlock()->getType() != DatabaseBlock
::TYPE_AUTO
) {
377 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
378 SpecialPage
::getTitleFor( 'Block', $username ),
379 $sp->msg( 'change-blocklink' )->text()
381 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
382 SpecialPage
::getTitleFor( 'Unblock', $username ),
383 $sp->msg( 'unblocklink' )->text()
385 } else { # User is not blocked
386 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
387 SpecialPage
::getTitleFor( 'Block', $username ),
388 $sp->msg( 'blocklink' )->text()
394 $tools['log-block'] = $linkRenderer->makeKnownLink(
395 SpecialPage
::getTitleFor( 'Log', 'block' ),
396 $sp->msg( 'sp-contributions-blocklog' )->text(),
398 [ 'page' => $userpage->getPrefixedText() ]
401 # Suppression log link (T61120)
402 if ( $sp->getUser()->isAllowed( 'suppressionlog' ) ) {
403 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
404 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
405 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
407 [ 'offender' => $username ]
411 # Don't show some links for IP ranges
413 # Uploads: hide if IPs cannot upload (T220674)
414 if ( !$isIP ||
$target->isAllowed( 'upload' ) ) {
415 $tools['uploads'] = $linkRenderer->makeKnownLink(
416 SpecialPage
::getTitleFor( 'Listfiles', $username ),
417 $sp->msg( 'sp-contributions-uploads' )->text()
423 $tools['logs'] = $linkRenderer->makeKnownLink(
424 SpecialPage
::getTitleFor( 'Log', $username ),
425 $sp->msg( 'sp-contributions-logs' )->text()
428 # Add link to deleted user contributions for priviledged users
430 if ( $sp->getUser()->isAllowed( 'deletedhistory' ) ) {
431 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
432 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
433 $sp->msg( 'sp-contributions-deleted', $username )->text()
438 # Add a link to change user rights for privileged users
439 $userrightsPage = new UserrightsPage();
440 $userrightsPage->setContext( $sp->getContext() );
441 if ( $userrightsPage->userCanChangeRights( $target ) ) {
442 $tools['userrights'] = $linkRenderer->makeKnownLink(
443 SpecialPage
::getTitleFor( 'Userrights', $username ),
444 $sp->msg( 'sp-contributions-userrights', $username )->text()
448 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
454 * Generates the namespace selector form with hidden attributes.
455 * @return string HTML fragment
457 protected function getForm() {
458 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
459 if ( !isset( $this->opts
['target'] ) ) {
460 $this->opts
['target'] = '';
462 $this->opts
['target'] = str_replace( '_', ' ', $this->opts
['target'] );
465 if ( !isset( $this->opts
['namespace'] ) ) {
466 $this->opts
['namespace'] = '';
469 if ( !isset( $this->opts
['nsInvert'] ) ) {
470 $this->opts
['nsInvert'] = '';
473 if ( !isset( $this->opts
['associated'] ) ) {
474 $this->opts
['associated'] = false;
477 if ( !isset( $this->opts
['start'] ) ) {
478 $this->opts
['start'] = '';
481 if ( !isset( $this->opts
['end'] ) ) {
482 $this->opts
['end'] = '';
485 if ( !isset( $this->opts
['tagfilter'] ) ) {
486 $this->opts
['tagfilter'] = '';
489 if ( !isset( $this->opts
['topOnly'] ) ) {
490 $this->opts
['topOnly'] = false;
493 if ( !isset( $this->opts
['newOnly'] ) ) {
494 $this->opts
['newOnly'] = false;
497 if ( !isset( $this->opts
['hideMinor'] ) ) {
498 $this->opts
['hideMinor'] = false;
501 // Modules required only for the form
502 $this->getOutput()->addModules( [
503 'mediawiki.userSuggest',
504 'mediawiki.special.contributions',
506 $this->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
507 $this->getOutput()->enableOOUI();
509 $form = Html
::openElement(
513 'action' => wfScript(),
514 'class' => 'mw-contributions-form'
518 # Add hidden params for tracking except for parameters in $skipParameters
536 foreach ( $this->opts
as $name => $value ) {
537 if ( in_array( $name, $skipParameters ) ) {
540 $form .= "\t" . Html
::hidden( $name, $value ) . "\n";
543 $tagFilter = ChangeTags
::buildTagFilterSelector(
544 $this->opts
['tagfilter'], false, $this->getContext() );
547 $filterSelection = Html
::rawElement(
550 implode( "\u{00A0}", $tagFilter )
553 $filterSelection = Html
::rawElement( 'div', [], '' );
556 $labelUsername = Xml
::radioLabel(
557 $this->msg( 'sp-contributions-username' )->text(),
562 [ 'class' => 'mw-input' ]
564 $input = Html
::input(
566 $this->opts
['target'],
569 'id' => 'mw-target-user-or-ip',
573 'mw-ui-input-inline',
574 'mw-autocomplete-user', // used by mediawiki.userSuggest
577 // Only autofocus if target hasn't been specified
578 $this->opts
['target'] ?
[] : [ 'autofocus' => true ]
582 $targetSelection = Html
::rawElement(
585 $labelUsername . ' ' . $input . ' '
588 $hidden = $this->opts
['namespace'] === '' ?
' mw-input-hidden' : '';
589 $namespaceSelection = Xml
::tags(
593 $this->msg( 'namespace' )->text(),
596 Html
::namespaceSelector(
597 [ 'selected' => $this->opts
['namespace'], 'all' => '', 'in-user-lang' => true ],
599 'name' => 'namespace',
601 'class' => 'namespaceselector',
606 [ 'class' => 'mw-input-with-label' . $hidden ],
608 $this->msg( 'invert' )->text(),
611 $this->opts
['nsInvert'],
613 'title' => $this->msg( 'tooltip-invert' )->text(),
614 'class' => 'mw-input'
618 Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' . $hidden ],
620 $this->msg( 'namespace_association' )->text(),
623 $this->opts
['associated'],
625 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
626 'class' => 'mw-input'
634 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
635 $filters[] = Html
::rawElement(
637 [ 'class' => 'mw-input-with-label' ],
639 $this->msg( 'history-show-deleted' )->text(),
641 'mw-show-deleted-only',
642 $this->opts
['deletedOnly'],
643 [ 'class' => 'mw-input' ]
648 $filters[] = Html
::rawElement(
650 [ 'class' => 'mw-input-with-label' ],
652 $this->msg( 'sp-contributions-toponly' )->text(),
655 $this->opts
['topOnly'],
656 [ 'class' => 'mw-input' ]
659 $filters[] = Html
::rawElement(
661 [ 'class' => 'mw-input-with-label' ],
663 $this->msg( 'sp-contributions-newonly' )->text(),
666 $this->opts
['newOnly'],
667 [ 'class' => 'mw-input' ]
670 $filters[] = Html
::rawElement(
672 [ 'class' => 'mw-input-with-label' ],
674 $this->msg( 'sp-contributions-hideminor' )->text(),
676 'mw-hide-minor-edits',
677 $this->opts
['hideMinor'],
678 [ 'class' => 'mw-input' ]
683 'SpecialContributions::getForm::filters',
687 $extraOptions = Html
::rawElement(
690 implode( '', $filters )
693 $dateRangeSelection = Html
::rawElement(
696 Xml
::label( $this->msg( 'date-range-from' )->text(), 'mw-date-start' ) . ' ' .
697 new DateInputWidget( [
699 'id' => 'mw-date-start',
701 'value' => $this->opts
['start'],
702 'longDisplayFormat' => true,
704 Xml
::label( $this->msg( 'date-range-to' )->text(), 'mw-date-end' ) . ' ' .
705 new DateInputWidget( [
707 'id' => 'mw-date-end',
709 'value' => $this->opts
['end'],
710 'longDisplayFormat' => true,
714 $submit = Xml
::tags( 'div', [],
716 $this->msg( 'sp-contributions-submit' )->text(),
717 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
721 $form .= Xml
::fieldset(
722 $this->msg( 'sp-contributions-search' )->text(),
724 $namespaceSelection .
727 $dateRangeSelection .
729 [ 'class' => 'mw-contributions-table' ]
732 $explain = $this->msg( 'sp-contributions-explain' );
733 if ( !$explain->isBlank() ) {
734 $form .= Html
::rawElement(
735 'p', [ 'id' => 'mw-sp-contributions-explain' ], $explain->parse()
739 $form .= Xml
::closeElement( 'form' );
745 * Return an array of subpages beginning with $search that this special page will accept.
747 * @param string $search Prefix to search for
748 * @param int $limit Maximum number of results to return (usually 10)
749 * @param int $offset Number of results to skip (usually 0)
750 * @return string[] Matching subpages
752 public function prefixSearchSubpages( $search, $limit, $offset ) {
753 $user = User
::newFromName( $search );
755 // No prefix suggestion for invalid user
758 // Autocomplete subpage as user list - public to allow caching
759 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
762 protected function getGroupName() {