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
77 if( ( $month = $wgRequest->getIntOrNull( 'month' ) ) !== null && $month !== -1 ) {
78 $this->opts
['month'] = intval( $month );
80 $this->opts
['month'] = '';
82 if( ( $year = $wgRequest->getIntOrNull( 'year' ) ) !== null ) {
83 $this->opts
['year'] = intval( $year );
84 } else if( $this->opts
['month'] ) {
85 $thisMonth = intval( gmdate( 'n' ) );
86 $thisYear = intval( gmdate( 'Y' ) );
87 if( intval( $this->opts
['month'] ) > $thisMonth ) {
90 $this->opts
['year'] = $thisYear;
92 $this->opts
['year'] = '';
96 $this->opts
['year'] = '';
97 $this->opts
['month'] = '';
100 // Add RSS/atom links
101 $this->setSyndicated();
102 $feedType = $wgRequest->getVal( 'feed' );
104 return $this->feed( $feedType );
107 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
109 $wgOut->addHTML( $this->getForm() );
111 $pager = new ContribsPager( $target, $this->opts
['namespace'], $this->opts
['year'], $this->opts
['month'] );
112 if( !$pager->getNumRows() ) {
113 $wgOut->addWikiMsg( 'nocontribs' );
117 # Show a message about slave lag, if applicable
118 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
119 $wgOut->showLagWarning( $lag );
122 '<p>' . $pager->getNavigationBar() . '</p>' .
124 '<p>' . $pager->getNavigationBar() . '</p>'
127 # If there were contributions, and it was a valid user or IP, show
128 # the appropriate "footer" message - WHOIS tools, etc.
129 if( $target != 'newbies' ) {
130 $message = IP
::isIPAddress( $target ) ?
131 'sp-contributions-footer-anon' : 'sp-contributions-footer';
133 $text = wfMsgNoTrans( $message, $target );
134 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
135 $wgOut->addHTML( '<div class="mw-contributions-footer">' );
136 $wgOut->addWikiText( $text );
137 $wgOut->addHTML( '</div>' );
142 protected function setSyndicated() {
144 $queryParams = array(
145 'namespace' => $this->opts
['namespace'],
146 'target' => $this->opts
['target']
148 $wgOut->setSyndicated( true );
149 $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
153 * Generates the subheading with links
154 * @param Title $nt Title object for the target
155 * @param integer $id User ID for the target
156 * @return String: appropriately-escaped HTML to be output literally
158 protected function contributionsSub( $nt, $id ) {
159 global $wgSysopUserBans, $wgLang, $wgUser;
161 $sk = $wgUser->getSkin();
164 $user = $nt->getText();
166 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
168 $talk = $nt->getTalkPage();
171 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
172 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && IP
::isIPAddress( $nt->getText() ) ) ) {
174 if( $wgUser->isAllowed( 'block' ) )
175 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip',
176 $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
178 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ),
179 wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
182 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ),
183 'user=' . $nt->getPartialUrl() );
185 # Add link to deleted user contributions for priviledged users
186 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
187 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'DeletedContributions',
188 $nt->getDBkey() ), wfMsgHtml( 'deletedcontributions' ) );
191 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
193 $links = implode( ' | ', $tools );
196 // Old message 'contribsub' had one parameter, but that doesn't work for
197 // languages that want to put the "for" bit right after $user but before
198 // $links. If 'contribsub' is around, use it for reverse compatibility,
199 // otherwise use 'contribsub2'.
200 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
201 return wfMsgHtml( 'contribsub2', $user, $links );
203 return wfMsgHtml( 'contribsub', "$user ($links)" );
208 * Generates the namespace selector form with hidden attributes.
209 * @param $this->opts Array: the options to be included.
211 protected function getForm() {
212 global $wgScript, $wgTitle;
214 $this->opts
['title'] = $wgTitle->getPrefixedText();
215 if( !isset( $this->opts
['target'] ) ) {
216 $this->opts
['target'] = '';
218 $this->opts
['target'] = str_replace( '_' , ' ' , $this->opts
['target'] );
221 if( !isset( $this->opts
['namespace'] ) ) {
222 $this->opts
['namespace'] = '';
225 if( !isset( $this->opts
['contribs'] ) ) {
226 $this->opts
['contribs'] = 'user';
229 if( !isset( $this->opts
['year'] ) ) {
230 $this->opts
['year'] = '';
233 if( !isset( $this->opts
['month'] ) ) {
234 $this->opts
['month'] = '';
237 if( $this->opts
['contribs'] == 'newbie' ) {
238 $this->opts
['target'] = '';
241 if( !isset( $this->opts
['tagfilter'] ) ) {
242 $this->opts
['tagfilter'] = '';
245 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
247 foreach ( $this->opts
as $name => $value ) {
248 if( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
251 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
254 $tagFilter = ChangeTags
::buildTagFilterSelector( $this->opts
['tagfilter'] );
257 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
258 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ),
259 'contribs', 'newbie' , 'newbie', $this->opts
['contribs'] == 'newbie' ?
true : false ) . '<br />' .
260 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ),
261 'contribs' , 'user', 'user', $this->opts
['contribs'] == 'user' ?
true : false ) . ' ' .
262 Xml
::input( 'target', 20, $this->opts
['target']) . ' '.
263 '<span style="white-space: nowrap">' .
264 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
265 Xml
::namespaceSelector( $this->opts
['namespace'], '' ) .
267 ( $tagFilter ? Xml
::tags( 'p', null, implode( ' ', $tagFilter ) ) : '' ) .
268 Xml
::openElement( 'p' ) .
269 '<span style="white-space: nowrap">' .
270 Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
271 Xml
::input( 'year', 4, $this->opts
['year'], array('id' => 'year', 'maxlength' => 4) ) .
274 '<span style="white-space: nowrap">' .
275 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
276 Xml
::monthSelector( $this->opts
['month'], -1 ) . ' '.
278 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
279 Xml
::closeElement( 'p' );
281 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
282 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
283 $f .= "<p>{$explain}</p>";
285 $f .= '</fieldset>' .
286 Xml
::closeElement( 'form' );
291 * Output a subscription feed listing recent edits to this page.
292 * @param string $type
294 protected function feed( $type ) {
295 global $wgRequest, $wgFeed, $wgFeedClasses, $wgFeedLimit;
299 $wgOut->addWikiMsg( 'feed-unavailable' );
303 if( !isset( $wgFeedClasses[$type] ) ) {
305 $wgOut->addWikiMsg( 'feed-invalid' );
309 $feed = new $wgFeedClasses[$type](
311 wfMsgExt( 'tagline', 'parsemag' ),
312 $this->getTitle()->getFullUrl() );
314 // Already valid title
315 $nt = Title
::makeTitleSafe( NS_USER
, $this->opts
['target'] );
316 $target = $this->opts
['target'] == 'newbies' ?
'newbies' : $nt->getText();
318 $pager = new ContribsPager( $target, $this->opts
['namespace'],
319 $this->opts
['year'], $this->opts
['month'], $this->opts
['tagfilter'] );
321 $pager->mLimit
= min( $this->opts
['limit'], $wgFeedLimit );
324 if( $pager->getNumRows() > 0 ) {
325 while( $row = $pager->mResult
->fetchObject() ) {
326 $feed->outItem( $this->feedItem( $row ) );
332 protected function feedTitle() {
333 global $wgContLanguageCode, $wgSitename;
334 $page = SpecialPage
::getPage( 'Contributions' );
335 $desc = $page->getDescription();
336 return "$wgSitename - $desc [$wgContLanguageCode]";
339 protected function feedItem( $row ) {
340 $title = Title
::MakeTitle( intval( $row->page_namespace
), $row->page_title
);
342 $date = $row->rev_timestamp
;
343 $comments = $title->getTalkPage()->getFullURL();
344 $revision = Revision
::newFromTitle( $title, $row->rev_id
);
347 $title->getPrefixedText(),
348 $this->feedItemDesc( $revision ),
349 $title->getFullURL(),
351 $this->feedItemAuthor( $revision ),
359 protected function feedItemAuthor( $revision ) {
360 return $revision->getUserText();
363 protected function feedItemDesc( $revision ) {
365 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
366 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
367 "</p>\n<hr />\n<div>" .
368 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
375 * Pager for Special:Contributions
376 * @ingroup SpecialPage Pager
378 class ContribsPager
extends ReverseChronologicalPager
{
379 public $mDefaultDirection = true;
380 var $messages, $target;
381 var $namespace = '', $mDb;
383 function __construct( $target, $namespace = false, $year = false, $month = false, $tagFilter = false ) {
384 parent
::__construct();
385 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter minoreditletter' ) as $msg ) {
386 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escape') );
388 $this->target
= $target;
389 $this->namespace = $namespace;
390 $this->tagFilter
= $tagFilter;
392 $this->getDateCond( $year, $month );
394 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
397 function getDefaultQuery() {
398 $query = parent
::getDefaultQuery();
399 $query['target'] = $this->target
;
403 function getQueryInfo() {
404 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
406 $conds = array_merge( $userCond, $this->getNamespaceCond() );
407 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
412 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
413 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
414 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted'
417 'options' => array( 'USE INDEX' => array('revision' => $index) ),
418 'join_conds' => $join_cond
421 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'], $queryInfo['fields'], $queryInfo['conds'], $queryInfo['join_conds'], $this->tagFilter
);
423 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
427 function getUserCond() {
428 $condition = array();
429 $join_conds = array();
430 if( $this->target
== 'newbies' ) {
431 $tables = array( 'user_groups', 'page', 'revision' );
432 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
433 $condition[] = 'rev_user >' . (int)($max - $max / 100);
434 $condition[] = 'ug_group IS NULL';
435 $index = 'user_timestamp';
436 # FIXME: other groups may have 'bot' rights
437 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
439 $tables = array( 'page', 'revision' );
440 $condition['rev_user_text'] = $this->target
;
441 $index = 'usertext_timestamp';
443 return array( $tables, $index, $condition, $join_conds );
446 function getNamespaceCond() {
447 if( $this->namespace !== '' ) {
448 return array( 'page_namespace' => (int)$this->namespace );
454 function getIndexField() {
455 return 'rev_timestamp';
458 function getStartBody() {
462 function getEndBody() {
467 * Generates each row in the contributions list.
469 * Contributions which are marked "top" are currently on top of the history.
470 * For these contributions, a [rollback] link is shown for users with roll-
471 * back privileges. The rollback link restores the most recent version that
472 * was not written by the target user.
474 * @todo This would probably look a lot nicer in a table.
476 function formatRow( $row ) {
477 global $wgLang, $wgUser, $wgContLang;
478 wfProfileIn( __METHOD__
);
480 $sk = $this->getSkin();
481 $rev = new Revision( $row );
484 $page = Title
::newFromRow( $row );
485 $page->resetArticleId( $row->rev_page
); // use process cache
486 $link = $sk->makeLinkObj( $page, $page->getPrefixedText(), $page->isRedirect() ?
'redirect=no' : '' );
487 # Mark current revisions
488 $difftext = $topmarktext = '';
489 if( $row->rev_id
== $row->page_latest
) {
490 $topmarktext .= '<strong>' . $this->messages
['uctop'] . '</strong>';
491 if( !$row->page_is_new
) {
492 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=0' ) . ')';
494 if( $page->quickUserCan( 'rollback') && $page->quickUserCan( 'edit' ) ) {
495 $topmarktext .= ' '.$sk->generateRollback( $rev );
498 $difftext .= $this->messages
['newarticle'];
501 # Is there a visible previous revision?
502 if( $rev->userCan(Revision
::DELETED_TEXT
) ) {
503 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'],
504 'diff=prev&oldid='.$row->rev_id
) . ')';
506 $difftext = '(' . $this->messages
['diff'] . ')';
508 $histlink = '('.$sk->makeKnownLinkObj( $page, $this->messages
['hist'], 'action=history' ) . ')';
510 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
511 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
512 $d = $sk->makeKnownLinkObj( $page, $date, 'oldid='.intval($row->rev_id
) );
514 if( $this->target
== 'newbies' ) {
515 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
516 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
521 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
522 $d = '<span class="history-deleted">' . $d . '</span>';
525 if( $rev->getParentId() === 0 ) {
526 $nflag = '<span class="newpage">' . $this->messages
['newpageletter'] . '</span>';
531 if( $rev->isMinor() ) {
532 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
537 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
538 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
539 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
543 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'contributions' );
544 $classes = array_merge( $classes, $newClasses );
545 $ret .= " $tagSummary";
547 // Let extensions add data
548 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
550 $classes = implode( ' ', $classes );
551 $ret = "<li class=\"$classes\">$ret</li>\n";
552 wfProfileOut( __METHOD__
);
557 * Get the Database object in use
561 public function getDatabase() {