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 ) {
53 $rc = new RecentChange
;
54 $rc->loadFromRow( $row );
58 public static function newFromCurRow( $row ) {
59 $rc = new RecentChange
;
60 $rc->loadFromCurRow( $row );
61 $rc->notificationtimestamp
= false;
62 $rc->numberofWatchingusers
= false;
67 * Obtain the recent change with a given rc_id value
69 * @param $rcid rc_id value to retrieve
70 * @return RecentChange
72 public static function newFromId( $rcid ) {
73 $dbr = wfGetDB( DB_SLAVE
);
74 $res = $dbr->select( 'recentchanges', '*', array( 'rc_id' => $rcid ), __METHOD__
);
75 if( $res && $dbr->numRows( $res ) > 0 ) {
76 $row = $dbr->fetchObject( $res );
77 $dbr->freeResult( $res );
78 return self
::newFromRow( $row );
85 * Find the first recent change matching some specific conditions
87 * @param array $conds Array of conditions
88 * @param mixed $fname Override the method name in profiling/logs
89 * @return RecentChange
91 public static function newFromConds( $conds, $fname = false ) {
92 if( $fname === false )
94 $dbr = wfGetDB( DB_SLAVE
);
101 if( $res instanceof ResultWrapper
&& $res->numRows() > 0 ) {
102 $row = $res->fetchObject();
104 return self
::newFromRow( $row );
111 public function setAttribs( $attribs ) {
112 $this->mAttribs
= $attribs;
115 public function setExtra( $extra ) {
116 $this->mExtra
= $extra;
119 public function &getTitle() {
120 if( $this->mTitle
=== false ) {
121 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
122 # Make sure the correct page ID is process cached
123 $this->mTitle
->resetArticleID( $this->mAttribs
['rc_cur_id'] );
125 return $this->mTitle
;
128 public function getMovedToTitle() {
129 if( $this->mMovedToTitle
=== false ) {
130 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
131 $this->mAttribs
['rc_moved_to_title'] );
133 return $this->mMovedToTitle
;
136 # Writes the data in this object to the database
137 public function save() {
138 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPOmitBots;
139 $fname = 'RecentChange::save';
141 $dbw = wfGetDB( DB_MASTER
);
142 if( !is_array($this->mExtra
) ) {
143 $this->mExtra
= array();
145 $this->mExtra
['lang'] = $wgLocalInterwiki;
147 if( !$wgPutIPinRC ) {
148 $this->mAttribs
['rc_ip'] = '';
151 # If our database is strict about IP addresses, use NULL instead of an empty string
152 if( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
153 unset( $this->mAttribs
['rc_ip'] );
156 # Fixup database timestamps
157 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
158 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
159 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
161 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
162 if( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
163 unset( $this->mAttribs
['rc_cur_id'] );
167 $dbw->insert( 'recentchanges', $this->mAttribs
, $fname );
170 $this->mAttribs
['rc_id'] = $dbw->insertId();
173 wfRunHooks( 'RecentChange_save', array( &$this ) );
175 # Notify external application via UDP
176 if( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
177 self
::sendToUDP( $this->getIRCLine() );
180 # E-mail notifications
181 global $wgUseEnotif, $wgShowUpdatedMarker, $wgUser;
182 if( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
184 if( $this->mAttribs
['rc_user'] ) {
185 $editor = ($wgUser->getId() == $this->mAttribs
['rc_user']) ?
186 $wgUser : User
::newFromID( $this->mAttribs
['rc_user'] );
189 $editor = ($wgUser->getName() == $this->mAttribs
['rc_user_text']) ?
190 $wgUser : User
::newFromName( $this->mAttribs
['rc_user_text'], false );
192 # FIXME: this would be better as an extension hook
193 $enotif = new EmailNotification();
194 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
195 $enotif->notifyOnPageChange( $editor, $title,
196 $this->mAttribs
['rc_timestamp'],
197 $this->mAttribs
['rc_comment'],
198 $this->mAttribs
['rc_minor'],
199 $this->mAttribs
['rc_last_oldid'] );
203 public function notifyRC2UDP() {
204 global $wgRC2UDPAddress, $wgRC2UDPOmitBots;
205 # Notify external application via UDP
206 if( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
207 self
::sendToUDP( $this->getIRCLine() );
212 * Send some text to UDP
213 * @param string $line
214 * @param string $prefix
215 * @param string $address
216 * @return bool success
218 public static function sendToUDP( $line, $address = '', $prefix = '' ) {
219 global $wgRC2UDPAddress, $wgRC2UDPPrefix, $wgRC2UDPPort;
220 # Assume default for standard RC case
221 $address = $address ?
$address : $wgRC2UDPAddress;
222 $prefix = $prefix ?
$prefix : $wgRC2UDPPrefix;
223 # Notify external application via UDP
225 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
227 $line = $prefix . $line;
228 wfDebug( __METHOD__
. ": sending UDP line: $line\n" );
229 socket_sendto( $conn, $line, strlen($line), 0, $address, $wgRC2UDPPort );
230 socket_close( $conn );
233 wfDebug( __METHOD__
. ": failed to create UDP socket\n" );
240 * Remove newlines, carriage returns and decode html entites
241 * @param string $line
244 public static function cleanupForIRC( $text ) {
245 return Sanitizer
::decodeCharReferences( str_replace( array( "\n", "\r" ), array( "", "" ), $text ) );
249 * Mark a given change as patrolled
251 * @param mixed $change RecentChange or corresponding rc_id
252 * @param bool $auto for automatic patrol
253 * @return See doMarkPatrolled(), or null if $change is not an existing rc_id
255 public static function markPatrolled( $change, $auto = false ) {
256 $change = $change instanceof RecentChange
258 : RecentChange
::newFromId($change);
259 if( !$change instanceof RecentChange
) {
262 return $change->doMarkPatrolled( $auto );
266 * Mark this RecentChange as patrolled
268 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
269 * @param bool $auto for automatic patrol
270 * @return array of permissions errors, see Title::getUserPermissionsErrors()
272 public function doMarkPatrolled( $auto = false ) {
273 global $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
275 // If recentchanges patrol is disabled, only new pages
277 if( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute('rc_type') != RC_NEW
) ) {
278 $errors[] = array('rcpatroldisabled');
280 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
281 $right = $auto ?
'autopatrol' : 'patrol';
282 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $wgUser ) );
283 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$wgUser, false)) ) {
284 $errors[] = array('hookaborted');
286 // Users without the 'autopatrol' right can't patrol their
288 if( $wgUser->getName() == $this->getAttribute('rc_user_text') && !$wgUser->isAllowed('autopatrol') ) {
289 $errors[] = array('markedaspatrollederror-noautopatrol');
294 // If the change was patrolled already, do nothing
295 if( $this->getAttribute('rc_patrolled') ) {
298 // Actually set the 'patrolled' flag in RC
299 $this->reallyMarkPatrolled();
300 // Log this patrol event
301 PatrolLog
::record( $this, $auto );
302 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$wgUser, false) );
307 * Mark this RecentChange patrolled, without error checking
308 * @return int Number of affected rows
310 public function reallyMarkPatrolled() {
311 $dbw = wfGetDB( DB_MASTER
);
318 'rc_id' => $this->getAttribute('rc_id')
322 return $dbw->affectedRows();
325 # Makes an entry in the database corresponding to an edit
326 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
327 $lastTimestamp, $bot, $ip='', $oldSize=0, $newSize=0, $newId=0, $patrol=0 )
334 $rc = new RecentChange
;
335 $rc->mAttribs
= array(
336 'rc_timestamp' => $timestamp,
337 'rc_cur_time' => $timestamp,
338 'rc_namespace' => $title->getNamespace(),
339 'rc_title' => $title->getDBkey(),
340 'rc_type' => RC_EDIT
,
341 'rc_minor' => $minor ?
1 : 0,
342 'rc_cur_id' => $title->getArticleID(),
343 'rc_user' => $user->getId(),
344 'rc_user_text' => $user->getName(),
345 'rc_comment' => $comment,
346 'rc_this_oldid' => $newId,
347 'rc_last_oldid' => $oldId,
348 'rc_bot' => $bot ?
1 : 0,
349 'rc_moved_to_ns' => 0,
350 'rc_moved_to_title' => '',
352 'rc_patrolled' => intval($patrol),
353 'rc_new' => 0, # obsolete
354 'rc_old_len' => $oldSize,
355 'rc_new_len' => $newSize,
358 'rc_log_type' => null,
359 'rc_log_action' => '',
364 'prefixedDBkey' => $title->getPrefixedDBkey(),
365 'lastTimestamp' => $lastTimestamp,
366 'oldSize' => $oldSize,
367 'newSize' => $newSize,
374 * Makes an entry in the database corresponding to page creation
375 * Note: the title object must be loaded with the new id using resetArticleID()
376 * @todo Document parameters and return
378 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
379 $ip='', $size=0, $newId=0, $patrol=0 )
386 $rc = new RecentChange
;
387 $rc->mAttribs
= array(
388 'rc_timestamp' => $timestamp,
389 'rc_cur_time' => $timestamp,
390 'rc_namespace' => $title->getNamespace(),
391 'rc_title' => $title->getDBkey(),
393 'rc_minor' => $minor ?
1 : 0,
394 'rc_cur_id' => $title->getArticleID(),
395 'rc_user' => $user->getId(),
396 'rc_user_text' => $user->getName(),
397 'rc_comment' => $comment,
398 'rc_this_oldid' => $newId,
399 'rc_last_oldid' => 0,
400 'rc_bot' => $bot ?
1 : 0,
401 'rc_moved_to_ns' => 0,
402 'rc_moved_to_title' => '',
404 'rc_patrolled' => intval($patrol),
405 'rc_new' => 1, # obsolete
407 'rc_new_len' => $size,
410 'rc_log_type' => null,
411 'rc_log_action' => '',
416 'prefixedDBkey' => $title->getPrefixedDBkey(),
417 'lastTimestamp' => 0,
425 # Makes an entry in the database corresponding to a rename
426 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
434 $rc = new RecentChange
;
435 $rc->mAttribs
= array(
436 'rc_timestamp' => $timestamp,
437 'rc_cur_time' => $timestamp,
438 'rc_namespace' => $oldTitle->getNamespace(),
439 'rc_title' => $oldTitle->getDBkey(),
440 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
442 'rc_cur_id' => $oldTitle->getArticleID(),
443 'rc_user' => $user->getId(),
444 'rc_user_text' => $user->getName(),
445 'rc_comment' => $comment,
446 'rc_this_oldid' => 0,
447 'rc_last_oldid' => 0,
448 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
449 'rc_moved_to_ns' => $newTitle->getNamespace(),
450 'rc_moved_to_title' => $newTitle->getDBkey(),
452 'rc_new' => 0, # obsolete
454 'rc_old_len' => NULL,
455 'rc_new_len' => NULL,
457 'rc_logid' => 0, # notifyMove not used anymore
458 'rc_log_type' => null,
459 'rc_log_action' => '',
464 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
465 'lastTimestamp' => 0,
466 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
471 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
472 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
475 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
476 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
479 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='', $type,
480 $action, $target, $logComment, $params, $newId=0 )
482 global $wgLogRestrictions;
483 # Don't add private logs to RC!
484 if( isset($wgLogRestrictions[$type]) && $wgLogRestrictions[$type] != '*' ) {
487 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
488 $target, $logComment, $params, $newId );
493 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip='',
494 $type, $action, $target, $logComment, $params, $newId=0 )
502 $rc = new RecentChange
;
503 $rc->mAttribs
= array(
504 'rc_timestamp' => $timestamp,
505 'rc_cur_time' => $timestamp,
506 'rc_namespace' => $target->getNamespace(),
507 'rc_title' => $target->getDBkey(),
510 'rc_cur_id' => $target->getArticleID(),
511 'rc_user' => $user->getId(),
512 'rc_user_text' => $user->getName(),
513 'rc_comment' => $logComment,
514 'rc_this_oldid' => 0,
515 'rc_last_oldid' => 0,
516 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
517 'rc_moved_to_ns' => 0,
518 'rc_moved_to_title' => '',
521 'rc_new' => 0, # obsolete
522 'rc_old_len' => NULL,
523 'rc_new_len' => NULL,
525 'rc_logid' => $newId,
526 'rc_log_type' => $type,
527 'rc_log_action' => $action,
528 'rc_params' => $params
531 'prefixedDBkey' => $title->getPrefixedDBkey(),
532 'lastTimestamp' => 0,
533 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
538 # Initialises the members of this object from a mysql row object
539 public function loadFromRow( $row ) {
540 $this->mAttribs
= get_object_vars( $row );
541 $this->mAttribs
['rc_timestamp'] = wfTimestamp(TS_MW
, $this->mAttribs
['rc_timestamp']);
542 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
545 # Makes a pseudo-RC entry from a cur row
546 public function loadFromCurRow( $row ) {
547 $this->mAttribs
= array(
548 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
549 'rc_cur_time' => $row->rev_timestamp
,
550 'rc_user' => $row->rev_user
,
551 'rc_user_text' => $row->rev_user_text
,
552 'rc_namespace' => $row->page_namespace
,
553 'rc_title' => $row->page_title
,
554 'rc_comment' => $row->rev_comment
,
555 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
556 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
557 'rc_cur_id' => $row->page_id
,
558 'rc_this_oldid' => $row->rev_id
,
559 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
561 'rc_moved_to_ns' => 0,
562 'rc_moved_to_title' => '',
564 'rc_id' => $row->rc_id
,
565 'rc_patrolled' => $row->rc_patrolled
,
566 'rc_new' => $row->page_is_new
, # obsolete
567 'rc_old_len' => $row->rc_old_len
,
568 'rc_new_len' => $row->rc_new_len
,
569 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
570 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
571 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
572 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
573 'rc_deleted' => $row->rc_deleted
// MUST be set
578 * Get an attribute value
580 * @param $name Attribute name
583 public function getAttribute( $name ) {
584 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
588 * Gets the end part of the diff URL associated with this object
589 * Blank if no diff link should be displayed
591 public function diffLinkTrail( $forceCur ) {
592 if( $this->mAttribs
['rc_type'] == RC_EDIT
) {
593 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
594 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
596 $trail .= '&diff=0' ;
598 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
606 public function getIRCLine() {
607 global $wgUseRCPatrol, $wgUseNPPatrol, $wgRC2UDPInterwikiPrefix, $wgLocalInterwiki;
609 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
610 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
611 extract($this->mAttribs
);
612 extract($this->mExtra
);
614 if( $rc_type == RC_LOG
) {
615 $titleObj = Title
::newFromText( "Log/$rc_log_type", NS_SPECIAL
);
617 $titleObj =& $this->getTitle();
619 $title = $titleObj->getPrefixedText();
620 $title = self
::cleanupForIRC( $title );
622 if( $rc_type == RC_LOG
) {
625 if( $rc_type == RC_NEW
) {
626 $url = "oldid=$rc_this_oldid";
628 $url = "diff=$rc_this_oldid&oldid=$rc_last_oldid";
630 if( $wgUseRCPatrol ||
($rc_type == RC_NEW
&& $wgUseNPPatrol) ) {
631 $url .= "&rcid=$rc_id";
633 // XXX: *HACK* this should use getFullURL(), hacked for SSL madness --brion 2005-12-26
634 // XXX: *HACK^2* the preg_replace() undoes much of what getInternalURL() does, but we
635 // XXX: need to call it so that URL paths on the Wikimedia secure server can be fixed
636 // XXX: by a custom GetInternalURL hook --vyznev 2008-12-10
637 $url = preg_replace( '/title=[^&]*&/', '', $titleObj->getInternalURL( $url ) );
640 if( isset( $oldSize ) && isset( $newSize ) ) {
641 $szdiff = $newSize - $oldSize;
643 $szdiff = "\002$szdiff\002";
644 } elseif($szdiff >= 0) {
645 $szdiff = '+' . $szdiff ;
647 $szdiff = '(' . $szdiff . ')' ;
652 $user = self
::cleanupForIRC( $rc_user_text );
654 if( $rc_type == RC_LOG
) {
655 $targetText = $this->getTitle()->getPrefixedText();
656 $comment = self
::cleanupForIRC( str_replace("[[$targetText]]","[[\00302$targetText\00310]]",$actionComment) );
657 $flag = $rc_log_action;
659 $comment = self
::cleanupForIRC( $rc_comment );
661 if( !$rc_patrolled && ($wgUseRCPatrol ||
$rc_new && $wgUseNPPatrol) ) {
664 $flag .= ($rc_new ?
"N" : "") . ($rc_minor ?
"M" : "") . ($rc_bot ?
"B" : "");
667 if ( $wgRC2UDPInterwikiPrefix === true ) {
668 $prefix = $wgLocalInterwiki;
669 } elseif ( $wgRC2UDPInterwikiPrefix ) {
670 $prefix = $wgRC2UDPInterwikiPrefix;
674 if ( $prefix !== false ) {
675 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
677 $titleString = "\00314[[\00307$title\00314]]";
680 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
681 # no colour (\003) switches back to the term default
682 $fullString = "$titleString\0034 $flag\00310 " .
683 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
689 * Returns the change size (HTML).
690 * The lengths can be given optionally.
692 public function getCharacterDifference( $old = 0, $new = 0 ) {
694 $old = $this->mAttribs
['rc_old_len'];
697 $new = $this->mAttribs
['rc_new_len'];
699 if( $old === NULL ||
$new === NULL ) {
702 return ChangesList
::showCharacterDifference( $old, $new );