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();
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( 'escape' ) );
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' ) && $page->quickUserCan( 'edit' ) ) {
568 $topmarktext .= ' '.$sk->generateRollback( $rev );
571 # Is there a visible previous revision?
572 if( !$rev->isDeleted( Revision
::DELETED_TEXT
) && $rev->getParentId() !== 0 ) {
573 $difftext = $this->messages
['pipe-separator'] . $sk->linkKnown(
575 $this->messages
['diff'],
579 'oldid' => $row->rev_id
583 $difftext = $this->messages
['pipe-separator'] . $this->messages
['diff'] . ')';
585 $histlink = '('.$sk->linkKnown(
587 $this->messages
['hist'],
589 array( 'action' => 'history' )
592 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
593 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
594 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
595 $d = '<span class="history-deleted">' . $date . '</span>';
599 htmlspecialchars($date),
601 array( 'oldid' => intval( $row->rev_id
) )
605 if( $this->target
== 'newbies' ) {
606 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
607 $userlink .= ' ' . wfMsg( 'parentheses', $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) ) . ' ';
612 if( $rev->getParentId() === 0 ) {
613 $nflag = ChangesList
::flag( 'newpage' );
618 if( $rev->isMinor() ) {
619 $mflag = ChangesList
::flag( 'minor' );
624 // Don't show useless link to people who cannot hide revisions
625 $canHide = $wgUser->isAllowed( 'deleterevision' );
626 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
627 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
628 $del = $this->mSkin
->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
631 'type' => 'revision',
632 'target' => $page->getPrefixedDbkey(),
633 'ids' => $rev->getId()
635 $del = $this->mSkin
->revDeleteLink( $query,
636 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
643 $ret = "{$del}{$d} {$histlink}{$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
645 # Denote if username is redacted for this edit
646 if( $rev->getVisibility() & Revision
::DELETED_USER
) {
647 $ret .= " <strong>" . wfMsgHtml('rev-deleted-user') . "</strong>";
651 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'contributions' );
652 $classes = array_merge( $classes, $newClasses );
653 $ret .= " $tagSummary";
655 // Let extensions add data
656 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
658 $classes = implode( ' ', $classes );
659 $ret = "<li class=\"$classes\">$ret</li>\n";
660 wfProfileOut( __METHOD__
);
665 * Get the Database object in use
669 public function getDatabase() {