*check for sanitized IPv6 IPs
[lhc/web/wiklou.git] / includes / SpecialContributions.php
1 <?php
2 /**
3 * @addtogroup SpecialPage
4 */
5
6 class ContribsFinder {
7 var $username, $offset, $limit, $namespace;
8 var $dbr;
9
10 /**
11 * Constructor
12 * @param $username Username as a string
13 */
14 function ContribsFinder( $username ) {
15 $this->username = $username;
16 $this->namespace = false;
17 $this->dbr = wfGetDB( DB_SLAVE, 'contributions' );
18 }
19
20 function setNamespace( $ns ) {
21 $this->namespace = $ns;
22 }
23
24 function setLimit( $limit ) {
25 $this->limit = $limit;
26 }
27
28 function setOffset( $offset ) {
29 $this->offset = $offset;
30 }
31
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).
37 */
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";
47
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;
54 }
55 }
56
57 /**
58 * Get timestamps of first and last contributions made by the user.
59 * @return Array containing first rev_timestamp and last rev_timestamp.
60 */
61 function getEditLimits() {
62 return array(
63 $this->getEditLimit( "ASC" ),
64 $this->getEditLimit( "DESC" )
65 );
66 }
67
68 function getUserCond() {
69 $condition = '';
70
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);
79 } else if ( IP::isIPv6( $this->username ) ) {
80 # All stored IPs should be sanitized from now on, check for exact matches for reverse compatibility
81 $condition = '(rev_user_text=' . $this->dbr->addQuotes(IP::sanitizeIP($this->username)) . ' OR rev_user_text=' . $this->dbr->addQuotes($this->username) . ')';
82 }
83
84 if ( $condition == '' ) {
85 $condition = ' rev_user_text=' . $this->dbr->addQuotes( $this->username );
86 $index = 'usertext_timestamp';
87 } else {
88 #$condition = ' rev_user '.$condition ;
89 $index = 'user_timestamp';
90 }
91 return array( $index, $condition );
92 }
93
94 function getNamespaceCond() {
95 if ( $this->namespace !== false )
96 return ' AND page_namespace = ' . (int)$this->namespace;
97 return '';
98 }
99
100 /**
101 * @return Timestamp of first entry in previous page.
102 */
103 function getPreviousOffsetForPaging() {
104 list( $index, $usercond ) = $this->getUserCond();
105 $nscond = $this->getNamespaceCond();
106
107 $use_index = $this->dbr->useIndexClause( $index );
108 list( $page, $revision ) = $this->dbr->tableNamesN( 'page', 'revision' );
109
110 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
111 "WHERE page_id = rev_page AND rev_timestamp > '" . $this->offset . "' AND " .
112 $usercond . $nscond;
113 $sql .= " ORDER BY rev_timestamp ASC";
114 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
115 $res = $this->dbr->query( $sql );
116
117 $numRows = $this->dbr->numRows( $res );
118 if ( $numRows ) {
119 $this->dbr->dataSeek( $res, $numRows - 1 );
120 $row = $this->dbr->fetchObject( $res );
121 $offset = $row->rev_timestamp;
122 } else {
123 $offset = false;
124 }
125 $this->dbr->freeResult( $res );
126 return $offset;
127 }
128
129 /**
130 * @return Timestamp of first entry in next page.
131 */
132 function getFirstOffsetForPaging() {
133 list( $index, $usercond ) = $this->getUserCond();
134 $use_index = $this->dbr->useIndexClause( $index );
135 list( $page, $revision ) = $this->dbr->tableNamesN( 'page', 'revision' );
136 $nscond = $this->getNamespaceCond();
137 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
138 "WHERE page_id = rev_page AND " .
139 $usercond . $nscond;
140 $sql .= " ORDER BY rev_timestamp ASC";
141 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
142 $res = $this->dbr->query( $sql );
143
144 $numRows = $this->dbr->numRows( $res );
145 if ( $numRows ) {
146 $this->dbr->dataSeek( $res, $numRows - 1 );
147 $row = $this->dbr->fetchObject( $res );
148 $offset = $row->rev_timestamp;
149 } else {
150 $offset = false;
151 }
152 $this->dbr->freeResult( $res );
153 return $offset;
154 }
155
156 /* private */ function makeSql() {
157 $offsetQuery = '';
158
159 list( $page, $revision ) = $this->dbr->tableNamesN( 'page', 'revision' );
160 list( $index, $userCond ) = $this->getUserCond();
161
162 if ( $this->offset )
163 $offsetQuery = "AND rev_timestamp < '{$this->offset}'";
164
165 $nscond = $this->getNamespaceCond();
166 $use_index = $this->dbr->useIndexClause( $index );
167 $sql = 'SELECT ' .
168 'page_namespace,page_title,page_is_new,page_latest,'.
169 join(',', Revision::selectFields()).
170 " FROM $page,$revision $use_index " .
171 "WHERE page_id=rev_page AND $userCond $nscond $offsetQuery " .
172 'ORDER BY rev_timestamp DESC';
173 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
174 return $sql;
175 }
176
177 /**
178 * This do the search for the user given when creating the object.
179 * It should probably be the only public function in this class.
180 * @return Array of contributions.
181 */
182 function find() {
183 $contribs = array();
184 $res = $this->dbr->query( $this->makeSql(), __METHOD__ );
185 while ( $c = $this->dbr->fetchObject( $res ) )
186 $contribs[] = $c;
187 $this->dbr->freeResult( $res );
188 return $contribs;
189 }
190 };
191
192 /**
193 * Special page "user contributions".
194 * Shows a list of the contributions of a user.
195 *
196 * @return none
197 * @param $par String: (optional) user name of the user for which to show the contributions
198 */
199 function wfSpecialContributions( $par = null ) {
200 global $wgUser, $wgOut, $wgLang, $wgRequest;
201
202 $target = isset( $par ) ? $par : $wgRequest->getVal( 'target' );
203 $radiobox = $wgRequest->getVal( 'newbie' );
204
205 // check for radiobox
206 if ( $radiobox == 'contribs-newbie' ) $target = 'newbies';
207
208 if ( !strlen( $target ) ) {
209 $wgOut->addHTML( contributionsForm( '' ) );
210 return;
211 }
212
213 $nt = Title::newFromURL( $target );
214 if ( !$nt ) {
215 $wgOut->addHTML( contributionsForm( '' ) );
216 return;
217 }
218
219 $options = array();
220
221 list( $options['limit'], $options['offset']) = wfCheckLimits();
222 $options['offset'] = $wgRequest->getVal( 'offset' );
223
224 /* Offset must either be numeric, or a "standard" timestamp */
225 $dbr = wfGetDB( DB_SLAVE );
226 if ( !strlen( $options['offset'] )
227 || ($dbr->realTimestamps()
228 && !preg_match ( '/^\d\d\d\d\-\d\d\-\d\d \d\d:\d\d:\d\d(?:[+-]\d\d)?$/', $options['offset'] )
229 )
230 || (!$dbr->realTimestamps()
231 && !preg_match( '/^[0-9]+$/', $options['offset'] )
232 )
233 ) {
234 $options['offset'] = '';
235 }
236
237 $title = SpecialPage::getTitleFor( 'Contributions' );
238 $options['target'] = $target;
239
240 $nt = Title::makeTitle( NS_USER, $nt->getDBkey() );
241 $finder = new ContribsFinder( ( $target == 'newbies' ) ? 'newbies' : $nt->getText() );
242 $finder->setLimit( $options['limit'] );
243 $finder->setOffset( $options['offset'] );
244
245 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
246 $options['namespace'] = intval( $ns );
247 $finder->setNamespace( $options['namespace'] );
248 } else {
249 $options['namespace'] = '';
250 }
251
252 if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) {
253 $options['bot'] = '1';
254 }
255
256 if ( $wgRequest->getText( 'go' ) == 'prev' ) {
257 $offset = $finder->getPreviousOffsetForPaging();
258 if ( $offset !== false ) {
259 $options['offset'] = $offset;
260 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
261 $wgOut->redirect( $prevurl );
262 return;
263 }
264 }
265
266 if ( $wgRequest->getText( 'go' ) == 'first' && $target != 'newbies') {
267 $offset = $finder->getFirstOffsetForPaging();
268 if ( $offset !== false ) {
269 $options['offset'] = $offset;
270 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
271 $wgOut->redirect( $prevurl );
272 return;
273 }
274 }
275
276 if ( $target == 'newbies' ) {
277 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
278 } else if ( preg_match( "/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/(24|16)/", $target ) ) {
279 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $target ) );
280 } else {
281 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub( $nt ) ) );
282 }
283
284 $id = User::idFromName( $nt->getText() );
285 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
286
287 $wgOut->addHTML( contributionsForm( $options) );
288
289 $contribs = $finder->find();
290
291 if ( count( $contribs ) == 0) {
292 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
293 return;
294 }
295
296 list( $early, $late ) = $finder->getEditLimits();
297 $lastts = count( $contribs ) ? $contribs[count( $contribs ) - 1]->rev_timestamp : 0;
298 $atstart = ( !count( $contribs ) || $late == $contribs[0]->rev_timestamp );
299 $atend = ( !count( $contribs ) || $early == $lastts );
300
301 // These four are defaults
302 $newestlink = wfMsgHtml( 'sp-contributions-newest' );
303 $oldestlink = wfMsgHtml( 'sp-contributions-oldest' );
304 $newerlink = wfMsgHtml( 'sp-contributions-newer', $options['limit'] );
305 $olderlink = wfMsgHtml( 'sp-contributions-older', $options['limit'] );
306
307 if ( !$atstart ) {
308 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => '' ), $options ) );
309 $newestlink = "<a href=\"$stuff\">$newestlink</a>";
310 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'prev' ), $options ) );
311 $newerlink = "<a href=\"$stuff\">$newerlink</a>";
312 }
313
314 if ( !$atend ) {
315 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'first' ), $options ) );
316 $oldestlink = "<a href=\"$stuff\">$oldestlink</a>";
317 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => $lastts ), $options ) );
318 $olderlink = "<a href=\"$stuff\">$olderlink</a>";
319 }
320
321 if ( $target == 'newbies' ) {
322 $firstlast ="($newestlink)";
323 } else {
324 $firstlast = "($newestlink | $oldestlink)";
325 }
326
327 $urls = array();
328 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
329 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'limit' => $num ), $options ) );
330 $urls[] = "<a href=\"$stuff\">".$wgLang->formatNum( $num )."</a>";
331 }
332 $bits = implode( $urls, ' | ' );
333
334 $prevnextbits = $firstlast .' '. wfMsgHtml( 'viewprevnext', $newerlink, $olderlink, $bits );
335
336 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
337
338 $wgOut->addHTML( "<ul>\n" );
339
340 $sk = $wgUser->getSkin();
341 foreach ( $contribs as $contrib )
342 $wgOut->addHTML( ucListEdit( $sk, $contrib ) );
343
344 $wgOut->addHTML( "</ul>\n" );
345 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
346 }
347
348 /**
349 * Generates the subheading with links
350 * @param $nt @see Title object for the target
351 */
352 function contributionsSub( $nt ) {
353 global $wgSysopUserBans, $wgLang, $wgUser;
354
355 $sk = $wgUser->getSkin();
356 $id = User::idFromName( $nt->getText() );
357
358 if ( 0 == $id ) {
359 $ul = $nt->getText();
360 } else {
361 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
362 }
363 $talk = $nt->getTalkPage();
364 if( $talk ) {
365 # Talk page link
366 $tools[] = $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK ) );
367 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
368 # Block link
369 if( $wgUser->isAllowed( 'block' ) )
370 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
371 # Block log link
372 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
373 }
374 # Other logs link
375 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
376 $ul .= ' (' . implode( ' | ', $tools ) . ')';
377 }
378 return $ul;
379 }
380
381 /**
382 * Generates the namespace selector form with hidden attributes.
383 * @param $options Array: the options to be included.
384 */
385 function contributionsForm( $options ) {
386 global $wgScript, $wgTitle, $wgRequest;
387
388 $options['title'] = $wgTitle->getPrefixedText();
389 if (!isset($options['target']))
390 $options['target'] = '';
391 if (!isset($options['namespace']))
392 $options['namespace'] = 0;
393
394 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
395
396 foreach ( $options as $name => $value ) {
397 if( $name === 'namespace') continue;
398 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
399 }
400
401 $f .= '<fieldset>' .
402 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
403 Xml::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'newbie' , 'contribs-newbie' , 'contribs-newbie', 'contribs-newbie' ) . '<br />' .
404 Xml::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'newbie' , 'contribs-all', 'contribs-all', 'contribs-all' ) . ' ' .
405 Xml::input( 'target', 20, $options['target']) . ' '.
406 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
407 Xml::namespaceSelector( $options['namespace'], '' ) .
408 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
409 '</fieldset>' .
410 Xml::closeElement( 'form' );
411 return $f;
412 }
413
414 /**
415 * Generates each row in the contributions list.
416 *
417 * Contributions which are marked "top" are currently on top of the history.
418 * For these contributions, a [rollback] link is shown for users with sysop
419 * privileges. The rollback link restores the most recent version that was not
420 * written by the target user.
421 *
422 * @todo This would probably look a lot nicer in a table.
423 */
424 function ucListEdit( $sk, $row ) {
425 $fname = 'ucListEdit';
426 wfProfileIn( $fname );
427
428 global $wgLang, $wgUser, $wgRequest;
429 static $messages;
430 if( !isset( $messages ) ) {
431 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
432 $messages[$msg] = wfMsgExt( $msg, array( 'escape') );
433 }
434 }
435
436 $rev = new Revision( $row );
437
438 $page = Title::makeTitle( $row->page_namespace, $row->page_title );
439 $link = $sk->makeKnownLinkObj( $page );
440 $difftext = $topmarktext = '';
441 if( $row->rev_id == $row->page_latest ) {
442 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
443 if( !$row->page_is_new ) {
444 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=0' ) . ')';
445 } else {
446 $difftext .= $messages['newarticle'];
447 }
448
449 if( $wgUser->isAllowed( 'rollback' ) ) {
450 $topmarktext .= ' '.$sk->generateRollback( $rev );
451 }
452
453 }
454 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
455 $difftext = '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
456 } else {
457 $difftext = '(' . $messages['diff'] . ')';
458 }
459 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
460
461 $comment = $sk->revComment( $rev );
462 $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
463
464 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
465 $d = '<span class="history-deleted">' . $d . '</span>';
466 }
467
468 if( $row->rev_minor_edit ) {
469 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
470 } else {
471 $mflag = '';
472 }
473
474 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
475 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
476 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
477 }
478 $ret = "<li>$ret</li>\n";
479 wfProfileOut( $fname );
480 return $ret;
481 }
482
483 ?>