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