* (bug 12574) Allow bots to specify whether an edit should be marked as a bot
[lhc/web/wiklou.git] / includes / RecentChange.php
1 <?php
2 /**
3 *
4 */
5
6 /**
7 * Utility class for creating new RC entries
8 * mAttribs:
9 * rc_id id of the row in the recentchanges table
10 * rc_timestamp time the entry was made
11 * rc_cur_time timestamp on the cur row
12 * rc_namespace namespace #
13 * rc_title non-prefixed db key
14 * rc_type is new entry, used to determine whether updating is necessary
15 * rc_minor is minor
16 * rc_cur_id page_id of associated page entry
17 * rc_user user id who made the entry
18 * rc_user_text user name who made the entry
19 * rc_comment edit summary
20 * rc_this_oldid rev_id associated with this entry (or zero)
21 * rc_last_oldid rev_id associated with the entry before this one (or zero)
22 * rc_bot is bot, hidden
23 * rc_ip IP address of the user in dotted quad notation
24 * rc_new obsolete, use rc_type==RC_NEW
25 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
26 * rc_old_len integer byte length of the text before the edit
27 * rc_new_len the same after the edit
28 *
29 * mExtra:
30 * prefixedDBkey prefixed db key, used by external app via msg queue
31 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
32 * lang the interwiki prefix, automatically set in save()
33 * oldSize text size before the change
34 * newSize text size after the change
35 *
36 * temporary: not stored in the database
37 * notificationtimestamp
38 * numberofWatchingusers
39 *
40 * @todo document functions and variables
41 */
42 class RecentChange
43 {
44 var $mAttribs = array(), $mExtra = array();
45 var $mTitle = false, $mMovedToTitle = false;
46 var $numberofWatchingusers = 0 ; # Dummy to prevent error message in SpecialRecentchangeslinked
47
48 # Factory methods
49
50 public static function newFromRow( $row )
51 {
52 $rc = new RecentChange;
53 $rc->loadFromRow( $row );
54 return $rc;
55 }
56
57 public static function newFromCurRow( $row, $rc_this_oldid = 0 )
58 {
59 $rc = new RecentChange;
60 $rc->loadFromCurRow( $row, $rc_this_oldid );
61 $rc->notificationtimestamp = false;
62 $rc->numberofWatchingusers = false;
63 return $rc;
64 }
65
66 /**
67 * Obtain the recent change with a given rc_id value
68 *
69 * @param $rcid rc_id value to retrieve
70 * @return RecentChange
71 */
72 public static function newFromId( $rcid ) {
73 $dbr = wfGetDB( DB_SLAVE );
74 $res = $dbr->select( 'recentchanges', '*', array( 'rc_id' => $rcid ), __METHOD__ );
75 if( $res && $dbr->numRows( $res ) > 0 ) {
76 $row = $dbr->fetchObject( $res );
77 $dbr->freeResult( $res );
78 return self::newFromRow( $row );
79 } else {
80 return NULL;
81 }
82 }
83
84 /**
85 * Find the first recent change matching some specific conditions
86 *
87 * @param array $conds Array of conditions
88 * @param mixed $fname Override the method name in profiling/logs
89 * @return RecentChange
90 */
91 public static function newFromConds( $conds, $fname = false ) {
92 if( $fname === false )
93 $fname = __METHOD__;
94 $dbr = wfGetDB( DB_SLAVE );
95 $res = $dbr->select(
96 'recentchanges',
97 '*',
98 $conds,
99 $fname
100 );
101 if( $res instanceof ResultWrapper && $res->numRows() > 0 ) {
102 $row = $res->fetchObject();
103 $res->free();
104 return self::newFromRow( $row );
105 }
106 return null;
107 }
108
109 # Accessors
110
111 function setAttribs( $attribs )
112 {
113 $this->mAttribs = $attribs;
114 }
115
116 function setExtra( $extra )
117 {
118 $this->mExtra = $extra;
119 }
120
121 function &getTitle()
122 {
123 if ( $this->mTitle === false ) {
124 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
125 }
126 return $this->mTitle;
127 }
128
129 function getMovedToTitle()
130 {
131 if ( $this->mMovedToTitle === false ) {
132 $this->mMovedToTitle = Title::makeTitle( $this->mAttribs['rc_moved_to_ns'],
133 $this->mAttribs['rc_moved_to_title'] );
134 }
135 return $this->mMovedToTitle;
136 }
137
138 # Writes the data in this object to the database
139 function save()
140 {
141 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix;
142 $fname = 'RecentChange::save';
143
144 $dbw = wfGetDB( DB_MASTER );
145 if ( !is_array($this->mExtra) ) {
146 $this->mExtra = array();
147 }
148 $this->mExtra['lang'] = $wgLocalInterwiki;
149
150 if ( !$wgPutIPinRC ) {
151 $this->mAttribs['rc_ip'] = '';
152 }
153
154 ## If our database is strict about IP addresses, use NULL instead of an empty string
155 if ( $dbw->strictIPs() and $this->mAttribs['rc_ip'] == '' ) {
156 unset( $this->mAttribs['rc_ip'] );
157 }
158
159 # Fixup database timestamps
160 $this->mAttribs['rc_timestamp'] = $dbw->timestamp($this->mAttribs['rc_timestamp']);
161 $this->mAttribs['rc_cur_time'] = $dbw->timestamp($this->mAttribs['rc_cur_time']);
162 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
163
164 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
165 if ( $dbw->cascadingDeletes() and $this->mAttribs['rc_cur_id']==0 ) {
166 unset ( $this->mAttribs['rc_cur_id'] );
167 }
168
169 # Insert new row
170 $dbw->insert( 'recentchanges', $this->mAttribs, $fname );
171
172 # Set the ID
173 $this->mAttribs['rc_id'] = $dbw->insertId();
174
175 # Update old rows, if necessary
176 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
177 $lastTime = $this->mExtra['lastTimestamp'];
178 #$now = $this->mAttribs['rc_timestamp'];
179 #$curId = $this->mAttribs['rc_cur_id'];
180
181 # Don't bother looking for entries that have probably
182 # been purged, it just locks up the indexes needlessly.
183 global $wgRCMaxAge;
184 $age = time() - wfTimestamp( TS_UNIX, $lastTime );
185 if( $age < $wgRCMaxAge ) {
186 # live hack, will commit once tested - kate
187 # Update rc_this_oldid for the entries which were current
188 #
189 #$oldid = $this->mAttribs['rc_last_oldid'];
190 #$ns = $this->mAttribs['rc_namespace'];
191 #$title = $this->mAttribs['rc_title'];
192 #
193 #$dbw->update( 'recentchanges',
194 # array( /* SET */
195 # 'rc_this_oldid' => $oldid
196 # ), array( /* WHERE */
197 # 'rc_namespace' => $ns,
198 # 'rc_title' => $title,
199 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
200 # ), $fname
201 #);
202 }
203
204 # Update rc_cur_time
205 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
206 # array( 'rc_cur_id' => $curId ), $fname );
207 }
208
209 # Notify external application via UDP
210 if ( $wgRC2UDPAddress ) {
211 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
212 if ( $conn ) {
213 $line = $wgRC2UDPPrefix . $this->getIRCLine();
214 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
215 socket_close( $conn );
216 }
217 }
218
219 # E-mail notifications
220 global $wgUseEnotif;
221 if( $wgUseEnotif ) {
222 # this would be better as an extension hook
223 global $wgUser;
224 $enotif = new EmailNotification;
225 $title = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
226 $enotif->notifyOnPageChange( $wgUser, $title,
227 $this->mAttribs['rc_timestamp'],
228 $this->mAttribs['rc_comment'],
229 $this->mAttribs['rc_minor'],
230 $this->mAttribs['rc_last_oldid'] );
231 }
232
233 # Notify extensions
234 wfRunHooks( 'RecentChange_save', array( &$this ) );
235 }
236
237 /**
238 * Mark a given change as patrolled
239 *
240 * @param mixed $change RecentChange or corresponding rc_id
241 */
242 public static function markPatrolled( $change ) {
243 $rcid = $change instanceof RecentChange
244 ? $change->mAttribs['rc_id']
245 : $change;
246 $dbw = wfGetDB( DB_MASTER );
247 $dbw->update(
248 'recentchanges',
249 array(
250 'rc_patrolled' => 1
251 ),
252 array(
253 'rc_id' => $rcid
254 ),
255 __METHOD__
256 );
257 }
258
259 # Makes an entry in the database corresponding to an edit
260 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
261 $oldId, $lastTimestamp, $bot = "default", $ip = '', $oldSize = 0, $newSize = 0,
262 $newId = 0)
263 {
264
265 global $wgRequest;
266
267 if ( $bot === 'default' ) {
268 $bot = $user->isAllowed( 'bot' );
269 if ( $bot ) {
270 $bot = $wgRequest->getBool( 'bot' , true );
271 }
272 }
273
274 if ( !$ip ) {
275 $ip = wfGetIP();
276 if ( !$ip ) {
277 $ip = '';
278 }
279 }
280
281 $rc = new RecentChange;
282 $rc->mAttribs = array(
283 'rc_timestamp' => $timestamp,
284 'rc_cur_time' => $timestamp,
285 'rc_namespace' => $title->getNamespace(),
286 'rc_title' => $title->getDBkey(),
287 'rc_type' => RC_EDIT,
288 'rc_minor' => $minor ? 1 : 0,
289 'rc_cur_id' => $title->getArticleID(),
290 'rc_user' => $user->getID(),
291 'rc_user_text' => $user->getName(),
292 'rc_comment' => $comment,
293 'rc_this_oldid' => $newId,
294 'rc_last_oldid' => $oldId,
295 'rc_bot' => $bot ? 1 : 0,
296 'rc_moved_to_ns' => 0,
297 'rc_moved_to_title' => '',
298 'rc_ip' => $ip,
299 'rc_patrolled' => 0,
300 'rc_new' => 0, # obsolete
301 'rc_old_len' => $oldSize,
302 'rc_new_len' => $newSize
303 );
304
305 $rc->mExtra = array(
306 'prefixedDBkey' => $title->getPrefixedDBkey(),
307 'lastTimestamp' => $lastTimestamp,
308 'oldSize' => $oldSize,
309 'newSize' => $newSize,
310 );
311 $rc->save();
312 return( $rc->mAttribs['rc_id'] );
313 }
314
315 /**
316 * Makes an entry in the database corresponding to page creation
317 * Note: the title object must be loaded with the new id using resetArticleID()
318 * @todo Document parameters and return
319 */
320 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = 'default',
321 $ip='', $size = 0, $newId = 0 )
322 {
323 global $wgRequest;
324
325 if ( !$ip ) {
326 $ip = wfGetIP();
327 if ( !$ip ) {
328 $ip = '';
329 }
330 }
331 if ( $bot === 'default' ) {
332 $bot = $user->isAllowed( 'bot' );
333 if ( $bot ) {
334 $bot = $wgRequest->getBool( 'bot' , true );
335 }
336 }
337
338 $rc = new RecentChange;
339 $rc->mAttribs = array(
340 'rc_timestamp' => $timestamp,
341 'rc_cur_time' => $timestamp,
342 'rc_namespace' => $title->getNamespace(),
343 'rc_title' => $title->getDBkey(),
344 'rc_type' => RC_NEW,
345 'rc_minor' => $minor ? 1 : 0,
346 'rc_cur_id' => $title->getArticleID(),
347 'rc_user' => $user->getID(),
348 'rc_user_text' => $user->getName(),
349 'rc_comment' => $comment,
350 'rc_this_oldid' => $newId,
351 'rc_last_oldid' => 0,
352 'rc_bot' => $bot ? 1 : 0,
353 'rc_moved_to_ns' => 0,
354 'rc_moved_to_title' => '',
355 'rc_ip' => $ip,
356 'rc_patrolled' => 0,
357 'rc_new' => 1, # obsolete
358 'rc_old_len' => 0,
359 'rc_new_len' => $size
360 );
361
362 $rc->mExtra = array(
363 'prefixedDBkey' => $title->getPrefixedDBkey(),
364 'lastTimestamp' => 0,
365 'oldSize' => 0,
366 'newSize' => $size
367 );
368 $rc->save();
369 return( $rc->mAttribs['rc_id'] );
370 }
371
372 # Makes an entry in the database corresponding to a rename
373 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
374 {
375 global $wgRequest;
376
377 if ( !$ip ) {
378 $ip = wfGetIP();
379 if ( !$ip ) {
380 $ip = '';
381 }
382 }
383
384 $rc = new RecentChange;
385 $rc->mAttribs = array(
386 'rc_timestamp' => $timestamp,
387 'rc_cur_time' => $timestamp,
388 'rc_namespace' => $oldTitle->getNamespace(),
389 'rc_title' => $oldTitle->getDBkey(),
390 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
391 'rc_minor' => 0,
392 'rc_cur_id' => $oldTitle->getArticleID(),
393 'rc_user' => $user->getID(),
394 'rc_user_text' => $user->getName(),
395 'rc_comment' => $comment,
396 'rc_this_oldid' => 0,
397 'rc_last_oldid' => 0,
398 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot' , true ) : 0,
399 'rc_moved_to_ns' => $newTitle->getNamespace(),
400 'rc_moved_to_title' => $newTitle->getDBkey(),
401 'rc_ip' => $ip,
402 'rc_new' => 0, # obsolete
403 'rc_patrolled' => 1,
404 'rc_old_len' => NULL,
405 'rc_new_len' => NULL,
406 );
407
408 $rc->mExtra = array(
409 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
410 'lastTimestamp' => 0,
411 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
412 );
413 $rc->save();
414 }
415
416 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
417 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
418 }
419
420 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
421 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
422 }
423
424 # A log entry is different to an edit in that previous revisions are
425 # not kept
426 public static function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
427 $type, $action, $target, $logComment, $params )
428 {
429 global $wgRequest;
430
431 if ( !$ip ) {
432 $ip = wfGetIP();
433 if ( !$ip ) {
434 $ip = '';
435 }
436 }
437
438 $rc = new RecentChange;
439 $rc->mAttribs = array(
440 'rc_timestamp' => $timestamp,
441 'rc_cur_time' => $timestamp,
442 'rc_namespace' => $title->getNamespace(),
443 'rc_title' => $title->getDBkey(),
444 'rc_type' => RC_LOG,
445 'rc_minor' => 0,
446 'rc_cur_id' => $title->getArticleID(),
447 'rc_user' => $user->getID(),
448 'rc_user_text' => $user->getName(),
449 'rc_comment' => $comment,
450 'rc_this_oldid' => 0,
451 'rc_last_oldid' => 0,
452 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot' , true ) : 0,
453 'rc_moved_to_ns' => 0,
454 'rc_moved_to_title' => '',
455 'rc_ip' => $ip,
456 'rc_patrolled' => 1,
457 'rc_new' => 0, # obsolete
458 'rc_old_len' => NULL,
459 'rc_new_len' => NULL,
460 );
461 $rc->mExtra = array(
462 'prefixedDBkey' => $title->getPrefixedDBkey(),
463 'lastTimestamp' => 0,
464 'logType' => $type,
465 'logAction' => $action,
466 'logComment' => $logComment,
467 'logTarget' => $target,
468 'logParams' => $params
469 );
470 $rc->save();
471 }
472
473 # Initialises the members of this object from a mysql row object
474 function loadFromRow( $row )
475 {
476 $this->mAttribs = get_object_vars( $row );
477 $this->mAttribs["rc_timestamp"] = wfTimestamp(TS_MW, $this->mAttribs["rc_timestamp"]);
478 $this->mExtra = array();
479 }
480
481 # Makes a pseudo-RC entry from a cur row
482 function loadFromCurRow( $row )
483 {
484 $this->mAttribs = array(
485 'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp),
486 'rc_cur_time' => $row->rev_timestamp,
487 'rc_user' => $row->rev_user,
488 'rc_user_text' => $row->rev_user_text,
489 'rc_namespace' => $row->page_namespace,
490 'rc_title' => $row->page_title,
491 'rc_comment' => $row->rev_comment,
492 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
493 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
494 'rc_cur_id' => $row->page_id,
495 'rc_this_oldid' => $row->rev_id,
496 'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0,
497 'rc_bot' => 0,
498 'rc_moved_to_ns' => 0,
499 'rc_moved_to_title' => '',
500 'rc_ip' => '',
501 'rc_id' => $row->rc_id,
502 'rc_patrolled' => $row->rc_patrolled,
503 'rc_new' => $row->page_is_new, # obsolete
504 'rc_old_len' => $row->rc_old_len,
505 'rc_new_len' => $row->rc_new_len,
506 );
507
508 $this->mExtra = array();
509 }
510
511 /**
512 * Get an attribute value
513 *
514 * @param $name Attribute name
515 * @return mixed
516 */
517 public function getAttribute( $name ) {
518 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : NULL;
519 }
520
521 /**
522 * Gets the end part of the diff URL associated with this object
523 * Blank if no diff link should be displayed
524 */
525 function diffLinkTrail( $forceCur )
526 {
527 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
528 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
529 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
530 if ( $forceCur ) {
531 $trail .= '&diff=0' ;
532 } else {
533 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
534 }
535 } else {
536 $trail = '';
537 }
538 return $trail;
539 }
540
541 function cleanupForIRC( $text ) {
542 return str_replace(array("\n", "\r"), array("", ""), $text);
543 }
544
545 function getIRCLine() {
546 global $wgUseRCPatrol;
547
548 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
549 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
550 extract($this->mAttribs);
551 extract($this->mExtra);
552
553 $titleObj =& $this->getTitle();
554 if ( $rc_type == RC_LOG ) {
555 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
556 } else {
557 $title = $titleObj->getPrefixedText();
558 }
559 $title = $this->cleanupForIRC( $title );
560
561 $bad = array("\n", "\r");
562 $empty = array("", "");
563 $title = $titleObj->getPrefixedText();
564 $title = str_replace($bad, $empty, $title);
565
566 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
567 if ( $rc_type == RC_LOG ) {
568 $url = '';
569 } elseif ( $rc_new && $wgUseRCPatrol ) {
570 $url = $titleObj->getInternalURL("rcid=$rc_id");
571 } else if ( $rc_new ) {
572 $url = $titleObj->getInternalURL();
573 } else if ( $wgUseRCPatrol ) {
574 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
575 } else {
576 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
577 }
578
579 if ( isset( $oldSize ) && isset( $newSize ) ) {
580 $szdiff = $newSize - $oldSize;
581 if ($szdiff < -500) {
582 $szdiff = "\002$szdiff\002";
583 } elseif ($szdiff >= 0) {
584 $szdiff = '+' . $szdiff ;
585 }
586 $szdiff = '(' . $szdiff . ')' ;
587 } else {
588 $szdiff = '';
589 }
590
591 $user = $this->cleanupForIRC( $rc_user_text );
592
593 if ( $rc_type == RC_LOG ) {
594 $logTargetText = $logTarget->getPrefixedText();
595 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
596 $flag = $logAction;
597 } else {
598 $comment = $this->cleanupForIRC( $rc_comment );
599 $flag = ($rc_minor ? "M" : "") . ($rc_new ? "N" : "");
600 }
601 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
602 # no colour (\003) switches back to the term default
603 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
604 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
605 return $fullString;
606 }
607
608 /**
609 * Returns the change size (HTML).
610 * The lengths can be given optionally.
611 */
612 function getCharacterDifference( $old = 0, $new = 0 ) {
613 global $wgRCChangedSizeThreshold, $wgLang;
614
615 if( $old === 0 ) {
616 $old = $this->mAttribs['rc_old_len'];
617 }
618 if( $new === 0 ) {
619 $new = $this->mAttribs['rc_new_len'];
620 }
621
622 if( $old === NULL || $new === NULL ) {
623 return '';
624 }
625
626 $szdiff = $new - $old;
627 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
628 $wgLang->formatNum($szdiff) );
629
630 if( $szdiff < $wgRCChangedSizeThreshold ) {
631 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
632 } elseif( $szdiff === 0 ) {
633 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
634 } elseif( $szdiff > 0 ) {
635 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
636 } else {
637 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';
638 }
639 }
640 }
641
642