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 $out->addModuleStyles( [
45 'mediawiki.special.changeslist',
46 'mediawiki.widgets.DateInputWidget.styles',
48 $out->addModules( 'mediawiki.special.contributions' );
49 $this->addHelpLink( 'Help:User contributions' );
53 $request = $this->getRequest();
55 if ( $par !== null ) {
58 $target = $request->getVal( 'target' );
61 if ( $request->getVal( 'contribs' ) == 'newbie' ||
$par === 'newbies' ) {
63 $this->opts
['contribs'] = 'newbie';
65 $this->opts
['contribs'] = 'user';
68 $this->opts
['deletedOnly'] = $request->getBool( 'deletedOnly' );
70 if ( !strlen( $target ) ) {
71 if ( !$this->including() ) {
72 $out->addHTML( $this->getForm() );
78 $user = $this->getUser();
80 $this->opts
['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
81 $this->opts
['target'] = $target;
82 $this->opts
['topOnly'] = $request->getBool( 'topOnly' );
83 $this->opts
['newOnly'] = $request->getBool( 'newOnly' );
84 $this->opts
['hideMinor'] = $request->getBool( 'hideMinor' );
86 $nt = Title
::makeTitleSafe( NS_USER
, $target );
88 $out->addHTML( $this->getForm() );
92 $userObj = User
::newFromName( $nt->getText(), false );
94 $out->addHTML( $this->getForm() );
98 $id = $userObj->getId();
100 if ( $this->opts
['contribs'] != 'newbie' ) {
101 $target = $nt->getText();
102 $out->addSubtitle( $this->contributionsSub( $userObj ) );
103 $out->setHTMLTitle( $this->msg(
105 $this->msg( 'contributions-title', $target )->plain()
106 )->inContentLanguage() );
108 # For IP ranges, we want the contributionsSub, but not the skin-dependent
109 # links under 'Tools', which may include irrelevant links like 'Logs'.
110 if ( !IP
::isValidRange( $target ) ) {
111 $this->getSkin()->setRelevantUser( $userObj );
114 $out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub' ) );
115 $out->setHTMLTitle( $this->msg(
117 $this->msg( 'sp-contributions-newbies-title' )->plain()
118 )->inContentLanguage() );
121 $ns = $request->getVal( 'namespace', null );
122 if ( $ns !== null && $ns !== '' ) {
123 $this->opts
['namespace'] = intval( $ns );
125 $this->opts
['namespace'] = '';
128 $this->opts
['associated'] = $request->getBool( 'associated' );
129 $this->opts
['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
130 $this->opts
['tagfilter'] = (string)$request->getVal( 'tagfilter' );
132 // Allows reverts to have the bot flag in recent changes. It is just here to
133 // be passed in the form at the top of the page
134 if ( $user->isAllowed( 'markbotedits' ) && $request->getBool( 'bot' ) ) {
135 $this->opts
['bot'] = '1';
138 $skip = $request->getText( 'offset' ) ||
$request->getText( 'dir' ) == 'prev';
139 # Offset overrides year/month selection
141 $this->opts
['year'] = $request->getVal( 'year' );
142 $this->opts
['month'] = $request->getVal( 'month' );
144 $this->opts
['start'] = $request->getVal( 'start' );
145 $this->opts
['end'] = $request->getVal( 'end' );
147 $this->opts
= ContribsPager
::processDateFilter( $this->opts
);
149 $feedType = $request->getVal( 'feed' );
152 'action' => 'feedcontributions',
155 if ( $this->opts
['topOnly'] ) {
156 $feedParams['toponly'] = true;
158 if ( $this->opts
['newOnly'] ) {
159 $feedParams['newonly'] = true;
161 if ( $this->opts
['hideMinor'] ) {
162 $feedParams['hideminor'] = true;
164 if ( $this->opts
['deletedOnly'] ) {
165 $feedParams['deletedonly'] = true;
167 if ( $this->opts
['tagfilter'] !== '' ) {
168 $feedParams['tagfilter'] = $this->opts
['tagfilter'];
170 if ( $this->opts
['namespace'] !== '' ) {
171 $feedParams['namespace'] = $this->opts
['namespace'];
173 // Don't use year and month for the feed URL, but pass them on if
174 // we redirect to API (if $feedType is specified)
175 if ( $feedType && $this->opts
['year'] !== null ) {
176 $feedParams['year'] = $this->opts
['year'];
178 if ( $feedType && $this->opts
['month'] !== null ) {
179 $feedParams['month'] = $this->opts
['month'];
183 // Maintain some level of backwards compatibility
184 // If people request feeds using the old parameters, redirect to API
185 $feedParams['feedformat'] = $feedType;
186 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
188 $out->redirect( $url, '301' );
193 // Add RSS/atom links
194 $this->addFeedLinks( $feedParams );
196 if ( Hooks
::run( 'SpecialContributionsBeforeMainOutput', [ $id, $userObj, $this ] ) ) {
197 if ( !$this->including() ) {
198 $out->addHTML( $this->getForm() );
200 $pager = new ContribsPager( $this->getContext(), [
202 'contribs' => $this->opts
['contribs'],
203 'namespace' => $this->opts
['namespace'],
204 'tagfilter' => $this->opts
['tagfilter'],
205 'start' => $this->opts
['start'],
206 'end' => $this->opts
['end'],
207 'deletedOnly' => $this->opts
['deletedOnly'],
208 'topOnly' => $this->opts
['topOnly'],
209 'newOnly' => $this->opts
['newOnly'],
210 'hideMinor' => $this->opts
['hideMinor'],
211 'nsInvert' => $this->opts
['nsInvert'],
212 'associated' => $this->opts
['associated'],
215 if ( IP
::isValidRange( $target ) && !$pager->isQueryableRange( $target ) ) {
216 // Valid range, but outside CIDR limit.
217 $limits = $this->getConfig()->get( 'RangeContributionsCIDRLimit' );
218 $limit = $limits[ IP
::isIPv4( $target ) ?
'IPv4' : 'IPv6' ];
219 $out->addWikiMsg( 'sp-contributions-outofrange', $limit );
220 } elseif ( !$pager->getNumRows() ) {
221 $out->addWikiMsg( 'nocontribs', $target );
223 # Show a message about replica DB lag, if applicable
224 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
225 $lag = $lb->safeGetLag( $pager->getDatabase() );
227 $out->showLagWarning( $lag );
230 $output = $pager->getBody();
231 if ( !$this->including() ) {
232 $output = '<p>' . $pager->getNavigationBar() . '</p>' .
234 '<p>' . $pager->getNavigationBar() . '</p>';
236 $out->addHTML( $output );
239 $out->preventClickjacking( $pager->getPreventClickjacking() );
241 # Show the appropriate "footer" message - WHOIS tools, etc.
242 if ( $this->opts
['contribs'] == 'newbie' ) {
243 $message = 'sp-contributions-footer-newbies';
244 } elseif ( 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';
256 if ( !$this->including() ) {
257 if ( !$this->msg( $message, $target )->isDisabled() ) {
259 "<div class='mw-contributions-footer'>\n$1\n</div>",
260 [ $message, $target ] );
268 * Generates the subheading with links
269 * @param User $userObj User object for the target
270 * @return string Appropriately-escaped HTML to be output literally
271 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
274 protected function contributionsSub( $userObj ) {
275 if ( $userObj->isAnon() ) {
276 // Show a warning message that the user being searched for doesn't exists.
277 // User::isIP returns true for IP address and usemod IPs like '123.123.123.xxx',
278 // but returns false for IP ranges. We don't want to suggest either of these are
279 // valid usernames which we would with the 'contributions-userdoesnotexist' message.
280 if ( !User
::isIP( $userObj->getName() ) && !$userObj->isIPRange() ) {
281 $this->getOutput()->wrapWikiMsg(
282 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
284 'contributions-userdoesnotexist',
285 wfEscapeWikiText( $userObj->getName() ),
288 if ( !$this->including() ) {
289 $this->getOutput()->setStatusCode( 404 );
292 $user = htmlspecialchars( $userObj->getName() );
294 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
296 $nt = $userObj->getUserPage();
297 $talk = $userObj->getTalkPage();
300 $tools = self
::getUserLinks( $this, $userObj );
301 $links = $this->getLanguage()->pipeList( $tools );
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 Block::newFromTarget the CIDR string and not a user object.
308 if ( $userObj->isIPRange() ) {
309 $block = Block
::newFromTarget( $userObj->getName(), $userObj->getName() );
311 $block = Block
::newFromTarget( $userObj, $userObj );
314 if ( !is_null( $block ) && $block->getType() != Block
::TYPE_AUTO
) {
315 if ( $block->getType() == Block
::TYPE_RANGE
) {
316 $nt = MWNamespace
::getCanonicalName( NS_USER
) . ':' . $block->getTarget();
319 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
320 LogEventsList
::showLogExtract(
327 'showIfEmpty' => false,
330 'sp-contributions-blocked-notice-anon' :
331 'sp-contributions-blocked-notice',
332 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
334 'offset' => '' # don't use WebRequest parameter offset
341 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
345 * Links to different places.
347 * @note This function is also called in DeletedContributionsPage
348 * @param SpecialPage $sp SpecialPage instance, for context
349 * @param User $target Target user object
352 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
353 $id = $target->getId();
354 $username = $target->getName();
355 $userpage = $target->getUserPage();
356 $talkpage = $target->getTalkPage();
358 $linkRenderer = $sp->getLinkRenderer();
360 # No talk pages for IP ranges.
361 if ( !IP
::isValidRange( $username ) ) {
362 $tools['user-talk'] = $linkRenderer->makeLink(
364 $sp->msg( 'sp-contributions-talk' )->text()
368 if ( ( $id !== null ) ||
( $id === null && IP
::isIPAddress( $username ) ) ) {
369 if ( $sp->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
370 if ( $target->isBlocked() && $target->getBlock()->getType() != Block
::TYPE_AUTO
) {
371 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
372 SpecialPage
::getTitleFor( 'Block', $username ),
373 $sp->msg( 'change-blocklink' )->text()
375 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
376 SpecialPage
::getTitleFor( 'Unblock', $username ),
377 $sp->msg( 'unblocklink' )->text()
379 } else { # User is not blocked
380 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
381 SpecialPage
::getTitleFor( 'Block', $username ),
382 $sp->msg( 'blocklink' )->text()
388 $tools['log-block'] = $linkRenderer->makeKnownLink(
389 SpecialPage
::getTitleFor( 'Log', 'block' ),
390 $sp->msg( 'sp-contributions-blocklog' )->text(),
392 [ 'page' => $userpage->getPrefixedText() ]
395 # Suppression log link (T61120)
396 if ( $sp->getUser()->isAllowed( 'suppressionlog' ) ) {
397 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
398 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
399 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
401 [ 'offender' => $username ]
406 # Don't show some links for IP ranges
407 if ( !IP
::isValidRange( $username ) ) {
409 $tools['uploads'] = $linkRenderer->makeKnownLink(
410 SpecialPage
::getTitleFor( 'Listfiles', $username ),
411 $sp->msg( 'sp-contributions-uploads' )->text()
415 $tools['logs'] = $linkRenderer->makeKnownLink(
416 SpecialPage
::getTitleFor( 'Log', $username ),
417 $sp->msg( 'sp-contributions-logs' )->text()
420 # Add link to deleted user contributions for priviledged users
421 if ( $sp->getUser()->isAllowed( 'deletedhistory' ) ) {
422 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
423 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
424 $sp->msg( 'sp-contributions-deleted', $username )->text()
429 # Add a link to change user rights for privileged users
430 $userrightsPage = new UserrightsPage();
431 $userrightsPage->setContext( $sp->getContext() );
432 if ( $userrightsPage->userCanChangeRights( $target ) ) {
433 $tools['userrights'] = $linkRenderer->makeKnownLink(
434 SpecialPage
::getTitleFor( 'Userrights', $username ),
435 $sp->msg( 'sp-contributions-userrights', $username )->text()
439 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
445 * Generates the namespace selector form with hidden attributes.
446 * @return string HTML fragment
448 protected function getForm() {
449 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
450 if ( !isset( $this->opts
['target'] ) ) {
451 $this->opts
['target'] = '';
453 $this->opts
['target'] = str_replace( '_', ' ', $this->opts
['target'] );
456 if ( !isset( $this->opts
['namespace'] ) ) {
457 $this->opts
['namespace'] = '';
460 if ( !isset( $this->opts
['nsInvert'] ) ) {
461 $this->opts
['nsInvert'] = '';
464 if ( !isset( $this->opts
['associated'] ) ) {
465 $this->opts
['associated'] = false;
468 if ( !isset( $this->opts
['contribs'] ) ) {
469 $this->opts
['contribs'] = 'user';
472 if ( !isset( $this->opts
['start'] ) ) {
473 $this->opts
['start'] = '';
476 if ( !isset( $this->opts
['end'] ) ) {
477 $this->opts
['end'] = '';
480 if ( $this->opts
['contribs'] == 'newbie' ) {
481 $this->opts
['target'] = '';
484 if ( !isset( $this->opts
['tagfilter'] ) ) {
485 $this->opts
['tagfilter'] = '';
488 if ( !isset( $this->opts
['topOnly'] ) ) {
489 $this->opts
['topOnly'] = false;
492 if ( !isset( $this->opts
['newOnly'] ) ) {
493 $this->opts
['newOnly'] = false;
496 if ( !isset( $this->opts
['hideMinor'] ) ) {
497 $this->opts
['hideMinor'] = false;
500 $form = Html
::openElement(
504 'action' => wfScript(),
505 'class' => 'mw-contributions-form'
509 # Add hidden params for tracking except for parameters in $skipParameters
527 foreach ( $this->opts
as $name => $value ) {
528 if ( in_array( $name, $skipParameters ) ) {
531 $form .= "\t" . Html
::hidden( $name, $value ) . "\n";
534 $tagFilter = ChangeTags
::buildTagFilterSelector(
535 $this->opts
['tagfilter'], false, $this->getContext() );
538 $filterSelection = Html
::rawElement(
541 implode( ' ', $tagFilter )
544 $filterSelection = Html
::rawElement( 'div', [], '' );
547 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
549 $labelNewbies = Xml
::radioLabel(
550 $this->msg( 'sp-contributions-newbies' )->text(),
554 $this->opts
['contribs'] == 'newbie',
555 [ 'class' => 'mw-input' ]
557 $labelUsername = Xml
::radioLabel(
558 $this->msg( 'sp-contributions-username' )->text(),
562 $this->opts
['contribs'] == 'user',
563 [ 'class' => 'mw-input' ]
565 $input = Html
::input(
567 $this->opts
['target'],
573 'mw-ui-input-inline',
574 'mw-autocomplete-user', // used by mediawiki.userSuggest
577 // Only autofocus if target hasn't been specified or in non-newbies mode
578 ( $this->opts
['contribs'] === 'newbie' ||
$this->opts
['target'] )
579 ?
[] : [ 'autofocus' => true ]
583 $targetSelection = Html
::rawElement(
586 $labelNewbies . '<br>' . $labelUsername . ' ' . $input . ' '
589 $namespaceSelection = Xml
::tags(
593 $this->msg( 'namespace' )->text(),
597 Html
::namespaceSelector(
598 [ 'selected' => $this->opts
['namespace'], 'all' => '' ],
600 'name' => 'namespace',
602 'class' => 'namespaceselector',
607 [ 'class' => 'mw-input-with-label' ],
609 $this->msg( 'invert' )->text(),
612 $this->opts
['nsInvert'],
614 'title' => $this->msg( 'tooltip-invert' )->text(),
615 'class' => 'mw-input'
619 Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' ],
621 $this->msg( 'namespace_association' )->text(),
624 $this->opts
['associated'],
626 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
627 'class' => 'mw-input'
635 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
636 $filters[] = Html
::rawElement(
638 [ 'class' => 'mw-input-with-label' ],
640 $this->msg( 'history-show-deleted' )->text(),
642 'mw-show-deleted-only',
643 $this->opts
['deletedOnly'],
644 [ 'class' => 'mw-input' ]
649 $filters[] = Html
::rawElement(
651 [ 'class' => 'mw-input-with-label' ],
653 $this->msg( 'sp-contributions-toponly' )->text(),
656 $this->opts
['topOnly'],
657 [ 'class' => 'mw-input' ]
660 $filters[] = Html
::rawElement(
662 [ 'class' => 'mw-input-with-label' ],
664 $this->msg( 'sp-contributions-newonly' )->text(),
667 $this->opts
['newOnly'],
668 [ 'class' => 'mw-input' ]
671 $filters[] = Html
::rawElement(
673 [ 'class' => 'mw-input-with-label' ],
675 $this->msg( 'sp-contributions-hideminor' )->text(),
677 'mw-hide-minor-edits',
678 $this->opts
['hideMinor'],
679 [ 'class' => 'mw-input' ]
684 'SpecialContributions::getForm::filters',
688 $extraOptions = Html
::rawElement(
691 implode( '', $filters )
694 $dateRangeSelection = Html
::rawElement(
697 Xml
::label( wfMessage( 'date-range-from' )->text(), 'mw-date-start' ) . ' ' .
698 new DateInputWidget( [
700 'id' => 'mw-date-start',
702 'value' => $this->opts
['start'],
703 'longDisplayFormat' => true,
705 Xml
::label( wfMessage( 'date-range-to' )->text(), 'mw-date-end' ) . ' ' .
706 new DateInputWidget( [
708 'id' => 'mw-date-end',
710 'value' => $this->opts
['end'],
711 'longDisplayFormat' => true,
715 $submit = Xml
::tags( 'div', [],
717 $this->msg( 'sp-contributions-submit' )->text(),
718 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
722 $form .= Xml
::fieldset(
723 $this->msg( 'sp-contributions-search' )->text(),
725 $namespaceSelection .
728 $dateRangeSelection .
730 [ 'class' => 'mw-contributions-table' ]
733 $explain = $this->msg( 'sp-contributions-explain' );
734 if ( !$explain->isBlank() ) {
735 $form .= "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>";
738 $form .= Xml
::closeElement( 'form' );
744 * Return an array of subpages beginning with $search that this special page will accept.
746 * @param string $search Prefix to search for
747 * @param int $limit Maximum number of results to return (usually 10)
748 * @param int $offset Number of results to skip (usually 0)
749 * @return string[] Matching subpages
751 public function prefixSearchSubpages( $search, $limit, $offset ) {
752 $user = User
::newFromName( $search );
754 // No prefix suggestion for invalid user
757 // Autocomplete subpage as user list - public to allow caching
758 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
761 protected function getGroupName() {