3 * Special:Contributions, show user contributions in a paged list
9 * Pager for Special:Contributions
10 * @ingroup SpecialPage Pager
12 class ContribsPager
extends ReverseChronologicalPager
{
13 public $mDefaultDirection = true;
14 var $messages, $target;
15 var $namespace = '', $mDb;
17 function __construct( $target, $namespace = false, $year = false, $month = false ) {
18 parent
::__construct();
19 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter minoreditletter' ) as $msg ) {
20 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escape') );
22 $this->target
= $target;
23 $this->namespace = $namespace;
25 $this->getDateCond( $year, $month );
27 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
30 function getDefaultQuery() {
31 $query = parent
::getDefaultQuery();
32 $query['target'] = $this->target
;
36 function getQueryInfo() {
37 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
38 $conds = array_merge( array('page_id=rev_page'), $userCond, $this->getNamespaceCond() );
42 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page',
43 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user',
44 'rev_user_text', 'rev_parent_id', 'rev_deleted'
47 'options' => array( 'USE INDEX' => array('revision' => $index) ),
48 'join_conds' => $join_cond
50 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
54 function getUserCond() {
56 $join_conds = array();
57 if ( $this->target
== 'newbies' ) {
58 $tables = array( 'user_groups', 'page', 'revision' );
59 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
60 $condition[] = 'rev_user >' . (int)($max - $max / 100);
61 $condition[] = 'ug_group IS NULL';
62 $index = 'user_timestamp';
63 # FIXME: other groups may have 'bot' rights
64 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
66 $tables = array( 'page', 'revision' );
67 $condition['rev_user_text'] = $this->target
;
68 $index = 'usertext_timestamp';
70 return array( $tables, $index, $condition, $join_conds );
73 function getNamespaceCond() {
74 if ( $this->namespace !== '' ) {
75 return array( 'page_namespace' => (int)$this->namespace );
81 function getIndexField() {
82 return 'rev_timestamp';
85 function getStartBody() {
89 function getEndBody() {
94 * Generates each row in the contributions list.
96 * Contributions which are marked "top" are currently on top of the history.
97 * For these contributions, a [rollback] link is shown for users with roll-
98 * back privileges. The rollback link restores the most recent version that
99 * was not written by the target user.
101 * @todo This would probably look a lot nicer in a table.
103 function formatRow( $row ) {
104 wfProfileIn( __METHOD__
);
106 global $wgLang, $wgUser, $wgContLang;
108 $sk = $this->getSkin();
109 $rev = new Revision( $row );
111 $page = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
112 $link = $sk->makeKnownLinkObj( $page );
113 $difftext = $topmarktext = '';
114 if( $row->rev_id
== $row->page_latest
) {
115 $topmarktext .= '<strong>' . $this->messages
['uctop'] . '</strong>';
116 if( !$row->page_is_new
) {
117 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=0' ) . ')';
119 $difftext .= $this->messages
['newarticle'];
122 if( !$page->getUserPermissionsErrors( 'rollback', $wgUser )
123 && !$page->getUserPermissionsErrors( 'edit', $wgUser ) ) {
124 $topmarktext .= ' '.$sk->generateRollback( $rev );
128 # Is there a visible previous revision?
129 if( $rev->userCan(Revision
::DELETED_TEXT
) ) {
130 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=prev&oldid='.$row->rev_id
) . ')';
132 $difftext = '(' . $this->messages
['diff'] . ')';
134 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages
['hist'], 'action=history' ) . ')';
136 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
137 $d = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
139 if( $this->target
== 'newbies' ) {
140 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
141 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
146 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
147 $d = '<span class="history-deleted">' . $d . '</span>';
150 if( $rev->getParentId() === 0 ) {
151 $nflag = '<span class="newpage">' . $this->messages
['newpageletter'] . '</span>';
156 if( $row->rev_minor_edit
) {
157 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
162 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
163 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
164 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
166 // Let extensions add data
167 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
169 $ret = "<li>$ret</li>\n";
170 wfProfileOut( __METHOD__
);
175 * Get the Database object in use
179 public function getDatabase() {
186 * Special page "user contributions".
187 * Shows a list of the contributions of a user.
190 * @param $par String: (optional) user name of the user for which to show the contributions
192 function wfSpecialContributions( $par = null ) {
193 global $wgUser, $wgOut, $wgLang, $wgRequest;
197 if ( isset( $par ) && $par == 'newbies' ) {
199 $options['contribs'] = 'newbie';
200 } elseif ( isset( $par ) ) {
203 $target = $wgRequest->getVal( 'target' );
206 // check for radiobox
207 if ( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
209 $options['contribs'] = 'newbie';
212 if ( !strlen( $target ) ) {
213 $wgOut->addHTML( contributionsForm( '' ) );
217 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
218 $options['target'] = $target;
220 $nt = Title
::makeTitleSafe( NS_USER
, $target );
222 $wgOut->addHTML( contributionsForm( '' ) );
225 $id = User
::idFromName( $nt->getText() );
227 if ( $target != 'newbies' ) {
228 $target = $nt->getText();
229 $wgOut->setSubtitle( contributionsSub( $nt, $id ) );
230 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'contributions-title', $target ) ) );
232 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
233 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'sp-contributions-newbies-title' ) ) );
236 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
237 $options['namespace'] = intval( $ns );
239 $options['namespace'] = '';
242 // Allows reverts to have the bot flag in recent changes. It is just here to
243 // be passed in the form at the top of the page
244 if ( $wgUser->isAllowed( 'markbotedits' ) && $wgRequest->getBool( 'bot' ) ) {
245 $options['bot'] = '1';
248 $skip = $wgRequest->getText( 'offset' ) ||
$wgRequest->getText( 'dir' ) == 'prev';
249 # Offset overrides year/month selection
250 if ( ( $month = $wgRequest->getIntOrNull( 'month' ) ) !== null && $month !== -1 ) {
251 $options['month'] = intval( $month );
253 $options['month'] = '';
255 if ( ( $year = $wgRequest->getIntOrNull( 'year' ) ) !== null ) {
256 $options['year'] = intval( $year );
257 } else if( $options['month'] ) {
258 $thisMonth = intval( gmdate( 'n' ) );
259 $thisYear = intval( gmdate( 'Y' ) );
260 if( intval( $options['month'] ) > $thisMonth ) {
263 $options['year'] = $thisYear;
265 $options['year'] = '';
268 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
271 $options['year'] = '';
272 $options['month'] = '';
275 $wgOut->addHTML( contributionsForm( $options ) );
277 $pager = new ContribsPager( $target, $options['namespace'], $options['year'], $options['month'] );
278 if ( !$pager->getNumRows() ) {
279 $wgOut->addWikiMsg( 'nocontribs' );
283 # Show a message about slave lag, if applicable
284 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
285 $wgOut->showLagWarning( $lag );
288 '<p>' . $pager->getNavigationBar() . '</p>' .
290 '<p>' . $pager->getNavigationBar() . '</p>' );
292 # If there were contributions, and it was a valid user or IP, show
293 # the appropriate "footer" message - WHOIS tools, etc.
294 if( $target != 'newbies' ) {
295 $message = IP
::isIPAddress( $target )
296 ?
'sp-contributions-footer-anon'
297 : 'sp-contributions-footer';
300 $text = wfMsgNoTrans( $message, $target );
301 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
302 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
303 $wgOut->addWikiText( $text );
304 $wgOut->addHtml( '</div>' );
310 * Generates the subheading with links
311 * @param Title $nt Title object for the target
312 * @param integer $id User ID for the target
313 * @return String: appropriately-escaped HTML to be output literally
315 function contributionsSub( $nt, $id ) {
316 global $wgSysopUserBans, $wgLang, $wgUser;
318 $sk = $wgUser->getSkin();
321 $user = $nt->getText();
323 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
325 $talk = $nt->getTalkPage();
328 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
329 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && User
::isIP( $nt->getText() ) ) ) {
331 if( $wgUser->isAllowed( 'block' ) )
332 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
334 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
337 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
339 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
341 $links = implode( ' | ', $tools );
344 // Old message 'contribsub' had one parameter, but that doesn't work for
345 // languages that want to put the "for" bit right after $user but before
346 // $links. If 'contribsub' is around, use it for reverse compatibility,
347 // otherwise use 'contribsub2'.
348 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
349 return wfMsgHtml( 'contribsub2', $user, $links );
351 return wfMsgHtml( 'contribsub', "$user ($links)" );
356 * Generates the namespace selector form with hidden attributes.
357 * @param $options Array: the options to be included.
359 function contributionsForm( $options ) {
360 global $wgScript, $wgTitle, $wgRequest;
362 $options['title'] = $wgTitle->getPrefixedText();
363 if ( !isset( $options['target'] ) ) {
364 $options['target'] = '';
366 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
369 if ( !isset( $options['namespace'] ) ) {
370 $options['namespace'] = '';
373 if ( !isset( $options['contribs'] ) ) {
374 $options['contribs'] = 'user';
377 if ( !isset( $options['year'] ) ) {
378 $options['year'] = '';
381 if ( !isset( $options['month'] ) ) {
382 $options['month'] = '';
385 if ( $options['contribs'] == 'newbie' ) {
386 $options['target'] = '';
389 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
391 foreach ( $options as $name => $value ) {
392 if ( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
395 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
399 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
400 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'contribs' , 'newbie' , 'newbie', $options['contribs'] == 'newbie' ?
true : false ) . '<br />' .
401 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'contribs' , 'user', 'user', $options['contribs'] == 'user' ?
true : false ) . ' ' .
402 Xml
::input( 'target', 20, $options['target']) . ' '.
403 '<span style="white-space: nowrap">' .
404 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
405 Xml
::namespaceSelector( $options['namespace'], '' ) .
407 Xml
::openElement( 'p' ) .
408 '<span style="white-space: nowrap">' .
409 Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
410 Xml
::input( 'year', 4, $options['year'], array('id' => 'year', 'maxlength' => 4) ) .
413 '<span style="white-space: nowrap">' .
414 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
415 Xml
::monthSelector( $options['month'], -1 ) . ' '.
417 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
418 Xml
::closeElement( 'p' );
420 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
421 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
422 $f .= "<p>{$explain}</p>";
424 $f .= '</fieldset>' .
425 Xml
::closeElement( 'form' );