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();
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 return self
::newFromRow( $row );
84 * Find the first recent change matching some specific conditions
86 * @param $conds Array of conditions
87 * @param $fname Mixed: override the method name in profiling/logs
88 * @return RecentChange
90 public static function newFromConds( $conds, $fname = false ) {
91 if( $fname === false )
93 $dbr = wfGetDB( DB_SLAVE
);
100 if( $res instanceof ResultWrapper
&& $res->numRows() > 0 ) {
101 $row = $res->fetchObject();
103 return self
::newFromRow( $row );
110 public function setAttribs( $attribs ) {
111 $this->mAttribs
= $attribs;
114 public function setExtra( $extra ) {
115 $this->mExtra
= $extra;
122 public function &getTitle() {
123 if( $this->mTitle
=== false ) {
124 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
125 # Make sure the correct page ID is process cached
126 $this->mTitle
->resetArticleID( $this->mAttribs
['rc_cur_id'] );
128 return $this->mTitle
;
131 public function getMovedToTitle() {
132 if( $this->mMovedToTitle
=== false ) {
133 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
134 $this->mAttribs
['rc_moved_to_title'] );
136 return $this->mMovedToTitle
;
139 # Writes the data in this object to the database
140 public function save() {
141 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPOmitBots;
142 $fname = 'RecentChange::save';
144 $dbw = wfGetDB( DB_MASTER
);
145 if( !is_array($this->mExtra
) ) {
146 $this->mExtra
= array();
148 $this->mExtra
['lang'] = $wgLocalInterwiki;
150 if( !$wgPutIPinRC ) {
151 $this->mAttribs
['rc_ip'] = '';
154 # If our database is strict about IP addresses, use NULL instead of an empty string
155 if( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
156 unset( $this->mAttribs
['rc_ip'] );
159 # Fixup database timestamps
160 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
161 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
162 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
164 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
165 if( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
166 unset( $this->mAttribs
['rc_cur_id'] );
170 $dbw->insert( 'recentchanges', $this->mAttribs
, $fname );
173 $this->mAttribs
['rc_id'] = $dbw->insertId();
176 wfRunHooks( 'RecentChange_save', array( &$this ) );
178 # Notify external application via UDP
179 if( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
180 self
::sendToUDP( $this->getIRCLine() );
183 # E-mail notifications
184 global $wgUseEnotif, $wgShowUpdatedMarker, $wgUser;
185 if( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
187 if( $this->mAttribs
['rc_user'] ) {
188 $editor = ($wgUser->getId() == $this->mAttribs
['rc_user']) ?
189 $wgUser : User
::newFromID( $this->mAttribs
['rc_user'] );
192 $editor = ($wgUser->getName() == $this->mAttribs
['rc_user_text']) ?
193 $wgUser : User
::newFromName( $this->mAttribs
['rc_user_text'], false );
195 # FIXME: this would be better as an extension hook
196 $enotif = new EmailNotification();
197 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
198 $enotif->notifyOnPageChange( $editor, $title,
199 $this->mAttribs
['rc_timestamp'],
200 $this->mAttribs
['rc_comment'],
201 $this->mAttribs
['rc_minor'],
202 $this->mAttribs
['rc_last_oldid'] );
206 public function notifyRC2UDP() {
207 global $wgRC2UDPAddress, $wgRC2UDPOmitBots;
208 # Notify external application via UDP
209 if( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
210 self
::sendToUDP( $this->getIRCLine() );
215 * Send some text to UDP.
216 * @see RecentChange::cleanupForIRC
217 * @param $line String: text to send
218 * @param $address String: defaults to $wgRC2UDPAddress.
219 * @param $prefix String: defaults to $wgRC2UDPPrefix.
220 * @param $port Int: defaults to $wgRC2UDPPort. (Since 1.17)
221 * @return Boolean: success
223 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) {
224 global $wgRC2UDPAddress, $wgRC2UDPPrefix, $wgRC2UDPPort;
225 # Assume default for standard RC case
226 $address = $address ?
$address : $wgRC2UDPAddress;
227 $prefix = $prefix ?
$prefix : $wgRC2UDPPrefix;
228 $port = $port ?
$port : $wgRC2UDPPort;
229 # Notify external application via UDP
231 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
233 $line = $prefix . $line;
234 wfDebug( __METHOD__
. ": sending UDP line: $line\n" );
235 socket_sendto( $conn, $line, strlen($line), 0, $address, $port );
236 socket_close( $conn );
239 wfDebug( __METHOD__
. ": failed to create UDP socket\n" );
246 * Remove newlines, carriage returns and decode html entites
247 * @param $text String
250 public static function cleanupForIRC( $text ) {
251 return Sanitizer
::decodeCharReferences( str_replace( array( "\n", "\r" ), array( "", "" ), $text ) );
255 * Mark a given change as patrolled
257 * @param $change Mixed: RecentChange or corresponding rc_id
258 * @param $auto Boolean: for automatic patrol
259 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
261 public static function markPatrolled( $change, $auto = false ) {
262 $change = $change instanceof RecentChange
264 : RecentChange
::newFromId($change);
265 if( !$change instanceof RecentChange
) {
268 return $change->doMarkPatrolled( $auto );
272 * Mark this RecentChange as patrolled
274 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
275 * @param $auto Boolean: for automatic patrol
276 * @return array of permissions errors, see Title::getUserPermissionsErrors()
278 public function doMarkPatrolled( $auto = false ) {
279 global $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
281 // If recentchanges patrol is disabled, only new pages
283 if( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute('rc_type') != RC_NEW
) ) {
284 $errors[] = array('rcpatroldisabled');
286 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
287 $right = $auto ?
'autopatrol' : 'patrol';
288 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $wgUser ) );
289 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$wgUser, false)) ) {
290 $errors[] = array('hookaborted');
292 // Users without the 'autopatrol' right can't patrol their
294 if( $wgUser->getName() == $this->getAttribute('rc_user_text') && !$wgUser->isAllowed('autopatrol') ) {
295 $errors[] = array('markedaspatrollederror-noautopatrol');
300 // If the change was patrolled already, do nothing
301 if( $this->getAttribute('rc_patrolled') ) {
304 // Actually set the 'patrolled' flag in RC
305 $this->reallyMarkPatrolled();
306 // Log this patrol event
307 PatrolLog
::record( $this, $auto );
308 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$wgUser, false) );
313 * Mark this RecentChange patrolled, without error checking
314 * @return Integer: number of affected rows
316 public function reallyMarkPatrolled() {
317 $dbw = wfGetDB( DB_MASTER
);
324 'rc_id' => $this->getAttribute('rc_id')
328 return $dbw->affectedRows();
331 # Makes an entry in the database corresponding to an edit
332 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
333 $lastTimestamp, $bot, $ip='', $oldSize=0, $newSize=0, $newId=0, $patrol=0 )
340 $rc = new RecentChange
;
341 $rc->mAttribs
= array(
342 'rc_timestamp' => $timestamp,
343 'rc_cur_time' => $timestamp,
344 'rc_namespace' => $title->getNamespace(),
345 'rc_title' => $title->getDBkey(),
346 'rc_type' => RC_EDIT
,
347 'rc_minor' => $minor ?
1 : 0,
348 'rc_cur_id' => $title->getArticleID(),
349 'rc_user' => $user->getId(),
350 'rc_user_text' => $user->getName(),
351 'rc_comment' => $comment,
352 'rc_this_oldid' => $newId,
353 'rc_last_oldid' => $oldId,
354 'rc_bot' => $bot ?
1 : 0,
355 'rc_moved_to_ns' => 0,
356 'rc_moved_to_title' => '',
358 'rc_patrolled' => intval($patrol),
359 'rc_new' => 0, # obsolete
360 'rc_old_len' => $oldSize,
361 'rc_new_len' => $newSize,
364 'rc_log_type' => null,
365 'rc_log_action' => '',
370 'prefixedDBkey' => $title->getPrefixedDBkey(),
371 'lastTimestamp' => $lastTimestamp,
372 'oldSize' => $oldSize,
373 'newSize' => $newSize,
380 * Makes an entry in the database corresponding to page creation
381 * Note: the title object must be loaded with the new id using resetArticleID()
382 * @todo Document parameters and return
384 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
385 $ip='', $size=0, $newId=0, $patrol=0 )
392 $rc = new RecentChange
;
393 $rc->mAttribs
= array(
394 'rc_timestamp' => $timestamp,
395 'rc_cur_time' => $timestamp,
396 'rc_namespace' => $title->getNamespace(),
397 'rc_title' => $title->getDBkey(),
399 'rc_minor' => $minor ?
1 : 0,
400 'rc_cur_id' => $title->getArticleID(),
401 'rc_user' => $user->getId(),
402 'rc_user_text' => $user->getName(),
403 'rc_comment' => $comment,
404 'rc_this_oldid' => $newId,
405 'rc_last_oldid' => 0,
406 'rc_bot' => $bot ?
1 : 0,
407 'rc_moved_to_ns' => 0,
408 'rc_moved_to_title' => '',
410 'rc_patrolled' => intval($patrol),
411 'rc_new' => 1, # obsolete
413 'rc_new_len' => $size,
416 'rc_log_type' => null,
417 'rc_log_action' => '',
422 'prefixedDBkey' => $title->getPrefixedDBkey(),
423 'lastTimestamp' => 0,
431 # Makes an entry in the database corresponding to a rename
432 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
440 $rc = new RecentChange
;
441 $rc->mAttribs
= array(
442 'rc_timestamp' => $timestamp,
443 'rc_cur_time' => $timestamp,
444 'rc_namespace' => $oldTitle->getNamespace(),
445 'rc_title' => $oldTitle->getDBkey(),
446 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
448 'rc_cur_id' => $oldTitle->getArticleID(),
449 'rc_user' => $user->getId(),
450 'rc_user_text' => $user->getName(),
451 'rc_comment' => $comment,
452 'rc_this_oldid' => 0,
453 'rc_last_oldid' => 0,
454 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
455 'rc_moved_to_ns' => $newTitle->getNamespace(),
456 'rc_moved_to_title' => $newTitle->getDBkey(),
458 'rc_new' => 0, # obsolete
460 'rc_old_len' => null,
461 'rc_new_len' => null,
463 'rc_logid' => 0, # notifyMove not used anymore
464 'rc_log_type' => null,
465 'rc_log_action' => '',
470 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
471 'lastTimestamp' => 0,
472 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
477 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
478 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
481 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
482 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
485 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='', $type,
486 $action, $target, $logComment, $params, $newId=0 )
488 global $wgLogRestrictions;
489 # Don't add private logs to RC!
490 if( isset($wgLogRestrictions[$type]) && $wgLogRestrictions[$type] != '*' ) {
493 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
494 $target, $logComment, $params, $newId );
499 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip='',
500 $type, $action, $target, $logComment, $params, $newId=0 )
508 $rc = new RecentChange
;
509 $rc->mAttribs
= array(
510 'rc_timestamp' => $timestamp,
511 'rc_cur_time' => $timestamp,
512 'rc_namespace' => $target->getNamespace(),
513 'rc_title' => $target->getDBkey(),
516 'rc_cur_id' => $target->getArticleID(),
517 'rc_user' => $user->getId(),
518 'rc_user_text' => $user->getName(),
519 'rc_comment' => $logComment,
520 'rc_this_oldid' => 0,
521 'rc_last_oldid' => 0,
522 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
523 'rc_moved_to_ns' => 0,
524 'rc_moved_to_title' => '',
527 'rc_new' => 0, # obsolete
528 'rc_old_len' => null,
529 'rc_new_len' => null,
531 'rc_logid' => $newId,
532 'rc_log_type' => $type,
533 'rc_log_action' => $action,
534 'rc_params' => $params
537 'prefixedDBkey' => $title->getPrefixedDBkey(),
538 'lastTimestamp' => 0,
539 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
544 # Initialises the members of this object from a mysql row object
545 public function loadFromRow( $row ) {
546 $this->mAttribs
= get_object_vars( $row );
547 $this->mAttribs
['rc_timestamp'] = wfTimestamp(TS_MW
, $this->mAttribs
['rc_timestamp']);
548 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
551 # Makes a pseudo-RC entry from a cur row
552 public function loadFromCurRow( $row ) {
553 $this->mAttribs
= array(
554 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
555 'rc_cur_time' => $row->rev_timestamp
,
556 'rc_user' => $row->rev_user
,
557 'rc_user_text' => $row->rev_user_text
,
558 'rc_namespace' => $row->page_namespace
,
559 'rc_title' => $row->page_title
,
560 'rc_comment' => $row->rev_comment
,
561 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
562 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
563 'rc_cur_id' => $row->page_id
,
564 'rc_this_oldid' => $row->rev_id
,
565 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
567 'rc_moved_to_ns' => 0,
568 'rc_moved_to_title' => '',
570 'rc_id' => $row->rc_id
,
571 'rc_patrolled' => $row->rc_patrolled
,
572 'rc_new' => $row->page_is_new
, # obsolete
573 'rc_old_len' => $row->rc_old_len
,
574 'rc_new_len' => $row->rc_new_len
,
575 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
576 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
577 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
578 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
579 'rc_deleted' => $row->rc_deleted
// MUST be set
584 * Get an attribute value
586 * @param $name String Attribute name
589 public function getAttribute( $name ) {
590 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : null;
593 public function getAttributes() {
594 return $this->mAttribs
;
598 * Gets the end part of the diff URL associated with this object
599 * Blank if no diff link should be displayed
601 public function diffLinkTrail( $forceCur ) {
602 if( $this->mAttribs
['rc_type'] == RC_EDIT
) {
603 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
604 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
606 $trail .= '&diff=0' ;
608 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
616 public function getIRCLine() {
617 global $wgUseRCPatrol, $wgUseNPPatrol, $wgRC2UDPInterwikiPrefix, $wgLocalInterwiki;
619 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
620 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
621 extract($this->mAttribs
);
622 extract($this->mExtra
);
624 if( $rc_type == RC_LOG
) {
625 $titleObj = Title
::newFromText( "Log/$rc_log_type", NS_SPECIAL
);
627 $titleObj =& $this->getTitle();
629 $title = $titleObj->getPrefixedText();
630 $title = self
::cleanupForIRC( $title );
632 if( $rc_type == RC_LOG
) {
635 if( $rc_type == RC_NEW
) {
636 $url = "oldid=$rc_this_oldid";
638 $url = "diff=$rc_this_oldid&oldid=$rc_last_oldid";
640 if( $wgUseRCPatrol ||
($rc_type == RC_NEW
&& $wgUseNPPatrol) ) {
641 $url .= "&rcid=$rc_id";
643 // XXX: *HACK* this should use getFullURL(), hacked for SSL madness --brion 2005-12-26
644 // XXX: *HACK^2* the preg_replace() undoes much of what getInternalURL() does, but we
645 // XXX: need to call it so that URL paths on the Wikimedia secure server can be fixed
646 // XXX: by a custom GetInternalURL hook --vyznev 2008-12-10
647 $url = preg_replace( '/title=[^&]*&/', '', $titleObj->getInternalURL( $url ) );
650 if( isset( $oldSize ) && isset( $newSize ) ) {
651 $szdiff = $newSize - $oldSize;
653 $szdiff = "\002$szdiff\002";
654 } elseif($szdiff >= 0) {
655 $szdiff = '+' . $szdiff ;
657 $szdiff = '(' . $szdiff . ')' ;
662 $user = self
::cleanupForIRC( $rc_user_text );
664 if( $rc_type == RC_LOG
) {
665 $targetText = $this->getTitle()->getPrefixedText();
666 $comment = self
::cleanupForIRC( str_replace("[[$targetText]]","[[\00302$targetText\00310]]",$actionComment) );
667 $flag = $rc_log_action;
669 $comment = self
::cleanupForIRC( $rc_comment );
671 if( !$rc_patrolled && ($wgUseRCPatrol ||
$rc_new && $wgUseNPPatrol) ) {
674 $flag .= ($rc_new ?
"N" : "") . ($rc_minor ?
"M" : "") . ($rc_bot ?
"B" : "");
677 if ( $wgRC2UDPInterwikiPrefix === true ) {
678 $prefix = $wgLocalInterwiki;
679 } elseif ( $wgRC2UDPInterwikiPrefix ) {
680 $prefix = $wgRC2UDPInterwikiPrefix;
684 if ( $prefix !== false ) {
685 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
687 $titleString = "\00314[[\00307$title\00314]]";
690 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
691 # no colour (\003) switches back to the term default
692 $fullString = "$titleString\0034 $flag\00310 " .
693 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
699 * Returns the change size (HTML).
700 * The lengths can be given optionally.
702 public function getCharacterDifference( $old = 0, $new = 0 ) {
704 $old = $this->mAttribs
['rc_old_len'];
707 $new = $this->mAttribs
['rc_new_len'];
709 if( $old === null ||
$new === null ) {
712 return ChangesList
::showCharacterDifference( $old, $new );