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 // Allows reverts to have the bot flag in recent changes. It is just here to
68 // be passed in the form at the top of the page
69 if( $wgUser->isAllowed( 'markbotedits' ) && $wgRequest->getBool( 'bot' ) ) {
70 $this->opts
['bot'] = '1';
73 $skip = $wgRequest->getText( 'offset' ) ||
$wgRequest->getText( 'dir' ) == 'prev';
74 # Offset overrides year/month selection
75 if( ( $month = $wgRequest->getIntOrNull( 'month' ) ) !== null && $month !== -1 ) {
76 $this->opts
['month'] = intval( $month );
78 $this->opts
['month'] = '';
80 if( ( $year = $wgRequest->getIntOrNull( 'year' ) ) !== null ) {
81 $this->opts
['year'] = intval( $year );
82 } else if( $this->opts
['month'] ) {
83 $thisMonth = intval( gmdate( 'n' ) );
84 $thisYear = intval( gmdate( 'Y' ) );
85 if( intval( $this->opts
['month'] ) > $thisMonth ) {
88 $this->opts
['year'] = $thisYear;
90 $this->opts
['year'] = '';
94 $this->opts
['year'] = '';
95 $this->opts
['month'] = '';
99 $this->setSyndicated();
100 $feedType = $wgRequest->getVal( 'feed' );
102 return $this->feed( $feedType );
105 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
107 $wgOut->addHTML( $this->getForm( $this->opts
) );
109 $pager = new ContribsPager( $target, $this->opts
['namespace'], $this->opts
['year'], $this->opts
['month'] );
110 if( !$pager->getNumRows() ) {
111 $wgOut->addWikiMsg( 'nocontribs' );
115 # Show a message about slave lag, if applicable
116 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
117 $wgOut->showLagWarning( $lag );
120 '<p>' . $pager->getNavigationBar() . '</p>' .
122 '<p>' . $pager->getNavigationBar() . '</p>'
125 # If there were contributions, and it was a valid user or IP, show
126 # the appropriate "footer" message - WHOIS tools, etc.
127 if( $target != 'newbies' ) {
128 $message = IP
::isIPAddress( $target ) ?
129 'sp-contributions-footer-anon' : 'sp-contributions-footer';
131 $text = wfMsgNoTrans( $message, $target );
132 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
133 $wgOut->addHTML( '<div class="mw-contributions-footer">' );
134 $wgOut->addWikiText( $text );
135 $wgOut->addHTML( '</div>' );
140 protected function setSyndicated() {
142 $queryParams = array(
143 'namespace' => $this->opts
['namespace'],
144 'target' => $this->opts
['target']
146 $wgOut->setSyndicated( true );
147 $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
151 * Generates the subheading with links
152 * @param Title $nt Title object for the target
153 * @param integer $id User ID for the target
154 * @return String: appropriately-escaped HTML to be output literally
156 protected function contributionsSub( $nt, $id ) {
157 global $wgSysopUserBans, $wgLang, $wgUser;
159 $sk = $wgUser->getSkin();
162 $user = $nt->getText();
164 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
166 $talk = $nt->getTalkPage();
169 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
170 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && IP
::isIPAddress( $nt->getText() ) ) ) {
172 if( $wgUser->isAllowed( 'block' ) )
173 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip',
174 $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
176 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ),
177 wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
180 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ),
181 'user=' . $nt->getPartialUrl() );
183 # Add link to deleted user contributions for priviledged users
184 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
185 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'DeletedContributions',
186 $nt->getDBkey() ), wfMsgHtml( 'deletedcontributions' ) );
189 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
191 $links = implode( ' | ', $tools );
194 // Old message 'contribsub' had one parameter, but that doesn't work for
195 // languages that want to put the "for" bit right after $user but before
196 // $links. If 'contribsub' is around, use it for reverse compatibility,
197 // otherwise use 'contribsub2'.
198 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
199 return wfMsgHtml( 'contribsub2', $user, $links );
201 return wfMsgHtml( 'contribsub', "$user ($links)" );
206 * Generates the namespace selector form with hidden attributes.
207 * @param $this->opts Array: the options to be included.
209 protected function getForm() {
210 global $wgScript, $wgTitle;
212 $this->opts
['title'] = $wgTitle->getPrefixedText();
213 if( !isset( $this->opts
['target'] ) ) {
214 $this->opts
['target'] = '';
216 $this->opts
['target'] = str_replace( '_' , ' ' , $this->opts
['target'] );
219 if( !isset( $this->opts
['namespace'] ) ) {
220 $this->opts
['namespace'] = '';
223 if( !isset( $this->opts
['contribs'] ) ) {
224 $this->opts
['contribs'] = 'user';
227 if( !isset( $this->opts
['year'] ) ) {
228 $this->opts
['year'] = '';
231 if( !isset( $this->opts
['month'] ) ) {
232 $this->opts
['month'] = '';
235 if( $this->opts
['contribs'] == 'newbie' ) {
236 $this->opts
['target'] = '';
239 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
241 foreach ( $this->opts
as $name => $value ) {
242 if( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
245 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
249 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
250 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ),
251 'contribs', 'newbie' , 'newbie', $this->opts
['contribs'] == 'newbie' ?
true : false ) . '<br />' .
252 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ),
253 'contribs' , 'user', 'user', $this->opts
['contribs'] == 'user' ?
true : false ) . ' ' .
254 Xml
::input( 'target', 20, $this->opts
['target']) . ' '.
255 '<span style="white-space: nowrap">' .
256 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
257 Xml
::namespaceSelector( $this->opts
['namespace'], '' ) .
259 Xml
::openElement( 'p' ) .
260 '<span style="white-space: nowrap">' .
261 Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
262 Xml
::input( 'year', 4, $this->opts
['year'], array('id' => 'year', 'maxlength' => 4) ) .
265 '<span style="white-space: nowrap">' .
266 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
267 Xml
::monthSelector( $this->opts
['month'], -1 ) . ' '.
269 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
270 Xml
::closeElement( 'p' );
272 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
273 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
274 $f .= "<p>{$explain}</p>";
276 $f .= '</fieldset>' .
277 Xml
::closeElement( 'form' );
282 * Output a subscription feed listing recent edits to this page.
283 * @param string $type
285 protected function feed( $type ) {
286 global $wgRequest, $wgFeed, $wgFeedClasses, $wgFeedLimit;
290 $wgOut->addWikiMsg( 'feed-unavailable' );
294 if( !isset( $wgFeedClasses[$type] ) ) {
296 $wgOut->addWikiMsg( 'feed-invalid' );
300 $feed = new $wgFeedClasses[$type](
303 $this->getTitle()->getFullUrl() );
305 // Already valid title
306 $nt = Title
::makeTitleSafe( NS_USER
, $this->opts
['target'] );
307 $target = $this->opts
['target'] == 'newbies' ?
'newbies' : $nt->getText();
309 $pager = new ContribsPager( $target, $this->opts
['namespace'],
310 $this->opts
['year'], $this->opts
['month'] );
312 $pager->mLimit
= min( $this->opts
['limit'], $wgFeedLimit );
315 if( $pager->getNumRows() > 0 ) {
316 while( $row = $pager->mResult
->fetchObject() ) {
317 $feed->outItem( $this->feedItem( $row ) );
323 protected function feedTitle() {
324 global $wgContLanguageCode, $wgSitename;
325 $page = SpecialPage
::getPage( 'Contributions' );
326 $desc = $page->getDescription();
327 return "$wgSitename - $desc [$wgContLanguageCode]";
330 protected function feedItem( $row ) {
331 $title = Title
::MakeTitle( intval( $row->page_namespace
), $row->page_title
);
333 $date = $row->rev_timestamp
;
334 $comments = $title->getTalkPage()->getFullURL();
335 $revision = Revision
::newFromTitle( $title, $row->rev_id
);
338 $title->getPrefixedText(),
339 $this->feedItemDesc( $revision ),
340 $title->getFullURL(),
342 $this->feedItemAuthor( $revision ),
351 * Quickie hack... strip out wikilinks to more legible form from the comment.
353 protected function stripComment( $text ) {
354 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
357 protected function feedItemAuthor( $revision ) {
358 return $revision->getUserText();
361 protected function feedItemDesc( $revision ) {
363 return '<p>' . htmlspecialchars( $revision->getUserText() ) . ': ' .
364 htmlspecialchars( $this->stripComment( $revision->getComment() ) ) .
365 "</p>\n<hr />\n<div>" .
366 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
373 * Pager for Special:Contributions
374 * @ingroup SpecialPage Pager
376 class ContribsPager
extends ReverseChronologicalPager
{
377 public $mDefaultDirection = true;
378 var $messages, $target;
379 var $namespace = '', $mDb;
381 function __construct( $target, $namespace = false, $year = false, $month = false ) {
382 parent
::__construct();
383 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter minoreditletter' ) as $msg ) {
384 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escape') );
386 $this->target
= $target;
387 $this->namespace = $namespace;
389 $this->getDateCond( $year, $month );
391 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
394 function getDefaultQuery() {
395 $query = parent
::getDefaultQuery();
396 $query['target'] = $this->target
;
400 function getQueryInfo() {
401 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
402 $conds = array_merge( array('page_id=rev_page'), $userCond, $this->getNamespaceCond() );
406 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page',
407 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user',
408 'rev_user_text', 'rev_parent_id', 'rev_deleted'
411 'options' => array( 'USE INDEX' => array('revision' => $index) ),
412 'join_conds' => $join_cond
414 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
418 function getUserCond() {
419 $condition = array();
420 $join_conds = array();
421 if( $this->target
== 'newbies' ) {
422 $tables = array( 'user_groups', 'page', 'revision' );
423 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
424 $condition[] = 'rev_user >' . (int)($max - $max / 100);
425 $condition[] = 'ug_group IS NULL';
426 $index = 'user_timestamp';
427 # FIXME: other groups may have 'bot' rights
428 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
430 $tables = array( 'page', 'revision' );
431 $condition['rev_user_text'] = $this->target
;
432 $index = 'usertext_timestamp';
434 return array( $tables, $index, $condition, $join_conds );
437 function getNamespaceCond() {
438 if( $this->namespace !== '' ) {
439 return array( 'page_namespace' => (int)$this->namespace );
445 function getIndexField() {
446 return 'rev_timestamp';
449 function getStartBody() {
453 function getEndBody() {
458 * Generates each row in the contributions list.
460 * Contributions which are marked "top" are currently on top of the history.
461 * For these contributions, a [rollback] link is shown for users with roll-
462 * back privileges. The rollback link restores the most recent version that
463 * was not written by the target user.
465 * @todo This would probably look a lot nicer in a table.
467 function formatRow( $row ) {
468 wfProfileIn( __METHOD__
);
470 global $wgLang, $wgUser, $wgContLang;
472 $sk = $this->getSkin();
473 $rev = new Revision( $row );
475 $page = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
476 $link = $sk->makeKnownLinkObj( $page );
477 $difftext = $topmarktext = '';
478 if( $row->rev_id
== $row->page_latest
) {
479 $topmarktext .= '<strong>' . $this->messages
['uctop'] . '</strong>';
480 if( !$row->page_is_new
) {
481 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=0' ) . ')';
483 $difftext .= $this->messages
['newarticle'];
486 if( !$page->getUserPermissionsErrors( 'rollback', $wgUser )
487 && !$page->getUserPermissionsErrors( 'edit', $wgUser ) ) {
488 $topmarktext .= ' '.$sk->generateRollback( $rev );
492 # Is there a visible previous revision?
493 if( $rev->userCan(Revision
::DELETED_TEXT
) ) {
494 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=prev&oldid='.$row->rev_id
) . ')';
496 $difftext = '(' . $this->messages
['diff'] . ')';
498 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages
['hist'], 'action=history' ) . ')';
500 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
501 $d = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
503 if( $this->target
== 'newbies' ) {
504 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
505 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
510 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
511 $d = '<span class="history-deleted">' . $d . '</span>';
514 if( $rev->getParentId() === 0 ) {
515 $nflag = '<span class="newpage">' . $this->messages
['newpageletter'] . '</span>';
520 if( $row->rev_minor_edit
) {
521 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
526 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
527 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
528 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
530 // Let extensions add data
531 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
533 $ret = "<li>$ret</li>\n";
534 wfProfileOut( __METHOD__
);
539 * Get the Database object in use
543 public function getDatabase() {