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
;
33 $query['month'] = $this->mMonth
;
34 $query['year'] = $this->mYear
;
38 function getQueryInfo() {
39 list( $index, $userCond ) = $this->getUserCond();
40 $conds = array_merge( array('page_id=rev_page'), $userCond, $this->getNamespaceCond() );
42 'tables' => array( 'page', 'revision' ),
44 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page',
45 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user',
46 'rev_user_text', 'rev_parent_id', 'rev_deleted'
49 'options' => array( 'USE INDEX' => array('revision' => $index) )
51 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
55 function getUserCond() {
58 if ( $this->target
== 'newbies' ) {
59 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
60 $condition[] = 'rev_user >' . (int)($max - $max / 100);
61 $index = 'user_timestamp';
63 $condition['rev_user_text'] = $this->target
;
64 $index = 'usertext_timestamp';
66 return array( $index, $condition );
69 function getNamespaceCond() {
70 if ( $this->namespace !== '' ) {
71 return array( 'page_namespace' => (int)$this->namespace );
77 function getIndexField() {
78 return 'rev_timestamp';
81 function getStartBody() {
85 function getEndBody() {
90 * Generates each row in the contributions list.
92 * Contributions which are marked "top" are currently on top of the history.
93 * For these contributions, a [rollback] link is shown for users with roll-
94 * back privileges. The rollback link restores the most recent version that
95 * was not written by the target user.
97 * @todo This would probably look a lot nicer in a table.
99 function formatRow( $row ) {
100 wfProfileIn( __METHOD__
);
102 global $wgLang, $wgUser, $wgContLang;
104 $sk = $this->getSkin();
105 $rev = new Revision( $row );
107 $page = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
108 $link = $sk->makeKnownLinkObj( $page );
109 $difftext = $topmarktext = '';
110 if( $row->rev_id
== $row->page_latest
) {
111 $topmarktext .= '<strong>' . $this->messages
['uctop'] . '</strong>';
112 if( !$row->page_is_new
) {
113 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=0' ) . ')';
115 $difftext .= $this->messages
['newarticle'];
118 if( !$page->getUserPermissionsErrors( 'rollback', $wgUser )
119 && !$page->getUserPermissionsErrors( 'edit', $wgUser ) ) {
120 $topmarktext .= ' '.$sk->generateRollback( $rev );
124 # Is there a visible previous revision?
125 if( $rev->userCan(Revision
::DELETED_TEXT
) ) {
126 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages
['diff'], 'diff=prev&oldid='.$row->rev_id
) . ')';
128 $difftext = '(' . $this->messages
['diff'] . ')';
130 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages
['hist'], 'action=history' ) . ')';
132 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
133 $d = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
135 if( $this->target
== 'newbies' ) {
136 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
137 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) . ') ';
142 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
143 $d = '<span class="history-deleted">' . $d . '</span>';
146 if( $rev->getParentId() === 0 ) {
147 $nflag = '<span class="newpage">' . $this->messages
['newpageletter'] . '</span>';
152 if( $row->rev_minor_edit
) {
153 $mflag = '<span class="minor">' . $this->messages
['minoreditletter'] . '</span> ';
158 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink}{$comment} {$topmarktext}";
159 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
160 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
162 // Let extensions add data
163 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
165 $ret = "<li>$ret</li>\n";
166 wfProfileOut( __METHOD__
);
171 * Get the Database object in use
175 public function getDatabase() {
182 * Special page "user contributions".
183 * Shows a list of the contributions of a user.
186 * @param $par String: (optional) user name of the user for which to show the contributions
188 function wfSpecialContributions( $par = null ) {
189 global $wgUser, $wgOut, $wgLang, $wgRequest;
193 if ( isset( $par ) && $par == 'newbies' ) {
195 $options['contribs'] = 'newbie';
196 } elseif ( isset( $par ) ) {
199 $target = $wgRequest->getVal( 'target' );
202 // check for radiobox
203 if ( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
205 $options['contribs'] = 'newbie';
208 if ( !strlen( $target ) ) {
209 $wgOut->addHTML( contributionsForm( '' ) );
213 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
214 $options['target'] = $target;
216 $nt = Title
::makeTitleSafe( NS_USER
, $target );
218 $wgOut->addHTML( contributionsForm( '' ) );
221 $id = User
::idFromName( $nt->getText() );
223 if ( $target != 'newbies' ) {
224 $target = $nt->getText();
225 $wgOut->setSubtitle( contributionsSub( $nt, $id ) );
227 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
230 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
231 $options['namespace'] = intval( $ns );
233 $options['namespace'] = '';
236 $skip = $wgRequest->getText( 'offset' ) ||
$wgRequest->getText( 'dir' ) == 'prev';
237 # Offset overrides year/month selection
238 if ( ( $month = $wgRequest->getIntOrNull( 'month' ) ) !== null && $month !== -1 ) {
239 $options['month'] = intval( $month );
241 $options['month'] = '';
243 if ( ( $year = $wgRequest->getIntOrNull( 'year' ) ) !== null ) {
244 $options['year'] = intval( $year );
245 } else if( $options['month'] ) {
246 $thisMonth = intval( gmdate( 'n' ) );
247 $thisYear = intval( gmdate( 'Y' ) );
248 if( intval( $options['month'] ) > $thisMonth ) {
251 $options['year'] = $thisYear;
253 $options['year'] = '';
256 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
259 $options['year'] = '';
260 $options['month'] = '';
263 $wgOut->addHTML( contributionsForm( $options ) );
265 $pager = new ContribsPager( $target, $options['namespace'], $options['year'], $options['month'] );
266 if ( !$pager->getNumRows() ) {
267 $wgOut->addWikiMsg( 'nocontribs' );
271 # Show a message about slave lag, if applicable
272 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
273 $wgOut->showLagWarning( $lag );
276 '<p>' . $pager->getNavigationBar() . '</p>' .
278 '<p>' . $pager->getNavigationBar() . '</p>' );
280 # If there were contributions, and it was a valid user or IP, show
281 # the appropriate "footer" message - WHOIS tools, etc.
282 if( $target != 'newbies' ) {
283 $message = IP
::isIPAddress( $target )
284 ?
'sp-contributions-footer-anon'
285 : 'sp-contributions-footer';
288 $text = wfMsgNoTrans( $message, $target );
289 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
290 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
291 $wgOut->addWikiText( $text );
292 $wgOut->addHtml( '</div>' );
298 * Generates the subheading with links
299 * @param Title $nt Title object for the target
300 * @param integer $id User ID for the target
301 * @return String: appropriately-escaped HTML to be output literally
303 function contributionsSub( $nt, $id ) {
304 global $wgSysopUserBans, $wgLang, $wgUser;
306 $sk = $wgUser->getSkin();
309 $user = $nt->getText();
311 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
313 $talk = $nt->getTalkPage();
316 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
317 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && User
::isIP( $nt->getText() ) ) ) {
319 if( $wgUser->isAllowed( 'block' ) )
320 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
322 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
325 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
327 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
329 $links = implode( ' | ', $tools );
332 // Old message 'contribsub' had one parameter, but that doesn't work for
333 // languages that want to put the "for" bit right after $user but before
334 // $links. If 'contribsub' is around, use it for reverse compatibility,
335 // otherwise use 'contribsub2'.
336 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
337 return wfMsgHtml( 'contribsub2', $user, $links );
339 return wfMsgHtml( 'contribsub', "$user ($links)" );
344 * Generates the namespace selector form with hidden attributes.
345 * @param $options Array: the options to be included.
347 function contributionsForm( $options ) {
348 global $wgScript, $wgTitle, $wgRequest;
350 $options['title'] = $wgTitle->getPrefixedText();
351 if ( !isset( $options['target'] ) ) {
352 $options['target'] = '';
354 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
357 if ( !isset( $options['namespace'] ) ) {
358 $options['namespace'] = '';
361 if ( !isset( $options['contribs'] ) ) {
362 $options['contribs'] = 'user';
365 if ( !isset( $options['year'] ) ) {
366 $options['year'] = '';
369 if ( !isset( $options['month'] ) ) {
370 $options['month'] = '';
373 if ( $options['contribs'] == 'newbie' ) {
374 $options['target'] = '';
377 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
379 foreach ( $options as $name => $value ) {
380 if ( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
383 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
387 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
388 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'contribs' , 'newbie' , 'newbie', $options['contribs'] == 'newbie' ?
true : false ) . '<br />' .
389 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'contribs' , 'user', 'user', $options['contribs'] == 'user' ?
true : false ) . ' ' .
390 Xml
::input( 'target', 20, $options['target']) . ' '.
391 '<span style="white-space: nowrap">' .
392 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
393 Xml
::namespaceSelector( $options['namespace'], '' ) .
395 Xml
::openElement( 'p' ) .
396 '<span style="white-space: nowrap">' .
397 Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
398 Xml
::input( 'year', 4, $options['year'], array('id' => 'year', 'maxlength' => 4) ) .
401 '<span style="white-space: nowrap">' .
402 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
403 Xml
::monthSelector( $options['month'], -1 ) . ' '.
405 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
406 Xml
::closeElement( 'p' );
408 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
409 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
410 $f .= "<p>{$explain}</p>";
412 $f .= '</fieldset>' .
413 Xml
::closeElement( 'form' );