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 $row = $dbr->selectRow( 'recentchanges', '*', array( 'rc_id' => $rcid ), __METHOD__
);
93 if( $row !== false ) {
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 = __METHOD__
) {
108 $dbr = wfGetDB( DB_SLAVE
);
115 if( $res instanceof ResultWrapper
&& $res->numRows() > 0 ) {
116 $row = $res->fetchObject();
118 return self
::newFromRow( $row );
126 * @param $attribs array
128 public function setAttribs( $attribs ) {
129 $this->mAttribs
= $attribs;
133 * @param $extra array
135 public function setExtra( $extra ) {
136 $this->mExtra
= $extra;
143 public function &getTitle() {
144 if( $this->mTitle
=== false ) {
145 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
146 # Make sure the correct page ID is process cached
147 $this->mTitle
->resetArticleID( $this->mAttribs
['rc_cur_id'] );
149 return $this->mTitle
;
153 * @return bool|\Title
155 public function getMovedToTitle() {
156 if( $this->mMovedToTitle
=== false ) {
157 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
158 $this->mAttribs
['rc_moved_to_title'] );
160 return $this->mMovedToTitle
;
164 * Writes the data in this object to the database
167 public function save( $noudp = false ) {
168 global $wgLocalInterwiki, $wgPutIPinRC, $wgContLang;
170 $dbw = wfGetDB( DB_MASTER
);
171 if( !is_array($this->mExtra
) ) {
172 $this->mExtra
= array();
174 $this->mExtra
['lang'] = $wgLocalInterwiki;
176 if( !$wgPutIPinRC ) {
177 $this->mAttribs
['rc_ip'] = '';
180 # If our database is strict about IP addresses, use NULL instead of an empty string
181 if( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
182 unset( $this->mAttribs
['rc_ip'] );
185 # Make sure summary is truncated (whole multibyte characters)
186 $this->mAttribs
['rc_comment'] = $wgContLang->truncate( $this->mAttribs
['rc_comment'], 255 );
188 # Fixup database timestamps
189 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
190 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
191 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
193 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
194 if( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
195 unset( $this->mAttribs
['rc_cur_id'] );
199 $dbw->insert( 'recentchanges', $this->mAttribs
, __METHOD__
);
202 $this->mAttribs
['rc_id'] = $dbw->insertId();
205 wfRunHooks( 'RecentChange_save', array( &$this ) );
207 # Notify external application via UDP
209 $this->notifyRC2UDP();
212 # E-mail notifications
213 global $wgUseEnotif, $wgShowUpdatedMarker, $wgUser;
214 if( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
216 if( $this->mAttribs
['rc_user'] ) {
217 $editor = ($wgUser->getId() == $this->mAttribs
['rc_user']) ?
218 $wgUser : User
::newFromID( $this->mAttribs
['rc_user'] );
221 $editor = ($wgUser->getName() == $this->mAttribs
['rc_user_text']) ?
222 $wgUser : User
::newFromName( $this->mAttribs
['rc_user_text'], false );
224 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
226 # @todo FIXME: This would be better as an extension hook
227 $enotif = new EmailNotification();
228 $status = $enotif->notifyOnPageChange( $editor, $title,
229 $this->mAttribs
['rc_timestamp'],
230 $this->mAttribs
['rc_comment'],
231 $this->mAttribs
['rc_minor'],
232 $this->mAttribs
['rc_last_oldid'] );
236 public function notifyRC2UDP() {
237 global $wgRC2UDPAddress, $wgRC2UDPOmitBots;
238 # Notify external application via UDP
239 if( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
240 self
::sendToUDP( $this->getIRCLine() );
245 * Send some text to UDP.
246 * @see RecentChange::cleanupForIRC
247 * @param $line String: text to send
248 * @param $address String: defaults to $wgRC2UDPAddress.
249 * @param $prefix String: defaults to $wgRC2UDPPrefix.
250 * @param $port Int: defaults to $wgRC2UDPPort. (Since 1.17)
251 * @return Boolean: success
253 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) {
254 global $wgRC2UDPAddress, $wgRC2UDPPrefix, $wgRC2UDPPort;
255 # Assume default for standard RC case
256 $address = $address ?
$address : $wgRC2UDPAddress;
257 $prefix = $prefix ?
$prefix : $wgRC2UDPPrefix;
258 $port = $port ?
$port : $wgRC2UDPPort;
259 # Notify external application via UDP
261 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
263 $line = $prefix . $line;
264 wfDebug( __METHOD__
. ": sending UDP line: $line\n" );
265 socket_sendto( $conn, $line, strlen($line), 0, $address, $port );
266 socket_close( $conn );
269 wfDebug( __METHOD__
. ": failed to create UDP socket\n" );
276 * Remove newlines, carriage returns and decode html entites
277 * @param $text String
280 public static function cleanupForIRC( $text ) {
281 return Sanitizer
::decodeCharReferences( str_replace( array( "\n", "\r" ), array( "", "" ), $text ) );
285 * Mark a given change as patrolled
287 * @param $change Mixed: RecentChange or corresponding rc_id
288 * @param $auto Boolean: for automatic patrol
289 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
291 public static function markPatrolled( $change, $auto = false ) {
294 $change = $change instanceof RecentChange
296 : RecentChange
::newFromId($change);
298 if( !$change instanceof RecentChange
) {
301 return $change->doMarkPatrolled( $wgUser, $auto );
305 * Mark this RecentChange as patrolled
307 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
308 * @param $user User object doing the action
309 * @param $auto Boolean: for automatic patrol
310 * @return array of permissions errors, see Title::getUserPermissionsErrors()
312 public function doMarkPatrolled( User
$user, $auto = false ) {
313 global $wgUseRCPatrol, $wgUseNPPatrol;
315 // If recentchanges patrol is disabled, only new pages
317 if( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute('rc_type') != RC_NEW
) ) {
318 $errors[] = array('rcpatroldisabled');
320 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
321 $right = $auto ?
'autopatrol' : 'patrol';
322 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
323 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$user, false)) ) {
324 $errors[] = array('hookaborted');
326 // Users without the 'autopatrol' right can't patrol their
328 if( $user->getName() == $this->getAttribute('rc_user_text') && !$user->isAllowed('autopatrol') ) {
329 $errors[] = array('markedaspatrollederror-noautopatrol');
334 // If the change was patrolled already, do nothing
335 if( $this->getAttribute('rc_patrolled') ) {
338 // Actually set the 'patrolled' flag in RC
339 $this->reallyMarkPatrolled();
340 // Log this patrol event
341 PatrolLog
::record( $this, $auto, $user );
342 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$user, false) );
347 * Mark this RecentChange patrolled, without error checking
348 * @return Integer: number of affected rows
350 public function reallyMarkPatrolled() {
351 $dbw = wfGetDB( DB_MASTER
);
358 'rc_id' => $this->getAttribute('rc_id')
362 return $dbw->affectedRows();
366 * Makes an entry in the database corresponding to an edit
369 * @param $title Title
374 * @param $lastTimestamp
377 * @param $oldSize int
378 * @param $newSize int
381 * @return RecentChange
383 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
384 $lastTimestamp, $bot, $ip='', $oldSize=0, $newSize=0, $newId=0, $patrol=0 ) {
385 $rc = new RecentChange
;
386 $rc->mAttribs
= array(
387 'rc_timestamp' => $timestamp,
388 'rc_cur_time' => $timestamp,
389 'rc_namespace' => $title->getNamespace(),
390 'rc_title' => $title->getDBkey(),
391 'rc_type' => RC_EDIT
,
392 'rc_minor' => $minor ?
1 : 0,
393 'rc_cur_id' => $title->getArticleID(),
394 'rc_user' => $user->getId(),
395 'rc_user_text' => $user->getName(),
396 'rc_comment' => $comment,
397 'rc_this_oldid' => $newId,
398 'rc_last_oldid' => $oldId,
399 'rc_bot' => $bot ?
1 : 0,
400 'rc_moved_to_ns' => 0,
401 'rc_moved_to_title' => '',
402 'rc_ip' => self
::checkIPAddress( $ip ),
403 'rc_patrolled' => intval($patrol),
404 'rc_new' => 0, # obsolete
405 'rc_old_len' => $oldSize,
406 'rc_new_len' => $newSize,
409 'rc_log_type' => null,
410 'rc_log_action' => '',
415 'prefixedDBkey' => $title->getPrefixedDBkey(),
416 'lastTimestamp' => $lastTimestamp,
417 'oldSize' => $oldSize,
418 'newSize' => $newSize,
425 * Makes an entry in the database corresponding to page creation
426 * Note: the title object must be loaded with the new id using resetArticleID()
427 * @todo Document parameters and return
430 * @param $title Title
439 * @return RecentChange
441 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
442 $ip='', $size=0, $newId=0, $patrol=0 ) {
443 $rc = new RecentChange
;
444 $rc->mAttribs
= array(
445 'rc_timestamp' => $timestamp,
446 'rc_cur_time' => $timestamp,
447 'rc_namespace' => $title->getNamespace(),
448 'rc_title' => $title->getDBkey(),
450 'rc_minor' => $minor ?
1 : 0,
451 'rc_cur_id' => $title->getArticleID(),
452 'rc_user' => $user->getId(),
453 'rc_user_text' => $user->getName(),
454 'rc_comment' => $comment,
455 'rc_this_oldid' => $newId,
456 'rc_last_oldid' => 0,
457 'rc_bot' => $bot ?
1 : 0,
458 'rc_moved_to_ns' => 0,
459 'rc_moved_to_title' => '',
460 'rc_ip' => self
::checkIPAddress( $ip ),
461 'rc_patrolled' => intval($patrol),
462 'rc_new' => 1, # obsolete
464 'rc_new_len' => $size,
467 'rc_log_type' => null,
468 'rc_log_action' => '',
473 'prefixedDBkey' => $title->getPrefixedDBkey(),
474 'lastTimestamp' => 0,
486 * @param $actionComment
494 * @param $actionCommentIRC string
497 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
498 $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='' )
500 global $wgLogRestrictions;
501 # Don't add private logs to RC!
502 if( isset($wgLogRestrictions[$type]) && $wgLogRestrictions[$type] != '*' ) {
505 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
506 $target, $logComment, $params, $newId, $actionCommentIRC );
513 * @param $title Title
515 * @param $actionComment
519 * @param $target Title
523 * @param $actionCommentIRC string
524 * @return RecentChange
526 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
527 $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='' ) {
530 $rc = new RecentChange
;
531 $rc->mAttribs
= array(
532 'rc_timestamp' => $timestamp,
533 'rc_cur_time' => $timestamp,
534 'rc_namespace' => $target->getNamespace(),
535 'rc_title' => $target->getDBkey(),
538 'rc_cur_id' => $target->getArticleID(),
539 'rc_user' => $user->getId(),
540 'rc_user_text' => $user->getName(),
541 'rc_comment' => $logComment,
542 'rc_this_oldid' => 0,
543 'rc_last_oldid' => 0,
544 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
545 'rc_moved_to_ns' => 0,
546 'rc_moved_to_title' => '',
547 'rc_ip' => self
::checkIPAddress( $ip ),
549 'rc_new' => 0, # obsolete
550 'rc_old_len' => null,
551 'rc_new_len' => null,
553 'rc_logid' => $newId,
554 'rc_log_type' => $type,
555 'rc_log_action' => $action,
556 'rc_params' => $params
560 'prefixedDBkey' => $title->getPrefixedDBkey(),
561 'lastTimestamp' => 0,
562 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
563 'actionCommentIRC' => $actionCommentIRC
569 * Initialises the members of this object from a mysql row object
573 public function loadFromRow( $row ) {
574 $this->mAttribs
= get_object_vars( $row );
575 $this->mAttribs
['rc_timestamp'] = wfTimestamp(TS_MW
, $this->mAttribs
['rc_timestamp']);
576 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
580 * Makes a pseudo-RC entry from a cur row
584 public function loadFromCurRow( $row ) {
585 $this->mAttribs
= array(
586 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
587 'rc_cur_time' => $row->rev_timestamp
,
588 'rc_user' => $row->rev_user
,
589 'rc_user_text' => $row->rev_user_text
,
590 'rc_namespace' => $row->page_namespace
,
591 'rc_title' => $row->page_title
,
592 'rc_comment' => $row->rev_comment
,
593 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
594 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
595 'rc_cur_id' => $row->page_id
,
596 'rc_this_oldid' => $row->rev_id
,
597 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
599 'rc_moved_to_ns' => 0,
600 'rc_moved_to_title' => '',
602 'rc_id' => $row->rc_id
,
603 'rc_patrolled' => $row->rc_patrolled
,
604 'rc_new' => $row->page_is_new
, # obsolete
605 'rc_old_len' => $row->rc_old_len
,
606 'rc_new_len' => $row->rc_new_len
,
607 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
608 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
609 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
610 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
611 'rc_deleted' => $row->rc_deleted
// MUST be set
616 * Get an attribute value
618 * @param $name String Attribute name
621 public function getAttribute( $name ) {
622 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : null;
628 public function getAttributes() {
629 return $this->mAttribs
;
633 * Gets the end part of the diff URL associated with this object
634 * Blank if no diff link should be displayed
638 public function diffLinkTrail( $forceCur ) {
639 if( $this->mAttribs
['rc_type'] == RC_EDIT
) {
640 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
641 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
643 $trail .= '&diff=0' ;
645 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
656 public function getIRCLine() {
657 global $wgUseRCPatrol, $wgUseNPPatrol, $wgRC2UDPInterwikiPrefix, $wgLocalInterwiki,
658 $wgCanonicalServer, $wgScript;
660 if( $this->mAttribs
['rc_type'] == RC_LOG
) {
661 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->mAttribs
['rc_log_type'] );
663 $titleObj =& $this->getTitle();
665 $title = $titleObj->getPrefixedText();
666 $title = self
::cleanupForIRC( $title );
668 if( $this->mAttribs
['rc_type'] == RC_LOG
) {
671 $url = $wgCanonicalServer . $wgScript;
672 if( $this->mAttribs
['rc_type'] == RC_NEW
) {
673 $query = '?oldid=' . $this->mAttribs
['rc_this_oldid'];
675 $query = '?diff=' . $this->mAttribs
['rc_this_oldid'] . '&oldid=' . $this->mAttribs
['rc_last_oldid'];
677 if ( $wgUseRCPatrol ||
( $this->mAttribs
['rc_type'] == RC_NEW
&& $wgUseNPPatrol ) ) {
678 $query .= '&rcid=' . $this->mAttribs
['rc_id'];
680 // HACK: We need this hook for WMF's secure server setup
681 wfRunHooks( 'IRCLineURL', array( &$url, &$query ) );
685 if( $this->mAttribs
['rc_old_len'] !== null && $this->mAttribs
['rc_new_len'] !== null ) {
686 $szdiff = $this->mAttribs
['rc_new_len'] - $this->mAttribs
['rc_old_len'];
688 $szdiff = "\002$szdiff\002";
689 } elseif($szdiff >= 0) {
690 $szdiff = '+' . $szdiff ;
692 // @todo i18n with parentheses in content language?
693 $szdiff = '(' . $szdiff . ')' ;
698 $user = self
::cleanupForIRC( $this->mAttribs
['rc_user_text'] );
700 if ( $this->mAttribs
['rc_type'] == RC_LOG
) {
701 $targetText = $this->getTitle()->getPrefixedText();
702 $comment = self
::cleanupForIRC( str_replace( "[[$targetText]]", "[[\00302$targetText\00310]]", $this->mExtra
['actionCommentIRC'] ) );
703 $flag = $this->mAttribs
['rc_log_action'];
705 $comment = self
::cleanupForIRC( $this->mAttribs
['rc_comment'] );
707 if ( !$this->mAttribs
['rc_patrolled'] && ( $wgUseRCPatrol ||
$this->mAttribs
['rc_new'] && $wgUseNPPatrol ) ) {
710 $flag .= ( $this->mAttribs
['rc_new'] ?
"N" : "" ) . ( $this->mAttribs
['rc_minor'] ?
"M" : "" ) . ( $this->mAttribs
['rc_bot'] ?
"B" : "" );
713 if ( $wgRC2UDPInterwikiPrefix === true && $wgLocalInterwiki !== false ) {
714 $prefix = $wgLocalInterwiki;
715 } elseif ( $wgRC2UDPInterwikiPrefix ) {
716 $prefix = $wgRC2UDPInterwikiPrefix;
720 if ( $prefix !== false ) {
721 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
723 $titleString = "\00314[[\00307$title\00314]]";
726 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
727 # no colour (\003) switches back to the term default
728 $fullString = "$titleString\0034 $flag\00310 " .
729 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
735 * Returns the change size (HTML).
736 * The lengths can be given optionally.
741 public function getCharacterDifference( $old = 0, $new = 0 ) {
743 $old = $this->mAttribs
['rc_old_len'];
746 $new = $this->mAttribs
['rc_new_len'];
748 if( $old === null ||
$new === null ) {
751 return ChangesList
::showCharacterDifference( $old, $new );
754 private static function checkIPAddress( $ip ) {
757 if ( !IP
::isIPAddress( $ip ) ) {
758 throw new MWException( "Attempt to write \"" . $ip . "\" as an IP address into recent changes" );
761 $ip = $wgRequest->getIP();