disable ipv6 cruft
[mediawiki.git] / includes / SpecialContributions.php
blob16d497843baa9e291ef79a69e47cd3206f4ed93d
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, '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;
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 wfTimestamp( TS_MW, $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 = 'rev_user >' . (int)($max - $max / 100);
75 /* WTF? -- disabling
76 else if ( IP::isIPv6( $this->username ) ) {
77 # All stored IPs should be sanitized from now on, check for exact matches for reverse compatibility
78 $condition = '(rev_user_text=' . $this->dbr->addQuotes(IP::sanitizeIP($this->username)) . ' OR rev_user_text=' . $this->dbr->addQuotes($this->username) . ')';
82 if ( $condition == '' ) {
83 $condition = ' rev_user_text=' . $this->dbr->addQuotes( $this->username );
84 $index = 'usertext_timestamp';
85 } else {
86 #$condition = ' rev_user '.$condition ;
87 $index = 'user_timestamp';
89 return array( $index, $condition );
92 function getNamespaceCond() {
93 if ( $this->namespace !== false )
94 return ' AND page_namespace = ' . (int)$this->namespace;
95 return '';
98 /**
99 * @return Timestamp of first entry in previous page.
101 function getPreviousOffsetForPaging() {
102 list( $index, $usercond ) = $this->getUserCond();
103 $nscond = $this->getNamespaceCond();
105 $use_index = $this->dbr->useIndexClause( $index );
106 list( $page, $revision ) = $this->dbr->tableNamesN( 'page', 'revision' );
108 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
109 "WHERE page_id = rev_page AND rev_timestamp > '" . $this->dbr->timestamp($this->offset) . "' AND " .
110 $usercond . $nscond;
111 $sql .= " ORDER BY rev_timestamp ASC";
112 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
113 $res = $this->dbr->query( $sql );
115 $numRows = $this->dbr->numRows( $res );
116 if ( $numRows ) {
117 $this->dbr->dataSeek( $res, $numRows - 1 );
118 $row = $this->dbr->fetchObject( $res );
119 $offset = wfTimestamp( TS_MW, $row->rev_timestamp );
120 } else {
121 $offset = false;
123 $this->dbr->freeResult( $res );
124 return $offset;
128 * @return Timestamp of first entry in next page.
130 function getFirstOffsetForPaging() {
131 list( $index, $usercond ) = $this->getUserCond();
132 $use_index = $this->dbr->useIndexClause( $index );
133 list( $page, $revision ) = $this->dbr->tableNamesN( 'page', 'revision' );
134 $nscond = $this->getNamespaceCond();
135 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
136 "WHERE page_id = rev_page AND " .
137 $usercond . $nscond;
138 $sql .= " ORDER BY rev_timestamp ASC";
139 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
140 $res = $this->dbr->query( $sql );
142 $numRows = $this->dbr->numRows( $res );
143 if ( $numRows ) {
144 $this->dbr->dataSeek( $res, $numRows - 1 );
145 $row = $this->dbr->fetchObject( $res );
146 $offset = wfTimestamp( TS_MW, $row->rev_timestamp );
147 } else {
148 $offset = false;
150 $this->dbr->freeResult( $res );
151 return $offset;
154 /* private */ function makeSql() {
155 $offsetQuery = '';
157 list( $page, $revision ) = $this->dbr->tableNamesN( 'page', 'revision' );
158 list( $index, $userCond ) = $this->getUserCond();
160 if ( $this->offset ) {
161 $offsetQuery = "AND rev_timestamp < '" . $this->dbr->timestamp($this->offset) . "'";
164 $nscond = $this->getNamespaceCond();
165 $use_index = $this->dbr->useIndexClause( $index );
166 $sql = 'SELECT ' .
167 'page_namespace,page_title,page_is_new,page_latest,'.
168 join(',', Revision::selectFields()).
169 " FROM $page,$revision $use_index " .
170 "WHERE page_id=rev_page AND $userCond $nscond $offsetQuery " .
171 'ORDER BY rev_timestamp DESC';
172 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
173 return $sql;
177 * This do the search for the user given when creating the object.
178 * It should probably be the only public function in this class.
179 * @return Array of contributions.
181 function find() {
182 $contribs = array();
183 $res = $this->dbr->query( $this->makeSql(), __METHOD__ );
184 while ( $c = $this->dbr->fetchObject( $res ) )
185 $contribs[] = $c;
186 $this->dbr->freeResult( $res );
187 return $contribs;
192 * Special page "user contributions".
193 * Shows a list of the contributions of a user.
195 * @return none
196 * @param $par String: (optional) user name of the user for which to show the contributions
198 function wfSpecialContributions( $par = null ) {
199 global $wgUser, $wgOut, $wgLang, $wgRequest;
201 $target = isset( $par ) ? $par : $wgRequest->getVal( 'target' );
202 $radiobox = $wgRequest->getVal( 'newbie' );
204 // check for radiobox
205 if ( $radiobox == 'contribs-newbie' ) $target = 'newbies';
207 if ( !strlen( $target ) ) {
208 $wgOut->addHTML( contributionsForm( '' ) );
209 return;
212 $nt = Title::newFromURL( $target );
213 if ( !$nt ) {
214 $wgOut->addHTML( contributionsForm( '' ) );
215 return;
218 $options = array();
220 list( $options['limit'], $options['offset']) = wfCheckLimits();
221 $options['offset'] = $wgRequest->getVal( 'offset' );
223 /* Offset must be numeric or an empty string */
224 if ( !strlen( $options['offset'] ) || !preg_match( '/^[0-9]+$/', $options['offset'] ) )
225 $options['offset'] = '';
227 $title = SpecialPage::getTitleFor( 'Contributions' );
228 $options['target'] = $target;
230 $nt = Title::makeTitle( NS_USER, $nt->getDBkey() );
231 $finder = new ContribsFinder( ( $target == 'newbies' ) ? 'newbies' : $nt->getText() );
232 $finder->setLimit( $options['limit'] );
233 $finder->setOffset( $options['offset'] );
235 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
236 $options['namespace'] = intval( $ns );
237 $finder->setNamespace( $options['namespace'] );
238 } else {
239 $options['namespace'] = '';
242 if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) {
243 $options['bot'] = '1';
246 if ( $wgRequest->getText( 'go' ) == 'prev' ) {
247 $offset = $finder->getPreviousOffsetForPaging();
248 if ( $offset !== false ) {
249 $options['offset'] = $offset;
250 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
251 $wgOut->redirect( $prevurl );
252 return;
256 if ( $wgRequest->getText( 'go' ) == 'first' && $target != 'newbies') {
257 $offset = $finder->getFirstOffsetForPaging();
258 if ( $offset !== false ) {
259 $options['offset'] = $offset;
260 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
261 $wgOut->redirect( $prevurl );
262 return;
266 if ( $target == 'newbies' ) {
267 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
268 } else if ( preg_match( "/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/(?:24|16)/", $target ) ) {
269 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $target ) );
270 } else {
271 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub( $nt ) ) );
274 $id = User::idFromName( $nt->getText() );
275 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
277 $wgOut->addHTML( contributionsForm( $options) );
279 $contribs = $finder->find();
281 if ( count( $contribs ) == 0) {
282 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
283 return;
286 list( $early, $late ) = $finder->getEditLimits();
287 $lastts = count( $contribs ) ? wfTimestamp( TS_MW, $contribs[count( $contribs ) - 1]->rev_timestamp) : 0;
288 $atstart = ( !count( $contribs ) || $late == (wfTimestamp( TS_MW, $contribs[0]->rev_timestamp ) ));
289 $atend = ( !count( $contribs ) || $early == $lastts );
291 // These four are defaults
292 $newestlink = wfMsgHtml( 'sp-contributions-newest' );
293 $oldestlink = wfMsgHtml( 'sp-contributions-oldest' );
294 $newerlink = wfMsgHtml( 'sp-contributions-newer', $options['limit'] );
295 $olderlink = wfMsgHtml( 'sp-contributions-older', $options['limit'] );
297 if ( !$atstart ) {
298 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => '' ), $options ) );
299 $newestlink = "<a href=\"$stuff\">$newestlink</a>";
300 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'prev' ), $options ) );
301 $newerlink = "<a href=\"$stuff\">$newerlink</a>";
304 if ( !$atend ) {
305 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'first' ), $options ) );
306 $oldestlink = "<a href=\"$stuff\">$oldestlink</a>";
307 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => $lastts ), $options ) );
308 $olderlink = "<a href=\"$stuff\">$olderlink</a>";
311 if ( $target == 'newbies' ) {
312 $firstlast ="($newestlink)";
313 } else {
314 $firstlast = "($newestlink | $oldestlink)";
317 $urls = array();
318 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
319 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'limit' => $num ), $options ) );
320 $urls[] = "<a href=\"$stuff\">".$wgLang->formatNum( $num )."</a>";
322 $bits = implode( $urls, ' | ' );
324 $prevnextbits = $firstlast .' '. wfMsgHtml( 'viewprevnext', $newerlink, $olderlink, $bits );
326 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
328 $wgOut->addHTML( "<ul>\n" );
330 $sk = $wgUser->getSkin();
331 foreach ( $contribs as $contrib )
332 $wgOut->addHTML( ucListEdit( $sk, $contrib ) );
334 $wgOut->addHTML( "</ul>\n" );
335 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
337 # If there were contributions, and it was a valid user or IP, show
338 # the appropriate "footer" message - WHOIS tools, etc.
339 if( count( $contribs ) > 0 && $target != 'newbies' && $nt instanceof Title ) {
340 $message = IP::isIPAddress( $target )
341 ? 'sp-contributions-footer-anon'
342 : 'sp-contributions-footer';
343 $text = wfMsg( $message, $target );
344 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
345 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
346 $wgOut->addWikiText( wfMsg( $message, $target ) );
347 $wgOut->addHtml( '</div>' );
354 * Generates the subheading with links
355 * @param $nt see the Title object for the target
357 function contributionsSub( $nt ) {
358 global $wgSysopUserBans, $wgUser;
360 $sk = $wgUser->getSkin();
361 $id = User::idFromName( $nt->getText() );
363 if ( 0 == $id ) {
364 $ul = $nt->getText();
365 } else {
366 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
368 $talk = $nt->getTalkPage();
369 if( $talk ) {
370 # Talk page link
371 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
372 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
373 # Block link
374 if( $wgUser->isAllowed( 'block' ) )
375 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
376 # Block log link
377 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
379 # Other logs link
380 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
381 $ul .= ' (' . implode( ' | ', $tools ) . ')';
383 return $ul;
387 * Generates the namespace selector form with hidden attributes.
388 * @param $options Array: the options to be included.
390 function contributionsForm( $options ) {
391 global $wgScript, $wgTitle, $wgRequest;
393 $options['title'] = $wgTitle->getPrefixedText();
394 if (!isset($options['target']))
395 $options['target'] = '';
396 if (!isset($options['namespace']))
397 $options['namespace'] = 0;
399 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
401 foreach ( $options as $name => $value ) {
402 if( $name === 'namespace') continue;
403 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
406 $f .= '<fieldset>' .
407 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
408 Xml::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'newbie' , 'contribs-newbie' , 'contribs-newbie', 'contribs-newbie' ) . '<br />' .
409 Xml::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'newbie' , 'contribs-all', 'contribs-all', 'contribs-all' ) . ' ' .
410 Xml::input( 'target', 30, $options['target']) . ' '.
411 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
412 Xml::namespaceSelector( $options['namespace'], '' ) .
413 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
414 '</fieldset>' .
415 Xml::closeElement( 'form' );
416 return $f;
420 * Generates each row in the contributions list.
422 * Contributions which are marked "top" are currently on top of the history.
423 * For these contributions, a [rollback] link is shown for users with sysop
424 * privileges. The rollback link restores the most recent version that was not
425 * written by the target user.
427 * @todo This would probably look a lot nicer in a table.
429 function ucListEdit( $sk, $row ) {
430 $fname = 'ucListEdit';
431 wfProfileIn( $fname );
433 global $wgLang, $wgUser, $wgRequest;
434 static $messages;
435 if( !isset( $messages ) ) {
436 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
437 $messages[$msg] = wfMsgExt( $msg, array( 'escape') );
441 $rev = new Revision( $row );
443 $page = Title::makeTitle( $row->page_namespace, $row->page_title );
444 $link = $sk->makeKnownLinkObj( $page );
445 $difftext = $topmarktext = '';
446 if( $row->rev_id == $row->page_latest ) {
447 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
448 if( !$row->page_is_new ) {
449 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=0' ) . ')';
450 } else {
451 $difftext .= $messages['newarticle'];
454 if( $wgUser->isAllowed( 'rollback' ) ) {
455 $topmarktext .= ' '.$sk->generateRollback( $rev );
459 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
460 $difftext = '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
461 } else {
462 $difftext = '(' . $messages['diff'] . ')';
464 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
466 $comment = $sk->revComment( $rev );
467 $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
469 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
470 $d = '<span class="history-deleted">' . $d . '</span>';
473 if( $row->rev_minor_edit ) {
474 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
475 } else {
476 $mflag = '';
479 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
480 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
481 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
483 $ret = "<li>$ret</li>\n";
484 wfProfileOut( $fname );
485 return $ret;