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 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
;
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_parent_id', '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, false, true );
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( $rev->getParentId() === 0 ) {
170 $nflag = '<span class="newpage">' . $this->messages
['newpageletter'] . '</span>';
175 if( $row->rev_minor_edit
) {
176 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
181 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink}{$comment} {$topmarktext}";
182 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
183 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
185 $ret = "<li>$ret</li>\n";
186 wfProfileOut( __METHOD__
);
191 * Get the Database object in use
195 public function getDatabase() {
202 * Special page "user contributions".
203 * Shows a list of the contributions of a user.
206 * @param $par String: (optional) user name of the user for which to show the contributions
208 function wfSpecialContributions( $par = null ) {
209 global $wgUser, $wgOut, $wgLang, $wgRequest;
213 if ( isset( $par ) && $par == 'newbies' ) {
215 $options['contribs'] = 'newbie';
216 } elseif ( isset( $par ) ) {
219 $target = $wgRequest->getVal( 'target' );
222 // check for radiobox
223 if ( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
225 $options['contribs'] = 'newbie';
228 if ( !strlen( $target ) ) {
229 $wgOut->addHTML( contributionsForm( '' ) );
233 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
234 $options['target'] = $target;
236 $nt = Title
::makeTitleSafe( NS_USER
, $target );
238 $wgOut->addHTML( contributionsForm( '' ) );
241 $id = User
::idFromName( $nt->getText() );
243 if ( $target != 'newbies' ) {
244 $target = $nt->getText();
245 $wgOut->setSubtitle( contributionsSub( $nt, $id ) );
247 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
250 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
251 $options['namespace'] = intval( $ns );
253 $options['namespace'] = '';
255 if ( $wgUser->isAllowed( 'markbotedit' ) && $wgRequest->getBool( 'bot' ) ) {
256 $options['bot'] = '1';
259 $skip = $wgRequest->getText( 'offset' ) ||
$wgRequest->getText( 'dir' ) == 'prev';
260 # Offset overrides year/month selection
261 if ( ( $month = $wgRequest->getIntOrNull( 'month' ) ) !== null && $month !== -1 ) {
262 $options['month'] = intval( $month );
264 $options['month'] = '';
266 if ( ( $year = $wgRequest->getIntOrNull( 'year' ) ) !== null ) {
267 $options['year'] = intval( $year );
268 } else if( $options['month'] ) {
269 $thisMonth = intval( gmdate( 'n' ) );
270 $thisYear = intval( gmdate( 'Y' ) );
271 if( intval( $options['month'] ) > $thisMonth ) {
274 $options['year'] = $thisYear;
276 $options['year'] = '';
279 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
281 $wgOut->addHTML( contributionsForm( $options ) );
282 # Show original selected options, don't apply them so as to allow paging
283 $_GET['year'] = ''; // hack for Pager
284 $_GET['month'] = ''; // hack for Pager
286 $options['year'] = '';
287 $options['month'] = '';
290 $pager = new ContribsPager( $target, $options['namespace'], $options['year'], $options['month'] );
291 if ( !$pager->getNumRows() ) {
292 $wgOut->addWikiMsg( 'nocontribs' );
296 # Show a message about slave lag, if applicable
297 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
298 $wgOut->showLagWarning( $lag );
301 '<p>' . $pager->getNavigationBar() . '</p>' .
303 '<p>' . $pager->getNavigationBar() . '</p>' );
305 # If there were contributions, and it was a valid user or IP, show
306 # the appropriate "footer" message - WHOIS tools, etc.
307 if( $target != 'newbies' ) {
308 $message = IP
::isIPAddress( $target )
309 ?
'sp-contributions-footer-anon'
310 : 'sp-contributions-footer';
313 $text = wfMsgNoTrans( $message, $target );
314 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
315 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
316 $wgOut->addWikiText( $text );
317 $wgOut->addHtml( '</div>' );
323 * Generates the subheading with links
324 * @param Title $nt Title object for the target
325 * @param integer $id User ID for the target
326 * @return String: appropriately-escaped HTML to be output literally
328 function contributionsSub( $nt, $id ) {
329 global $wgSysopUserBans, $wgLang, $wgUser;
331 $sk = $wgUser->getSkin();
334 $user = $nt->getText();
336 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
338 $talk = $nt->getTalkPage();
341 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
342 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && User
::isIP( $nt->getText() ) ) ) {
344 if( $wgUser->isAllowed( 'block' ) )
345 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
347 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
350 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
352 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
354 $links = implode( ' | ', $tools );
357 // Old message 'contribsub' had one parameter, but that doesn't work for
358 // languages that want to put the "for" bit right after $user but before
359 // $links. If 'contribsub' is around, use it for reverse compatibility,
360 // otherwise use 'contribsub2'.
361 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
362 return wfMsgHtml( 'contribsub2', $user, $links );
364 return wfMsgHtml( 'contribsub', "$user ($links)" );
369 * Generates the namespace selector form with hidden attributes.
370 * @param $options Array: the options to be included.
372 function contributionsForm( $options ) {
373 global $wgScript, $wgTitle, $wgRequest;
375 $options['title'] = $wgTitle->getPrefixedText();
376 if ( !isset( $options['target'] ) ) {
377 $options['target'] = '';
379 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
382 if ( !isset( $options['namespace'] ) ) {
383 $options['namespace'] = '';
386 if ( !isset( $options['contribs'] ) ) {
387 $options['contribs'] = 'user';
390 if ( !isset( $options['year'] ) ) {
391 $options['year'] = '';
394 if ( !isset( $options['month'] ) ) {
395 $options['month'] = '';
398 if ( $options['contribs'] == 'newbie' ) {
399 $options['target'] = '';
402 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
404 foreach ( $options as $name => $value ) {
405 if ( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
408 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
412 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
413 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'contribs' , 'newbie' , 'newbie', $options['contribs'] == 'newbie' ?
true : false ) . '<br />' .
414 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'contribs' , 'user', 'user', $options['contribs'] == 'user' ?
true : false ) . ' ' .
415 Xml
::input( 'target', 20, $options['target']) . ' '.
416 '<span style="white-space: nowrap">' .
417 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
418 Xml
::namespaceSelector( $options['namespace'], '' ) .
420 Xml
::openElement( 'p' ) .
421 '<span style="white-space: nowrap">' .
422 Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
423 Xml
::input( 'year', 4, $options['year'], array('id' => 'year', 'maxlength' => 4) ) .
426 '<span style="white-space: nowrap">' .
427 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
428 Xml
::monthSelector( $options['month'], -1 ) . ' '.
430 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
431 Xml
::closeElement( 'p' );
433 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
434 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
435 $f .= "<p>{$explain}</p>";
437 $f .= '</fieldset>' .
438 Xml
::closeElement( 'form' );