3 * Special:Contributions, show user contributions in a paged list
4 * @addtogroup SpecialPage
7 class ContribsPager
extends IndexPager
{
8 public $mDefaultDirection = true;
9 var $messages, $target;
10 var $namespace = '', $mDb;
12 function __construct( $target, $namespace = false, $year = false, $month = false ) {
13 parent
::__construct();
14 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
15 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escape') );
17 $this->target
= $target;
18 $this->namespace = $namespace;
20 $year = intval($year);
21 $month = intval($month);
23 $this->year
= ($year > 0 && $year < 10000) ?
$year : false;
24 $this->month
= ($month > 1 && $month < 13) ?
$month : false;
25 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
28 function getDefaultQuery() {
29 $query = parent
::getDefaultQuery();
30 $query['target'] = $this->target
;
34 function getQueryInfo() {
35 list( $index, $userCond ) = $this->getUserCond();
36 $conds = array_merge( array('page_id=rev_page'), $userCond, $this->getNamespaceCond(), $this->GetDateCond() );
39 'tables' => array( 'page', 'revision' ),
41 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page',
42 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user',
43 'rev_user_text', 'rev_deleted'
46 'options' => array( 'FORCE INDEX' => $index )
50 function getUserCond() {
53 if ( $this->target
== 'newbies' ) {
54 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
55 $condition[] = 'rev_user >' . (int)($max - $max / 100);
56 $index = 'user_timestamp';
58 $condition['rev_user_text'] = $this->target
;
59 $index = 'usertext_timestamp';
61 return array( $index, $condition );
64 function getNamespaceCond() {
65 if ( $this->namespace !== '' ) {
66 return array( 'page_namespace' => (int)$this->namespace );
72 function getDateCond() {
75 if ( $this->year ||
$this->month
) {
76 // Assume this year if only a month is given
78 $year_start = $this->year
;
80 $year_start = substr( wfTimestampNow(), 0, 4 );
84 $month_start = str_pad($this->month
, 2, '0', STR_PAD_LEFT
);
85 $month_end = str_pad($this->month +
1, 2, '0', STR_PAD_LEFT
);
86 $year_end = $year_start;
90 $year_end = $year_start +
1;
93 $ts_start = str_pad($year_start . $month_start, 14, '0' );
94 $ts_end = str_pad($year_end . $month_end, 14, '0' );
96 $condition[] = "rev_timestamp >= $ts_start";
97 $condition[] = "rev_timestamp < $ts_end";
103 function getIndexField() {
104 return 'rev_timestamp';
107 function getStartBody() {
111 function getEndBody() {
115 function getNavigationBar() {
116 if ( isset( $this->mNavigationBar
) ) {
117 return $this->mNavigationBar
;
120 'prev' => wfMsgHtml( "sp-contributions-newer", $this->mLimit
),
121 'next' => wfMsgHtml( 'sp-contributions-older', $this->mLimit
),
122 'first' => wfMsgHtml('sp-contributions-newest'),
123 'last' => wfMsgHtml( 'sp-contributions-oldest' )
126 $pagingLinks = $this->getPagingLinks( $linkTexts );
127 $limitLinks = $this->getLimitLinks();
128 $limits = implode( ' | ', $limitLinks );
130 $this->mNavigationBar
= "({$pagingLinks['first']} | {$pagingLinks['last']}) " .
131 wfMsgHtml("viewprevnext", $pagingLinks['prev'], $pagingLinks['next'], $limits);
132 return $this->mNavigationBar
;
136 * Generates each row in the contributions list.
138 * Contributions which are marked "top" are currently on top of the history.
139 * For these contributions, a [rollback] link is shown for users with sysop
140 * privileges. The rollback link restores the most recent version that was not
141 * written by the target user.
143 * @todo This would probably look a lot nicer in a table.
145 function formatRow( $row ) {
146 wfProfileIn( __METHOD__
);
148 global $wgLang, $wgUser;
150 $sk = $this->getSkin();
151 $rev = new Revision( $row );
153 $page = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
154 $link = $sk->makeKnownLinkObj( $page );
155 $difftext = $topmarktext = '';
156 if( $row->rev_id
== $row->page_latest
) {
157 $topmarktext .= '<strong>' . $this->messages
['uctop'] . '</strong>';
158 if( !$row->page_is_new
) {
159 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=0' ) . ')';
161 $difftext .= $this->messages
['newarticle'];
164 if( $wgUser->isAllowed( 'rollback' ) ) {
165 $topmarktext .= ' '.$sk->generateRollback( $rev );
169 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
170 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=prev&oldid='.$row->rev_id
) . ')';
172 $difftext = '(' . $this->messages
['diff'] . ')';
174 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages
['hist'], 'action=history' ) . ')';
176 $comment = $sk->revComment( $rev );
177 $d = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
179 if( $this->target
== 'newbies' ) {
180 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
181 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
186 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
187 $d = '<span class="history-deleted">' . $d . '</span>';
190 if( $row->rev_minor_edit
) {
191 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
196 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link}{$userlink}{$comment} {$topmarktext}";
197 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
198 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
200 $ret = "<li>$ret</li>\n";
201 wfProfileOut( __METHOD__
);
206 * Get the Database object in use
210 public function getDatabase() {
217 * Special page "user contributions".
218 * Shows a list of the contributions of a user.
221 * @param $par String: (optional) user name of the user for which to show the contributions
223 function wfSpecialContributions( $par = null ) {
224 global $wgUser, $wgOut, $wgLang, $wgRequest;
228 if ( isset( $par ) && $par == 'newbies' ) {
230 $options['contribs'] = 'newbie';
231 } elseif ( isset( $par ) ) {
234 $target = $wgRequest->getVal( 'target' );
237 // check for radiobox
238 if ( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
240 $options['contribs'] = 'newbie';
243 if ( !strlen( $target ) ) {
244 $wgOut->addHTML( contributionsForm( '' ) );
248 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
249 $options['target'] = $target;
251 $nt = Title
::makeTitleSafe( NS_USER
, $target );
253 $wgOut->addHTML( contributionsForm( '' ) );
256 $id = User
::idFromName( $nt->getText() );
258 if ( $target != 'newbies' ) {
259 $target = $nt->getText();
260 $wgOut->setSubtitle( contributionsSub( $nt, $id ) );
262 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
265 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
266 $options['namespace'] = intval( $ns );
268 $options['namespace'] = '';
270 if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) {
271 $options['bot'] = '1';
274 if ( ( $month = $wgRequest->getVal( 'month', null ) ) !== null && $month !== '' ) {
275 $options['month'] = intval( $month );
277 $options['month'] = '';
280 if ( ( $year = $wgRequest->getVal( 'year', null ) ) !== null && $year !== '' ) {
281 $options['year'] = intval( $year );
282 } else if( $month ) {
283 $options['year'] = intval( substr( wfTimestampNow(), 0, 4 ) );
285 $options['year'] = '';
288 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
290 $wgOut->addHTML( contributionsForm( $options ) );
292 $pager = new ContribsPager( $target, $options['namespace'], $options['year'], $options['month'] );
293 if ( !$pager->getNumRows() ) {
294 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
298 # Show a message about slave lag, if applicable
299 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
300 $wgOut->showLagWarning( $lag );
303 '<p>' . $pager->getNavigationBar() . '</p>' .
305 '<p>' . $pager->getNavigationBar() . '</p>' );
307 # If there were contributions, and it was a valid user or IP, show
308 # the appropriate "footer" message - WHOIS tools, etc.
309 if( $target != 'newbies' ) {
310 $message = IP
::isIPAddress( $target )
311 ?
'sp-contributions-footer-anon'
312 : 'sp-contributions-footer';
315 $text = wfMsg( $message, $target );
316 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
317 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
318 $wgOut->addWikiText( $text );
319 $wgOut->addHtml( '</div>' );
325 * Generates the subheading with links
326 * @param Title $nt Title object for the target
327 * @param integer $id User ID for the target
328 * @return String: appropriately-escaped HTML to be output literally
330 function contributionsSub( $nt, $id ) {
331 global $wgSysopUserBans, $wgLang, $wgUser;
333 $sk = $wgUser->getSkin();
336 $user = $nt->getText();
338 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
340 $talk = $nt->getTalkPage();
343 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
344 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && User
::isIP( $nt->getText() ) ) ) {
346 if( $wgUser->isAllowed( 'block' ) )
347 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
349 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
352 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
353 $links = implode( ' | ', $tools );
356 // Old message 'contribsub' had one parameter, but that doesn't work for
357 // languages that want to put the "for" bit right after $user but before
358 // $links. If 'contribsub' is around, use it for reverse compatibility,
359 // otherwise use 'contribsub2'.
360 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
361 return wfMsgHtml( 'contribsub2', $user, $links );
363 return wfMsgHtml( 'contribsub', "$user ($links)" );
368 * Generates the namespace selector form with hidden attributes.
369 * @param $options Array: the options to be included.
371 function contributionsForm( $options ) {
372 global $wgScript, $wgTitle, $wgRequest;
374 $options['title'] = $wgTitle->getPrefixedText();
375 if ( !isset( $options['target'] ) ) {
376 $options['target'] = '';
378 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
381 if ( !isset( $options['namespace'] ) ) {
382 $options['namespace'] = '';
385 if ( !isset( $options['contribs'] ) ) {
386 $options['contribs'] = 'user';
389 if ( !isset( $options['year'] ) ) {
390 $options['year'] = '';
393 if ( !isset( $options['month'] ) ) {
394 $options['month'] = '';
397 if ( $options['contribs'] == 'newbie' ) {
398 $options['target'] = '';
401 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
403 foreach ( $options as $name => $value ) {
404 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
407 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
411 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
412 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'contribs' , 'newbie' , 'newbie', $options['contribs'] == 'newbie' ?
true : false ) . '<br />' .
413 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'contribs' , 'user', 'user', $options['contribs'] == 'user' ?
true : false ) . ' ' .
414 Xml
::input( 'target', 20, $options['target']) . ' '.
415 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
416 Xml
::namespaceSelector( $options['namespace'], '' ) .
417 Xml
::openElement( 'p' ) .
418 Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
419 Xml
::input( 'year', 4, $options['year'], array('id' => 'year', 'maxlength' => 4) ) . ' '.
420 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
421 xml
::monthSelector( $options['month'], -1 ) .
422 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
423 Xml
::closeElement( 'p' ) .
425 Xml
::closeElement( 'form' );