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 < 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
;
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() );
43 'tables' => array( 'page', 'revision' ),
45 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page',
46 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user',
47 'rev_user_text', 'rev_parent_id', 'rev_deleted'
50 'options' => array( 'USE INDEX' => $index )
54 function getUserCond() {
57 if ( $this->target
== 'newbies' ) {
58 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
59 $condition[] = 'rev_user >' . (int)($max - $max / 100);
60 $index = 'user_timestamp';
62 $condition['rev_user_text'] = $this->target
;
63 $index = 'usertext_timestamp';
65 return array( $index, $condition );
68 function getNamespaceCond() {
69 if ( $this->namespace !== '' ) {
70 return array( 'page_namespace' => (int)$this->namespace );
76 function getDateCond() {
77 if ( $this->year ||
$this->month
) {
78 // Assume this year if only a month is given
80 $year_start = $this->year
;
82 $year_start = substr( wfTimestampNow(), 0, 4 );
83 $thisMonth = gmdate( 'n' );
84 if( $this->month
> $thisMonth ) {
85 // Future contributions aren't supposed to happen. :)
91 $month_end = str_pad($this->month +
1, 2, '0', STR_PAD_LEFT
);
92 $year_end = $year_start;
95 $year_end = $year_start +
1;
97 $ts_end = str_pad($year_end . $month_end, 14, '0' );
99 $this->mOffset
= $ts_end;
103 function getIndexField() {
104 return 'rev_timestamp';
107 function getStartBody() {
111 function getEndBody() {
116 * Generates each row in the contributions list.
118 * Contributions which are marked "top" are currently on top of the history.
119 * For these contributions, a [rollback] link is shown for users with roll-
120 * back privileges. The rollback link restores the most recent version that
121 * was not written by the target user.
123 * @todo This would probably look a lot nicer in a table.
125 function formatRow( $row ) {
126 wfProfileIn( __METHOD__
);
128 global $wgLang, $wgUser, $wgContLang;
130 $sk = $this->getSkin();
131 $rev = new Revision( $row );
133 $page = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
134 $link = $sk->makeKnownLinkObj( $page );
135 $difftext = $topmarktext = '';
136 if( $row->rev_id
== $row->page_latest
) {
137 $topmarktext .= '<strong>' . $this->messages
['uctop'] . '</strong>';
138 if( !$row->page_is_new
) {
139 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=0' ) . ')';
141 $difftext .= $this->messages
['newarticle'];
144 if( !$page->getUserPermissionsErrors( 'rollback', $wgUser )
145 && !$page->getUserPermissionsErrors( 'edit', $wgUser ) ) {
146 $topmarktext .= ' '.$sk->generateRollback( $rev );
150 # Is there a visable previous revision?
151 if( $rev->userCan(Revision
::DELETED_TEXT
) ) {
152 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=prev&oldid='.$row->rev_id
) . ')';
154 $difftext = '(' . $this->messages
['diff'] . ')';
156 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages
['hist'], 'action=history' ) . ')';
158 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
159 $d = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
161 if( $this->target
== 'newbies' ) {
162 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
163 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
168 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
169 $d = '<span class="history-deleted">' . $d . '</span>';
172 if( $rev->getParentId() === 0 ) {
173 $nflag = '<span class="newpage">' . $this->messages
['newpageletter'] . '</span>';
178 if( $row->rev_minor_edit
) {
179 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
184 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink}{$comment} {$topmarktext}";
185 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
186 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
188 $ret = "<li>$ret</li>\n";
189 wfProfileOut( __METHOD__
);
194 * Get the Database object in use
198 public function getDatabase() {
205 * Special page "user contributions".
206 * Shows a list of the contributions of a user.
209 * @param $par String: (optional) user name of the user for which to show the contributions
211 function wfSpecialContributions( $par = null ) {
212 global $wgUser, $wgOut, $wgLang, $wgRequest;
216 if ( isset( $par ) && $par == 'newbies' ) {
218 $options['contribs'] = 'newbie';
219 } elseif ( isset( $par ) ) {
222 $target = $wgRequest->getVal( 'target' );
225 // check for radiobox
226 if ( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
228 $options['contribs'] = 'newbie';
231 if ( !strlen( $target ) ) {
232 $wgOut->addHTML( contributionsForm( '' ) );
236 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
237 $options['target'] = $target;
239 $nt = Title
::makeTitleSafe( NS_USER
, $target );
241 $wgOut->addHTML( contributionsForm( '' ) );
244 $id = User
::idFromName( $nt->getText() );
246 if ( $target != 'newbies' ) {
247 $target = $nt->getText();
248 $wgOut->setSubtitle( contributionsSub( $nt, $id ) );
250 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
253 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
254 $options['namespace'] = intval( $ns );
256 $options['namespace'] = '';
258 if ( $wgUser->isAllowed( 'markbotedit' ) && $wgRequest->getBool( 'bot' ) ) {
259 $options['bot'] = '1';
262 $skip = $wgRequest->getText( 'offset' ) ||
$wgRequest->getText( 'dir' ) == 'prev';
263 # Offset overrides year/month selection
264 if ( ( $month = $wgRequest->getIntOrNull( 'month' ) ) !== null && $month !== -1 ) {
265 $options['month'] = intval( $month );
267 $options['month'] = '';
269 if ( ( $year = $wgRequest->getIntOrNull( 'year' ) ) !== null ) {
270 $options['year'] = intval( $year );
271 } else if( $options['month'] ) {
272 $thisMonth = intval( gmdate( 'n' ) );
273 $thisYear = intval( gmdate( 'Y' ) );
274 if( intval( $options['month'] ) > $thisMonth ) {
277 $options['year'] = $thisYear;
279 $options['year'] = '';
282 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
285 $options['year'] = '';
286 $options['month'] = '';
289 $wgOut->addHTML( contributionsForm( $options ) );
291 $pager = new ContribsPager( $target, $options['namespace'], $options['year'], $options['month'] );
292 if ( !$pager->getNumRows() ) {
293 $wgOut->addWikiMsg( 'nocontribs' );
297 # Show a message about slave lag, if applicable
298 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
299 $wgOut->showLagWarning( $lag );
302 '<p>' . $pager->getNavigationBar() . '</p>' .
304 '<p>' . $pager->getNavigationBar() . '</p>' );
306 # If there were contributions, and it was a valid user or IP, show
307 # the appropriate "footer" message - WHOIS tools, etc.
308 if( $target != 'newbies' ) {
309 $message = IP
::isIPAddress( $target )
310 ?
'sp-contributions-footer-anon'
311 : 'sp-contributions-footer';
314 $text = wfMsgNoTrans( $message, $target );
315 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
316 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
317 $wgOut->addWikiText( $text );
318 $wgOut->addHtml( '</div>' );
324 * Generates the subheading with links
325 * @param Title $nt Title object for the target
326 * @param integer $id User ID for the target
327 * @return String: appropriately-escaped HTML to be output literally
329 function contributionsSub( $nt, $id ) {
330 global $wgSysopUserBans, $wgLang, $wgUser;
332 $sk = $wgUser->getSkin();
335 $user = $nt->getText();
337 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
339 $talk = $nt->getTalkPage();
342 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
343 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && User
::isIP( $nt->getText() ) ) ) {
345 if( $wgUser->isAllowed( 'block' ) )
346 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
348 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
351 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
353 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
355 $links = implode( ' | ', $tools );
358 // Old message 'contribsub' had one parameter, but that doesn't work for
359 // languages that want to put the "for" bit right after $user but before
360 // $links. If 'contribsub' is around, use it for reverse compatibility,
361 // otherwise use 'contribsub2'.
362 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
363 return wfMsgHtml( 'contribsub2', $user, $links );
365 return wfMsgHtml( 'contribsub', "$user ($links)" );
370 * Generates the namespace selector form with hidden attributes.
371 * @param $options Array: the options to be included.
373 function contributionsForm( $options ) {
374 global $wgScript, $wgTitle, $wgRequest;
376 $options['title'] = $wgTitle->getPrefixedText();
377 if ( !isset( $options['target'] ) ) {
378 $options['target'] = '';
380 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
383 if ( !isset( $options['namespace'] ) ) {
384 $options['namespace'] = '';
387 if ( !isset( $options['contribs'] ) ) {
388 $options['contribs'] = 'user';
391 if ( !isset( $options['year'] ) ) {
392 $options['year'] = '';
395 if ( !isset( $options['month'] ) ) {
396 $options['month'] = '';
399 if ( $options['contribs'] == 'newbie' ) {
400 $options['target'] = '';
403 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
405 foreach ( $options as $name => $value ) {
406 if ( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
409 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
413 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
414 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'contribs' , 'newbie' , 'newbie', $options['contribs'] == 'newbie' ?
true : false ) . '<br />' .
415 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'contribs' , 'user', 'user', $options['contribs'] == 'user' ?
true : false ) . ' ' .
416 Xml
::input( 'target', 20, $options['target']) . ' '.
417 '<span style="white-space: nowrap">' .
418 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
419 Xml
::namespaceSelector( $options['namespace'], '' ) .
421 Xml
::openElement( 'p' ) .
422 '<span style="white-space: nowrap">' .
423 Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
424 Xml
::input( 'year', 4, $options['year'], array('id' => 'year', 'maxlength' => 4) ) .
427 '<span style="white-space: nowrap">' .
428 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
429 Xml
::monthSelector( $options['month'], -1 ) . ' '.
431 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
432 Xml
::closeElement( 'p' );
434 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
435 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
436 $f .= "<p>{$explain}</p>";
438 $f .= '</fieldset>' .
439 Xml
::closeElement( 'form' );