eb857cff509d0c2d0b8c0b88c8c13466f4455f00
3 * @addtogroup SpecialPage
7 var $username, $offset, $limit, $namespace;
12 * @param $username Username as a string
14 function ContribsFinder( $username ) {
15 $this->username
= $username;
16 $this->namespace = false;
17 $this->dbr
= wfGetDB( DB_SLAVE
, 'contributions' );
20 function setNamespace( $ns ) {
21 $this->namespace = $ns;
24 function setLimit( $limit ) {
25 $this->limit
= $limit;
28 function setOffset( $offset ) {
29 $this->offset
= $offset;
33 * Get timestamp of either first or last contribution made by the user.
34 * @todo Maybe it should be private ?
35 * @param $dir string 'ASC' or 'DESC'.
36 * @return Revision timestamp (rev_timestamp).
38 function getEditLimit( $dir ) {
39 list( $index, $usercond ) = $this->getUserCond();
40 $nscond = $this->getNamespaceCond();
41 $use_index = $this->dbr
->useIndexClause( $index );
42 list( $revision, $page) = $this->dbr
->tableNamesN( 'revision', 'page' );
43 $sql = "SELECT rev_timestamp " .
44 " FROM $page,$revision $use_index " .
45 " WHERE rev_page=page_id AND $usercond $nscond" .
46 " ORDER BY rev_timestamp $dir LIMIT 1";
48 $res = $this->dbr
->query( $sql, __METHOD__
);
49 $row = $this->dbr
->fetchObject( $res );
51 return wfTimestamp( TS_MW
, $row->rev_timestamp
);
58 * Get timestamps of first and last contributions made by the user.
59 * @return Array containing first rev_timestamp and last rev_timestamp.
61 function getEditLimits() {
63 $this->getEditLimit( "ASC" ),
64 $this->getEditLimit( "DESC" )
68 function getUserCond() {
71 if ( $this->username
== 'newbies' ) {
72 $max = $this->dbr
->selectField( 'user', 'max(user_id)', false, 'make_sql' );
73 $condition = '>' . (int)($max - $max / 100);
76 if ( $condition == '' ) {
77 $condition = ' rev_user_text=' . $this->dbr
->addQuotes( $this->username
);
78 $index = 'usertext_timestamp';
80 $condition = ' rev_user '.$condition ;
81 $index = 'user_timestamp';
83 return array( $index, $condition );
86 function getNamespaceCond() {
87 if ( $this->namespace !== false )
88 return ' AND page_namespace = ' . (int)$this->namespace;
93 * @return Timestamp of first entry in previous page.
95 function getPreviousOffsetForPaging() {
96 list( $index, $usercond ) = $this->getUserCond();
97 $nscond = $this->getNamespaceCond();
99 $use_index = $this->dbr
->useIndexClause( $index );
100 list( $page, $revision ) = $this->dbr
->tableNamesN( 'page', 'revision' );
102 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
103 "WHERE page_id = rev_page AND rev_timestamp > '" . $this->dbr
->timestamp( $this->offset
) . "' AND " .
105 $sql .= " ORDER BY rev_timestamp ASC";
106 $sql = $this->dbr
->limitResult( $sql, $this->limit
, 0 );
107 $res = $this->dbr
->query( $sql );
109 $numRows = $this->dbr
->numRows( $res );
111 $this->dbr
->dataSeek( $res, $numRows - 1 );
112 $row = $this->dbr
->fetchObject( $res );
113 $offset = wfTimestamp( TS_MW
, $row->rev_timestamp
);
117 $this->dbr
->freeResult( $res );
122 * @return Timestamp of first entry in next page.
124 function getFirstOffsetForPaging() {
125 list( $index, $usercond ) = $this->getUserCond();
126 $use_index = $this->dbr
->useIndexClause( $index );
127 list( $page, $revision ) = $this->dbr
->tableNamesN( 'page', 'revision' );
128 $nscond = $this->getNamespaceCond();
129 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
130 "WHERE page_id = rev_page AND " .
132 $sql .= " ORDER BY rev_timestamp ASC";
133 $sql = $this->dbr
->limitResult( $sql, $this->limit
, 0 );
134 $res = $this->dbr
->query( $sql );
136 $numRows = $this->dbr
->numRows( $res );
138 $this->dbr
->dataSeek( $res, $numRows - 1 );
139 $row = $this->dbr
->fetchObject( $res );
140 $offset = wfTimestamp( TS_MW
, $row->rev_timestamp
);
144 $this->dbr
->freeResult( $res );
148 /* private */ function makeSql() {
151 list( $page, $revision ) = $this->dbr
->tableNamesN( 'page', 'revision' );
152 list( $index, $userCond ) = $this->getUserCond();
155 $offsetQuery = "AND rev_timestamp < '" . $this->dbr
->timestamp($this->offset
) . "'";
157 $nscond = $this->getNamespaceCond();
158 $use_index = $this->dbr
->useIndexClause( $index );
160 'page_namespace,page_title,page_is_new,page_latest,'.
161 'rev_id,rev_page,rev_text_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user,rev_user_text,'.
163 "FROM $page,$revision $use_index " .
164 "WHERE page_id=rev_page AND $userCond $nscond $offsetQuery " .
165 'ORDER BY rev_timestamp DESC';
166 $sql = $this->dbr
->limitResult( $sql, $this->limit
, 0 );
171 * This do the search for the user given when creating the object.
172 * It should probably be the only public function in this class.
173 * @return Array of contributions.
177 $res = $this->dbr
->query( $this->makeSql(), __METHOD__
);
178 while ( $c = $this->dbr
->fetchObject( $res ) )
180 $this->dbr
->freeResult( $res );
186 * Special page "user contributions".
187 * Shows a list of the contributions of a user.
190 * @param $par String: (optional) user name of the user for which to show the contributions
192 function wfSpecialContributions( $par = null ) {
193 global $wgUser, $wgOut, $wgLang, $wgRequest;
197 if ( isset( $par ) && $par == 'newbies' ) {
199 $options['contribs'] = 'newbie';
200 } elseif ( isset( $par ) ) {
203 $target = ucfirst( $wgRequest->getVal( 'target' ) );
206 // check for radiobox
207 if ( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
209 $options['contribs'] = 'newbie';
212 if ( !strlen( $target ) ) {
213 $wgOut->addHTML( contributionsForm( '' ) );
217 $nt = Title
::newFromURL( $target );
219 $wgOut->addHTML( contributionsForm( '' ) );
223 list( $options['limit'], $options['offset']) = wfCheckLimits();
224 $options['offset'] = $wgRequest->getVal( 'offset' );
225 /* Offset must be an integral. */
226 if ( !strlen( $options['offset'] ) ||
!preg_match( '/^[0-9]+$/', $options['offset'] ) ) {
227 $options['offset'] = '';
230 $title = SpecialPage
::getTitleFor( 'Contributions' );
231 $options['target'] = $target;
233 $nt = Title
::makeTitle( NS_USER
, $nt->getDBkey() );
234 $finder = new ContribsFinder( ( $target == 'newbies' ) ?
'newbies' : $nt->getText() );
235 $finder->setLimit( $options['limit'] );
236 $finder->setOffset( $options['offset'] );
238 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
239 $options['namespace'] = intval( $ns );
240 $finder->setNamespace( $options['namespace'] );
242 $options['namespace'] = '';
245 if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) {
246 $options['bot'] = '1';
249 if ( $wgRequest->getText( 'go' ) == 'prev' ) {
250 $offset = $finder->getPreviousOffsetForPaging();
251 if ( $offset !== false ) {
252 $options['offset'] = $offset;
253 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
254 $wgOut->redirect( $prevurl );
259 if ( $wgRequest->getText( 'go' ) == 'first' && $target != 'newbies') {
260 $offset = $finder->getFirstOffsetForPaging();
261 if ( $offset !== false ) {
262 $options['offset'] = $offset;
263 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
264 $wgOut->redirect( $prevurl );
269 if ( $target == 'newbies' ) {
270 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
272 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub( $nt ) ) );
275 $id = User
::idFromName( $nt->getText() );
276 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
278 $wgOut->addHTML( contributionsForm( $options) );
280 $contribs = $finder->find();
282 if ( count( $contribs ) == 0) {
283 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
287 list( $early, $late ) = $finder->getEditLimits();
288 $lastts = count( $contribs ) ?
wfTimestamp( TS_MW
, $contribs[count( $contribs ) - 1]->rev_timestamp
) : 0;
289 $atstart = ( !count( $contribs ) ||
$late == wfTimestamp( TS_MW
, $contribs[0]->rev_timestamp
) );
290 $atend = ( !count( $contribs ) ||
$early == $lastts );
292 // These four are defaults
293 $newestlink = wfMsgHtml( 'sp-contributions-newest' );
294 $oldestlink = wfMsgHtml( 'sp-contributions-oldest' );
295 $newerlink = wfMsgHtml( 'sp-contributions-newer', $options['limit'] );
296 $olderlink = wfMsgHtml( 'sp-contributions-older', $options['limit'] );
299 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => '' ), $options ) );
300 $newestlink = "<a href=\"$stuff\">$newestlink</a>";
301 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'prev' ), $options ) );
302 $newerlink = "<a href=\"$stuff\">$newerlink</a>";
306 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'first' ), $options ) );
307 $oldestlink = "<a href=\"$stuff\">$oldestlink</a>";
308 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => $lastts ), $options ) );
309 $olderlink = "<a href=\"$stuff\">$olderlink</a>";
312 if ( $target == 'newbies' ) {
313 $firstlast ="($newestlink)";
315 $firstlast = "($newestlink | $oldestlink)";
319 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
320 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'limit' => $num ), $options ) );
321 $urls[] = "<a href=\"$stuff\">".$wgLang->formatNum( $num )."</a>";
323 $bits = implode( $urls, ' | ' );
325 $prevnextbits = $firstlast .' '. wfMsgHtml( 'viewprevnext', $newerlink, $olderlink, $bits );
327 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
329 $wgOut->addHTML( "<ul>\n" );
331 $sk = $wgUser->getSkin();
332 foreach ( $contribs as $contrib )
333 $wgOut->addHTML( ucListEdit( $sk, $contrib ) );
335 $wgOut->addHTML( "</ul>\n" );
336 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
338 # If there were contributions, and it was a valid user or IP, show
339 # the appropriate "footer" message - WHOIS tools, etc.
340 if( count( $contribs ) > 0 && $target != 'newbies' && $nt instanceof Title
) {
341 $message = IP
::isIPAddress( $target )
342 ?
'sp-contributions-footer-anon'
343 : 'sp-contributions-footer';
344 $text = wfMsg( $message, $target );
345 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
346 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
347 $wgOut->addWikiText( wfMsg( $message, $target ) );
348 $wgOut->addHtml( '</div>' );
355 * Generates the subheading with links
356 * @param $nt @see Title object for the target
358 function contributionsSub( $nt ) {
359 global $wgSysopUserBans, $wgLang, $wgUser;
361 $sk = $wgUser->getSkin();
362 $id = User
::idFromName( $nt->getText() );
365 $ul = $nt->getText();
367 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
369 $talk = $nt->getTalkPage();
372 $tools[] = $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK
) );
373 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && User
::isIP( $nt->getText() ) ) ) {
375 if( $wgUser->isAllowed( 'block' ) )
376 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
378 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
381 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
382 $ul .= ' (' . implode( ' | ', $tools ) . ')';
388 * Generates the namespace selector form with hidden attributes.
389 * @param $options Array: the options to be included.
391 function contributionsForm( $options ) {
392 global $wgScript, $wgTitle, $wgRequest;
394 $options['title'] = $wgTitle->getPrefixedText();
395 if ( !isset( $options['target'] ) ) {
396 $options['target'] = '';
398 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
401 if ( !isset( $options['namespace'] ) ) {
402 $options['namespace'] = '';
405 if ( !isset( $options['contribs'] ) ) {
406 $options['contribs'] = 'user';
409 if ( $options['contribs'] == 'newbie' ) {
410 $options['target'] = '';
413 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
415 foreach ( $options as $name => $value ) {
416 if( $name === 'namespace' ||
$name === 'target' ||
$name === 'contribs' ) continue;
417 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
421 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
422 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'contribs' , 'newbie' , 'newbie', $options['contribs'] == 'newbie' ?
true : false ) . '<br />' .
423 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'contribs' , 'user', 'user', $options['contribs'] == 'user' ?
true : false ) . ' ' .
424 Xml
::input( 'target', 20, $options['target']) . ' '.
425 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
426 Xml
::namespaceSelector( $options['namespace'], '' ) .
427 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
429 Xml
::closeElement( 'form' );
434 * Generates each row in the contributions list.
436 * Contributions which are marked "top" are currently on top of the history.
437 * For these contributions, a [rollback] link is shown for users with sysop
438 * privileges. The rollback link restores the most recent version that was not
439 * written by the target user.
441 * @todo This would probably look a lot nicer in a table.
443 function ucListEdit( $sk, $row ) {
444 $fname = 'ucListEdit';
445 wfProfileIn( $fname );
447 global $wgLang, $wgUser, $wgRequest;
449 if( !isset( $messages ) ) {
450 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
451 $messages[$msg] = wfMsgExt( $msg, array( 'escape') );
455 $rev = new Revision( $row );
457 $page = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
458 $link = $sk->makeKnownLinkObj( $page );
459 $difftext = $topmarktext = '';
460 if( $row->rev_id
== $row->page_latest
) {
461 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
462 if( !$row->page_is_new
) {
463 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=0' ) . ')';
465 $difftext .= $messages['newarticle'];
468 if( $wgUser->isAllowed( 'rollback' ) ) {
469 $topmarktext .= ' '.$sk->generateRollback( $rev );
473 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
474 $difftext = '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=prev&oldid='.$row->rev_id
) . ')';
476 $difftext = '(' . $messages['diff'] . ')';
478 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
480 $comment = $sk->revComment( $rev );
481 $d = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
483 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
484 $d = '<span class="history-deleted">' . $d . '</span>';
487 if( $row->rev_minor_edit
) {
488 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
493 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
494 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
495 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
497 $ret = "<li>$ret</li>\n";
498 wfProfileOut( $fname );