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 = IP
::isIPAddress( $target ) ?
115 'sp-contributions-footer-anon' : 'sp-contributions-footer';
117 $text = wfMsgNoTrans( $message, $target );
118 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
120 "<div class='mw-contributions-footer'>\n$1\n</div>", $message );
125 protected function setSyndicated() {
127 $queryParams = array(
128 'namespace' => $this->opts
['namespace'],
129 'target' => $this->opts
['target']
131 $wgOut->setSyndicated( true );
132 $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
136 * Generates the subheading with links
137 * @param Title $nt Title object for the target
138 * @param integer $id User ID for the target
139 * @return String: appropriately-escaped HTML to be output literally
141 protected function contributionsSub( $nt, $id ) {
142 global $wgSysopUserBans, $wgLang, $wgUser;
144 $sk = $wgUser->getSkin();
147 $user = $nt->getText();
149 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
151 $talk = $nt->getTalkPage();
154 $tools[] = $sk->link( $talk, wfMsgHtml( 'sp-contributions-talk' ) );
155 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && IP
::isIPAddress( $nt->getText() ) ) ) {
157 if( $wgUser->isAllowed( 'block' ) )
158 $tools[] = $sk->linkKnown(
159 SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ),
160 wfMsgHtml( 'blocklink' )
163 $tools[] = $sk->linkKnown(
164 SpecialPage
::getTitleFor( 'Log' ),
165 wfMsgHtml( 'sp-contributions-blocklog' ),
169 'page' => $nt->getPrefixedText()
174 $tools[] = $sk->linkKnown(
175 SpecialPage
::getTitleFor( 'Log' ),
176 wfMsgHtml( 'sp-contributions-logs' ),
178 array( 'user' => $nt->getText() )
181 # Add link to deleted user contributions for priviledged users
182 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
183 $tools[] = $sk->linkKnown(
184 SpecialPage
::getTitleFor( 'DeletedContributions', $nt->getDBkey() ),
185 wfMsgHtml( 'sp-contributions-deleted' )
189 # Add a link to change user rights for privileged users
190 $userrightsPage = new UserrightsPage();
191 if( 0 !== $id && $userrightsPage->userCanChangeRights( User
::newFromId( $id ) ) ) {
192 $tools[] = $sk->linkKnown(
193 SpecialPage
::getTitleFor( 'Userrights', $nt->getDBkey() ),
194 wfMsgHtml( 'sp-contributions-userrights' )
198 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
200 $links = $wgLang->pipeList( $tools );
203 // Old message 'contribsub' had one parameter, but that doesn't work for
204 // languages that want to put the "for" bit right after $user but before
205 // $links. If 'contribsub' is around, use it for reverse compatibility,
206 // otherwise use 'contribsub2'.
207 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
208 return wfMsgHtml( 'contribsub2', $user, $links );
210 return wfMsgHtml( 'contribsub', "$user ($links)" );
215 * Generates the namespace selector form with hidden attributes.
216 * @param $this->opts Array: the options to be included.
218 protected function getForm() {
221 $this->opts
['title'] = $this->getTitle()->getPrefixedText();
222 if( !isset( $this->opts
['target'] ) ) {
223 $this->opts
['target'] = '';
225 $this->opts
['target'] = str_replace( '_' , ' ' , $this->opts
['target'] );
228 if( !isset( $this->opts
['namespace'] ) ) {
229 $this->opts
['namespace'] = '';
232 if( !isset( $this->opts
['contribs'] ) ) {
233 $this->opts
['contribs'] = 'user';
236 if( !isset( $this->opts
['year'] ) ) {
237 $this->opts
['year'] = '';
240 if( !isset( $this->opts
['month'] ) ) {
241 $this->opts
['month'] = '';
244 if( $this->opts
['contribs'] == 'newbie' ) {
245 $this->opts
['target'] = '';
248 if( !isset( $this->opts
['tagfilter'] ) ) {
249 $this->opts
['tagfilter'] = '';
252 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
253 # Add hidden params for tracking
254 foreach ( $this->opts
as $name => $value ) {
255 if( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
258 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
261 $tagFilter = ChangeTags
::buildTagFilterSelector( $this->opts
['tagfilter'] );
264 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
265 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parsemag' ) ),
266 'contribs', 'newbie' , 'newbie', $this->opts
['contribs'] == 'newbie' ?
true : false ) . '<br />' .
267 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parsemag' ) ),
268 'contribs' , 'user', 'user', $this->opts
['contribs'] == 'user' ?
true : false ) . ' ' .
269 Xml
::input( 'target', 20, $this->opts
['target']) . ' '.
270 '<span style="white-space: nowrap">' .
271 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
272 Xml
::namespaceSelector( $this->opts
['namespace'], '' ) .
274 ( $tagFilter ? Xml
::tags( 'p', null, implode( ' ', $tagFilter ) ) : '' ) .
275 Xml
::openElement( 'p' ) .
276 '<span style="white-space: nowrap">' .
277 Xml
::dateMenu( $this->opts
['year'], $this->opts
['month'] ) .
279 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
280 Xml
::closeElement( 'p' );
282 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
283 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
284 $f .= "<p>{$explain}</p>";
286 $f .= '</fieldset>' .
287 Xml
::closeElement( 'form' );
292 * Output a subscription feed listing recent edits to this page.
293 * @param string $type
295 protected function feed( $type ) {
296 global $wgRequest, $wgFeed, $wgFeedClasses, $wgFeedLimit;
300 $wgOut->addWikiMsg( 'feed-unavailable' );
304 if( !isset( $wgFeedClasses[$type] ) ) {
306 $wgOut->addWikiMsg( 'feed-invalid' );
310 $feed = new $wgFeedClasses[$type](
312 wfMsgExt( 'tagline', 'parsemag' ),
313 $this->getTitle()->getFullUrl() . "/" . urlencode($this->opts
['target'])
316 // Already valid title
317 $nt = Title
::makeTitleSafe( NS_USER
, $this->opts
['target'] );
318 $target = $this->opts
['target'] == 'newbies' ?
'newbies' : $nt->getText();
320 $pager = new ContribsPager( $target, $this->opts
['namespace'],
321 $this->opts
['year'], $this->opts
['month'], $this->opts
['tagfilter'] );
323 $pager->mLimit
= min( $this->opts
['limit'], $wgFeedLimit );
326 if( $pager->getNumRows() > 0 ) {
327 while( $row = $pager->mResult
->fetchObject() ) {
328 $feed->outItem( $this->feedItem( $row ) );
334 protected function feedTitle() {
335 global $wgContLanguageCode, $wgSitename;
336 $page = SpecialPage
::getPage( 'Contributions' );
337 $desc = $page->getDescription();
338 return "$wgSitename - $desc [$wgContLanguageCode]";
341 protected function feedItem( $row ) {
342 $title = Title
::MakeTitle( intval( $row->page_namespace
), $row->page_title
);
344 $date = $row->rev_timestamp
;
345 $comments = $title->getTalkPage()->getFullURL();
346 $revision = Revision
::newFromTitle( $title, $row->rev_id
);
349 $title->getPrefixedText(),
350 $this->feedItemDesc( $revision ),
351 $title->getFullURL(),
353 $this->feedItemAuthor( $revision ),
361 protected function feedItemAuthor( $revision ) {
362 return $revision->getUserText();
365 protected function feedItemDesc( $revision ) {
367 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
368 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
369 "</p>\n<hr />\n<div>" .
370 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
377 * Pager for Special:Contributions
378 * @ingroup SpecialPage Pager
380 class ContribsPager
extends ReverseChronologicalPager
{
381 public $mDefaultDirection = true;
382 var $messages, $target;
383 var $namespace = '', $mDb;
385 function __construct( $target, $namespace = false, $year = false, $month = false, $tagFilter = false ) {
386 parent
::__construct();
387 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter minoreditletter' ) as $msg ) {
388 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escape') );
390 $this->target
= $target;
391 $this->namespace = $namespace;
392 $this->tagFilter
= $tagFilter;
394 $this->getDateCond( $year, $month );
396 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
399 function getDefaultQuery() {
400 $query = parent
::getDefaultQuery();
401 $query['target'] = $this->target
;
405 function getQueryInfo() {
407 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
409 $conds = array_merge( $userCond, $this->getNamespaceCond() );
410 // Paranoia: avoid brute force searches (bug 17342)
411 if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
412 $conds[] = $this->mDb
->bitAnd('rev_deleted', Revision
::DELETED_USER
) . ' = 0';
414 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
419 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
420 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
421 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted'
424 'options' => array( 'USE INDEX' => array('revision' => $index) ),
425 'join_conds' => $join_cond
428 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'],
429 $queryInfo['fields'],
431 $queryInfo['join_conds'],
432 $queryInfo['options'],
435 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
439 function getUserCond() {
440 $condition = array();
441 $join_conds = array();
442 if( $this->target
== 'newbies' ) {
443 $tables = array( 'user_groups', 'page', 'revision' );
444 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
445 $condition[] = 'rev_user >' . (int)($max - $max / 100);
446 $condition[] = 'ug_group IS NULL';
447 $index = 'user_timestamp';
448 # FIXME: other groups may have 'bot' rights
449 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
451 $tables = array( 'page', 'revision' );
452 $condition['rev_user_text'] = $this->target
;
453 $index = 'usertext_timestamp';
455 return array( $tables, $index, $condition, $join_conds );
458 function getNamespaceCond() {
459 if( $this->namespace !== '' ) {
460 return array( 'page_namespace' => (int)$this->namespace );
466 function getIndexField() {
467 return 'rev_timestamp';
470 function getStartBody() {
474 function getEndBody() {
479 * Generates each row in the contributions list.
481 * Contributions which are marked "top" are currently on top of the history.
482 * For these contributions, a [rollback] link is shown for users with roll-
483 * back privileges. The rollback link restores the most recent version that
484 * was not written by the target user.
486 * @todo This would probably look a lot nicer in a table.
488 function formatRow( $row ) {
489 global $wgUser, $wgLang, $wgContLang;
490 wfProfileIn( __METHOD__
);
492 $sk = $this->getSkin();
493 $rev = new Revision( $row );
496 $page = Title
::newFromRow( $row );
497 $page->resetArticleId( $row->rev_page
); // use process cache
500 htmlspecialchars( $page->getPrefixedText() ),
502 $page->isRedirect() ?
array( 'redirect' => 'no' ) : array()
504 # Mark current revisions
505 $difftext = $topmarktext = '';
506 if( $row->rev_id
== $row->page_latest
) {
507 $topmarktext .= '<span class="mw-uctop">' . $this->messages
['uctop'] . '</span>';
508 if( !$row->page_is_new
) {
509 $difftext .= '(' . $sk->linkKnown(
511 $this->messages
['diff'],
516 if( $page->quickUserCan( 'rollback') && $page->quickUserCan( 'edit' ) ) {
517 $topmarktext .= ' '.$sk->generateRollback( $rev );
520 $difftext .= $this->messages
['newarticle'];
523 # Is there a visible previous revision?
524 if( !$rev->isDeleted(Revision
::DELETED_TEXT
) ) {
525 $difftext = '(' . $sk->linkKnown(
527 $this->messages
['diff'],
531 'oldid' => $row->rev_id
535 $difftext = '(' . $this->messages
['diff'] . ')';
537 $histlink = '('.$sk->linkKnown(
539 $this->messages
['hist'],
541 array( 'action' => 'history' )
544 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
545 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
546 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
547 $d = '<span class="history-deleted">' . $date . '</span>';
551 htmlspecialchars($date),
553 array( 'oldid' => intval( $row->rev_id
) )
557 if( $this->target
== 'newbies' ) {
558 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
559 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
564 if( $rev->getParentId() === 0 ) {
565 $nflag = '<span class="newpage">' . $this->messages
['newpageletter'] . '</span>';
570 if( $rev->isMinor() ) {
571 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
576 if( $wgUser->isAllowed( 'deleterevision' ) ) {
577 // If revision was hidden from sysops
578 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
579 $del = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
580 '(' . $this->message
['rev-delundel'] . ')' ) . ' ';
581 // Otherwise, show the link...
584 'type' => 'revision',
585 'target' => $page->getPrefixedDbkey(),
586 'ids' => $rev->getId() );
587 $del = $this->mSkin
->revDeleteLink( $query,
588 $rev->isDeleted( Revision
::DELETED_RESTRICTED
) ) . ' ';
594 $ret = "{$del}{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
597 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'contributions' );
598 $classes = array_merge( $classes, $newClasses );
599 $ret .= " $tagSummary";
601 // Let extensions add data
602 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
604 $classes = implode( ' ', $classes );
605 $ret = "<li class=\"$classes\">$ret</li>\n";
606 wfProfileOut( __METHOD__
);
611 * Get the Database object in use
615 public function getDatabase() {