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( 'mediawiki.special' );
41 $this->addHelpLink( 'Help:User contributions' );
44 $request = $this->getRequest();
46 if ( $par !== null ) {
49 $target = $request->getVal( 'target' );
52 if ( $request->getVal( 'contribs' ) == 'newbie' ||
$par === 'newbies' ) {
54 $this->opts
['contribs'] = 'newbie';
56 $this->opts
['contribs'] = 'user';
59 $this->opts
['deletedOnly'] = $request->getBool( 'deletedOnly' );
61 if ( !strlen( $target ) ) {
62 if ( !$this->including() ) {
63 $out->addHTML( $this->getForm() );
69 $user = $this->getUser();
71 $this->opts
['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
72 $this->opts
['target'] = $target;
73 $this->opts
['topOnly'] = $request->getBool( 'topOnly' );
74 $this->opts
['newOnly'] = $request->getBool( 'newOnly' );
75 $this->opts
['hideMinor'] = $request->getBool( 'hideMinor' );
77 $nt = Title
::makeTitleSafe( NS_USER
, $target );
79 $out->addHTML( $this->getForm() );
83 $userObj = User
::newFromName( $nt->getText(), false );
85 $out->addHTML( $this->getForm() );
89 $id = $userObj->getId();
91 if ( $this->opts
['contribs'] != 'newbie' ) {
92 $target = $nt->getText();
93 $out->addSubtitle( $this->contributionsSub( $userObj ) );
94 $out->setHTMLTitle( $this->msg(
96 $this->msg( 'contributions-title', $target )->plain()
97 )->inContentLanguage() );
98 $this->getSkin()->setRelevantUser( $userObj );
100 $out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub' ) );
101 $out->setHTMLTitle( $this->msg(
103 $this->msg( 'sp-contributions-newbies-title' )->plain()
104 )->inContentLanguage() );
107 $ns = $request->getVal( 'namespace', null );
108 if ( $ns !== null && $ns !== '' ) {
109 $this->opts
['namespace'] = intval( $ns );
111 $this->opts
['namespace'] = '';
114 $this->opts
['associated'] = $request->getBool( 'associated' );
115 $this->opts
['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
116 $this->opts
['tagfilter'] = (string)$request->getVal( 'tagfilter' );
118 // Allows reverts to have the bot flag in recent changes. It is just here to
119 // be passed in the form at the top of the page
120 if ( $user->isAllowed( 'markbotedits' ) && $request->getBool( 'bot' ) ) {
121 $this->opts
['bot'] = '1';
124 $skip = $request->getText( 'offset' ) ||
$request->getText( 'dir' ) == 'prev';
125 # Offset overrides year/month selection
127 $this->opts
['year'] = '';
128 $this->opts
['month'] = '';
130 $this->opts
['year'] = $request->getIntOrNull( 'year' );
131 $this->opts
['month'] = $request->getIntOrNull( 'month' );
134 $feedType = $request->getVal( 'feed' );
137 'action' => 'feedcontributions',
140 if ( $this->opts
['topOnly'] ) {
141 $feedParams['toponly'] = true;
143 if ( $this->opts
['newOnly'] ) {
144 $feedParams['newonly'] = true;
146 if ( $this->opts
['hideMinor'] ) {
147 $feedParams['hideminor'] = true;
149 if ( $this->opts
['deletedOnly'] ) {
150 $feedParams['deletedonly'] = true;
152 if ( $this->opts
['tagfilter'] !== '' ) {
153 $feedParams['tagfilter'] = $this->opts
['tagfilter'];
155 if ( $this->opts
['namespace'] !== '' ) {
156 $feedParams['namespace'] = $this->opts
['namespace'];
158 // Don't use year and month for the feed URL, but pass them on if
159 // we redirect to API (if $feedType is specified)
160 if ( $feedType && $this->opts
['year'] !== null ) {
161 $feedParams['year'] = $this->opts
['year'];
163 if ( $feedType && $this->opts
['month'] !== null ) {
164 $feedParams['month'] = $this->opts
['month'];
168 // Maintain some level of backwards compatibility
169 // If people request feeds using the old parameters, redirect to API
170 $feedParams['feedformat'] = $feedType;
171 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
173 $out->redirect( $url, '301' );
178 // Add RSS/atom links
179 $this->addFeedLinks( $feedParams );
181 if ( Hooks
::run( 'SpecialContributionsBeforeMainOutput', [ $id, $userObj, $this ] ) ) {
182 if ( !$this->including() ) {
183 $out->addHTML( $this->getForm() );
185 $pager = new ContribsPager( $this->getContext(), [
187 'contribs' => $this->opts
['contribs'],
188 'namespace' => $this->opts
['namespace'],
189 'tagfilter' => $this->opts
['tagfilter'],
190 'year' => $this->opts
['year'],
191 'month' => $this->opts
['month'],
192 'deletedOnly' => $this->opts
['deletedOnly'],
193 'topOnly' => $this->opts
['topOnly'],
194 'newOnly' => $this->opts
['newOnly'],
195 'hideMinor' => $this->opts
['hideMinor'],
196 'nsInvert' => $this->opts
['nsInvert'],
197 'associated' => $this->opts
['associated'],
200 if ( !$pager->getNumRows() ) {
201 $out->addWikiMsg( 'nocontribs', $target );
203 # Show a message about slave lag, if applicable
204 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
206 $out->showLagWarning( $lag );
209 $output = $pager->getBody();
210 if ( !$this->including() ) {
211 $output = '<p>' . $pager->getNavigationBar() . '</p>' .
213 '<p>' . $pager->getNavigationBar() . '</p>';
215 $out->addHTML( $output );
217 $out->preventClickjacking( $pager->getPreventClickjacking() );
219 # Show the appropriate "footer" message - WHOIS tools, etc.
220 if ( $this->opts
['contribs'] == 'newbie' ) {
221 $message = 'sp-contributions-footer-newbies';
222 } elseif ( IP
::isIPAddress( $target ) ) {
223 $message = 'sp-contributions-footer-anon';
224 } elseif ( $userObj->isAnon() ) {
225 // No message for non-existing users
228 $message = 'sp-contributions-footer';
232 if ( !$this->including() ) {
233 if ( !$this->msg( $message, $target )->isDisabled() ) {
235 "<div class='mw-contributions-footer'>\n$1\n</div>",
236 [ $message, $target ] );
244 * Generates the subheading with links
245 * @param User $userObj User object for the target
246 * @return string Appropriately-escaped HTML to be output literally
247 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
250 protected function contributionsSub( $userObj ) {
251 if ( $userObj->isAnon() ) {
252 // Show a warning message that the user being searched for doesn't exists
253 if ( !User
::isIP( $userObj->getName() ) ) {
254 $this->getOutput()->wrapWikiMsg(
255 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
257 'contributions-userdoesnotexist',
258 wfEscapeWikiText( $userObj->getName() ),
261 if ( !$this->including() ) {
262 $this->getOutput()->setStatusCode( 404 );
265 $user = htmlspecialchars( $userObj->getName() );
267 $user = Linker
::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
269 $nt = $userObj->getUserPage();
270 $talk = $userObj->getTalkPage();
273 $tools = $this->getUserLinks( $nt, $talk, $userObj );
274 $links = $this->getLanguage()->pipeList( $tools );
276 // Show a note if the user is blocked and display the last block log entry.
277 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
278 // and also this will display a totally irrelevant log entry as a current block.
279 if ( !$this->including() ) {
280 $block = Block
::newFromTarget( $userObj, $userObj );
281 if ( !is_null( $block ) && $block->getType() != Block
::TYPE_AUTO
) {
282 if ( $block->getType() == Block
::TYPE_RANGE
) {
283 $nt = MWNamespace
::getCanonicalName( NS_USER
) . ':' . $block->getTarget();
286 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
287 LogEventsList
::showLogExtract(
294 'showIfEmpty' => false,
297 'sp-contributions-blocked-notice-anon' :
298 'sp-contributions-blocked-notice',
299 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
301 'offset' => '' # don't use WebRequest parameter offset
308 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
312 * Links to different places.
313 * @param Title $userpage Target user page
314 * @param Title $talkpage Talk page
315 * @param User $target Target user object
318 public function getUserLinks( Title
$userpage, Title
$talkpage, User
$target ) {
320 $id = $target->getId();
321 $username = $target->getName();
323 $tools[] = Linker
::link( $talkpage, $this->msg( 'sp-contributions-talk' )->escaped() );
325 if ( ( $id !== null ) ||
( $id === null && IP
::isIPAddress( $username ) ) ) {
326 if ( $this->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
327 if ( $target->isBlocked() && $target->getBlock()->getType() != Block
::TYPE_AUTO
) {
328 $tools[] = Linker
::linkKnown( # Change block link
329 SpecialPage
::getTitleFor( 'Block', $username ),
330 $this->msg( 'change-blocklink' )->escaped()
332 $tools[] = Linker
::linkKnown( # Unblock link
333 SpecialPage
::getTitleFor( 'Unblock', $username ),
334 $this->msg( 'unblocklink' )->escaped()
336 } else { # User is not blocked
337 $tools[] = Linker
::linkKnown( # Block link
338 SpecialPage
::getTitleFor( 'Block', $username ),
339 $this->msg( 'blocklink' )->escaped()
345 $tools[] = Linker
::linkKnown(
346 SpecialPage
::getTitleFor( 'Log', 'block' ),
347 $this->msg( 'sp-contributions-blocklog' )->escaped(),
349 [ 'page' => $userpage->getPrefixedText() ]
352 # Suppression log link (bug 59120)
353 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
354 $tools[] = Linker
::linkKnown(
355 SpecialPage
::getTitleFor( 'Log', 'suppress' ),
356 $this->msg( 'sp-contributions-suppresslog' )->escaped(),
358 [ 'offender' => $username ]
363 $tools[] = Linker
::linkKnown(
364 SpecialPage
::getTitleFor( 'Listfiles', $username ),
365 $this->msg( 'sp-contributions-uploads' )->escaped()
369 $tools[] = Linker
::linkKnown(
370 SpecialPage
::getTitleFor( 'Log', $username ),
371 $this->msg( 'sp-contributions-logs' )->escaped()
374 # Add link to deleted user contributions for priviledged users
375 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
376 $tools[] = Linker
::linkKnown(
377 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
378 $this->msg( 'sp-contributions-deleted' )->escaped()
382 # Add a link to change user rights for privileged users
383 $userrightsPage = new UserrightsPage();
384 $userrightsPage->setContext( $this->getContext() );
385 if ( $userrightsPage->userCanChangeRights( $target ) ) {
386 $tools[] = Linker
::linkKnown(
387 SpecialPage
::getTitleFor( 'Userrights', $username ),
388 $this->msg( 'sp-contributions-userrights' )->escaped()
392 Hooks
::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools ] );
398 * Generates the namespace selector form with hidden attributes.
399 * @return string HTML fragment
401 protected function getForm() {
402 $this->opts
['title'] = $this->getPageTitle()->getPrefixedText();
403 if ( !isset( $this->opts
['target'] ) ) {
404 $this->opts
['target'] = '';
406 $this->opts
['target'] = str_replace( '_', ' ', $this->opts
['target'] );
409 if ( !isset( $this->opts
['namespace'] ) ) {
410 $this->opts
['namespace'] = '';
413 if ( !isset( $this->opts
['nsInvert'] ) ) {
414 $this->opts
['nsInvert'] = '';
417 if ( !isset( $this->opts
['associated'] ) ) {
418 $this->opts
['associated'] = false;
421 if ( !isset( $this->opts
['contribs'] ) ) {
422 $this->opts
['contribs'] = 'user';
425 if ( !isset( $this->opts
['year'] ) ) {
426 $this->opts
['year'] = '';
429 if ( !isset( $this->opts
['month'] ) ) {
430 $this->opts
['month'] = '';
433 if ( $this->opts
['contribs'] == 'newbie' ) {
434 $this->opts
['target'] = '';
437 if ( !isset( $this->opts
['tagfilter'] ) ) {
438 $this->opts
['tagfilter'] = '';
441 if ( !isset( $this->opts
['topOnly'] ) ) {
442 $this->opts
['topOnly'] = false;
445 if ( !isset( $this->opts
['newOnly'] ) ) {
446 $this->opts
['newOnly'] = false;
449 if ( !isset( $this->opts
['hideMinor'] ) ) {
450 $this->opts
['hideMinor'] = false;
453 $form = Html
::openElement(
457 'action' => wfScript(),
458 'class' => 'mw-contributions-form'
462 # Add hidden params for tracking except for parameters in $skipParameters
478 foreach ( $this->opts
as $name => $value ) {
479 if ( in_array( $name, $skipParameters ) ) {
482 $form .= "\t" . Html
::hidden( $name, $value ) . "\n";
485 $tagFilter = ChangeTags
::buildTagFilterSelector( $this->opts
['tagfilter'] );
488 $filterSelection = Html
::rawElement(
491 implode( ' ', $tagFilter )
494 $filterSelection = Html
::rawElement( 'td', [ 'colspan' => 2 ], '' );
497 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
499 $labelNewbies = Xml
::radioLabel(
500 $this->msg( 'sp-contributions-newbies' )->text(),
504 $this->opts
['contribs'] == 'newbie',
505 [ 'class' => 'mw-input' ]
507 $labelUsername = Xml
::radioLabel(
508 $this->msg( 'sp-contributions-username' )->text(),
512 $this->opts
['contribs'] == 'user',
513 [ 'class' => 'mw-input' ]
515 $input = Html
::input(
517 $this->opts
['target'],
524 'mw-ui-input-inline',
525 'mw-autocomplete-user', // used by mediawiki.userSuggest
528 // Only autofocus if target hasn't been specified or in non-newbies mode
529 ( $this->opts
['contribs'] === 'newbie' ||
$this->opts
['target'] )
530 ?
[] : [ 'autofocus' => true ]
534 $targetSelection = Html
::rawElement(
537 $labelNewbies . '<br />' . $labelUsername . ' ' . $input . ' '
540 $namespaceSelection = Xml
::tags(
544 $this->msg( 'namespace' )->text(),
548 Html
::namespaceSelector(
549 [ 'selected' => $this->opts
['namespace'], 'all' => '' ],
551 'name' => 'namespace',
553 'class' => 'namespaceselector',
558 [ 'class' => 'mw-input-with-label' ],
560 $this->msg( 'invert' )->text(),
563 $this->opts
['nsInvert'],
565 'title' => $this->msg( 'tooltip-invert' )->text(),
566 'class' => 'mw-input'
570 Html
::rawElement( 'span', [ 'class' => 'mw-input-with-label' ],
572 $this->msg( 'namespace_association' )->text(),
575 $this->opts
['associated'],
577 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
578 'class' => 'mw-input'
586 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
587 $filters[] = Html
::rawElement(
589 [ 'class' => 'mw-input-with-label' ],
591 $this->msg( 'history-show-deleted' )->text(),
593 'mw-show-deleted-only',
594 $this->opts
['deletedOnly'],
595 [ 'class' => 'mw-input' ]
600 $filters[] = Html
::rawElement(
602 [ 'class' => 'mw-input-with-label' ],
604 $this->msg( 'sp-contributions-toponly' )->text(),
607 $this->opts
['topOnly'],
608 [ 'class' => 'mw-input' ]
611 $filters[] = Html
::rawElement(
613 [ 'class' => 'mw-input-with-label' ],
615 $this->msg( 'sp-contributions-newonly' )->text(),
618 $this->opts
['newOnly'],
619 [ 'class' => 'mw-input' ]
622 $filters[] = Html
::rawElement(
624 [ 'class' => 'mw-input-with-label' ],
626 $this->msg( 'sp-contributions-hideminor' )->text(),
628 'mw-hide-minor-edits',
629 $this->opts
['hideMinor'],
630 [ 'class' => 'mw-input' ]
635 'SpecialContributions::getForm::filters',
639 $extraOptions = Html
::rawElement(
642 implode( '', $filters )
645 $dateSelectionAndSubmit = Xml
::tags( 'td', [ 'colspan' => 2 ],
647 $this->opts
['year'] === '' ? MWTimestamp
::getInstance()->format( 'Y' ) : $this->opts
['year'],
651 $this->msg( 'sp-contributions-submit' )->text(),
652 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
656 $form .= Xml
::fieldset( $this->msg( 'sp-contributions-search' )->text() );
657 $form .= Html
::rawElement( 'table', [ 'class' => 'mw-contributions-table' ], "\n" .
658 Html
::rawElement( 'tr', [], $targetSelection ) . "\n" .
659 Html
::rawElement( 'tr', [], $namespaceSelection ) . "\n" .
660 Html
::rawElement( 'tr', [], $filterSelection ) . "\n" .
661 Html
::rawElement( 'tr', [], $extraOptions ) . "\n" .
662 Html
::rawElement( 'tr', [], $dateSelectionAndSubmit ) . "\n"
665 $explain = $this->msg( 'sp-contributions-explain' );
666 if ( !$explain->isBlank() ) {
667 $form .= "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>";
670 $form .= Xml
::closeElement( 'fieldset' ) . Xml
::closeElement( 'form' );
676 * Return an array of subpages beginning with $search that this special page will accept.
678 * @param string $search Prefix to search for
679 * @param int $limit Maximum number of results to return (usually 10)
680 * @param int $offset Number of results to skip (usually 0)
681 * @return string[] Matching subpages
683 public function prefixSearchSubpages( $search, $limit, $offset ) {
684 $user = User
::newFromName( $search );
686 // No prefix suggestion for invalid user
689 // Autocomplete subpage as user list - public to allow caching
690 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
693 protected function getGroupName() {