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 = 'rev_user >' . (int)($max - $max / 100);
74 } else if ( IP
::isIPv4( $this->username
) && preg_match("/\/(24|16)$/", $this->username
, $matches) ) {
75 $abcd = explode( ".", $this->username
);
76 if( $matches[1] == 24 ) $ipmask = $abcd[0] . '.' . $abcd[1] . '.' . $abcd[2] . '.%';
77 else $ipmask=$abcd[0] . '.' . $abcd[1] . '.%';
78 $condition = 'rev_user_text LIKE ' . $this->dbr
->addQuotes($ipmask);
79 } else if ( IP
::isIPv6( $this->username
) && preg_match("/^(64|80|96|112)$/", $this->username
) ) {
80 $abcdefgh = explode( ":", IP
::sanitizeIP($this->username
) );
81 $abcd = implode( ":", array_slice($abcdefgh, 0, 4) );
82 switch( $matches[1] ) {
84 $ipmask = $abcd . ':' . $abcd[4] . ':' . $abcd[5] . ':' . $abcd[6] . ':%';
87 $ipmask = $abcd . ':' . $abcd[4] . ':' . $abcd[5] . ':%';
90 $ipmask = $abcd . ':' . $abcd[4] . ':%';
93 $ipmask = $abcd . ':%';
96 $condition = 'rev_user_text LIKE ' . $this->dbr
->addQuotes($ipmask);
99 else if ( IP
::isIPv6( $this->username
) ) {
100 # All stored IPs should be sanitized from now on, check for exact matches for reverse compatibility
101 $condition = '(rev_user_text=' . $this->dbr
->addQuotes(IP
::sanitizeIP($this->username
)) . ' OR rev_user_text=' . $this->dbr
->addQuotes($this->username
) . ')';
104 if ( $condition == '' ) {
105 $condition = ' rev_user_text=' . $this->dbr
->addQuotes( $this->username
);
106 $index = 'usertext_timestamp';
108 #$condition = ' rev_user '.$condition ;
109 $index = 'user_timestamp';
111 return array( $index, $condition );
114 function getNamespaceCond() {
115 if ( $this->namespace !== false )
116 return ' AND page_namespace = ' . (int)$this->namespace;
121 * @return Timestamp of first entry in previous page.
123 function getPreviousOffsetForPaging() {
124 list( $index, $usercond ) = $this->getUserCond();
125 $nscond = $this->getNamespaceCond();
127 $use_index = $this->dbr
->useIndexClause( $index );
128 list( $page, $revision ) = $this->dbr
->tableNamesN( 'page', 'revision' );
130 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
131 "WHERE page_id = rev_page AND rev_timestamp > '" . $this->dbr
->timestamp($this->offset
) . "' AND " .
133 $sql .= " ORDER BY rev_timestamp ASC";
134 $sql = $this->dbr
->limitResult( $sql, $this->limit
, 0 );
135 $res = $this->dbr
->query( $sql );
137 $numRows = $this->dbr
->numRows( $res );
139 $this->dbr
->dataSeek( $res, $numRows - 1 );
140 $row = $this->dbr
->fetchObject( $res );
141 $offset = wfTimestamp( TS_MW
, $row->rev_timestamp
);
145 $this->dbr
->freeResult( $res );
150 * @return Timestamp of first entry in next page.
152 function getFirstOffsetForPaging() {
153 list( $index, $usercond ) = $this->getUserCond();
154 $use_index = $this->dbr
->useIndexClause( $index );
155 list( $page, $revision ) = $this->dbr
->tableNamesN( 'page', 'revision' );
156 $nscond = $this->getNamespaceCond();
157 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
158 "WHERE page_id = rev_page AND " .
160 $sql .= " ORDER BY rev_timestamp ASC";
161 $sql = $this->dbr
->limitResult( $sql, $this->limit
, 0 );
162 $res = $this->dbr
->query( $sql );
164 $numRows = $this->dbr
->numRows( $res );
166 $this->dbr
->dataSeek( $res, $numRows - 1 );
167 $row = $this->dbr
->fetchObject( $res );
168 $offset = wfTimestamp( TS_MW
, $row->rev_timestamp
);
172 $this->dbr
->freeResult( $res );
176 /* private */ function makeSql() {
179 list( $page, $revision ) = $this->dbr
->tableNamesN( 'page', 'revision' );
180 list( $index, $userCond ) = $this->getUserCond();
182 if ( $this->offset
) {
183 $offsetQuery = "AND rev_timestamp < '" . $this->dbr
->timestamp($this->offset
) . "'";
186 $nscond = $this->getNamespaceCond();
187 $use_index = $this->dbr
->useIndexClause( $index );
189 'page_namespace,page_title,page_is_new,page_latest,'.
190 join(',', Revision
::selectFields()).
191 " FROM $page,$revision $use_index " .
192 "WHERE page_id=rev_page AND $userCond $nscond $offsetQuery " .
193 'ORDER BY rev_timestamp DESC';
194 $sql = $this->dbr
->limitResult( $sql, $this->limit
, 0 );
199 * This do the search for the user given when creating the object.
200 * It should probably be the only public function in this class.
201 * @return Array of contributions.
205 $res = $this->dbr
->query( $this->makeSql(), __METHOD__
);
206 while ( $c = $this->dbr
->fetchObject( $res ) )
208 $this->dbr
->freeResult( $res );
214 * Special page "user contributions".
215 * Shows a list of the contributions of a user.
218 * @param $par String: (optional) user name of the user for which to show the contributions
220 function wfSpecialContributions( $par = null ) {
221 global $wgUser, $wgOut, $wgLang, $wgRequest;
223 $target = isset( $par ) ?
$par : $wgRequest->getVal( 'target' );
224 $radiobox = $wgRequest->getVal( 'newbie' );
226 // check for radiobox
227 if ( $radiobox == 'contribs-newbie' ) $target = 'newbies';
229 if ( !strlen( $target ) ) {
230 $wgOut->addHTML( contributionsForm( '' ) );
234 $nt = Title
::newFromURL( $target );
236 $wgOut->addHTML( contributionsForm( '' ) );
242 list( $options['limit'], $options['offset']) = wfCheckLimits();
243 $options['offset'] = $wgRequest->getVal( 'offset' );
245 /* Offset must be numeric or an empty string */
246 if ( !strlen( $options['offset'] ) ||
!preg_match( '/^[0-9]+$/', $options['offset'] ) )
247 $options['offset'] = '';
249 $title = SpecialPage
::getTitleFor( 'Contributions' );
250 $options['target'] = $target;
252 $nt = Title
::makeTitle( NS_USER
, $nt->getDBkey() );
253 $finder = new ContribsFinder( ( $target == 'newbies' ) ?
'newbies' : $nt->getText() );
254 $finder->setLimit( $options['limit'] );
255 $finder->setOffset( $options['offset'] );
257 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
258 $options['namespace'] = intval( $ns );
259 $finder->setNamespace( $options['namespace'] );
261 $options['namespace'] = '';
264 if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) {
265 $options['bot'] = '1';
268 if ( $wgRequest->getText( 'go' ) == 'prev' ) {
269 $offset = $finder->getPreviousOffsetForPaging();
270 if ( $offset !== false ) {
271 $options['offset'] = $offset;
272 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
273 $wgOut->redirect( $prevurl );
278 if ( $wgRequest->getText( 'go' ) == 'first' && $target != 'newbies') {
279 $offset = $finder->getFirstOffsetForPaging();
280 if ( $offset !== false ) {
281 $options['offset'] = $offset;
282 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
283 $wgOut->redirect( $prevurl );
288 if ( $target == 'newbies' ) {
289 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
290 } else if ( preg_match( "/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/(24|16)/", $target ) ) {
291 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $target ) );
293 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub( $nt ) ) );
296 $id = User
::idFromName( $nt->getText() );
297 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
299 $wgOut->addHTML( contributionsForm( $options) );
301 $contribs = $finder->find();
303 if ( count( $contribs ) == 0) {
304 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
308 list( $early, $late ) = $finder->getEditLimits();
309 $lastts = count( $contribs ) ?
wfTimestamp( TS_MW
, $contribs[count( $contribs ) - 1]->rev_timestamp
) : 0;
310 $atstart = ( !count( $contribs ) ||
$late == (wfTimestamp( TS_MW
, $contribs[0]->rev_timestamp
) ));
311 $atend = ( !count( $contribs ) ||
$early == $lastts );
313 // These four are defaults
314 $newestlink = wfMsgHtml( 'sp-contributions-newest' );
315 $oldestlink = wfMsgHtml( 'sp-contributions-oldest' );
316 $newerlink = wfMsgHtml( 'sp-contributions-newer', $options['limit'] );
317 $olderlink = wfMsgHtml( 'sp-contributions-older', $options['limit'] );
320 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => '' ), $options ) );
321 $newestlink = "<a href=\"$stuff\">$newestlink</a>";
322 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'prev' ), $options ) );
323 $newerlink = "<a href=\"$stuff\">$newerlink</a>";
327 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'first' ), $options ) );
328 $oldestlink = "<a href=\"$stuff\">$oldestlink</a>";
329 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => $lastts ), $options ) );
330 $olderlink = "<a href=\"$stuff\">$olderlink</a>";
333 if ( $target == 'newbies' ) {
334 $firstlast ="($newestlink)";
336 $firstlast = "($newestlink | $oldestlink)";
340 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
341 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'limit' => $num ), $options ) );
342 $urls[] = "<a href=\"$stuff\">".$wgLang->formatNum( $num )."</a>";
344 $bits = implode( $urls, ' | ' );
346 $prevnextbits = $firstlast .' '. wfMsgHtml( 'viewprevnext', $newerlink, $olderlink, $bits );
348 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
350 $wgOut->addHTML( "<ul>\n" );
352 $sk = $wgUser->getSkin();
353 foreach ( $contribs as $contrib )
354 $wgOut->addHTML( ucListEdit( $sk, $contrib ) );
356 $wgOut->addHTML( "</ul>\n" );
357 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
359 # If there were contributions, and it was a valid user or IP, show
360 # the appropriate "footer" message - WHOIS tools, etc.
361 if( count( $contribs ) > 0 && $target != 'newbies' && $nt instanceof Title
) {
362 $message = IP
::isIPAddress( $target )
363 ?
'sp-contributions-footer-anon'
364 : 'sp-contributions-footer';
365 $text = wfMsg( $message, $target );
366 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
367 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
368 $wgOut->addWikiText( wfMsg( $message, $target ) );
369 $wgOut->addHtml( '</div>' );
376 * Generates the subheading with links
377 * @param $nt @see Title object for the target
379 function contributionsSub( $nt ) {
380 global $wgSysopUserBans, $wgLang, $wgUser;
382 $sk = $wgUser->getSkin();
383 $id = User
::idFromName( $nt->getText() );
386 $ul = $nt->getText();
388 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
390 $talk = $nt->getTalkPage();
393 $tools[] = $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK
) );
394 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && User
::isIP( $nt->getText() ) ) ) {
396 if( $wgUser->isAllowed( 'block' ) )
397 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
399 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
402 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
403 $ul .= ' (' . implode( ' | ', $tools ) . ')';
409 * Generates the namespace selector form with hidden attributes.
410 * @param $options Array: the options to be included.
412 function contributionsForm( $options ) {
413 global $wgScript, $wgTitle, $wgRequest;
415 $options['title'] = $wgTitle->getPrefixedText();
416 if (!isset($options['target']))
417 $options['target'] = '';
418 if (!isset($options['namespace']))
419 $options['namespace'] = 0;
421 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
423 foreach ( $options as $name => $value ) {
424 if( $name === 'namespace') continue;
425 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
429 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
430 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'newbie' , 'contribs-newbie' , 'contribs-newbie', 'contribs-newbie' ) . '<br />' .
431 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'newbie' , 'contribs-all', 'contribs-all', 'contribs-all' ) . ' ' .
432 Xml
::input( 'target', 30, $options['target']) . ' '.
433 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
434 Xml
::namespaceSelector( $options['namespace'], '' ) .
435 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
437 Xml
::closeElement( 'form' );
442 * Generates each row in the contributions list.
444 * Contributions which are marked "top" are currently on top of the history.
445 * For these contributions, a [rollback] link is shown for users with sysop
446 * privileges. The rollback link restores the most recent version that was not
447 * written by the target user.
449 * @todo This would probably look a lot nicer in a table.
451 function ucListEdit( $sk, $row ) {
452 $fname = 'ucListEdit';
453 wfProfileIn( $fname );
455 global $wgLang, $wgUser, $wgRequest;
457 if( !isset( $messages ) ) {
458 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
459 $messages[$msg] = wfMsgExt( $msg, array( 'escape') );
463 $rev = new Revision( $row );
465 $page = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
466 $link = $sk->makeKnownLinkObj( $page );
467 $difftext = $topmarktext = '';
468 if( $row->rev_id
== $row->page_latest
) {
469 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
470 if( !$row->page_is_new
) {
471 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=0' ) . ')';
473 $difftext .= $messages['newarticle'];
476 if( $wgUser->isAllowed( 'rollback' ) ) {
477 $topmarktext .= ' '.$sk->generateRollback( $rev );
481 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
482 $difftext = '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=prev&oldid='.$row->rev_id
) . ')';
484 $difftext = '(' . $messages['diff'] . ')';
486 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
488 $comment = $sk->revComment( $rev );
489 $d = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
491 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
492 $d = '<span class="history-deleted">' . $d . '</span>';
495 if( $row->rev_minor_edit
) {
496 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
501 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
502 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
503 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
505 $ret = "<li>$ret</li>\n";
506 wfProfileOut( $fname );