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;
118 public function &getTitle() {
119 if( $this->mTitle
=== false ) {
120 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
121 # Make sure the correct page ID is process cached
122 $this->mTitle
->resetArticleID( $this->mAttribs
['rc_cur_id'] );
124 return $this->mTitle
;
127 public function getMovedToTitle() {
128 if( $this->mMovedToTitle
=== false ) {
129 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
130 $this->mAttribs
['rc_moved_to_title'] );
132 return $this->mMovedToTitle
;
135 # Writes the data in this object to the database
136 public function save() {
137 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPOmitBots;
138 $fname = 'RecentChange::save';
140 $dbw = wfGetDB( DB_MASTER
);
141 if( !is_array($this->mExtra
) ) {
142 $this->mExtra
= array();
144 $this->mExtra
['lang'] = $wgLocalInterwiki;
146 if( !$wgPutIPinRC ) {
147 $this->mAttribs
['rc_ip'] = '';
150 # If our database is strict about IP addresses, use NULL instead of an empty string
151 if( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
152 unset( $this->mAttribs
['rc_ip'] );
155 # Fixup database timestamps
156 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
157 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
158 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
160 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
161 if( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
162 unset( $this->mAttribs
['rc_cur_id'] );
166 $dbw->insert( 'recentchanges', $this->mAttribs
, $fname );
169 $this->mAttribs
['rc_id'] = $dbw->insertId();
172 wfRunHooks( 'RecentChange_save', array( &$this ) );
174 # Notify external application via UDP
175 if( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
176 self
::sendToUDP( $this->getIRCLine() );
179 # E-mail notifications
180 global $wgUseEnotif, $wgShowUpdatedMarker, $wgUser;
181 if( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
183 if( $this->mAttribs
['rc_user'] ) {
184 $editor = ($wgUser->getId() == $this->mAttribs
['rc_user']) ?
185 $wgUser : User
::newFromID( $this->mAttribs
['rc_user'] );
188 $editor = ($wgUser->getName() == $this->mAttribs
['rc_user_text']) ?
189 $wgUser : User
::newFromName( $this->mAttribs
['rc_user_text'], false );
191 # FIXME: this would be better as an extension hook
192 $enotif = new EmailNotification();
193 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
194 $enotif->notifyOnPageChange( $editor, $title,
195 $this->mAttribs
['rc_timestamp'],
196 $this->mAttribs
['rc_comment'],
197 $this->mAttribs
['rc_minor'],
198 $this->mAttribs
['rc_last_oldid'] );
202 public function notifyRC2UDP() {
203 global $wgRC2UDPAddress, $wgRC2UDPOmitBots;
204 # Notify external application via UDP
205 if( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
206 self
::sendToUDP( $this->getIRCLine() );
211 * Send some text to UDP.
212 * @see RecentChange::cleanupForIRC
213 * @param $line String: text to send
214 * @param $address String: defaults to $wgRC2UDPAddress.
215 * @param $prefix String: defaults to $wgRC2UDPPrefix.
216 * @param $port Int: defaults to $wgRC2UDPPort. (Since 1.17)
217 * @return Boolean: success
219 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) {
220 global $wgRC2UDPAddress, $wgRC2UDPPrefix, $wgRC2UDPPort;
221 # Assume default for standard RC case
222 $address = $address ?
$address : $wgRC2UDPAddress;
223 $prefix = $prefix ?
$prefix : $wgRC2UDPPrefix;
224 $port = $port ?
$port : $wgRC2UDPPort;
225 # Notify external application via UDP
227 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
229 $line = $prefix . $line;
230 wfDebug( __METHOD__
. ": sending UDP line: $line\n" );
231 socket_sendto( $conn, $line, strlen($line), 0, $address, $port );
232 socket_close( $conn );
235 wfDebug( __METHOD__
. ": failed to create UDP socket\n" );
242 * Remove newlines, carriage returns and decode html entites
243 * @param $text String
246 public static function cleanupForIRC( $text ) {
247 return Sanitizer
::decodeCharReferences( str_replace( array( "\n", "\r" ), array( "", "" ), $text ) );
251 * Mark a given change as patrolled
253 * @param $change Mixed: RecentChange or corresponding rc_id
254 * @param $auto Boolean: for automatic patrol
255 * @return See doMarkPatrolled(), or null if $change is not an existing rc_id
257 public static function markPatrolled( $change, $auto = false ) {
258 $change = $change instanceof RecentChange
260 : RecentChange
::newFromId($change);
261 if( !$change instanceof RecentChange
) {
264 return $change->doMarkPatrolled( $auto );
268 * Mark this RecentChange as patrolled
270 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
271 * @param $auto Boolean: for automatic patrol
272 * @return array of permissions errors, see Title::getUserPermissionsErrors()
274 public function doMarkPatrolled( $auto = false ) {
275 global $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
277 // If recentchanges patrol is disabled, only new pages
279 if( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute('rc_type') != RC_NEW
) ) {
280 $errors[] = array('rcpatroldisabled');
282 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
283 $right = $auto ?
'autopatrol' : 'patrol';
284 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $wgUser ) );
285 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$wgUser, false)) ) {
286 $errors[] = array('hookaborted');
288 // Users without the 'autopatrol' right can't patrol their
290 if( $wgUser->getName() == $this->getAttribute('rc_user_text') && !$wgUser->isAllowed('autopatrol') ) {
291 $errors[] = array('markedaspatrollederror-noautopatrol');
296 // If the change was patrolled already, do nothing
297 if( $this->getAttribute('rc_patrolled') ) {
300 // Actually set the 'patrolled' flag in RC
301 $this->reallyMarkPatrolled();
302 // Log this patrol event
303 PatrolLog
::record( $this, $auto );
304 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$wgUser, false) );
309 * Mark this RecentChange patrolled, without error checking
310 * @return Integer: number of affected rows
312 public function reallyMarkPatrolled() {
313 $dbw = wfGetDB( DB_MASTER
);
320 'rc_id' => $this->getAttribute('rc_id')
324 return $dbw->affectedRows();
327 # Makes an entry in the database corresponding to an edit
328 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
329 $lastTimestamp, $bot, $ip='', $oldSize=0, $newSize=0, $newId=0, $patrol=0 )
336 $rc = new RecentChange
;
337 $rc->mAttribs
= array(
338 'rc_timestamp' => $timestamp,
339 'rc_cur_time' => $timestamp,
340 'rc_namespace' => $title->getNamespace(),
341 'rc_title' => $title->getDBkey(),
342 'rc_type' => RC_EDIT
,
343 'rc_minor' => $minor ?
1 : 0,
344 'rc_cur_id' => $title->getArticleID(),
345 'rc_user' => $user->getId(),
346 'rc_user_text' => $user->getName(),
347 'rc_comment' => $comment,
348 'rc_this_oldid' => $newId,
349 'rc_last_oldid' => $oldId,
350 'rc_bot' => $bot ?
1 : 0,
351 'rc_moved_to_ns' => 0,
352 'rc_moved_to_title' => '',
354 'rc_patrolled' => intval($patrol),
355 'rc_new' => 0, # obsolete
356 'rc_old_len' => $oldSize,
357 'rc_new_len' => $newSize,
360 'rc_log_type' => null,
361 'rc_log_action' => '',
366 'prefixedDBkey' => $title->getPrefixedDBkey(),
367 'lastTimestamp' => $lastTimestamp,
368 'oldSize' => $oldSize,
369 'newSize' => $newSize,
376 * Makes an entry in the database corresponding to page creation
377 * Note: the title object must be loaded with the new id using resetArticleID()
378 * @todo Document parameters and return
380 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
381 $ip='', $size=0, $newId=0, $patrol=0 )
388 $rc = new RecentChange
;
389 $rc->mAttribs
= array(
390 'rc_timestamp' => $timestamp,
391 'rc_cur_time' => $timestamp,
392 'rc_namespace' => $title->getNamespace(),
393 'rc_title' => $title->getDBkey(),
395 'rc_minor' => $minor ?
1 : 0,
396 'rc_cur_id' => $title->getArticleID(),
397 'rc_user' => $user->getId(),
398 'rc_user_text' => $user->getName(),
399 'rc_comment' => $comment,
400 'rc_this_oldid' => $newId,
401 'rc_last_oldid' => 0,
402 'rc_bot' => $bot ?
1 : 0,
403 'rc_moved_to_ns' => 0,
404 'rc_moved_to_title' => '',
406 'rc_patrolled' => intval($patrol),
407 'rc_new' => 1, # obsolete
409 'rc_new_len' => $size,
412 'rc_log_type' => null,
413 'rc_log_action' => '',
418 'prefixedDBkey' => $title->getPrefixedDBkey(),
419 'lastTimestamp' => 0,
427 # Makes an entry in the database corresponding to a rename
428 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
436 $rc = new RecentChange
;
437 $rc->mAttribs
= array(
438 'rc_timestamp' => $timestamp,
439 'rc_cur_time' => $timestamp,
440 'rc_namespace' => $oldTitle->getNamespace(),
441 'rc_title' => $oldTitle->getDBkey(),
442 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
444 'rc_cur_id' => $oldTitle->getArticleID(),
445 'rc_user' => $user->getId(),
446 'rc_user_text' => $user->getName(),
447 'rc_comment' => $comment,
448 'rc_this_oldid' => 0,
449 'rc_last_oldid' => 0,
450 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
451 'rc_moved_to_ns' => $newTitle->getNamespace(),
452 'rc_moved_to_title' => $newTitle->getDBkey(),
454 'rc_new' => 0, # obsolete
456 'rc_old_len' => null,
457 'rc_new_len' => null,
459 'rc_logid' => 0, # notifyMove not used anymore
460 'rc_log_type' => null,
461 'rc_log_action' => '',
466 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
467 'lastTimestamp' => 0,
468 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
473 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
474 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
477 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
478 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
481 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='', $type,
482 $action, $target, $logComment, $params, $newId=0 )
484 global $wgLogRestrictions;
485 # Don't add private logs to RC!
486 if( isset($wgLogRestrictions[$type]) && $wgLogRestrictions[$type] != '*' ) {
489 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
490 $target, $logComment, $params, $newId );
495 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip='',
496 $type, $action, $target, $logComment, $params, $newId=0 )
504 $rc = new RecentChange
;
505 $rc->mAttribs
= array(
506 'rc_timestamp' => $timestamp,
507 'rc_cur_time' => $timestamp,
508 'rc_namespace' => $target->getNamespace(),
509 'rc_title' => $target->getDBkey(),
512 'rc_cur_id' => $target->getArticleID(),
513 'rc_user' => $user->getId(),
514 'rc_user_text' => $user->getName(),
515 'rc_comment' => $logComment,
516 'rc_this_oldid' => 0,
517 'rc_last_oldid' => 0,
518 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
519 'rc_moved_to_ns' => 0,
520 'rc_moved_to_title' => '',
523 'rc_new' => 0, # obsolete
524 'rc_old_len' => null,
525 'rc_new_len' => null,
527 'rc_logid' => $newId,
528 'rc_log_type' => $type,
529 'rc_log_action' => $action,
530 'rc_params' => $params
533 'prefixedDBkey' => $title->getPrefixedDBkey(),
534 'lastTimestamp' => 0,
535 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
540 # Initialises the members of this object from a mysql row object
541 public function loadFromRow( $row ) {
542 $this->mAttribs
= get_object_vars( $row );
543 $this->mAttribs
['rc_timestamp'] = wfTimestamp(TS_MW
, $this->mAttribs
['rc_timestamp']);
544 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
547 # Makes a pseudo-RC entry from a cur row
548 public function loadFromCurRow( $row ) {
549 $this->mAttribs
= array(
550 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
551 'rc_cur_time' => $row->rev_timestamp
,
552 'rc_user' => $row->rev_user
,
553 'rc_user_text' => $row->rev_user_text
,
554 'rc_namespace' => $row->page_namespace
,
555 'rc_title' => $row->page_title
,
556 'rc_comment' => $row->rev_comment
,
557 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
558 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
559 'rc_cur_id' => $row->page_id
,
560 'rc_this_oldid' => $row->rev_id
,
561 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
563 'rc_moved_to_ns' => 0,
564 'rc_moved_to_title' => '',
566 'rc_id' => $row->rc_id
,
567 'rc_patrolled' => $row->rc_patrolled
,
568 'rc_new' => $row->page_is_new
, # obsolete
569 'rc_old_len' => $row->rc_old_len
,
570 'rc_new_len' => $row->rc_new_len
,
571 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
572 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
573 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
574 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
575 'rc_deleted' => $row->rc_deleted
// MUST be set
580 * Get an attribute value
582 * @param $name Attribute name
585 public function getAttribute( $name ) {
586 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : null;
589 public function getAttributes() {
590 return $this->mAttribs
;
594 * Gets the end part of the diff URL associated with this object
595 * Blank if no diff link should be displayed
597 public function diffLinkTrail( $forceCur ) {
598 if( $this->mAttribs
['rc_type'] == RC_EDIT
) {
599 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
600 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
602 $trail .= '&diff=0' ;
604 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
612 public function getIRCLine() {
613 global $wgUseRCPatrol, $wgUseNPPatrol, $wgRC2UDPInterwikiPrefix, $wgLocalInterwiki;
615 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
616 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
617 extract($this->mAttribs
);
618 extract($this->mExtra
);
620 if( $rc_type == RC_LOG
) {
621 $titleObj = Title
::newFromText( "Log/$rc_log_type", NS_SPECIAL
);
623 $titleObj =& $this->getTitle();
625 $title = $titleObj->getPrefixedText();
626 $title = self
::cleanupForIRC( $title );
628 if( $rc_type == RC_LOG
) {
631 if( $rc_type == RC_NEW
) {
632 $url = "oldid=$rc_this_oldid";
634 $url = "diff=$rc_this_oldid&oldid=$rc_last_oldid";
636 if( $wgUseRCPatrol ||
($rc_type == RC_NEW
&& $wgUseNPPatrol) ) {
637 $url .= "&rcid=$rc_id";
639 // XXX: *HACK* this should use getFullURL(), hacked for SSL madness --brion 2005-12-26
640 // XXX: *HACK^2* the preg_replace() undoes much of what getInternalURL() does, but we
641 // XXX: need to call it so that URL paths on the Wikimedia secure server can be fixed
642 // XXX: by a custom GetInternalURL hook --vyznev 2008-12-10
643 $url = preg_replace( '/title=[^&]*&/', '', $titleObj->getInternalURL( $url ) );
646 if( isset( $oldSize ) && isset( $newSize ) ) {
647 $szdiff = $newSize - $oldSize;
649 $szdiff = "\002$szdiff\002";
650 } elseif($szdiff >= 0) {
651 $szdiff = '+' . $szdiff ;
653 $szdiff = '(' . $szdiff . ')' ;
658 $user = self
::cleanupForIRC( $rc_user_text );
660 if( $rc_type == RC_LOG
) {
661 $targetText = $this->getTitle()->getPrefixedText();
662 $comment = self
::cleanupForIRC( str_replace("[[$targetText]]","[[\00302$targetText\00310]]",$actionComment) );
663 $flag = $rc_log_action;
665 $comment = self
::cleanupForIRC( $rc_comment );
667 if( !$rc_patrolled && ($wgUseRCPatrol ||
$rc_new && $wgUseNPPatrol) ) {
670 $flag .= ($rc_new ?
"N" : "") . ($rc_minor ?
"M" : "") . ($rc_bot ?
"B" : "");
673 if ( $wgRC2UDPInterwikiPrefix === true ) {
674 $prefix = $wgLocalInterwiki;
675 } elseif ( $wgRC2UDPInterwikiPrefix ) {
676 $prefix = $wgRC2UDPInterwikiPrefix;
680 if ( $prefix !== false ) {
681 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
683 $titleString = "\00314[[\00307$title\00314]]";
686 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
687 # no colour (\003) switches back to the term default
688 $fullString = "$titleString\0034 $flag\00310 " .
689 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
695 * Returns the change size (HTML).
696 * The lengths can be given optionally.
698 public function getCharacterDifference( $old = 0, $new = 0 ) {
700 $old = $this->mAttribs
['rc_old_len'];
703 $new = $this->mAttribs
['rc_new_len'];
705 if( $old === null ||
$new === null ) {
708 return ChangesList
::showCharacterDifference( $old, $new );