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( [
45 'mediawiki.interface.helpers.styles',
47 'mediawiki.special.changeslist',
49 $out->addModules( 'mediawiki.special.recentchanges' );
50 $this->addHelpLink( 'Help:User contributions' );
53 $request = $this->getRequest();
55 $target = $par ??
$request->getVal( 'target' );
57 if ( $request->getVal( 'contribs' ) == 'newbie' ||
$par === 'newbies' ) {
59 $this->opts
['contribs'] = 'newbie';
61 $this->opts
['contribs'] = 'user';
64 $this->opts
['deletedOnly'] = $request->getBool( 'deletedOnly' );
66 if ( !strlen( $target ) ) {
67 if ( !$this->including() ) {
68 $out->addHTML( $this->getForm() );
74 $user = $this->getUser();
76 $this->opts
['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
77 $this->opts
['target'] = $target;
78 $this->opts
['topOnly'] = $request->getBool( 'topOnly' );
79 $this->opts
['newOnly'] = $request->getBool( 'newOnly' );
80 $this->opts
['hideMinor'] = $request->getBool( 'hideMinor' );
83 if ( $this->opts
['contribs'] === 'newbie' ) {
84 $userObj = User
::newFromName( $target ); // hysterical raisins
85 $out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub' ) );
86 $out->setHTMLTitle( $this->msg(
88 $this->msg( 'sp-contributions-newbies-title' )->plain()
89 )->inContentLanguage() );
90 } elseif ( ExternalUserNames
::isExternal( $target ) ) {
91 $userObj = User
::newFromName( $target, false );
93 $out->addHTML( $this->getForm() );
97 $out->addSubtitle( $this->contributionsSub( $userObj ) );
98 $out->setHTMLTitle( $this->msg(
100 $this->msg( 'contributions-title', $target )->plain()
101 )->inContentLanguage() );
103 $nt = Title
::makeTitleSafe( NS_USER
, $target );
105 $out->addHTML( $this->getForm() );
108 $userObj = User
::newFromName( $nt->getText(), false );
110 $out->addHTML( $this->getForm() );
113 $id = $userObj->getId();
115 $target = $nt->getText();
116 $out->addSubtitle( $this->contributionsSub( $userObj ) );
117 $out->setHTMLTitle( $this->msg(
119 $this->msg( 'contributions-title', $target )->plain()
120 )->inContentLanguage() );
122 # For IP ranges, we want the contributionsSub, but not the skin-dependent
123 # links under 'Tools', which may include irrelevant links like 'Logs'.
124 if ( !IP
::isValidRange( $target ) ) {
125 $this->getSkin()->setRelevantUser( $userObj );
129 $ns = $request->getVal( 'namespace', null );
130 if ( $ns !== null && $ns !== '' ) {
131 $this->opts
['namespace'] = intval( $ns );
133 $this->opts
['namespace'] = '';
136 $this->opts
['associated'] = $request->getBool( 'associated' );
137 $this->opts
['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
138 $this->opts
['tagfilter'] = (string)$request->getVal( 'tagfilter' );
140 // Allows reverts to have the bot flag in recent changes. It is just here to
141 // be passed in the form at the top of the page
142 if ( $user->isAllowed( 'markbotedits' ) && $request->getBool( 'bot' ) ) {
143 $this->opts
['bot'] = '1';
146 $skip = $request->getText( 'offset' ) ||
$request->getText( 'dir' ) == 'prev';
147 # Offset overrides year/month selection
149 $this->opts
['year'] = $request->getVal( 'year' );
150 $this->opts
['month'] = $request->getVal( 'month' );
152 $this->opts
['start'] = $request->getVal( 'start' );
153 $this->opts
['end'] = $request->getVal( 'end' );
155 $this->opts
= ContribsPager
::processDateFilter( $this->opts
);
157 if ( $this->opts
['namespace'] < NS_MAIN
) {
158 $this->getOutput()->wrapWikiMsg(
159 "<div class=\"mw-negative-namespace-not-supported error\">\n\$1\n</div>",
160 [ 'negative-namespace-not-supported' ]
162 $out->addHTML( $this->getForm() );
166 $feedType = $request->getVal( 'feed' );
169 'action' => 'feedcontributions',
172 if ( $this->opts
['topOnly'] ) {
173 $feedParams['toponly'] = true;
175 if ( $this->opts
['newOnly'] ) {
176 $feedParams['newonly'] = true;
178 if ( $this->opts
['hideMinor'] ) {
179 $feedParams['hideminor'] = true;
181 if ( $this->opts
['deletedOnly'] ) {
182 $feedParams['deletedonly'] = true;
184 if ( $this->opts
['tagfilter'] !== '' ) {
185 $feedParams['tagfilter'] = $this->opts
['tagfilter'];
187 if ( $this->opts
['namespace'] !== '' ) {
188 $feedParams['namespace'] = $this->opts
['namespace'];
190 // Don't use year and month for the feed URL, but pass them on if
191 // we redirect to API (if $feedType is specified)
192 if ( $feedType && $this->opts
['year'] !== null ) {
193 $feedParams['year'] = $this->opts
['year'];
195 if ( $feedType && $this->opts
['month'] !== null ) {
196 $feedParams['month'] = $this->opts
['month'];
200 // Maintain some level of backwards compatibility
201 // If people request feeds using the old parameters, redirect to API
202 $feedParams['feedformat'] = $feedType;
203 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
205 $out->redirect( $url, '301' );
210 // Add RSS/atom links
211 $this->addFeedLinks( $feedParams );
213 if ( Hooks
::run( 'SpecialContributionsBeforeMainOutput', [ $id, $userObj, $this ] ) ) {
214 if ( !$this->including() ) {
215 $out->addHTML( $this->getForm() );
217 $pager = new ContribsPager( $this->getContext(), [
219 'contribs' => $this->opts
['contribs'],
220 'namespace' => $this->opts
['namespace'],
221 'tagfilter' => $this->opts
['tagfilter'],
222 'start' => $this->opts
['start'],
223 'end' => $this->opts
['end'],
224 'deletedOnly' => $this->opts
['deletedOnly'],
225 'topOnly' => $this->opts
['topOnly'],
226 'newOnly' => $this->opts
['newOnly'],
227 'hideMinor' => $this->opts
['hideMinor'],
228 'nsInvert' => $this->opts
['nsInvert'],
229 'associated' => $this->opts
['associated'],
232 if ( IP
::isValidRange( $target ) && !$pager->isQueryableRange( $target ) ) {
233 // Valid range, but outside CIDR limit.
234 $limits = $this->getConfig()->get( 'RangeContributionsCIDRLimit' );
235 $limit = $limits[ IP
::isIPv4( $target ) ?
'IPv4' : 'IPv6' ];
236 $out->addWikiMsg( 'sp-contributions-outofrange', $limit );
237 } elseif ( !$pager->getNumRows() ) {
238 $out->addWikiMsg( 'nocontribs', $target );
240 # Show a message about replica DB lag, if applicable
241 $lag = $pager->getDatabase()->getSessionLagStatus()['lag'];
243 $out->showLagWarning( $lag );
246 $output = $pager->getBody();
247 if ( !$this->including() ) {
248 $output = $pager->getNavigationBar() .
250 $pager->getNavigationBar();
252 $out->addHTML( $output );
255 $out->preventClickjacking( $pager->getPreventClickjacking() );
257 # Show the appropriate "footer" message - WHOIS tools, etc.
258 if ( $this->opts
['contribs'] == 'newbie' ) {
259 $message = 'sp-contributions-footer-newbies';
260 } elseif ( IP
::isValidRange( $target ) ) {
261 $message = 'sp-contributions-footer-anon-range';
262 } elseif ( IP
::isIPAddress( $target ) ) {
263 $message = 'sp-contributions-footer-anon';
264 } elseif ( $userObj->isAnon() ) {
265 // No message for non-existing users
268 $message = 'sp-contributions-footer';
271 if ( $message && !$this->including() && !$this->msg( $message, $target )->isDisabled() ) {
273 "<div class='mw-contributions-footer'>\n$1\n</div>",
274 [ $message, $target ] );
280 * Generates the subheading with links
281 * @param User $userObj User object for the target
282 * @return string Appropriately-escaped HTML to be output literally
283 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
286 protected function contributionsSub( $userObj ) {
287 if ( $userObj->isAnon() ) {
288 // Show a warning message that the user being searched for doesn't exists.
289 // User::isIP returns true for IP address and usemod IPs like '123.123.123.xxx',
290 // but returns false for IP ranges. We don't want to suggest either of these are
291 // valid usernames which we would with the 'contributions-userdoesnotexist' message.
292 if ( !User
::isIP( $userObj->getName() ) && !$userObj->isIPRange() ) {
293 $this->getOutput()->wrapWikiMsg(
294 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
296 'contributions-userdoesnotexist',
297 wfEscapeWikiText( $userObj->getName() ),
300 if ( !$this->including() ) {
301 $this->getOutput()->setStatusCode( 404 );
304 $user = htmlspecialchars( $userObj->getName() );
306 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
308 $nt = $userObj->getUserPage();
309 $talk = $userObj->getTalkPage();
312 $tools = self
::getUserLinks( $this, $userObj );
313 $links = Html
::openElement( 'span', [ 'class' => 'mw-changeslist-links' ] );
314 foreach ( $tools as $tool ) {
315 $links .= Html
::rawElement( 'span', [], $tool ) . ' ';
317 $links = trim( $links ) . Html
::closeElement( 'span' );
319 // Show a note if the user is blocked and display the last block log entry.
320 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
321 // and also this will display a totally irrelevant log entry as a current block.
322 if ( !$this->including() ) {
323 // For IP ranges you must give Block::newFromTarget the CIDR string and not a user object.
324 if ( $userObj->isIPRange() ) {
325 $block = Block
::newFromTarget( $userObj->getName(), $userObj->getName() );
327 $block = Block
::newFromTarget( $userObj, $userObj );
330 if ( !is_null( $block ) && $block->getType() != Block
::TYPE_AUTO
) {
331 if ( $block->getType() == Block
::TYPE_RANGE
) {
332 $nt = MediaWikiServices
::getInstance()->getNamespaceInfo()->
333 getCanonicalName( NS_USER
) . ':' . $block->getTarget();
336 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
337 LogEventsList
::showLogExtract(
344 'showIfEmpty' => false,
347 'sp-contributions-blocked-notice-anon' :
348 'sp-contributions-blocked-notice',
349 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
351 'offset' => '' # don't use WebRequest parameter offset
358 return Html
::rawElement( 'div', [ 'class' => 'mw-contributions-user-tools' ],
359 $this->msg( 'contributions-subtitle' )->rawParams( $user )->params( $userObj->getName() )
365 * Links to different places.
367 * @note This function is also called in DeletedContributionsPage
368 * @param SpecialPage $sp SpecialPage instance, for context
369 * @param User $target Target user object
372 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
373 $id = $target->getId();
374 $username = $target->getName();
375 $userpage = $target->getUserPage();
376 $talkpage = $target->getTalkPage();
377 $isIP = IP
::isValid( $username );
378 $isRange = IP
::isValidRange( $username );
380 $linkRenderer = $sp->getLinkRenderer();
382 # No talk pages for IP ranges.
384 $tools['user-talk'] = $linkRenderer->makeLink(
386 $sp->msg( 'sp-contributions-talk' )->text()
390 if ( $sp->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
391 if ( $target->getBlock() && $target->getBlock()->getType() != Block
::TYPE_AUTO
) {
392 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
393 SpecialPage
::getTitleFor( 'Block', $username ),
394 $sp->msg( 'change-blocklink' )->text()
396 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
397 SpecialPage
::getTitleFor( 'Unblock', $username ),
398 $sp->msg( 'unblocklink' )->text()
400 } else { # User is not blocked
401 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
402 SpecialPage
::getTitleFor( 'Block', $username ),
403 $sp->msg( 'blocklink' )->text()
409 $tools['log-block'] = $linkRenderer->makeKnownLink(
410 SpecialPage
::getTitleFor( 'Log', 'block' ),
411 $sp->msg( 'sp-contributions-blocklog' )->text(),
413 [ 'page' => $userpage->getPrefixedText() ]
416 # Suppression log link (T61120)
417 if ( $sp->getUser()->isAllowed( 'suppressionlog' ) ) {
418 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
419 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
420 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
422 [ 'offender' => $username ]
426 # Don't show some links for IP ranges
428 # Uploads: hide if IPs cannot upload (T220674)
429 if ( !$isIP ||
$target->isAllowed( 'upload' ) ) {
430 $tools['uploads'] = $linkRenderer->makeKnownLink(
431 SpecialPage
::getTitleFor( 'Listfiles', $username ),
432 $sp->msg( 'sp-contributions-uploads' )->text()
438 $tools['logs'] = $linkRenderer->makeKnownLink(
439 SpecialPage
::getTitleFor( 'Log', $username ),
440 $sp->msg( 'sp-contributions-logs' )->text()
443 # Add link to deleted user contributions for priviledged users
445 if ( $sp->getUser()->isAllowed( 'deletedhistory' ) ) {
446 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
447 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
448 $sp->msg( 'sp-contributions-deleted', $username )->text()
453 # Add a link to change user rights for privileged users
454 $userrightsPage = new UserrightsPage();
455 $userrightsPage->setContext( $sp->getContext() );
456 if ( $userrightsPage->userCanChangeRights( $target ) ) {
457 $tools['userrights'] = $linkRenderer->makeKnownLink(
458 SpecialPage
::getTitleFor( 'Userrights', $username ),
459 $sp->msg( 'sp-contributions-userrights', $username )->text()
463 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
469 * Generates the namespace selector form with hidden attributes.
470 * @return string HTML fragment
472 protected function getForm() {
473 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
474 if ( !isset( $this->opts
['target'] ) ) {
475 $this->opts
['target'] = '';
477 $this->opts
['target'] = str_replace( '_', ' ', $this->opts
['target'] );
480 if ( !isset( $this->opts
['namespace'] ) ) {
481 $this->opts
['namespace'] = '';
484 if ( !isset( $this->opts
['nsInvert'] ) ) {
485 $this->opts
['nsInvert'] = '';
488 if ( !isset( $this->opts
['associated'] ) ) {
489 $this->opts
['associated'] = false;
492 if ( !isset( $this->opts
['contribs'] ) ) {
493 $this->opts
['contribs'] = 'user';
496 if ( !isset( $this->opts
['start'] ) ) {
497 $this->opts
['start'] = '';
500 if ( !isset( $this->opts
['end'] ) ) {
501 $this->opts
['end'] = '';
504 if ( $this->opts
['contribs'] == 'newbie' ) {
505 $this->opts
['target'] = '';
508 if ( !isset( $this->opts
['tagfilter'] ) ) {
509 $this->opts
['tagfilter'] = '';
512 if ( !isset( $this->opts
['topOnly'] ) ) {
513 $this->opts
['topOnly'] = false;
516 if ( !isset( $this->opts
['newOnly'] ) ) {
517 $this->opts
['newOnly'] = false;
520 if ( !isset( $this->opts
['hideMinor'] ) ) {
521 $this->opts
['hideMinor'] = false;
524 // Modules required only for the form
525 $this->getOutput()->addModules( [
526 'mediawiki.userSuggest',
527 'mediawiki.special.contributions',
529 $this->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
530 $this->getOutput()->enableOOUI();
532 $form = Html
::openElement(
536 'action' => wfScript(),
537 'class' => 'mw-contributions-form'
541 # Add hidden params for tracking except for parameters in $skipParameters
559 foreach ( $this->opts
as $name => $value ) {
560 if ( in_array( $name, $skipParameters ) ) {
563 $form .= "\t" . Html
::hidden( $name, $value ) . "\n";
566 $tagFilter = ChangeTags
::buildTagFilterSelector(
567 $this->opts
['tagfilter'], false, $this->getContext() );
570 $filterSelection = Html
::rawElement(
573 implode( "\u{00A0}", $tagFilter )
576 $filterSelection = Html
::rawElement( 'div', [], '' );
579 $labelNewbies = Xml
::radioLabel(
580 $this->msg( 'sp-contributions-newbies' )->text(),
584 $this->opts
['contribs'] == 'newbie',
585 [ 'class' => 'mw-input' ]
587 $labelUsername = Xml
::radioLabel(
588 $this->msg( 'sp-contributions-username' )->text(),
592 $this->opts
['contribs'] == 'user',
593 [ 'class' => 'mw-input' ]
595 $input = Html
::input(
597 $this->opts
['target'],
600 'id' => 'mw-target-user-or-ip',
604 'mw-ui-input-inline',
605 'mw-autocomplete-user', // used by mediawiki.userSuggest
608 // Only autofocus if target hasn't been specified or in non-newbies mode
609 ( $this->opts
['contribs'] === 'newbie' ||
$this->opts
['target'] )
610 ?
[] : [ 'autofocus' => true ]
614 $targetSelection = Html
::rawElement(
617 $labelNewbies . '<br>' . $labelUsername . ' ' . $input . ' '
620 $hidden = $this->opts
['namespace'] === '' ?
' mw-input-hidden' : '';
621 $namespaceSelection = Xml
::tags(
625 $this->msg( 'namespace' )->text(),
629 Html
::namespaceSelector(
630 [ 'selected' => $this->opts
['namespace'], 'all' => '', 'in-user-lang' => true ],
632 'name' => 'namespace',
634 'class' => 'namespaceselector',
639 [ 'class' => 'mw-input-with-label' . $hidden ],
641 $this->msg( 'invert' )->text(),
644 $this->opts
['nsInvert'],
646 'title' => $this->msg( 'tooltip-invert' )->text(),
647 'class' => 'mw-input'
651 Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' . $hidden ],
653 $this->msg( 'namespace_association' )->text(),
656 $this->opts
['associated'],
658 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
659 'class' => 'mw-input'
667 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
668 $filters[] = Html
::rawElement(
670 [ 'class' => 'mw-input-with-label' ],
672 $this->msg( 'history-show-deleted' )->text(),
674 'mw-show-deleted-only',
675 $this->opts
['deletedOnly'],
676 [ 'class' => 'mw-input' ]
681 $filters[] = Html
::rawElement(
683 [ 'class' => 'mw-input-with-label' ],
685 $this->msg( 'sp-contributions-toponly' )->text(),
688 $this->opts
['topOnly'],
689 [ 'class' => 'mw-input' ]
692 $filters[] = Html
::rawElement(
694 [ 'class' => 'mw-input-with-label' ],
696 $this->msg( 'sp-contributions-newonly' )->text(),
699 $this->opts
['newOnly'],
700 [ 'class' => 'mw-input' ]
703 $filters[] = Html
::rawElement(
705 [ 'class' => 'mw-input-with-label' ],
707 $this->msg( 'sp-contributions-hideminor' )->text(),
709 'mw-hide-minor-edits',
710 $this->opts
['hideMinor'],
711 [ 'class' => 'mw-input' ]
716 'SpecialContributions::getForm::filters',
720 $extraOptions = Html
::rawElement(
723 implode( '', $filters )
726 $dateRangeSelection = Html
::rawElement(
729 Xml
::label( $this->msg( 'date-range-from' )->text(), 'mw-date-start' ) . ' ' .
730 new DateInputWidget( [
732 'id' => 'mw-date-start',
734 'value' => $this->opts
['start'],
735 'longDisplayFormat' => true,
737 Xml
::label( $this->msg( 'date-range-to' )->text(), 'mw-date-end' ) . ' ' .
738 new DateInputWidget( [
740 'id' => 'mw-date-end',
742 'value' => $this->opts
['end'],
743 'longDisplayFormat' => true,
747 $submit = Xml
::tags( 'div', [],
749 $this->msg( 'sp-contributions-submit' )->text(),
750 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
754 $form .= Xml
::fieldset(
755 $this->msg( 'sp-contributions-search' )->text(),
757 $namespaceSelection .
760 $dateRangeSelection .
762 [ 'class' => 'mw-contributions-table' ]
765 $explain = $this->msg( 'sp-contributions-explain' );
766 if ( !$explain->isBlank() ) {
767 $form .= Html
::rawElement(
768 'p', [ 'id' => 'mw-sp-contributions-explain' ], $explain->parse()
772 $form .= Xml
::closeElement( 'form' );
778 * Return an array of subpages beginning with $search that this special page will accept.
780 * @param string $search Prefix to search for
781 * @param int $limit Maximum number of results to return (usually 10)
782 * @param int $offset Number of results to skip (usually 0)
783 * @return string[] Matching subpages
785 public function prefixSearchSubpages( $search, $limit, $offset ) {
786 $user = User
::newFromName( $search );
788 // No prefix suggestion for invalid user
791 // Autocomplete subpage as user list - public to allow caching
792 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
795 protected function getGroupName() {