4 * @subpackage SpecialPage
7 /** @package MediaWiki */
9 var $username, $offset, $limit, $namespace;
12 function ContribsFinder( $username ) {
13 $this->username
= $username;
14 $this->namespace = false;
15 $this->dbr
=& wfGetDB( DB_SLAVE
);
18 function setNamespace( $ns ) {
19 $this->namespace = $ns;
22 function setLimit( $limit ) {
23 $this->limit
= $limit;
26 function setOffset( $offset ) {
27 $this->offset
= $offset;
30 function getEditLimit( $dir ) {
31 list( $index, $usercond ) = $this->getUserCond();
32 $nscond = $this->getNamespaceCond();
33 $use_index = $this->dbr
->useIndexClause( $index );
34 list( $revision, $page) = $this->dbr
->tableNamesN( 'revision', 'page' );
35 $sql = "SELECT rev_timestamp " .
36 " FROM $page,$revision $use_index " .
37 " WHERE rev_page=page_id AND $usercond $nscond" .
38 " ORDER BY rev_timestamp $dir LIMIT 1";
40 $res = $this->dbr
->query( $sql, __METHOD__
);
41 $row = $this->dbr
->fetchObject( $res );
43 return $row->rev_timestamp
;
49 function getEditLimits() {
51 $this->getEditLimit( "ASC" ),
52 $this->getEditLimit( "DESC" )
56 function getUserCond() {
59 if ( $this->username
== 'newbies' ) {
60 $max = $this->dbr
->selectField( 'user', 'max(user_id)', false, 'make_sql' );
61 $condition = '>' . (int)($max - $max / 100);
64 if ( $condition == '' ) {
65 $condition = ' rev_user_text=' . $this->dbr
->addQuotes( $this->username
);
66 $index = 'usertext_timestamp';
68 $condition = ' rev_user '.$condition ;
69 $index = 'user_timestamp';
71 return array( $index, $condition );
74 function getNamespaceCond() {
75 if ( $this->namespace !== false )
76 return ' AND page_namespace = ' . (int)$this->namespace;
80 function getPreviousOffsetForPaging() {
81 list( $index, $usercond ) = $this->getUserCond();
82 $nscond = $this->getNamespaceCond();
84 $use_index = $this->dbr
->useIndexClause( $index );
85 list( $page, $revision ) = $this->dbr
->tableNamesN( 'page', 'revision' );
87 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
88 "WHERE page_id = rev_page AND rev_timestamp > '" . $this->offset
. "' AND " .
90 $sql .= " ORDER BY rev_timestamp ASC";
91 $sql = $this->dbr
->limitResult( $sql, $this->limit
, 0 );
92 $res = $this->dbr
->query( $sql );
94 $numRows = $this->dbr
->numRows( $res );
96 $this->dbr
->dataSeek( $res, $numRows - 1 );
97 $row = $this->dbr
->fetchObject( $res );
98 $offset = $row->rev_timestamp
;
102 $this->dbr
->freeResult( $res );
106 function getFirstOffsetForPaging() {
107 list( $index, $usercond ) = $this->getUserCond();
108 $use_index = $this->dbr
->useIndexClause( $index );
109 list( $page, $revision ) = $this->dbr
->tableNamesN( 'page', 'revision' );
110 $nscond = $this->getNamespaceCond();
111 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
112 "WHERE page_id = rev_page AND " .
114 $sql .= " ORDER BY rev_timestamp ASC";
115 $sql = $this->dbr
->limitResult( $sql, $this->limit
, 0 );
116 $res = $this->dbr
->query( $sql );
118 $numRows = $this->dbr
->numRows( $res );
120 $this->dbr
->dataSeek( $res, $numRows - 1 );
121 $row = $this->dbr
->fetchObject( $res );
122 $offset = $row->rev_timestamp
;
126 $this->dbr
->freeResult( $res );
130 /* private */ function makeSql() {
133 list( $page, $revision ) = $this->dbr
->tableNamesN( 'page', 'revision' );
134 list( $index, $userCond ) = $this->getUserCond();
137 $offsetQuery = "AND rev_timestamp <= '{$this->offset}'";
139 $nscond = $this->getNamespaceCond();
140 $use_index = $this->dbr
->useIndexClause( $index );
142 page_namespace,page_title,page_is_new,page_latest,
143 rev_id,rev_page,rev_text_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user,rev_user_text,
145 FROM $page,$revision $use_index
146 WHERE page_id=rev_page AND $userCond $nscond $offsetQuery
147 ORDER BY rev_timestamp DESC";
148 $sql = $this->dbr
->limitResult( $sql, $this->limit
, 0 );
154 $res = $this->dbr
->query( $this->makeSql(), __METHOD__
);
155 while ( $c = $this->dbr
->fetchObject( $res ) )
157 $this->dbr
->freeResult( $res );
163 * Special page "user contributions".
164 * Shows a list of the contributions of a user.
167 * @param $par String: (optional) user name of the user for which to show the contributions
169 function wfSpecialContributions( $par = null ) {
170 global $wgUser, $wgOut, $wgLang, $wgRequest;
172 $target = isset( $par ) ?
$par : $wgRequest->getVal( 'target' );
173 if ( !strlen( $target ) ) {
174 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
178 $nt = Title
::newFromURL( $target );
180 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
186 list( $options['limit'], $options['offset']) = wfCheckLimits();
187 $options['offset'] = $wgRequest->getVal( 'offset' );
188 /* Offset must be an integral, unless the db is using timestamps */
189 $dbr =& wfGetDB( DB_SLAVE
);
190 if ( !strlen( $options['offset'] ) ||
191 ( !$dbr->realTimestamps() && !preg_match( '/^[0-9]+$/', $options['offset'] ) ) )
192 $options['offset'] = '';
194 $title = SpecialPage
::getTitleFor( 'Contributions' );
195 $options['target'] = $target;
197 $nt =& Title
::makeTitle( NS_USER
, $nt->getDBkey() );
198 $finder = new ContribsFinder( ( $target == 'newbies' ) ?
'newbies' : $nt->getText() );
199 $finder->setLimit( $options['limit'] );
200 $finder->setOffset( $options['offset'] );
202 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
203 $options['namespace'] = intval( $ns );
204 $finder->setNamespace( $options['namespace'] );
206 $options['namespace'] = '';
209 if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) {
210 $options['bot'] = '1';
213 if ( $wgRequest->getText( 'go' ) == 'prev' ) {
214 $offset = $finder->getPreviousOffsetForPaging();
215 if ( $offset !== false ) {
216 $options['offset'] = $offset;
217 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
218 $wgOut->redirect( $prevurl );
223 if ( $wgRequest->getText( 'go' ) == 'first' && $target != 'newbies') {
224 $offset = $finder->getFirstOffsetForPaging();
225 if ( $offset !== false ) {
226 $options['offset'] = $offset;
227 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
228 $wgOut->redirect( $prevurl );
233 if ( $target == 'newbies' ) {
234 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
236 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub( $nt ) ) );
239 $id = User
::idFromName( $nt->getText() );
240 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
242 $wgOut->addHTML( contributionsForm( $options) );
244 $contribs = $finder->find();
246 if ( count( $contribs ) == 0) {
247 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
251 list( $early, $late ) = $finder->getEditLimits();
252 $lastts = count( $contribs ) ?
$contribs[count( $contribs ) - 1]->rev_timestamp
: 0;
253 $atstart = ( !count( $contribs ) ||
$late == $contribs[0]->rev_timestamp
);
254 $atend = ( !count( $contribs ) ||
$early == $lastts );
256 // These four are defaults
257 $newestlink = wfMsgHtml( 'sp-contributions-newest' );
258 $oldestlink = wfMsgHtml( 'sp-contributions-oldest' );
259 $newerlink = wfMsgHtml( 'sp-contributions-newer', $options['limit'] );
260 $olderlink = wfMsgHtml( 'sp-contributions-older', $options['limit'] );
263 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => '' ), $options ) );
264 $newestlink = "<a href=\"$stuff\">$newestlink</a>";
265 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'prev' ), $options ) );
266 $newerlink = "<a href=\"$stuff\">$newerlink</a>";
270 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'first' ), $options ) );
271 $oldestlink = "<a href=\"$stuff\">$oldestlink</a>";
272 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => $lastts ), $options ) );
273 $olderlink = "<a href=\"$stuff\">$olderlink</a>";
276 if ( $target == 'newbies' ) {
277 $firstlast ="($newestlink)";
279 $firstlast = "($newestlink | $oldestlink)";
283 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
284 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'limit' => $num ), $options ) );
285 $urls[] = "<a href=\"$stuff\">".$wgLang->formatNum( $num )."</a>";
287 $bits = implode( $urls, ' | ' );
289 $prevnextbits = $firstlast .' '. wfMsgHtml( 'viewprevnext', $newerlink, $olderlink, $bits );
291 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
293 $wgOut->addHTML( "<ul>\n" );
295 $sk = $wgUser->getSkin();
296 foreach ( $contribs as $contrib )
297 $wgOut->addHTML( ucListEdit( $sk, $contrib ) );
299 $wgOut->addHTML( "</ul>\n" );
300 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
304 * Generates the subheading with links
305 * @param $nt @see Title object for the target
307 function contributionsSub( $nt ) {
308 global $wgSysopUserBans, $wgLang, $wgUser;
310 $sk = $wgUser->getSkin();
311 $id = User
::idFromName( $nt->getText() );
314 $ul = $nt->getText();
316 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
318 $talk = $nt->getTalkPage();
321 $tools[] = $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK
) );
322 if( ( $id != 0 && $wgSysopUserBans ) ||
( $id == 0 && User
::isIP( $nt->getText() ) ) ) {
324 if( $wgUser->isAllowed( 'block' ) )
325 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
327 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), htmlspecialchars( LogPage
::logName( 'block' ) ), 'type=block&page=' . $nt->getPrefixedUrl() );
330 $tools[] = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
331 $ul .= ' (' . implode( ' | ', $tools ) . ')';
337 * Generates the namespace selector form with hidden attributes.
338 * @param $options Array: the options to be included.
340 function contributionsForm( $options ) {
341 global $wgScript, $wgTitle;
343 $options['title'] = $wgTitle->getPrefixedText();
345 $f = "<form method='get' action=\"$wgScript\">\n";
346 foreach ( $options as $name => $value ) {
347 if( $name === 'namespace') continue;
348 $f .= "\t" . wfElement( 'input', array(
351 'value' => $value ) ) . "\n";
354 $f .= '<p>' . wfMsgHtml( 'namespace' ) . ' ' .
355 HTMLnamespaceselector( $options['namespace'], '' ) .
356 wfElement( 'input', array(
358 'value' => wfMsg( 'allpagessubmit' ) )
366 * Generates each row in the contributions list.
368 * Contributions which are marked "top" are currently on top of the history.
369 * For these contributions, a [rollback] link is shown for users with sysop
370 * privileges. The rollback link restores the most recent version that was not
371 * written by the target user.
373 * @todo This would probably look a lot nicer in a table.
375 function ucListEdit( $sk, $row ) {
376 $fname = 'ucListEdit';
377 wfProfileIn( $fname );
379 global $wgLang, $wgUser, $wgRequest;
381 if( !isset( $messages ) ) {
382 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
383 $messages[$msg] = wfMsgExt( $msg, array( 'escape') );
387 $rev = new Revision( $row );
389 $page = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
390 $link = $sk->makeKnownLinkObj( $page );
391 $difftext = $topmarktext = '';
392 if( $row->rev_id
== $row->page_latest
) {
393 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
394 if( !$row->page_is_new
) {
395 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=0' ) . ')';
397 $difftext .= $messages['newarticle'];
400 if( $wgUser->isAllowed( 'rollback' ) ) {
401 $topmarktext .= ' '.$sk->generateRollback( $rev );
405 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
406 $difftext = '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=prev&oldid='.$row->rev_id
) . ')';
408 $difftext = '(' . $messages['diff'] . ')';
410 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
412 $comment = $sk->revComment( $rev );
413 $d = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
415 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
416 $d = '<span class="history-deleted">' . $d . '</span>';
419 if( $row->rev_minor_edit
) {
420 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
425 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
426 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
427 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
429 $ret = "<li>$ret</li>\n";
430 wfProfileOut( $fname );