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
63 * @return RecentChange
65 public static function newFromRow( $row ) {
66 $rc = new RecentChange
;
67 $rc->loadFromRow( $row );
73 * @return RecentChange
75 public static function newFromCurRow( $row ) {
76 $rc = new RecentChange
;
77 $rc->loadFromCurRow( $row );
78 $rc->notificationtimestamp
= false;
79 $rc->numberofWatchingusers
= false;
84 * Obtain the recent change with a given rc_id value
86 * @param $rcid Int rc_id value to retrieve
87 * @return RecentChange
89 public static function newFromId( $rcid ) {
90 $dbr = wfGetDB( DB_SLAVE
);
91 $res = $dbr->select( 'recentchanges', '*', array( 'rc_id' => $rcid ), __METHOD__
);
92 if( $res && $dbr->numRows( $res ) > 0 ) {
93 $row = $dbr->fetchObject( $res );
94 return self
::newFromRow( $row );
101 * Find the first recent change matching some specific conditions
103 * @param $conds Array of conditions
104 * @param $fname Mixed: override the method name in profiling/logs
105 * @return RecentChange
107 public static function newFromConds( $conds, $fname = false ) {
108 if( $fname === false )
110 $dbr = wfGetDB( DB_SLAVE
);
117 if( $res instanceof ResultWrapper
&& $res->numRows() > 0 ) {
118 $row = $res->fetchObject();
120 return self
::newFromRow( $row );
127 public function setAttribs( $attribs ) {
128 $this->mAttribs
= $attribs;
131 public function setExtra( $extra ) {
132 $this->mExtra
= $extra;
139 public function &getTitle() {
140 if( $this->mTitle
=== false ) {
141 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
142 # Make sure the correct page ID is process cached
143 $this->mTitle
->resetArticleID( $this->mAttribs
['rc_cur_id'] );
145 return $this->mTitle
;
149 * @return bool|\Title
151 public function getMovedToTitle() {
152 if( $this->mMovedToTitle
=== false ) {
153 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
154 $this->mAttribs
['rc_moved_to_title'] );
156 return $this->mMovedToTitle
;
160 * Writes the data in this object to the database
163 public function save( $noudp = false ) {
164 global $wgLocalInterwiki, $wgPutIPinRC;
166 $dbw = wfGetDB( DB_MASTER
);
167 if( !is_array($this->mExtra
) ) {
168 $this->mExtra
= array();
170 $this->mExtra
['lang'] = $wgLocalInterwiki;
172 if( !$wgPutIPinRC ) {
173 $this->mAttribs
['rc_ip'] = '';
176 # If our database is strict about IP addresses, use NULL instead of an empty string
177 if( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
178 unset( $this->mAttribs
['rc_ip'] );
181 # Fixup database timestamps
182 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
183 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
184 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
186 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
187 if( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
188 unset( $this->mAttribs
['rc_cur_id'] );
192 $dbw->insert( 'recentchanges', $this->mAttribs
, __METHOD__
);
195 $this->mAttribs
['rc_id'] = $dbw->insertId();
198 wfRunHooks( 'RecentChange_save', array( &$this ) );
200 # Notify external application via UDP
202 $this->notifyRC2UDP();
205 # E-mail notifications
206 global $wgUseEnotif, $wgShowUpdatedMarker, $wgUser;
207 if( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
209 if( $this->mAttribs
['rc_user'] ) {
210 $editor = ($wgUser->getId() == $this->mAttribs
['rc_user']) ?
211 $wgUser : User
::newFromID( $this->mAttribs
['rc_user'] );
214 $editor = ($wgUser->getName() == $this->mAttribs
['rc_user_text']) ?
215 $wgUser : User
::newFromName( $this->mAttribs
['rc_user_text'], false );
217 # @todo FIXME: This would be better as an extension hook
218 $enotif = new EmailNotification();
219 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
220 $enotif->notifyOnPageChange( $editor, $title,
221 $this->mAttribs
['rc_timestamp'],
222 $this->mAttribs
['rc_comment'],
223 $this->mAttribs
['rc_minor'],
224 $this->mAttribs
['rc_last_oldid'] );
228 public function notifyRC2UDP() {
229 global $wgRC2UDPAddress, $wgRC2UDPOmitBots;
230 # Notify external application via UDP
231 if( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
232 self
::sendToUDP( $this->getIRCLine() );
237 * Send some text to UDP.
238 * @see RecentChange::cleanupForIRC
239 * @param $line String: text to send
240 * @param $address String: defaults to $wgRC2UDPAddress.
241 * @param $prefix String: defaults to $wgRC2UDPPrefix.
242 * @param $port Int: defaults to $wgRC2UDPPort. (Since 1.17)
243 * @return Boolean: success
245 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) {
246 global $wgRC2UDPAddress, $wgRC2UDPPrefix, $wgRC2UDPPort;
247 # Assume default for standard RC case
248 $address = $address ?
$address : $wgRC2UDPAddress;
249 $prefix = $prefix ?
$prefix : $wgRC2UDPPrefix;
250 $port = $port ?
$port : $wgRC2UDPPort;
251 # Notify external application via UDP
253 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
255 $line = $prefix . $line;
256 wfDebug( __METHOD__
. ": sending UDP line: $line\n" );
257 socket_sendto( $conn, $line, strlen($line), 0, $address, $port );
258 socket_close( $conn );
261 wfDebug( __METHOD__
. ": failed to create UDP socket\n" );
268 * Remove newlines, carriage returns and decode html entites
269 * @param $text String
272 public static function cleanupForIRC( $text ) {
273 return Sanitizer
::decodeCharReferences( str_replace( array( "\n", "\r" ), array( "", "" ), $text ) );
277 * Mark a given change as patrolled
279 * @param $change Mixed: RecentChange or corresponding rc_id
280 * @param $auto Boolean: for automatic patrol
281 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
283 public static function markPatrolled( $change, $auto = false ) {
286 $change = $change instanceof RecentChange
288 : RecentChange
::newFromId($change);
290 if( !$change instanceof RecentChange
) {
293 return $change->doMarkPatrolled( $wgUser, $auto );
297 * Mark this RecentChange as patrolled
299 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
300 * @param $user User object doing the action
301 * @param $auto Boolean: for automatic patrol
302 * @return array of permissions errors, see Title::getUserPermissionsErrors()
304 public function doMarkPatrolled( User
$user, $auto = false ) {
305 global $wgUseRCPatrol, $wgUseNPPatrol;
307 // If recentchanges patrol is disabled, only new pages
309 if( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute('rc_type') != RC_NEW
) ) {
310 $errors[] = array('rcpatroldisabled');
312 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
313 $right = $auto ?
'autopatrol' : 'patrol';
314 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
315 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$user, false)) ) {
316 $errors[] = array('hookaborted');
318 // Users without the 'autopatrol' right can't patrol their
320 if( $user->getName() == $this->getAttribute('rc_user_text') && !$user->isAllowed('autopatrol') ) {
321 $errors[] = array('markedaspatrollederror-noautopatrol');
326 // If the change was patrolled already, do nothing
327 if( $this->getAttribute('rc_patrolled') ) {
330 // Actually set the 'patrolled' flag in RC
331 $this->reallyMarkPatrolled();
332 // Log this patrol event
333 PatrolLog
::record( $this, $auto );
334 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$user, false) );
339 * Mark this RecentChange patrolled, without error checking
340 * @return Integer: number of affected rows
342 public function reallyMarkPatrolled() {
343 $dbw = wfGetDB( DB_MASTER
);
350 'rc_id' => $this->getAttribute('rc_id')
354 return $dbw->affectedRows();
358 * Makes an entry in the database corresponding to an edit
361 * @param $title Title
366 * @param $lastTimestamp
369 * @param $oldSize int
370 * @param $newSize int
373 * @return RecentChange
375 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
376 $lastTimestamp, $bot, $ip='', $oldSize=0, $newSize=0, $newId=0, $patrol=0 ) {
379 $ip = $wgRequest->getIP();
383 $rc = new RecentChange
;
384 $rc->mAttribs
= array(
385 'rc_timestamp' => $timestamp,
386 'rc_cur_time' => $timestamp,
387 'rc_namespace' => $title->getNamespace(),
388 'rc_title' => $title->getDBkey(),
389 'rc_type' => RC_EDIT
,
390 'rc_minor' => $minor ?
1 : 0,
391 'rc_cur_id' => $title->getArticleID(),
392 'rc_user' => $user->getId(),
393 'rc_user_text' => $user->getName(),
394 'rc_comment' => $comment,
395 'rc_this_oldid' => $newId,
396 'rc_last_oldid' => $oldId,
397 'rc_bot' => $bot ?
1 : 0,
398 'rc_moved_to_ns' => 0,
399 'rc_moved_to_title' => '',
401 'rc_patrolled' => intval($patrol),
402 'rc_new' => 0, # obsolete
403 'rc_old_len' => $oldSize,
404 'rc_new_len' => $newSize,
407 'rc_log_type' => null,
408 'rc_log_action' => '',
413 'prefixedDBkey' => $title->getPrefixedDBkey(),
414 'lastTimestamp' => $lastTimestamp,
415 'oldSize' => $oldSize,
416 'newSize' => $newSize,
423 * Makes an entry in the database corresponding to page creation
424 * Note: the title object must be loaded with the new id using resetArticleID()
425 * @todo Document parameters and return
428 * @param $title Title
437 * @return RecentChange
439 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
440 $ip='', $size=0, $newId=0, $patrol=0 ) {
443 $ip = $wgRequest->getIP();
447 $rc = new RecentChange
;
448 $rc->mAttribs
= array(
449 'rc_timestamp' => $timestamp,
450 'rc_cur_time' => $timestamp,
451 'rc_namespace' => $title->getNamespace(),
452 'rc_title' => $title->getDBkey(),
454 'rc_minor' => $minor ?
1 : 0,
455 'rc_cur_id' => $title->getArticleID(),
456 'rc_user' => $user->getId(),
457 'rc_user_text' => $user->getName(),
458 'rc_comment' => $comment,
459 'rc_this_oldid' => $newId,
460 'rc_last_oldid' => 0,
461 'rc_bot' => $bot ?
1 : 0,
462 'rc_moved_to_ns' => 0,
463 'rc_moved_to_title' => '',
465 'rc_patrolled' => intval($patrol),
466 'rc_new' => 1, # obsolete
468 'rc_new_len' => $size,
471 'rc_log_type' => null,
472 'rc_log_action' => '',
477 'prefixedDBkey' => $title->getPrefixedDBkey(),
478 'lastTimestamp' => 0,
490 * @param $actionComment
500 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='', $type,
501 $action, $target, $logComment, $params, $newId=0 )
503 global $wgLogRestrictions;
504 # Don't add private logs to RC!
505 if( isset($wgLogRestrictions[$type]) && $wgLogRestrictions[$type] != '*' ) {
508 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
509 $target, $logComment, $params, $newId );
516 * @param $title Title
518 * @param $actionComment
522 * @param $target Title
526 * @return RecentChange
528 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip='',
529 $type, $action, $target, $logComment, $params, $newId=0 ) {
532 $ip = $wgRequest->getIP();
538 $rc = new RecentChange
;
539 $rc->mAttribs
= array(
540 'rc_timestamp' => $timestamp,
541 'rc_cur_time' => $timestamp,
542 'rc_namespace' => $target->getNamespace(),
543 'rc_title' => $target->getDBkey(),
546 'rc_cur_id' => $target->getArticleID(),
547 'rc_user' => $user->getId(),
548 'rc_user_text' => $user->getName(),
549 'rc_comment' => $logComment,
550 'rc_this_oldid' => 0,
551 'rc_last_oldid' => 0,
552 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
553 'rc_moved_to_ns' => 0,
554 'rc_moved_to_title' => '',
557 'rc_new' => 0, # obsolete
558 'rc_old_len' => null,
559 'rc_new_len' => null,
561 'rc_logid' => $newId,
562 'rc_log_type' => $type,
563 'rc_log_action' => $action,
564 'rc_params' => $params
567 'prefixedDBkey' => $title->getPrefixedDBkey(),
568 'lastTimestamp' => 0,
569 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
575 * Initialises the members of this object from a mysql row object
579 public function loadFromRow( $row ) {
580 $this->mAttribs
= get_object_vars( $row );
581 $this->mAttribs
['rc_timestamp'] = wfTimestamp(TS_MW
, $this->mAttribs
['rc_timestamp']);
582 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
586 * Makes a pseudo-RC entry from a cur row
590 public function loadFromCurRow( $row ) {
591 $this->mAttribs
= array(
592 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
593 'rc_cur_time' => $row->rev_timestamp
,
594 'rc_user' => $row->rev_user
,
595 'rc_user_text' => $row->rev_user_text
,
596 'rc_namespace' => $row->page_namespace
,
597 'rc_title' => $row->page_title
,
598 'rc_comment' => $row->rev_comment
,
599 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
600 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
601 'rc_cur_id' => $row->page_id
,
602 'rc_this_oldid' => $row->rev_id
,
603 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
605 'rc_moved_to_ns' => 0,
606 'rc_moved_to_title' => '',
608 'rc_id' => $row->rc_id
,
609 'rc_patrolled' => $row->rc_patrolled
,
610 'rc_new' => $row->page_is_new
, # obsolete
611 'rc_old_len' => $row->rc_old_len
,
612 'rc_new_len' => $row->rc_new_len
,
613 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
614 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
615 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
616 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
617 'rc_deleted' => $row->rc_deleted
// MUST be set
622 * Get an attribute value
624 * @param $name String Attribute name
627 public function getAttribute( $name ) {
628 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : null;
634 public function getAttributes() {
635 return $this->mAttribs
;
639 * Gets the end part of the diff URL associated with this object
640 * Blank if no diff link should be displayed
644 public function diffLinkTrail( $forceCur ) {
645 if( $this->mAttribs
['rc_type'] == RC_EDIT
) {
646 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
647 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
649 $trail .= '&diff=0' ;
651 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
662 public function getIRCLine() {
663 global $wgUseRCPatrol, $wgUseNPPatrol, $wgRC2UDPInterwikiPrefix, $wgLocalInterwiki,
664 $wgCanonicalServer, $wgScript;
666 if( $this->mAttribs
['rc_type'] == RC_LOG
) {
667 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->mAttribs
['rc_log_type'] );
669 $titleObj =& $this->getTitle();
671 $title = $titleObj->getPrefixedText();
672 $title = self
::cleanupForIRC( $title );
674 if( $this->mAttribs
['rc_type'] == RC_LOG
) {
677 $url = $wgCanonicalServer . $wgScript;
678 if( $this->mAttribs
['rc_type'] == RC_NEW
) {
679 $query = '?oldid=' . $this->mAttribs
['rc_this_oldid'];
681 $query = '?diff=' . $this->mAttribs
['rc_this_oldid'] . '&oldid=' . $this->mAttribs
['rc_last_oldid'];
683 if ( $wgUseRCPatrol ||
( $this->mAttribs
['rc_type'] == RC_NEW
&& $wgUseNPPatrol ) ) {
684 $query .= '&rcid=' . $this->mAttribs
['rc_id'];
686 // HACK: We need this hook for WMF's secure server setup
687 wfRunHooks( 'IRCLineURL', array( &$url, &$query ) );
691 if( $this->mAttribs
['rc_old_len'] !== null && $this->mAttribs
['rc_new_len'] !== null ) {
692 $szdiff = $this->mAttribs
['rc_new_len'] - $this->mAttribs
['rc_old_len'];
694 $szdiff = "\002$szdiff\002";
695 } elseif($szdiff >= 0) {
696 $szdiff = '+' . $szdiff ;
698 $szdiff = '(' . $szdiff . ')' ;
703 $user = self
::cleanupForIRC( $this->mAttribs
['rc_user_text'] );
705 if ( $this->mAttribs
['rc_type'] == RC_LOG
) {
706 $targetText = $this->getTitle()->getPrefixedText();
707 $comment = self
::cleanupForIRC( str_replace( "[[$targetText]]", "[[\00302$targetText\00310]]", $this->mExtra
['actionComment'] ) );
708 $flag = $this->mAttribs
['rc_log_action'];
710 $comment = self
::cleanupForIRC( $this->mAttribs
['rc_comment'] );
712 if ( !$this->mAttribs
['rc_patrolled'] && ( $wgUseRCPatrol ||
$this->mAttribs
['rc_new'] && $wgUseNPPatrol ) ) {
715 $flag .= ( $this->mAttribs
['rc_new'] ?
"N" : "" ) . ( $this->mAttribs
['rc_minor'] ?
"M" : "" ) . ( $this->mAttribs
['rc_bot'] ?
"B" : "" );
718 if ( $wgRC2UDPInterwikiPrefix === true && $wgLocalInterwiki !== false ) {
719 $prefix = $wgLocalInterwiki;
720 } elseif ( $wgRC2UDPInterwikiPrefix ) {
721 $prefix = $wgRC2UDPInterwikiPrefix;
725 if ( $prefix !== false ) {
726 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
728 $titleString = "\00314[[\00307$title\00314]]";
731 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
732 # no colour (\003) switches back to the term default
733 $fullString = "$titleString\0034 $flag\00310 " .
734 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
740 * Returns the change size (HTML).
741 * The lengths can be given optionally.
746 public function getCharacterDifference( $old = 0, $new = 0 ) {
748 $old = $this->mAttribs
['rc_old_len'];
751 $new = $this->mAttribs
['rc_new_len'];
753 if( $old === null ||
$new === null ) {
756 return ChangesList
::showCharacterDifference( $old, $new );