3 * Special:Contributions, show user contributions in a paged list
8 class SpecialContributions
extends SpecialPage
{
10 public function __construct() {
11 parent
::__construct( 'Contributions' );
14 function execute( $par ) {
15 global $wgUser, $wgOut, $wgLang, $wgRequest;
18 $this->outputHeader();
22 if ( isset( $par ) && $par == 'newbies' ) {
24 $options['contribs'] = 'newbie';
25 } elseif ( isset( $par ) ) {
28 $target = $wgRequest->getVal( 'target' );
32 if ( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
34 $options['contribs'] = 'newbie';
37 if ( !strlen( $target ) ) {
38 $wgOut->addHTML( $this->getForm( '' ) );
42 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
43 $options['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 $options['namespace'] = intval( $ns );
64 $options['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 $options['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 $options['month'] = intval( $month );
78 $options['month'] = '';
80 if ( ( $year = $wgRequest->getIntOrNull( 'year' ) ) !== null ) {
81 $options['year'] = intval( $year );
82 } else if( $options['month'] ) {
83 $thisMonth = intval( gmdate( 'n' ) );
84 $thisYear = intval( gmdate( 'Y' ) );
85 if( intval( $options['month'] ) > $thisMonth ) {
88 $options['year'] = $thisYear;
90 $options['year'] = '';
93 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
96 $options['year'] = '';
97 $options['month'] = '';
100 $wgOut->addHTML( $this->getForm( $options ) );
102 $pager = new ContribsPager( $target, $options['namespace'], $options['year'], $options['month'] );
103 if ( !$pager->getNumRows() ) {
104 $wgOut->addWikiMsg( 'nocontribs' );
108 # Show a message about slave lag, if applicable
109 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
110 $wgOut->showLagWarning( $lag );
113 '<p>' . $pager->getNavigationBar() . '</p>' .
115 '<p>' . $pager->getNavigationBar() . '</p>' );
117 # If there were contributions, and it was a valid user or IP, show
118 # the appropriate "footer" message - WHOIS tools, etc.
119 if( $target != 'newbies' ) {
120 $message = IP
::isIPAddress( $target )
121 ?
'sp-contributions-footer-anon'
122 : 'sp-contributions-footer';
124 $text = wfMsgNoTrans( $message, $target );
125 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
126 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
127 $wgOut->addWikiText( $text );
128 $wgOut->addHtml( '</div>' );
134 * Generates the subheading with links
135 * @param Title $nt Title object for the target
136 * @param integer $id User ID for the target
137 * @return String: appropriately-escaped HTML to be output literally
139 protected function contributionsSub( $nt, $id ) {
140 global $wgSysopUserBans, $wgLang, $wgUser;
142 $sk = $wgUser->getSkin();
145 $user = $nt->getText();
147 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
149 $talk = $nt->getTalkPage();
152 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
153 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && IP
::isIPAddress( $nt->getText() ) ) ) {
155 if( $wgUser->isAllowed( 'block' ) )
156 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip',
157 $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
159 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ),
160 wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
163 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ),
164 'user=' . $nt->getPartialUrl() );
166 # Add link to deleted user contributions for priviledged users
167 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
168 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'DeletedContributions',
169 $nt->getDBkey() ), wfMsgHtml( 'deletedcontributions' ) );
172 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
174 $links = implode( ' | ', $tools );
177 // Old message 'contribsub' had one parameter, but that doesn't work for
178 // languages that want to put the "for" bit right after $user but before
179 // $links. If 'contribsub' is around, use it for reverse compatibility,
180 // otherwise use 'contribsub2'.
181 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
182 return wfMsgHtml( 'contribsub2', $user, $links );
184 return wfMsgHtml( 'contribsub', "$user ($links)" );
189 * Generates the namespace selector form with hidden attributes.
190 * @param $options Array: the options to be included.
192 protected function getForm( $options ) {
193 global $wgScript, $wgTitle, $wgRequest;
195 $options['title'] = $wgTitle->getPrefixedText();
196 if ( !isset( $options['target'] ) ) {
197 $options['target'] = '';
199 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
202 if ( !isset( $options['namespace'] ) ) {
203 $options['namespace'] = '';
206 if ( !isset( $options['contribs'] ) ) {
207 $options['contribs'] = 'user';
210 if ( !isset( $options['year'] ) ) {
211 $options['year'] = '';
214 if ( !isset( $options['month'] ) ) {
215 $options['month'] = '';
218 if ( $options['contribs'] == 'newbie' ) {
219 $options['target'] = '';
222 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
224 foreach ( $options as $name => $value ) {
225 if ( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
228 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
232 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
233 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ),
234 'contribs', 'newbie' , 'newbie', $options['contribs'] == 'newbie' ?
true : false ) . '<br />' .
235 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ),
236 'contribs' , 'user', 'user', $options['contribs'] == 'user' ?
true : false ) . ' ' .
237 Xml
::input( 'target', 20, $options['target']) . ' '.
238 '<span style="white-space: nowrap">' .
239 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
240 Xml
::namespaceSelector( $options['namespace'], '' ) .
242 Xml
::openElement( 'p' ) .
243 '<span style="white-space: nowrap">' .
244 Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
245 Xml
::input( 'year', 4, $options['year'], array('id' => 'year', 'maxlength' => 4) ) .
248 '<span style="white-space: nowrap">' .
249 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
250 Xml
::monthSelector( $options['month'], -1 ) . ' '.
252 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
253 Xml
::closeElement( 'p' );
255 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
256 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
257 $f .= "<p>{$explain}</p>";
259 $f .= '</fieldset>' .
260 Xml
::closeElement( 'form' );
266 * Pager for Special:Contributions
267 * @ingroup SpecialPage Pager
269 class ContribsPager
extends ReverseChronologicalPager
{
270 public $mDefaultDirection = true;
271 var $messages, $target;
272 var $namespace = '', $mDb;
274 function __construct( $target, $namespace = false, $year = false, $month = false ) {
275 parent
::__construct();
276 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter minoreditletter' ) as $msg ) {
277 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escape') );
279 $this->target
= $target;
280 $this->namespace = $namespace;
282 $this->getDateCond( $year, $month );
284 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
287 function getDefaultQuery() {
288 $query = parent
::getDefaultQuery();
289 $query['target'] = $this->target
;
293 function getQueryInfo() {
294 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
295 $conds = array_merge( array('page_id=rev_page'), $userCond, $this->getNamespaceCond() );
299 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page',
300 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user',
301 'rev_user_text', 'rev_parent_id', 'rev_deleted'
304 'options' => array( 'USE INDEX' => array('revision' => $index) ),
305 'join_conds' => $join_cond
307 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
311 function getUserCond() {
312 $condition = array();
313 $join_conds = array();
314 if ( $this->target
== 'newbies' ) {
315 $tables = array( 'user_groups', 'page', 'revision' );
316 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
317 $condition[] = 'rev_user >' . (int)($max - $max / 100);
318 $condition[] = 'ug_group IS NULL';
319 $index = 'user_timestamp';
320 # FIXME: other groups may have 'bot' rights
321 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
323 $tables = array( 'page', 'revision' );
324 $condition['rev_user_text'] = $this->target
;
325 $index = 'usertext_timestamp';
327 return array( $tables, $index, $condition, $join_conds );
330 function getNamespaceCond() {
331 if ( $this->namespace !== '' ) {
332 return array( 'page_namespace' => (int)$this->namespace );
338 function getIndexField() {
339 return 'rev_timestamp';
342 function getStartBody() {
346 function getEndBody() {
351 * Generates each row in the contributions list.
353 * Contributions which are marked "top" are currently on top of the history.
354 * For these contributions, a [rollback] link is shown for users with roll-
355 * back privileges. The rollback link restores the most recent version that
356 * was not written by the target user.
358 * @todo This would probably look a lot nicer in a table.
360 function formatRow( $row ) {
361 wfProfileIn( __METHOD__
);
363 global $wgLang, $wgUser, $wgContLang;
365 $sk = $this->getSkin();
366 $rev = new Revision( $row );
368 $page = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
369 $link = $sk->makeKnownLinkObj( $page );
370 $difftext = $topmarktext = '';
371 if( $row->rev_id
== $row->page_latest
) {
372 $topmarktext .= '<strong>' . $this->messages
['uctop'] . '</strong>';
373 if( !$row->page_is_new
) {
374 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=0' ) . ')';
376 $difftext .= $this->messages
['newarticle'];
379 if( !$page->getUserPermissionsErrors( 'rollback', $wgUser )
380 && !$page->getUserPermissionsErrors( 'edit', $wgUser ) ) {
381 $topmarktext .= ' '.$sk->generateRollback( $rev );
385 # Is there a visible previous revision?
386 if( $rev->userCan(Revision
::DELETED_TEXT
) ) {
387 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=prev&oldid='.$row->rev_id
) . ')';
389 $difftext = '(' . $this->messages
['diff'] . ')';
391 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages
['hist'], 'action=history' ) . ')';
393 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
394 $d = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
396 if( $this->target
== 'newbies' ) {
397 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
398 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
403 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
404 $d = '<span class="history-deleted">' . $d . '</span>';
407 if( $rev->getParentId() === 0 ) {
408 $nflag = '<span class="newpage">' . $this->messages
['newpageletter'] . '</span>';
413 if( $row->rev_minor_edit
) {
414 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
419 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
420 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
421 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
423 // Let extensions add data
424 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
426 $ret = "<li>$ret</li>\n";
427 wfProfileOut( __METHOD__
);
432 * Get the Database object in use
436 public function getDatabase() {