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 ) {
333 $id = $target->getId();
334 $username = $target->getName();
335 $userpage = $target->getUserPage();
336 $talkpage = $target->getTalkPage();
338 $linkRenderer = $sp->getLinkRenderer();
339 $tools['user-talk'] = $linkRenderer->makeLink(
341 $sp->msg( 'sp-contributions-talk' )->text()
344 if ( ( $id !== null ) ||
( $id === null && IP
::isIPAddress( $username ) ) ) {
345 if ( $sp->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
346 if ( $target->isBlocked() && $target->getBlock()->getType() != Block
::TYPE_AUTO
) {
347 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
348 SpecialPage
::getTitleFor( 'Block', $username ),
349 $sp->msg( 'change-blocklink' )->text()
351 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
352 SpecialPage
::getTitleFor( 'Unblock', $username ),
353 $sp->msg( 'unblocklink' )->text()
355 } else { # User is not blocked
356 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
357 SpecialPage
::getTitleFor( 'Block', $username ),
358 $sp->msg( 'blocklink' )->text()
364 $tools['log-block'] = $linkRenderer->makeKnownLink(
365 SpecialPage
::getTitleFor( 'Log', 'block' ),
366 $sp->msg( 'sp-contributions-blocklog' )->text(),
368 [ 'page' => $userpage->getPrefixedText() ]
371 # Suppression log link (T61120)
372 if ( $sp->getUser()->isAllowed( 'suppressionlog' ) ) {
373 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
374 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
375 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
377 [ 'offender' => $username ]
382 $tools['uploads'] = $linkRenderer->makeKnownLink(
383 SpecialPage
::getTitleFor( 'Listfiles', $username ),
384 $sp->msg( 'sp-contributions-uploads' )->text()
388 $tools['logs'] = $linkRenderer->makeKnownLink(
389 SpecialPage
::getTitleFor( 'Log', $username ),
390 $sp->msg( 'sp-contributions-logs' )->text()
393 # Add link to deleted user contributions for priviledged users
394 if ( $sp->getUser()->isAllowed( 'deletedhistory' ) ) {
395 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
396 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
397 $sp->msg( 'sp-contributions-deleted', $username )->text()
401 # Add a link to change user rights for privileged users
402 $userrightsPage = new UserrightsPage();
403 $userrightsPage->setContext( $sp->getContext() );
404 if ( $userrightsPage->userCanChangeRights( $target ) ) {
405 $tools['userrights'] = $linkRenderer->makeKnownLink(
406 SpecialPage
::getTitleFor( 'Userrights', $username ),
407 $sp->msg( 'sp-contributions-userrights', $username )->text()
411 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
417 * Generates the namespace selector form with hidden attributes.
418 * @return string HTML fragment
420 protected function getForm() {
421 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
422 if ( !isset( $this->opts
['target'] ) ) {
423 $this->opts
['target'] = '';
425 $this->opts
['target'] = str_replace( '_', ' ', $this->opts
['target'] );
428 if ( !isset( $this->opts
['namespace'] ) ) {
429 $this->opts
['namespace'] = '';
432 if ( !isset( $this->opts
['nsInvert'] ) ) {
433 $this->opts
['nsInvert'] = '';
436 if ( !isset( $this->opts
['associated'] ) ) {
437 $this->opts
['associated'] = false;
440 if ( !isset( $this->opts
['contribs'] ) ) {
441 $this->opts
['contribs'] = 'user';
444 if ( !isset( $this->opts
['start'] ) ) {
445 $this->opts
['start'] = '';
448 if ( !isset( $this->opts
['end'] ) ) {
449 $this->opts
['end'] = '';
452 if ( $this->opts
['contribs'] == 'newbie' ) {
453 $this->opts
['target'] = '';
456 if ( !isset( $this->opts
['tagfilter'] ) ) {
457 $this->opts
['tagfilter'] = '';
460 if ( !isset( $this->opts
['topOnly'] ) ) {
461 $this->opts
['topOnly'] = false;
464 if ( !isset( $this->opts
['newOnly'] ) ) {
465 $this->opts
['newOnly'] = false;
468 if ( !isset( $this->opts
['hideMinor'] ) ) {
469 $this->opts
['hideMinor'] = false;
472 $form = Html
::openElement(
476 'action' => wfScript(),
477 'class' => 'mw-contributions-form'
481 # Add hidden params for tracking except for parameters in $skipParameters
499 foreach ( $this->opts
as $name => $value ) {
500 if ( in_array( $name, $skipParameters ) ) {
503 $form .= "\t" . Html
::hidden( $name, $value ) . "\n";
506 $tagFilter = ChangeTags
::buildTagFilterSelector(
507 $this->opts
['tagfilter'], false, $this->getContext() );
510 $filterSelection = Html
::rawElement(
513 implode( ' ', $tagFilter )
516 $filterSelection = Html
::rawElement( 'div', [], '' );
519 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
521 $labelNewbies = Xml
::radioLabel(
522 $this->msg( 'sp-contributions-newbies' )->text(),
526 $this->opts
['contribs'] == 'newbie',
527 [ 'class' => 'mw-input' ]
529 $labelUsername = Xml
::radioLabel(
530 $this->msg( 'sp-contributions-username' )->text(),
534 $this->opts
['contribs'] == 'user',
535 [ 'class' => 'mw-input' ]
537 $input = Html
::input(
539 $this->opts
['target'],
545 'mw-ui-input-inline',
546 'mw-autocomplete-user', // used by mediawiki.userSuggest
549 // Only autofocus if target hasn't been specified or in non-newbies mode
550 ( $this->opts
['contribs'] === 'newbie' ||
$this->opts
['target'] )
551 ?
[] : [ 'autofocus' => true ]
555 $targetSelection = Html
::rawElement(
558 $labelNewbies . '<br>' . $labelUsername . ' ' . $input . ' '
561 $namespaceSelection = Xml
::tags(
565 $this->msg( 'namespace' )->text(),
569 Html
::namespaceSelector(
570 [ 'selected' => $this->opts
['namespace'], 'all' => '' ],
572 'name' => 'namespace',
574 'class' => 'namespaceselector',
579 [ 'class' => 'mw-input-with-label' ],
581 $this->msg( 'invert' )->text(),
584 $this->opts
['nsInvert'],
586 'title' => $this->msg( 'tooltip-invert' )->text(),
587 'class' => 'mw-input'
591 Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' ],
593 $this->msg( 'namespace_association' )->text(),
596 $this->opts
['associated'],
598 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
599 'class' => 'mw-input'
607 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
608 $filters[] = Html
::rawElement(
610 [ 'class' => 'mw-input-with-label' ],
612 $this->msg( 'history-show-deleted' )->text(),
614 'mw-show-deleted-only',
615 $this->opts
['deletedOnly'],
616 [ 'class' => 'mw-input' ]
621 $filters[] = Html
::rawElement(
623 [ 'class' => 'mw-input-with-label' ],
625 $this->msg( 'sp-contributions-toponly' )->text(),
628 $this->opts
['topOnly'],
629 [ 'class' => 'mw-input' ]
632 $filters[] = Html
::rawElement(
634 [ 'class' => 'mw-input-with-label' ],
636 $this->msg( 'sp-contributions-newonly' )->text(),
639 $this->opts
['newOnly'],
640 [ 'class' => 'mw-input' ]
643 $filters[] = Html
::rawElement(
645 [ 'class' => 'mw-input-with-label' ],
647 $this->msg( 'sp-contributions-hideminor' )->text(),
649 'mw-hide-minor-edits',
650 $this->opts
['hideMinor'],
651 [ 'class' => 'mw-input' ]
656 'SpecialContributions::getForm::filters',
660 $extraOptions = Html
::rawElement(
663 implode( '', $filters )
666 $dateRangeSelection = Html
::rawElement(
669 Xml
::label( wfMessage( 'date-range-from' )->text(), 'mw-date-start' ) . ' ' .
670 new DateInputWidget( [
672 'id' => 'mw-date-start',
674 'value' => $this->opts
['start'],
675 'longDisplayFormat' => true,
677 Xml
::label( wfMessage( 'date-range-to' )->text(), 'mw-date-end' ) . ' ' .
678 new DateInputWidget( [
680 'id' => 'mw-date-end',
682 'value' => $this->opts
['end'],
683 'longDisplayFormat' => true,
687 $submit = Xml
::tags( 'div', [],
689 $this->msg( 'sp-contributions-submit' )->text(),
690 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
694 $form .= Xml
::fieldset(
695 $this->msg( 'sp-contributions-search' )->text(),
697 $namespaceSelection .
700 $dateRangeSelection .
702 [ 'class' => 'mw-contributions-table' ]
705 $explain = $this->msg( 'sp-contributions-explain' );
706 if ( !$explain->isBlank() ) {
707 $form .= "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>";
710 $form .= Xml
::closeElement( 'form' );
716 * Return an array of subpages beginning with $search that this special page will accept.
718 * @param string $search Prefix to search for
719 * @param int $limit Maximum number of results to return (usually 10)
720 * @param int $offset Number of results to skip (usually 0)
721 * @return string[] Matching subpages
723 public function prefixSearchSubpages( $search, $limit, $offset ) {
724 $user = User
::newFromName( $search );
726 // No prefix suggestion for invalid user
729 // Autocomplete subpage as user list - public to allow caching
730 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
733 protected function getGroupName() {