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 # If there were contributions, and it was a valid user or IP, show
113 # the appropriate "footer" message - WHOIS tools, etc.
114 if( $target != 'newbies' ) {
115 $message = IP
::isIPAddress( $target ) ?
116 'sp-contributions-footer-anon' : 'sp-contributions-footer';
118 $text = wfMsgNoTrans( $message, $target );
119 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
121 "<div class='mw-contributions-footer'>\n$1\n</div>", $message );
126 protected function setSyndicated() {
128 $queryParams = array(
129 'namespace' => $this->opts
['namespace'],
130 'target' => $this->opts
['target']
132 $wgOut->setSyndicated( true );
133 $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
137 * Generates the subheading with links
138 * @param Title $nt Title object for the target
139 * @param integer $id User ID for the target
140 * @return String: appropriately-escaped HTML to be output literally
142 protected function contributionsSub( $nt, $id ) {
143 global $wgSysopUserBans, $wgLang, $wgUser;
145 $sk = $wgUser->getSkin();
148 $user = $nt->getText();
150 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
152 $talk = $nt->getTalkPage();
155 $tools[] = $sk->link( $talk, wfMsgHtml( 'sp-contributions-talk' ) );
156 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && IP
::isIPAddress( $nt->getText() ) ) ) {
158 if( $wgUser->isAllowed( 'block' ) )
159 $tools[] = $sk->linkKnown(
160 SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ),
161 wfMsgHtml( 'blocklink' )
164 $tools[] = $sk->linkKnown(
165 SpecialPage
::getTitleFor( 'Log' ),
166 wfMsgHtml( 'sp-contributions-blocklog' ),
170 'page' => $nt->getPrefixedText()
175 $tools[] = $sk->linkKnown(
176 SpecialPage
::getTitleFor( 'Log' ),
177 wfMsg( 'sp-contributions-logs' ),
179 array( 'user' => $nt->getText() )
182 # Add link to deleted user contributions for priviledged users
183 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
184 $tools[] = $sk->linkKnown(
185 SpecialPage
::getTitleFor( 'DeletedContributions', $nt->getDBkey() ),
186 wfMsgHtml( 'sp-contributions-deleted' )
190 # Add a link to change user rights for privileged users
191 $userrightsPage = new UserrightsPage();
192 if( 0 !== $id && $userrightsPage->userCanChangeRights( User
::newFromId( $id ) ) ) {
193 $tools[] = $sk->linkKnown(
194 SpecialPage
::getTitleFor( 'Userrights', $nt->getDBkey() ),
195 wfMsgHtml( 'sp-contributions-userrights' )
199 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
201 $links = $wgLang->pipeList( $tools );
204 // Old message 'contribsub' had one parameter, but that doesn't work for
205 // languages that want to put the "for" bit right after $user but before
206 // $links. If 'contribsub' is around, use it for reverse compatibility,
207 // otherwise use 'contribsub2'.
208 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
209 return wfMsgHtml( 'contribsub2', $user, $links );
211 return wfMsgHtml( 'contribsub', "$user ($links)" );
216 * Generates the namespace selector form with hidden attributes.
217 * @param $this->opts Array: the options to be included.
219 protected function getForm() {
222 $this->opts
['title'] = SpecialPage
::getTitleFor( 'Contributions' )->getPrefixedText();
223 if( !isset( $this->opts
['target'] ) ) {
224 $this->opts
['target'] = '';
226 $this->opts
['target'] = str_replace( '_' , ' ' , $this->opts
['target'] );
229 if( !isset( $this->opts
['namespace'] ) ) {
230 $this->opts
['namespace'] = '';
233 if( !isset( $this->opts
['contribs'] ) ) {
234 $this->opts
['contribs'] = 'user';
237 if( !isset( $this->opts
['year'] ) ) {
238 $this->opts
['year'] = '';
241 if( !isset( $this->opts
['month'] ) ) {
242 $this->opts
['month'] = '';
245 if( $this->opts
['contribs'] == 'newbie' ) {
246 $this->opts
['target'] = '';
249 if( !isset( $this->opts
['tagfilter'] ) ) {
250 $this->opts
['tagfilter'] = '';
253 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
254 # Add hidden params for tracking
255 foreach ( $this->opts
as $name => $value ) {
256 if( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
259 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
262 $tagFilter = ChangeTags
::buildTagFilterSelector( $this->opts
['tagfilter'] );
265 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
266 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ),
267 'contribs', 'newbie' , 'newbie', $this->opts
['contribs'] == 'newbie' ?
true : false ) . '<br />' .
268 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parsemag' ) ),
269 'contribs' , 'user', 'user', $this->opts
['contribs'] == 'user' ?
true : false ) . ' ' .
270 Xml
::input( 'target', 20, $this->opts
['target']) . ' '.
271 '<span style="white-space: nowrap">' .
272 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
273 Xml
::namespaceSelector( $this->opts
['namespace'], '' ) .
275 ( $tagFilter ? Xml
::tags( 'p', null, implode( ' ', $tagFilter ) ) : '' ) .
276 Xml
::openElement( 'p' ) .
277 '<span style="white-space: nowrap">' .
278 Xml
::dateMenu( $this->opts
['year'], $this->opts
['month'] ) .
280 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
281 Xml
::closeElement( 'p' );
283 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
284 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
285 $f .= "<p>{$explain}</p>";
287 $f .= '</fieldset>' .
288 Xml
::closeElement( 'form' );
293 * Output a subscription feed listing recent edits to this page.
294 * @param string $type
296 protected function feed( $type ) {
297 global $wgRequest, $wgFeed, $wgFeedClasses, $wgFeedLimit;
301 $wgOut->addWikiMsg( 'feed-unavailable' );
305 if( !isset( $wgFeedClasses[$type] ) ) {
307 $wgOut->addWikiMsg( 'feed-invalid' );
311 $feed = new $wgFeedClasses[$type](
313 wfMsgExt( 'tagline', 'parsemag' ),
314 $this->getTitle()->getFullUrl() . "/" . urlencode($this->opts
['target'])
317 // Already valid title
318 $nt = Title
::makeTitleSafe( NS_USER
, $this->opts
['target'] );
319 $target = $this->opts
['target'] == 'newbies' ?
'newbies' : $nt->getText();
321 $pager = new ContribsPager( $target, $this->opts
['namespace'],
322 $this->opts
['year'], $this->opts
['month'], $this->opts
['tagfilter'] );
324 $pager->mLimit
= min( $this->opts
['limit'], $wgFeedLimit );
327 if( $pager->getNumRows() > 0 ) {
328 while( $row = $pager->mResult
->fetchObject() ) {
329 $feed->outItem( $this->feedItem( $row ) );
335 protected function feedTitle() {
336 global $wgContLanguageCode, $wgSitename;
337 $page = SpecialPage
::getPage( 'Contributions' );
338 $desc = $page->getDescription();
339 return "$wgSitename - $desc [$wgContLanguageCode]";
342 protected function feedItem( $row ) {
343 $title = Title
::MakeTitle( intval( $row->page_namespace
), $row->page_title
);
345 $date = $row->rev_timestamp
;
346 $comments = $title->getTalkPage()->getFullURL();
347 $revision = Revision
::newFromTitle( $title, $row->rev_id
);
350 $title->getPrefixedText(),
351 $this->feedItemDesc( $revision ),
352 $title->getFullURL(),
354 $this->feedItemAuthor( $revision ),
362 protected function feedItemAuthor( $revision ) {
363 return $revision->getUserText();
366 protected function feedItemDesc( $revision ) {
368 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
369 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
370 "</p>\n<hr />\n<div>" .
371 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
378 * Pager for Special:Contributions
379 * @ingroup SpecialPage Pager
381 class ContribsPager
extends ReverseChronologicalPager
{
382 public $mDefaultDirection = true;
383 var $messages, $target;
384 var $namespace = '', $mDb;
386 function __construct( $target, $namespace = false, $year = false, $month = false, $tagFilter = false ) {
387 parent
::__construct();
388 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter minoreditletter' ) as $msg ) {
389 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escape') );
391 $this->target
= $target;
392 $this->namespace = $namespace;
393 $this->tagFilter
= $tagFilter;
395 $this->getDateCond( $year, $month );
397 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
400 function getDefaultQuery() {
401 $query = parent
::getDefaultQuery();
402 $query['target'] = $this->target
;
406 function getQueryInfo() {
408 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
410 $conds = array_merge( $userCond, $this->getNamespaceCond() );
411 // Paranoia: avoid brute force searches (bug 17342)
412 if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
413 $conds[] = $this->mDb
->bitAnd('rev_deleted', Revision
::DELETED_USER
) . ' = 0';
415 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
420 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
421 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
422 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted'
425 'options' => array( 'USE INDEX' => array('revision' => $index) ),
426 'join_conds' => $join_cond
429 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'],
430 $queryInfo['fields'],
432 $queryInfo['join_conds'],
433 $queryInfo['options'],
436 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
440 function getUserCond() {
441 $condition = array();
442 $join_conds = array();
443 if( $this->target
== 'newbies' ) {
444 $tables = array( 'user_groups', 'page', 'revision' );
445 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
446 $condition[] = 'rev_user >' . (int)($max - $max / 100);
447 $condition[] = 'ug_group IS NULL';
448 $index = 'user_timestamp';
449 # FIXME: other groups may have 'bot' rights
450 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
452 $tables = array( 'page', 'revision' );
453 $condition['rev_user_text'] = $this->target
;
454 $index = 'usertext_timestamp';
456 return array( $tables, $index, $condition, $join_conds );
459 function getNamespaceCond() {
460 if( $this->namespace !== '' ) {
461 return array( 'page_namespace' => (int)$this->namespace );
467 function getIndexField() {
468 return 'rev_timestamp';
471 function getStartBody() {
475 function getEndBody() {
480 * Generates each row in the contributions list.
482 * Contributions which are marked "top" are currently on top of the history.
483 * For these contributions, a [rollback] link is shown for users with roll-
484 * back privileges. The rollback link restores the most recent version that
485 * was not written by the target user.
487 * @todo This would probably look a lot nicer in a table.
489 function formatRow( $row ) {
490 global $wgUser, $wgLang, $wgContLang;
491 wfProfileIn( __METHOD__
);
493 $sk = $this->getSkin();
494 $rev = new Revision( $row );
497 $page = Title
::newFromRow( $row );
498 $page->resetArticleId( $row->rev_page
); // use process cache
501 $page->getPrefixedText(),
503 $page->isRedirect() ?
array( 'redirect' => 'no' ) : array()
505 # Mark current revisions
506 $difftext = $topmarktext = '';
507 if( $row->rev_id
== $row->page_latest
) {
508 $topmarktext .= '<span class="mw-uctop">' . $this->messages
['uctop'] . '</span>';
509 if( !$row->page_is_new
) {
510 $difftext .= '(' . $sk->linkKnown(
512 $this->messages
['diff'],
517 if( $page->quickUserCan( 'rollback') && $page->quickUserCan( 'edit' ) ) {
518 $topmarktext .= ' '.$sk->generateRollback( $rev );
521 $difftext .= $this->messages
['newarticle'];
524 # Is there a visible previous revision?
525 if( $rev->userCan(Revision
::DELETED_TEXT
) ) {
526 $difftext = '(' . $sk->linkKnown(
528 $this->messages
['diff'],
532 'oldid' => $row->rev_id
536 $difftext = '(' . $this->messages
['diff'] . ')';
538 $histlink = '('.$sk->linkKnown(
540 $this->messages
['hist'],
542 array( 'action' => 'history' )
545 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
546 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
549 htmlspecialchars($date),
551 array( 'oldid' => intval( $row->rev_id
) )
554 if( $this->target
== 'newbies' ) {
555 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
556 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
561 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
562 $d = '<span class="history-deleted">' . $d . '</span>';
565 if( $rev->getParentId() === 0 ) {
566 $nflag = '<span class="newpage">' . $this->messages
['newpageletter'] . '</span>';
571 if( $rev->isMinor() ) {
572 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
577 if( $wgUser->isAllowed( 'deleterevision' ) ) {
578 // If revision was hidden from sysops
579 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
580 $del = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
581 '(' . $this->message
['rev-delundel'] . ')' ) . ' ';
582 // Otherwise, show the link...
585 'type' => 'revision',
586 'target' => $page->getPrefixedDbkey(),
587 'ids' => $rev->getId() );
588 $del = $this->mSkin
->revDeleteLink( $query,
589 $rev->isDeleted( Revision
::DELETED_RESTRICTED
) ) . ' ';
595 $ret = "{$del}{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
598 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'contributions' );
599 $classes = array_merge( $classes, $newClasses );
600 $ret .= " $tagSummary";
602 // Let extensions add data
603 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
605 $classes = implode( ' ', $classes );
606 $ret = "<li class=\"$classes\">$ret</li>\n";
607 wfProfileOut( __METHOD__
);
612 * Get the Database object in use
616 public function getDatabase() {