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 $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 ( preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/(24|16)/", $this->username
) ) {
75 $abcd = explode( ".", $this->username
);
76 if( substr( $this->username
, -2 ) == 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);
81 if ( $condition == '' ) {
82 $condition = ' rev_user_text=' . $this->dbr
->addQuotes( $this->username
);
83 $index = 'usertext_timestamp';
85 #$condition = ' rev_user '.$condition ;
86 $index = 'user_timestamp';
88 return array( $index, $condition );
91 function getNamespaceCond() {
92 if ( $this->namespace !== false )
93 return ' AND page_namespace = ' . (int)$this->namespace;
98 * @return Timestamp of first entry in previous page.
100 function getPreviousOffsetForPaging() {
101 list( $index, $usercond ) = $this->getUserCond();
102 $nscond = $this->getNamespaceCond();
104 $use_index = $this->dbr
->useIndexClause( $index );
105 list( $page, $revision ) = $this->dbr
->tableNamesN( 'page', 'revision' );
107 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
108 "WHERE page_id = rev_page AND rev_timestamp > '" . $this->offset
. "' AND " .
110 $sql .= " ORDER BY rev_timestamp ASC";
111 $sql = $this->dbr
->limitResult( $sql, $this->limit
, 0 );
112 $res = $this->dbr
->query( $sql );
114 $numRows = $this->dbr
->numRows( $res );
116 $this->dbr
->dataSeek( $res, $numRows - 1 );
117 $row = $this->dbr
->fetchObject( $res );
118 $offset = $row->rev_timestamp
;
122 $this->dbr
->freeResult( $res );
127 * @return Timestamp of first entry in next page.
129 function getFirstOffsetForPaging() {
130 list( $index, $usercond ) = $this->getUserCond();
131 $use_index = $this->dbr
->useIndexClause( $index );
132 list( $page, $revision ) = $this->dbr
->tableNamesN( 'page', 'revision' );
133 $nscond = $this->getNamespaceCond();
134 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
135 "WHERE page_id = rev_page AND " .
137 $sql .= " ORDER BY rev_timestamp ASC";
138 $sql = $this->dbr
->limitResult( $sql, $this->limit
, 0 );
139 $res = $this->dbr
->query( $sql );
141 $numRows = $this->dbr
->numRows( $res );
143 $this->dbr
->dataSeek( $res, $numRows - 1 );
144 $row = $this->dbr
->fetchObject( $res );
145 $offset = $row->rev_timestamp
;
149 $this->dbr
->freeResult( $res );
153 /* private */ function makeSql() {
156 list( $page, $revision ) = $this->dbr
->tableNamesN( 'page', 'revision' );
157 list( $index, $userCond ) = $this->getUserCond();
160 $offsetQuery = "AND rev_timestamp < '{$this->offset}'";
162 $nscond = $this->getNamespaceCond();
163 $use_index = $this->dbr
->useIndexClause( $index );
165 'page_namespace,page_title,page_is_new,page_latest,'.
166 join(',', Revision
::selectFields()).
167 " FROM $page,$revision $use_index " .
168 "WHERE page_id=rev_page AND $userCond $nscond $offsetQuery " .
169 'ORDER BY rev_timestamp DESC';
170 $sql = $this->dbr
->limitResult( $sql, $this->limit
, 0 );
175 * This do the search for the user given when creating the object.
176 * It should probably be the only public function in this class.
177 * @return Array of contributions.
181 $res = $this->dbr
->query( $this->makeSql(), __METHOD__
);
182 while ( $c = $this->dbr
->fetchObject( $res ) )
184 $this->dbr
->freeResult( $res );
190 * Special page "user contributions".
191 * Shows a list of the contributions of a user.
194 * @param $par String: (optional) user name of the user for which to show the contributions
196 function wfSpecialContributions( $par = null ) {
197 global $wgUser, $wgOut, $wgLang, $wgRequest;
199 $target = isset( $par ) ?
$par : $wgRequest->getVal( 'target' );
200 $radiobox = $wgRequest->getVal( 'newbie' );
202 // check for radiobox
203 if ( $radiobox == 'contribs-newbie' ) $target = 'newbies';
205 if ( !strlen( $target ) ) {
206 $wgOut->addHTML( contributionsForm( '' ) );
210 $nt = Title
::newFromURL( $target );
212 $wgOut->addHTML( contributionsForm( '' ) );
218 list( $options['limit'], $options['offset']) = wfCheckLimits();
219 $options['offset'] = $wgRequest->getVal( 'offset' );
221 /* Offset must either be numeric, or a "standard" timestamp */
222 $dbr = wfGetDB( DB_SLAVE
);
223 if ( !strlen( $options['offset'] )
224 ||
($dbr->realTimestamps()
225 && !preg_match ( '/^\d\d\d\d\-\d\d\-\d\d \d\d:\d\d:\d\d(?:[+-]\d\d)?$/', $options['offset'] )
227 ||
(!$dbr->realTimestamps()
228 && !preg_match( '/^[0-9]+$/', $options['offset'] )
231 $options['offset'] = '';
234 $title = SpecialPage
::getTitleFor( 'Contributions' );
235 $options['target'] = $target;
237 $nt = Title
::makeTitle( NS_USER
, $nt->getDBkey() );
238 $finder = new ContribsFinder( ( $target == 'newbies' ) ?
'newbies' : $nt->getText() );
239 $finder->setLimit( $options['limit'] );
240 $finder->setOffset( $options['offset'] );
242 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
243 $options['namespace'] = intval( $ns );
244 $finder->setNamespace( $options['namespace'] );
246 $options['namespace'] = '';
249 if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) {
250 $options['bot'] = '1';
253 if ( $wgRequest->getText( 'go' ) == 'prev' ) {
254 $offset = $finder->getPreviousOffsetForPaging();
255 if ( $offset !== false ) {
256 $options['offset'] = $offset;
257 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
258 $wgOut->redirect( $prevurl );
263 if ( $wgRequest->getText( 'go' ) == 'first' && $target != 'newbies') {
264 $offset = $finder->getFirstOffsetForPaging();
265 if ( $offset !== false ) {
266 $options['offset'] = $offset;
267 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
268 $wgOut->redirect( $prevurl );
273 if ( $target == 'newbies' ) {
274 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
275 } else if ( preg_match( "/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/(24|16)/", $target ) ) {
276 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $target ) );
278 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub( $nt ) ) );
281 $id = User
::idFromName( $nt->getText() );
282 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
284 $wgOut->addHTML( contributionsForm( $options) );
286 $contribs = $finder->find();
288 if ( count( $contribs ) == 0) {
289 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
293 list( $early, $late ) = $finder->getEditLimits();
294 $lastts = count( $contribs ) ?
$contribs[count( $contribs ) - 1]->rev_timestamp
: 0;
295 $atstart = ( !count( $contribs ) ||
$late == $contribs[0]->rev_timestamp
);
296 $atend = ( !count( $contribs ) ||
$early == $lastts );
298 // These four are defaults
299 $newestlink = wfMsgHtml( 'sp-contributions-newest' );
300 $oldestlink = wfMsgHtml( 'sp-contributions-oldest' );
301 $newerlink = wfMsgHtml( 'sp-contributions-newer', $options['limit'] );
302 $olderlink = wfMsgHtml( 'sp-contributions-older', $options['limit'] );
305 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => '' ), $options ) );
306 $newestlink = "<a href=\"$stuff\">$newestlink</a>";
307 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'prev' ), $options ) );
308 $newerlink = "<a href=\"$stuff\">$newerlink</a>";
312 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'first' ), $options ) );
313 $oldestlink = "<a href=\"$stuff\">$oldestlink</a>";
314 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => $lastts ), $options ) );
315 $olderlink = "<a href=\"$stuff\">$olderlink</a>";
318 if ( $target == 'newbies' ) {
319 $firstlast ="($newestlink)";
321 $firstlast = "($newestlink | $oldestlink)";
325 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
326 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'limit' => $num ), $options ) );
327 $urls[] = "<a href=\"$stuff\">".$wgLang->formatNum( $num )."</a>";
329 $bits = implode( $urls, ' | ' );
331 $prevnextbits = $firstlast .' '. wfMsgHtml( 'viewprevnext', $newerlink, $olderlink, $bits );
333 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
335 $wgOut->addHTML( "<ul>\n" );
337 $sk = $wgUser->getSkin();
338 foreach ( $contribs as $contrib )
339 $wgOut->addHTML( ucListEdit( $sk, $contrib ) );
341 $wgOut->addHTML( "</ul>\n" );
342 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
346 * Generates the subheading with links
347 * @param $nt @see Title object for the target
349 function contributionsSub( $nt ) {
350 global $wgSysopUserBans, $wgLang, $wgUser;
352 $sk = $wgUser->getSkin();
353 $id = User
::idFromName( $nt->getText() );
356 $ul = $nt->getText();
358 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
360 $talk = $nt->getTalkPage();
363 $tools[] = $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK
) );
364 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && User
::isIP( $nt->getText() ) ) ) {
366 if( $wgUser->isAllowed( 'block' ) )
367 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
369 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
372 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
373 $ul .= ' (' . implode( ' | ', $tools ) . ')';
379 * Generates the namespace selector form with hidden attributes.
380 * @param $options Array: the options to be included.
382 function contributionsForm( $options ) {
383 global $wgScript, $wgTitle, $wgRequest;
385 $options['title'] = $wgTitle->getPrefixedText();
386 if (!isset($options['target']))
387 $options['target'] = '';
388 if (!isset($options['namespace']))
389 $options['namespace'] = 0;
391 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
393 foreach ( $options as $name => $value ) {
394 if( $name === 'namespace') continue;
395 $f .= "\t" . Xml
::hidden( $name, $value ) . "\n";
399 Xml
::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
400 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'newbie' , 'contribs-newbie' , 'contribs-newbie', 'contribs-newbie' ) . '<br />' .
401 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'newbie' , 'contribs-all', 'contribs-all', 'contribs-all' ) . ' ' .
402 Xml
::input( 'target', 20, $options['target']) . ' '.
403 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
404 Xml
::namespaceSelector( $options['namespace'], '' ) .
405 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
407 Xml
::closeElement( 'form' );
412 * Generates each row in the contributions list.
414 * Contributions which are marked "top" are currently on top of the history.
415 * For these contributions, a [rollback] link is shown for users with sysop
416 * privileges. The rollback link restores the most recent version that was not
417 * written by the target user.
419 * @todo This would probably look a lot nicer in a table.
421 function ucListEdit( $sk, $row ) {
422 $fname = 'ucListEdit';
423 wfProfileIn( $fname );
425 global $wgLang, $wgUser, $wgRequest;
427 if( !isset( $messages ) ) {
428 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
429 $messages[$msg] = wfMsgExt( $msg, array( 'escape') );
433 $rev = new Revision( $row );
435 $page = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
436 $link = $sk->makeKnownLinkObj( $page );
437 $difftext = $topmarktext = '';
438 if( $row->rev_id
== $row->page_latest
) {
439 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
440 if( !$row->page_is_new
) {
441 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=0' ) . ')';
443 $difftext .= $messages['newarticle'];
446 if( $wgUser->isAllowed( 'rollback' ) ) {
447 $topmarktext .= ' '.$sk->generateRollback( $rev );
451 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
452 $difftext = '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=prev&oldid='.$row->rev_id
) . ')';
454 $difftext = '(' . $messages['diff'] . ')';
456 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
458 $comment = $sk->revComment( $rev );
459 $d = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
461 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
462 $d = '<span class="history-deleted">' . $d . '</span>';
465 if( $row->rev_minor_edit
) {
466 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
471 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
472 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
473 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
475 $ret = "<li>$ret</li>\n";
476 wfProfileOut( $fname );