3 * Special:Contributions, show user contributions in a paged list
4 * @addtogroup SpecialPage
7 class ContribsPager
extends ReverseChronologicalPager
{
8 public $mDefaultDirection = true;
9 var $messages, $target;
10 var $namespace = '', $year = '', $month = '', $mDb;
12 function __construct( $target, $namespace = false, $year = false, $month = false ) {
13 parent
::__construct();
14 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter 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 : false;
24 $this->month
= ($month > 0 && $month < 13) ?
$month : false;
27 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
30 function getDefaultQuery() {
31 $query = parent
::getDefaultQuery();
32 $query['target'] = $this->target
;
33 $query['month'] = $this->month
;
34 $query['year'] = $this->year
;
38 function getQueryInfo() {
39 list( $index, $userCond ) = $this->getUserCond();
40 $conds = array_merge( array('page_id=rev_page'), $userCond, $this->getNamespaceCond() );
42 'tables' => array( 'page', 'revision' ),
44 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page',
45 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user',
46 'rev_user_text', 'rev_parent_id', 'rev_deleted'
49 'options' => array( 'USE INDEX' => $index )
53 function getUserCond() {
56 if ( $this->target
== 'newbies' ) {
57 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
58 $condition[] = 'rev_user >' . (int)($max - $max / 100);
59 $index = 'user_timestamp';
61 $condition['rev_user_text'] = $this->target
;
62 $index = 'usertext_timestamp';
64 return array( $index, $condition );
67 function getNamespaceCond() {
68 if ( $this->namespace !== '' ) {
69 return array( 'page_namespace' => (int)$this->namespace );
75 function getDateCond() {
76 // Given an optional year and month, we need to generate a timestamp
77 // to use as "WHERE rev_timestamp <= result"
78 // Examples: year = 2006 equals < 20070101 (+000000)
79 // year=2005, month=1 equals < 20050201
80 // year=2005, month=12 equals < 20060101
82 if (!$this->year
&& !$this->month
)
89 // If no year given, assume the current one
90 $year = gmdate( 'Y' );
91 // If this month hasn't happened yet this year, go back to last year's month
92 if( $this->month
> gmdate( 'n' ) ) {
98 $month = $this->month +
1;
99 // For December, we want January 1 of the next year
106 // No month implies we want up to the end of the year in question
113 $ymd = (int)sprintf( "%04d%02d01", $year, $month );
119 $this->mOffset
= $this->mDb
->timestamp( "${ymd}000000" );
122 function getIndexField() {
123 return 'rev_timestamp';
126 function getStartBody() {
130 function getEndBody() {
135 * Generates each row in the contributions list.
137 * Contributions which are marked "top" are currently on top of the history.
138 * For these contributions, a [rollback] link is shown for users with roll-
139 * back privileges. The rollback link restores the most recent version that
140 * was not written by the target user.
142 * @todo This would probably look a lot nicer in a table.
144 function formatRow( $row ) {
145 wfProfileIn( __METHOD__
);
147 global $wgLang, $wgUser, $wgContLang;
149 $sk = $this->getSkin();
150 $rev = new Revision( $row );
152 $page = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
153 $link = $sk->makeKnownLinkObj( $page );
154 $difftext = $topmarktext = '';
155 if( $row->rev_id
== $row->page_latest
) {
156 $topmarktext .= '<strong>' . $this->messages
['uctop'] . '</strong>';
157 if( !$row->page_is_new
) {
158 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=0' ) . ')';
160 $difftext .= $this->messages
['newarticle'];
163 if( !$page->getUserPermissionsErrors( 'rollback', $wgUser )
164 && !$page->getUserPermissionsErrors( 'edit', $wgUser ) ) {
165 $topmarktext .= ' '.$sk->generateRollback( $rev );
169 # Is there a visible previous revision?
170 if( $rev->userCan(Revision
::DELETED_TEXT
) ) {
171 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=prev&oldid='.$row->rev_id
) . ')';
173 $difftext = '(' . $this->messages
['diff'] . ')';
175 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages
['hist'], 'action=history' ) . ')';
177 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
178 $d = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
180 if( $this->target
== 'newbies' ) {
181 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
182 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
187 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
188 $d = '<span class="history-deleted">' . $d . '</span>';
191 if( $rev->getParentId() === 0 ) {
192 $nflag = '<span class="newpage">' . $this->messages
['newpageletter'] . '</span>';
197 if( $row->rev_minor_edit
) {
198 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
203 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink}{$comment} {$topmarktext}";
204 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
205 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
207 $ret = "<li>$ret</li>\n";
208 wfProfileOut( __METHOD__
);
213 * Get the Database object in use
217 public function getDatabase() {
224 * Special page "user contributions".
225 * Shows a list of the contributions of a user.
228 * @param $par String: (optional) user name of the user for which to show the contributions
230 function wfSpecialContributions( $par = null ) {
231 global $wgUser, $wgOut, $wgLang, $wgRequest;
235 if ( isset( $par ) && $par == 'newbies' ) {
237 $options['contribs'] = 'newbie';
238 } elseif ( isset( $par ) ) {
241 $target = $wgRequest->getVal( 'target' );
244 // check for radiobox
245 if ( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
247 $options['contribs'] = 'newbie';
250 if ( !strlen( $target ) ) {
251 $wgOut->addHTML( contributionsForm( '' ) );
255 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
256 $options['target'] = $target;
258 $nt = Title
::makeTitleSafe( NS_USER
, $target );
260 $wgOut->addHTML( contributionsForm( '' ) );
263 $id = User
::idFromName( $nt->getText() );
265 if ( $target != 'newbies' ) {
266 $target = $nt->getText();
267 $wgOut->setSubtitle( contributionsSub( $nt, $id ) );
269 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
272 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
273 $options['namespace'] = intval( $ns );
275 $options['namespace'] = '';
277 if ( $wgUser->isAllowed( 'markbotedit' ) && $wgRequest->getBool( 'bot' ) ) {
278 $options['bot'] = '1';
281 $skip = $wgRequest->getText( 'offset' ) ||
$wgRequest->getText( 'dir' ) == 'prev';
282 # Offset overrides year/month selection
283 if ( ( $month = $wgRequest->getIntOrNull( 'month' ) ) !== null && $month !== -1 ) {
284 $options['month'] = intval( $month );
286 $options['month'] = '';
288 if ( ( $year = $wgRequest->getIntOrNull( 'year' ) ) !== null ) {
289 $options['year'] = intval( $year );
290 } else if( $options['month'] ) {
291 $thisMonth = intval( gmdate( 'n' ) );
292 $thisYear = intval( gmdate( 'Y' ) );
293 if( intval( $options['month'] ) > $thisMonth ) {
296 $options['year'] = $thisYear;
298 $options['year'] = '';
301 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
304 $options['year'] = '';
305 $options['month'] = '';
308 $wgOut->addHTML( contributionsForm( $options ) );
310 $pager = new ContribsPager( $target, $options['namespace'], $options['year'], $options['month'] );
311 if ( !$pager->getNumRows() ) {
312 $wgOut->addWikiMsg( 'nocontribs' );
316 # Show a message about slave lag, if applicable
317 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
318 $wgOut->showLagWarning( $lag );
321 '<p>' . $pager->getNavigationBar() . '</p>' .
323 '<p>' . $pager->getNavigationBar() . '</p>' );
325 # If there were contributions, and it was a valid user or IP, show
326 # the appropriate "footer" message - WHOIS tools, etc.
327 if( $target != 'newbies' ) {
328 $message = IP
::isIPAddress( $target )
329 ?
'sp-contributions-footer-anon'
330 : 'sp-contributions-footer';
333 $text = wfMsgNoTrans( $message, $target );
334 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
335 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
336 $wgOut->addWikiText( $text );
337 $wgOut->addHtml( '</div>' );
343 * Generates the subheading with links
344 * @param Title $nt Title object for the target
345 * @param integer $id User ID for the target
346 * @return String: appropriately-escaped HTML to be output literally
348 function contributionsSub( $nt, $id ) {
349 global $wgSysopUserBans, $wgLang, $wgUser;
351 $sk = $wgUser->getSkin();
354 $user = $nt->getText();
356 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
358 $talk = $nt->getTalkPage();
361 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
362 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && User
::isIP( $nt->getText() ) ) ) {
364 if( $wgUser->isAllowed( 'block' ) )
365 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
367 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
370 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
372 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
374 $links = implode( ' | ', $tools );
377 // Old message 'contribsub' had one parameter, but that doesn't work for
378 // languages that want to put the "for" bit right after $user but before
379 // $links. If 'contribsub' is around, use it for reverse compatibility,
380 // otherwise use 'contribsub2'.
381 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
382 return wfMsgHtml( 'contribsub2', $user, $links );
384 return wfMsgHtml( 'contribsub', "$user ($links)" );
389 * Generates the namespace selector form with hidden attributes.
390 * @param $options Array: the options to be included.
392 function contributionsForm( $options ) {
393 global $wgScript, $wgTitle, $wgRequest;
395 $options['title'] = $wgTitle->getPrefixedText();
396 if ( !isset( $options['target'] ) ) {
397 $options['target'] = '';
399 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
402 if ( !isset( $options['namespace'] ) ) {
403 $options['namespace'] = '';
406 if ( !isset( $options['contribs'] ) ) {
407 $options['contribs'] = 'user';
410 if ( !isset( $options['year'] ) ) {
411 $options['year'] = '';
414 if ( !isset( $options['month'] ) ) {
415 $options['month'] = '';
418 if ( $options['contribs'] == 'newbie' ) {
419 $options['target'] = '';
422 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
424 foreach ( $options as $name => $value ) {
425 if ( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
428 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
432 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
433 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'contribs' , 'newbie' , 'newbie', $options['contribs'] == 'newbie' ?
true : false ) . '<br />' .
434 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'contribs' , 'user', 'user', $options['contribs'] == 'user' ?
true : false ) . ' ' .
435 Xml
::input( 'target', 20, $options['target']) . ' '.
436 '<span style="white-space: nowrap">' .
437 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
438 Xml
::namespaceSelector( $options['namespace'], '' ) .
440 Xml
::openElement( 'p' ) .
441 '<span style="white-space: nowrap">' .
442 Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
443 Xml
::input( 'year', 4, $options['year'], array('id' => 'year', 'maxlength' => 4) ) .
446 '<span style="white-space: nowrap">' .
447 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
448 Xml
::monthSelector( $options['month'], -1 ) . ' '.
450 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
451 Xml
::closeElement( 'p' );
453 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
454 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
455 $f .= "<p>{$explain}</p>";
457 $f .= '</fieldset>' .
458 Xml
::closeElement( 'form' );