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 join(',', Revision
::selectFields()) .
162 " FROM $page,$revision $use_index " .
163 "WHERE page_id=rev_page AND $userCond $nscond $offsetQuery " .
164 'ORDER BY rev_timestamp DESC';
165 $sql = $this->dbr
->limitResult( $sql, $this->limit
, 0 );
170 * This do the search for the user given when creating the object.
171 * It should probably be the only public function in this class.
172 * @return Array of contributions.
176 $res = $this->dbr
->query( $this->makeSql(), __METHOD__
);
177 while ( $c = $this->dbr
->fetchObject( $res ) )
179 $this->dbr
->freeResult( $res );
185 * Special page "user contributions".
186 * Shows a list of the contributions of a user.
189 * @param $par String: (optional) user name of the user for which to show the contributions
191 function wfSpecialContributions( $par = null ) {
192 global $wgUser, $wgOut, $wgLang, $wgRequest;
194 $target = isset( $par ) ?
$par : $wgRequest->getVal( 'target' );
195 if ( !strlen( $target ) ) {
196 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
200 $nt = Title
::newFromURL( $target );
202 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
208 list( $options['limit'], $options['offset']) = wfCheckLimits();
209 $options['offset'] = $wgRequest->getVal( 'offset' );
210 /* Offset must be an integral. */
211 if ( !strlen( $options['offset'] ) ||
!preg_match( '/^[0-9]+$/', $options['offset'] ) )
212 $options['offset'] = '';
214 $title = SpecialPage
::getTitleFor( 'Contributions' );
215 $options['target'] = $target;
217 $nt =& Title
::makeTitle( NS_USER
, $nt->getDBkey() );
218 $finder = new ContribsFinder( ( $target == 'newbies' ) ?
'newbies' : $nt->getText() );
219 $finder->setLimit( $options['limit'] );
220 $finder->setOffset( $options['offset'] );
222 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
223 $options['namespace'] = intval( $ns );
224 $finder->setNamespace( $options['namespace'] );
226 $options['namespace'] = '';
229 if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) {
230 $options['bot'] = '1';
233 if ( $wgRequest->getText( 'go' ) == 'prev' ) {
234 $offset = $finder->getPreviousOffsetForPaging();
235 if ( $offset !== false ) {
236 $options['offset'] = $offset;
237 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
238 $wgOut->redirect( $prevurl );
243 if ( $wgRequest->getText( 'go' ) == 'first' && $target != 'newbies') {
244 $offset = $finder->getFirstOffsetForPaging();
245 if ( $offset !== false ) {
246 $options['offset'] = $offset;
247 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
248 $wgOut->redirect( $prevurl );
253 if ( $target == 'newbies' ) {
254 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
256 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub( $nt ) ) );
259 $id = User
::idFromName( $nt->getText() );
260 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
262 $wgOut->addHTML( contributionsForm( $options) );
264 $contribs = $finder->find();
266 if ( count( $contribs ) == 0) {
267 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
271 list( $early, $late ) = $finder->getEditLimits();
272 $lastts = count( $contribs ) ?
wfTimestamp( TS_MW
, $contribs[count( $contribs ) - 1]->rev_timestamp
) : 0;
273 $atstart = ( !count( $contribs ) ||
$late == wfTimestamp( TS_MW
, $contribs[0]->rev_timestamp
) );
274 $atend = ( !count( $contribs ) ||
$early == $lastts );
276 // These four are defaults
277 $newestlink = wfMsgHtml( 'sp-contributions-newest' );
278 $oldestlink = wfMsgHtml( 'sp-contributions-oldest' );
279 $newerlink = wfMsgHtml( 'sp-contributions-newer', $options['limit'] );
280 $olderlink = wfMsgHtml( 'sp-contributions-older', $options['limit'] );
283 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => '' ), $options ) );
284 $newestlink = "<a href=\"$stuff\">$newestlink</a>";
285 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'prev' ), $options ) );
286 $newerlink = "<a href=\"$stuff\">$newerlink</a>";
290 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'first' ), $options ) );
291 $oldestlink = "<a href=\"$stuff\">$oldestlink</a>";
292 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => $lastts ), $options ) );
293 $olderlink = "<a href=\"$stuff\">$olderlink</a>";
296 if ( $target == 'newbies' ) {
297 $firstlast ="($newestlink)";
299 $firstlast = "($newestlink | $oldestlink)";
303 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
304 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'limit' => $num ), $options ) );
305 $urls[] = "<a href=\"$stuff\">".$wgLang->formatNum( $num )."</a>";
307 $bits = implode( $urls, ' | ' );
309 $prevnextbits = $firstlast .' '. wfMsgHtml( 'viewprevnext', $newerlink, $olderlink, $bits );
311 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
313 $wgOut->addHTML( "<ul>\n" );
315 $sk = $wgUser->getSkin();
316 foreach ( $contribs as $contrib )
317 $wgOut->addHTML( ucListEdit( $sk, $contrib ) );
319 $wgOut->addHTML( "</ul>\n" );
320 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
322 # If there were contributions, and it was a valid user or IP, show
323 # the appropriate "footer" message - WHOIS tools, etc.
324 if( count( $contribs ) > 0 && $target != 'newbies' && $nt instanceof Title
) {
325 $message = IP
::isIPAddress( $target )
326 ?
'sp-contributions-footer-anon'
327 : 'sp-contributions-footer';
328 $text = wfMsg( $message, $target );
329 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
330 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
331 $wgOut->addWikiText( wfMsg( $message, $target ) );
332 $wgOut->addHtml( '</div>' );
339 * Generates the subheading with links
340 * @param $nt @see Title object for the target
342 function contributionsSub( $nt ) {
343 global $wgSysopUserBans, $wgLang, $wgUser;
345 $sk = $wgUser->getSkin();
346 $id = User
::idFromName( $nt->getText() );
349 $ul = $nt->getText();
351 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
353 $talk = $nt->getTalkPage();
356 $tools[] = $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK
) );
357 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && User
::isIP( $nt->getText() ) ) ) {
359 if( $wgUser->isAllowed( 'block' ) )
360 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
362 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
365 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
366 $ul .= ' (' . implode( ' | ', $tools ) . ')';
372 * Generates the namespace selector form with hidden attributes.
373 * @param $options Array: the options to be included.
375 function contributionsForm( $options ) {
376 global $wgScript, $wgTitle;
378 $options['title'] = $wgTitle->getPrefixedText();
380 $f = "<form method='get' action=\"$wgScript\">\n";
381 foreach ( $options as $name => $value ) {
382 if( $name === 'namespace') continue;
383 $f .= "\t" . wfElement( 'input', array(
386 'value' => $value ) ) . "\n";
389 $f .= '<p>' . wfMsgHtml( 'namespace' ) . ' ' .
390 HTMLnamespaceselector( $options['namespace'], '' ) .
391 wfElement( 'input', array(
393 'value' => wfMsg( 'allpagessubmit' ) )
401 * Generates each row in the contributions list.
403 * Contributions which are marked "top" are currently on top of the history.
404 * For these contributions, a [rollback] link is shown for users with sysop
405 * privileges. The rollback link restores the most recent version that was not
406 * written by the target user.
408 * @todo This would probably look a lot nicer in a table.
410 function ucListEdit( $sk, $row ) {
411 $fname = 'ucListEdit';
412 wfProfileIn( $fname );
414 global $wgLang, $wgUser, $wgRequest;
416 if( !isset( $messages ) ) {
417 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
418 $messages[$msg] = wfMsgExt( $msg, array( 'escape') );
422 $rev = new Revision( $row );
424 $page = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
425 $link = $sk->makeKnownLinkObj( $page );
426 $difftext = $topmarktext = '';
427 if( $row->rev_id
== $row->page_latest
) {
428 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
429 if( !$row->page_is_new
) {
430 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=0' ) . ')';
432 $difftext .= $messages['newarticle'];
435 if( $wgUser->isAllowed( 'rollback' ) ) {
436 $topmarktext .= ' '.$sk->generateRollback( $rev );
440 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
441 $difftext = '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=prev&oldid='.$row->rev_id
) . ')';
443 $difftext = '(' . $messages['diff'] . ')';
445 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
447 $comment = $sk->revComment( $rev );
448 $d = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
450 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
451 $d = '<span class="history-deleted">' . $d . '</span>';
454 if( $row->rev_minor_edit
) {
455 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
460 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
461 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
462 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
464 $ret = "<li>$ret</li>\n";
465 wfProfileOut( $fname );