8 * Utility class for creating new RC entries
10 * rc_id id of the row in the recentchanges table
11 * rc_timestamp time the entry was made
12 * rc_cur_time timestamp on the cur row
13 * rc_namespace namespace #
14 * rc_title non-prefixed db key
15 * rc_type is new entry, used to determine whether updating is necessary
17 * rc_cur_id page_id of associated page entry
18 * rc_user user id who made the entry
19 * rc_user_text user name who made the entry
20 * rc_comment edit summary
21 * rc_this_oldid rev_id associated with this entry (or zero)
22 * rc_last_oldid rev_id associated with the entry before this one (or zero)
23 * rc_bot is bot, hidden
24 * rc_ip IP address of the user in dotted quad notation
25 * rc_new obsolete, use rc_type==RC_NEW
26 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
27 * rc_old_len integer byte length of the text before the edit
28 * rc_new_len the same after the edit
31 * prefixedDBkey prefixed db key, used by external app via msg queue
32 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
33 * lang the interwiki prefix, automatically set in save()
34 * oldSize text size before the change
35 * newSize text size after the change
37 * temporary: not stored in the database
38 * notificationtimestamp
39 * numberofWatchingusers
41 * @todo document functions and variables
46 var $mAttribs = array(), $mExtra = array();
47 var $mTitle = false, $mMovedToTitle = false;
48 var $numberofWatchingusers = 0 ; # Dummy to prevent error message in SpecialRecentchangeslinked
52 /* static */ function newFromRow( $row )
54 $rc = new RecentChange
;
55 $rc->loadFromRow( $row );
59 public static function newFromCurRow( $row, $rc_this_oldid = 0 )
61 $rc = new RecentChange
;
62 $rc->loadFromCurRow( $row, $rc_this_oldid );
63 $rc->notificationtimestamp
= false;
64 $rc->numberofWatchingusers
= false;
69 * Obtain the recent change with a given rc_id value
71 * @param $rcid rc_id value to retrieve
72 * @return RecentChange
74 public static function newFromId( $rcid ) {
75 $dbr =& wfGetDB( DB_SLAVE
);
76 $res = $dbr->select( 'recentchanges', '*', array( 'rc_id' => $rcid ), __METHOD__
);
77 if( $res && $dbr->numRows( $res ) > 0 ) {
78 $row = $dbr->fetchObject( $res );
79 $dbr->freeResult( $res );
80 return self
::newFromRow( $row );
88 function setAttribs( $attribs )
90 $this->mAttribs
= $attribs;
93 function setExtra( $extra )
95 $this->mExtra
= $extra;
100 if ( $this->mTitle
=== false ) {
101 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
103 return $this->mTitle
;
106 function getMovedToTitle()
108 if ( $this->mMovedToTitle
=== false ) {
109 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
110 $this->mAttribs
['rc_moved_to_title'] );
112 return $this->mMovedToTitle
;
115 # Writes the data in this object to the database
118 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix;
119 $fname = 'RecentChange::save';
121 $dbw =& wfGetDB( DB_MASTER
);
122 if ( !is_array($this->mExtra
) ) {
123 $this->mExtra
= array();
125 $this->mExtra
['lang'] = $wgLocalInterwiki;
127 if ( !$wgPutIPinRC ) {
128 $this->mAttribs
['rc_ip'] = '';
131 ## If our database is strict about IP addresses, use NULL instead of an empty string
132 if ( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
133 unset( $this->mAttribs
['rc_ip'] );
136 # Fixup database timestamps
137 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
138 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
139 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
141 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
142 if ( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
143 unset ( $this->mAttribs
['rc_cur_id'] );
147 $dbw->insert( 'recentchanges', $this->mAttribs
, $fname );
150 $this->mAttribs
['rc_id'] = $dbw->insertId();
152 # Update old rows, if necessary
153 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
154 $lastTime = $this->mExtra
['lastTimestamp'];
155 #$now = $this->mAttribs['rc_timestamp'];
156 #$curId = $this->mAttribs['rc_cur_id'];
158 # Don't bother looking for entries that have probably
159 # been purged, it just locks up the indexes needlessly.
161 $age = time() - wfTimestamp( TS_UNIX
, $lastTime );
162 if( $age < $wgRCMaxAge ) {
163 # live hack, will commit once tested - kate
164 # Update rc_this_oldid for the entries which were current
166 #$oldid = $this->mAttribs['rc_last_oldid'];
167 #$ns = $this->mAttribs['rc_namespace'];
168 #$title = $this->mAttribs['rc_title'];
170 #$dbw->update( 'recentchanges',
172 # 'rc_this_oldid' => $oldid
173 # ), array( /* WHERE */
174 # 'rc_namespace' => $ns,
175 # 'rc_title' => $title,
176 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
182 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
183 # array( 'rc_cur_id' => $curId ), $fname );
186 # Notify external application via UDP
187 if ( $wgRC2UDPAddress ) {
188 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
190 $line = $wgRC2UDPPrefix . $this->getIRCLine();
191 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
192 socket_close( $conn );
196 # E-mail notifications
199 # this would be better as an extension hook
200 include_once( "UserMailer.php" );
201 $enotif = new EmailNotification();
202 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
203 $enotif->notifyOnPageChange( $title,
204 $this->mAttribs
['rc_timestamp'],
205 $this->mAttribs
['rc_comment'],
206 $this->mAttribs
['rc_minor'],
207 $this->mAttribs
['rc_last_oldid'] );
211 wfRunHooks( 'RecentChange_save', array( &$this ) );
214 # Marks a certain row as patrolled
215 function markPatrolled( $rcid )
217 $fname = 'RecentChange::markPatrolled';
219 $dbw =& wfGetDB( DB_MASTER
);
221 $dbw->update( 'recentchanges',
224 ), array( /* WHERE */
230 # Makes an entry in the database corresponding to an edit
231 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
232 $oldId, $lastTimestamp, $bot = "default", $ip = '', $oldSize = 0, $newSize = 0,
236 if ( $bot === 'default' ) {
237 $bot = $user->isAllowed( 'bot' );
247 $rc = new RecentChange
;
248 $rc->mAttribs
= array(
249 'rc_timestamp' => $timestamp,
250 'rc_cur_time' => $timestamp,
251 'rc_namespace' => $title->getNamespace(),
252 'rc_title' => $title->getDBkey(),
253 'rc_type' => RC_EDIT
,
254 'rc_minor' => $minor ?
1 : 0,
255 'rc_cur_id' => $title->getArticleID(),
256 'rc_user' => $user->getID(),
257 'rc_user_text' => $user->getName(),
258 'rc_comment' => $comment,
259 'rc_this_oldid' => $newId,
260 'rc_last_oldid' => $oldId,
261 'rc_bot' => $bot ?
1 : 0,
262 'rc_moved_to_ns' => 0,
263 'rc_moved_to_title' => '',
266 'rc_new' => 0, # obsolete
267 'rc_old_len' => $oldSize,
268 'rc_new_len' => $newSize
272 'prefixedDBkey' => $title->getPrefixedDBkey(),
273 'lastTimestamp' => $lastTimestamp,
274 'oldSize' => $oldSize,
275 'newSize' => $newSize,
278 return( $rc->mAttribs
['rc_id'] );
282 * Makes an entry in the database corresponding to page creation
283 * Note: the title object must be loaded with the new id using resetArticleID()
284 * @todo Document parameters and return
288 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default",
289 $ip='', $size = 0, $newId = 0 )
297 if ( $bot == 'default' ) {
298 $bot = $user->isAllowed( 'bot' );
301 $rc = new RecentChange
;
302 $rc->mAttribs
= array(
303 'rc_timestamp' => $timestamp,
304 'rc_cur_time' => $timestamp,
305 'rc_namespace' => $title->getNamespace(),
306 'rc_title' => $title->getDBkey(),
308 'rc_minor' => $minor ?
1 : 0,
309 'rc_cur_id' => $title->getArticleID(),
310 'rc_user' => $user->getID(),
311 'rc_user_text' => $user->getName(),
312 'rc_comment' => $comment,
313 'rc_this_oldid' => $newId,
314 'rc_last_oldid' => 0,
315 'rc_bot' => $bot ?
1 : 0,
316 'rc_moved_to_ns' => 0,
317 'rc_moved_to_title' => '',
320 'rc_new' => 1, # obsolete
322 'rc_new_len' => $size
326 'prefixedDBkey' => $title->getPrefixedDBkey(),
327 'lastTimestamp' => 0,
332 return( $rc->mAttribs
['rc_id'] );
335 # Makes an entry in the database corresponding to a rename
336 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
345 $rc = new RecentChange
;
346 $rc->mAttribs
= array(
347 'rc_timestamp' => $timestamp,
348 'rc_cur_time' => $timestamp,
349 'rc_namespace' => $oldTitle->getNamespace(),
350 'rc_title' => $oldTitle->getDBkey(),
351 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
353 'rc_cur_id' => $oldTitle->getArticleID(),
354 'rc_user' => $user->getID(),
355 'rc_user_text' => $user->getName(),
356 'rc_comment' => $comment,
357 'rc_this_oldid' => 0,
358 'rc_last_oldid' => 0,
359 'rc_bot' => $user->isAllowed( 'bot' ) ?
1 : 0,
360 'rc_moved_to_ns' => $newTitle->getNamespace(),
361 'rc_moved_to_title' => $newTitle->getDBkey(),
363 'rc_new' => 0, # obsolete
365 'rc_old_len' => NULL,
366 'rc_new_len' => NULL,
370 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
371 'lastTimestamp' => 0,
372 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
377 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
378 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
381 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
382 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
385 # A log entry is different to an edit in that previous revisions are
387 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
388 $type, $action, $target, $logComment, $params )
397 $rc = new RecentChange
;
398 $rc->mAttribs
= array(
399 'rc_timestamp' => $timestamp,
400 'rc_cur_time' => $timestamp,
401 'rc_namespace' => $title->getNamespace(),
402 'rc_title' => $title->getDBkey(),
405 'rc_cur_id' => $title->getArticleID(),
406 'rc_user' => $user->getID(),
407 'rc_user_text' => $user->getName(),
408 'rc_comment' => $comment,
409 'rc_this_oldid' => 0,
410 'rc_last_oldid' => 0,
411 'rc_bot' => $user->isAllowed( 'bot' ) ?
1 : 0,
412 'rc_moved_to_ns' => 0,
413 'rc_moved_to_title' => '',
416 'rc_new' => 0, # obsolete
417 'rc_old_len' => NULL,
418 'rc_new_len' => NULL,
421 'prefixedDBkey' => $title->getPrefixedDBkey(),
422 'lastTimestamp' => 0,
424 'logAction' => $action,
425 'logComment' => $logComment,
426 'logTarget' => $target,
427 'logParams' => $params
432 # Initialises the members of this object from a mysql row object
433 function loadFromRow( $row )
435 $this->mAttribs
= get_object_vars( $row );
436 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
437 $this->mExtra
= array();
440 # Makes a pseudo-RC entry from a cur row
441 function loadFromCurRow( $row )
443 $this->mAttribs
= array(
444 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
445 'rc_cur_time' => $row->rev_timestamp
,
446 'rc_user' => $row->rev_user
,
447 'rc_user_text' => $row->rev_user_text
,
448 'rc_namespace' => $row->page_namespace
,
449 'rc_title' => $row->page_title
,
450 'rc_comment' => $row->rev_comment
,
451 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
452 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
453 'rc_cur_id' => $row->page_id
,
454 'rc_this_oldid' => $row->rev_id
,
455 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
457 'rc_moved_to_ns' => 0,
458 'rc_moved_to_title' => '',
460 'rc_id' => $row->rc_id
,
461 'rc_patrolled' => $row->rc_patrolled
,
462 'rc_new' => $row->page_is_new
, # obsolete
463 'rc_old_len' => $row->rc_old_len
,
464 'rc_new_len' => $row->rc_new_len
,
467 $this->mExtra
= array();
471 * Get an attribute value
473 * @param $name Attribute name
476 public function getAttribute( $name ) {
477 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
481 * Gets the end part of the diff URL associated with this object
482 * Blank if no diff link should be displayed
484 function diffLinkTrail( $forceCur )
486 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
487 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
488 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
490 $trail .= '&diff=0' ;
492 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
500 function cleanupForIRC( $text ) {
501 return str_replace(array("\n", "\r"), array("", ""), $text);
504 function getIRCLine() {
505 global $wgUseRCPatrol;
507 extract($this->mAttribs
);
508 extract($this->mExtra
);
510 $titleObj =& $this->getTitle();
511 if ( $rc_type == RC_LOG
) {
512 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
514 $title = $titleObj->getPrefixedText();
516 $title = $this->cleanupForIRC( $title );
518 $bad = array("\n", "\r");
519 $empty = array("", "");
520 $title = $titleObj->getPrefixedText();
521 $title = str_replace($bad, $empty, $title);
523 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
524 if ( $rc_type == RC_LOG
) {
526 } elseif ( $rc_new && $wgUseRCPatrol ) {
527 $url = $titleObj->getInternalURL("rcid=$rc_id");
528 } else if ( $rc_new ) {
529 $url = $titleObj->getInternalURL();
530 } else if ( $wgUseRCPatrol ) {
531 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
533 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
536 if ( isset( $oldSize ) && isset( $newSize ) ) {
537 $szdiff = $newSize - $oldSize;
538 if ($szdiff < -500) {
539 $szdiff = "\002$szdiff\002";
540 } elseif ($szdiff >= 0) {
541 $szdiff = '+' . $szdiff ;
543 $szdiff = '(' . $szdiff . ')' ;
548 $user = $this->cleanupForIRC( $rc_user_text );
550 if ( $rc_type == RC_LOG
) {
551 $logTargetText = $logTarget->getPrefixedText();
552 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
555 $comment = $this->cleanupForIRC( $rc_comment );
556 $flag = ($rc_minor ?
"M" : "") . ($rc_new ?
"N" : "");
558 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
559 # no colour (\003) switches back to the term default
560 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
561 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
566 * Returns the change size (HTML).
567 * The lengths can be given optionally.
569 function getCharacterDifference( $old = 0, $new = 0 ) {
570 global $wgRCChangedSizeThreshold, $wgLang;
573 $old = $this->mAttribs
['rc_old_len'];
576 $new = $this->mAttribs
['rc_new_len'];
579 if( $old === NULL ||
$new === NULL ) {
583 $szdiff = $new - $old;
585 if( $szdiff < $wgRCChangedSizeThreshold ) {
586 return '<strong class=\'mw-plusminus-neg\'>(' . $wgLang->formatNum( $szdiff ) . ')</strong>';
587 } elseif( $szdiff === 0 ) {
588 return '<span class=\'mw-plusminus-null\'>(' . $wgLang->formatNum( $szdiff ) . ')</span>';
589 } elseif( $szdiff > 0 ) {
590 return '<span class=\'mw-plusminus-pos\'>(+' . $wgLang->formatNum( $szdiff ) . ')</span>';
592 return '<span class=\'mw-plusminus-neg\'>(' . $wgLang->formatNum( $szdiff ) . ')</span>';