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
30 class SpecialContributions
extends SpecialPage
{
34 public function __construct() {
35 parent
::__construct( 'Contributions' );
38 public function execute( $par ) {
39 global $wgUser, $wgOut, $wgRequest;
42 $this->outputHeader();
43 $wgOut->addModuleStyles( 'mediawiki.special' );
45 $this->opts
= array();
47 if( $par == 'newbies' ) {
49 $this->opts
['contribs'] = 'newbie';
50 } elseif( isset( $par ) ) {
53 $target = $wgRequest->getVal( 'target' );
57 if( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
59 $this->opts
['contribs'] = 'newbie';
62 $this->opts
['deletedOnly'] = $wgRequest->getBool( 'deletedOnly' );
64 if( !strlen( $target ) ) {
65 $wgOut->addHTML( $this->getForm() );
69 $this->opts
['limit'] = $wgRequest->getInt( 'limit', $wgUser->getOption('rclimit') );
70 $this->opts
['target'] = $target;
71 $this->opts
['topOnly'] = $wgRequest->getBool( 'topOnly' );
73 $nt = Title
::makeTitleSafe( NS_USER
, $target );
75 $wgOut->addHTML( $this->getForm() );
78 $id = User
::idFromName( $nt->getText() );
80 if( $target != 'newbies' ) {
81 $target = $nt->getText();
82 $wgOut->setSubtitle( $this->contributionsSub( $nt, $id ) );
83 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsgExt( 'contributions-title', array( 'parsemag' ),$target ) ) );
84 $user = User
::newFromName( $target, false );
85 if ( is_object( $user ) ) {
86 $this->getSkin()->setRelevantUser( $user );
89 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
90 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'sp-contributions-newbies-title' ) ) );
93 if( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
94 $this->opts
['namespace'] = intval( $ns );
96 $this->opts
['namespace'] = '';
99 $this->opts
['tagFilter'] = (string) $wgRequest->getVal( 'tagFilter' );
101 // Allows reverts to have the bot flag in recent changes. It is just here to
102 // be passed in the form at the top of the page
103 if( $wgUser->isAllowed( 'markbotedits' ) && $wgRequest->getBool( 'bot' ) ) {
104 $this->opts
['bot'] = '1';
107 $skip = $wgRequest->getText( 'offset' ) ||
$wgRequest->getText( 'dir' ) == 'prev';
108 # Offset overrides year/month selection
110 $this->opts
['year'] = '';
111 $this->opts
['month'] = '';
113 $this->opts
['year'] = $wgRequest->getIntOrNull( 'year' );
114 $this->opts
['month'] = $wgRequest->getIntOrNull( 'month' );
117 $feedType = $wgRequest->getVal( 'feed' );
119 // Maintain some level of backwards compatability
120 // If people request feeds using the old parameters, redirect to API
122 'action' => 'feedcontributions',
123 'feedformat' => $feedType,
126 if ( $this->opts
['topOnly'] ) {
127 $apiParams['toponly'] = true;
129 if ( $this->opts
['deletedOnly'] ) {
130 $apiParams['deletedonly'] = true;
132 if ( $this->opts
['tagFilter'] !== '' ) {
133 $apiParams['tagfilter'] = $this->opts
['tagFilter'];
135 if ( $this->opts
['namespace'] !== '' ) {
136 $apiParams['namespace'] = $this->opts
['namespace'];
138 if ( $this->opts
['year'] !== null ) {
139 $apiParams['year'] = $this->opts
['year'];
141 if ( $this->opts
['month'] !== null ) {
142 $apiParams['month'] = $this->opts
['month'];
145 $url = wfScript( 'api' ) . '?' . wfArrayToCGI( $apiParams );
147 $wgOut->redirect( $url, '301' );
151 // Add RSS/atom links
152 $this->addFeedLinks( array( 'action' => 'feedcontributions', 'user' => $target ) );
154 if ( wfRunHooks( 'SpecialContributionsBeforeMainOutput', array( $id ) ) ) {
156 $wgOut->addHTML( $this->getForm() );
158 $pager = new ContribsPager( array(
160 'namespace' => $this->opts
['namespace'],
161 'year' => $this->opts
['year'],
162 'month' => $this->opts
['month'],
163 'deletedOnly' => $this->opts
['deletedOnly'],
164 'topOnly' => $this->opts
['topOnly'],
166 if( !$pager->getNumRows() ) {
167 $wgOut->addWikiMsg( 'nocontribs', $target );
169 # Show a message about slave lag, if applicable
170 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
172 $wgOut->showLagWarning( $lag );
175 '<p>' . $pager->getNavigationBar() . '</p>' .
177 '<p>' . $pager->getNavigationBar() . '</p>'
180 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
182 # Show the appropriate "footer" message - WHOIS tools, etc.
183 if( $target != 'newbies' ) {
184 $message = 'sp-contributions-footer';
185 if ( IP
::isIPAddress( $target ) ) {
186 $message = 'sp-contributions-footer-anon';
188 $user = User
::newFromName( $target );
189 if ( !$user ||
$user->isAnon() ) {
190 // No message for non-existing users
195 if( !wfMessage( $message, $target )->isDisabled() ) {
197 "<div class='mw-contributions-footer'>\n$1\n</div>",
198 array( $message, $target ) );
205 * Generates the subheading with links
206 * @param $nt Title object for the target
207 * @param $id Integer: User ID for the target
208 * @return String: appropriately-escaped HTML to be output literally
209 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php. Could be combined.
211 protected function contributionsSub( $nt, $id ) {
212 global $wgLang, $wgUser, $wgOut;
214 $sk = $this->getSkin();
216 if ( $id === null ) {
217 $user = htmlspecialchars( $nt->getText() );
219 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
221 $userObj = User
::newFromName( $nt->getText(), /* check for username validity not needed */ false );
222 $talk = $nt->getTalkPage();
224 $tools = self
::getUserLinks( $nt, $talk, $userObj, $wgUser );
225 $links = $wgLang->pipeList( $tools );
227 // Show a note if the user is blocked and display the last block log entry.
228 if ( $userObj->isBlocked() ) {
229 LogEventsList
::showLogExtract(
232 $nt->getPrefixedText(),
236 'showIfEmpty' => false,
239 'sp-contributions-blocked-notice-anon' :
240 'sp-contributions-blocked-notice',
241 $nt->getText() # Support GENDER in 'sp-contributions-blocked-notice'
243 'offset' => '' # don't use $wgRequest parameter offset
249 // Old message 'contribsub' had one parameter, but that doesn't work for
250 // languages that want to put the "for" bit right after $user but before
251 // $links. If 'contribsub' is around, use it for reverse compatibility,
252 // otherwise use 'contribsub2'.
253 if( wfEmptyMsg( 'contribsub' ) ) {
254 return wfMsgHtml( 'contribsub2', $user, $links );
256 return wfMsgHtml( 'contribsub', "$user ($links)" );
261 * Links to different places.
262 * @param $userpage Title: Target user page
263 * @param $talkpage Title: Talk page
264 * @param $target User: Target user object
265 * @param $subject User: The viewing user ($wgUser is still checked in some cases, like userrights page!!)
267 public static function getUserLinks( Title
$userpage, Title
$talkpage, User
$target, User
$subject ) {
269 $sk = $subject->getSkin();
270 $id = $target->getId();
271 $username = $target->getName();
273 $tools[] = $sk->link( $talkpage, wfMsgHtml( 'sp-contributions-talk' ) );
275 if( ( $id !== null ) ||
( $id === null && IP
::isIPAddress( $username ) ) ) {
276 if( $subject->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
277 if ( $target->isBlocked() ) {
278 $tools[] = $sk->linkKnown( # Change block link
279 SpecialPage
::getTitleFor( 'Block', $username ),
280 wfMsgHtml( 'change-blocklink' )
282 $tools[] = $sk->linkKnown( # Unblock link
283 SpecialPage
::getTitleFor( 'Unblock', $username ),
284 wfMsgHtml( 'unblocklink' )
286 } else { # User is not blocked
287 $tools[] = $sk->linkKnown( # Block link
288 SpecialPage
::getTitleFor( 'Block', $username ),
289 wfMsgHtml( 'blocklink' )
294 $tools[] = $sk->linkKnown(
295 SpecialPage
::getTitleFor( 'Log', 'block' ),
296 wfMsgHtml( 'sp-contributions-blocklog' ),
299 'page' => $userpage->getPrefixedText()
304 $tools[] = $sk->linkKnown(
305 SpecialPage
::getTitleFor( 'Listfiles', $username ),
306 wfMsgHtml( 'sp-contributions-uploads' )
310 $tools[] = $sk->linkKnown(
311 SpecialPage
::getTitleFor( 'Log', $username ),
312 wfMsgHtml( 'sp-contributions-logs' )
315 # Add link to deleted user contributions for priviledged users
316 if( $subject->isAllowed( 'deletedhistory' ) && !$subject->isBlocked() ) {
317 $tools[] = $sk->linkKnown(
318 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
319 wfMsgHtml( 'sp-contributions-deleted' )
323 # Add a link to change user rights for privileged users
324 $userrightsPage = new UserrightsPage();
325 if( $id !== null && $userrightsPage->userCanChangeRights( $target ) ) {
326 $tools[] = $sk->linkKnown(
327 SpecialPage
::getTitleFor( 'Userrights', $username ),
328 wfMsgHtml( 'sp-contributions-userrights' )
332 wfRunHooks( 'ContributionsToolLinks', array( $id, $userpage, &$tools ) );
337 * Generates the namespace selector form with hidden attributes.
338 * @return String: HTML fragment
340 protected function getForm() {
341 global $wgScript, $wgMiserMode;
343 $this->opts
['title'] = $this->getTitle()->getPrefixedText();
344 if( !isset( $this->opts
['target'] ) ) {
345 $this->opts
['target'] = '';
347 $this->opts
['target'] = str_replace( '_' , ' ' , $this->opts
['target'] );
350 if( !isset( $this->opts
['namespace'] ) ) {
351 $this->opts
['namespace'] = '';
354 if( !isset( $this->opts
['contribs'] ) ) {
355 $this->opts
['contribs'] = 'user';
358 if( !isset( $this->opts
['year'] ) ) {
359 $this->opts
['year'] = '';
362 if( !isset( $this->opts
['month'] ) ) {
363 $this->opts
['month'] = '';
366 if( $this->opts
['contribs'] == 'newbie' ) {
367 $this->opts
['target'] = '';
370 if( !isset( $this->opts
['tagFilter'] ) ) {
371 $this->opts
['tagFilter'] = '';
374 if( !isset( $this->opts
['topOnly'] ) ) {
375 $this->opts
['topOnly'] = false;
378 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'class' => 'mw-contributions-form' ) );
380 # Add hidden params for tracking except for parameters in $skipParameters
381 $skipParameters = array( 'namespace', 'deletedOnly', 'target', 'contribs', 'year', 'month', 'topOnly' );
382 foreach ( $this->opts
as $name => $value ) {
383 if( in_array( $name, $skipParameters ) ) {
386 $f .= "\t" . Html
::hidden( $name, $value ) . "\n";
389 $tagFilter = ChangeTags
::buildTagFilterSelector( $this->opts
['tagFilter'] );
392 if ( !$wgMiserMode ) {
393 $fNS = Html
::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
394 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' . Xml
::namespaceSelector( $this->opts
['namespace'], '' )
398 $f .= Xml
::fieldset( wfMsg( 'sp-contributions-search' ) ) .
399 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parsemag' ) ),
400 'contribs', 'newbie' , 'newbie', $this->opts
['contribs'] == 'newbie' ) . '<br />' .
401 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parsemag' ) ),
402 'contribs' , 'user', 'user', $this->opts
['contribs'] == 'user' ) . ' ' .
403 Html
::input( 'target', $this->opts
['target'], 'text', array(
406 ) +
( $this->opts
['target'] ?
array() : array( 'autofocus' ) ) ) . ' '.
408 Xml
::checkLabel( wfMsg( 'history-show-deleted' ),
409 'deletedOnly', 'mw-show-deleted-only', $this->opts
['deletedOnly'] ) . '<br />' .
410 Xml
::tags( 'p', null, Xml
::checkLabel( wfMsg( 'sp-contributions-toponly' ),
411 'topOnly', 'mw-show-top-only', $this->opts
['topOnly'] ) ) .
412 ( $tagFilter ? Xml
::tags( 'p', null, implode( ' ', $tagFilter ) ) : '' ) .
413 Html
::rawElement( 'p', array( 'style' => 'white-space: nowrap' ),
414 Xml
::dateMenu( $this->opts
['year'], $this->opts
['month'] ) . ' ' .
415 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) )
417 $explain = wfMessage( 'sp-contributions-explain' );
418 if ( $explain->exists() ) {
419 $f .= "<p id='mw-sp-contributions-explain'>{$explain}</p>";
421 $f .= Xml
::closeElement('fieldset' ) .
422 Xml
::closeElement( 'form' );
428 * Pager for Special:Contributions
429 * @ingroup SpecialPage Pager
431 class ContribsPager
extends ReverseChronologicalPager
{
432 public $mDefaultDirection = true;
433 var $messages, $target;
434 var $namespace = '', $mDb;
435 var $preventClickjacking = false;
437 function __construct( $options ) {
438 parent
::__construct();
440 $msgs = array( 'uctop', 'diff', 'newarticle', 'rollbacklink', 'diff', 'hist', 'rev-delundel', 'pipe-separator' );
442 foreach( $msgs as $msg ) {
443 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
446 $this->target
= isset( $options['target'] ) ?
$options['target'] : '';
447 $this->namespace = isset( $options['namespace'] ) ?
$options['namespace'] : '';
448 $this->tagFilter
= isset( $options['tagFilter'] ) ?
$options['tagFilter'] : false;
450 $this->deletedOnly
= !empty( $options['deletedOnly'] );
451 $this->topOnly
= !empty( $options['topOnly'] );
453 $year = isset( $options['year'] ) ?
$options['year'] : false;
454 $month = isset( $options['month'] ) ?
$options['month'] : false;
455 $this->getDateCond( $year, $month );
457 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
460 function getDefaultQuery() {
461 $query = parent
::getDefaultQuery();
462 $query['target'] = $this->target
;
466 function getTitle() {
467 return SpecialPage
::getTitleFor( 'Contributions' );
470 function getQueryInfo() {
472 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
474 $conds = array_merge( $userCond, $this->getNamespaceCond() );
475 // Paranoia: avoid brute force searches (bug 17342)
476 if( !$wgUser->isAllowed( 'deletedhistory' ) ||
$wgUser->isBlocked() ) {
477 $conds[] = $this->mDb
->bitAnd('rev_deleted',Revision
::DELETED_USER
) . ' = 0';
478 } elseif( !$wgUser->isAllowed( 'suppressrevision' ) ) {
479 $conds[] = $this->mDb
->bitAnd('rev_deleted',Revision
::SUPPRESSED_USER
) .
480 ' != ' . Revision
::SUPPRESSED_USER
;
482 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
487 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
488 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
489 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted',
490 'rc_old_len', 'rc_new_len'
493 'options' => array( 'USE INDEX' => array('revision' => $index) ),
494 'join_conds' => $join_cond
496 ChangeTags
::modifyDisplayQuery(
497 $queryInfo['tables'],
498 $queryInfo['fields'],
500 $queryInfo['join_conds'],
501 $queryInfo['options'],
505 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
509 function getUserCond() {
510 $condition = array();
511 $join_conds = array();
513 if( $this->target
== 'newbies' ) {
514 $tables = array( 'recentchanges', 'user_groups', 'page', 'revision' );
515 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
516 $condition[] = 'rev_user >' . (int)($max - $max / 100);
517 $condition[] = 'ug_group IS NULL';
518 $index = 'user_timestamp';
519 # @todo FIXME: Other groups may have 'bot' rights
520 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
521 $join_conds['recentchanges'] = array( 'INNER JOIN', "rev_id = rc_this_oldid AND rev_timestamp = rc_timestamp" );
523 $tables = array( 'recentchanges', 'page', 'revision' );
524 $condition['rev_user_text'] = $this->target
;
525 $join_conds['recentchanges'] = array( 'INNER JOIN', "rev_id = rc_this_oldid AND rev_timestamp = rc_timestamp" );
526 $index = 'usertext_timestamp';
528 if( $this->deletedOnly
) {
529 $condition[] = "rev_deleted != '0'";
531 if( $this->topOnly
) {
532 $condition[] = "rev_id = page_latest";
534 return array( $tables, $index, $condition, $join_conds );
537 function getNamespaceCond() {
539 if( $this->namespace !== '' && !$wgMiserMode ) {
540 return array( 'page_namespace' => (int)$this->namespace );
546 function getIndexField() {
547 return 'rev_timestamp';
550 function getStartBody() {
554 function getEndBody() {
559 * Generates each row in the contributions list.
561 * Contributions which are marked "top" are currently on top of the history.
562 * For these contributions, a [rollback] link is shown for users with roll-
563 * back privileges. The rollback link restores the most recent version that
564 * was not written by the target user.
566 * @todo This would probably look a lot nicer in a table.
568 function formatRow( $row ) {
569 global $wgUser, $wgLang;
570 wfProfileIn( __METHOD__
);
572 $sk = $this->getSkin();
573 $rev = new Revision( $row );
576 $page = Title
::newFromRow( $row );
579 htmlspecialchars( $page->getPrefixedText() ),
581 $page->isRedirect() ?
array( 'redirect' => 'no' ) : array()
583 # Mark current revisions
585 if( $row->rev_id
== $row->page_latest
) {
586 $topmarktext .= '<span class="mw-uctop">' . $this->messages
['uctop'] . '</span>';
588 if( !$row->page_is_new
&& $page->quickUserCan( 'rollback' )
589 && $page->quickUserCan( 'edit' ) )
591 $this->preventClickjacking();
592 $topmarktext .= ' '.$sk->generateRollback( $rev );
595 # Is there a visible previous revision?
596 if( $rev->userCan( Revision
::DELETED_TEXT
) && $rev->getParentId() !== 0 ) {
597 $difftext = $sk->linkKnown(
599 $this->messages
['diff'],
603 'oldid' => $row->rev_id
607 $difftext = $this->messages
['diff'];
609 $histlink = $sk->linkKnown(
611 $this->messages
['hist'],
613 array( 'action' => 'history' )
616 $comment = $wgLang->getDirMark() . $sk->revComment( $rev, false, true );
617 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
618 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
621 htmlspecialchars($date),
623 array( 'oldid' => intval( $row->rev_id
) )
626 $d = htmlspecialchars( $date );
628 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
629 $d = '<span class="history-deleted">' . $d . '</span>';
632 if( $this->target
== 'newbies' ) {
633 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
634 $userlink .= ' ' . wfMsg( 'parentheses', $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) ) . ' ';
639 if( $rev->getParentId() === 0 ) {
640 $nflag = ChangesList
::flag( 'newpage' );
645 if( $rev->isMinor() ) {
646 $mflag = ChangesList
::flag( 'minor' );
651 // Don't show useless link to people who cannot hide revisions
652 $canHide = $wgUser->isAllowed( 'deleterevision' );
653 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
654 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
655 $del = $this->mSkin
->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
658 'type' => 'revision',
659 'target' => $page->getPrefixedDbkey(),
660 'ids' => $rev->getId()
662 $del = $this->mSkin
->revDeleteLink( $query,
663 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
670 $diffHistLinks = '(' . $difftext . $this->messages
['pipe-separator'] . $histlink . ')';
672 $diffOut = ' . . ' . $wgLang->getDirMark() .
673 ChangesList
::showCharacterDifference( $row->rc_old_len
, $row->rc_new_len
);
675 $ret = "{$del}{$d} {$diffHistLinks} {$nflag}{$mflag} {$link}{$diffOut}{$userlink} {$comment} {$topmarktext}";
677 # Denote if username is redacted for this edit
678 if( $rev->isDeleted( Revision
::DELETED_USER
) ) {
679 $ret .= " <strong>" . wfMsgHtml('rev-deleted-user-contribs') . "</strong>";
683 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'contributions' );
684 $classes = array_merge( $classes, $newClasses );
685 $ret .= " $tagSummary";
687 // Let extensions add data
688 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
690 $classes = implode( ' ', $classes );
691 $ret = "<li class=\"$classes\">$ret</li>\n";
692 wfProfileOut( __METHOD__
);
697 * Get the Database object in use
699 * @return DatabaseBase
701 public function getDatabase() {
706 * Overwrite Pager function and return a helpful comment
708 function getSqlComment() {
709 if ( $this->namespace ||
$this->deletedOnly
) {
710 return 'contributions page filtered for namespace or RevisionDeleted edits'; // potentially slow, see CR r58153
712 return 'contributions page unfiltered';
716 protected function preventClickjacking() {
717 $this->preventClickjacking
= true;
720 public function getPreventClickjacking() {
721 return $this->preventClickjacking
;