4 * Utility class for creating new RC entries
7 * rc_id id of the row in the recentchanges table
8 * rc_timestamp time the entry was made
9 * rc_cur_time timestamp on the cur row
10 * rc_namespace namespace #
11 * rc_title non-prefixed db key
12 * rc_type is new entry, used to determine whether updating is necessary
14 * rc_cur_id page_id of associated page entry
15 * rc_user user id who made the entry
16 * rc_user_text user name who made the entry
17 * rc_comment edit summary
18 * rc_this_oldid rev_id associated with this entry (or zero)
19 * rc_last_oldid rev_id associated with the entry before this one (or zero)
20 * rc_bot is bot, hidden
21 * rc_ip IP address of the user in dotted quad notation
22 * rc_new obsolete, use rc_type==RC_NEW
23 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
24 * rc_old_len integer byte length of the text before the edit
25 * rc_new_len the same after the edit
26 * rc_deleted partial deletion
27 * rc_logid the log_id value for this log entry (or zero)
28 * rc_log_type the log type (or null)
29 * rc_log_action the log action (or null)
30 * rc_params log params
33 * prefixedDBkey prefixed db key, used by external app via msg queue
34 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
35 * lang the interwiki prefix, automatically set in save()
36 * oldSize text size before the change
37 * newSize text size after the change
39 * temporary: not stored in the database
40 * notificationtimestamp
41 * numberofWatchingusers
43 * @todo document functions and variables
46 var $mAttribs = array(), $mExtra = array();
56 var $mMovedToTitle = false;
57 var $numberofWatchingusers = 0 ; # Dummy to prevent error message in SpecialRecentchangeslinked
58 var $notificationtimestamp;
64 * @return RecentChange
66 public static function newFromRow( $row ) {
67 $rc = new RecentChange
;
68 $rc->loadFromRow( $row );
74 * @return RecentChange
76 public static function newFromCurRow( $row ) {
77 $rc = new RecentChange
;
78 $rc->loadFromCurRow( $row );
79 $rc->notificationtimestamp
= false;
80 $rc->numberofWatchingusers
= false;
85 * Obtain the recent change with a given rc_id value
87 * @param $rcid Int rc_id value to retrieve
88 * @return RecentChange
90 public static function newFromId( $rcid ) {
91 $dbr = wfGetDB( DB_SLAVE
);
92 $res = $dbr->select( 'recentchanges', '*', array( 'rc_id' => $rcid ), __METHOD__
);
93 if( $res && $dbr->numRows( $res ) > 0 ) {
94 $row = $dbr->fetchObject( $res );
95 return self
::newFromRow( $row );
102 * Find the first recent change matching some specific conditions
104 * @param $conds Array of conditions
105 * @param $fname Mixed: override the method name in profiling/logs
106 * @return RecentChange
108 public static function newFromConds( $conds, $fname = __METHOD__
) {
109 $dbr = wfGetDB( DB_SLAVE
);
116 if( $res instanceof ResultWrapper
&& $res->numRows() > 0 ) {
117 $row = $res->fetchObject();
119 return self
::newFromRow( $row );
127 * @param $attribs array
129 public function setAttribs( $attribs ) {
130 $this->mAttribs
= $attribs;
134 * @param $extra array
136 public function setExtra( $extra ) {
137 $this->mExtra
= $extra;
144 public function &getTitle() {
145 if( $this->mTitle
=== false ) {
146 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
147 # Make sure the correct page ID is process cached
148 $this->mTitle
->resetArticleID( $this->mAttribs
['rc_cur_id'] );
150 return $this->mTitle
;
154 * @return bool|\Title
156 public function getMovedToTitle() {
157 if( $this->mMovedToTitle
=== false ) {
158 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
159 $this->mAttribs
['rc_moved_to_title'] );
161 return $this->mMovedToTitle
;
165 * Writes the data in this object to the database
168 public function save( $noudp = false ) {
169 global $wgLocalInterwiki, $wgPutIPinRC;
171 $dbw = wfGetDB( DB_MASTER
);
172 if( !is_array($this->mExtra
) ) {
173 $this->mExtra
= array();
175 $this->mExtra
['lang'] = $wgLocalInterwiki;
177 if( !$wgPutIPinRC ) {
178 $this->mAttribs
['rc_ip'] = '';
181 # If our database is strict about IP addresses, use NULL instead of an empty string
182 if( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
183 unset( $this->mAttribs
['rc_ip'] );
186 # Fixup database timestamps
187 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
188 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
189 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
191 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
192 if( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
193 unset( $this->mAttribs
['rc_cur_id'] );
197 $dbw->insert( 'recentchanges', $this->mAttribs
, __METHOD__
);
200 $this->mAttribs
['rc_id'] = $dbw->insertId();
203 wfRunHooks( 'RecentChange_save', array( &$this ) );
205 # Notify external application via UDP
207 $this->notifyRC2UDP();
210 # E-mail notifications
211 global $wgUseEnotif, $wgShowUpdatedMarker, $wgUser;
212 if( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
214 if( $this->mAttribs
['rc_user'] ) {
215 $editor = ($wgUser->getId() == $this->mAttribs
['rc_user']) ?
216 $wgUser : User
::newFromID( $this->mAttribs
['rc_user'] );
219 $editor = ($wgUser->getName() == $this->mAttribs
['rc_user_text']) ?
220 $wgUser : User
::newFromName( $this->mAttribs
['rc_user_text'], false );
222 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
224 # @todo FIXME: This would be better as an extension hook
225 $enotif = new EmailNotification();
226 $status = $enotif->notifyOnPageChange( $editor, $title,
227 $this->mAttribs
['rc_timestamp'],
228 $this->mAttribs
['rc_comment'],
229 $this->mAttribs
['rc_minor'],
230 $this->mAttribs
['rc_last_oldid'] );
234 public function notifyRC2UDP() {
235 global $wgRC2UDPAddress, $wgRC2UDPOmitBots;
236 # Notify external application via UDP
237 if( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
238 self
::sendToUDP( $this->getIRCLine() );
243 * Send some text to UDP.
244 * @see RecentChange::cleanupForIRC
245 * @param $line String: text to send
246 * @param $address String: defaults to $wgRC2UDPAddress.
247 * @param $prefix String: defaults to $wgRC2UDPPrefix.
248 * @param $port Int: defaults to $wgRC2UDPPort. (Since 1.17)
249 * @return Boolean: success
251 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) {
252 global $wgRC2UDPAddress, $wgRC2UDPPrefix, $wgRC2UDPPort;
253 # Assume default for standard RC case
254 $address = $address ?
$address : $wgRC2UDPAddress;
255 $prefix = $prefix ?
$prefix : $wgRC2UDPPrefix;
256 $port = $port ?
$port : $wgRC2UDPPort;
257 # Notify external application via UDP
259 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
261 $line = $prefix . $line;
262 wfDebug( __METHOD__
. ": sending UDP line: $line\n" );
263 socket_sendto( $conn, $line, strlen($line), 0, $address, $port );
264 socket_close( $conn );
267 wfDebug( __METHOD__
. ": failed to create UDP socket\n" );
274 * Remove newlines, carriage returns and decode html entites
275 * @param $text String
278 public static function cleanupForIRC( $text ) {
279 return Sanitizer
::decodeCharReferences( str_replace( array( "\n", "\r" ), array( "", "" ), $text ) );
283 * Mark a given change as patrolled
285 * @param $change Mixed: RecentChange or corresponding rc_id
286 * @param $auto Boolean: for automatic patrol
287 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
289 public static function markPatrolled( $change, $auto = false ) {
292 $change = $change instanceof RecentChange
294 : RecentChange
::newFromId($change);
296 if( !$change instanceof RecentChange
) {
299 return $change->doMarkPatrolled( $wgUser, $auto );
303 * Mark this RecentChange as patrolled
305 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
306 * @param $user User object doing the action
307 * @param $auto Boolean: for automatic patrol
308 * @return array of permissions errors, see Title::getUserPermissionsErrors()
310 public function doMarkPatrolled( User
$user, $auto = false ) {
311 global $wgUseRCPatrol, $wgUseNPPatrol;
313 // If recentchanges patrol is disabled, only new pages
315 if( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute('rc_type') != RC_NEW
) ) {
316 $errors[] = array('rcpatroldisabled');
318 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
319 $right = $auto ?
'autopatrol' : 'patrol';
320 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
321 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$user, false)) ) {
322 $errors[] = array('hookaborted');
324 // Users without the 'autopatrol' right can't patrol their
326 if( $user->getName() == $this->getAttribute('rc_user_text') && !$user->isAllowed('autopatrol') ) {
327 $errors[] = array('markedaspatrollederror-noautopatrol');
332 // If the change was patrolled already, do nothing
333 if( $this->getAttribute('rc_patrolled') ) {
336 // Actually set the 'patrolled' flag in RC
337 $this->reallyMarkPatrolled();
338 // Log this patrol event
339 PatrolLog
::record( $this, $auto, $user );
340 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$user, false) );
345 * Mark this RecentChange patrolled, without error checking
346 * @return Integer: number of affected rows
348 public function reallyMarkPatrolled() {
349 $dbw = wfGetDB( DB_MASTER
);
356 'rc_id' => $this->getAttribute('rc_id')
360 return $dbw->affectedRows();
364 * Makes an entry in the database corresponding to an edit
367 * @param $title Title
372 * @param $lastTimestamp
375 * @param $oldSize int
376 * @param $newSize int
379 * @return RecentChange
381 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
382 $lastTimestamp, $bot, $ip='', $oldSize=0, $newSize=0, $newId=0, $patrol=0 ) {
385 $ip = $wgRequest->getIP();
389 $rc = new RecentChange
;
390 $rc->mAttribs
= array(
391 'rc_timestamp' => $timestamp,
392 'rc_cur_time' => $timestamp,
393 'rc_namespace' => $title->getNamespace(),
394 'rc_title' => $title->getDBkey(),
395 'rc_type' => RC_EDIT
,
396 'rc_minor' => $minor ?
1 : 0,
397 'rc_cur_id' => $title->getArticleID(),
398 'rc_user' => $user->getId(),
399 'rc_user_text' => $user->getName(),
400 'rc_comment' => $comment,
401 'rc_this_oldid' => $newId,
402 'rc_last_oldid' => $oldId,
403 'rc_bot' => $bot ?
1 : 0,
404 'rc_moved_to_ns' => 0,
405 'rc_moved_to_title' => '',
407 'rc_patrolled' => intval($patrol),
408 'rc_new' => 0, # obsolete
409 'rc_old_len' => $oldSize,
410 'rc_new_len' => $newSize,
413 'rc_log_type' => null,
414 'rc_log_action' => '',
419 'prefixedDBkey' => $title->getPrefixedDBkey(),
420 'lastTimestamp' => $lastTimestamp,
421 'oldSize' => $oldSize,
422 'newSize' => $newSize,
429 * Makes an entry in the database corresponding to page creation
430 * Note: the title object must be loaded with the new id using resetArticleID()
431 * @todo Document parameters and return
434 * @param $title Title
443 * @return RecentChange
445 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
446 $ip='', $size=0, $newId=0, $patrol=0 ) {
449 $ip = $wgRequest->getIP();
455 $rc = new RecentChange
;
456 $rc->mAttribs
= array(
457 'rc_timestamp' => $timestamp,
458 'rc_cur_time' => $timestamp,
459 'rc_namespace' => $title->getNamespace(),
460 'rc_title' => $title->getDBkey(),
462 'rc_minor' => $minor ?
1 : 0,
463 'rc_cur_id' => $title->getArticleID(),
464 'rc_user' => $user->getId(),
465 'rc_user_text' => $user->getName(),
466 'rc_comment' => $comment,
467 'rc_this_oldid' => $newId,
468 'rc_last_oldid' => 0,
469 'rc_bot' => $bot ?
1 : 0,
470 'rc_moved_to_ns' => 0,
471 'rc_moved_to_title' => '',
473 'rc_patrolled' => intval($patrol),
474 'rc_new' => 1, # obsolete
476 'rc_new_len' => $size,
479 'rc_log_type' => null,
480 'rc_log_action' => '',
485 'prefixedDBkey' => $title->getPrefixedDBkey(),
486 'lastTimestamp' => 0,
498 * @param $actionComment
506 * @param $actionCommentIRC string
509 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
510 $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='' )
512 global $wgLogRestrictions;
513 # Don't add private logs to RC!
514 if( isset($wgLogRestrictions[$type]) && $wgLogRestrictions[$type] != '*' ) {
517 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
518 $target, $logComment, $params, $newId, $actionCommentIRC );
525 * @param $title Title
527 * @param $actionComment
531 * @param $target Title
535 * @param $actionCommentIRC string
536 * @return RecentChange
538 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
539 $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='' ) {
542 $ip = $wgRequest->getIP();
548 $rc = new RecentChange
;
549 $rc->mAttribs
= array(
550 'rc_timestamp' => $timestamp,
551 'rc_cur_time' => $timestamp,
552 'rc_namespace' => $target->getNamespace(),
553 'rc_title' => $target->getDBkey(),
556 'rc_cur_id' => $target->getArticleID(),
557 'rc_user' => $user->getId(),
558 'rc_user_text' => $user->getName(),
559 'rc_comment' => $logComment,
560 'rc_this_oldid' => 0,
561 'rc_last_oldid' => 0,
562 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
563 'rc_moved_to_ns' => 0,
564 'rc_moved_to_title' => '',
567 'rc_new' => 0, # obsolete
568 'rc_old_len' => null,
569 'rc_new_len' => null,
571 'rc_logid' => $newId,
572 'rc_log_type' => $type,
573 'rc_log_action' => $action,
574 'rc_params' => $params
577 'prefixedDBkey' => $title->getPrefixedDBkey(),
578 'lastTimestamp' => 0,
579 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
580 'actionCommentIRC' => $actionCommentIRC
586 * Initialises the members of this object from a mysql row object
590 public function loadFromRow( $row ) {
591 $this->mAttribs
= get_object_vars( $row );
592 $this->mAttribs
['rc_timestamp'] = wfTimestamp(TS_MW
, $this->mAttribs
['rc_timestamp']);
593 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
597 * Makes a pseudo-RC entry from a cur row
601 public function loadFromCurRow( $row ) {
602 $this->mAttribs
= array(
603 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
604 'rc_cur_time' => $row->rev_timestamp
,
605 'rc_user' => $row->rev_user
,
606 'rc_user_text' => $row->rev_user_text
,
607 'rc_namespace' => $row->page_namespace
,
608 'rc_title' => $row->page_title
,
609 'rc_comment' => $row->rev_comment
,
610 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
611 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
612 'rc_cur_id' => $row->page_id
,
613 'rc_this_oldid' => $row->rev_id
,
614 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
616 'rc_moved_to_ns' => 0,
617 'rc_moved_to_title' => '',
619 'rc_id' => $row->rc_id
,
620 'rc_patrolled' => $row->rc_patrolled
,
621 'rc_new' => $row->page_is_new
, # obsolete
622 'rc_old_len' => $row->rc_old_len
,
623 'rc_new_len' => $row->rc_new_len
,
624 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
625 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
626 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
627 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
628 'rc_deleted' => $row->rc_deleted
// MUST be set
633 * Get an attribute value
635 * @param $name String Attribute name
638 public function getAttribute( $name ) {
639 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : null;
645 public function getAttributes() {
646 return $this->mAttribs
;
650 * Gets the end part of the diff URL associated with this object
651 * Blank if no diff link should be displayed
655 public function diffLinkTrail( $forceCur ) {
656 if( $this->mAttribs
['rc_type'] == RC_EDIT
) {
657 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
658 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
660 $trail .= '&diff=0' ;
662 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
673 public function getIRCLine() {
674 global $wgUseRCPatrol, $wgUseNPPatrol, $wgRC2UDPInterwikiPrefix, $wgLocalInterwiki,
675 $wgCanonicalServer, $wgScript;
677 if( $this->mAttribs
['rc_type'] == RC_LOG
) {
678 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->mAttribs
['rc_log_type'] );
680 $titleObj =& $this->getTitle();
682 $title = $titleObj->getPrefixedText();
683 $title = self
::cleanupForIRC( $title );
685 if( $this->mAttribs
['rc_type'] == RC_LOG
) {
688 $url = $wgCanonicalServer . $wgScript;
689 if( $this->mAttribs
['rc_type'] == RC_NEW
) {
690 $query = '?oldid=' . $this->mAttribs
['rc_this_oldid'];
692 $query = '?diff=' . $this->mAttribs
['rc_this_oldid'] . '&oldid=' . $this->mAttribs
['rc_last_oldid'];
694 if ( $wgUseRCPatrol ||
( $this->mAttribs
['rc_type'] == RC_NEW
&& $wgUseNPPatrol ) ) {
695 $query .= '&rcid=' . $this->mAttribs
['rc_id'];
697 // HACK: We need this hook for WMF's secure server setup
698 wfRunHooks( 'IRCLineURL', array( &$url, &$query ) );
702 if( $this->mAttribs
['rc_old_len'] !== null && $this->mAttribs
['rc_new_len'] !== null ) {
703 $szdiff = $this->mAttribs
['rc_new_len'] - $this->mAttribs
['rc_old_len'];
705 $szdiff = "\002$szdiff\002";
706 } elseif($szdiff >= 0) {
707 $szdiff = '+' . $szdiff ;
709 $szdiff = '(' . $szdiff . ')' ;
714 $user = self
::cleanupForIRC( $this->mAttribs
['rc_user_text'] );
716 if ( $this->mAttribs
['rc_type'] == RC_LOG
) {
717 $targetText = $this->getTitle()->getPrefixedText();
718 $comment = self
::cleanupForIRC( str_replace( "[[$targetText]]", "[[\00302$targetText\00310]]", $this->mExtra
['actionCommentIRC'] ) );
719 $flag = $this->mAttribs
['rc_log_action'];
721 $comment = self
::cleanupForIRC( $this->mAttribs
['rc_comment'] );
723 if ( !$this->mAttribs
['rc_patrolled'] && ( $wgUseRCPatrol ||
$this->mAttribs
['rc_new'] && $wgUseNPPatrol ) ) {
726 $flag .= ( $this->mAttribs
['rc_new'] ?
"N" : "" ) . ( $this->mAttribs
['rc_minor'] ?
"M" : "" ) . ( $this->mAttribs
['rc_bot'] ?
"B" : "" );
729 if ( $wgRC2UDPInterwikiPrefix === true && $wgLocalInterwiki !== false ) {
730 $prefix = $wgLocalInterwiki;
731 } elseif ( $wgRC2UDPInterwikiPrefix ) {
732 $prefix = $wgRC2UDPInterwikiPrefix;
736 if ( $prefix !== false ) {
737 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
739 $titleString = "\00314[[\00307$title\00314]]";
742 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
743 # no colour (\003) switches back to the term default
744 $fullString = "$titleString\0034 $flag\00310 " .
745 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
751 * Returns the change size (HTML).
752 * The lengths can be given optionally.
757 public function getCharacterDifference( $old = 0, $new = 0 ) {
759 $old = $this->mAttribs
['rc_old_len'];
762 $new = $this->mAttribs
['rc_new_len'];
764 if( $old === null ||
$new === null ) {
767 return ChangesList
::showCharacterDifference( $old, $new );