Ajax show editors message moved to the extension repository (r19556)
[mediawiki.git] / includes / SpecialContributions.php
blob863a81fd4caaebee0cd68306c0bc1cf19f58400b
1 <?php
2 /**
3 * @addtogroup SpecialPage
4 */
6 class ContribsFinder {
7 var $username, $offset, $limit, $namespace;
8 var $dbr;
10 /**
11 * Constructor
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 );
20 function setNamespace( $ns ) {
21 $this->namespace = $ns;
24 function setLimit( $limit ) {
25 $this->limit = $limit;
28 function setOffset( $offset ) {
29 $this->offset = $offset;
32 /**
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 );
50 if ( $row ) {
51 return $row->rev_timestamp;
52 } else {
53 return false;
57 /**
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() {
62 return array(
63 $this->getEditLimit( "ASC" ),
64 $this->getEditLimit( "DESC" )
68 function getUserCond() {
69 $condition = '';
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';
79 } else {
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;
89 return '';
92 /**
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->offset . "' AND " .
104 $usercond . $nscond;
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 );
110 if ( $numRows ) {
111 $this->dbr->dataSeek( $res, $numRows - 1 );
112 $row = $this->dbr->fetchObject( $res );
113 $offset = $row->rev_timestamp;
114 } else {
115 $offset = false;
117 $this->dbr->freeResult( $res );
118 return $offset;
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 " .
131 $usercond . $nscond;
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 );
137 if ( $numRows ) {
138 $this->dbr->dataSeek( $res, $numRows - 1 );
139 $row = $this->dbr->fetchObject( $res );
140 $offset = $row->rev_timestamp;
141 } else {
142 $offset = false;
144 $this->dbr->freeResult( $res );
145 return $offset;
148 /* private */ function makeSql() {
149 $offsetQuery = '';
151 list( $page, $revision ) = $this->dbr->tableNamesN( 'page', 'revision' );
152 list( $index, $userCond ) = $this->getUserCond();
154 if ( $this->offset )
155 $offsetQuery = "AND rev_timestamp < '{$this->offset}'";
157 $nscond = $this->getNamespaceCond();
158 $use_index = $this->dbr->useIndexClause( $index );
159 $sql = 'SELECT ' .
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,'.
162 'rev_deleted ' .
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 );
167 return $sql;
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.
175 function find() {
176 $contribs = array();
177 $res = $this->dbr->query( $this->makeSql(), __METHOD__ );
178 while ( $c = $this->dbr->fetchObject( $res ) )
179 $contribs[] = $c;
180 $this->dbr->freeResult( $res );
181 return $contribs;
186 * Special page "user contributions".
187 * Shows a list of the contributions of a user.
189 * @return none
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;
195 $target = isset( $par ) ? $par : $wgRequest->getVal( 'target' );
196 if ( !strlen( $target ) ) {
197 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
198 return;
201 $nt = Title::newFromURL( $target );
202 if ( !$nt ) {
203 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
204 return;
207 $options = array();
209 list( $options['limit'], $options['offset']) = wfCheckLimits();
210 $options['offset'] = $wgRequest->getVal( 'offset' );
211 /* Offset must be an integral. */
212 if ( !strlen( $options['offset'] ) || !preg_match( '/^[0-9]+$/', $options['offset'] ) )
213 $options['offset'] = '';
215 $title = SpecialPage::getTitleFor( 'Contributions' );
216 $options['target'] = $target;
218 $nt =& Title::makeTitle( NS_USER, $nt->getDBkey() );
219 $finder = new ContribsFinder( ( $target == 'newbies' ) ? 'newbies' : $nt->getText() );
220 $finder->setLimit( $options['limit'] );
221 $finder->setOffset( $options['offset'] );
223 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
224 $options['namespace'] = intval( $ns );
225 $finder->setNamespace( $options['namespace'] );
226 } else {
227 $options['namespace'] = '';
230 if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) {
231 $options['bot'] = '1';
234 if ( $wgRequest->getText( 'go' ) == 'prev' ) {
235 $offset = $finder->getPreviousOffsetForPaging();
236 if ( $offset !== false ) {
237 $options['offset'] = $offset;
238 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
239 $wgOut->redirect( $prevurl );
240 return;
244 if ( $wgRequest->getText( 'go' ) == 'first' && $target != 'newbies') {
245 $offset = $finder->getFirstOffsetForPaging();
246 if ( $offset !== false ) {
247 $options['offset'] = $offset;
248 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
249 $wgOut->redirect( $prevurl );
250 return;
254 if ( $target == 'newbies' ) {
255 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
256 } else {
257 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub( $nt ) ) );
260 $id = User::idFromName( $nt->getText() );
261 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
263 $wgOut->addHTML( contributionsForm( $options) );
265 $contribs = $finder->find();
267 if ( count( $contribs ) == 0) {
268 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
269 return;
272 list( $early, $late ) = $finder->getEditLimits();
273 $lastts = count( $contribs ) ? $contribs[count( $contribs ) - 1]->rev_timestamp : 0;
274 $atstart = ( !count( $contribs ) || $late == $contribs[0]->rev_timestamp );
275 $atend = ( !count( $contribs ) || $early == $lastts );
277 // These four are defaults
278 $newestlink = wfMsgHtml( 'sp-contributions-newest' );
279 $oldestlink = wfMsgHtml( 'sp-contributions-oldest' );
280 $newerlink = wfMsgHtml( 'sp-contributions-newer', $options['limit'] );
281 $olderlink = wfMsgHtml( 'sp-contributions-older', $options['limit'] );
283 if ( !$atstart ) {
284 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => '' ), $options ) );
285 $newestlink = "<a href=\"$stuff\">$newestlink</a>";
286 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'prev' ), $options ) );
287 $newerlink = "<a href=\"$stuff\">$newerlink</a>";
290 if ( !$atend ) {
291 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'first' ), $options ) );
292 $oldestlink = "<a href=\"$stuff\">$oldestlink</a>";
293 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => $lastts ), $options ) );
294 $olderlink = "<a href=\"$stuff\">$olderlink</a>";
297 if ( $target == 'newbies' ) {
298 $firstlast ="($newestlink)";
299 } else {
300 $firstlast = "($newestlink | $oldestlink)";
303 $urls = array();
304 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
305 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'limit' => $num ), $options ) );
306 $urls[] = "<a href=\"$stuff\">".$wgLang->formatNum( $num )."</a>";
308 $bits = implode( $urls, ' | ' );
310 $prevnextbits = $firstlast .' '. wfMsgHtml( 'viewprevnext', $newerlink, $olderlink, $bits );
312 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
314 $wgOut->addHTML( "<ul>\n" );
316 $sk = $wgUser->getSkin();
317 foreach ( $contribs as $contrib )
318 $wgOut->addHTML( ucListEdit( $sk, $contrib ) );
320 $wgOut->addHTML( "</ul>\n" );
321 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
325 * Generates the subheading with links
326 * @param $nt @see Title object for the target
328 function contributionsSub( $nt ) {
329 global $wgSysopUserBans, $wgLang, $wgUser;
331 $sk = $wgUser->getSkin();
332 $id = User::idFromName( $nt->getText() );
334 if ( 0 == $id ) {
335 $ul = $nt->getText();
336 } else {
337 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
339 $talk = $nt->getTalkPage();
340 if( $talk ) {
341 # Talk page link
342 $tools[] = $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK ) );
343 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
344 # Block link
345 if( $wgUser->isAllowed( 'block' ) )
346 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
347 # Block log link
348 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
350 # Other logs link
351 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
352 $ul .= ' (' . implode( ' | ', $tools ) . ')';
354 return $ul;
358 * Generates the namespace selector form with hidden attributes.
359 * @param $options Array: the options to be included.
361 function contributionsForm( $options ) {
362 global $wgScript, $wgTitle;
364 $options['title'] = $wgTitle->getPrefixedText();
366 $f = "<form method='get' action=\"$wgScript\">\n";
367 foreach ( $options as $name => $value ) {
368 if( $name === 'namespace') continue;
369 $f .= "\t" . wfElement( 'input', array(
370 'name' => $name,
371 'type' => 'hidden',
372 'value' => $value ) ) . "\n";
375 $f .= '<p>' . wfMsgHtml( 'namespace' ) . ' ' .
376 HTMLnamespaceselector( $options['namespace'], '' ) .
377 wfElement( 'input', array(
378 'type' => 'submit',
379 'value' => wfMsg( 'allpagessubmit' ) )
381 "</p></form>\n";
383 return $f;
387 * Generates each row in the contributions list.
389 * Contributions which are marked "top" are currently on top of the history.
390 * For these contributions, a [rollback] link is shown for users with sysop
391 * privileges. The rollback link restores the most recent version that was not
392 * written by the target user.
394 * @todo This would probably look a lot nicer in a table.
396 function ucListEdit( $sk, $row ) {
397 $fname = 'ucListEdit';
398 wfProfileIn( $fname );
400 global $wgLang, $wgUser, $wgRequest;
401 static $messages;
402 if( !isset( $messages ) ) {
403 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
404 $messages[$msg] = wfMsgExt( $msg, array( 'escape') );
408 $rev = new Revision( $row );
410 $page = Title::makeTitle( $row->page_namespace, $row->page_title );
411 $link = $sk->makeKnownLinkObj( $page );
412 $difftext = $topmarktext = '';
413 if( $row->rev_id == $row->page_latest ) {
414 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
415 if( !$row->page_is_new ) {
416 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=0' ) . ')';
417 } else {
418 $difftext .= $messages['newarticle'];
421 if( $wgUser->isAllowed( 'rollback' ) ) {
422 $topmarktext .= ' '.$sk->generateRollback( $rev );
426 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
427 $difftext = '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
428 } else {
429 $difftext = '(' . $messages['diff'] . ')';
431 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
433 $comment = $sk->revComment( $rev );
434 $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
436 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
437 $d = '<span class="history-deleted">' . $d . '</span>';
440 if( $row->rev_minor_edit ) {
441 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
442 } else {
443 $mflag = '';
446 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
447 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
448 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
450 $ret = "<li>$ret</li>\n";
451 wfProfileOut( $fname );
452 return $ret;