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 = '', $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 > 0 && $month < 13) ?
$month : false;
27 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
30 function getDefaultQuery() {
31 $query = parent
::getDefaultQuery();
32 $query['target'] = $this->target
;
36 function getQueryInfo() {
37 list( $index, $userCond ) = $this->getUserCond();
38 $conds = array_merge( array('page_id=rev_page'), $userCond, $this->getNamespaceCond() );
41 'tables' => array( 'page', 'revision' ),
43 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page',
44 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user',
45 'rev_user_text', 'rev_deleted'
48 'options' => array( 'USE INDEX' => $index )
52 function getUserCond() {
55 if ( $this->target
== 'newbies' ) {
56 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
57 $condition[] = 'rev_user >' . (int)($max - $max / 100);
58 $index = 'user_timestamp';
60 $condition['rev_user_text'] = $this->target
;
61 $index = 'usertext_timestamp';
63 return array( $index, $condition );
66 function getNamespaceCond() {
67 if ( $this->namespace !== '' ) {
68 return array( 'page_namespace' => (int)$this->namespace );
74 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 );
81 $thisMonth = gmdate( 'n' );
82 if( $this->month
> $thisMonth ) {
83 // Future contributions aren't supposed to happen. :)
89 $month_end = str_pad($this->month +
1, 2, '0', STR_PAD_LEFT
);
90 $year_end = $year_start;
93 $year_end = $year_start +
1;
95 $ts_end = str_pad($year_end . $month_end, 14, '0' );
97 $this->mOffset
= $ts_end;
101 function getIndexField() {
102 return 'rev_timestamp';
105 function getStartBody() {
109 function getEndBody() {
114 * Generates each row in the contributions list.
116 * Contributions which are marked "top" are currently on top of the history.
117 * For these contributions, a [rollback] link is shown for users with roll-
118 * back privileges. The rollback link restores the most recent version that
119 * was not written by the target user.
121 * @todo This would probably look a lot nicer in a table.
123 function formatRow( $row ) {
124 wfProfileIn( __METHOD__
);
126 global $wgLang, $wgUser, $wgContLang;
128 $sk = $this->getSkin();
129 $rev = new Revision( $row );
131 $page = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
132 $link = $sk->makeKnownLinkObj( $page );
133 $difftext = $topmarktext = '';
134 if( $row->rev_id
== $row->page_latest
) {
135 $topmarktext .= '<strong>' . $this->messages
['uctop'] . '</strong>';
136 if( !$row->page_is_new
) {
137 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=0' ) . ')';
139 $difftext .= $this->messages
['newarticle'];
142 if( !$page->getUserPermissionsErrors( 'rollback', $wgUser )
143 && !$page->getUserPermissionsErrors( 'edit', $wgUser ) ) {
144 $topmarktext .= ' '.$sk->generateRollback( $rev );
148 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
149 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=prev&oldid='.$row->rev_id
) . ')';
151 $difftext = '(' . $this->messages
['diff'] . ')';
153 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages
['hist'], 'action=history' ) . ')';
155 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev );
156 $d = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
158 if( $this->target
== 'newbies' ) {
159 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
160 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
165 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
166 $d = '<span class="history-deleted">' . $d . '</span>';
169 if( $row->rev_minor_edit
) {
170 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
175 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link}{$userlink}{$comment} {$topmarktext}";
176 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
177 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
179 $ret = "<li>$ret</li>\n";
180 wfProfileOut( __METHOD__
);
185 * Get the Database object in use
189 public function getDatabase() {
196 * Special page "user contributions".
197 * Shows a list of the contributions of a user.
200 * @param $par String: (optional) user name of the user for which to show the contributions
202 function wfSpecialContributions( $par = null ) {
203 global $wgUser, $wgOut, $wgLang, $wgRequest;
207 if ( isset( $par ) && $par == 'newbies' ) {
209 $options['contribs'] = 'newbie';
210 } elseif ( isset( $par ) ) {
213 $target = $wgRequest->getVal( 'target' );
216 // check for radiobox
217 if ( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
219 $options['contribs'] = 'newbie';
222 if ( !strlen( $target ) ) {
223 $wgOut->addHTML( contributionsForm( '' ) );
227 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
228 $options['target'] = $target;
230 $nt = Title
::makeTitleSafe( NS_USER
, $target );
232 $wgOut->addHTML( contributionsForm( '' ) );
235 $id = User
::idFromName( $nt->getText() );
237 if ( $target != 'newbies' ) {
238 $target = $nt->getText();
239 $wgOut->setSubtitle( contributionsSub( $nt, $id ) );
241 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
244 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
245 $options['namespace'] = intval( $ns );
247 $options['namespace'] = '';
249 if ( $wgUser->isAllowed( 'markbotedit' ) && $wgRequest->getBool( 'bot' ) ) {
250 $options['bot'] = '1';
253 $skip = $wgRequest->getText( 'offset' ) ||
$wgRequest->getText( 'dir' ) == 'prev';
254 # Offset overrides year/month selection
255 if ( ( $month = $wgRequest->getIntOrNull( 'month' ) ) !== null && $month !== -1 ) {
256 $options['month'] = intval( $month );
258 $options['month'] = '';
260 if ( ( $year = $wgRequest->getIntOrNull( 'year' ) ) !== null ) {
261 $options['year'] = intval( $year );
262 } else if( $options['month'] ) {
263 $thisMonth = intval( gmdate( 'n' ) );
264 $thisYear = intval( gmdate( 'Y' ) );
265 if( intval( $options['month'] ) > $thisMonth ) {
268 $options['year'] = $thisYear;
270 $options['year'] = '';
273 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
275 $wgOut->addHTML( contributionsForm( $options ) );
276 # Show original selected options, don't apply them so as to allow paging
277 $_GET['year'] = ''; // hack for Pager
278 $_GET['month'] = ''; // hack for Pager
280 $options['year'] = '';
281 $options['month'] = '';
284 $pager = new ContribsPager( $target, $options['namespace'], $options['year'], $options['month'] );
285 if ( !$pager->getNumRows() ) {
286 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
290 # Show a message about slave lag, if applicable
291 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
292 $wgOut->showLagWarning( $lag );
295 '<p>' . $pager->getNavigationBar() . '</p>' .
297 '<p>' . $pager->getNavigationBar() . '</p>' );
299 # If there were contributions, and it was a valid user or IP, show
300 # the appropriate "footer" message - WHOIS tools, etc.
301 if( $target != 'newbies' ) {
302 $message = IP
::isIPAddress( $target )
303 ?
'sp-contributions-footer-anon'
304 : 'sp-contributions-footer';
307 $text = wfMsg( $message, $target );
308 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
309 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
310 $wgOut->addWikiText( $text );
311 $wgOut->addHtml( '</div>' );
317 * Generates the subheading with links
318 * @param Title $nt Title object for the target
319 * @param integer $id User ID for the target
320 * @return String: appropriately-escaped HTML to be output literally
322 function contributionsSub( $nt, $id ) {
323 global $wgSysopUserBans, $wgLang, $wgUser;
325 $sk = $wgUser->getSkin();
328 $user = $nt->getText();
330 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
332 $talk = $nt->getTalkPage();
335 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
336 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && User
::isIP( $nt->getText() ) ) ) {
338 if( $wgUser->isAllowed( 'block' ) )
339 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
341 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
344 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
346 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
348 $links = implode( ' | ', $tools );
351 // Old message 'contribsub' had one parameter, but that doesn't work for
352 // languages that want to put the "for" bit right after $user but before
353 // $links. If 'contribsub' is around, use it for reverse compatibility,
354 // otherwise use 'contribsub2'.
355 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
356 return wfMsgHtml( 'contribsub2', $user, $links );
358 return wfMsgHtml( 'contribsub', "$user ($links)" );
363 * Generates the namespace selector form with hidden attributes.
364 * @param $options Array: the options to be included.
366 function contributionsForm( $options ) {
367 global $wgScript, $wgTitle, $wgRequest;
369 $options['title'] = $wgTitle->getPrefixedText();
370 if ( !isset( $options['target'] ) ) {
371 $options['target'] = '';
373 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
376 if ( !isset( $options['namespace'] ) ) {
377 $options['namespace'] = '';
380 if ( !isset( $options['contribs'] ) ) {
381 $options['contribs'] = 'user';
384 if ( !isset( $options['year'] ) ) {
385 $options['year'] = '';
388 if ( !isset( $options['month'] ) ) {
389 $options['month'] = '';
392 if ( $options['contribs'] == 'newbie' ) {
393 $options['target'] = '';
396 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
398 foreach ( $options as $name => $value ) {
399 if ( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
402 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
406 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
407 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'contribs' , 'newbie' , 'newbie', $options['contribs'] == 'newbie' ?
true : false ) . '<br />' .
408 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'contribs' , 'user', 'user', $options['contribs'] == 'user' ?
true : false ) . ' ' .
409 Xml
::input( 'target', 20, $options['target']) . ' '.
410 '<span style="white-space: nowrap">' .
411 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
412 Xml
::namespaceSelector( $options['namespace'], '' ) .
414 Xml
::openElement( 'p' ) .
415 '<span style="white-space: nowrap">' .
416 Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
417 Xml
::input( 'year', 4, $options['year'], array('id' => 'year', 'maxlength' => 4) ) .
420 '<span style="white-space: nowrap">' .
421 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
422 Xml
::monthSelector( $options['month'], -1 ) . ' '.
424 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
425 Xml
::closeElement( 'p' );
427 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
428 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
429 $f .= "<p>{$explain}</p>";
431 $f .= '</fieldset>' .
432 Xml
::closeElement( 'form' );