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 );
85 * Find the first recent change matching some specific conditions
87 * @param array $conds Array of conditions
88 * @param mixed $fname Override the method name in profiling/logs
89 * @return RecentChange
91 public static function newFromConds( $conds, $fname = false ) {
92 if( $fname === false )
94 $dbr = wfGetDB( DB_SLAVE
);
101 if( $res instanceof ResultWrapper
&& $res->numRows() > 0 ) {
102 $row = $res->fetchObject();
104 return self
::newFromRow( $row );
111 function setAttribs( $attribs )
113 $this->mAttribs
= $attribs;
116 function setExtra( $extra )
118 $this->mExtra
= $extra;
123 if ( $this->mTitle
=== false ) {
124 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
126 return $this->mTitle
;
129 function getMovedToTitle()
131 if ( $this->mMovedToTitle
=== false ) {
132 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
133 $this->mAttribs
['rc_moved_to_title'] );
135 return $this->mMovedToTitle
;
138 # Writes the data in this object to the database
141 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix;
142 $fname = 'RecentChange::save';
144 $dbw = wfGetDB( DB_MASTER
);
145 if ( !is_array($this->mExtra
) ) {
146 $this->mExtra
= array();
148 $this->mExtra
['lang'] = $wgLocalInterwiki;
150 if ( !$wgPutIPinRC ) {
151 $this->mAttribs
['rc_ip'] = '';
154 ## If our database is strict about IP addresses, use NULL instead of an empty string
155 if ( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
156 unset( $this->mAttribs
['rc_ip'] );
159 # Fixup database timestamps
160 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
161 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
162 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
164 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
165 if ( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
166 unset ( $this->mAttribs
['rc_cur_id'] );
170 $dbw->insert( 'recentchanges', $this->mAttribs
, $fname );
173 $this->mAttribs
['rc_id'] = $dbw->insertId();
175 # Update old rows, if necessary
176 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
177 $lastTime = $this->mExtra
['lastTimestamp'];
178 #$now = $this->mAttribs['rc_timestamp'];
179 #$curId = $this->mAttribs['rc_cur_id'];
181 # Don't bother looking for entries that have probably
182 # been purged, it just locks up the indexes needlessly.
184 $age = time() - wfTimestamp( TS_UNIX
, $lastTime );
185 if( $age < $wgRCMaxAge ) {
186 # live hack, will commit once tested - kate
187 # Update rc_this_oldid for the entries which were current
189 #$oldid = $this->mAttribs['rc_last_oldid'];
190 #$ns = $this->mAttribs['rc_namespace'];
191 #$title = $this->mAttribs['rc_title'];
193 #$dbw->update( 'recentchanges',
195 # 'rc_this_oldid' => $oldid
196 # ), array( /* WHERE */
197 # 'rc_namespace' => $ns,
198 # 'rc_title' => $title,
199 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
205 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
206 # array( 'rc_cur_id' => $curId ), $fname );
209 # Notify external application via UDP
210 if ( $wgRC2UDPAddress ) {
211 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
213 $line = $wgRC2UDPPrefix . $this->getIRCLine();
214 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
215 socket_close( $conn );
219 # E-mail notifications
222 # this would be better as an extension hook
224 $enotif = new EmailNotification
;
225 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
226 $enotif->notifyOnPageChange( $wgUser, $title,
227 $this->mAttribs
['rc_timestamp'],
228 $this->mAttribs
['rc_comment'],
229 $this->mAttribs
['rc_minor'],
230 $this->mAttribs
['rc_last_oldid'] );
234 wfRunHooks( 'RecentChange_save', array( &$this ) );
238 * Mark a given change as patrolled
240 * @param mixed $change RecentChange or corresponding rc_id
242 public static function markPatrolled( $change ) {
243 $rcid = $change instanceof RecentChange
244 ?
$change->mAttribs
['rc_id']
246 $dbw = wfGetDB( DB_MASTER
);
259 # Makes an entry in the database corresponding to an edit
260 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
261 $oldId, $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0,
271 $rc = new RecentChange
;
272 $rc->mAttribs
= array(
273 'rc_timestamp' => $timestamp,
274 'rc_cur_time' => $timestamp,
275 'rc_namespace' => $title->getNamespace(),
276 'rc_title' => $title->getDBkey(),
277 'rc_type' => RC_EDIT
,
278 'rc_minor' => $minor ?
1 : 0,
279 'rc_cur_id' => $title->getArticleID(),
280 'rc_user' => $user->getID(),
281 'rc_user_text' => $user->getName(),
282 'rc_comment' => $comment,
283 'rc_this_oldid' => $newId,
284 'rc_last_oldid' => $oldId,
285 'rc_bot' => $bot ?
1 : 0,
286 'rc_moved_to_ns' => 0,
287 'rc_moved_to_title' => '',
290 'rc_new' => 0, # obsolete
291 'rc_old_len' => $oldSize,
292 'rc_new_len' => $newSize
296 'prefixedDBkey' => $title->getPrefixedDBkey(),
297 'lastTimestamp' => $lastTimestamp,
298 'oldSize' => $oldSize,
299 'newSize' => $newSize,
302 return( $rc->mAttribs
['rc_id'] );
306 * Makes an entry in the database corresponding to page creation
307 * Note: the title object must be loaded with the new id using resetArticleID()
308 * @todo Document parameters and return
310 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
311 $ip='', $size = 0, $newId = 0 )
320 $rc = new RecentChange
;
321 $rc->mAttribs
= array(
322 'rc_timestamp' => $timestamp,
323 'rc_cur_time' => $timestamp,
324 'rc_namespace' => $title->getNamespace(),
325 'rc_title' => $title->getDBkey(),
327 'rc_minor' => $minor ?
1 : 0,
328 'rc_cur_id' => $title->getArticleID(),
329 'rc_user' => $user->getID(),
330 'rc_user_text' => $user->getName(),
331 'rc_comment' => $comment,
332 'rc_this_oldid' => $newId,
333 'rc_last_oldid' => 0,
334 'rc_bot' => $bot ?
1 : 0,
335 'rc_moved_to_ns' => 0,
336 'rc_moved_to_title' => '',
339 'rc_new' => 1, # obsolete
341 'rc_new_len' => $size
345 'prefixedDBkey' => $title->getPrefixedDBkey(),
346 'lastTimestamp' => 0,
351 return( $rc->mAttribs
['rc_id'] );
354 # Makes an entry in the database corresponding to a rename
355 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
366 $rc = new RecentChange
;
367 $rc->mAttribs
= array(
368 'rc_timestamp' => $timestamp,
369 'rc_cur_time' => $timestamp,
370 'rc_namespace' => $oldTitle->getNamespace(),
371 'rc_title' => $oldTitle->getDBkey(),
372 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
374 'rc_cur_id' => $oldTitle->getArticleID(),
375 'rc_user' => $user->getID(),
376 'rc_user_text' => $user->getName(),
377 'rc_comment' => $comment,
378 'rc_this_oldid' => 0,
379 'rc_last_oldid' => 0,
380 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
381 'rc_moved_to_ns' => $newTitle->getNamespace(),
382 'rc_moved_to_title' => $newTitle->getDBkey(),
384 'rc_new' => 0, # obsolete
386 'rc_old_len' => NULL,
387 'rc_new_len' => NULL,
391 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
392 'lastTimestamp' => 0,
393 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
398 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
399 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
402 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
403 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
406 # A log entry is different to an edit in that previous revisions are
408 public static function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
409 $type, $action, $target, $logComment, $params )
420 $rc = new RecentChange
;
421 $rc->mAttribs
= array(
422 'rc_timestamp' => $timestamp,
423 'rc_cur_time' => $timestamp,
424 'rc_namespace' => $title->getNamespace(),
425 'rc_title' => $title->getDBkey(),
428 'rc_cur_id' => $title->getArticleID(),
429 'rc_user' => $user->getID(),
430 'rc_user_text' => $user->getName(),
431 'rc_comment' => $comment,
432 'rc_this_oldid' => 0,
433 'rc_last_oldid' => 0,
434 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
435 'rc_moved_to_ns' => 0,
436 'rc_moved_to_title' => '',
439 'rc_new' => 0, # obsolete
440 'rc_old_len' => NULL,
441 'rc_new_len' => NULL,
444 'prefixedDBkey' => $title->getPrefixedDBkey(),
445 'lastTimestamp' => 0,
447 'logAction' => $action,
448 'logComment' => $logComment,
449 'logTarget' => $target,
450 'logParams' => $params
455 # Initialises the members of this object from a mysql row object
456 function loadFromRow( $row )
458 $this->mAttribs
= get_object_vars( $row );
459 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
460 $this->mExtra
= array();
463 # Makes a pseudo-RC entry from a cur row
464 function loadFromCurRow( $row )
466 $this->mAttribs
= array(
467 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
468 'rc_cur_time' => $row->rev_timestamp
,
469 'rc_user' => $row->rev_user
,
470 'rc_user_text' => $row->rev_user_text
,
471 'rc_namespace' => $row->page_namespace
,
472 'rc_title' => $row->page_title
,
473 'rc_comment' => $row->rev_comment
,
474 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
475 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
476 'rc_cur_id' => $row->page_id
,
477 'rc_this_oldid' => $row->rev_id
,
478 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
480 'rc_moved_to_ns' => 0,
481 'rc_moved_to_title' => '',
483 'rc_id' => $row->rc_id
,
484 'rc_patrolled' => $row->rc_patrolled
,
485 'rc_new' => $row->page_is_new
, # obsolete
486 'rc_old_len' => $row->rc_old_len
,
487 'rc_new_len' => $row->rc_new_len
,
490 $this->mExtra
= array();
494 * Get an attribute value
496 * @param $name Attribute name
499 public function getAttribute( $name ) {
500 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
504 * Gets the end part of the diff URL associated with this object
505 * Blank if no diff link should be displayed
507 function diffLinkTrail( $forceCur )
509 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
510 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
511 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
513 $trail .= '&diff=0' ;
515 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
523 function cleanupForIRC( $text ) {
524 return str_replace(array("\n", "\r"), array("", ""), $text);
527 function getIRCLine() {
528 global $wgUseRCPatrol;
530 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
531 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
532 extract($this->mAttribs
);
533 extract($this->mExtra
);
535 $titleObj =& $this->getTitle();
536 if ( $rc_type == RC_LOG
) {
537 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
539 $title = $titleObj->getPrefixedText();
541 $title = $this->cleanupForIRC( $title );
543 $bad = array("\n", "\r");
544 $empty = array("", "");
545 $title = $titleObj->getPrefixedText();
546 $title = str_replace($bad, $empty, $title);
548 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
549 if ( $rc_type == RC_LOG
) {
551 } elseif ( $rc_new && $wgUseRCPatrol ) {
552 $url = $titleObj->getInternalURL("rcid=$rc_id");
553 } else if ( $rc_new ) {
554 $url = $titleObj->getInternalURL();
555 } else if ( $wgUseRCPatrol ) {
556 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
558 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
561 if ( isset( $oldSize ) && isset( $newSize ) ) {
562 $szdiff = $newSize - $oldSize;
563 if ($szdiff < -500) {
564 $szdiff = "\002$szdiff\002";
565 } elseif ($szdiff >= 0) {
566 $szdiff = '+' . $szdiff ;
568 $szdiff = '(' . $szdiff . ')' ;
573 $user = $this->cleanupForIRC( $rc_user_text );
575 if ( $rc_type == RC_LOG
) {
576 $logTargetText = $logTarget->getPrefixedText();
577 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
580 $comment = $this->cleanupForIRC( $rc_comment );
581 $flag = ($rc_minor ?
"M" : "") . ($rc_new ?
"N" : "");
583 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
584 # no colour (\003) switches back to the term default
585 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
586 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
591 * Returns the change size (HTML).
592 * The lengths can be given optionally.
594 function getCharacterDifference( $old = 0, $new = 0 ) {
595 global $wgRCChangedSizeThreshold, $wgLang;
598 $old = $this->mAttribs
['rc_old_len'];
601 $new = $this->mAttribs
['rc_new_len'];
604 if( $old === NULL ||
$new === NULL ) {
608 $szdiff = $new - $old;
609 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
610 $wgLang->formatNum($szdiff) );
612 if( $szdiff < $wgRCChangedSizeThreshold ) {
613 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
614 } elseif( $szdiff === 0 ) {
615 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
616 } elseif( $szdiff > 0 ) {
617 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
619 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';