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
225 global $wgUseEnotif, $wgShowUpdatedMarker, $wgUser;
226 if( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
228 if( $this->mAttribs
['rc_user'] ) {
229 $editor = ($wgUser->getID() == $this->mAttribs
['rc_user']) ?
230 $wgUser : User
::newFromID( $this->mAttribs
['rc_user'] );
233 $editor = ($wgUser->getName() == $this->mAttribs
['rc_user_text']) ?
234 $wgUser : User
::newFromName( $this->mAttribs
['rc_user_text'], false );
236 # FIXME: this would be better as an extension hook
237 $enotif = new EmailNotification();
238 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
239 $enotif->notifyOnPageChange( $editor, $title,
240 $this->mAttribs
['rc_timestamp'],
241 $this->mAttribs
['rc_comment'],
242 $this->mAttribs
['rc_minor'],
243 $this->mAttribs
['rc_last_oldid'] );
247 wfRunHooks( 'RecentChange_save', array( &$this ) );
251 * Mark a given change as patrolled
253 * @param mixed $change RecentChange or corresponding rc_id
254 * @returns integer number of affected rows
256 public static function markPatrolled( $change ) {
257 $rcid = $change instanceof RecentChange
258 ?
$change->mAttribs
['rc_id']
260 $dbw = wfGetDB( DB_MASTER
);
271 return $dbw->affectedRows();
274 # Makes an entry in the database corresponding to an edit
275 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
276 $oldId, $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0,
286 $rc = new RecentChange
;
287 $rc->mAttribs
= array(
288 'rc_timestamp' => $timestamp,
289 'rc_cur_time' => $timestamp,
290 'rc_namespace' => $title->getNamespace(),
291 'rc_title' => $title->getDBkey(),
292 'rc_type' => RC_EDIT
,
293 'rc_minor' => $minor ?
1 : 0,
294 'rc_cur_id' => $title->getArticleID(),
295 'rc_user' => $user->getID(),
296 'rc_user_text' => $user->getName(),
297 'rc_comment' => $comment,
298 'rc_this_oldid' => $newId,
299 'rc_last_oldid' => $oldId,
300 'rc_bot' => $bot ?
1 : 0,
301 'rc_moved_to_ns' => 0,
302 'rc_moved_to_title' => '',
305 'rc_new' => 0, # obsolete
306 'rc_old_len' => $oldSize,
307 'rc_new_len' => $newSize,
310 'rc_log_type' => null,
311 'rc_log_action' => '',
316 'prefixedDBkey' => $title->getPrefixedDBkey(),
317 'lastTimestamp' => $lastTimestamp,
318 'oldSize' => $oldSize,
319 'newSize' => $newSize,
322 return( $rc->mAttribs
['rc_id'] );
326 * Makes an entry in the database corresponding to page creation
327 * Note: the title object must be loaded with the new id using resetArticleID()
328 * @todo Document parameters and return
330 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
331 $ip='', $size = 0, $newId = 0 )
340 $rc = new RecentChange
;
341 $rc->mAttribs
= array(
342 'rc_timestamp' => $timestamp,
343 'rc_cur_time' => $timestamp,
344 'rc_namespace' => $title->getNamespace(),
345 'rc_title' => $title->getDBkey(),
347 'rc_minor' => $minor ?
1 : 0,
348 'rc_cur_id' => $title->getArticleID(),
349 'rc_user' => $user->getID(),
350 'rc_user_text' => $user->getName(),
351 'rc_comment' => $comment,
352 'rc_this_oldid' => $newId,
353 'rc_last_oldid' => 0,
354 'rc_bot' => $bot ?
1 : 0,
355 'rc_moved_to_ns' => 0,
356 'rc_moved_to_title' => '',
359 'rc_new' => 1, # obsolete
361 'rc_new_len' => $size,
364 'rc_log_type' => null,
365 'rc_log_action' => '',
370 'prefixedDBkey' => $title->getPrefixedDBkey(),
371 'lastTimestamp' => 0,
376 return( $rc->mAttribs
['rc_id'] );
379 # Makes an entry in the database corresponding to a rename
380 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
391 $rc = new RecentChange
;
392 $rc->mAttribs
= array(
393 'rc_timestamp' => $timestamp,
394 'rc_cur_time' => $timestamp,
395 'rc_namespace' => $oldTitle->getNamespace(),
396 'rc_title' => $oldTitle->getDBkey(),
397 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
399 'rc_cur_id' => $oldTitle->getArticleID(),
400 'rc_user' => $user->getID(),
401 'rc_user_text' => $user->getName(),
402 'rc_comment' => $comment,
403 'rc_this_oldid' => 0,
404 'rc_last_oldid' => 0,
405 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
406 'rc_moved_to_ns' => $newTitle->getNamespace(),
407 'rc_moved_to_title' => $newTitle->getDBkey(),
409 'rc_new' => 0, # obsolete
411 'rc_old_len' => NULL,
412 'rc_new_len' => NULL,
414 'rc_logid' => 0, # notifyMove not used anymore
415 'rc_log_type' => null,
416 'rc_log_action' => '',
421 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
422 'lastTimestamp' => 0,
423 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
428 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
429 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
432 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
433 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
436 # A log entry is different to an edit in that previous revisions are not kept
437 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='',
438 $type, $action, $target, $logComment, $params, $newId=0 )
449 $rc = new RecentChange
;
450 $rc->mAttribs
= array(
451 'rc_timestamp' => $timestamp,
452 'rc_cur_time' => $timestamp,
453 'rc_namespace' => $target->getNamespace(),
454 'rc_title' => $target->getDBkey(),
457 'rc_cur_id' => $target->getArticleID(),
458 'rc_user' => $user->getID(),
459 'rc_user_text' => $user->getName(),
460 'rc_comment' => $logComment,
461 'rc_this_oldid' => 0,
462 'rc_last_oldid' => 0,
463 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
464 'rc_moved_to_ns' => 0,
465 'rc_moved_to_title' => '',
468 'rc_new' => 0, # obsolete
469 'rc_old_len' => NULL,
470 'rc_new_len' => NULL,
472 'rc_logid' => $newId,
473 'rc_log_type' => $type,
474 'rc_log_action' => $action,
475 'rc_params' => $params
478 'prefixedDBkey' => $title->getPrefixedDBkey(),
479 'lastTimestamp' => 0,
480 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
485 # Initialises the members of this object from a mysql row object
486 function loadFromRow( $row )
488 $this->mAttribs
= get_object_vars( $row );
489 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
490 $this->mExtra
= array();
493 # Makes a pseudo-RC entry from a cur row
494 function loadFromCurRow( $row )
496 $this->mAttribs
= array(
497 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
498 'rc_cur_time' => $row->rev_timestamp
,
499 'rc_user' => $row->rev_user
,
500 'rc_user_text' => $row->rev_user_text
,
501 'rc_namespace' => $row->page_namespace
,
502 'rc_title' => $row->page_title
,
503 'rc_comment' => $row->rev_comment
,
504 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
505 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
506 'rc_cur_id' => $row->page_id
,
507 'rc_this_oldid' => $row->rev_id
,
508 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
510 'rc_moved_to_ns' => 0,
511 'rc_moved_to_title' => '',
513 'rc_id' => $row->rc_id
,
514 'rc_patrolled' => $row->rc_patrolled
,
515 'rc_new' => $row->page_is_new
, # obsolete
516 'rc_old_len' => $row->rc_old_len
,
517 'rc_new_len' => $row->rc_new_len
,
518 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
519 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
520 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
521 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
522 // this one REALLY should be set...
523 'rc_deleted' => isset($row->rc_deleted
) ?
$row->rc_deleted
: 0,
526 $this->mExtra
= array();
530 * Get an attribute value
532 * @param $name Attribute name
535 public function getAttribute( $name ) {
536 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
540 * Gets the end part of the diff URL associated with this object
541 * Blank if no diff link should be displayed
543 function diffLinkTrail( $forceCur )
545 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
546 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
547 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
549 $trail .= '&diff=0' ;
551 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
559 function cleanupForIRC( $text ) {
560 return str_replace(array("\n", "\r"), array("", ""), $text);
563 function getIRCLine() {
564 global $wgUseRCPatrol;
566 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
567 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
568 extract($this->mAttribs
);
569 extract($this->mExtra
);
571 if ( $rc_type == RC_LOG
) {
572 $titleObj = Title
::newFromText( "Log/$rc_log_type", NS_SPECIAL
);
574 $titleObj =& $this->getTitle();
576 $title = $titleObj->getPrefixedText();
577 $title = $this->cleanupForIRC( $title );
579 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
580 if ( $rc_type == RC_LOG
) {
582 } elseif ( $rc_new && $wgUseRCPatrol ) {
583 $url = $titleObj->getInternalURL("rcid=$rc_id");
584 } else if ( $rc_new ) {
585 $url = $titleObj->getInternalURL();
586 } else if ( $wgUseRCPatrol ) {
587 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
589 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
592 if ( isset( $oldSize ) && isset( $newSize ) ) {
593 $szdiff = $newSize - $oldSize;
594 if ($szdiff < -500) {
595 $szdiff = "\002$szdiff\002";
596 } elseif ($szdiff >= 0) {
597 $szdiff = '+' . $szdiff ;
599 $szdiff = '(' . $szdiff . ')' ;
604 $user = $this->cleanupForIRC( $rc_user_text );
606 if ( $rc_type == RC_LOG
) {
607 $logTargetText = $this->getTitle()->getPrefixedText();
608 $comment = $this->cleanupForIRC( str_replace($logTargetText,"\00302$logTargetText\00310",$actionComment) );
609 $flag = $rc_log_action;
611 $comment = $this->cleanupForIRC( $rc_comment );
612 $flag = ($rc_minor ?
"M" : "") . ($rc_new ?
"N" : "");
614 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
615 # no colour (\003) switches back to the term default
616 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
617 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
622 * Returns the change size (HTML).
623 * The lengths can be given optionally.
625 function getCharacterDifference( $old = 0, $new = 0 ) {
626 global $wgRCChangedSizeThreshold, $wgLang;
629 $old = $this->mAttribs
['rc_old_len'];
632 $new = $this->mAttribs
['rc_new_len'];
635 if( $old === NULL ||
$new === NULL ) {
639 $szdiff = $new - $old;
640 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
641 $wgLang->formatNum($szdiff) );
643 if( $szdiff < $wgRCChangedSizeThreshold ) {
644 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
645 } elseif( $szdiff === 0 ) {
646 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
647 } elseif( $szdiff > 0 ) {
648 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
650 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';