3 * Special:Contributions, show user contributions in a paged list
8 class SpecialContributions
extends SpecialPage
{
10 public function __construct() {
11 parent
::__construct( 'Contributions' );
14 public function execute( $par ) {
15 global $wgUser, $wgOut, $wgLang, $wgRequest;
18 $this->outputHeader();
20 $this->opts
= array();
22 if( $par == 'newbies' ) {
24 $this->opts
['contribs'] = 'newbie';
25 } elseif( isset( $par ) ) {
28 $target = $wgRequest->getVal( 'target' );
32 if( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
34 $this->opts
['contribs'] = 'newbie';
37 $this->opts
['deletedOnly'] = $wgRequest->getCheck( 'deletedOnly' );
39 if( !strlen( $target ) ) {
40 $wgOut->addHTML( $this->getForm() );
44 $this->opts
['limit'] = $wgRequest->getInt( 'limit', $wgUser->getOption('rclimit') );
45 $this->opts
['target'] = $target;
47 $nt = Title
::makeTitleSafe( NS_USER
, $target );
49 $wgOut->addHTML( $this->getForm() );
52 $id = User
::idFromName( $nt->getText() );
54 if( $target != 'newbies' ) {
55 $target = $nt->getText();
56 $wgOut->setSubtitle( $this->contributionsSub( $nt, $id ) );
57 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsgExt( 'contributions-title', array( 'parsemag' ),$target ) ) );
59 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
60 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'sp-contributions-newbies-title' ) ) );
63 if( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
64 $this->opts
['namespace'] = intval( $ns );
66 $this->opts
['namespace'] = '';
69 $this->opts
['tagfilter'] = (string) $wgRequest->getVal( 'tagfilter' );
71 // Allows reverts to have the bot flag in recent changes. It is just here to
72 // be passed in the form at the top of the page
73 if( $wgUser->isAllowed( 'markbotedits' ) && $wgRequest->getBool( 'bot' ) ) {
74 $this->opts
['bot'] = '1';
77 $skip = $wgRequest->getText( 'offset' ) ||
$wgRequest->getText( 'dir' ) == 'prev';
78 # Offset overrides year/month selection
80 $this->opts
['year'] = '';
81 $this->opts
['month'] = '';
83 $this->opts
['year'] = $wgRequest->getIntOrNull( 'year' );
84 $this->opts
['month'] = $wgRequest->getIntOrNull( 'month' );
88 $this->setSyndicated();
89 $feedType = $wgRequest->getVal( 'feed' );
91 return $this->feed( $feedType );
94 if ( wfRunHooks( 'SpecialContributionsBeforeMainOutput', array( $id ) ) ) {
96 $wgOut->addHTML( $this->getForm() );
98 $pager = new ContribsPager( $target, $this->opts
['namespace'], $this->opts
['year'],
99 $this->opts
['month'], false, $this->opts
['deletedOnly'] );
100 if( !$pager->getNumRows() ) {
101 $wgOut->addWikiMsg( 'nocontribs', $target );
103 # Show a message about slave lag, if applicable
104 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
105 $wgOut->showLagWarning( $lag );
108 '<p>' . $pager->getNavigationBar() . '</p>' .
110 '<p>' . $pager->getNavigationBar() . '</p>'
115 # Show the appropriate "footer" message - WHOIS tools, etc.
116 if( $target != 'newbies' ) {
117 $message = 'sp-contributions-footer';
118 if ( IP
::isIPAddress( $target ) ) {
119 $message = 'sp-contributions-footer-anon';
121 $user = User
::newFromName( $target );
122 if ( !$user ||
$user->isAnon() ) {
123 // No message for non-existing users
128 $text = wfMsgNoTrans( $message, $target );
129 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
131 "<div class='mw-contributions-footer'>\n$1\n</div>",
132 array( $message, $target ) );
138 protected function setSyndicated() {
140 $wgOut->setSyndicated( true );
141 $wgOut->setFeedAppendQuery( wfArrayToCGI( $this->opts
) );
145 * Generates the subheading with links
146 * @param $nt Title object for the target
147 * @param $id Integer: User ID for the target
148 * @return String: appropriately-escaped HTML to be output literally
149 * @todo Fixme: almost the same as getSubTitle in SpecialDeletedContributions.php. Could be combined.
151 protected function contributionsSub( $nt, $id ) {
152 global $wgSysopUserBans, $wgLang, $wgUser, $wgOut;
154 $sk = $wgUser->getSkin();
156 if ( $id === null ) {
157 $user = htmlspecialchars( $nt->getText() );
159 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
161 $userObj = User
::newFromName( $nt->getText(), /* check for username validity not needed */ false );
162 $talk = $nt->getTalkPage();
165 $tools[] = $sk->link( $talk, wfMsgHtml( 'sp-contributions-talk' ) );
166 if( ( $id !== null && $wgSysopUserBans ) ||
( $id === null && IP
::isIPAddress( $nt->getText() ) ) ) {
167 if( $wgUser->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
168 if ( $userObj->isBlocked() ) {
169 $tools[] = $sk->linkKnown( # Change block link
170 SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ),
171 wfMsgHtml( 'change-blocklink' )
173 $tools[] = $sk->linkKnown( # Unblock link
174 SpecialPage
::getTitleFor( 'Ipblocklist' ),
175 wfMsgHtml( 'unblocklink' ),
178 'action' => 'unblock',
179 'ip' => $nt->getDBkey()
183 else { # User is not blocked
184 $tools[] = $sk->linkKnown( # Block link
185 SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ),
186 wfMsgHtml( 'blocklink' )
191 $tools[] = $sk->linkKnown(
192 SpecialPage
::getTitleFor( 'Log' ),
193 wfMsgHtml( 'sp-contributions-blocklog' ),
197 'page' => $nt->getPrefixedText()
202 $tools[] = $sk->linkKnown(
203 SpecialPage
::getTitleFor( 'Log' ),
204 wfMsgHtml( 'sp-contributions-logs' ),
206 array( 'user' => $nt->getText() )
209 # Add link to deleted user contributions for priviledged users
210 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
211 $tools[] = $sk->linkKnown(
212 SpecialPage
::getTitleFor( 'DeletedContributions', $nt->getDBkey() ),
213 wfMsgHtml( 'sp-contributions-deleted' )
217 # Add a link to change user rights for privileged users
218 $userrightsPage = new UserrightsPage();
219 if( $id !== null && $userrightsPage->userCanChangeRights( User
::newFromId( $id ) ) ) {
220 $tools[] = $sk->linkKnown(
221 SpecialPage
::getTitleFor( 'Userrights', $nt->getDBkey() ),
222 wfMsgHtml( 'sp-contributions-userrights' )
226 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
228 $links = $wgLang->pipeList( $tools );
230 // Show a note if the user is blocked and display the last block log entry.
231 if ( $userObj->isBlocked() ) {
232 LogEventsList
::showLogExtract(
235 $nt->getPrefixedText(),
239 'showIfEmpty' => false,
242 'sp-contributions-blocked-notice-anon' :
243 'sp-contributions-blocked-notice',
244 $nt->getText() # Support GENDER in 'sp-contributions-blocked-notice'
246 'offset' => '' # don't use $wgRequest parameter offset
252 // Old message 'contribsub' had one parameter, but that doesn't work for
253 // languages that want to put the "for" bit right after $user but before
254 // $links. If 'contribsub' is around, use it for reverse compatibility,
255 // otherwise use 'contribsub2'.
256 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
257 return wfMsgHtml( 'contribsub2', $user, $links );
259 return wfMsgHtml( 'contribsub', "$user ($links)" );
264 * Generates the namespace selector form with hidden attributes.
265 * @return String: HTML fragment
267 protected function getForm() {
270 $this->opts
['title'] = $this->getTitle()->getPrefixedText();
271 if( !isset( $this->opts
['target'] ) ) {
272 $this->opts
['target'] = '';
274 $this->opts
['target'] = str_replace( '_' , ' ' , $this->opts
['target'] );
277 if( !isset( $this->opts
['namespace'] ) ) {
278 $this->opts
['namespace'] = '';
281 if( !isset( $this->opts
['contribs'] ) ) {
282 $this->opts
['contribs'] = 'user';
285 if( !isset( $this->opts
['year'] ) ) {
286 $this->opts
['year'] = '';
289 if( !isset( $this->opts
['month'] ) ) {
290 $this->opts
['month'] = '';
293 if( $this->opts
['contribs'] == 'newbie' ) {
294 $this->opts
['target'] = '';
297 if( !isset( $this->opts
['tagfilter'] ) ) {
298 $this->opts
['tagfilter'] = '';
301 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
303 # Add hidden params for tracking except for parameters in $skipParameters
304 $skipParameters = array( 'namespace', 'deletedOnly', 'target', 'contribs', 'year', 'month' );
305 foreach ( $this->opts
as $name => $value ) {
306 if( in_array( $name, $skipParameters ) ) {
309 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
312 $tagFilter = ChangeTags
::buildTagFilterSelector( $this->opts
['tagfilter'] );
315 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
316 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parsemag' ) ),
317 'contribs', 'newbie' , 'newbie', $this->opts
['contribs'] == 'newbie' ?
true : false ) . '<br />' .
318 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parsemag' ) ),
319 'contribs' , 'user', 'user', $this->opts
['contribs'] == 'user' ?
true : false ) . ' ' .
320 Html
::input( 'target', $this->opts
['target'], 'text', array(
323 ) +
( $this->opts
['target'] ?
array() : array( 'autofocus' ) ) ) . ' '.
324 '<span style="white-space: nowrap">' .
325 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
326 Xml
::namespaceSelector( $this->opts
['namespace'], '' ) .
328 Xml
::checkLabel( wfMsg( 'history-show-deleted' ),
329 'deletedOnly', 'mw-show-deleted-only', $this->opts
['deletedOnly'] ) .
330 ( $tagFilter ? Xml
::tags( 'p', null, implode( ' ', $tagFilter ) ) : '' ) .
331 Xml
::openElement( 'p' ) .
332 '<span style="white-space: nowrap">' .
333 Xml
::dateMenu( $this->opts
['year'], $this->opts
['month'] ) .
335 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
336 Xml
::closeElement( 'p' );
338 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
339 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
340 $f .= "<p id='mw-sp-contributions-explain'>{$explain}</p>";
342 $f .= '</fieldset>' .
343 Xml
::closeElement( 'form' );
348 * Output a subscription feed listing recent edits to this page.
349 * @param $type String
351 protected function feed( $type ) {
352 global $wgRequest, $wgFeed, $wgFeedClasses, $wgFeedLimit;
356 $wgOut->addWikiMsg( 'feed-unavailable' );
360 if( !isset( $wgFeedClasses[$type] ) ) {
362 $wgOut->addWikiMsg( 'feed-invalid' );
366 $feed = new $wgFeedClasses[$type](
368 wfMsgExt( 'tagline', 'parsemag' ),
369 $this->getTitle()->getFullUrl() . "/" . urlencode($this->opts
['target'])
372 // Already valid title
373 $nt = Title
::makeTitleSafe( NS_USER
, $this->opts
['target'] );
374 $target = $this->opts
['target'] == 'newbies' ?
'newbies' : $nt->getText();
376 $pager = new ContribsPager( $target, $this->opts
['namespace'],
377 $this->opts
['year'], $this->opts
['month'], $this->opts
['tagfilter'], $this->opts
['deletedOnly'] );
379 $pager->mLimit
= min( $this->opts
['limit'], $wgFeedLimit );
382 if( $pager->getNumRows() > 0 ) {
383 while( $row = $pager->mResult
->fetchObject() ) {
384 $feed->outItem( $this->feedItem( $row ) );
390 protected function feedTitle() {
391 global $wgContLanguageCode, $wgSitename;
392 $page = SpecialPage
::getPage( 'Contributions' );
393 $desc = $page->getDescription();
394 return "$wgSitename - $desc [$wgContLanguageCode]";
397 protected function feedItem( $row ) {
398 $title = Title
::MakeTitle( intval( $row->page_namespace
), $row->page_title
);
400 $date = $row->rev_timestamp
;
401 $comments = $title->getTalkPage()->getFullURL();
402 $revision = Revision
::newFromTitle( $title, $row->rev_id
);
405 $title->getPrefixedText(),
406 $this->feedItemDesc( $revision ),
407 $title->getFullURL(),
409 $this->feedItemAuthor( $revision ),
417 protected function feedItemAuthor( $revision ) {
418 return $revision->getUserText();
421 protected function feedItemDesc( $revision ) {
423 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
424 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
425 "</p>\n<hr />\n<div>" .
426 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
433 * Pager for Special:Contributions
434 * @ingroup SpecialPage Pager
436 class ContribsPager
extends ReverseChronologicalPager
{
437 public $mDefaultDirection = true;
438 var $messages, $target;
439 var $namespace = '', $mDb;
441 function __construct( $target, $namespace = false, $year = false, $month = false, $tagFilter = false, $deletedOnly = false ) {
442 parent
::__construct();
444 $msgs = array( 'uctop', 'diff', 'newarticle', 'rollbacklink', 'diff', 'hist', 'rev-delundel', 'pipe-separator' );
446 foreach( $msgs as $msg ) {
447 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
450 $this->target
= $target;
451 $this->namespace = $namespace;
452 $this->tagFilter
= $tagFilter;
453 $this->deletedOnly
= $deletedOnly;
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 getQueryInfo() {
468 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
470 $conds = array_merge( $userCond, $this->getNamespaceCond() );
471 // Paranoia: avoid brute force searches (bug 17342)
472 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
473 $conds[] = $this->mDb
->bitAnd('rev_deleted',Revision
::DELETED_USER
) . ' = 0';
474 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
475 $conds[] = $this->mDb
->bitAnd('rev_deleted',Revision
::SUPPRESSED_USER
) .
476 ' != ' . Revision
::SUPPRESSED_USER
;
478 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
483 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
484 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
485 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted'
488 'options' => array( 'USE INDEX' => array('revision' => $index) ),
489 'join_conds' => $join_cond
492 ChangeTags
::modifyDisplayQuery(
493 $queryInfo['tables'],
494 $queryInfo['fields'],
496 $queryInfo['join_conds'],
497 $queryInfo['options'],
501 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
505 function getUserCond() {
506 $condition = array();
507 $join_conds = array();
508 if( $this->target
== 'newbies' ) {
509 $tables = array( 'user_groups', 'page', 'revision' );
510 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
511 $condition[] = 'rev_user >' . (int)($max - $max / 100);
512 $condition[] = 'ug_group IS NULL';
513 $index = 'user_timestamp';
514 # FIXME: other groups may have 'bot' rights
515 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
517 $tables = array( 'page', 'revision' );
518 $condition['rev_user_text'] = $this->target
;
519 $index = 'usertext_timestamp';
521 if ( $this->deletedOnly
) {
522 $condition[] = "rev_deleted != '0'";
524 return array( $tables, $index, $condition, $join_conds );
527 function getNamespaceCond() {
528 if( $this->namespace !== '' ) {
529 return array( 'page_namespace' => (int)$this->namespace );
535 function getIndexField() {
536 return 'rev_timestamp';
539 function getStartBody() {
543 function getEndBody() {
548 * Generates each row in the contributions list.
550 * Contributions which are marked "top" are currently on top of the history.
551 * For these contributions, a [rollback] link is shown for users with roll-
552 * back privileges. The rollback link restores the most recent version that
553 * was not written by the target user.
555 * @todo This would probably look a lot nicer in a table.
557 function formatRow( $row ) {
558 global $wgUser, $wgLang, $wgContLang;
559 wfProfileIn( __METHOD__
);
561 $sk = $this->getSkin();
562 $rev = new Revision( $row );
565 $page = Title
::newFromRow( $row );
566 $page->resetArticleId( $row->rev_page
); // use process cache
569 htmlspecialchars( $page->getPrefixedText() ),
571 $page->isRedirect() ?
array( 'redirect' => 'no' ) : array()
573 # Mark current revisions
574 $difftext = $topmarktext = '';
575 if( $row->rev_id
== $row->page_latest
) {
576 $topmarktext .= '<span class="mw-uctop">' . $this->messages
['uctop'] . '</span>';
578 if( !$row->page_is_new
&& $page->quickUserCan( 'rollback' )
579 && $page->quickUserCan( 'edit' ) )
581 $topmarktext .= ' '.$sk->generateRollback( $rev );
584 # Is there a visible previous revision?
585 if( $rev->userCan( Revision
::DELETED_TEXT
) && $rev->getParentId() !== 0 ) {
586 $difftext = $sk->linkKnown(
588 $this->messages
['diff'],
592 'oldid' => $row->rev_id
596 $difftext = $this->messages
['diff'];
598 $histlink = $sk->linkKnown(
600 $this->messages
['hist'],
602 array( 'action' => 'history' )
605 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
606 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
607 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
608 $d = '<span class="history-deleted">' . $date . '</span>';
612 htmlspecialchars($date),
614 array( 'oldid' => intval( $row->rev_id
) )
618 if( $this->target
== 'newbies' ) {
619 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
620 $userlink .= ' ' . wfMsg( 'parentheses', $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) ) . ' ';
625 if( $rev->getParentId() === 0 ) {
626 $nflag = ChangesList
::flag( 'newpage' );
631 if( $rev->isMinor() ) {
632 $mflag = ChangesList
::flag( 'minor' );
637 // Don't show useless link to people who cannot hide revisions
638 $canHide = $wgUser->isAllowed( 'deleterevision' );
639 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
640 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
641 $del = $this->mSkin
->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
644 'type' => 'revision',
645 'target' => $page->getPrefixedDbkey(),
646 'ids' => $rev->getId()
648 $del = $this->mSkin
->revDeleteLink( $query,
649 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
656 $diffHistLinks = '(' . $difftext . $this->messages
['pipe-separator'] . $histlink . ')';
657 $ret = "{$del}{$d} {$diffHistLinks} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
659 # Denote if username is redacted for this edit
660 if( $rev->isDeleted( Revision
::DELETED_USER
) ) {
661 $ret .= " <strong>" . wfMsgHtml('rev-deleted-user-contribs') . "</strong>";
665 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'contributions' );
666 $classes = array_merge( $classes, $newClasses );
667 $ret .= " $tagSummary";
669 // Let extensions add data
670 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
672 $classes = implode( ' ', $classes );
673 $ret = "<li class=\"$classes\">$ret</li>\n";
674 wfProfileOut( __METHOD__
);
679 * Get the Database object in use
683 public function getDatabase() {
688 * Overwrite Pager function and return a helpful comment
690 function getSqlComment() {
691 if ( $this->namespace ||
$this->deletedOnly
) {
692 return 'contributions page filtered for namespace or RevisionDeleted edits'; // potentially slow, see CR r58153
694 return 'contributions page unfiltered';