3 * Special:Contributions, show user contributions in a paged list
9 * Pager for Special:Contributions
10 * @ingroup SpecialPage Pager
12 class ContribsPager
extends ReverseChronologicalPager
{
13 public $mDefaultDirection = true;
14 var $messages, $target;
15 var $namespace = '', $mDb;
17 function __construct( $target, $namespace = false, $year = false, $month = false ) {
18 parent
::__construct();
19 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter minoreditletter' ) as $msg ) {
20 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escape') );
22 $this->target
= $target;
23 $this->namespace = $namespace;
25 $this->getDateCond( $year, $month );
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() );
40 'tables' => array( 'page', 'revision' ),
42 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page',
43 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user',
44 'rev_user_text', 'rev_parent_id', 'rev_deleted'
47 'options' => array( 'USE INDEX' => array('revision' => $index) )
49 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
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 getIndexField() {
76 return 'rev_timestamp';
79 function getStartBody() {
83 function getEndBody() {
88 * Generates each row in the contributions list.
90 * Contributions which are marked "top" are currently on top of the history.
91 * For these contributions, a [rollback] link is shown for users with roll-
92 * back privileges. The rollback link restores the most recent version that
93 * was not written by the target user.
95 * @todo This would probably look a lot nicer in a table.
97 function formatRow( $row ) {
98 wfProfileIn( __METHOD__
);
100 global $wgLang, $wgUser, $wgContLang;
102 $sk = $this->getSkin();
103 $rev = new Revision( $row );
105 $page = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
106 $link = $sk->makeKnownLinkObj( $page );
107 $difftext = $topmarktext = '';
108 if( $row->rev_id
== $row->page_latest
) {
109 $topmarktext .= '<strong>' . $this->messages
['uctop'] . '</strong>';
110 if( !$row->page_is_new
) {
111 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=0' ) . ')';
113 $difftext .= $this->messages
['newarticle'];
116 if( !$page->getUserPermissionsErrors( 'rollback', $wgUser )
117 && !$page->getUserPermissionsErrors( 'edit', $wgUser ) ) {
118 $topmarktext .= ' '.$sk->generateRollback( $rev );
122 # Is there a visible previous revision?
123 if( $rev->userCan(Revision
::DELETED_TEXT
) ) {
124 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=prev&oldid='.$row->rev_id
) . ')';
126 $difftext = '(' . $this->messages
['diff'] . ')';
128 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages
['hist'], 'action=history' ) . ')';
130 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
131 $d = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
133 if( $this->target
== 'newbies' ) {
134 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
135 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
140 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
141 $d = '<span class="history-deleted">' . $d . '</span>';
144 if( $rev->getParentId() === 0 ) {
145 $nflag = '<span class="newpage">' . $this->messages
['newpageletter'] . '</span>';
150 if( $row->rev_minor_edit
) {
151 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
156 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
157 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
158 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
160 // Let extensions add data
161 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
163 $ret = "<li>$ret</li>\n";
164 wfProfileOut( __METHOD__
);
169 * Get the Database object in use
173 public function getDatabase() {
180 * Special page "user contributions".
181 * Shows a list of the contributions of a user.
184 * @param $par String: (optional) user name of the user for which to show the contributions
186 function wfSpecialContributions( $par = null ) {
187 global $wgUser, $wgOut, $wgLang, $wgRequest;
191 if ( isset( $par ) && $par == 'newbies' ) {
193 $options['contribs'] = 'newbie';
194 } elseif ( isset( $par ) ) {
197 $target = $wgRequest->getVal( 'target' );
200 // check for radiobox
201 if ( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
203 $options['contribs'] = 'newbie';
206 if ( !strlen( $target ) ) {
207 $wgOut->addHTML( contributionsForm( '' ) );
211 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
212 $options['target'] = $target;
214 $nt = Title
::makeTitleSafe( NS_USER
, $target );
216 $wgOut->addHTML( contributionsForm( '' ) );
219 $id = User
::idFromName( $nt->getText() );
221 if ( $target != 'newbies' ) {
222 $target = $nt->getText();
223 $wgOut->setSubtitle( contributionsSub( $nt, $id ) );
224 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'contributions-title', $target ) ) );
226 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
227 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'sp-contributions-newbies-title' ) ) );
230 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
231 $options['namespace'] = intval( $ns );
233 $options['namespace'] = '';
236 // Allows reverts to have the bot flag in recent changes. It is just here to
237 // be passed in the form at the top of the page
238 if ( $wgUser->isAllowed( 'markbotedits' ) && $wgRequest->getBool( 'bot' ) ) {
239 $options['bot'] = '1';
242 $skip = $wgRequest->getText( 'offset' ) ||
$wgRequest->getText( 'dir' ) == 'prev';
243 # Offset overrides year/month selection
244 if ( ( $month = $wgRequest->getIntOrNull( 'month' ) ) !== null && $month !== -1 ) {
245 $options['month'] = intval( $month );
247 $options['month'] = '';
249 if ( ( $year = $wgRequest->getIntOrNull( 'year' ) ) !== null ) {
250 $options['year'] = intval( $year );
251 } else if( $options['month'] ) {
252 $thisMonth = intval( gmdate( 'n' ) );
253 $thisYear = intval( gmdate( 'Y' ) );
254 if( intval( $options['month'] ) > $thisMonth ) {
257 $options['year'] = $thisYear;
259 $options['year'] = '';
262 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
265 $options['year'] = '';
266 $options['month'] = '';
269 $wgOut->addHTML( contributionsForm( $options ) );
271 $pager = new ContribsPager( $target, $options['namespace'], $options['year'], $options['month'] );
272 if ( !$pager->getNumRows() ) {
273 $wgOut->addWikiMsg( 'nocontribs' );
277 # Show a message about slave lag, if applicable
278 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
279 $wgOut->showLagWarning( $lag );
282 '<p>' . $pager->getNavigationBar() . '</p>' .
284 '<p>' . $pager->getNavigationBar() . '</p>' );
286 # If there were contributions, and it was a valid user or IP, show
287 # the appropriate "footer" message - WHOIS tools, etc.
288 if( $target != 'newbies' ) {
289 $message = IP
::isIPAddress( $target )
290 ?
'sp-contributions-footer-anon'
291 : 'sp-contributions-footer';
294 $text = wfMsgNoTrans( $message, $target );
295 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
296 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
297 $wgOut->addWikiText( $text );
298 $wgOut->addHtml( '</div>' );
304 * Generates the subheading with links
305 * @param Title $nt Title object for the target
306 * @param integer $id User ID for the target
307 * @return String: appropriately-escaped HTML to be output literally
309 function contributionsSub( $nt, $id ) {
310 global $wgSysopUserBans, $wgLang, $wgUser;
312 $sk = $wgUser->getSkin();
315 $user = $nt->getText();
317 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
319 $talk = $nt->getTalkPage();
322 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
323 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && User
::isIP( $nt->getText() ) ) ) {
325 if( $wgUser->isAllowed( 'block' ) )
326 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
328 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
331 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
333 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
335 $links = implode( ' | ', $tools );
338 // Old message 'contribsub' had one parameter, but that doesn't work for
339 // languages that want to put the "for" bit right after $user but before
340 // $links. If 'contribsub' is around, use it for reverse compatibility,
341 // otherwise use 'contribsub2'.
342 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
343 return wfMsgHtml( 'contribsub2', $user, $links );
345 return wfMsgHtml( 'contribsub', "$user ($links)" );
350 * Generates the namespace selector form with hidden attributes.
351 * @param $options Array: the options to be included.
353 function contributionsForm( $options ) {
354 global $wgScript, $wgTitle, $wgRequest;
356 $options['title'] = $wgTitle->getPrefixedText();
357 if ( !isset( $options['target'] ) ) {
358 $options['target'] = '';
360 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
363 if ( !isset( $options['namespace'] ) ) {
364 $options['namespace'] = '';
367 if ( !isset( $options['contribs'] ) ) {
368 $options['contribs'] = 'user';
371 if ( !isset( $options['year'] ) ) {
372 $options['year'] = '';
375 if ( !isset( $options['month'] ) ) {
376 $options['month'] = '';
379 if ( $options['contribs'] == 'newbie' ) {
380 $options['target'] = '';
383 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
385 foreach ( $options as $name => $value ) {
386 if ( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
389 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
393 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
394 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'contribs' , 'newbie' , 'newbie', $options['contribs'] == 'newbie' ?
true : false ) . '<br />' .
395 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'contribs' , 'user', 'user', $options['contribs'] == 'user' ?
true : false ) . ' ' .
396 Xml
::input( 'target', 20, $options['target']) . ' '.
397 '<span style="white-space: nowrap">' .
398 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
399 Xml
::namespaceSelector( $options['namespace'], '' ) .
401 Xml
::openElement( 'p' ) .
402 '<span style="white-space: nowrap">' .
403 Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
404 Xml
::input( 'year', 4, $options['year'], array('id' => 'year', 'maxlength' => 4) ) .
407 '<span style="white-space: nowrap">' .
408 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
409 Xml
::monthSelector( $options['month'], -1 ) . ' '.
411 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
412 Xml
::closeElement( 'p' );
414 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
415 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
416 $f .= "<p>{$explain}</p>";
418 $f .= '</fieldset>' .
419 Xml
::closeElement( 'form' );