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 if( !strlen( $target ) ) {
38 $wgOut->addHTML( $this->getForm() );
42 $this->opts['limit'] = $wgRequest->getInt( 'limit', $wgUser->getOption('rclimit') );
43 $this->opts['target'] = $target;
45 $nt = Title::makeTitleSafe( NS_USER, $target );
47 $wgOut->addHTML( $this->getForm() );
50 $id = User::idFromName( $nt->getText() );
52 if( $target != 'newbies' ) {
53 $target = $nt->getText();
54 $wgOut->setSubtitle( $this->contributionsSub( $nt, $id ) );
55 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsgExt( 'contributions-title', array( 'parsemag' ),$target ) ) );
57 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
58 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'sp-contributions-newbies-title' ) ) );
61 if( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
62 $this->opts['namespace'] = intval( $ns );
64 $this->opts['namespace'] = '';
67 $this->opts['tagfilter'] = (string) $wgRequest->getVal( 'tagfilter' );
69 // Allows reverts to have the bot flag in recent changes. It is just here to
70 // be passed in the form at the top of the page
71 if( $wgUser->isAllowed( 'markbotedits' ) && $wgRequest->getBool( 'bot' ) ) {
72 $this->opts['bot'] = '1';
75 $skip = $wgRequest->getText( 'offset' ) || $wgRequest->getText( 'dir' ) == 'prev';
76 # Offset overrides year/month selection
78 $this->opts['year'] = '';
79 $this->opts['month'] = '';
81 $this->opts['year'] = $wgRequest->getIntOrNull( 'year' );
82 $this->opts['month'] = $wgRequest->getIntOrNull( 'month' );
86 $this->setSyndicated();
87 $feedType = $wgRequest->getVal( 'feed' );
89 return $this->feed( $feedType );
92 if ( wfRunHooks( 'SpecialContributionsBeforeMainOutput', array( $id ) ) ) {
94 $wgOut->addHTML( $this->getForm() );
96 $pager = new ContribsPager( $target, $this->opts['namespace'], $this->opts['year'], $this->opts['month'] );
97 if( !$pager->getNumRows() ) {
98 $wgOut->addWikiMsg( 'nocontribs', $target );
100 # Show a message about slave lag, if applicable
101 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
102 $wgOut->showLagWarning( $lag );
105 '<p>' . $pager->getNavigationBar() . '</p>' .
107 '<p>' . $pager->getNavigationBar() . '</p>'
110 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
113 # Show the appropriate "footer" message - WHOIS tools, etc.
114 if( $target != 'newbies' ) {
115 $message = 'sp-contributions-footer';
116 if ( IP::isIPAddress( $target ) ) {
117 $message = 'sp-contributions-footer-anon';
119 $user = User::newFromName( $target );
120 if ( !$user || $user->isAnon() ) {
121 // No message for non-existing users
126 $text = wfMsgNoTrans( $message, $target );
127 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
129 "<div class='mw-contributions-footer'>\n$1\n</div>",
130 array( $message, $target ) );
136 protected function setSyndicated() {
138 $wgOut->setSyndicated( true );
139 $wgOut->setFeedAppendQuery( wfArrayToCGI( $this->opts ) );
143 * Generates the subheading with links
144 * @param Title $nt @see Title object for the target
145 * @param integer $id User ID for the target
146 * @return String: appropriately-escaped HTML to be output literally
147 * @todo Fixme: almost the same as getSubTitle in SpecialDeletedContributions.php. Could be combined.
149 protected function contributionsSub( $nt, $id ) {
150 global $wgSysopUserBans, $wgLang, $wgUser, $wgOut;
152 $sk = $wgUser->getSkin();
154 if ( $id === null ) {
155 $user = htmlspecialchars( $nt->getText() );
157 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
159 $userObj = User::newFromName( $nt->getText(), /* check for username validity not needed */ false );
160 $talk = $nt->getTalkPage();
163 $tools[] = $sk->link( $talk, wfMsgHtml( 'sp-contributions-talk' ) );
164 if( ( $id !== null && $wgSysopUserBans ) || ( $id === null && IP::isIPAddress( $nt->getText() ) ) ) {
165 if( $wgUser->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
166 if ( $userObj->isBlocked() ) {
167 $tools[] = $sk->linkKnown( # Change block link
168 SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ),
169 wfMsgHtml( 'change-blocklink' )
171 $tools[] = $sk->linkKnown( # Unblock link
172 SpecialPage::getTitleFor( 'BlockList' ),
173 wfMsgHtml( 'unblocklink' ),
176 'action' => 'unblock',
177 'ip' => $nt->getDBkey()
181 else { # User is not blocked
182 $tools[] = $sk->linkKnown( # Block link
183 SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ),
184 wfMsgHtml( 'blocklink' )
189 $tools[] = $sk->linkKnown(
190 SpecialPage::getTitleFor( 'Log' ),
191 wfMsgHtml( 'sp-contributions-blocklog' ),
195 'page' => $nt->getPrefixedText()
200 $tools[] = $sk->linkKnown(
201 SpecialPage::getTitleFor( 'Log' ),
202 wfMsgHtml( 'sp-contributions-logs' ),
204 array( 'user' => $nt->getText() )
207 # Add link to deleted user contributions for priviledged users
208 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
209 $tools[] = $sk->linkKnown(
210 SpecialPage::getTitleFor( 'DeletedContributions', $nt->getDBkey() ),
211 wfMsgHtml( 'sp-contributions-deleted' )
215 # Add a link to change user rights for privileged users
216 $userrightsPage = new UserrightsPage();
217 if( $id !== null && $userrightsPage->userCanChangeRights( User::newFromId( $id ) ) ) {
218 $tools[] = $sk->linkKnown(
219 SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
220 wfMsgHtml( 'sp-contributions-userrights' )
224 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
226 $links = $wgLang->pipeList( $tools );
228 // Show a note if the user is blocked and display the last block log entry.
229 if ( $userObj->isBlocked() ) {
230 LogEventsList::showLogExtract(
233 $nt->getPrefixedText(),
237 'showIfEmpty' => false,
239 'sp-contributions-blocked-notice',
240 $nt->getText() # Support GENDER in 'sp-contributions-blocked-notice'
242 'offset' => '' # don't use $wgRequest parameter offset
248 // Old message 'contribsub' had one parameter, but that doesn't work for
249 // languages that want to put the "for" bit right after $user but before
250 // $links. If 'contribsub' is around, use it for reverse compatibility,
251 // otherwise use 'contribsub2'.
252 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
253 return wfMsgHtml( 'contribsub2', $user, $links );
255 return wfMsgHtml( 'contribsub', "$user ($links)" );
260 * Generates the namespace selector form with hidden attributes.
261 * @param $this->opts Array: the options to be included.
263 protected function getForm() {
266 $this->opts['title'] = $this->getTitle()->getPrefixedText();
267 if( !isset( $this->opts['target'] ) ) {
268 $this->opts['target'] = '';
270 $this->opts['target'] = str_replace( '_' , ' ' , $this->opts['target'] );
273 if( !isset( $this->opts['namespace'] ) ) {
274 $this->opts['namespace'] = '';
277 if( !isset( $this->opts['contribs'] ) ) {
278 $this->opts['contribs'] = 'user';
281 if( !isset( $this->opts['year'] ) ) {
282 $this->opts['year'] = '';
285 if( !isset( $this->opts['month'] ) ) {
286 $this->opts['month'] = '';
289 if( $this->opts['contribs'] == 'newbie' ) {
290 $this->opts['target'] = '';
293 if( !isset( $this->opts['tagfilter'] ) ) {
294 $this->opts['tagfilter'] = '';
297 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
298 # Add hidden params for tracking
299 foreach ( $this->opts as $name => $value ) {
300 if( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
303 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
306 $tagFilter = ChangeTags::buildTagFilterSelector( $this->opts['tagfilter'] );
309 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
310 Xml::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parsemag' ) ),
311 'contribs', 'newbie' , 'newbie', $this->opts['contribs'] == 'newbie' ? true : false ) . '<br />' .
312 Xml::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parsemag' ) ),
313 'contribs' , 'user', 'user', $this->opts['contribs'] == 'user' ? true : false ) . ' ' .
314 Html::input( 'target', $this->opts['target'], 'text', array(
317 ) + ( $this->opts['target'] ? array() : array( 'autofocus' ) ) ) . ' '.
318 '<span style="white-space: nowrap">' .
319 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
320 Xml::namespaceSelector( $this->opts['namespace'], '' ) .
322 ( $tagFilter ? Xml::tags( 'p', null, implode( ' ', $tagFilter ) ) : '' ) .
323 Xml::openElement( 'p' ) .
324 '<span style="white-space: nowrap">' .
325 Xml::dateMenu( $this->opts['year'], $this->opts['month'] ) .
327 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
328 Xml::closeElement( 'p' );
330 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
331 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
332 $f .= "<p id='mw-sp-contributions-explain'>{$explain}</p>";
334 $f .= '</fieldset>' .
335 Xml::closeElement( 'form' );
340 * Output a subscription feed listing recent edits to this page.
341 * @param string $type
343 protected function feed( $type ) {
344 global $wgRequest, $wgFeed, $wgFeedClasses, $wgFeedLimit;
348 $wgOut->addWikiMsg( 'feed-unavailable' );
352 if( !isset( $wgFeedClasses[$type] ) ) {
354 $wgOut->addWikiMsg( 'feed-invalid' );
358 $feed = new $wgFeedClasses[$type](
360 wfMsgExt( 'tagline', 'parsemag' ),
361 $this->getTitle()->getFullUrl() . "/" . urlencode($this->opts['target'])
364 // Already valid title
365 $nt = Title::makeTitleSafe( NS_USER, $this->opts['target'] );
366 $target = $this->opts['target'] == 'newbies' ? 'newbies' : $nt->getText();
368 $pager = new ContribsPager( $target, $this->opts['namespace'],
369 $this->opts['year'], $this->opts['month'], $this->opts['tagfilter'] );
371 $pager->mLimit = min( $this->opts['limit'], $wgFeedLimit );
374 if( $pager->getNumRows() > 0 ) {
375 while( $row = $pager->mResult->fetchObject() ) {
376 $feed->outItem( $this->feedItem( $row ) );
382 protected function feedTitle() {
383 global $wgContLanguageCode, $wgSitename;
384 $page = SpecialPage::getPage( 'Contributions' );
385 $desc = $page->getDescription();
386 return "$wgSitename - $desc [$wgContLanguageCode]";
389 protected function feedItem( $row ) {
390 $title = Title::MakeTitle( intval( $row->page_namespace ), $row->page_title );
392 $date = $row->rev_timestamp;
393 $comments = $title->getTalkPage()->getFullURL();
394 $revision = Revision::newFromTitle( $title, $row->rev_id );
397 $title->getPrefixedText(),
398 $this->feedItemDesc( $revision ),
399 $title->getFullURL(),
401 $this->feedItemAuthor( $revision ),
409 protected function feedItemAuthor( $revision ) {
410 return $revision->getUserText();
413 protected function feedItemDesc( $revision ) {
415 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
416 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
417 "</p>\n<hr />\n<div>" .
418 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
425 * Pager for Special:Contributions
426 * @ingroup SpecialPage Pager
428 class ContribsPager extends ReverseChronologicalPager {
429 public $mDefaultDirection = true;
430 var $messages, $target;
431 var $namespace = '', $mDb;
432 var $preventClickjacking = false;
434 function __construct( $target, $namespace = false, $year = false, $month = false, $tagFilter = false ) {
435 parent::__construct();
437 $msgs = array( 'uctop', 'diff', 'newarticle', 'rollbacklink', 'diff', 'hist', 'rev-delundel', 'pipe-separator' );
439 foreach( $msgs as $msg ) {
440 $this->messages[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
443 $this->target = $target;
444 $this->namespace = $namespace;
445 $this->tagFilter = $tagFilter;
447 $this->getDateCond( $year, $month );
449 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
452 function getDefaultQuery() {
453 $query = parent::getDefaultQuery();
454 $query['target'] = $this->target;
458 function getQueryInfo() {
460 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
462 $conds = array_merge( $userCond, $this->getNamespaceCond() );
463 // Paranoia: avoid brute force searches (bug 17342)
464 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
465 $conds[] = $this->mDb->bitAnd('rev_deleted',Revision::DELETED_USER) . ' = 0';
466 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
467 $conds[] = $this->mDb->bitAnd('rev_deleted',Revision::SUPPRESSED_USER) .
468 ' != ' . Revision::SUPPRESSED_USER;
470 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
475 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
476 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
477 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted'
480 'options' => array( 'USE INDEX' => array('revision' => $index) ),
481 'join_conds' => $join_cond
484 ChangeTags::modifyDisplayQuery(
485 $queryInfo['tables'],
486 $queryInfo['fields'],
488 $queryInfo['join_conds'],
489 $queryInfo['options'],
493 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
497 function getUserCond() {
498 $condition = array();
499 $join_conds = array();
500 if( $this->target == 'newbies' ) {
501 $tables = array( 'user_groups', 'page', 'revision' );
502 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
503 $condition[] = 'rev_user >' . (int)($max - $max / 100);
504 $condition[] = 'ug_group IS NULL';
505 $index = 'user_timestamp';
506 # FIXME: other groups may have 'bot' rights
507 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
509 $tables = array( 'page', 'revision' );
510 $condition['rev_user_text'] = $this->target;
511 $index = 'usertext_timestamp';
513 return array( $tables, $index, $condition, $join_conds );
516 function getNamespaceCond() {
517 if( $this->namespace !== '' ) {
518 return array( 'page_namespace' => (int)$this->namespace );
524 function getIndexField() {
525 return 'rev_timestamp';
528 function getStartBody() {
532 function getEndBody() {
537 * Generates each row in the contributions list.
539 * Contributions which are marked "top" are currently on top of the history.
540 * For these contributions, a [rollback] link is shown for users with roll-
541 * back privileges. The rollback link restores the most recent version that
542 * was not written by the target user.
544 * @todo This would probably look a lot nicer in a table.
546 function formatRow( $row ) {
547 global $wgUser, $wgLang, $wgContLang;
548 wfProfileIn( __METHOD__ );
550 $sk = $this->getSkin();
551 $rev = new Revision( $row );
554 $page = Title::newFromRow( $row );
555 $page->resetArticleId( $row->rev_page ); // use process cache
558 htmlspecialchars( $page->getPrefixedText() ),
560 $page->isRedirect() ? array( 'redirect' => 'no' ) : array()
562 # Mark current revisions
563 $difftext = $topmarktext = '';
564 if( $row->rev_id == $row->page_latest ) {
565 $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
567 if( !$row->page_is_new && $page->quickUserCan( 'rollback' )
568 && $page->quickUserCan( 'edit' ) )
570 $this->preventClickjacking();
571 $topmarktext .= ' '.$sk->generateRollback( $rev );
574 # Is there a visible previous revision?
575 if( $rev->userCan( Revision::DELETED_TEXT ) && $rev->getParentId() !== 0 ) {
576 $difftext = $sk->linkKnown(
578 $this->messages['diff'],
582 'oldid' => $row->rev_id
586 $difftext = $this->messages['diff'];
588 $histlink = $sk->linkKnown(
590 $this->messages['hist'],
592 array( 'action' => 'history' )
595 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
596 $date = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
597 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
598 $d = '<span class="history-deleted">' . $date . '</span>';
602 htmlspecialchars($date),
604 array( 'oldid' => intval( $row->rev_id ) )
608 if( $this->target == 'newbies' ) {
609 $userlink = ' . . ' . $sk->userLink( $row->rev_user, $row->rev_user_text );
610 $userlink .= ' ' . wfMsg( 'parentheses', $sk->userTalkLink( $row->rev_user, $row->rev_user_text ) ) . ' ';
615 if( $rev->getParentId() === 0 ) {
616 $nflag = ChangesList::flag( 'newpage' );
621 if( $rev->isMinor() ) {
622 $mflag = ChangesList::flag( 'minor' );
627 // Don't show useless link to people who cannot hide revisions
628 $canHide = $wgUser->isAllowed( 'deleterevision' );
629 if( $canHide || ($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
630 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
631 $del = $this->mSkin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
634 'type' => 'revision',
635 'target' => $page->getPrefixedDbkey(),
636 'ids' => $rev->getId()
638 $del = $this->mSkin->revDeleteLink( $query,
639 $rev->isDeleted( Revision::DELETED_RESTRICTED ), $canHide );
646 $diffHistLinks = '(' . $difftext . $this->messages['pipe-separator'] . $histlink . ')';
647 $ret = "{$del}{$d} {$diffHistLinks} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
649 # Denote if username is redacted for this edit
650 if( $rev->isDeleted( Revision::DELETED_USER ) ) {
651 $ret .= " <strong>" . wfMsgHtml('rev-deleted-user-contribs') . "</strong>";
655 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $row->ts_tags, 'contributions' );
656 $classes = array_merge( $classes, $newClasses );
657 $ret .= " $tagSummary";
659 // Let extensions add data
660 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
662 $classes = implode( ' ', $classes );
663 $ret = "<li class=\"$classes\">$ret</li>\n";
664 wfProfileOut( __METHOD__ );
669 * Get the Database object in use
673 public function getDatabase() {
677 protected function preventClickjacking() {
678 $this->preventClickjacking = true;
681 public function getPreventClickjacking() {
682 return $this->preventClickjacking;