7 * Utility class for creating new RC entries
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
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
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
49 var $mAttribs = array(), $mExtra = array();
50 var $mTitle = false, $mMovedToTitle = false;
51 var $numberofWatchingusers = 0 ; # Dummy to prevent error message in SpecialRecentchangeslinked
55 public static function newFromRow( $row )
57 $rc = new RecentChange
;
58 $rc->loadFromRow( $row );
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;
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 );
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 )
99 $dbr = wfGetDB( DB_SLAVE
);
106 if( $res instanceof ResultWrapper
&& $res->numRows() > 0 ) {
107 $row = $res->fetchObject();
109 return self
::newFromRow( $row );
116 function setAttribs( $attribs )
118 $this->mAttribs
= $attribs;
121 function setExtra( $extra )
123 $this->mExtra
= $extra;
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
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'] );
175 $dbw->insert( 'recentchanges', $this->mAttribs
, $fname );
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.
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',
200 # 'rc_this_oldid' => $oldid
201 # ), array( /* WHERE */
202 # 'rc_namespace' => $ns,
203 # 'rc_title' => $title,
204 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
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
);
218 $line = $wgRC2UDPPrefix . $this->getIRCLine();
219 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
220 socket_close( $conn );
224 # E-mail notifications
227 # this would be better as an extension hook
229 include_once( "UserMailer.php" );
230 $enotif = new EmailNotification();
231 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
232 $enotif->notifyOnPageChange( $wgUser, $title,
233 $this->mAttribs
['rc_timestamp'],
234 $this->mAttribs
['rc_comment'],
235 $this->mAttribs
['rc_minor'],
236 $this->mAttribs
['rc_last_oldid'] );
240 wfRunHooks( 'RecentChange_save', array( &$this ) );
244 * Mark a given change as patrolled
246 * @param mixed $change RecentChange or corresponding rc_id
248 public static function markPatrolled( $change ) {
249 $rcid = $change instanceof RecentChange
250 ?
$change->mAttribs
['rc_id']
252 $dbw = wfGetDB( DB_MASTER
);
265 # Makes an entry in the database corresponding to an edit
266 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
267 $oldId, $lastTimestamp, $bot = "default", $ip = '', $oldSize = 0, $newSize = 0,
271 if ( $bot === 'default' ) {
272 $bot = $user->isAllowed( 'bot' );
282 $rc = new RecentChange
;
283 $rc->mAttribs
= array(
284 'rc_timestamp' => $timestamp,
285 'rc_cur_time' => $timestamp,
286 'rc_namespace' => $title->getNamespace(),
287 'rc_title' => $title->getDBkey(),
288 'rc_type' => RC_EDIT
,
289 'rc_minor' => $minor ?
1 : 0,
290 'rc_cur_id' => $title->getArticleID(),
291 'rc_user' => $user->getID(),
292 'rc_user_text' => $user->getName(),
293 'rc_comment' => $comment,
294 'rc_this_oldid' => $newId,
295 'rc_last_oldid' => $oldId,
296 'rc_bot' => $bot ?
1 : 0,
297 'rc_moved_to_ns' => 0,
298 'rc_moved_to_title' => '',
301 'rc_new' => 0, # obsolete
302 'rc_old_len' => $oldSize,
303 'rc_new_len' => $newSize,
306 'rc_log_type' => null,
307 'rc_log_action' => '',
312 'prefixedDBkey' => $title->getPrefixedDBkey(),
313 'lastTimestamp' => $lastTimestamp,
314 'oldSize' => $oldSize,
315 'newSize' => $newSize,
318 return( $rc->mAttribs
['rc_id'] );
322 * Makes an entry in the database corresponding to page creation
323 * Note: the title object must be loaded with the new id using resetArticleID()
324 * @todo Document parameters and return
326 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = 'default',
327 $ip='', $size = 0, $newId = 0 )
333 if ( $bot === 'default' ) {
334 $bot = $user->isAllowed( 'bot' );
337 $rc = new RecentChange
;
338 $rc->mAttribs
= array(
339 'rc_timestamp' => $timestamp,
340 'rc_cur_time' => $timestamp,
341 'rc_namespace' => $title->getNamespace(),
342 'rc_title' => $title->getDBkey(),
344 'rc_minor' => $minor ?
1 : 0,
345 'rc_cur_id' => $title->getArticleID(),
346 'rc_user' => $user->getID(),
347 'rc_user_text' => $user->getName(),
348 'rc_comment' => $comment,
349 'rc_this_oldid' => $newId,
350 'rc_last_oldid' => 0,
351 'rc_bot' => $bot ?
1 : 0,
352 'rc_moved_to_ns' => 0,
353 'rc_moved_to_title' => '',
356 'rc_new' => 1, # obsolete
358 'rc_new_len' => $size,
361 'rc_log_type' => null,
362 'rc_log_action' => '',
367 'prefixedDBkey' => $title->getPrefixedDBkey(),
368 'lastTimestamp' => 0,
373 return( $rc->mAttribs
['rc_id'] );
376 # Makes an entry in the database corresponding to a rename
377 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
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
,
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' ) ?
1 : 0,
399 'rc_moved_to_ns' => $newTitle->getNamespace(),
400 'rc_moved_to_title' => $newTitle->getDBkey(),
402 'rc_new' => 0, # obsolete
404 'rc_old_len' => NULL,
405 'rc_new_len' => NULL,
407 'rc_logid' => 0, # notifyMove not used anymore
408 'rc_log_type' => null,
409 'rc_log_action' => '',
414 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
415 'lastTimestamp' => 0,
416 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
421 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
422 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
425 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
426 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
429 # A log entry is different to an edit in that previous revisions are not kept
430 public static function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
431 $type, $action, $target, $logComment, $params, $newId=0 )
438 $rc = new RecentChange
;
439 $rc->mAttribs
= array(
440 'rc_timestamp' => $timestamp,
441 'rc_cur_time' => $timestamp,
442 'rc_namespace' => $target->getNamespace(),
443 'rc_title' => $target->getDBkey(),
446 'rc_cur_id' => $target->getArticleID(),
447 'rc_user' => $user->getID(),
448 'rc_user_text' => $user->getName(),
449 'rc_comment' => $logComment,
450 'rc_this_oldid' => 0,
451 'rc_last_oldid' => 0,
452 'rc_bot' => $user->isAllowed( 'bot' ) ?
1 : 0,
453 'rc_moved_to_ns' => 0,
454 'rc_moved_to_title' => '',
457 'rc_new' => 0, # obsolete
458 'rc_old_len' => NULL,
459 'rc_new_len' => NULL,
461 'rc_logid' => $newId,
462 'rc_log_type' => $type,
463 'rc_log_action' => $action,
464 'rc_params' => $params
467 'prefixedDBkey' => $title->getPrefixedDBkey(),
468 'lastTimestamp' => 0,
470 'logAction' => $action,
471 'logComment' => $logComment,
472 'logTarget' => $target,
473 'logParams' => $params
478 # Initialises the members of this object from a mysql row object
479 function loadFromRow( $row )
481 $this->mAttribs
= get_object_vars( $row );
482 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
483 $this->mExtra
= array();
486 # Makes a pseudo-RC entry from a cur row
487 function loadFromCurRow( $row )
489 $this->mAttribs
= array(
490 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
491 'rc_cur_time' => $row->rev_timestamp
,
492 'rc_user' => $row->rev_user
,
493 'rc_user_text' => $row->rev_user_text
,
494 'rc_namespace' => $row->page_namespace
,
495 'rc_title' => $row->page_title
,
496 'rc_comment' => $row->rev_comment
,
497 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
498 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
499 'rc_cur_id' => $row->page_id
,
500 'rc_this_oldid' => $row->rev_id
,
501 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
503 'rc_moved_to_ns' => 0,
504 'rc_moved_to_title' => '',
506 'rc_id' => $row->rc_id
,
507 'rc_patrolled' => $row->rc_patrolled
,
508 'rc_new' => $row->page_is_new
, # obsolete
509 'rc_old_len' => $row->rc_old_len
,
510 'rc_new_len' => $row->rc_new_len
,
511 'rc_deleted' => $row->rc_deleted
,
512 'rc_logid' => $row->rc_logid
,
513 'rc_log_type' => $row->rc_log_type
,
514 'rc_log_action' => $row->rc_log_action
,
515 'rc_params' => $row->rc_params
518 $this->mExtra
= array();
522 * Get an attribute value
524 * @param $name Attribute name
527 public function getAttribute( $name ) {
528 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
532 * Gets the end part of the diff URL associated with this object
533 * Blank if no diff link should be displayed
535 function diffLinkTrail( $forceCur )
537 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
538 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
539 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
541 $trail .= '&diff=0' ;
543 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
551 function cleanupForIRC( $text ) {
552 return str_replace(array("\n", "\r"), array("", ""), $text);
555 function getIRCLine() {
556 global $wgUseRCPatrol;
558 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
559 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
560 extract($this->mAttribs
);
561 extract($this->mExtra
);
563 $titleObj =& $this->getTitle();
564 if ( $rc_type == RC_LOG
) {
565 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
567 $title = $titleObj->getPrefixedText();
569 $title = $this->cleanupForIRC( $title );
571 $bad = array("\n", "\r");
572 $empty = array("", "");
573 $title = $titleObj->getPrefixedText();
574 $title = str_replace($bad, $empty, $title);
576 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
577 if ( $rc_type == RC_LOG
) {
579 } elseif ( $rc_new && $wgUseRCPatrol ) {
580 $url = $titleObj->getInternalURL("rcid=$rc_id");
581 } else if ( $rc_new ) {
582 $url = $titleObj->getInternalURL();
583 } else if ( $wgUseRCPatrol ) {
584 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
586 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
589 if ( isset( $oldSize ) && isset( $newSize ) ) {
590 $szdiff = $newSize - $oldSize;
591 if ($szdiff < -500) {
592 $szdiff = "\002$szdiff\002";
593 } elseif ($szdiff >= 0) {
594 $szdiff = '+' . $szdiff ;
596 $szdiff = '(' . $szdiff . ')' ;
601 $user = $this->cleanupForIRC( $rc_user_text );
603 if ( $rc_type == RC_LOG
) {
604 $logTargetText = $logTarget->getPrefixedText();
605 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
608 $comment = $this->cleanupForIRC( $rc_comment );
609 $flag = ($rc_minor ?
"M" : "") . ($rc_new ?
"N" : "");
611 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
612 # no colour (\003) switches back to the term default
613 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
614 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
619 * Returns the change size (HTML).
620 * The lengths can be given optionally.
622 function getCharacterDifference( $old = 0, $new = 0 ) {
623 global $wgRCChangedSizeThreshold, $wgLang;
626 $old = $this->mAttribs
['rc_old_len'];
629 $new = $this->mAttribs
['rc_new_len'];
632 if( $old === NULL ||
$new === NULL ) {
636 $szdiff = $new - $old;
637 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
638 $wgLang->formatNum($szdiff) );
640 if( $szdiff < $wgRCChangedSizeThreshold ) {
641 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
642 } elseif( $szdiff === 0 ) {
643 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
644 } elseif( $szdiff > 0 ) {
645 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
647 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';