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
25 * Special:Contributions, show user contributions in a paged list
27 * @ingroup SpecialPage
29 class SpecialContributions
extends IncludableSpecialPage
{
32 public function __construct() {
33 parent
::__construct( 'Contributions' );
36 public function execute( $par ) {
38 $this->outputHeader();
39 $out = $this->getOutput();
40 $out->addModuleStyles( [
42 'mediawiki.special.changeslist',
43 'mediawiki.widgets.DateInputWidget.styles',
45 $out->addModules( 'mediawiki.special.contributions' );
46 $this->addHelpLink( 'Help:User contributions' );
50 $request = $this->getRequest();
52 if ( $par !== null ) {
55 $target = $request->getVal( 'target' );
58 if ( $request->getVal( 'contribs' ) == 'newbie' ||
$par === 'newbies' ) {
60 $this->opts
['contribs'] = 'newbie';
62 $this->opts
['contribs'] = 'user';
65 $this->opts
['deletedOnly'] = $request->getBool( 'deletedOnly' );
67 if ( !strlen( $target ) ) {
68 if ( !$this->including() ) {
69 $out->addHTML( $this->getForm() );
75 $user = $this->getUser();
77 $this->opts
['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
78 $this->opts
['target'] = $target;
79 $this->opts
['topOnly'] = $request->getBool( 'topOnly' );
80 $this->opts
['newOnly'] = $request->getBool( 'newOnly' );
81 $this->opts
['hideMinor'] = $request->getBool( 'hideMinor' );
83 $nt = Title
::makeTitleSafe( NS_USER
, $target );
85 $out->addHTML( $this->getForm() );
89 $userObj = User
::newFromName( $nt->getText(), false );
91 $out->addHTML( $this->getForm() );
95 $id = $userObj->getId();
97 if ( $this->opts
['contribs'] != 'newbie' ) {
98 $target = $nt->getText();
99 $out->addSubtitle( $this->contributionsSub( $userObj ) );
100 $out->setHTMLTitle( $this->msg(
102 $this->msg( 'contributions-title', $target )->plain()
103 )->inContentLanguage() );
104 $this->getSkin()->setRelevantUser( $userObj );
106 $out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub' ) );
107 $out->setHTMLTitle( $this->msg(
109 $this->msg( 'sp-contributions-newbies-title' )->plain()
110 )->inContentLanguage() );
113 $ns = $request->getVal( 'namespace', null );
114 if ( $ns !== null && $ns !== '' ) {
115 $this->opts
['namespace'] = intval( $ns );
117 $this->opts
['namespace'] = '';
120 $this->opts
['associated'] = $request->getBool( 'associated' );
121 $this->opts
['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
122 $this->opts
['tagfilter'] = (string)$request->getVal( 'tagfilter' );
124 // Allows reverts to have the bot flag in recent changes. It is just here to
125 // be passed in the form at the top of the page
126 if ( $user->isAllowed( 'markbotedits' ) && $request->getBool( 'bot' ) ) {
127 $this->opts
['bot'] = '1';
130 $skip = $request->getText( 'offset' ) ||
$request->getText( 'dir' ) == 'prev';
131 # Offset overrides year/month selection
133 $this->opts
['year'] = '';
134 $this->opts
['month'] = '';
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' );
141 $this->opts
= ContribsPager
::processDateFilter( $this->opts
);
144 $feedType = $request->getVal( 'feed' );
147 'action' => 'feedcontributions',
150 if ( $this->opts
['topOnly'] ) {
151 $feedParams['toponly'] = true;
153 if ( $this->opts
['newOnly'] ) {
154 $feedParams['newonly'] = true;
156 if ( $this->opts
['hideMinor'] ) {
157 $feedParams['hideminor'] = true;
159 if ( $this->opts
['deletedOnly'] ) {
160 $feedParams['deletedonly'] = true;
162 if ( $this->opts
['tagfilter'] !== '' ) {
163 $feedParams['tagfilter'] = $this->opts
['tagfilter'];
165 if ( $this->opts
['namespace'] !== '' ) {
166 $feedParams['namespace'] = $this->opts
['namespace'];
168 // Don't use year and month for the feed URL, but pass them on if
169 // we redirect to API (if $feedType is specified)
170 if ( $feedType && $this->opts
['year'] !== null ) {
171 $feedParams['year'] = $this->opts
['year'];
173 if ( $feedType && $this->opts
['month'] !== null ) {
174 $feedParams['month'] = $this->opts
['month'];
178 // Maintain some level of backwards compatibility
179 // If people request feeds using the old parameters, redirect to API
180 $feedParams['feedformat'] = $feedType;
181 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
183 $out->redirect( $url, '301' );
188 // Add RSS/atom links
189 $this->addFeedLinks( $feedParams );
191 if ( Hooks
::run( 'SpecialContributionsBeforeMainOutput', [ $id, $userObj, $this ] ) ) {
192 if ( !$this->including() ) {
193 $out->addHTML( $this->getForm() );
195 $pager = new ContribsPager( $this->getContext(), [
197 'contribs' => $this->opts
['contribs'],
198 'namespace' => $this->opts
['namespace'],
199 'tagfilter' => $this->opts
['tagfilter'],
200 'start' => $this->opts
['start'],
201 'end' => $this->opts
['end'],
202 'deletedOnly' => $this->opts
['deletedOnly'],
203 'topOnly' => $this->opts
['topOnly'],
204 'newOnly' => $this->opts
['newOnly'],
205 'hideMinor' => $this->opts
['hideMinor'],
206 'nsInvert' => $this->opts
['nsInvert'],
207 'associated' => $this->opts
['associated'],
210 if ( !$pager->getNumRows() ) {
211 $out->addWikiMsg( 'nocontribs', $target );
213 # Show a message about replica DB lag, if applicable
214 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
216 $out->showLagWarning( $lag );
219 $output = $pager->getBody();
220 if ( !$this->including() ) {
221 $output = '<p>' . $pager->getNavigationBar() . '</p>' .
223 '<p>' . $pager->getNavigationBar() . '</p>';
225 $out->addHTML( $output );
227 $out->preventClickjacking( $pager->getPreventClickjacking() );
229 # Show the appropriate "footer" message - WHOIS tools, etc.
230 if ( $this->opts
['contribs'] == 'newbie' ) {
231 $message = 'sp-contributions-footer-newbies';
232 } elseif ( IP
::isIPAddress( $target ) ) {
233 $message = 'sp-contributions-footer-anon';
234 } elseif ( $userObj->isAnon() ) {
235 // No message for non-existing users
238 $message = 'sp-contributions-footer';
242 if ( !$this->including() ) {
243 if ( !$this->msg( $message, $target )->isDisabled() ) {
245 "<div class='mw-contributions-footer'>\n$1\n</div>",
246 [ $message, $target ] );
254 * Generates the subheading with links
255 * @param User $userObj User object for the target
256 * @return string Appropriately-escaped HTML to be output literally
257 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
260 protected function contributionsSub( $userObj ) {
261 if ( $userObj->isAnon() ) {
262 // Show a warning message that the user being searched for doesn't exists
263 if ( !User
::isIP( $userObj->getName() ) ) {
264 $this->getOutput()->wrapWikiMsg(
265 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
267 'contributions-userdoesnotexist',
268 wfEscapeWikiText( $userObj->getName() ),
271 if ( !$this->including() ) {
272 $this->getOutput()->setStatusCode( 404 );
275 $user = htmlspecialchars( $userObj->getName() );
277 $user = $this->getLinkRenderer()->makeLink( $userObj->getUserPage(), $userObj->getName() );
279 $nt = $userObj->getUserPage();
280 $talk = $userObj->getTalkPage();
283 $tools = self
::getUserLinks( $this, $userObj );
284 $links = $this->getLanguage()->pipeList( $tools );
286 // Show a note if the user is blocked and display the last block log entry.
287 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
288 // and also this will display a totally irrelevant log entry as a current block.
289 if ( !$this->including() ) {
290 $block = Block
::newFromTarget( $userObj, $userObj );
291 if ( !is_null( $block ) && $block->getType() != Block
::TYPE_AUTO
) {
292 if ( $block->getType() == Block
::TYPE_RANGE
) {
293 $nt = MWNamespace
::getCanonicalName( NS_USER
) . ':' . $block->getTarget();
296 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
297 LogEventsList
::showLogExtract(
304 'showIfEmpty' => false,
307 'sp-contributions-blocked-notice-anon' :
308 'sp-contributions-blocked-notice',
309 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
311 'offset' => '' # don't use WebRequest parameter offset
318 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
322 * Links to different places.
324 * @note This function is also called in DeletedContributionsPage
325 * @param SpecialPage $sp SpecialPage instance, for context
326 * @param User $target Target user object
329 public static function getUserLinks( SpecialPage
$sp, User
$target ) {
331 $id = $target->getId();
332 $username = $target->getName();
333 $userpage = $target->getUserPage();
334 $talkpage = $target->getTalkPage();
336 $linkRenderer = $sp->getLinkRenderer();
337 $tools['user-talk'] = $linkRenderer->makeLink(
339 $sp->msg( 'sp-contributions-talk' )->text()
342 if ( ( $id !== null ) ||
( $id === null && IP
::isIPAddress( $username ) ) ) {
343 if ( $sp->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
344 if ( $target->isBlocked() && $target->getBlock()->getType() != Block
::TYPE_AUTO
) {
345 $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
346 SpecialPage
::getTitleFor( 'Block', $username ),
347 $sp->msg( 'change-blocklink' )->text()
349 $tools['unblock'] = $linkRenderer->makeKnownLink( # Unblock link
350 SpecialPage
::getTitleFor( 'Unblock', $username ),
351 $sp->msg( 'unblocklink' )->text()
353 } else { # User is not blocked
354 $tools['block'] = $linkRenderer->makeKnownLink( # Block link
355 SpecialPage
::getTitleFor( 'Block', $username ),
356 $sp->msg( 'blocklink' )->text()
362 $tools['log-block'] = $linkRenderer->makeKnownLink(
363 SpecialPage
::getTitleFor( 'Log', 'block' ),
364 $sp->msg( 'sp-contributions-blocklog' )->text(),
366 [ 'page' => $userpage->getPrefixedText() ]
369 # Suppression log link (T61120)
370 if ( $sp->getUser()->isAllowed( 'suppressionlog' ) ) {
371 $tools['log-suppression'] = $linkRenderer->makeKnownLink(
372 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
373 $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
375 [ 'offender' => $username ]
380 $tools['uploads'] = $linkRenderer->makeKnownLink(
381 SpecialPage
::getTitleFor( 'Listfiles', $username ),
382 $sp->msg( 'sp-contributions-uploads' )->text()
386 $tools['logs'] = $linkRenderer->makeKnownLink(
387 SpecialPage
::getTitleFor( 'Log', $username ),
388 $sp->msg( 'sp-contributions-logs' )->text()
391 # Add link to deleted user contributions for priviledged users
392 if ( $sp->getUser()->isAllowed( 'deletedhistory' ) ) {
393 $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
394 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
395 $sp->msg( 'sp-contributions-deleted', $username )->text()
399 # Add a link to change user rights for privileged users
400 $userrightsPage = new UserrightsPage();
401 $userrightsPage->setContext( $sp->getContext() );
402 if ( $userrightsPage->userCanChangeRights( $target ) ) {
403 $tools['userrights'] = $linkRenderer->makeKnownLink(
404 SpecialPage
::getTitleFor( 'Userrights', $username ),
405 $sp->msg( 'sp-contributions-userrights', $username )->text()
409 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools, $sp ] );
415 * Generates the namespace selector form with hidden attributes.
416 * @return string HTML fragment
418 protected function getForm() {
419 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
420 if ( !isset( $this->opts
['target'] ) ) {
421 $this->opts
['target'] = '';
423 $this->opts
['target'] = str_replace( '_', ' ', $this->opts
['target'] );
426 if ( !isset( $this->opts
['namespace'] ) ) {
427 $this->opts
['namespace'] = '';
430 if ( !isset( $this->opts
['nsInvert'] ) ) {
431 $this->opts
['nsInvert'] = '';
434 if ( !isset( $this->opts
['associated'] ) ) {
435 $this->opts
['associated'] = false;
438 if ( !isset( $this->opts
['contribs'] ) ) {
439 $this->opts
['contribs'] = 'user';
442 if ( !isset( $this->opts
['start'] ) ) {
443 $this->opts
['start'] = '';
446 if ( !isset( $this->opts
['end'] ) ) {
447 $this->opts
['end'] = '';
450 if ( $this->opts
['contribs'] == 'newbie' ) {
451 $this->opts
['target'] = '';
454 if ( !isset( $this->opts
['tagfilter'] ) ) {
455 $this->opts
['tagfilter'] = '';
458 if ( !isset( $this->opts
['topOnly'] ) ) {
459 $this->opts
['topOnly'] = false;
462 if ( !isset( $this->opts
['newOnly'] ) ) {
463 $this->opts
['newOnly'] = false;
466 if ( !isset( $this->opts
['hideMinor'] ) ) {
467 $this->opts
['hideMinor'] = false;
470 $form = Html
::openElement(
474 'action' => wfScript(),
475 'class' => 'mw-contributions-form'
479 # Add hidden params for tracking except for parameters in $skipParameters
497 foreach ( $this->opts
as $name => $value ) {
498 if ( in_array( $name, $skipParameters ) ) {
501 $form .= "\t" . Html
::hidden( $name, $value ) . "\n";
504 $tagFilter = ChangeTags
::buildTagFilterSelector(
505 $this->opts
['tagfilter'], false, $this->getContext() );
508 $filterSelection = Html
::rawElement(
511 implode( ' ', $tagFilter )
514 $filterSelection = Html
::rawElement( 'div', [], '' );
517 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
519 $labelNewbies = Xml
::radioLabel(
520 $this->msg( 'sp-contributions-newbies' )->text(),
524 $this->opts
['contribs'] == 'newbie',
525 [ 'class' => 'mw-input' ]
527 $labelUsername = Xml
::radioLabel(
528 $this->msg( 'sp-contributions-username' )->text(),
532 $this->opts
['contribs'] == 'user',
533 [ 'class' => 'mw-input' ]
535 $input = Html
::input(
537 $this->opts
['target'],
543 'mw-ui-input-inline',
544 'mw-autocomplete-user', // used by mediawiki.userSuggest
547 // Only autofocus if target hasn't been specified or in non-newbies mode
548 ( $this->opts
['contribs'] === 'newbie' ||
$this->opts
['target'] )
549 ?
[] : [ 'autofocus' => true ]
553 $targetSelection = Html
::rawElement(
556 $labelNewbies . '<br>' . $labelUsername . ' ' . $input . ' '
559 $namespaceSelection = Xml
::tags(
563 $this->msg( 'namespace' )->text(),
567 Html
::namespaceSelector(
568 [ 'selected' => $this->opts
['namespace'], 'all' => '' ],
570 'name' => 'namespace',
572 'class' => 'namespaceselector',
577 [ 'class' => 'mw-input-with-label' ],
579 $this->msg( 'invert' )->text(),
582 $this->opts
['nsInvert'],
584 'title' => $this->msg( 'tooltip-invert' )->text(),
585 'class' => 'mw-input'
589 Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' ],
591 $this->msg( 'namespace_association' )->text(),
594 $this->opts
['associated'],
596 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
597 'class' => 'mw-input'
605 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
606 $filters[] = Html
::rawElement(
608 [ 'class' => 'mw-input-with-label' ],
610 $this->msg( 'history-show-deleted' )->text(),
612 'mw-show-deleted-only',
613 $this->opts
['deletedOnly'],
614 [ 'class' => 'mw-input' ]
619 $filters[] = Html
::rawElement(
621 [ 'class' => 'mw-input-with-label' ],
623 $this->msg( 'sp-contributions-toponly' )->text(),
626 $this->opts
['topOnly'],
627 [ 'class' => 'mw-input' ]
630 $filters[] = Html
::rawElement(
632 [ 'class' => 'mw-input-with-label' ],
634 $this->msg( 'sp-contributions-newonly' )->text(),
637 $this->opts
['newOnly'],
638 [ 'class' => 'mw-input' ]
641 $filters[] = Html
::rawElement(
643 [ 'class' => 'mw-input-with-label' ],
645 $this->msg( 'sp-contributions-hideminor' )->text(),
647 'mw-hide-minor-edits',
648 $this->opts
['hideMinor'],
649 [ 'class' => 'mw-input' ]
654 'SpecialContributions::getForm::filters',
658 $extraOptions = Html
::rawElement(
661 implode( '', $filters )
664 $dateRangeSelection = Html
::rawElement(
667 Xml
::label( wfMessage( 'date-range-from' )->text(), 'mw-date-start' ) . ' ' .
668 new \Mediawiki\Widget\
DateInputWidget( [
670 'id' => 'mw-date-start',
672 'value' => $this->opts
['start'],
673 'longDisplayFormat' => true,
675 Xml
::label( wfMessage( 'date-range-to' )->text(), 'mw-date-end' ) . ' ' .
676 new \Mediawiki\Widget\
DateInputWidget( [
678 'id' => 'mw-date-end',
680 'value' => $this->opts
['end'],
681 'longDisplayFormat' => true,
685 $submit = Xml
::tags( 'div', [],
687 $this->msg( 'sp-contributions-submit' )->text(),
688 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
692 $form .= Xml
::fieldset(
693 $this->msg( 'sp-contributions-search' )->text(),
695 $namespaceSelection .
698 $dateRangeSelection .
700 [ 'class' => 'mw-contributions-table' ]
703 $explain = $this->msg( 'sp-contributions-explain' );
704 if ( !$explain->isBlank() ) {
705 $form .= "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>";
708 $form .= Xml
::closeElement( 'form' );
714 * Return an array of subpages beginning with $search that this special page will accept.
716 * @param string $search Prefix to search for
717 * @param int $limit Maximum number of results to return (usually 10)
718 * @param int $offset Number of results to skip (usually 0)
719 * @return string[] Matching subpages
721 public function prefixSearchSubpages( $search, $limit, $offset ) {
722 $user = User
::newFromName( $search );
724 // No prefix suggestion for invalid user
727 // Autocomplete subpage as user list - public to allow caching
728 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
731 protected function getGroupName() {