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 );
91 function setAttribs( $attribs )
93 $this->mAttribs
= $attribs;
96 function setExtra( $extra )
98 $this->mExtra
= $extra;
103 if ( $this->mTitle
=== false ) {
104 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
106 return $this->mTitle
;
109 function getMovedToTitle()
111 if ( $this->mMovedToTitle
=== false ) {
112 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
113 $this->mAttribs
['rc_moved_to_title'] );
115 return $this->mMovedToTitle
;
118 # Writes the data in this object to the database
121 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix;
122 $fname = 'RecentChange::save';
124 $dbw = wfGetDB( DB_MASTER
);
125 if ( !is_array($this->mExtra
) ) {
126 $this->mExtra
= array();
128 $this->mExtra
['lang'] = $wgLocalInterwiki;
130 if ( !$wgPutIPinRC ) {
131 $this->mAttribs
['rc_ip'] = '';
134 ## If our database is strict about IP addresses, use NULL instead of an empty string
135 if ( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
136 unset( $this->mAttribs
['rc_ip'] );
139 # Fixup database timestamps
140 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
141 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
142 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
144 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
145 if ( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
146 unset ( $this->mAttribs
['rc_cur_id'] );
150 $dbw->insert( 'recentchanges', $this->mAttribs
, $fname );
153 $this->mAttribs
['rc_id'] = $dbw->insertId();
155 # Update old rows, if necessary
156 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
157 $lastTime = $this->mExtra
['lastTimestamp'];
158 #$now = $this->mAttribs['rc_timestamp'];
159 #$curId = $this->mAttribs['rc_cur_id'];
161 # Don't bother looking for entries that have probably
162 # been purged, it just locks up the indexes needlessly.
164 $age = time() - wfTimestamp( TS_UNIX
, $lastTime );
165 if( $age < $wgRCMaxAge ) {
166 # live hack, will commit once tested - kate
167 # Update rc_this_oldid for the entries which were current
169 #$oldid = $this->mAttribs['rc_last_oldid'];
170 #$ns = $this->mAttribs['rc_namespace'];
171 #$title = $this->mAttribs['rc_title'];
173 #$dbw->update( 'recentchanges',
175 # 'rc_this_oldid' => $oldid
176 # ), array( /* WHERE */
177 # 'rc_namespace' => $ns,
178 # 'rc_title' => $title,
179 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
185 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
186 # array( 'rc_cur_id' => $curId ), $fname );
189 # Notify external application via UDP
190 if ( $wgRC2UDPAddress ) {
191 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
193 $line = $wgRC2UDPPrefix . $this->getIRCLine();
194 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
195 socket_close( $conn );
199 # E-mail notifications
202 # this would be better as an extension hook
203 include_once( "UserMailer.php" );
204 $enotif = new EmailNotification();
205 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
206 $enotif->notifyOnPageChange( $title,
207 $this->mAttribs
['rc_timestamp'],
208 $this->mAttribs
['rc_comment'],
209 $this->mAttribs
['rc_minor'],
210 $this->mAttribs
['rc_last_oldid'] );
214 wfRunHooks( 'RecentChange_save', array( &$this ) );
217 # Marks a certain row as patrolled
218 function markPatrolled( $rcid )
220 $fname = 'RecentChange::markPatrolled';
222 $dbw = wfGetDB( DB_MASTER
);
224 $dbw->update( 'recentchanges',
227 ), array( /* WHERE */
233 # Makes an entry in the database corresponding to an edit
234 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
235 $oldId, $lastTimestamp, $bot="default", $ip='', $oldSize=0, $newSize=0, $newId=0)
238 if ( $bot === 'default' ) {
239 $bot = $user->isAllowed( 'bot' );
249 $rc = new RecentChange
;
250 $rc->mAttribs
= array(
251 'rc_timestamp' => $timestamp,
252 'rc_cur_time' => $timestamp,
253 'rc_namespace' => $title->getNamespace(),
254 'rc_title' => $title->getDBkey(),
255 'rc_type' => RC_EDIT
,
256 'rc_minor' => $minor ?
1 : 0,
257 'rc_cur_id' => $title->getArticleID(),
258 'rc_user' => $user->getID(),
259 'rc_user_text' => $user->getName(),
260 'rc_comment' => $comment,
261 'rc_this_oldid' => $newId,
262 'rc_last_oldid' => $oldId,
263 'rc_bot' => $bot ?
1 : 0,
264 'rc_moved_to_ns' => 0,
265 'rc_moved_to_title' => '',
268 'rc_new' => 0, # obsolete
269 'rc_old_len' => $oldSize,
270 'rc_new_len' => $newSize,
273 'rc_log_type' => null,
274 'rc_log_action' => '',
279 'prefixedDBkey' => $title->getPrefixedDBkey(),
280 'lastTimestamp' => $lastTimestamp,
281 'oldSize' => $oldSize,
282 'newSize' => $newSize,
285 return( $rc->mAttribs
['rc_id'] );
289 * Makes an entry in the database corresponding to page creation
290 * Note: the title object must be loaded with the new id using resetArticleID()
291 * @todo Document parameters and return
295 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default",
296 $ip='', $size=0, $newId=0 )
305 if ( $bot == 'default' ) {
306 $bot = $user->isAllowed( 'bot' );
309 $rc = new RecentChange
;
310 $rc->mAttribs
= array(
311 'rc_timestamp' => $timestamp,
312 'rc_cur_time' => $timestamp,
313 'rc_namespace' => $title->getNamespace(),
314 'rc_title' => $title->getDBkey(),
316 'rc_minor' => $minor ?
1 : 0,
317 'rc_cur_id' => $title->getArticleID(),
318 'rc_user' => $user->getID(),
319 'rc_user_text' => $user->getName(),
320 'rc_comment' => $comment,
321 'rc_this_oldid' => $newId,
322 'rc_last_oldid' => 0,
323 'rc_bot' => $bot ?
1 : 0,
324 'rc_moved_to_ns' => 0,
325 'rc_moved_to_title' => '',
328 'rc_new' => 1, # obsolete
330 'rc_new_len' => $size,
333 'rc_log_type' => null,
334 'rc_log_action' => '',
339 'prefixedDBkey' => $title->getPrefixedDBkey(),
340 'lastTimestamp' => 0,
345 return( $rc->mAttribs
['rc_id'] );
348 # Makes an entry in the database corresponding to a rename
349 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
358 $rc = new RecentChange
;
359 $rc->mAttribs
= array(
360 'rc_timestamp' => $timestamp,
361 'rc_cur_time' => $timestamp,
362 'rc_namespace' => $oldTitle->getNamespace(),
363 'rc_title' => $oldTitle->getDBkey(),
364 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
366 'rc_cur_id' => $oldTitle->getArticleID(),
367 'rc_user' => $user->getID(),
368 'rc_user_text' => $user->getName(),
369 'rc_comment' => $comment,
370 'rc_this_oldid' => 0,
371 'rc_last_oldid' => 0,
372 'rc_bot' => $user->isAllowed( 'bot' ) ?
1 : 0,
373 'rc_moved_to_ns' => $newTitle->getNamespace(),
374 'rc_moved_to_title' => $newTitle->getDBkey(),
376 'rc_new' => 0, # obsolete
378 'rc_old_len' => NULL,
379 'rc_new_len' => NULL,
381 'rc_logid' => 0, # notifyMove not used anymore
382 'rc_log_type' => null,
383 'rc_log_action' => '',
388 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
389 'lastTimestamp' => 0,
390 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
395 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
396 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
399 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
400 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
403 # A log entry is different to an edit in that previous revisions are not kept
404 /*static*/ function notifyLog( $timestamp, &$title, &$user, $actionText = null, $ip='',
405 $type, $action, $target, $logComment, $params, $newId=0 )
409 if ( !$ip ) $ip = '';
411 $rc = new RecentChange
;
412 $rc->mAttribs
= array(
413 'rc_timestamp' => $timestamp,
414 'rc_cur_time' => $timestamp,
415 'rc_namespace' => $title->getNamespace(),
416 'rc_title' => $title->getDBkey(),
419 'rc_cur_id' => $title->getArticleID(),
420 'rc_user' => $user->getID(),
421 'rc_user_text' => $user->getName(),
422 'rc_comment' => $logComment,
423 'rc_this_oldid' => 0,
424 'rc_last_oldid' => 0,
425 'rc_bot' => $user->isAllowed( 'bot' ) ?
1 : 0,
426 'rc_moved_to_ns' => 0,
427 'rc_moved_to_title' => '',
430 'rc_new' => 0, # obsolete
431 'rc_old_len' => NULL,
432 'rc_new_len' => NULL,
434 'rc_logid' => $newId,
435 'rc_log_type' => $type,
436 'rc_log_action' => $action,
437 'rc_params' => $params
440 'prefixedDBkey' => $title->getPrefixedDBkey(),
441 'lastTimestamp' => 0,
443 'logAction' => $action,
444 'logComment' => $logComment,
445 'logTarget' => $target,
446 'logParams' => $params
451 # Initialises the members of this object from a mysql row object
452 function loadFromRow( $row )
454 $this->mAttribs
= get_object_vars( $row );
455 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
456 $this->mExtra
= array();
459 # Makes a pseudo-RC entry from a cur row
460 function loadFromCurRow( $row )
462 $this->mAttribs
= array(
463 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
464 'rc_cur_time' => $row->rev_timestamp
,
465 'rc_user' => $row->rev_user
,
466 'rc_user_text' => $row->rev_user_text
,
467 'rc_namespace' => $row->page_namespace
,
468 'rc_title' => $row->page_title
,
469 'rc_comment' => $row->rev_comment
,
470 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
471 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
472 'rc_cur_id' => $row->page_id
,
473 'rc_this_oldid' => $row->rev_id
,
474 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
476 'rc_moved_to_ns' => 0,
477 'rc_moved_to_title' => '',
479 'rc_id' => $row->rc_id
,
480 'rc_patrolled' => $row->rc_patrolled
,
481 'rc_new' => $row->page_is_new
, # obsolete
482 'rc_old_len' => $row->rc_old_len
,
483 'rc_new_len' => $row->rc_new_len
,
484 'rc_deleted' => $row->rc_deleted
,
485 'rc_logid' => $row->rc_logid
,
486 'rc_log_type' => $row->rc_log_type
,
487 'rc_log_action' => $row->rc_log_action
,
488 'rc_params' => $row->rc_params
491 $this->mExtra
= array();
495 * Get an attribute value
497 * @param $name Attribute name
500 public function getAttribute( $name ) {
501 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
505 * Gets the end part of the diff URL associated with this object
506 * Blank if no diff link should be displayed
508 function diffLinkTrail( $forceCur )
510 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
511 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
512 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
514 $trail .= '&diff=0' ;
516 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
524 function cleanupForIRC( $text ) {
525 return str_replace(array("\n", "\r"), array("", ""), $text);
528 function getIRCLine() {
529 global $wgUseRCPatrol;
531 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
532 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
533 extract($this->mAttribs
);
534 extract($this->mExtra
);
536 $titleObj =& $this->getTitle();
537 if ( $rc_type == RC_LOG
) {
538 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
540 $title = $titleObj->getPrefixedText();
542 $title = $this->cleanupForIRC( $title );
544 $bad = array("\n", "\r");
545 $empty = array("", "");
546 $title = $titleObj->getPrefixedText();
547 $title = str_replace($bad, $empty, $title);
549 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
550 if ( $rc_type == RC_LOG
) {
552 } elseif ( $rc_new && $wgUseRCPatrol ) {
553 $url = $titleObj->getInternalURL("rcid=$rc_id");
554 } else if ( $rc_new ) {
555 $url = $titleObj->getInternalURL();
556 } else if ( $wgUseRCPatrol ) {
557 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
559 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
562 if ( isset( $oldSize ) && isset( $newSize ) ) {
563 $szdiff = $newSize - $oldSize;
564 if ($szdiff < -500) {
565 $szdiff = "\002$szdiff\002";
566 } elseif ($szdiff >= 0) {
567 $szdiff = '+' . $szdiff ;
569 $szdiff = '(' . $szdiff . ')' ;
574 $user = $this->cleanupForIRC( $rc_user_text );
576 if ( $rc_type == RC_LOG
) {
577 $logTargetText = $logTarget->getPrefixedText();
578 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
581 $comment = $this->cleanupForIRC( $rc_comment );
582 $flag = ($rc_minor ?
"M" : "") . ($rc_new ?
"N" : "");
584 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
585 # no colour (\003) switches back to the term default
586 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
587 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
592 * Returns the change size (HTML).
593 * The lengths can be given optionally.
595 function getCharacterDifference( $old = 0, $new = 0 ) {
596 global $wgRCChangedSizeThreshold, $wgLang;
599 $old = $this->mAttribs
['rc_old_len'];
602 $new = $this->mAttribs
['rc_new_len'];
605 if( $old === NULL ||
$new === NULL ) {
609 $szdiff = $new - $old;
610 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
611 $wgLang->formatNum($szdiff) );
613 if( $szdiff < $wgRCChangedSizeThreshold ) {
614 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
615 } elseif( $szdiff === 0 ) {
616 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
617 } elseif( $szdiff > 0 ) {
618 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
620 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';