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
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
36 * temporary: not stored in the database
37 * notificationtimestamp
38 * numberofWatchingusers
40 * @todo document functions and variables
44 var $mAttribs = array(), $mExtra = array();
45 var $mTitle = false, $mMovedToTitle = false;
46 var $numberofWatchingusers = 0 ; # Dummy to prevent error message in SpecialRecentchangeslinked
50 public static function newFromRow( $row )
52 $rc = new RecentChange
;
53 $rc->loadFromRow( $row );
57 public static function newFromCurRow( $row, $rc_this_oldid = 0 )
59 $rc = new RecentChange
;
60 $rc->loadFromCurRow( $row, $rc_this_oldid );
61 $rc->notificationtimestamp
= false;
62 $rc->numberofWatchingusers
= false;
67 * Obtain the recent change with a given rc_id value
69 * @param $rcid rc_id value to retrieve
70 * @return RecentChange
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 );
86 function setAttribs( $attribs )
88 $this->mAttribs
= $attribs;
91 function setExtra( $extra )
93 $this->mExtra
= $extra;
98 if ( $this->mTitle
=== false ) {
99 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
101 return $this->mTitle
;
104 function getMovedToTitle()
106 if ( $this->mMovedToTitle
=== false ) {
107 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
108 $this->mAttribs
['rc_moved_to_title'] );
110 return $this->mMovedToTitle
;
113 # Writes the data in this object to the database
116 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix;
117 $fname = 'RecentChange::save';
119 $dbw = wfGetDB( DB_MASTER
);
120 if ( !is_array($this->mExtra
) ) {
121 $this->mExtra
= array();
123 $this->mExtra
['lang'] = $wgLocalInterwiki;
125 if ( !$wgPutIPinRC ) {
126 $this->mAttribs
['rc_ip'] = '';
129 ## If our database is strict about IP addresses, use NULL instead of an empty string
130 if ( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
131 unset( $this->mAttribs
['rc_ip'] );
134 # Fixup database timestamps
135 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
136 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
137 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
139 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
140 if ( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
141 unset ( $this->mAttribs
['rc_cur_id'] );
145 $dbw->insert( 'recentchanges', $this->mAttribs
, $fname );
148 $this->mAttribs
['rc_id'] = $dbw->insertId();
150 # Update old rows, if necessary
151 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
152 $lastTime = $this->mExtra
['lastTimestamp'];
153 #$now = $this->mAttribs['rc_timestamp'];
154 #$curId = $this->mAttribs['rc_cur_id'];
156 # Don't bother looking for entries that have probably
157 # been purged, it just locks up the indexes needlessly.
159 $age = time() - wfTimestamp( TS_UNIX
, $lastTime );
160 if( $age < $wgRCMaxAge ) {
161 # live hack, will commit once tested - kate
162 # Update rc_this_oldid for the entries which were current
164 #$oldid = $this->mAttribs['rc_last_oldid'];
165 #$ns = $this->mAttribs['rc_namespace'];
166 #$title = $this->mAttribs['rc_title'];
168 #$dbw->update( 'recentchanges',
170 # 'rc_this_oldid' => $oldid
171 # ), array( /* WHERE */
172 # 'rc_namespace' => $ns,
173 # 'rc_title' => $title,
174 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
180 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
181 # array( 'rc_cur_id' => $curId ), $fname );
184 # Notify external application via UDP
185 if ( $wgRC2UDPAddress ) {
186 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
188 $line = $wgRC2UDPPrefix . $this->getIRCLine();
189 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
190 socket_close( $conn );
194 # E-mail notifications
197 # this would be better as an extension hook
199 include_once( "UserMailer.php" );
200 $enotif = new EmailNotification();
201 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
202 $enotif->notifyOnPageChange( $wgUser, $title,
203 $this->mAttribs
['rc_timestamp'],
204 $this->mAttribs
['rc_comment'],
205 $this->mAttribs
['rc_minor'],
206 $this->mAttribs
['rc_last_oldid'] );
210 wfRunHooks( 'RecentChange_save', array( &$this ) );
213 # Marks a certain row as patrolled
214 function markPatrolled( $rcid )
216 $fname = 'RecentChange::markPatrolled';
218 $dbw = wfGetDB( DB_MASTER
);
220 $dbw->update( 'recentchanges',
223 ), array( /* WHERE */
229 # Makes an entry in the database corresponding to an edit
230 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
231 $oldId, $lastTimestamp, $bot = "default", $ip = '', $oldSize = 0, $newSize = 0,
235 if ( $bot === 'default' ) {
236 $bot = $user->isAllowed( 'bot' );
246 $rc = new RecentChange
;
247 $rc->mAttribs
= array(
248 'rc_timestamp' => $timestamp,
249 'rc_cur_time' => $timestamp,
250 'rc_namespace' => $title->getNamespace(),
251 'rc_title' => $title->getDBkey(),
252 'rc_type' => RC_EDIT
,
253 'rc_minor' => $minor ?
1 : 0,
254 'rc_cur_id' => $title->getArticleID(),
255 'rc_user' => $user->getID(),
256 'rc_user_text' => $user->getName(),
257 'rc_comment' => $comment,
258 'rc_this_oldid' => $newId,
259 'rc_last_oldid' => $oldId,
260 'rc_bot' => $bot ?
1 : 0,
261 'rc_moved_to_ns' => 0,
262 'rc_moved_to_title' => '',
265 'rc_new' => 0, # obsolete
266 'rc_old_len' => $oldSize,
267 'rc_new_len' => $newSize
271 'prefixedDBkey' => $title->getPrefixedDBkey(),
272 'lastTimestamp' => $lastTimestamp,
273 'oldSize' => $oldSize,
274 'newSize' => $newSize,
277 return( $rc->mAttribs
['rc_id'] );
281 * Makes an entry in the database corresponding to page creation
282 * Note: the title object must be loaded with the new id using resetArticleID()
283 * @todo Document parameters and return
285 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = 'default',
286 $ip='', $size = 0, $newId = 0 )
294 if ( $bot === 'default' ) {
295 $bot = $user->isAllowed( 'bot' );
298 $rc = new RecentChange
;
299 $rc->mAttribs
= array(
300 'rc_timestamp' => $timestamp,
301 'rc_cur_time' => $timestamp,
302 'rc_namespace' => $title->getNamespace(),
303 'rc_title' => $title->getDBkey(),
305 'rc_minor' => $minor ?
1 : 0,
306 'rc_cur_id' => $title->getArticleID(),
307 'rc_user' => $user->getID(),
308 'rc_user_text' => $user->getName(),
309 'rc_comment' => $comment,
310 'rc_this_oldid' => $newId,
311 'rc_last_oldid' => 0,
312 'rc_bot' => $bot ?
1 : 0,
313 'rc_moved_to_ns' => 0,
314 'rc_moved_to_title' => '',
317 'rc_new' => 1, # obsolete
319 'rc_new_len' => $size
323 'prefixedDBkey' => $title->getPrefixedDBkey(),
324 'lastTimestamp' => 0,
329 return( $rc->mAttribs
['rc_id'] );
332 # Makes an entry in the database corresponding to a rename
333 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
342 $rc = new RecentChange
;
343 $rc->mAttribs
= array(
344 'rc_timestamp' => $timestamp,
345 'rc_cur_time' => $timestamp,
346 'rc_namespace' => $oldTitle->getNamespace(),
347 'rc_title' => $oldTitle->getDBkey(),
348 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
350 'rc_cur_id' => $oldTitle->getArticleID(),
351 'rc_user' => $user->getID(),
352 'rc_user_text' => $user->getName(),
353 'rc_comment' => $comment,
354 'rc_this_oldid' => 0,
355 'rc_last_oldid' => 0,
356 'rc_bot' => $user->isAllowed( 'bot' ) ?
1 : 0,
357 'rc_moved_to_ns' => $newTitle->getNamespace(),
358 'rc_moved_to_title' => $newTitle->getDBkey(),
360 'rc_new' => 0, # obsolete
362 'rc_old_len' => NULL,
363 'rc_new_len' => NULL,
367 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
368 'lastTimestamp' => 0,
369 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
374 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
375 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
378 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
379 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
382 # A log entry is different to an edit in that previous revisions are
384 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
385 $type, $action, $target, $logComment, $params )
394 $rc = new RecentChange
;
395 $rc->mAttribs
= array(
396 'rc_timestamp' => $timestamp,
397 'rc_cur_time' => $timestamp,
398 'rc_namespace' => $title->getNamespace(),
399 'rc_title' => $title->getDBkey(),
402 'rc_cur_id' => $title->getArticleID(),
403 'rc_user' => $user->getID(),
404 'rc_user_text' => $user->getName(),
405 'rc_comment' => $comment,
406 'rc_this_oldid' => 0,
407 'rc_last_oldid' => 0,
408 'rc_bot' => $user->isAllowed( 'bot' ) ?
1 : 0,
409 'rc_moved_to_ns' => 0,
410 'rc_moved_to_title' => '',
413 'rc_new' => 0, # obsolete
414 'rc_old_len' => NULL,
415 'rc_new_len' => NULL,
418 'prefixedDBkey' => $title->getPrefixedDBkey(),
419 'lastTimestamp' => 0,
421 'logAction' => $action,
422 'logComment' => $logComment,
423 'logTarget' => $target,
424 'logParams' => $params
429 # Initialises the members of this object from a mysql row object
430 function loadFromRow( $row )
432 $this->mAttribs
= get_object_vars( $row );
433 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
434 $this->mExtra
= array();
437 # Makes a pseudo-RC entry from a cur row
438 function loadFromCurRow( $row )
440 $this->mAttribs
= array(
441 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
442 'rc_cur_time' => $row->rev_timestamp
,
443 'rc_user' => $row->rev_user
,
444 'rc_user_text' => $row->rev_user_text
,
445 'rc_namespace' => $row->page_namespace
,
446 'rc_title' => $row->page_title
,
447 'rc_comment' => $row->rev_comment
,
448 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
449 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
450 'rc_cur_id' => $row->page_id
,
451 'rc_this_oldid' => $row->rev_id
,
452 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
454 'rc_moved_to_ns' => 0,
455 'rc_moved_to_title' => '',
457 'rc_id' => $row->rc_id
,
458 'rc_patrolled' => $row->rc_patrolled
,
459 'rc_new' => $row->page_is_new
, # obsolete
460 'rc_old_len' => $row->rc_old_len
,
461 'rc_new_len' => $row->rc_new_len
,
464 $this->mExtra
= array();
468 * Get an attribute value
470 * @param $name Attribute name
473 public function getAttribute( $name ) {
474 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
478 * Gets the end part of the diff URL associated with this object
479 * Blank if no diff link should be displayed
481 function diffLinkTrail( $forceCur )
483 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
484 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
485 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
487 $trail .= '&diff=0' ;
489 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
497 function cleanupForIRC( $text ) {
498 return str_replace(array("\n", "\r"), array("", ""), $text);
501 function getIRCLine() {
502 global $wgUseRCPatrol;
504 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
505 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
506 extract($this->mAttribs
);
507 extract($this->mExtra
);
509 $titleObj =& $this->getTitle();
510 if ( $rc_type == RC_LOG
) {
511 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
513 $title = $titleObj->getPrefixedText();
515 $title = $this->cleanupForIRC( $title );
517 $bad = array("\n", "\r");
518 $empty = array("", "");
519 $title = $titleObj->getPrefixedText();
520 $title = str_replace($bad, $empty, $title);
522 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
523 if ( $rc_type == RC_LOG
) {
525 } elseif ( $rc_new && $wgUseRCPatrol ) {
526 $url = $titleObj->getInternalURL("rcid=$rc_id");
527 } else if ( $rc_new ) {
528 $url = $titleObj->getInternalURL();
529 } else if ( $wgUseRCPatrol ) {
530 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
532 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
535 if ( isset( $oldSize ) && isset( $newSize ) ) {
536 $szdiff = $newSize - $oldSize;
537 if ($szdiff < -500) {
538 $szdiff = "\002$szdiff\002";
539 } elseif ($szdiff >= 0) {
540 $szdiff = '+' . $szdiff ;
542 $szdiff = '(' . $szdiff . ')' ;
547 $user = $this->cleanupForIRC( $rc_user_text );
549 if ( $rc_type == RC_LOG
) {
550 $logTargetText = $logTarget->getPrefixedText();
551 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
554 $comment = $this->cleanupForIRC( $rc_comment );
555 $flag = ($rc_minor ?
"M" : "") . ($rc_new ?
"N" : "");
557 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
558 # no colour (\003) switches back to the term default
559 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
560 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
565 * Returns the change size (HTML).
566 * The lengths can be given optionally.
568 function getCharacterDifference( $old = 0, $new = 0 ) {
569 global $wgRCChangedSizeThreshold, $wgLang;
572 $old = $this->mAttribs
['rc_old_len'];
575 $new = $this->mAttribs
['rc_new_len'];
578 if( $old === NULL ||
$new === NULL ) {
582 $szdiff = $new - $old;
583 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
584 $wgLang->formatNum($szdiff) );
586 if( $szdiff < $wgRCChangedSizeThreshold ) {
587 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
588 } elseif( $szdiff === 0 ) {
589 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
590 } elseif( $szdiff > 0 ) {
591 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
593 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';