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 sysop
118 * privileges. The rollback link restores the most recent version that was not
119 * 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( $wgUser->isAllowed( 'rollback' ) ) {
143 $topmarktext .= ' '.$sk->generateRollback( $rev );
147 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
148 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=prev&oldid='.$row->rev_id
) . ')';
150 $difftext = '(' . $this->messages
['diff'] . ')';
152 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages
['hist'], 'action=history' ) . ')';
154 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev );
155 $d = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
157 if( $this->target
== 'newbies' ) {
158 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
159 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
164 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
165 $d = '<span class="history-deleted">' . $d . '</span>';
168 if( $row->rev_minor_edit
) {
169 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
174 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link}{$userlink}{$comment} {$topmarktext}";
175 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
176 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
178 $ret = "<li>$ret</li>\n";
179 wfProfileOut( __METHOD__
);
184 * Get the Database object in use
188 public function getDatabase() {
195 * Special page "user contributions".
196 * Shows a list of the contributions of a user.
199 * @param $par String: (optional) user name of the user for which to show the contributions
201 function wfSpecialContributions( $par = null ) {
202 global $wgUser, $wgOut, $wgLang, $wgRequest;
206 if ( isset( $par ) && $par == 'newbies' ) {
208 $options['contribs'] = 'newbie';
209 } elseif ( isset( $par ) ) {
212 $target = $wgRequest->getVal( 'target' );
215 // check for radiobox
216 if ( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
218 $options['contribs'] = 'newbie';
221 if ( !strlen( $target ) ) {
222 $wgOut->addHTML( contributionsForm( '' ) );
226 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
227 $options['target'] = $target;
229 $nt = Title
::makeTitleSafe( NS_USER
, $target );
231 $wgOut->addHTML( contributionsForm( '' ) );
234 $id = User
::idFromName( $nt->getText() );
236 if ( $target != 'newbies' ) {
237 $target = $nt->getText();
238 $wgOut->setSubtitle( contributionsSub( $nt, $id ) );
240 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
243 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
244 $options['namespace'] = intval( $ns );
246 $options['namespace'] = '';
248 if ( $wgUser->isAllowed( 'markbotedit' ) && $wgRequest->getBool( 'bot' ) ) {
249 $options['bot'] = '1';
252 $skip = $wgRequest->getText( 'offset' ) ||
$wgRequest->getText( 'dir' ) == 'prev';
253 # Offset overrides year/month selection
254 if ( ( $month = $wgRequest->getIntOrNull( 'month' ) ) !== null && $month !== -1 ) {
255 $options['month'] = intval( $month );
257 $options['month'] = '';
259 if ( ( $year = $wgRequest->getIntOrNull( 'year' ) ) !== null ) {
260 $options['year'] = intval( $year );
261 } else if( $options['month'] ) {
262 $thisMonth = intval( gmdate( 'n' ) );
263 $thisYear = intval( gmdate( 'Y' ) );
264 if( intval( $options['month'] ) > $thisMonth ) {
267 $options['year'] = $thisYear;
269 $options['year'] = '';
272 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
274 $wgOut->addHTML( contributionsForm( $options ) );
275 # Show original selected options, don't apply them so as to allow paging
276 $_GET['year'] = ''; // hack for Pager
277 $_GET['month'] = ''; // hack for Pager
279 $options['year'] = '';
280 $options['month'] = '';
283 $pager = new ContribsPager( $target, $options['namespace'], $options['year'], $options['month'] );
284 if ( !$pager->getNumRows() ) {
285 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
289 # Show a message about slave lag, if applicable
290 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
291 $wgOut->showLagWarning( $lag );
294 '<p>' . $pager->getNavigationBar() . '</p>' .
296 '<p>' . $pager->getNavigationBar() . '</p>' );
298 # If there were contributions, and it was a valid user or IP, show
299 # the appropriate "footer" message - WHOIS tools, etc.
300 if( $target != 'newbies' ) {
301 $message = IP
::isIPAddress( $target )
302 ?
'sp-contributions-footer-anon'
303 : 'sp-contributions-footer';
306 $text = wfMsg( $message, $target );
307 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
308 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
309 $wgOut->addWikiText( $text );
310 $wgOut->addHtml( '</div>' );
316 * Generates the subheading with links
317 * @param Title $nt Title object for the target
318 * @param integer $id User ID for the target
319 * @return String: appropriately-escaped HTML to be output literally
321 function contributionsSub( $nt, $id ) {
322 global $wgSysopUserBans, $wgLang, $wgUser;
324 $sk = $wgUser->getSkin();
327 $user = $nt->getText();
329 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
331 $talk = $nt->getTalkPage();
334 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
335 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && User
::isIP( $nt->getText() ) ) ) {
337 if( $wgUser->isAllowed( 'block' ) )
338 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
340 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
343 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
345 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
347 $links = implode( ' | ', $tools );
350 // Old message 'contribsub' had one parameter, but that doesn't work for
351 // languages that want to put the "for" bit right after $user but before
352 // $links. If 'contribsub' is around, use it for reverse compatibility,
353 // otherwise use 'contribsub2'.
354 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
355 return wfMsgHtml( 'contribsub2', $user, $links );
357 return wfMsgHtml( 'contribsub', "$user ($links)" );
362 * Generates the namespace selector form with hidden attributes.
363 * @param $options Array: the options to be included.
365 function contributionsForm( $options ) {
366 global $wgScript, $wgTitle, $wgRequest;
368 $options['title'] = $wgTitle->getPrefixedText();
369 if ( !isset( $options['target'] ) ) {
370 $options['target'] = '';
372 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
375 if ( !isset( $options['namespace'] ) ) {
376 $options['namespace'] = '';
379 if ( !isset( $options['contribs'] ) ) {
380 $options['contribs'] = 'user';
383 if ( !isset( $options['year'] ) ) {
384 $options['year'] = '';
387 if ( !isset( $options['month'] ) ) {
388 $options['month'] = '';
391 if ( $options['contribs'] == 'newbie' ) {
392 $options['target'] = '';
395 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
397 foreach ( $options as $name => $value ) {
398 if ( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
401 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
405 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
406 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'contribs' , 'newbie' , 'newbie', $options['contribs'] == 'newbie' ?
true : false ) . '<br />' .
407 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'contribs' , 'user', 'user', $options['contribs'] == 'user' ?
true : false ) . ' ' .
408 Xml
::input( 'target', 20, $options['target']) . ' '.
409 '<span style="white-space: nowrap">' .
410 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
411 Xml
::namespaceSelector( $options['namespace'], '' ) .
413 Xml
::openElement( 'p' ) .
414 '<span style="white-space: nowrap">' .
415 Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
416 Xml
::input( 'year', 4, $options['year'], array('id' => 'year', 'maxlength' => 4) ) .
419 '<span style="white-space: nowrap">' .
420 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
421 Xml
::monthSelector( $options['month'], -1 ) . ' '.
423 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
424 Xml
::closeElement( 'p' );
426 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
427 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
428 $f .= "<p>{$explain}</p>";
430 $f .= '</fieldset>' .
431 Xml
::closeElement( 'form' );