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\Widget\DateInputWidget
;
27 * Special:Contributions, show user contributions in a paged list
29 * @ingroup SpecialPage
31 class SpecialContributions
extends IncludableSpecialPage
{
34 public function __construct() {
35 parent
::__construct( 'Contributions' );
38 public function execute( $par ) {
40 $this->outputHeader();
41 $out = $this->getOutput();
42 $out->addModuleStyles( [
44 'mediawiki.special.changeslist',
45 'mediawiki.widgets.DateInputWidget.styles',
47 $out->addModules( 'mediawiki.special.contributions' );
48 $this->addHelpLink( 'Help:User contributions' );
52 $request = $this->getRequest();
54 if ( $par !== null ) {
57 $target = $request->getVal( 'target' );
60 if ( $request->getVal( 'contribs' ) == 'newbie' ||
$par === 'newbies' ) {
62 $this->opts
['contribs'] = 'newbie';
64 $this->opts
['contribs'] = 'user';
67 $this->opts
['deletedOnly'] = $request->getBool( 'deletedOnly' );
69 if ( !strlen( $target ) ) {
70 if ( !$this->including() ) {
71 $out->addHTML( $this->getForm() );
77 $user = $this->getUser();
79 $this->opts
['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
80 $this->opts
['target'] = $target;
81 $this->opts
['topOnly'] = $request->getBool( 'topOnly' );
82 $this->opts
['newOnly'] = $request->getBool( 'newOnly' );
83 $this->opts
['hideMinor'] = $request->getBool( 'hideMinor' );
85 $nt = Title
::makeTitleSafe( NS_USER
, $target );
87 $out->addHTML( $this->getForm() );
91 $userObj = User
::newFromName( $nt->getText(), false );
93 $out->addHTML( $this->getForm() );
97 $id = $userObj->getId();
99 if ( $this->opts
['contribs'] != 'newbie' ) {
100 $target = $nt->getText();
101 $out->addSubtitle( $this->contributionsSub( $userObj ) );
102 $out->setHTMLTitle( $this->msg(
104 $this->msg( 'contributions-title', $target )->plain()
105 )->inContentLanguage() );
106 $this->getSkin()->setRelevantUser( $userObj );
108 $out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub' ) );
109 $out->setHTMLTitle( $this->msg(
111 $this->msg( 'sp-contributions-newbies-title' )->plain()
112 )->inContentLanguage() );
115 $ns = $request->getVal( 'namespace', null );
116 if ( $ns !== null && $ns !== '' ) {
117 $this->opts
['namespace'] = intval( $ns );
119 $this->opts
['namespace'] = '';
122 $this->opts
['associated'] = $request->getBool( 'associated' );
123 $this->opts
['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
124 $this->opts
['tagfilter'] = (string)$request->getVal( 'tagfilter' );
126 // Allows reverts to have the bot flag in recent changes. It is just here to
127 // be passed in the form at the top of the page
128 if ( $user->isAllowed( 'markbotedits' ) && $request->getBool( 'bot' ) ) {
129 $this->opts
['bot'] = '1';
132 $skip = $request->getText( 'offset' ) ||
$request->getText( 'dir' ) == 'prev';
133 # Offset overrides year/month selection
135 $this->opts
['year'] = '';
136 $this->opts
['month'] = '';
138 $this->opts
['year'] = $request->getVal( 'year' );
139 $this->opts
['month'] = $request->getVal( 'month' );
141 $this->opts
['start'] = $request->getVal( 'start' );
142 $this->opts
['end'] = $request->getVal( 'end' );
143 $this->opts
= ContribsPager
::processDateFilter( $this->opts
);
146 $feedType = $request->getVal( 'feed' );
149 'action' => 'feedcontributions',
152 if ( $this->opts
['topOnly'] ) {
153 $feedParams['toponly'] = true;
155 if ( $this->opts
['newOnly'] ) {
156 $feedParams['newonly'] = true;
158 if ( $this->opts
['hideMinor'] ) {
159 $feedParams['hideminor'] = true;
161 if ( $this->opts
['deletedOnly'] ) {
162 $feedParams['deletedonly'] = true;
164 if ( $this->opts
['tagfilter'] !== '' ) {
165 $feedParams['tagfilter'] = $this->opts
['tagfilter'];
167 if ( $this->opts
['namespace'] !== '' ) {
168 $feedParams['namespace'] = $this->opts
['namespace'];
170 // Don't use year and month for the feed URL, but pass them on if
171 // we redirect to API (if $feedType is specified)
172 if ( $feedType && $this->opts
['year'] !== null ) {
173 $feedParams['year'] = $this->opts
['year'];
175 if ( $feedType && $this->opts
['month'] !== null ) {
176 $feedParams['month'] = $this->opts
['month'];
180 // Maintain some level of backwards compatibility
181 // If people request feeds using the old parameters, redirect to API
182 $feedParams['feedformat'] = $feedType;
183 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
185 $out->redirect( $url, '301' );
190 // Add RSS/atom links
191 $this->addFeedLinks( $feedParams );
193 if ( Hooks
::run( 'SpecialContributionsBeforeMainOutput', [ $id, $userObj, $this ] ) ) {
194 if ( !$this->including() ) {
195 $out->addHTML( $this->getForm() );
197 $pager = new ContribsPager( $this->getContext(), [
199 'contribs' => $this->opts
['contribs'],
200 'namespace' => $this->opts
['namespace'],
201 'tagfilter' => $this->opts
['tagfilter'],
202 'start' => $this->opts
['start'],
203 'end' => $this->opts
['end'],
204 'deletedOnly' => $this->opts
['deletedOnly'],
205 'topOnly' => $this->opts
['topOnly'],
206 'newOnly' => $this->opts
['newOnly'],
207 'hideMinor' => $this->opts
['hideMinor'],
208 'nsInvert' => $this->opts
['nsInvert'],
209 'associated' => $this->opts
['associated'],
212 if ( !$pager->getNumRows() ) {
213 $out->addWikiMsg( 'nocontribs', $target );
215 # Show a message about replica DB lag, if applicable
216 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
218 $out->showLagWarning( $lag );
221 $output = $pager->getBody();
222 if ( !$this->including() ) {
223 $output = '<p>' . $pager->getNavigationBar() . '</p>' .
225 '<p>' . $pager->getNavigationBar() . '</p>';
227 $out->addHTML( $output );
229 $out->preventClickjacking( $pager->getPreventClickjacking() );
231 # Show the appropriate "footer" message - WHOIS tools, etc.
232 if ( $this->opts
['contribs'] == 'newbie' ) {
233 $message = 'sp-contributions-footer-newbies';
234 } elseif ( IP
::isIPAddress( $target ) ) {
235 $message = 'sp-contributions-footer-anon';
236 } elseif ( $userObj->isAnon() ) {
237 // No message for non-existing users
240 $message = 'sp-contributions-footer';
244 if ( !$this->including() ) {
245 if ( !$this->msg( $message, $target )->isDisabled() ) {
247 "<div class='mw-contributions-footer'>\n$1\n</div>",
248 [ $message, $target ] );
256 * Generates the subheading with links
257 * @param User $userObj User object for the target
258 * @return string Appropriately-escaped HTML to be output literally
259 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
262 protected function contributionsSub( $userObj ) {
263 if ( $userObj->isAnon() ) {
264 // Show a warning message that the user being searched for doesn't exists
265 if ( !User
::isIP( $userObj->getName() ) ) {
266 $this->getOutput()->wrapWikiMsg(
267 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
269 'contributions-userdoesnotexist',
270 wfEscapeWikiText( $userObj->getName() ),
273 if ( !$this->including() ) {
274 $this->getOutput()->setStatusCode( 404 );
277 $user = htmlspecialchars( $userObj->getName() );
279 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
281 $nt = $userObj->getUserPage();
282 $talk = $userObj->getTalkPage();
285 $tools = self
::getUserLinks( $this, $userObj );
286 $links = $this->getLanguage()->pipeList( $tools );
288 // Show a note if the user is blocked and display the last block log entry.
289 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
290 // and also this will display a totally irrelevant log entry as a current block.
291 if ( !$this->including() ) {
292 $block = Block
::newFromTarget( $userObj, $userObj );
293 if ( !is_null( $block ) && $block->getType() != Block
::TYPE_AUTO
) {
294 if ( $block->getType() == Block
::TYPE_RANGE
) {
295 $nt = MWNamespace
::getCanonicalName( NS_USER
) . ':' . $block->getTarget();
298 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
299 LogEventsList
::showLogExtract(
306 'showIfEmpty' => false,
309 'sp-contributions-blocked-notice-anon' :
310 'sp-contributions-blocked-notice',
311 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
313 'offset' => '' # don't use WebRequest parameter offset
320 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
324 * Links to different places.
326 * @note This function is also called in DeletedContributionsPage
327 * @param SpecialPage $sp SpecialPage instance, for context
328 * @param User $target Target user object
331 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
332 $id = $target->getId();
333 $username = $target->getName();
334 $userpage = $target->getUserPage();
335 $talkpage = $target->getTalkPage();
337 $linkRenderer = $sp->getLinkRenderer();
338 $tools['user-talk'] = $linkRenderer->makeLink(
340 $sp->msg( 'sp-contributions-talk' )->text()
343 if ( ( $id !== null ) ||
( $id === null && IP
::isIPAddress( $username ) ) ) {
344 if ( $sp->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
345 if ( $target->isBlocked() && $target->getBlock()->getType() != Block
::TYPE_AUTO
) {
346 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
347 SpecialPage
::getTitleFor( 'Block', $username ),
348 $sp->msg( 'change-blocklink' )->text()
350 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
351 SpecialPage
::getTitleFor( 'Unblock', $username ),
352 $sp->msg( 'unblocklink' )->text()
354 } else { # User is not blocked
355 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
356 SpecialPage
::getTitleFor( 'Block', $username ),
357 $sp->msg( 'blocklink' )->text()
363 $tools['log-block'] = $linkRenderer->makeKnownLink(
364 SpecialPage
::getTitleFor( 'Log', 'block' ),
365 $sp->msg( 'sp-contributions-blocklog' )->text(),
367 [ 'page' => $userpage->getPrefixedText() ]
370 # Suppression log link (T61120)
371 if ( $sp->getUser()->isAllowed( 'suppressionlog' ) ) {
372 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
373 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
374 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
376 [ 'offender' => $username ]
381 $tools['uploads'] = $linkRenderer->makeKnownLink(
382 SpecialPage
::getTitleFor( 'Listfiles', $username ),
383 $sp->msg( 'sp-contributions-uploads' )->text()
387 $tools['logs'] = $linkRenderer->makeKnownLink(
388 SpecialPage
::getTitleFor( 'Log', $username ),
389 $sp->msg( 'sp-contributions-logs' )->text()
392 # Add link to deleted user contributions for priviledged users
393 if ( $sp->getUser()->isAllowed( 'deletedhistory' ) ) {
394 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
395 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
396 $sp->msg( 'sp-contributions-deleted', $username )->text()
400 # Add a link to change user rights for privileged users
401 $userrightsPage = new UserrightsPage();
402 $userrightsPage->setContext( $sp->getContext() );
403 if ( $userrightsPage->userCanChangeRights( $target ) ) {
404 $tools['userrights'] = $linkRenderer->makeKnownLink(
405 SpecialPage
::getTitleFor( 'Userrights', $username ),
406 $sp->msg( 'sp-contributions-userrights', $username )->text()
410 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
416 * Generates the namespace selector form with hidden attributes.
417 * @return string HTML fragment
419 protected function getForm() {
420 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
421 if ( !isset( $this->opts
['target'] ) ) {
422 $this->opts
['target'] = '';
424 $this->opts
['target'] = str_replace( '_', ' ', $this->opts
['target'] );
427 if ( !isset( $this->opts
['namespace'] ) ) {
428 $this->opts
['namespace'] = '';
431 if ( !isset( $this->opts
['nsInvert'] ) ) {
432 $this->opts
['nsInvert'] = '';
435 if ( !isset( $this->opts
['associated'] ) ) {
436 $this->opts
['associated'] = false;
439 if ( !isset( $this->opts
['contribs'] ) ) {
440 $this->opts
['contribs'] = 'user';
443 if ( !isset( $this->opts
['start'] ) ) {
444 $this->opts
['start'] = '';
447 if ( !isset( $this->opts
['end'] ) ) {
448 $this->opts
['end'] = '';
451 if ( $this->opts
['contribs'] == 'newbie' ) {
452 $this->opts
['target'] = '';
455 if ( !isset( $this->opts
['tagfilter'] ) ) {
456 $this->opts
['tagfilter'] = '';
459 if ( !isset( $this->opts
['topOnly'] ) ) {
460 $this->opts
['topOnly'] = false;
463 if ( !isset( $this->opts
['newOnly'] ) ) {
464 $this->opts
['newOnly'] = false;
467 if ( !isset( $this->opts
['hideMinor'] ) ) {
468 $this->opts
['hideMinor'] = false;
471 $form = Html
::openElement(
475 'action' => wfScript(),
476 'class' => 'mw-contributions-form'
480 # Add hidden params for tracking except for parameters in $skipParameters
498 foreach ( $this->opts
as $name => $value ) {
499 if ( in_array( $name, $skipParameters ) ) {
502 $form .= "\t" . Html
::hidden( $name, $value ) . "\n";
505 $tagFilter = ChangeTags
::buildTagFilterSelector(
506 $this->opts
['tagfilter'], false, $this->getContext() );
509 $filterSelection = Html
::rawElement(
512 implode( ' ', $tagFilter )
515 $filterSelection = Html
::rawElement( 'div', [], '' );
518 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
520 $labelNewbies = Xml
::radioLabel(
521 $this->msg( 'sp-contributions-newbies' )->text(),
525 $this->opts
['contribs'] == 'newbie',
526 [ 'class' => 'mw-input' ]
528 $labelUsername = Xml
::radioLabel(
529 $this->msg( 'sp-contributions-username' )->text(),
533 $this->opts
['contribs'] == 'user',
534 [ 'class' => 'mw-input' ]
536 $input = Html
::input(
538 $this->opts
['target'],
544 'mw-ui-input-inline',
545 'mw-autocomplete-user', // used by mediawiki.userSuggest
548 // Only autofocus if target hasn't been specified or in non-newbies mode
549 ( $this->opts
['contribs'] === 'newbie' ||
$this->opts
['target'] )
550 ?
[] : [ 'autofocus' => true ]
554 $targetSelection = Html
::rawElement(
557 $labelNewbies . '<br>' . $labelUsername . ' ' . $input . ' '
560 $namespaceSelection = Xml
::tags(
564 $this->msg( 'namespace' )->text(),
568 Html
::namespaceSelector(
569 [ 'selected' => $this->opts
['namespace'], 'all' => '' ],
571 'name' => 'namespace',
573 'class' => 'namespaceselector',
578 [ 'class' => 'mw-input-with-label' ],
580 $this->msg( 'invert' )->text(),
583 $this->opts
['nsInvert'],
585 'title' => $this->msg( 'tooltip-invert' )->text(),
586 'class' => 'mw-input'
590 Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' ],
592 $this->msg( 'namespace_association' )->text(),
595 $this->opts
['associated'],
597 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
598 'class' => 'mw-input'
606 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
607 $filters[] = Html
::rawElement(
609 [ 'class' => 'mw-input-with-label' ],
611 $this->msg( 'history-show-deleted' )->text(),
613 'mw-show-deleted-only',
614 $this->opts
['deletedOnly'],
615 [ 'class' => 'mw-input' ]
620 $filters[] = Html
::rawElement(
622 [ 'class' => 'mw-input-with-label' ],
624 $this->msg( 'sp-contributions-toponly' )->text(),
627 $this->opts
['topOnly'],
628 [ 'class' => 'mw-input' ]
631 $filters[] = Html
::rawElement(
633 [ 'class' => 'mw-input-with-label' ],
635 $this->msg( 'sp-contributions-newonly' )->text(),
638 $this->opts
['newOnly'],
639 [ 'class' => 'mw-input' ]
642 $filters[] = Html
::rawElement(
644 [ 'class' => 'mw-input-with-label' ],
646 $this->msg( 'sp-contributions-hideminor' )->text(),
648 'mw-hide-minor-edits',
649 $this->opts
['hideMinor'],
650 [ 'class' => 'mw-input' ]
655 'SpecialContributions::getForm::filters',
659 $extraOptions = Html
::rawElement(
662 implode( '', $filters )
665 $dateRangeSelection = Html
::rawElement(
668 Xml
::label( wfMessage( 'date-range-from' )->text(), 'mw-date-start' ) . ' ' .
669 new DateInputWidget( [
671 'id' => 'mw-date-start',
673 'value' => $this->opts
['start'],
674 'longDisplayFormat' => true,
676 Xml
::label( wfMessage( 'date-range-to' )->text(), 'mw-date-end' ) . ' ' .
677 new DateInputWidget( [
679 'id' => 'mw-date-end',
681 'value' => $this->opts
['end'],
682 'longDisplayFormat' => true,
686 $submit = Xml
::tags( 'div', [],
688 $this->msg( 'sp-contributions-submit' )->text(),
689 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
693 $form .= Xml
::fieldset(
694 $this->msg( 'sp-contributions-search' )->text(),
696 $namespaceSelection .
699 $dateRangeSelection .
701 [ 'class' => 'mw-contributions-table' ]
704 $explain = $this->msg( 'sp-contributions-explain' );
705 if ( !$explain->isBlank() ) {
706 $form .= "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>";
709 $form .= Xml
::closeElement( 'form' );
715 * Return an array of subpages beginning with $search that this special page will accept.
717 * @param string $search Prefix to search for
718 * @param int $limit Maximum number of results to return (usually 10)
719 * @param int $offset Number of results to skip (usually 0)
720 * @return string[] Matching subpages
722 public function prefixSearchSubpages( $search, $limit, $offset ) {
723 $user = User
::newFromName( $search );
725 // No prefix suggestion for invalid user
728 // Autocomplete subpage as user list - public to allow caching
729 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
732 protected function getGroupName() {