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', 50 );
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' );
102 # Show a message about slave lag, if applicable
103 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
104 $wgOut->showLagWarning( $lag );
107 '<p>' . $pager->getNavigationBar() . '</p>' .
109 '<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 != '-' ) {
120 $wgOut->addHTML( '<div class="mw-contributions-footer">' );
121 $wgOut->addWikiText( $text );
122 $wgOut->addHTML( '</div>' );
127 protected function setSyndicated() {
129 $queryParams = array(
130 'namespace' => $this->opts
['namespace'],
131 'target' => $this->opts
['target']
133 $wgOut->setSyndicated( true );
134 $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
138 * Generates the subheading with links
139 * @param Title $nt Title object for the target
140 * @param integer $id User ID for the target
141 * @return String: appropriately-escaped HTML to be output literally
143 protected function contributionsSub( $nt, $id ) {
144 global $wgSysopUserBans, $wgLang, $wgUser;
146 $sk = $wgUser->getSkin();
149 $user = $nt->getText();
151 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
153 $talk = $nt->getTalkPage();
156 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
157 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && IP
::isIPAddress( $nt->getText() ) ) ) {
159 if( $wgUser->isAllowed( 'block' ) )
160 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip',
161 $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
163 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ),
164 wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
167 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ),
168 'user=' . $nt->getPartialUrl() );
170 # Add link to deleted user contributions for priviledged users
171 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
172 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'DeletedContributions',
173 $nt->getDBkey() ), wfMsgHtml( 'deletedcontributions' ) );
176 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
178 $links = $wgLang->pipeList( $tools );
181 // Old message 'contribsub' had one parameter, but that doesn't work for
182 // languages that want to put the "for" bit right after $user but before
183 // $links. If 'contribsub' is around, use it for reverse compatibility,
184 // otherwise use 'contribsub2'.
185 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
186 return wfMsgHtml( 'contribsub2', $user, $links );
188 return wfMsgHtml( 'contribsub', "$user ($links)" );
193 * Generates the namespace selector form with hidden attributes.
194 * @param $this->opts Array: the options to be included.
196 protected function getForm() {
197 global $wgScript, $wgTitle;
199 $this->opts
['title'] = $wgTitle->getPrefixedText();
200 if( !isset( $this->opts
['target'] ) ) {
201 $this->opts
['target'] = '';
203 $this->opts
['target'] = str_replace( '_' , ' ' , $this->opts
['target'] );
206 if( !isset( $this->opts
['namespace'] ) ) {
207 $this->opts
['namespace'] = '';
210 if( !isset( $this->opts
['contribs'] ) ) {
211 $this->opts
['contribs'] = 'user';
214 if( !isset( $this->opts
['year'] ) ) {
215 $this->opts
['year'] = '';
218 if( !isset( $this->opts
['month'] ) ) {
219 $this->opts
['month'] = '';
222 if( $this->opts
['contribs'] == 'newbie' ) {
223 $this->opts
['target'] = '';
226 if( !isset( $this->opts
['tagfilter'] ) ) {
227 $this->opts
['tagfilter'] = '';
230 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
231 # Add hidden params for tracking
232 foreach ( $this->opts
as $name => $value ) {
233 if( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
236 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
239 $tagFilter = ChangeTags
::buildTagFilterSelector( $this->opts
['tagfilter'] );
242 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
243 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ),
244 'contribs', 'newbie' , 'newbie', $this->opts
['contribs'] == 'newbie' ?
true : false ) . '<br />' .
245 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ),
246 'contribs' , 'user', 'user', $this->opts
['contribs'] == 'user' ?
true : false ) . ' ' .
247 Xml
::input( 'target', 20, $this->opts
['target']) . ' '.
248 '<span style="white-space: nowrap">' .
249 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
250 Xml
::namespaceSelector( $this->opts
['namespace'], '' ) .
252 ( $tagFilter ? Xml
::tags( 'p', null, implode( ' ', $tagFilter ) ) : '' ) .
253 Xml
::openElement( 'p' ) .
254 '<span style="white-space: nowrap">' .
255 Xml
::dateMenu( $this->opts
['year'], $this->opts
['month'] ) .
257 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
258 Xml
::closeElement( 'p' );
260 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
261 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
262 $f .= "<p>{$explain}</p>";
264 $f .= '</fieldset>' .
265 Xml
::closeElement( 'form' );
270 * Output a subscription feed listing recent edits to this page.
271 * @param string $type
273 protected function feed( $type ) {
274 global $wgRequest, $wgFeed, $wgFeedClasses, $wgFeedLimit;
278 $wgOut->addWikiMsg( 'feed-unavailable' );
282 if( !isset( $wgFeedClasses[$type] ) ) {
284 $wgOut->addWikiMsg( 'feed-invalid' );
288 $feed = new $wgFeedClasses[$type](
290 wfMsgExt( 'tagline', 'parsemag' ),
291 $this->getTitle()->getFullUrl() . "/" . htmlspecialchars( $this->opts
['target'] ) );
293 // Already valid title
294 $nt = Title
::makeTitleSafe( NS_USER
, $this->opts
['target'] );
295 $target = $this->opts
['target'] == 'newbies' ?
'newbies' : $nt->getText();
297 $pager = new ContribsPager( $target, $this->opts
['namespace'],
298 $this->opts
['year'], $this->opts
['month'], $this->opts
['tagfilter'] );
300 $pager->mLimit
= min( $this->opts
['limit'], $wgFeedLimit );
303 if( $pager->getNumRows() > 0 ) {
304 while( $row = $pager->mResult
->fetchObject() ) {
305 $feed->outItem( $this->feedItem( $row ) );
311 protected function feedTitle() {
312 global $wgContLanguageCode, $wgSitename;
313 $page = SpecialPage
::getPage( 'Contributions' );
314 $desc = $page->getDescription();
315 return "$wgSitename - $desc [$wgContLanguageCode]";
318 protected function feedItem( $row ) {
319 $title = Title
::MakeTitle( intval( $row->page_namespace
), $row->page_title
);
321 $date = $row->rev_timestamp
;
322 $comments = $title->getTalkPage()->getFullURL();
323 $revision = Revision
::newFromTitle( $title, $row->rev_id
);
326 $title->getPrefixedText(),
327 $this->feedItemDesc( $revision ),
328 $title->getFullURL(),
330 $this->feedItemAuthor( $revision ),
338 protected function feedItemAuthor( $revision ) {
339 return $revision->getUserText();
342 protected function feedItemDesc( $revision ) {
344 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
345 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
346 "</p>\n<hr />\n<div>" .
347 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
354 * Pager for Special:Contributions
355 * @ingroup SpecialPage Pager
357 class ContribsPager
extends ReverseChronologicalPager
{
358 public $mDefaultDirection = true;
359 var $messages, $target;
360 var $namespace = '', $mDb;
362 function __construct( $target, $namespace = false, $year = false, $month = false, $tagFilter = false ) {
363 parent
::__construct();
364 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter minoreditletter' ) as $msg ) {
365 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escape') );
367 $this->target
= $target;
368 $this->namespace = $namespace;
369 $this->tagFilter
= $tagFilter;
371 $this->getDateCond( $year, $month );
373 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
376 function getDefaultQuery() {
377 $query = parent
::getDefaultQuery();
378 $query['target'] = $this->target
;
382 function getQueryInfo() {
383 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
385 $conds = array_merge( $userCond, $this->getNamespaceCond() );
386 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
391 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
392 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
393 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted'
396 'options' => array( 'USE INDEX' => array('revision' => $index) ),
397 'join_conds' => $join_cond
400 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'], $queryInfo['fields'], $queryInfo['conds'], $queryInfo['join_conds'], $this->tagFilter
);
402 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
406 function getUserCond() {
407 $condition = array();
408 $join_conds = array();
409 if( $this->target
== 'newbies' ) {
410 $tables = array( 'user_groups', 'page', 'revision' );
411 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
412 $condition[] = 'rev_user >' . (int)($max - $max / 100);
413 $condition[] = 'ug_group IS NULL';
414 $index = 'user_timestamp';
415 # FIXME: other groups may have 'bot' rights
416 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
418 $tables = array( 'page', 'revision' );
419 $condition['rev_user_text'] = $this->target
;
420 $index = 'usertext_timestamp';
422 return array( $tables, $index, $condition, $join_conds );
425 function getNamespaceCond() {
426 if( $this->namespace !== '' ) {
427 return array( 'page_namespace' => (int)$this->namespace );
433 function getIndexField() {
434 return 'rev_timestamp';
437 function getStartBody() {
441 function getEndBody() {
446 * Generates each row in the contributions list.
448 * Contributions which are marked "top" are currently on top of the history.
449 * For these contributions, a [rollback] link is shown for users with roll-
450 * back privileges. The rollback link restores the most recent version that
451 * was not written by the target user.
453 * @todo This would probably look a lot nicer in a table.
455 function formatRow( $row ) {
456 global $wgLang, $wgUser, $wgContLang;
457 wfProfileIn( __METHOD__
);
459 $sk = $this->getSkin();
460 $rev = new Revision( $row );
463 $page = Title
::newFromRow( $row );
464 $page->resetArticleId( $row->rev_page
); // use process cache
465 $link = $sk->makeLinkObj( $page, $page->getPrefixedText(), $page->isRedirect() ?
'redirect=no' : '' );
466 # Mark current revisions
467 $difftext = $topmarktext = '';
468 if( $row->rev_id
== $row->page_latest
) {
469 $topmarktext .= '<strong>' . $this->messages
['uctop'] . '</strong>';
470 if( !$row->page_is_new
) {
471 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=0' ) . ')';
473 if( $page->quickUserCan( 'rollback') && $page->quickUserCan( 'edit' ) ) {
474 $topmarktext .= ' '.$sk->generateRollback( $rev );
477 $difftext .= $this->messages
['newarticle'];
480 # Is there a visible previous revision?
481 if( $rev->userCan(Revision
::DELETED_TEXT
) ) {
482 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'],
483 'diff=prev&oldid='.$row->rev_id
) . ')';
485 $difftext = '(' . $this->messages
['diff'] . ')';
487 $histlink = '('.$sk->makeKnownLinkObj( $page, $this->messages
['hist'], 'action=history' ) . ')';
489 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
490 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
491 $d = $sk->makeKnownLinkObj( $page, $date, 'oldid='.intval($row->rev_id
) );
493 if( $this->target
== 'newbies' ) {
494 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
495 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
500 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
501 $d = '<span class="history-deleted">' . $d . '</span>';
504 if( $rev->getParentId() === 0 ) {
505 $nflag = '<span class="newpage">' . $this->messages
['newpageletter'] . '</span>';
510 if( $rev->isMinor() ) {
511 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
516 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
517 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
518 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
522 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'contributions' );
523 $classes = array_merge( $classes, $newClasses );
524 $ret .= " $tagSummary";
526 // Let extensions add data
527 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
529 $classes = implode( ' ', $classes );
530 $ret = "<li class=\"$classes\">$ret</li>\n";
531 wfProfileOut( __METHOD__
);
536 * Get the Database object in use
540 public function getDatabase() {