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 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $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>'
112 # Show the appropriate "footer" message - WHOIS tools, etc.
113 if( $target != 'newbies' ) {
114 $message = 'sp-contributions-footer';
115 if ( IP
::isIPAddress( $target ) ) {
116 $message = 'sp-contributions-footer-anon';
118 $user = User
::newFromName( $target );
119 if ( !$user ||
$user->isAnon() ) {
120 // No message for non-existing users
125 $text = wfMsgNoTrans( $message, $target );
126 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
128 "<div class='mw-contributions-footer'>\n$1\n</div>",
129 array( $message, $target ) );
134 protected function setSyndicated() {
136 $queryParams = array(
137 'namespace' => $this->opts
['namespace'],
138 'target' => $this->opts
['target']
140 $wgOut->setSyndicated( true );
141 $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
145 * Generates the subheading with links
146 * @param Title $nt Title object for the target
147 * @param integer $id User ID for the target
148 * @return String: appropriately-escaped HTML to be output literally
150 protected function contributionsSub( $nt, $id ) {
151 global $wgSysopUserBans, $wgLang, $wgUser, $wgOut;
153 $sk = $wgUser->getSkin();
156 $user = htmlspecialchars( $nt->getText() );
158 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
160 $userObj = User
::newFromName( $nt->getText(), /* check for username validity not needed */ false );
161 $talk = $nt->getTalkPage();
164 $tools[] = $sk->link( $talk, wfMsgHtml( 'sp-contributions-talk' ) );
165 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && IP
::isIPAddress( $nt->getText() ) ) ) {
166 if( $wgUser->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
167 if ( $userObj->isBlocked() ) {
168 $tools[] = $sk->linkKnown( # Change block link
169 SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ),
170 wfMsgHtml( 'change-blocklink' )
172 $tools[] = $sk->linkKnown( # Unblock link
173 SpecialPage
::getTitleFor( 'BlockList' ),
174 wfMsgHtml( 'unblocklink' ),
177 'action' => 'unblock',
178 'ip' => $nt->getDBkey()
182 else { # User is not blocked
183 $tools[] = $sk->linkKnown( # Block link
184 SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ),
185 wfMsgHtml( 'blocklink' )
190 $tools[] = $sk->linkKnown(
191 SpecialPage
::getTitleFor( 'Log' ),
192 wfMsgHtml( 'sp-contributions-blocklog' ),
196 'page' => $nt->getPrefixedText()
201 $tools[] = $sk->linkKnown(
202 SpecialPage
::getTitleFor( 'Log' ),
203 wfMsgHtml( 'sp-contributions-logs' ),
205 array( 'user' => $nt->getText() )
208 # Add link to deleted user contributions for priviledged users
209 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
210 $tools[] = $sk->linkKnown(
211 SpecialPage
::getTitleFor( 'DeletedContributions', $nt->getDBkey() ),
212 wfMsgHtml( 'sp-contributions-deleted' )
216 # Add a link to change user rights for privileged users
217 $userrightsPage = new UserrightsPage();
218 if( 0 !== $id && $userrightsPage->userCanChangeRights( User
::newFromId( $id ) ) ) {
219 $tools[] = $sk->linkKnown(
220 SpecialPage
::getTitleFor( 'Userrights', $nt->getDBkey() ),
221 wfMsgHtml( 'sp-contributions-userrights' )
225 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
227 $links = $wgLang->pipeList( $tools );
229 // Show a note if the user is blocked and display the last block log entry.
230 if ( $userObj->isBlocked() ) {
231 LogEventsList
::showLogExtract(
234 $nt->getPrefixedText(),
238 'showIfEmpty' => false,
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', wfMsg( 'contribsub' ) ) ) {
254 return wfMsgHtml( 'contribsub2', $user, $links );
256 return wfMsgHtml( 'contribsub', "$user ($links)" );
261 * Generates the namespace selector form with hidden attributes.
262 * @param $this->opts Array: the options to be included.
264 protected function getForm() {
267 $this->opts
['title'] = $this->getTitle()->getPrefixedText();
268 if( !isset( $this->opts
['target'] ) ) {
269 $this->opts
['target'] = '';
271 $this->opts
['target'] = str_replace( '_' , ' ' , $this->opts
['target'] );
274 if( !isset( $this->opts
['namespace'] ) ) {
275 $this->opts
['namespace'] = '';
278 if( !isset( $this->opts
['contribs'] ) ) {
279 $this->opts
['contribs'] = 'user';
282 if( !isset( $this->opts
['year'] ) ) {
283 $this->opts
['year'] = '';
286 if( !isset( $this->opts
['month'] ) ) {
287 $this->opts
['month'] = '';
290 if( $this->opts
['contribs'] == 'newbie' ) {
291 $this->opts
['target'] = '';
294 if( !isset( $this->opts
['tagfilter'] ) ) {
295 $this->opts
['tagfilter'] = '';
298 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
299 # Add hidden params for tracking
300 foreach ( $this->opts
as $name => $value ) {
301 if( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
304 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
307 $tagFilter = ChangeTags
::buildTagFilterSelector( $this->opts
['tagfilter'] );
310 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
311 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parsemag' ) ),
312 'contribs', 'newbie' , 'newbie', $this->opts
['contribs'] == 'newbie' ?
true : false ) . '<br />' .
313 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parsemag' ) ),
314 'contribs' , 'user', 'user', $this->opts
['contribs'] == 'user' ?
true : false ) . ' ' .
315 Html
::input( 'target', $this->opts
['target'], 'text', array(
318 ) +
( $this->opts
['target'] ?
array() : array( 'autofocus' ) ) ) . ' '.
319 '<span style="white-space: nowrap">' .
320 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
321 Xml
::namespaceSelector( $this->opts
['namespace'], '' ) .
323 ( $tagFilter ? Xml
::tags( 'p', null, implode( ' ', $tagFilter ) ) : '' ) .
324 Xml
::openElement( 'p' ) .
325 '<span style="white-space: nowrap">' .
326 Xml
::dateMenu( $this->opts
['year'], $this->opts
['month'] ) .
328 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
329 Xml
::closeElement( 'p' );
331 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
332 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
333 $f .= "<p>{$explain}</p>";
335 $f .= '</fieldset>' .
336 Xml
::closeElement( 'form' );
341 * Output a subscription feed listing recent edits to this page.
342 * @param string $type
344 protected function feed( $type ) {
345 global $wgRequest, $wgFeed, $wgFeedClasses, $wgFeedLimit;
349 $wgOut->addWikiMsg( 'feed-unavailable' );
353 if( !isset( $wgFeedClasses[$type] ) ) {
355 $wgOut->addWikiMsg( 'feed-invalid' );
359 $feed = new $wgFeedClasses[$type](
361 wfMsgExt( 'tagline', 'parsemag' ),
362 $this->getTitle()->getFullUrl() . "/" . urlencode($this->opts
['target'])
365 // Already valid title
366 $nt = Title
::makeTitleSafe( NS_USER
, $this->opts
['target'] );
367 $target = $this->opts
['target'] == 'newbies' ?
'newbies' : $nt->getText();
369 $pager = new ContribsPager( $target, $this->opts
['namespace'],
370 $this->opts
['year'], $this->opts
['month'], $this->opts
['tagfilter'] );
372 $pager->mLimit
= min( $this->opts
['limit'], $wgFeedLimit );
375 if( $pager->getNumRows() > 0 ) {
376 while( $row = $pager->mResult
->fetchObject() ) {
377 $feed->outItem( $this->feedItem( $row ) );
383 protected function feedTitle() {
384 global $wgContLanguageCode, $wgSitename;
385 $page = SpecialPage
::getPage( 'Contributions' );
386 $desc = $page->getDescription();
387 return "$wgSitename - $desc [$wgContLanguageCode]";
390 protected function feedItem( $row ) {
391 $title = Title
::MakeTitle( intval( $row->page_namespace
), $row->page_title
);
393 $date = $row->rev_timestamp
;
394 $comments = $title->getTalkPage()->getFullURL();
395 $revision = Revision
::newFromTitle( $title, $row->rev_id
);
398 $title->getPrefixedText(),
399 $this->feedItemDesc( $revision ),
400 $title->getFullURL(),
402 $this->feedItemAuthor( $revision ),
410 protected function feedItemAuthor( $revision ) {
411 return $revision->getUserText();
414 protected function feedItemDesc( $revision ) {
416 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
417 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
418 "</p>\n<hr />\n<div>" .
419 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
426 * Pager for Special:Contributions
427 * @ingroup SpecialPage Pager
429 class ContribsPager
extends ReverseChronologicalPager
{
430 public $mDefaultDirection = true;
431 var $messages, $target;
432 var $namespace = '', $mDb;
434 function __construct( $target, $namespace = false, $year = false, $month = false, $tagFilter = false ) {
435 parent
::__construct();
436 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist rev-delundel pipe-separator' ) as $msg ) {
437 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escape') );
439 $this->target
= $target;
440 $this->namespace = $namespace;
441 $this->tagFilter
= $tagFilter;
443 $this->getDateCond( $year, $month );
445 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
448 function getDefaultQuery() {
449 $query = parent
::getDefaultQuery();
450 $query['target'] = $this->target
;
454 function getQueryInfo() {
456 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
458 $conds = array_merge( $userCond, $this->getNamespaceCond() );
459 // Paranoia: avoid brute force searches (bug 17342)
460 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
461 $conds[] = $this->mDb
->bitAnd('rev_deleted',Revision
::DELETED_USER
) . ' = 0';
462 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
463 $conds[] = $this->mDb
->bitAnd('rev_deleted',Revision
::SUPPRESSED_USER
) .
464 ' != ' . Revision
::SUPPRESSED_USER
;
466 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
471 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
472 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
473 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted'
476 'options' => array( 'USE INDEX' => array('revision' => $index) ),
477 'join_conds' => $join_cond
480 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'],
481 $queryInfo['fields'],
483 $queryInfo['join_conds'],
484 $queryInfo['options'],
487 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
491 function getUserCond() {
492 $condition = array();
493 $join_conds = array();
494 if( $this->target
== 'newbies' ) {
495 $tables = array( 'user_groups', 'page', 'revision' );
496 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
497 $condition[] = 'rev_user >' . (int)($max - $max / 100);
498 $condition[] = 'ug_group IS NULL';
499 $index = 'user_timestamp';
500 # FIXME: other groups may have 'bot' rights
501 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
503 $tables = array( 'page', 'revision' );
504 $condition['rev_user_text'] = $this->target
;
505 $index = 'usertext_timestamp';
507 return array( $tables, $index, $condition, $join_conds );
510 function getNamespaceCond() {
511 if( $this->namespace !== '' ) {
512 return array( 'page_namespace' => (int)$this->namespace );
518 function getIndexField() {
519 return 'rev_timestamp';
522 function getStartBody() {
526 function getEndBody() {
531 * Generates each row in the contributions list.
533 * Contributions which are marked "top" are currently on top of the history.
534 * For these contributions, a [rollback] link is shown for users with roll-
535 * back privileges. The rollback link restores the most recent version that
536 * was not written by the target user.
538 * @todo This would probably look a lot nicer in a table.
540 function formatRow( $row ) {
541 global $wgUser, $wgLang, $wgContLang;
542 wfProfileIn( __METHOD__
);
544 $sk = $this->getSkin();
545 $rev = new Revision( $row );
548 $page = Title
::newFromRow( $row );
549 $page->resetArticleId( $row->rev_page
); // use process cache
552 htmlspecialchars( $page->getPrefixedText() ),
554 $page->isRedirect() ?
array( 'redirect' => 'no' ) : array()
556 # Mark current revisions
557 $difftext = $topmarktext = '';
558 if( $row->rev_id
== $row->page_latest
) {
559 $topmarktext .= '<span class="mw-uctop">' . $this->messages
['uctop'] . '</span>';
560 if( !$row->page_is_new
) {
561 $difftext .= '(' . $sk->linkKnown(
563 $this->messages
['diff'],
568 if( $page->quickUserCan( 'rollback') && $page->quickUserCan( 'edit' ) ) {
569 $topmarktext .= ' '.$sk->generateRollback( $rev );
572 $difftext .= $this->messages
['newarticle'];
575 # Is there a visible previous revision?
576 if( !$rev->isDeleted(Revision
::DELETED_TEXT
) ) {
577 $difftext = $this->messages
['pipe-separator'] . $sk->linkKnown(
579 $this->messages
['diff'],
583 'oldid' => $row->rev_id
587 $difftext = $this->messages
['pipe-separator'] . $this->messages
['diff'] . ')';
589 $histlink = '('.$sk->linkKnown(
591 $this->messages
['hist'],
593 array( 'action' => 'history' )
596 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
597 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
598 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
599 $d = '<span class="history-deleted">' . $date . '</span>';
603 htmlspecialchars($date),
605 array( 'oldid' => intval( $row->rev_id
) )
609 if( $this->target
== 'newbies' ) {
610 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
611 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
616 if( $rev->getParentId() === 0 ) {
617 $nflag = ChangesList
::flag( 'newpage' );
622 if( $rev->isMinor() ) {
623 $mflag = ChangesList
::flag( 'minor' );
628 // Don't show useless link to people who cannot hide revisions
629 if( $wgUser->isAllowed('deleterevision') ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
630 // If revision was hidden from sysops
631 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
632 $del = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
633 '(' . $this->messages
['rev-delundel'] . ')' ) . ' ';
634 // Otherwise, show the link...
637 'type' => 'revision',
638 'target' => $page->getPrefixedDbkey(),
639 'ids' => $rev->getId() );
640 $del = $this->mSkin
->revDeleteLink( $query,
641 $rev->isDeleted( Revision
::DELETED_RESTRICTED
) ) . ' ';
647 $ret = "{$del}{$d} {$histlink}{$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
649 # Denote if username is redacted for this edit
650 if( $rev->getVisibility() & Revision
::DELETED_USER
) {
651 $ret .= " <strong>" . wfMsgHtml('rev-deleted-user') . "</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() {