r32045 committed from wrong working branch. Revert and commit the one I wanted.
[mediawiki.git] / includes / RecentChange.php
blobac647b05403fa4be9e6034a28731299cc12d28f8
1 <?php
2 /**
4 */
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 * rc_deleted partial deletion
29 * rc_logid the log_id value for this log entry (or zero)
30 * rc_log_type the log type (or null)
31 * rc_log_action the log action (or null)
32 * rc_params log params
34 * mExtra:
35 * prefixedDBkey prefixed db key, used by external app via msg queue
36 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
37 * lang the interwiki prefix, automatically set in save()
38 * oldSize text size before the change
39 * newSize text size after the change
41 * temporary: not stored in the database
42 * notificationtimestamp
43 * numberofWatchingusers
45 * @todo document functions and variables
47 class RecentChange
49 var $mAttribs = array(), $mExtra = array();
50 var $mTitle = false, $mMovedToTitle = false;
51 var $numberofWatchingusers = 0 ; # Dummy to prevent error message in SpecialRecentchangeslinked
53 # Factory methods
55 public static function newFromRow( $row )
57 $rc = new RecentChange;
58 $rc->loadFromRow( $row );
59 return $rc;
62 public static function newFromCurRow( $row, $rc_this_oldid = 0 )
64 $rc = new RecentChange;
65 $rc->loadFromCurRow( $row, $rc_this_oldid );
66 $rc->notificationtimestamp = false;
67 $rc->numberofWatchingusers = false;
68 return $rc;
71 /**
72 * Obtain the recent change with a given rc_id value
74 * @param $rcid rc_id value to retrieve
75 * @return RecentChange
77 public static function newFromId( $rcid ) {
78 $dbr = wfGetDB( DB_SLAVE );
79 $res = $dbr->select( 'recentchanges', '*', array( 'rc_id' => $rcid ), __METHOD__ );
80 if( $res && $dbr->numRows( $res ) > 0 ) {
81 $row = $dbr->fetchObject( $res );
82 $dbr->freeResult( $res );
83 return self::newFromRow( $row );
84 } else {
85 return NULL;
89 /**
90 * Find the first recent change matching some specific conditions
92 * @param array $conds Array of conditions
93 * @param mixed $fname Override the method name in profiling/logs
94 * @return RecentChange
96 public static function newFromConds( $conds, $fname = false ) {
97 if( $fname === false )
98 $fname = __METHOD__;
99 $dbr = wfGetDB( DB_SLAVE );
100 $res = $dbr->select(
101 'recentchanges',
102 '*',
103 $conds,
104 $fname
106 if( $res instanceof ResultWrapper && $res->numRows() > 0 ) {
107 $row = $res->fetchObject();
108 $res->free();
109 return self::newFromRow( $row );
111 return null;
114 # Accessors
116 function setAttribs( $attribs )
118 $this->mAttribs = $attribs;
121 function setExtra( $extra )
123 $this->mExtra = $extra;
126 function &getTitle()
128 if ( $this->mTitle === false ) {
129 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
131 return $this->mTitle;
134 function getMovedToTitle()
136 if ( $this->mMovedToTitle === false ) {
137 $this->mMovedToTitle = Title::makeTitle( $this->mAttribs['rc_moved_to_ns'],
138 $this->mAttribs['rc_moved_to_title'] );
140 return $this->mMovedToTitle;
143 # Writes the data in this object to the database
144 function save()
146 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix;
147 $fname = 'RecentChange::save';
149 $dbw = wfGetDB( DB_MASTER );
150 if ( !is_array($this->mExtra) ) {
151 $this->mExtra = array();
153 $this->mExtra['lang'] = $wgLocalInterwiki;
155 if ( !$wgPutIPinRC ) {
156 $this->mAttribs['rc_ip'] = '';
159 ## If our database is strict about IP addresses, use NULL instead of an empty string
160 if ( $dbw->strictIPs() and $this->mAttribs['rc_ip'] == '' ) {
161 unset( $this->mAttribs['rc_ip'] );
164 # Fixup database timestamps
165 $this->mAttribs['rc_timestamp'] = $dbw->timestamp($this->mAttribs['rc_timestamp']);
166 $this->mAttribs['rc_cur_time'] = $dbw->timestamp($this->mAttribs['rc_cur_time']);
167 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
169 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
170 if ( $dbw->cascadingDeletes() and $this->mAttribs['rc_cur_id']==0 ) {
171 unset ( $this->mAttribs['rc_cur_id'] );
174 # Insert new row
175 $dbw->insert( 'recentchanges', $this->mAttribs, $fname );
177 # Set the ID
178 $this->mAttribs['rc_id'] = $dbw->insertId();
180 # Update old rows, if necessary
181 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
182 $lastTime = $this->mExtra['lastTimestamp'];
183 #$now = $this->mAttribs['rc_timestamp'];
184 #$curId = $this->mAttribs['rc_cur_id'];
186 # Don't bother looking for entries that have probably
187 # been purged, it just locks up the indexes needlessly.
188 global $wgRCMaxAge;
189 $age = time() - wfTimestamp( TS_UNIX, $lastTime );
190 if( $age < $wgRCMaxAge ) {
191 # live hack, will commit once tested - kate
192 # Update rc_this_oldid for the entries which were current
194 #$oldid = $this->mAttribs['rc_last_oldid'];
195 #$ns = $this->mAttribs['rc_namespace'];
196 #$title = $this->mAttribs['rc_title'];
198 #$dbw->update( 'recentchanges',
199 # array( /* SET */
200 # 'rc_this_oldid' => $oldid
201 # ), array( /* WHERE */
202 # 'rc_namespace' => $ns,
203 # 'rc_title' => $title,
204 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
205 # ), $fname
209 # Update rc_cur_time
210 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
211 # array( 'rc_cur_id' => $curId ), $fname );
214 # Notify external application via UDP
215 if ( $wgRC2UDPAddress ) {
216 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
217 if ( $conn ) {
218 $line = $wgRC2UDPPrefix . $this->getIRCLine();
219 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
220 socket_close( $conn );
224 # E-mail notifications
225 global $wgUseEnotif;
226 if( $wgUseEnotif ) {
227 # this would be better as an extension hook
228 global $wgUser;
229 $enotif = new EmailNotification;
230 $title = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
231 $enotif->notifyOnPageChange( $wgUser, $title,
232 $this->mAttribs['rc_timestamp'],
233 $this->mAttribs['rc_comment'],
234 $this->mAttribs['rc_minor'],
235 $this->mAttribs['rc_last_oldid'] );
238 # Notify extensions
239 wfRunHooks( 'RecentChange_save', array( &$this ) );
243 * Mark a given change as patrolled
245 * @param mixed $change RecentChange or corresponding rc_id
246 * @returns integer number of affected rows
248 public static function markPatrolled( $change ) {
249 $rcid = $change instanceof RecentChange
250 ? $change->mAttribs['rc_id']
251 : $change;
252 $dbw = wfGetDB( DB_MASTER );
253 $dbw->update(
254 'recentchanges',
255 array(
256 'rc_patrolled' => 1
258 array(
259 'rc_id' => $rcid
261 __METHOD__
263 return $dbw->affectedRows();
266 # Makes an entry in the database corresponding to an edit
267 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
268 $oldId, $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0,
269 $newId = 0)
271 if ( !$ip ) {
272 $ip = wfGetIP();
273 if ( !$ip ) {
274 $ip = '';
278 $rc = new RecentChange;
279 $rc->mAttribs = array(
280 'rc_timestamp' => $timestamp,
281 'rc_cur_time' => $timestamp,
282 'rc_namespace' => $title->getNamespace(),
283 'rc_title' => $title->getDBkey(),
284 'rc_type' => RC_EDIT,
285 'rc_minor' => $minor ? 1 : 0,
286 'rc_cur_id' => $title->getArticleID(),
287 'rc_user' => $user->getID(),
288 'rc_user_text' => $user->getName(),
289 'rc_comment' => $comment,
290 'rc_this_oldid' => $newId,
291 'rc_last_oldid' => $oldId,
292 'rc_bot' => $bot ? 1 : 0,
293 'rc_moved_to_ns' => 0,
294 'rc_moved_to_title' => '',
295 'rc_ip' => $ip,
296 'rc_patrolled' => 0,
297 'rc_new' => 0, # obsolete
298 'rc_old_len' => $oldSize,
299 'rc_new_len' => $newSize
302 $rc->mExtra = array(
303 'prefixedDBkey' => $title->getPrefixedDBkey(),
304 'lastTimestamp' => $lastTimestamp,
305 'oldSize' => $oldSize,
306 'newSize' => $newSize,
308 $rc->save();
309 return( $rc->mAttribs['rc_id'] );
313 * Makes an entry in the database corresponding to page creation
314 * Note: the title object must be loaded with the new id using resetArticleID()
315 * @todo Document parameters and return
317 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
318 $ip='', $size = 0, $newId = 0 )
320 if ( !$ip ) {
321 $ip = wfGetIP();
322 if ( !$ip ) {
323 $ip = '';
327 $rc = new RecentChange;
328 $rc->mAttribs = array(
329 'rc_timestamp' => $timestamp,
330 'rc_cur_time' => $timestamp,
331 'rc_namespace' => $title->getNamespace(),
332 'rc_title' => $title->getDBkey(),
333 'rc_type' => RC_NEW,
334 'rc_minor' => $minor ? 1 : 0,
335 'rc_cur_id' => $title->getArticleID(),
336 'rc_user' => $user->getID(),
337 'rc_user_text' => $user->getName(),
338 'rc_comment' => $comment,
339 'rc_this_oldid' => $newId,
340 'rc_last_oldid' => 0,
341 'rc_bot' => $bot ? 1 : 0,
342 'rc_moved_to_ns' => 0,
343 'rc_moved_to_title' => '',
344 'rc_ip' => $ip,
345 'rc_patrolled' => 0,
346 'rc_new' => 1, # obsolete
347 'rc_old_len' => 0,
348 'rc_new_len' => $size
351 $rc->mExtra = array(
352 'prefixedDBkey' => $title->getPrefixedDBkey(),
353 'lastTimestamp' => 0,
354 'oldSize' => 0,
355 'newSize' => $size
357 $rc->save();
358 return( $rc->mAttribs['rc_id'] );
361 # Makes an entry in the database corresponding to a rename
362 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
364 global $wgRequest;
366 if ( !$ip ) {
367 $ip = wfGetIP();
368 if ( !$ip ) {
369 $ip = '';
373 $rc = new RecentChange;
374 $rc->mAttribs = array(
375 'rc_timestamp' => $timestamp,
376 'rc_cur_time' => $timestamp,
377 'rc_namespace' => $oldTitle->getNamespace(),
378 'rc_title' => $oldTitle->getDBkey(),
379 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
380 'rc_minor' => 0,
381 'rc_cur_id' => $oldTitle->getArticleID(),
382 'rc_user' => $user->getID(),
383 'rc_user_text' => $user->getName(),
384 'rc_comment' => $comment,
385 'rc_this_oldid' => 0,
386 'rc_last_oldid' => 0,
387 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot' , true ) : 0,
388 'rc_moved_to_ns' => $newTitle->getNamespace(),
389 'rc_moved_to_title' => $newTitle->getDBkey(),
390 'rc_ip' => $ip,
391 'rc_new' => 0, # obsolete
392 'rc_patrolled' => 1,
393 'rc_old_len' => NULL,
394 'rc_new_len' => NULL,
397 $rc->mExtra = array(
398 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
399 'lastTimestamp' => 0,
400 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
402 $rc->save();
405 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
406 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
409 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
410 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
413 # A log entry is different to an edit in that previous revisions are
414 # not kept
415 public static function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
416 $type, $action, $target, $logComment, $params )
418 global $wgRequest;
420 if ( !$ip ) {
421 $ip = wfGetIP();
422 if ( !$ip ) {
423 $ip = '';
427 $rc = new RecentChange;
428 $rc->mAttribs = array(
429 'rc_timestamp' => $timestamp,
430 'rc_cur_time' => $timestamp,
431 'rc_namespace' => $title->getNamespace(),
432 'rc_title' => $title->getDBkey(),
433 'rc_type' => RC_LOG,
434 'rc_minor' => 0,
435 'rc_cur_id' => $title->getArticleID(),
436 'rc_user' => $user->getID(),
437 'rc_user_text' => $user->getName(),
438 'rc_comment' => $comment,
439 'rc_this_oldid' => 0,
440 'rc_last_oldid' => 0,
441 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot' , true ) : 0,
442 'rc_moved_to_ns' => 0,
443 'rc_moved_to_title' => '',
444 'rc_ip' => $ip,
445 'rc_patrolled' => 1,
446 'rc_new' => 0, # obsolete
447 'rc_old_len' => NULL,
448 'rc_new_len' => NULL,
450 $rc->mExtra = array(
451 'prefixedDBkey' => $title->getPrefixedDBkey(),
452 'lastTimestamp' => 0,
453 'logType' => $type,
454 'logAction' => $action,
455 'logComment' => $logComment,
456 'logTarget' => $target,
457 'logParams' => $params
459 $rc->save();
462 # Initialises the members of this object from a mysql row object
463 function loadFromRow( $row )
465 $this->mAttribs = get_object_vars( $row );
466 $this->mAttribs["rc_timestamp"] = wfTimestamp(TS_MW, $this->mAttribs["rc_timestamp"]);
467 $this->mExtra = array();
470 # Makes a pseudo-RC entry from a cur row
471 function loadFromCurRow( $row )
473 $this->mAttribs = array(
474 'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp),
475 'rc_cur_time' => $row->rev_timestamp,
476 'rc_user' => $row->rev_user,
477 'rc_user_text' => $row->rev_user_text,
478 'rc_namespace' => $row->page_namespace,
479 'rc_title' => $row->page_title,
480 'rc_comment' => $row->rev_comment,
481 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
482 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
483 'rc_cur_id' => $row->page_id,
484 'rc_this_oldid' => $row->rev_id,
485 'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0,
486 'rc_bot' => 0,
487 'rc_moved_to_ns' => 0,
488 'rc_moved_to_title' => '',
489 'rc_ip' => '',
490 'rc_id' => $row->rc_id,
491 'rc_patrolled' => $row->rc_patrolled,
492 'rc_new' => $row->page_is_new, # obsolete
493 'rc_old_len' => $row->rc_old_len,
494 'rc_new_len' => $row->rc_new_len,
495 'rc_params' => isset($row->rc_params) ? $row->rc_params : '',
496 'rc_log_type' => isset($row->rc_log_type) ? $row->rc_log_type : null,
497 'rc_log_action' => isset($row->rc_log_action) ? $row->rc_log_action : null,
498 'rc_log_id' => isset($row->rc_log_id) ? $row->rc_log_id: 0,
499 // this one REALLY should be set...
500 'rc_deleted' => isset($row->rc_deleted) ? $row->rc_deleted: 0,
503 $this->mExtra = array();
507 * Get an attribute value
509 * @param $name Attribute name
510 * @return mixed
512 public function getAttribute( $name ) {
513 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : NULL;
517 * Gets the end part of the diff URL associated with this object
518 * Blank if no diff link should be displayed
520 function diffLinkTrail( $forceCur )
522 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
523 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
524 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
525 if ( $forceCur ) {
526 $trail .= '&diff=0' ;
527 } else {
528 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
530 } else {
531 $trail = '';
533 return $trail;
536 function cleanupForIRC( $text ) {
537 return str_replace(array("\n", "\r"), array("", ""), $text);
540 function getIRCLine() {
541 global $wgUseRCPatrol;
543 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
544 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
545 extract($this->mAttribs);
546 extract($this->mExtra);
548 $titleObj =& $this->getTitle();
549 if ( $rc_type == RC_LOG ) {
550 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
551 } else {
552 $title = $titleObj->getPrefixedText();
554 $title = $this->cleanupForIRC( $title );
556 $bad = array("\n", "\r");
557 $empty = array("", "");
558 $title = $titleObj->getPrefixedText();
559 $title = str_replace($bad, $empty, $title);
561 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
562 if ( $rc_type == RC_LOG ) {
563 $url = '';
564 } elseif ( $rc_new && $wgUseRCPatrol ) {
565 $url = $titleObj->getInternalURL("rcid=$rc_id");
566 } else if ( $rc_new ) {
567 $url = $titleObj->getInternalURL();
568 } else if ( $wgUseRCPatrol ) {
569 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
570 } else {
571 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
574 if ( isset( $oldSize ) && isset( $newSize ) ) {
575 $szdiff = $newSize - $oldSize;
576 if ($szdiff < -500) {
577 $szdiff = "\002$szdiff\002";
578 } elseif ($szdiff >= 0) {
579 $szdiff = '+' . $szdiff ;
581 $szdiff = '(' . $szdiff . ')' ;
582 } else {
583 $szdiff = '';
586 $user = $this->cleanupForIRC( $rc_user_text );
588 if ( $rc_type == RC_LOG ) {
589 $logTargetText = $logTarget->getPrefixedText();
590 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
591 $flag = $logAction;
592 } else {
593 $comment = $this->cleanupForIRC( $rc_comment );
594 $flag = ($rc_minor ? "M" : "") . ($rc_new ? "N" : "");
596 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
597 # no colour (\003) switches back to the term default
598 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
599 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
600 return $fullString;
604 * Returns the change size (HTML).
605 * The lengths can be given optionally.
607 function getCharacterDifference( $old = 0, $new = 0 ) {
608 global $wgRCChangedSizeThreshold, $wgLang;
610 if( $old === 0 ) {
611 $old = $this->mAttribs['rc_old_len'];
613 if( $new === 0 ) {
614 $new = $this->mAttribs['rc_new_len'];
617 if( $old === NULL || $new === NULL ) {
618 return '';
621 $szdiff = $new - $old;
622 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
623 $wgLang->formatNum($szdiff) );
625 if( $szdiff < $wgRCChangedSizeThreshold ) {
626 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
627 } elseif( $szdiff === 0 ) {
628 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
629 } elseif( $szdiff > 0 ) {
630 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
631 } else {
632 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';