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>", $message );
133 protected function setSyndicated() {
135 $queryParams = array(
136 'namespace' => $this->opts
['namespace'],
137 'target' => $this->opts
['target']
139 $wgOut->setSyndicated( true );
140 $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
144 * Generates the subheading with links
145 * @param Title $nt Title object for the target
146 * @param integer $id User ID for the target
147 * @return String: appropriately-escaped HTML to be output literally
149 protected function contributionsSub( $nt, $id ) {
150 global $wgSysopUserBans, $wgLang, $wgUser;
152 $sk = $wgUser->getSkin();
155 $user = htmlspecialchars( $nt->getText() );
157 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
159 $talk = $nt->getTalkPage();
162 $tools[] = $sk->link( $talk, wfMsgHtml( 'sp-contributions-talk' ) );
163 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && IP
::isIPAddress( $nt->getText() ) ) ) {
164 if( $wgUser->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
165 if ( User
::newFromId( $id )->isBlocked() ) {
166 $tools[] = $sk->linkKnown( # Change block link
167 SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ),
168 wfMsgHtml( 'change-blocklink' )
170 $tools[] = $sk->linkKnown( # Unblock link
171 SpecialPage
::getTitleFor( 'BlockList' ),
172 wfMsgHtml( 'unblocklink' ),
175 'action' => 'unblock',
176 'ip' => $nt->getDBkey()
180 else { # User is not blocked
181 $tools[] = $sk->linkKnown( # Block link
182 SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ),
183 wfMsgHtml( 'blocklink' )
188 $tools[] = $sk->linkKnown(
189 SpecialPage
::getTitleFor( 'Log' ),
190 wfMsgHtml( 'sp-contributions-blocklog' ),
194 'page' => $nt->getPrefixedText()
199 $tools[] = $sk->linkKnown(
200 SpecialPage
::getTitleFor( 'Log' ),
201 wfMsgHtml( 'sp-contributions-logs' ),
203 array( 'user' => $nt->getText() )
206 # Add link to deleted user contributions for priviledged users
207 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
208 $tools[] = $sk->linkKnown(
209 SpecialPage
::getTitleFor( 'DeletedContributions', $nt->getDBkey() ),
210 wfMsgHtml( 'sp-contributions-deleted' )
214 # Add a link to change user rights for privileged users
215 $userrightsPage = new UserrightsPage();
216 if( 0 !== $id && $userrightsPage->userCanChangeRights( User
::newFromId( $id ) ) ) {
217 $tools[] = $sk->linkKnown(
218 SpecialPage
::getTitleFor( 'Userrights', $nt->getDBkey() ),
219 wfMsgHtml( 'sp-contributions-userrights' )
223 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
225 $links = $wgLang->pipeList( $tools );
226 $this->showBlock( $nt );
229 // Old message 'contribsub' had one parameter, but that doesn't work for
230 // languages that want to put the "for" bit right after $user but before
231 // $links. If 'contribsub' is around, use it for reverse compatibility,
232 // otherwise use 'contribsub2'.
233 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
234 return wfMsgHtml( 'contribsub2', $user, $links );
236 return wfMsgHtml( 'contribsub', "$user ($links)" );
241 * Show a note if the user is blocked and display the last block log entry.
242 * @param Title $nt Title object for the target
244 protected function showBlock( $nt ) {
245 global $wgUser, $wgOut;
246 $loglist = new LogEventsList( $wgUser->getSkin(), $wgOut );
247 $pager = new LogPager( $loglist, 'block', false, $nt->getPrefixedText() );
248 // Check if there is something in the block log.
249 // If this is not the case, either the user is not blocked,
250 // or the account has been hidden via hideuser.
251 if( $pager->getNumRows() > 0 ) {
252 $pager->mLimit
= 1; # Show only latest log entry.
253 $wgOut->addHTML( '<div class="mw-warning-with-logexcerpt">' );
254 $wgOut->addWikiMsg( 'sp-contributions-blocked-notice' );
256 $loglist->beginLogEventsList() .
258 $loglist->endLogEventsList()
260 if( $pager->getNumRows() > $pager->mLimit
) {
261 $wgOut->addHTML( $wgUser->getSkin()->link(
262 SpecialPage
::getTitleFor( 'Log', 'block' ),
263 wfMsgHtml( 'log-fulllog' ),
265 array( 'page' => $nt->getPrefixedText() )
268 $wgOut->addHTML( '</div>' );
272 * Generates the namespace selector form with hidden attributes.
273 * @param $this->opts Array: the options to be included.
275 protected function getForm() {
278 $this->opts
['title'] = $this->getTitle()->getPrefixedText();
279 if( !isset( $this->opts
['target'] ) ) {
280 $this->opts
['target'] = '';
282 $this->opts
['target'] = str_replace( '_' , ' ' , $this->opts
['target'] );
285 if( !isset( $this->opts
['namespace'] ) ) {
286 $this->opts
['namespace'] = '';
289 if( !isset( $this->opts
['contribs'] ) ) {
290 $this->opts
['contribs'] = 'user';
293 if( !isset( $this->opts
['year'] ) ) {
294 $this->opts
['year'] = '';
297 if( !isset( $this->opts
['month'] ) ) {
298 $this->opts
['month'] = '';
301 if( $this->opts
['contribs'] == 'newbie' ) {
302 $this->opts
['target'] = '';
305 if( !isset( $this->opts
['tagfilter'] ) ) {
306 $this->opts
['tagfilter'] = '';
309 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
310 # Add hidden params for tracking
311 foreach ( $this->opts
as $name => $value ) {
312 if( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
315 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
318 $tagFilter = ChangeTags
::buildTagFilterSelector( $this->opts
['tagfilter'] );
321 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
322 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parsemag' ) ),
323 'contribs', 'newbie' , 'newbie', $this->opts
['contribs'] == 'newbie' ?
true : false ) . '<br />' .
324 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parsemag' ) ),
325 'contribs' , 'user', 'user', $this->opts
['contribs'] == 'user' ?
true : false ) . ' ' .
326 Html
::input( 'target', $this->opts
['target'], 'text', array(
329 ) +
( $this->opts
['target'] ?
array() : array( 'autofocus' ) ) ) . ' '.
330 '<span style="white-space: nowrap">' .
331 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
332 Xml
::namespaceSelector( $this->opts
['namespace'], '' ) .
334 ( $tagFilter ? Xml
::tags( 'p', null, implode( ' ', $tagFilter ) ) : '' ) .
335 Xml
::openElement( 'p' ) .
336 '<span style="white-space: nowrap">' .
337 Xml
::dateMenu( $this->opts
['year'], $this->opts
['month'] ) .
339 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
340 Xml
::closeElement( 'p' );
342 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
343 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
344 $f .= "<p>{$explain}</p>";
346 $f .= '</fieldset>' .
347 Xml
::closeElement( 'form' );
352 * Output a subscription feed listing recent edits to this page.
353 * @param string $type
355 protected function feed( $type ) {
356 global $wgRequest, $wgFeed, $wgFeedClasses, $wgFeedLimit;
360 $wgOut->addWikiMsg( 'feed-unavailable' );
364 if( !isset( $wgFeedClasses[$type] ) ) {
366 $wgOut->addWikiMsg( 'feed-invalid' );
370 $feed = new $wgFeedClasses[$type](
372 wfMsgExt( 'tagline', 'parsemag' ),
373 $this->getTitle()->getFullUrl() . "/" . urlencode($this->opts
['target'])
376 // Already valid title
377 $nt = Title
::makeTitleSafe( NS_USER
, $this->opts
['target'] );
378 $target = $this->opts
['target'] == 'newbies' ?
'newbies' : $nt->getText();
380 $pager = new ContribsPager( $target, $this->opts
['namespace'],
381 $this->opts
['year'], $this->opts
['month'], $this->opts
['tagfilter'] );
383 $pager->mLimit
= min( $this->opts
['limit'], $wgFeedLimit );
386 if( $pager->getNumRows() > 0 ) {
387 while( $row = $pager->mResult
->fetchObject() ) {
388 $feed->outItem( $this->feedItem( $row ) );
394 protected function feedTitle() {
395 global $wgContLanguageCode, $wgSitename;
396 $page = SpecialPage
::getPage( 'Contributions' );
397 $desc = $page->getDescription();
398 return "$wgSitename - $desc [$wgContLanguageCode]";
401 protected function feedItem( $row ) {
402 $title = Title
::MakeTitle( intval( $row->page_namespace
), $row->page_title
);
404 $date = $row->rev_timestamp
;
405 $comments = $title->getTalkPage()->getFullURL();
406 $revision = Revision
::newFromTitle( $title, $row->rev_id
);
409 $title->getPrefixedText(),
410 $this->feedItemDesc( $revision ),
411 $title->getFullURL(),
413 $this->feedItemAuthor( $revision ),
421 protected function feedItemAuthor( $revision ) {
422 return $revision->getUserText();
425 protected function feedItemDesc( $revision ) {
427 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
428 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
429 "</p>\n<hr />\n<div>" .
430 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
437 * Pager for Special:Contributions
438 * @ingroup SpecialPage Pager
440 class ContribsPager
extends ReverseChronologicalPager
{
441 public $mDefaultDirection = true;
442 var $messages, $target;
443 var $namespace = '', $mDb;
445 function __construct( $target, $namespace = false, $year = false, $month = false, $tagFilter = false ) {
446 parent
::__construct();
447 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist' ) as $msg ) {
448 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escape') );
450 $this->target
= $target;
451 $this->namespace = $namespace;
452 $this->tagFilter
= $tagFilter;
454 $this->getDateCond( $year, $month );
456 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
459 function getDefaultQuery() {
460 $query = parent
::getDefaultQuery();
461 $query['target'] = $this->target
;
465 function getQueryInfo() {
467 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
469 $conds = array_merge( $userCond, $this->getNamespaceCond() );
470 // Paranoia: avoid brute force searches (bug 17342)
471 if( !$wgUser->isAllowed( 'deleterevision' ) ) {
472 $conds[] = $this->mDb
->bitAnd('rev_deleted',Revision
::DELETED_USER
) . ' = 0';
473 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
474 $conds[] = $this->mDb
->bitAnd('rev_deleted',Revision
::SUPPRESSED_USER
) .
475 ' != ' . Revision
::SUPPRESSED_USER
;
477 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
482 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
483 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
484 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted'
487 'options' => array( 'USE INDEX' => array('revision' => $index) ),
488 'join_conds' => $join_cond
491 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'],
492 $queryInfo['fields'],
494 $queryInfo['join_conds'],
495 $queryInfo['options'],
498 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
502 function getUserCond() {
503 $condition = array();
504 $join_conds = array();
505 if( $this->target
== 'newbies' ) {
506 $tables = array( 'user_groups', 'page', 'revision' );
507 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
508 $condition[] = 'rev_user >' . (int)($max - $max / 100);
509 $condition[] = 'ug_group IS NULL';
510 $index = 'user_timestamp';
511 # FIXME: other groups may have 'bot' rights
512 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
514 $tables = array( 'page', 'revision' );
515 $condition['rev_user_text'] = $this->target
;
516 $index = 'usertext_timestamp';
518 return array( $tables, $index, $condition, $join_conds );
521 function getNamespaceCond() {
522 if( $this->namespace !== '' ) {
523 return array( 'page_namespace' => (int)$this->namespace );
529 function getIndexField() {
530 return 'rev_timestamp';
533 function getStartBody() {
537 function getEndBody() {
542 * Generates each row in the contributions list.
544 * Contributions which are marked "top" are currently on top of the history.
545 * For these contributions, a [rollback] link is shown for users with roll-
546 * back privileges. The rollback link restores the most recent version that
547 * was not written by the target user.
549 * @todo This would probably look a lot nicer in a table.
551 function formatRow( $row ) {
552 global $wgUser, $wgLang, $wgContLang;
553 wfProfileIn( __METHOD__
);
555 $sk = $this->getSkin();
556 $rev = new Revision( $row );
559 $page = Title
::newFromRow( $row );
560 $page->resetArticleId( $row->rev_page
); // use process cache
563 htmlspecialchars( $page->getPrefixedText() ),
565 $page->isRedirect() ?
array( 'redirect' => 'no' ) : array()
567 # Mark current revisions
568 $difftext = $topmarktext = '';
569 if( $row->rev_id
== $row->page_latest
) {
570 $topmarktext .= '<span class="mw-uctop">' . $this->messages
['uctop'] . '</span>';
571 if( !$row->page_is_new
) {
572 $difftext .= '(' . $sk->linkKnown(
574 $this->messages
['diff'],
579 if( $page->quickUserCan( 'rollback') && $page->quickUserCan( 'edit' ) ) {
580 $topmarktext .= ' '.$sk->generateRollback( $rev );
583 $difftext .= $this->messages
['newarticle'];
586 # Is there a visible previous revision?
587 if( !$rev->isDeleted(Revision
::DELETED_TEXT
) ) {
588 $difftext = '(' . $sk->linkKnown(
590 $this->messages
['diff'],
594 'oldid' => $row->rev_id
598 $difftext = '(' . $this->messages
['diff'] . ')';
600 $histlink = '('.$sk->linkKnown(
602 $this->messages
['hist'],
604 array( 'action' => 'history' )
607 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
608 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
609 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
610 $d = '<span class="history-deleted">' . $date . '</span>';
614 htmlspecialchars($date),
616 array( 'oldid' => intval( $row->rev_id
) )
620 if( $this->target
== 'newbies' ) {
621 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
622 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
627 if( $rev->getParentId() === 0 ) {
628 $nflag = ChangesList
::flag( 'newpage' );
633 if( $rev->isMinor() ) {
634 $mflag = ChangesList
::flag( 'minor' );
639 if( $wgUser->isAllowed( 'deleterevision' ) ) {
640 // If revision was hidden from sysops
641 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
642 $del = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
643 '(' . $this->message
['rev-delundel'] . ')' ) . ' ';
644 // Otherwise, show the link...
647 'type' => 'revision',
648 'target' => $page->getPrefixedDbkey(),
649 'ids' => $rev->getId() );
650 $del = $this->mSkin
->revDeleteLink( $query,
651 $rev->isDeleted( Revision
::DELETED_RESTRICTED
) ) . ' ';
657 $ret = "{$del}{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
660 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'contributions' );
661 $classes = array_merge( $classes, $newClasses );
662 $ret .= " $tagSummary";
664 // Let extensions add data
665 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
667 $classes = implode( ' ', $classes );
668 $ret = "<li class=\"$classes\">$ret</li>\n";
669 wfProfileOut( __METHOD__
);
674 * Get the Database object in use
678 public function getDatabase() {