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
61 public static function newFromRow( $row ) {
62 $rc = new RecentChange
;
63 $rc->loadFromRow( $row );
67 public static function newFromCurRow( $row ) {
68 $rc = new RecentChange
;
69 $rc->loadFromCurRow( $row );
70 $rc->notificationtimestamp
= false;
71 $rc->numberofWatchingusers
= false;
76 * Obtain the recent change with a given rc_id value
78 * @param $rcid Int rc_id value to retrieve
79 * @return RecentChange
81 public static function newFromId( $rcid ) {
82 $dbr = wfGetDB( DB_SLAVE
);
83 $res = $dbr->select( 'recentchanges', '*', array( 'rc_id' => $rcid ), __METHOD__
);
84 if( $res && $dbr->numRows( $res ) > 0 ) {
85 $row = $dbr->fetchObject( $res );
86 return self
::newFromRow( $row );
93 * Find the first recent change matching some specific conditions
95 * @param $conds Array of conditions
96 * @param $fname Mixed: override the method name in profiling/logs
97 * @return RecentChange
99 public static function newFromConds( $conds, $fname = false ) {
100 if( $fname === false )
102 $dbr = wfGetDB( DB_SLAVE
);
109 if( $res instanceof ResultWrapper
&& $res->numRows() > 0 ) {
110 $row = $res->fetchObject();
112 return self
::newFromRow( $row );
119 public function setAttribs( $attribs ) {
120 $this->mAttribs
= $attribs;
123 public function setExtra( $extra ) {
124 $this->mExtra
= $extra;
131 public function &getTitle() {
132 if( $this->mTitle
=== false ) {
133 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
134 # Make sure the correct page ID is process cached
135 $this->mTitle
->resetArticleID( $this->mAttribs
['rc_cur_id'] );
137 return $this->mTitle
;
140 public function getMovedToTitle() {
141 if( $this->mMovedToTitle
=== false ) {
142 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
143 $this->mAttribs
['rc_moved_to_title'] );
145 return $this->mMovedToTitle
;
148 # Writes the data in this object to the database
149 public function save() {
150 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPOmitBots;
151 $fname = 'RecentChange::save';
153 $dbw = wfGetDB( DB_MASTER
);
154 if( !is_array($this->mExtra
) ) {
155 $this->mExtra
= array();
157 $this->mExtra
['lang'] = $wgLocalInterwiki;
159 if( !$wgPutIPinRC ) {
160 $this->mAttribs
['rc_ip'] = '';
163 # If our database is strict about IP addresses, use NULL instead of an empty string
164 if( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
165 unset( $this->mAttribs
['rc_ip'] );
168 # Fixup database timestamps
169 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
170 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
171 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
173 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
174 if( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
175 unset( $this->mAttribs
['rc_cur_id'] );
179 $dbw->insert( 'recentchanges', $this->mAttribs
, $fname );
182 $this->mAttribs
['rc_id'] = $dbw->insertId();
185 wfRunHooks( 'RecentChange_save', array( &$this ) );
187 # Notify external application via UDP
188 if( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
189 self
::sendToUDP( $this->getIRCLine() );
192 # E-mail notifications
193 global $wgUseEnotif, $wgShowUpdatedMarker, $wgUser;
194 if( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
196 if( $this->mAttribs
['rc_user'] ) {
197 $editor = ($wgUser->getId() == $this->mAttribs
['rc_user']) ?
198 $wgUser : User
::newFromID( $this->mAttribs
['rc_user'] );
201 $editor = ($wgUser->getName() == $this->mAttribs
['rc_user_text']) ?
202 $wgUser : User
::newFromName( $this->mAttribs
['rc_user_text'], false );
204 # @todo FIXME: This would be better as an extension hook
205 $enotif = new EmailNotification();
206 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
207 $enotif->notifyOnPageChange( $editor, $title,
208 $this->mAttribs
['rc_timestamp'],
209 $this->mAttribs
['rc_comment'],
210 $this->mAttribs
['rc_minor'],
211 $this->mAttribs
['rc_last_oldid'] );
215 public function notifyRC2UDP() {
216 global $wgRC2UDPAddress, $wgRC2UDPOmitBots;
217 # Notify external application via UDP
218 if( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
219 self
::sendToUDP( $this->getIRCLine() );
224 * Send some text to UDP.
225 * @see RecentChange::cleanupForIRC
226 * @param $line String: text to send
227 * @param $address String: defaults to $wgRC2UDPAddress.
228 * @param $prefix String: defaults to $wgRC2UDPPrefix.
229 * @param $port Int: defaults to $wgRC2UDPPort. (Since 1.17)
230 * @return Boolean: success
232 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) {
233 global $wgRC2UDPAddress, $wgRC2UDPPrefix, $wgRC2UDPPort;
234 # Assume default for standard RC case
235 $address = $address ?
$address : $wgRC2UDPAddress;
236 $prefix = $prefix ?
$prefix : $wgRC2UDPPrefix;
237 $port = $port ?
$port : $wgRC2UDPPort;
238 # Notify external application via UDP
240 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
242 $line = $prefix . $line;
243 wfDebug( __METHOD__
. ": sending UDP line: $line\n" );
244 socket_sendto( $conn, $line, strlen($line), 0, $address, $port );
245 socket_close( $conn );
248 wfDebug( __METHOD__
. ": failed to create UDP socket\n" );
255 * Remove newlines, carriage returns and decode html entites
256 * @param $text String
259 public static function cleanupForIRC( $text ) {
260 return Sanitizer
::decodeCharReferences( str_replace( array( "\n", "\r" ), array( "", "" ), $text ) );
264 * Mark a given change as patrolled
266 * @param $change Mixed: RecentChange or corresponding rc_id
267 * @param $auto Boolean: for automatic patrol
268 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
270 public static function markPatrolled( $change, $auto = false ) {
273 $change = $change instanceof RecentChange
275 : RecentChange
::newFromId($change);
277 if( !$change instanceof RecentChange
) {
280 return $change->doMarkPatrolled( $wgUser, $auto );
284 * Mark this RecentChange as patrolled
286 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
287 * @param $user User object doing the action
288 * @param $auto Boolean: for automatic patrol
289 * @return array of permissions errors, see Title::getUserPermissionsErrors()
291 public function doMarkPatrolled( User
$user, $auto = false ) {
292 global $wgUseRCPatrol, $wgUseNPPatrol;
294 // If recentchanges patrol is disabled, only new pages
296 if( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute('rc_type') != RC_NEW
) ) {
297 $errors[] = array('rcpatroldisabled');
299 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
300 $right = $auto ?
'autopatrol' : 'patrol';
301 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
302 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$user, false)) ) {
303 $errors[] = array('hookaborted');
305 // Users without the 'autopatrol' right can't patrol their
307 if( $user->getName() == $this->getAttribute('rc_user_text') && !$user->isAllowed('autopatrol') ) {
308 $errors[] = array('markedaspatrollederror-noautopatrol');
313 // If the change was patrolled already, do nothing
314 if( $this->getAttribute('rc_patrolled') ) {
317 // Actually set the 'patrolled' flag in RC
318 $this->reallyMarkPatrolled();
319 // Log this patrol event
320 PatrolLog
::record( $this, $auto );
321 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$user, false) );
326 * Mark this RecentChange patrolled, without error checking
327 * @return Integer: number of affected rows
329 public function reallyMarkPatrolled() {
330 $dbw = wfGetDB( DB_MASTER
);
337 'rc_id' => $this->getAttribute('rc_id')
341 return $dbw->affectedRows();
345 * Makes an entry in the database corresponding to an edit
348 * @param $title Title
353 * @param $lastTimestamp
356 * @param $oldSize int
357 * @param $newSize int
360 * @return RecentChange
362 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
363 $lastTimestamp, $bot, $ip='', $oldSize=0, $newSize=0, $newId=0, $patrol=0 ) {
366 $ip = $wgRequest->getIP();
370 $rc = new RecentChange
;
371 $rc->mAttribs
= array(
372 'rc_timestamp' => $timestamp,
373 'rc_cur_time' => $timestamp,
374 'rc_namespace' => $title->getNamespace(),
375 'rc_title' => $title->getDBkey(),
376 'rc_type' => RC_EDIT
,
377 'rc_minor' => $minor ?
1 : 0,
378 'rc_cur_id' => $title->getArticleID(),
379 'rc_user' => $user->getId(),
380 'rc_user_text' => $user->getName(),
381 'rc_comment' => $comment,
382 'rc_this_oldid' => $newId,
383 'rc_last_oldid' => $oldId,
384 'rc_bot' => $bot ?
1 : 0,
385 'rc_moved_to_ns' => 0,
386 'rc_moved_to_title' => '',
388 'rc_patrolled' => intval($patrol),
389 'rc_new' => 0, # obsolete
390 'rc_old_len' => $oldSize,
391 'rc_new_len' => $newSize,
394 'rc_log_type' => null,
395 'rc_log_action' => '',
400 'prefixedDBkey' => $title->getPrefixedDBkey(),
401 'lastTimestamp' => $lastTimestamp,
402 'oldSize' => $oldSize,
403 'newSize' => $newSize,
410 * Makes an entry in the database corresponding to page creation
411 * Note: the title object must be loaded with the new id using resetArticleID()
412 * @todo Document parameters and return
415 * @param $title Title
424 * @return RecentChange
426 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
427 $ip='', $size=0, $newId=0, $patrol=0 ) {
430 $ip = $wgRequest->getIP();
434 $rc = new RecentChange
;
435 $rc->mAttribs
= array(
436 'rc_timestamp' => $timestamp,
437 'rc_cur_time' => $timestamp,
438 'rc_namespace' => $title->getNamespace(),
439 'rc_title' => $title->getDBkey(),
441 'rc_minor' => $minor ?
1 : 0,
442 'rc_cur_id' => $title->getArticleID(),
443 'rc_user' => $user->getId(),
444 'rc_user_text' => $user->getName(),
445 'rc_comment' => $comment,
446 'rc_this_oldid' => $newId,
447 'rc_last_oldid' => 0,
448 'rc_bot' => $bot ?
1 : 0,
449 'rc_moved_to_ns' => 0,
450 'rc_moved_to_title' => '',
452 'rc_patrolled' => intval($patrol),
453 'rc_new' => 1, # obsolete
455 'rc_new_len' => $size,
458 'rc_log_type' => null,
459 'rc_log_action' => '',
464 'prefixedDBkey' => $title->getPrefixedDBkey(),
465 'lastTimestamp' => 0,
473 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='', $type,
474 $action, $target, $logComment, $params, $newId=0 )
476 global $wgLogRestrictions;
477 # Don't add private logs to RC!
478 if( isset($wgLogRestrictions[$type]) && $wgLogRestrictions[$type] != '*' ) {
481 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
482 $target, $logComment, $params, $newId );
489 * @param $title Title
491 * @param $actionComment
495 * @param $target Title
499 * @return RecentChange
501 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip='',
502 $type, $action, $target, $logComment, $params, $newId=0 ) {
505 $ip = $wgRequest->getIP();
511 $rc = new RecentChange
;
512 $rc->mAttribs
= array(
513 'rc_timestamp' => $timestamp,
514 'rc_cur_time' => $timestamp,
515 'rc_namespace' => $target->getNamespace(),
516 'rc_title' => $target->getDBkey(),
519 'rc_cur_id' => $target->getArticleID(),
520 'rc_user' => $user->getId(),
521 'rc_user_text' => $user->getName(),
522 'rc_comment' => $logComment,
523 'rc_this_oldid' => 0,
524 'rc_last_oldid' => 0,
525 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
526 'rc_moved_to_ns' => 0,
527 'rc_moved_to_title' => '',
530 'rc_new' => 0, # obsolete
531 'rc_old_len' => null,
532 'rc_new_len' => null,
534 'rc_logid' => $newId,
535 'rc_log_type' => $type,
536 'rc_log_action' => $action,
537 'rc_params' => $params
540 'prefixedDBkey' => $title->getPrefixedDBkey(),
541 'lastTimestamp' => 0,
542 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
547 # Initialises the members of this object from a mysql row object
548 public function loadFromRow( $row ) {
549 $this->mAttribs
= get_object_vars( $row );
550 $this->mAttribs
['rc_timestamp'] = wfTimestamp(TS_MW
, $this->mAttribs
['rc_timestamp']);
551 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
554 # Makes a pseudo-RC entry from a cur row
555 public function loadFromCurRow( $row ) {
556 $this->mAttribs
= array(
557 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
558 'rc_cur_time' => $row->rev_timestamp
,
559 'rc_user' => $row->rev_user
,
560 'rc_user_text' => $row->rev_user_text
,
561 'rc_namespace' => $row->page_namespace
,
562 'rc_title' => $row->page_title
,
563 'rc_comment' => $row->rev_comment
,
564 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
565 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
566 'rc_cur_id' => $row->page_id
,
567 'rc_this_oldid' => $row->rev_id
,
568 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
570 'rc_moved_to_ns' => 0,
571 'rc_moved_to_title' => '',
573 'rc_id' => $row->rc_id
,
574 'rc_patrolled' => $row->rc_patrolled
,
575 'rc_new' => $row->page_is_new
, # obsolete
576 'rc_old_len' => $row->rc_old_len
,
577 'rc_new_len' => $row->rc_new_len
,
578 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
579 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
580 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
581 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
582 'rc_deleted' => $row->rc_deleted
// MUST be set
587 * Get an attribute value
589 * @param $name String Attribute name
592 public function getAttribute( $name ) {
593 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : null;
596 public function getAttributes() {
597 return $this->mAttribs
;
601 * Gets the end part of the diff URL associated with this object
602 * Blank if no diff link should be displayed
604 public function diffLinkTrail( $forceCur ) {
605 if( $this->mAttribs
['rc_type'] == RC_EDIT
) {
606 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
607 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
609 $trail .= '&diff=0' ;
611 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
619 public function getIRCLine() {
620 global $wgUseRCPatrol, $wgUseNPPatrol, $wgRC2UDPInterwikiPrefix, $wgLocalInterwiki,
621 $wgCanonicalServer, $wgScript;
623 if( $this->mAttribs
['rc_type'] == RC_LOG
) {
624 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->mAttribs
['rc_log_type'] );
626 $titleObj =& $this->getTitle();
628 $title = $titleObj->getPrefixedText();
629 $title = self
::cleanupForIRC( $title );
631 if( $this->mAttribs
['rc_type'] == RC_LOG
) {
634 $url = $wgCanonicalServer . $wgScript;
635 if( $this->mAttribs
['rc_type'] == RC_NEW
) {
636 $query = '?oldid=' . $this->mAttribs
['rc_this_oldid'];
638 $query = '?diff=' . $this->mAttribs
['rc_this_oldid'] . '&oldid=' . $this->mAttribs
['rc_last_oldid'];
640 if ( $wgUseRCPatrol ||
( $this->mAttribs
['rc_type'] == RC_NEW
&& $wgUseNPPatrol ) ) {
641 $query .= '&rcid=' . $this->mAttribs
['rc_id'];
643 // HACK: We need this hook for WMF's secure server setup
644 wfRunHooks( 'IRCLineURL', array( &$url, &$query ) );
648 if( $this->mAttribs
['rc_old_len'] !== null && $this->mAttribs
['rc_new_len'] !== null ) {
649 $szdiff = $this->mAttribs
['rc_new_len'] - $this->mAttribs
['rc_old_len'];
651 $szdiff = "\002$szdiff\002";
652 } elseif($szdiff >= 0) {
653 $szdiff = '+' . $szdiff ;
655 $szdiff = '(' . $szdiff . ')' ;
660 $user = self
::cleanupForIRC( $this->mAttribs
['rc_user_text'] );
662 if ( $this->mAttribs
['rc_type'] == RC_LOG
) {
663 $targetText = $this->getTitle()->getPrefixedText();
664 $comment = self
::cleanupForIRC( str_replace( "[[$targetText]]", "[[\00302$targetText\00310]]", $this->mExtra
['actionComment'] ) );
665 $flag = $this->mAttribs
['rc_log_action'];
667 $comment = self
::cleanupForIRC( $this->mAttribs
['rc_comment'] );
669 if ( !$this->mAttribs
['rc_patrolled'] && ( $wgUseRCPatrol ||
$this->mAttribs
['rc_new'] && $wgUseNPPatrol ) ) {
672 $flag .= ( $this->mAttribs
['rc_new'] ?
"N" : "" ) . ( $this->mAttribs
['rc_minor'] ?
"M" : "" ) . ( $this->mAttribs
['rc_bot'] ?
"B" : "" );
675 if ( $wgRC2UDPInterwikiPrefix === true && $wgLocalInterwiki !== false ) {
676 $prefix = $wgLocalInterwiki;
677 } elseif ( $wgRC2UDPInterwikiPrefix ) {
678 $prefix = $wgRC2UDPInterwikiPrefix;
682 if ( $prefix !== false ) {
683 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
685 $titleString = "\00314[[\00307$title\00314]]";
688 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
689 # no colour (\003) switches back to the term default
690 $fullString = "$titleString\0034 $flag\00310 " .
691 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
697 * Returns the change size (HTML).
698 * The lengths can be given optionally.
700 public function getCharacterDifference( $old = 0, $new = 0 ) {
702 $old = $this->mAttribs
['rc_old_len'];
705 $new = $this->mAttribs
['rc_new_len'];
707 if( $old === null ||
$new === null ) {
710 return ChangesList
::showCharacterDifference( $old, $new );