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 character count 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;
70 function setAttribs( $attribs )
72 $this->mAttribs
= $attribs;
75 function setExtra( $extra )
77 $this->mExtra
= $extra;
82 if ( $this->mTitle
=== false ) {
83 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
88 function getMovedToTitle()
90 if ( $this->mMovedToTitle
=== false ) {
91 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
92 $this->mAttribs
['rc_moved_to_title'] );
94 return $this->mMovedToTitle
;
97 # Writes the data in this object to the database
100 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix;
101 $fname = 'RecentChange::save';
103 $dbw =& wfGetDB( DB_MASTER
);
104 if ( !is_array($this->mExtra
) ) {
105 $this->mExtra
= array();
107 $this->mExtra
['lang'] = $wgLocalInterwiki;
109 if ( !$wgPutIPinRC ) {
110 $this->mAttribs
['rc_ip'] = '';
113 ## If our database is strict about IP addresses, use NULL instead of an empty string
114 if ( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
115 unset( $this->mAttribs
['rc_ip'] );
118 # Fixup database timestamps
119 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
120 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
121 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
123 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
124 if ( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
125 unset ( $this->mAttribs
['rc_cur_id'] );
129 $dbw->insert( 'recentchanges', $this->mAttribs
, $fname );
132 $this->mAttribs
['rc_id'] = $dbw->insertId();
134 # Update old rows, if necessary
135 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
136 $lastTime = $this->mExtra
['lastTimestamp'];
137 #$now = $this->mAttribs['rc_timestamp'];
138 #$curId = $this->mAttribs['rc_cur_id'];
140 # Don't bother looking for entries that have probably
141 # been purged, it just locks up the indexes needlessly.
143 $age = time() - wfTimestamp( TS_UNIX
, $lastTime );
144 if( $age < $wgRCMaxAge ) {
145 # live hack, will commit once tested - kate
146 # Update rc_this_oldid for the entries which were current
148 #$oldid = $this->mAttribs['rc_last_oldid'];
149 #$ns = $this->mAttribs['rc_namespace'];
150 #$title = $this->mAttribs['rc_title'];
152 #$dbw->update( 'recentchanges',
154 # 'rc_this_oldid' => $oldid
155 # ), array( /* WHERE */
156 # 'rc_namespace' => $ns,
157 # 'rc_title' => $title,
158 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
164 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
165 # array( 'rc_cur_id' => $curId ), $fname );
168 # Notify external application via UDP
169 if ( $wgRC2UDPAddress ) {
170 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
172 $line = $wgRC2UDPPrefix . $this->getIRCLine();
173 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
174 socket_close( $conn );
178 # E-mail notifications
181 # this would be better as an extension hook
182 include_once( "UserMailer.php" );
183 $enotif = new EmailNotification();
184 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
185 $enotif->notifyOnPageChange( $title,
186 $this->mAttribs
['rc_timestamp'],
187 $this->mAttribs
['rc_comment'],
188 $this->mAttribs
['rc_minor'],
189 $this->mAttribs
['rc_last_oldid'] );
193 wfRunHooks( 'RecentChange_save', array( &$this ) );
196 # Marks a certain row as patrolled
197 function markPatrolled( $rcid )
199 $fname = 'RecentChange::markPatrolled';
201 $dbw =& wfGetDB( DB_MASTER
);
203 $dbw->update( 'recentchanges',
206 ), array( /* WHERE */
212 # Makes an entry in the database corresponding to an edit
213 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
214 $oldId, $lastTimestamp, $bot = "default", $ip = '', $oldSize = 0, $newSize = 0,
218 if ( $bot === 'default' ) {
219 $bot = $user->isAllowed( 'bot' );
229 $rc = new RecentChange
;
230 $rc->mAttribs
= array(
231 'rc_timestamp' => $timestamp,
232 'rc_cur_time' => $timestamp,
233 'rc_namespace' => $title->getNamespace(),
234 'rc_title' => $title->getDBkey(),
235 'rc_type' => RC_EDIT
,
236 'rc_minor' => $minor ?
1 : 0,
237 'rc_cur_id' => $title->getArticleID(),
238 'rc_user' => $user->getID(),
239 'rc_user_text' => $user->getName(),
240 'rc_comment' => $comment,
241 'rc_this_oldid' => $newId,
242 'rc_last_oldid' => $oldId,
243 'rc_bot' => $bot ?
1 : 0,
244 'rc_moved_to_ns' => 0,
245 'rc_moved_to_title' => '',
248 'rc_new' => 0, # obsolete
249 'rc_old_len' => $oldSize,
250 'rc_new_len' => $newSize
254 'prefixedDBkey' => $title->getPrefixedDBkey(),
255 'lastTimestamp' => $lastTimestamp,
256 'oldSize' => $oldSize,
257 'newSize' => $newSize,
260 return( $rc->mAttribs
['rc_id'] );
264 * Makes an entry in the database corresponding to page creation
265 * Note: the title object must be loaded with the new id using resetArticleID()
266 * @todo Document parameters and return
270 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default",
271 $ip='', $size = 0, $newId = 0 )
279 if ( $bot == 'default' ) {
280 $bot = $user->isAllowed( 'bot' );
283 $rc = new RecentChange
;
284 $rc->mAttribs
= array(
285 'rc_timestamp' => $timestamp,
286 'rc_cur_time' => $timestamp,
287 'rc_namespace' => $title->getNamespace(),
288 'rc_title' => $title->getDBkey(),
290 'rc_minor' => $minor ?
1 : 0,
291 'rc_cur_id' => $title->getArticleID(),
292 'rc_user' => $user->getID(),
293 'rc_user_text' => $user->getName(),
294 'rc_comment' => $comment,
295 'rc_this_oldid' => $newId,
296 'rc_last_oldid' => 0,
297 'rc_bot' => $bot ?
1 : 0,
298 'rc_moved_to_ns' => 0,
299 'rc_moved_to_title' => '',
302 'rc_new' => 1, # obsolete
304 'rc_new_len' => $size
308 'prefixedDBkey' => $title->getPrefixedDBkey(),
309 'lastTimestamp' => 0,
314 return( $rc->mAttribs
['rc_id'] );
317 # Makes an entry in the database corresponding to a rename
318 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
327 $rc = new RecentChange
;
328 $rc->mAttribs
= array(
329 'rc_timestamp' => $timestamp,
330 'rc_cur_time' => $timestamp,
331 'rc_namespace' => $oldTitle->getNamespace(),
332 'rc_title' => $oldTitle->getDBkey(),
333 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
335 'rc_cur_id' => $oldTitle->getArticleID(),
336 'rc_user' => $user->getID(),
337 'rc_user_text' => $user->getName(),
338 'rc_comment' => $comment,
339 'rc_this_oldid' => 0,
340 'rc_last_oldid' => 0,
341 'rc_bot' => $user->isAllowed( 'bot' ) ?
1 : 0,
342 'rc_moved_to_ns' => $newTitle->getNamespace(),
343 'rc_moved_to_title' => $newTitle->getDBkey(),
345 'rc_new' => 0, # obsolete
347 'rc_old_len' => NULL,
348 'rc_new_len' => NULL,
352 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
353 'lastTimestamp' => 0,
354 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
359 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
360 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
363 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
364 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
367 # A log entry is different to an edit in that previous revisions are
369 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
370 $type, $action, $target, $logComment, $params )
379 $rc = new RecentChange
;
380 $rc->mAttribs
= array(
381 'rc_timestamp' => $timestamp,
382 'rc_cur_time' => $timestamp,
383 'rc_namespace' => $title->getNamespace(),
384 'rc_title' => $title->getDBkey(),
387 'rc_cur_id' => $title->getArticleID(),
388 'rc_user' => $user->getID(),
389 'rc_user_text' => $user->getName(),
390 'rc_comment' => $comment,
391 'rc_this_oldid' => 0,
392 'rc_last_oldid' => 0,
393 'rc_bot' => $user->isAllowed( 'bot' ) ?
1 : 0,
394 'rc_moved_to_ns' => 0,
395 'rc_moved_to_title' => '',
398 'rc_new' => 0, # obsolete
399 'rc_old_len' => NULL,
400 'rc_new_len' => NULL,
403 'prefixedDBkey' => $title->getPrefixedDBkey(),
404 'lastTimestamp' => 0,
406 'logAction' => $action,
407 'logComment' => $logComment,
408 'logTarget' => $target,
409 'logParams' => $params
414 # Initialises the members of this object from a mysql row object
415 function loadFromRow( $row )
417 $this->mAttribs
= get_object_vars( $row );
418 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
419 $this->mExtra
= array();
422 # Makes a pseudo-RC entry from a cur row, for watchlists and things
423 function loadFromCurRow( $row )
425 $this->mAttribs
= array(
426 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
427 'rc_cur_time' => $row->rev_timestamp
,
428 'rc_user' => $row->rev_user
,
429 'rc_user_text' => $row->rev_user_text
,
430 'rc_namespace' => $row->page_namespace
,
431 'rc_title' => $row->page_title
,
432 'rc_comment' => $row->rev_comment
,
433 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
434 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
435 'rc_cur_id' => $row->page_id
,
436 'rc_this_oldid' => $row->rev_id
,
437 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
439 'rc_moved_to_ns' => 0,
440 'rc_moved_to_title' => '',
442 'rc_id' => $row->rc_id
,
443 'rc_patrolled' => $row->rc_patrolled
,
444 'rc_new' => $row->page_is_new
, # obsolete
445 'rc_old_len' => NULL, # we can't get the text lengts from a cur row
446 'rc_new_len' => NULL,
449 $this->mExtra
= array();
454 * Gets the end part of the diff URL associated with this object
455 * Blank if no diff link should be displayed
457 function diffLinkTrail( $forceCur )
459 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
460 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
461 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
463 $trail .= '&diff=0' ;
465 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
473 function cleanupForIRC( $text ) {
474 return str_replace(array("\n", "\r"), array("", ""), $text);
477 function getIRCLine() {
478 global $wgUseRCPatrol;
480 extract($this->mAttribs
);
481 extract($this->mExtra
);
483 $titleObj =& $this->getTitle();
484 if ( $rc_type == RC_LOG
) {
485 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
487 $title = $titleObj->getPrefixedText();
489 $title = $this->cleanupForIRC( $title );
491 $bad = array("\n", "\r");
492 $empty = array("", "");
493 $title = $titleObj->getPrefixedText();
494 $title = str_replace($bad, $empty, $title);
496 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
497 if ( $rc_type == RC_LOG
) {
499 } elseif ( $rc_new && $wgUseRCPatrol ) {
500 $url = $titleObj->getInternalURL("rcid=$rc_id");
501 } else if ( $rc_new ) {
502 $url = $titleObj->getInternalURL();
503 } else if ( $wgUseRCPatrol ) {
504 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
506 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
509 if ( isset( $oldSize ) && isset( $newSize ) ) {
510 $szdiff = $newSize - $oldSize;
511 if ($szdiff < -500) {
512 $szdiff = "\002$szdiff\002";
513 } elseif ($szdiff >= 0) {
514 $szdiff = '+' . $szdiff ;
516 $szdiff = '(' . $szdiff . ')' ;
521 $user = $this->cleanupForIRC( $rc_user_text );
523 if ( $rc_type == RC_LOG
) {
524 $logTargetText = $logTarget->getPrefixedText();
525 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
528 $comment = $this->cleanupForIRC( $rc_comment );
529 $flag = ($rc_minor ?
"M" : "") . ($rc_new ?
"N" : "");
531 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
532 # no colour (\003) switches back to the term default
533 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
534 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
539 * Returns the change size (HTML).
540 * The lengths can be given optionally.
542 function getCharacterDifference( $old = 0, $new = 0 ) {
543 global $wgRCChangedSizeThreshold, $wgLang;
546 $old = $this->mAttribs
['rc_old_len'];
549 $new = $this->mAttribs
['rc_new_len'];
552 if( $old === NULL ||
$new === NULL ) {
556 $szdiff = $new - $old;
558 if( $szdiff < $wgRCChangedSizeThreshold ) {
559 return '<strong class=\'mw-plusminus-neg\'>(' . $wgLang->formatNum( $szdiff ) . ')</strong>';
560 } elseif( $szdiff === 0 ) {
561 return '<span class=\'mw-plusminus-null\'>(' . $wgLang->formatNum( $szdiff ) . ')</span>';
562 } elseif( $szdiff > 0 ) {
563 return '<span class=\'mw-plusminus-pos\'>(+' . $wgLang->formatNum( $szdiff ) . ')</span>';
565 return '<span class=\'mw-plusminus-neg\'>(' . $wgLang->formatNum( $szdiff ) . ')</span>';