4 * Utility class for creating new RC entries
6 * rc_id id of the row in the recentchanges table
7 * rc_timestamp time the entry was made
8 * rc_cur_time timestamp on the cur row
9 * rc_namespace namespace #
10 * rc_title non-prefixed db key
11 * rc_type is new entry, used to determine whether updating is necessary
13 * rc_cur_id page_id of associated page entry
14 * rc_user user id who made the entry
15 * rc_user_text user name who made the entry
16 * rc_comment edit summary
17 * rc_this_oldid rev_id associated with this entry (or zero)
18 * rc_last_oldid rev_id associated with the entry before this one (or zero)
19 * rc_bot is bot, hidden
20 * rc_ip IP address of the user in dotted quad notation
21 * rc_new obsolete, use rc_type==RC_NEW
22 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
23 * rc_old_len integer byte length of the text before the edit
24 * rc_new_len the same after the edit
25 * rc_deleted partial deletion
26 * rc_logid the log_id value for this log entry (or zero)
27 * rc_log_type the log type (or null)
28 * rc_log_action the log action (or null)
29 * rc_params log params
32 * prefixedDBkey prefixed db key, used by external app via msg queue
33 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
34 * lang the interwiki prefix, automatically set in save()
35 * oldSize text size before the change
36 * newSize text size after the change
38 * temporary: not stored in the database
39 * notificationtimestamp
40 * numberofWatchingusers
42 * @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 public static function newFromRow( $row )
54 $rc = new RecentChange
;
55 $rc->loadFromRow( $row );
59 public static function newFromCurRow( $row )
61 $rc = new RecentChange
;
62 $rc->loadFromCurRow( $row );
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 );
87 * Find the first recent change matching some specific conditions
89 * @param array $conds Array of conditions
90 * @param mixed $fname Override the method name in profiling/logs
91 * @return RecentChange
93 public static function newFromConds( $conds, $fname = false ) {
94 if( $fname === false )
96 $dbr = wfGetDB( DB_SLAVE
);
103 if( $res instanceof ResultWrapper
&& $res->numRows() > 0 ) {
104 $row = $res->fetchObject();
106 return self
::newFromRow( $row );
113 function setAttribs( $attribs )
115 $this->mAttribs
= $attribs;
118 function setExtra( $extra )
120 $this->mExtra
= $extra;
125 if ( $this->mTitle
=== false ) {
126 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
128 return $this->mTitle
;
131 function getMovedToTitle()
133 if ( $this->mMovedToTitle
=== false ) {
134 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
135 $this->mAttribs
['rc_moved_to_title'] );
137 return $this->mMovedToTitle
;
140 # Writes the data in this object to the database
143 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPOmitBots;
144 $fname = 'RecentChange::save';
146 $dbw = wfGetDB( DB_MASTER
);
147 if ( !is_array($this->mExtra
) ) {
148 $this->mExtra
= array();
150 $this->mExtra
['lang'] = $wgLocalInterwiki;
152 if ( !$wgPutIPinRC ) {
153 $this->mAttribs
['rc_ip'] = '';
156 # If our database is strict about IP addresses, use NULL instead of an empty string
157 if ( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
158 unset( $this->mAttribs
['rc_ip'] );
161 # Fixup database timestamps
162 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
163 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
164 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
166 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
167 if ( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
168 unset ( $this->mAttribs
['rc_cur_id'] );
172 $dbw->insert( 'recentchanges', $this->mAttribs
, $fname );
175 $this->mAttribs
['rc_id'] = $dbw->insertId();
177 # Update old rows, if necessary
178 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
179 $lastTime = $this->mExtra
['lastTimestamp'];
180 #$now = $this->mAttribs['rc_timestamp'];
181 #$curId = $this->mAttribs['rc_cur_id'];
183 # Don't bother looking for entries that have probably
184 # been purged, it just locks up the indexes needlessly.
186 $age = time() - wfTimestamp( TS_UNIX
, $lastTime );
187 if( $age < $wgRCMaxAge ) {
188 # live hack, will commit once tested - kate
189 # Update rc_this_oldid for the entries which were current
191 #$oldid = $this->mAttribs['rc_last_oldid'];
192 #$ns = $this->mAttribs['rc_namespace'];
193 #$title = $this->mAttribs['rc_title'];
195 #$dbw->update( 'recentchanges',
197 # 'rc_this_oldid' => $oldid
198 # ), array( /* WHERE */
199 # 'rc_namespace' => $ns,
200 # 'rc_title' => $title,
201 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
207 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
208 # array( 'rc_cur_id' => $curId ), $fname );
211 # Notify external application via UDP
212 if ( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
213 self
::sendToUDP( $this->getIRCLine() );
216 # E-mail notifications
217 global $wgUseEnotif, $wgShowUpdatedMarker, $wgUser;
218 if( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
220 if( $this->mAttribs
['rc_user'] ) {
221 $editor = ($wgUser->getId() == $this->mAttribs
['rc_user']) ?
222 $wgUser : User
::newFromID( $this->mAttribs
['rc_user'] );
225 $editor = ($wgUser->getName() == $this->mAttribs
['rc_user_text']) ?
226 $wgUser : User
::newFromName( $this->mAttribs
['rc_user_text'], false );
228 # FIXME: this would be better as an extension hook
229 $enotif = new EmailNotification();
230 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
231 $enotif->notifyOnPageChange( $editor, $title,
232 $this->mAttribs
['rc_timestamp'],
233 $this->mAttribs
['rc_comment'],
234 $this->mAttribs
['rc_minor'],
235 $this->mAttribs
['rc_last_oldid'] );
239 wfRunHooks( 'RecentChange_save', array( &$this ) );
243 * Send some text to UDP
244 * @param string $line
246 static function sendToUDP( $line ) {
247 global $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix;
248 # Notify external application via UDP
249 if( $wgRC2UDPAddress ) {
250 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
252 $line = $wgRC2UDPPrefix . $line;
253 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
254 socket_close( $conn );
260 * Mark a given change as patrolled
262 * @param mixed $change RecentChange or corresponding rc_id
263 * @param bool $auto for automatic patrol
264 * @return See doMarkPatrolled(), or null if $change is not an existing rc_id
266 public static function markPatrolled( $change, $auto = false ) {
267 $change = $change instanceof RecentChange
269 : RecentChange
::newFromId($change);
270 if( !$change instanceof RecentChange
) {
273 return $change->doMarkPatrolled( $auto );
277 * Mark this RecentChange as patrolled
279 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
280 * @param bool $auto for automatic patrol
281 * @return array of permissions errors, see Title::getUserPermissionsErrors()
283 public function doMarkPatrolled( $auto = false ) {
284 global $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
286 // If recentchanges patrol is disabled, only new pages
289 && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) )
291 $errors[] = array('rcpatroldisabled');
294 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
295 $right = $auto ?
'autopatrol' : 'patrol';
296 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $wgUser ) );
297 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$wgUser, false)) )
298 $errors[] = array('hookaborted');
300 // Users without the 'autopatrol' right can't patrol their
302 if( $wgUser->getName() == $this->getAttribute('rc_user_text') && !$wgUser->isAllowed('autopatrol') )
303 $errors[] = array('markedaspatrollederror-noautopatrol');
309 // If the change was patrolled already, do nothing
310 if( $this->getAttribute('rc_patrolled') )
313 // Actually set the 'patrolled' flag in RC
314 $this->reallyMarkPatrolled();
316 // Log this patrol event
317 PatrolLog
::record( $this, $auto );
318 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$wgUser, false) );
323 * Mark this RecentChange patrolled, without error checking
324 * @return int Number of affected rows
326 public function reallyMarkPatrolled() {
327 $dbw = wfGetDB( DB_MASTER
);
334 'rc_id' => $this->getAttribute('rc_id')
338 return $dbw->affectedRows();
341 # Makes an entry in the database corresponding to an edit
342 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
343 $oldId, $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0)
352 $rc = new RecentChange
;
353 $rc->mAttribs
= array(
354 'rc_timestamp' => $timestamp,
355 'rc_cur_time' => $timestamp,
356 'rc_namespace' => $title->getNamespace(),
357 'rc_title' => $title->getDBkey(),
358 'rc_type' => RC_EDIT
,
359 'rc_minor' => $minor ?
1 : 0,
360 'rc_cur_id' => $title->getArticleID(),
361 'rc_user' => $user->getId(),
362 'rc_user_text' => $user->getName(),
363 'rc_comment' => $comment,
364 'rc_this_oldid' => $newId,
365 'rc_last_oldid' => $oldId,
366 'rc_bot' => $bot ?
1 : 0,
367 'rc_moved_to_ns' => 0,
368 'rc_moved_to_title' => '',
371 'rc_new' => 0, # obsolete
372 'rc_old_len' => $oldSize,
373 'rc_new_len' => $newSize,
376 'rc_log_type' => null,
377 'rc_log_action' => '',
382 'prefixedDBkey' => $title->getPrefixedDBkey(),
383 'lastTimestamp' => $lastTimestamp,
384 'oldSize' => $oldSize,
385 'newSize' => $newSize,
392 * Makes an entry in the database corresponding to page creation
393 * Note: the title object must be loaded with the new id using resetArticleID()
394 * @todo Document parameters and return
396 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
397 $ip='', $size = 0, $newId = 0 )
406 $rc = new RecentChange
;
407 $rc->mAttribs
= array(
408 'rc_timestamp' => $timestamp,
409 'rc_cur_time' => $timestamp,
410 'rc_namespace' => $title->getNamespace(),
411 'rc_title' => $title->getDBkey(),
413 'rc_minor' => $minor ?
1 : 0,
414 'rc_cur_id' => $title->getArticleID(),
415 'rc_user' => $user->getId(),
416 'rc_user_text' => $user->getName(),
417 'rc_comment' => $comment,
418 'rc_this_oldid' => $newId,
419 'rc_last_oldid' => 0,
420 'rc_bot' => $bot ?
1 : 0,
421 'rc_moved_to_ns' => 0,
422 'rc_moved_to_title' => '',
425 'rc_new' => 1, # obsolete
427 'rc_new_len' => $size,
430 'rc_log_type' => null,
431 'rc_log_action' => '',
436 'prefixedDBkey' => $title->getPrefixedDBkey(),
437 'lastTimestamp' => 0,
445 # Makes an entry in the database corresponding to a rename
446 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
457 $rc = new RecentChange
;
458 $rc->mAttribs
= array(
459 'rc_timestamp' => $timestamp,
460 'rc_cur_time' => $timestamp,
461 'rc_namespace' => $oldTitle->getNamespace(),
462 'rc_title' => $oldTitle->getDBkey(),
463 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
465 'rc_cur_id' => $oldTitle->getArticleID(),
466 'rc_user' => $user->getId(),
467 'rc_user_text' => $user->getName(),
468 'rc_comment' => $comment,
469 'rc_this_oldid' => 0,
470 'rc_last_oldid' => 0,
471 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
472 'rc_moved_to_ns' => $newTitle->getNamespace(),
473 'rc_moved_to_title' => $newTitle->getDBkey(),
475 'rc_new' => 0, # obsolete
477 'rc_old_len' => NULL,
478 'rc_new_len' => NULL,
480 'rc_logid' => 0, # notifyMove not used anymore
481 'rc_log_type' => null,
482 'rc_log_action' => '',
487 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
488 'lastTimestamp' => 0,
489 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
494 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
495 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
498 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
499 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
502 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='',
503 $type, $action, $target, $logComment, $params, $newId=0 )
514 $rc = new RecentChange
;
515 $rc->mAttribs
= array(
516 'rc_timestamp' => $timestamp,
517 'rc_cur_time' => $timestamp,
518 'rc_namespace' => $target->getNamespace(),
519 'rc_title' => $target->getDBkey(),
522 'rc_cur_id' => $target->getArticleID(),
523 'rc_user' => $user->getId(),
524 'rc_user_text' => $user->getName(),
525 'rc_comment' => $logComment,
526 'rc_this_oldid' => 0,
527 'rc_last_oldid' => 0,
528 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
529 'rc_moved_to_ns' => 0,
530 'rc_moved_to_title' => '',
533 'rc_new' => 0, # obsolete
534 'rc_old_len' => NULL,
535 'rc_new_len' => NULL,
537 'rc_logid' => $newId,
538 'rc_log_type' => $type,
539 'rc_log_action' => $action,
540 'rc_params' => $params
543 'prefixedDBkey' => $title->getPrefixedDBkey(),
544 'lastTimestamp' => 0,
545 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
550 # Initialises the members of this object from a mysql row object
551 function loadFromRow( $row )
553 $this->mAttribs
= get_object_vars( $row );
554 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
555 $this->mExtra
= array();
558 # Makes a pseudo-RC entry from a cur row
559 function loadFromCurRow( $row )
561 $this->mAttribs
= array(
562 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
563 'rc_cur_time' => $row->rev_timestamp
,
564 'rc_user' => $row->rev_user
,
565 'rc_user_text' => $row->rev_user_text
,
566 'rc_namespace' => $row->page_namespace
,
567 'rc_title' => $row->page_title
,
568 'rc_comment' => $row->rev_comment
,
569 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
570 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
571 'rc_cur_id' => $row->page_id
,
572 'rc_this_oldid' => $row->rev_id
,
573 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
575 'rc_moved_to_ns' => 0,
576 'rc_moved_to_title' => '',
578 'rc_id' => $row->rc_id
,
579 'rc_patrolled' => $row->rc_patrolled
,
580 'rc_new' => $row->page_is_new
, # obsolete
581 'rc_old_len' => $row->rc_old_len
,
582 'rc_new_len' => $row->rc_new_len
,
583 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
584 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
585 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
586 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
587 // this one REALLY should be set...
588 'rc_deleted' => isset($row->rc_deleted
) ?
$row->rc_deleted
: 0,
591 $this->mExtra
= array();
595 * Get an attribute value
597 * @param $name Attribute name
600 public function getAttribute( $name ) {
601 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
605 * Gets the end part of the diff URL associated with this object
606 * Blank if no diff link should be displayed
608 function diffLinkTrail( $forceCur )
610 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
611 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
612 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
614 $trail .= '&diff=0' ;
616 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
624 function cleanupForIRC( $text ) {
625 return str_replace(array("\n", "\r"), array("", ""), $text);
628 function getIRCLine() {
629 global $wgUseRCPatrol;
631 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
632 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
633 extract($this->mAttribs
);
634 extract($this->mExtra
);
636 if ( $rc_type == RC_LOG
) {
637 $titleObj = Title
::newFromText( "Log/$rc_log_type", NS_SPECIAL
);
639 $titleObj =& $this->getTitle();
641 $title = $titleObj->getPrefixedText();
642 $title = $this->cleanupForIRC( $title );
644 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
645 if ( $rc_type == RC_LOG
) {
647 } elseif ( $rc_new && $wgUseRCPatrol ) {
648 $url = $titleObj->getInternalURL("rcid=$rc_id");
649 } else if ( $rc_new ) {
650 $url = $titleObj->getInternalURL();
651 } else if ( $wgUseRCPatrol ) {
652 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
654 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
657 if ( isset( $oldSize ) && isset( $newSize ) ) {
658 $szdiff = $newSize - $oldSize;
659 if ($szdiff < -500) {
660 $szdiff = "\002$szdiff\002";
661 } elseif ($szdiff >= 0) {
662 $szdiff = '+' . $szdiff ;
664 $szdiff = '(' . $szdiff . ')' ;
669 $user = $this->cleanupForIRC( $rc_user_text );
671 if ( $rc_type == RC_LOG
) {
672 $logTargetText = $this->getTitle()->getPrefixedText();
673 $comment = $this->cleanupForIRC( str_replace($logTargetText,"\00302$logTargetText\00310",$actionComment) );
674 $flag = $rc_log_action;
676 $comment = $this->cleanupForIRC( $rc_comment );
677 $flag = ($rc_new ?
"N" : "") . ($rc_minor ?
"M" : "") . ($rc_bot ?
"B" : "");
679 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
680 # no colour (\003) switches back to the term default
681 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
682 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
687 * Returns the change size (HTML).
688 * The lengths can be given optionally.
690 function getCharacterDifference( $old = 0, $new = 0 ) {
691 global $wgRCChangedSizeThreshold, $wgLang;
694 $old = $this->mAttribs
['rc_old_len'];
697 $new = $this->mAttribs
['rc_new_len'];
700 if( $old === NULL ||
$new === NULL ) {
704 $szdiff = $new - $old;
705 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
706 $wgLang->formatNum($szdiff) );
708 if( $szdiff < $wgRCChangedSizeThreshold ) {
709 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
710 } elseif( $szdiff === 0 ) {
711 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
712 } elseif( $szdiff > 0 ) {
713 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
715 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';