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', wfMsg( 'contributions-title', $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'] = $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( $this->opts
) );
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 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
243 foreach ( $this->opts
as $name => $value ) {
244 if( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
247 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
251 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
252 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ),
253 'contribs', 'newbie' , 'newbie', $this->opts
['contribs'] == 'newbie' ?
true : false ) . '<br />' .
254 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ),
255 'contribs' , 'user', 'user', $this->opts
['contribs'] == 'user' ?
true : false ) . ' ' .
256 Xml
::input( 'target', 20, $this->opts
['target']) . ' '.
257 '<span style="white-space: nowrap">' .
258 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
259 Xml
::namespaceSelector( $this->opts
['namespace'], '' ) .
261 Xml
::tags( 'p', null, implode( ' ', ChangeTags
::buildTagFilterSelector( $this->opts
['tagfilter'] ) ) ) .
262 Xml
::openElement( 'p' ) .
263 '<span style="white-space: nowrap">' .
264 Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
265 Xml
::input( 'year', 4, $this->opts
['year'], array('id' => 'year', 'maxlength' => 4) ) .
268 '<span style="white-space: nowrap">' .
269 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
270 Xml
::monthSelector( $this->opts
['month'], -1 ) . ' '.
272 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
273 Xml
::closeElement( 'p' );
275 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
276 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
277 $f .= "<p>{$explain}</p>";
279 $f .= '</fieldset>' .
280 Xml
::closeElement( 'form' );
285 * Output a subscription feed listing recent edits to this page.
286 * @param string $type
288 protected function feed( $type ) {
289 global $wgRequest, $wgFeed, $wgFeedClasses, $wgFeedLimit;
293 $wgOut->addWikiMsg( 'feed-unavailable' );
297 if( !isset( $wgFeedClasses[$type] ) ) {
299 $wgOut->addWikiMsg( 'feed-invalid' );
303 $feed = new $wgFeedClasses[$type](
305 wfMsgExt( 'tagline', 'parsemag' ),
306 $this->getTitle()->getFullUrl() );
308 // Already valid title
309 $nt = Title
::makeTitleSafe( NS_USER
, $this->opts
['target'] );
310 $target = $this->opts
['target'] == 'newbies' ?
'newbies' : $nt->getText();
312 $pager = new ContribsPager( $target, $this->opts
['namespace'],
313 $this->opts
['year'], $this->opts
['month'], $this->opts
['tagfilter'] );
315 $pager->mLimit
= min( $this->opts
['limit'], $wgFeedLimit );
318 if( $pager->getNumRows() > 0 ) {
319 while( $row = $pager->mResult
->fetchObject() ) {
320 $feed->outItem( $this->feedItem( $row ) );
326 protected function feedTitle() {
327 global $wgContLanguageCode, $wgSitename;
328 $page = SpecialPage
::getPage( 'Contributions' );
329 $desc = $page->getDescription();
330 return "$wgSitename - $desc [$wgContLanguageCode]";
333 protected function feedItem( $row ) {
334 $title = Title
::MakeTitle( intval( $row->page_namespace
), $row->page_title
);
336 $date = $row->rev_timestamp
;
337 $comments = $title->getTalkPage()->getFullURL();
338 $revision = Revision
::newFromTitle( $title, $row->rev_id
);
341 $title->getPrefixedText(),
342 $this->feedItemDesc( $revision ),
343 $title->getFullURL(),
345 $this->feedItemAuthor( $revision ),
353 protected function feedItemAuthor( $revision ) {
354 return $revision->getUserText();
357 protected function feedItemDesc( $revision ) {
359 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
360 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
361 "</p>\n<hr />\n<div>" .
362 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
369 * Pager for Special:Contributions
370 * @ingroup SpecialPage Pager
372 class ContribsPager
extends ReverseChronologicalPager
{
373 public $mDefaultDirection = true;
374 var $messages, $target;
375 var $namespace = '', $mDb;
377 function __construct( $target, $namespace = false, $year = false, $month = false, $tagFilter = false ) {
378 parent
::__construct();
379 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter minoreditletter' ) as $msg ) {
380 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escape') );
382 $this->target
= $target;
383 $this->namespace = $namespace;
384 $this->tagFilter
= $tagFilter;
386 $this->getDateCond( $year, $month );
388 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
391 function getDefaultQuery() {
392 $query = parent
::getDefaultQuery();
393 $query['target'] = $this->target
;
397 function getQueryInfo() {
398 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
400 $conds = array_merge( $userCond, $this->getNamespaceCond() );
401 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
406 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
407 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
408 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted'
411 'options' => array( 'USE INDEX' => array('revision' => $index) ),
412 'join_conds' => $join_cond
415 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'], $queryInfo['fields'], $queryInfo['conds'], $queryInfo['join_conds'], $this->tagFilter
);
417 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
421 function getUserCond() {
422 $condition = array();
423 $join_conds = array();
424 if( $this->target
== 'newbies' ) {
425 $tables = array( 'user_groups', 'page', 'revision' );
426 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
427 $condition[] = 'rev_user >' . (int)($max - $max / 100);
428 $condition[] = 'ug_group IS NULL';
429 $index = 'user_timestamp';
430 # FIXME: other groups may have 'bot' rights
431 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
433 $tables = array( 'page', 'revision' );
434 $condition['rev_user_text'] = $this->target
;
435 $index = 'usertext_timestamp';
437 return array( $tables, $index, $condition, $join_conds );
440 function getNamespaceCond() {
441 if( $this->namespace !== '' ) {
442 return array( 'page_namespace' => (int)$this->namespace );
448 function getIndexField() {
449 return 'rev_timestamp';
452 function getStartBody() {
456 function getEndBody() {
461 * Generates each row in the contributions list.
463 * Contributions which are marked "top" are currently on top of the history.
464 * For these contributions, a [rollback] link is shown for users with roll-
465 * back privileges. The rollback link restores the most recent version that
466 * was not written by the target user.
468 * @todo This would probably look a lot nicer in a table.
470 function formatRow( $row ) {
471 global $wgLang, $wgUser, $wgContLang;
472 wfProfileIn( __METHOD__
);
474 $sk = $this->getSkin();
475 $rev = new Revision( $row );
478 $page = Title
::newFromRow( $row );
479 $page->resetArticleId( $row->rev_page
); // use process cache
480 $link = $sk->makeLinkObj( $page, $page->getPrefixedText(), $page->isRedirect() ?
'redirect=no' : '' );
481 # Mark current revisions
482 $difftext = $topmarktext = '';
483 if( $row->rev_id
== $row->page_latest
) {
484 $topmarktext .= '<strong>' . $this->messages
['uctop'] . '</strong>';
485 if( !$row->page_is_new
) {
486 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=0' ) . ')';
488 if( $page->userCan( 'rollback') && $page->userCan( 'edit' ) ) {
489 $topmarktext .= ' '.$sk->generateRollback( $rev );
492 $difftext .= $this->messages
['newarticle'];
495 # Is there a visible previous revision?
496 if( $rev->userCan(Revision
::DELETED_TEXT
) ) {
497 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'],
498 'diff=prev&oldid='.$row->rev_id
) . ')';
500 $difftext = '(' . $this->messages
['diff'] . ')';
502 $histlink = '('.$sk->makeKnownLinkObj( $page, $this->messages
['hist'], 'action=history' ) . ')';
504 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
505 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
506 $d = $sk->makeKnownLinkObj( $page, $date, 'oldid='.intval($row->rev_id
) );
508 if( $this->target
== 'newbies' ) {
509 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
510 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
515 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
516 $d = '<span class="history-deleted">' . $d . '</span>';
519 if( $rev->getParentId() === 0 ) {
520 $nflag = '<span class="newpage">' . $this->messages
['newpageletter'] . '</span>';
525 if( $rev->isMinor() ) {
526 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
531 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
532 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
533 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
537 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'contributions' );
538 $classes = array_merge( $classes, $newClasses );
539 $ret .= " $tagSummary";
541 // Let extensions add data
542 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
544 $classes = implode( ' ', $classes );
545 $ret = "<li class=\"$classes\">$ret</li>\n";
546 wfProfileOut( __METHOD__
);
551 * Get the Database object in use
555 public function getDatabase() {