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'] );
123 return $this->mTitle
;
126 public function getMovedToTitle() {
127 if( $this->mMovedToTitle
=== false ) {
128 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
129 $this->mAttribs
['rc_moved_to_title'] );
131 return $this->mMovedToTitle
;
134 # Writes the data in this object to the database
135 public function save() {
136 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPOmitBots;
137 $fname = 'RecentChange::save';
139 $dbw = wfGetDB( DB_MASTER
);
140 if( !is_array($this->mExtra
) ) {
141 $this->mExtra
= array();
143 $this->mExtra
['lang'] = $wgLocalInterwiki;
145 if( !$wgPutIPinRC ) {
146 $this->mAttribs
['rc_ip'] = '';
149 # If our database is strict about IP addresses, use NULL instead of an empty string
150 if( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
151 unset( $this->mAttribs
['rc_ip'] );
154 # Fixup database timestamps
155 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
156 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
157 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
159 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
160 if( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
161 unset ( $this->mAttribs
['rc_cur_id'] );
165 $dbw->insert( 'recentchanges', $this->mAttribs
, $fname );
168 $this->mAttribs
['rc_id'] = $dbw->insertId();
170 # Notify external application via UDP
171 if( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
172 self
::sendToUDP( $this->getIRCLine() );
175 # E-mail notifications
176 global $wgUseEnotif, $wgShowUpdatedMarker, $wgUser;
177 if( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
179 if( $this->mAttribs
['rc_user'] ) {
180 $editor = ($wgUser->getId() == $this->mAttribs
['rc_user']) ?
181 $wgUser : User
::newFromID( $this->mAttribs
['rc_user'] );
184 $editor = ($wgUser->getName() == $this->mAttribs
['rc_user_text']) ?
185 $wgUser : User
::newFromName( $this->mAttribs
['rc_user_text'], false );
187 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
188 PageChangeNotification
::notifyOnPageChange( $editor, $title,
189 $this->mAttribs
['rc_timestamp'],
190 $this->mAttribs
['rc_comment'],
191 $this->mAttribs
['rc_minor'],
192 $this->mAttribs
['rc_last_oldid'] );
196 wfRunHooks( 'RecentChange_save', array( &$this ) );
200 * Send some text to UDP
201 * @param string $line
202 * @param string $prefix
203 * @param string $address
204 * @return bool success
206 public static function sendToUDP( $line, $address = '', $prefix = '' ) {
207 global $wgRC2UDPAddress, $wgRC2UDPPrefix, $wgRC2UDPPort;
208 # Assume default for standard RC case
209 $address = $address ?
$address : $wgRC2UDPAddress;
210 $prefix = $prefix ?
$prefix : $wgRC2UDPPrefix;
211 # Notify external application via UDP
213 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
215 $line = $prefix . $line;
216 wfDebug( __METHOD__
. ": sending UDP line: $line\n" );
217 socket_sendto( $conn, $line, strlen($line), 0, $address, $wgRC2UDPPort );
218 socket_close( $conn );
221 wfDebug( __METHOD__
. ": failed to create UDP socket\n" );
228 * Remove newlines and carriage returns
229 * @param string $line
232 public static function cleanupForIRC( $text ) {
233 return str_replace(array("\n", "\r"), array("", ""), $text);
237 * Mark a given change as patrolled
239 * @param mixed $change RecentChange or corresponding rc_id
240 * @param bool $auto for automatic patrol
241 * @return See doMarkPatrolled(), or null if $change is not an existing rc_id
243 public static function markPatrolled( $change, $auto = false ) {
244 $change = $change instanceof RecentChange
246 : RecentChange
::newFromId($change);
247 if( !$change instanceof RecentChange
) {
250 return $change->doMarkPatrolled( $auto );
254 * Mark this RecentChange as patrolled
256 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
257 * @param bool $auto for automatic patrol
258 * @return array of permissions errors, see Title::getUserPermissionsErrors()
260 public function doMarkPatrolled( $auto = false ) {
261 global $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
263 // If recentchanges patrol is disabled, only new pages
265 if( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute('rc_type') != RC_NEW
) ) {
266 $errors[] = array('rcpatroldisabled');
268 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
269 $right = $auto ?
'autopatrol' : 'patrol';
270 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $wgUser ) );
271 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$wgUser, false)) ) {
272 $errors[] = array('hookaborted');
274 // Users without the 'autopatrol' right can't patrol their
276 if( $wgUser->getName() == $this->getAttribute('rc_user_text') && !$wgUser->isAllowed('autopatrol') ) {
277 $errors[] = array('markedaspatrollederror-noautopatrol');
282 // If the change was patrolled already, do nothing
283 if( $this->getAttribute('rc_patrolled') ) {
286 // Actually set the 'patrolled' flag in RC
287 $this->reallyMarkPatrolled();
288 // Log this patrol event
289 PatrolLog
::record( $this, $auto );
290 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$wgUser, false) );
295 * Mark this RecentChange patrolled, without error checking
296 * @return int Number of affected rows
298 public function reallyMarkPatrolled() {
299 $dbw = wfGetDB( DB_MASTER
);
306 'rc_id' => $this->getAttribute('rc_id')
310 return $dbw->affectedRows();
313 # Makes an entry in the database corresponding to an edit
314 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
315 $lastTimestamp, $bot, $ip='', $oldSize=0, $newSize=0, $newId=0, $patrol=0 )
324 $rc = new RecentChange
;
325 $rc->mAttribs
= array(
326 'rc_timestamp' => $timestamp,
327 'rc_cur_time' => $timestamp,
328 'rc_namespace' => $title->getNamespace(),
329 'rc_title' => $title->getDBkey(),
330 'rc_type' => RC_EDIT
,
331 'rc_minor' => $minor ?
1 : 0,
332 'rc_cur_id' => $title->getArticleID(),
333 'rc_user' => $user->getId(),
334 'rc_user_text' => $user->getName(),
335 'rc_comment' => $comment,
336 'rc_this_oldid' => $newId,
337 'rc_last_oldid' => $oldId,
338 'rc_bot' => $bot ?
1 : 0,
339 'rc_moved_to_ns' => 0,
340 'rc_moved_to_title' => '',
342 'rc_patrolled' => intval($patrol),
343 'rc_new' => 0, # obsolete
344 'rc_old_len' => $oldSize,
345 'rc_new_len' => $newSize,
348 'rc_log_type' => null,
349 'rc_log_action' => '',
354 'prefixedDBkey' => $title->getPrefixedDBkey(),
355 'lastTimestamp' => $lastTimestamp,
356 'oldSize' => $oldSize,
357 'newSize' => $newSize,
364 * Makes an entry in the database corresponding to page creation
365 * Note: the title object must be loaded with the new id using resetArticleID()
366 * @todo Document parameters and return
368 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
369 $ip='', $size=0, $newId=0, $patrol=0 )
378 $rc = new RecentChange
;
379 $rc->mAttribs
= array(
380 'rc_timestamp' => $timestamp,
381 'rc_cur_time' => $timestamp,
382 'rc_namespace' => $title->getNamespace(),
383 'rc_title' => $title->getDBkey(),
385 'rc_minor' => $minor ?
1 : 0,
386 'rc_cur_id' => $title->getArticleID(),
387 'rc_user' => $user->getId(),
388 'rc_user_text' => $user->getName(),
389 'rc_comment' => $comment,
390 'rc_this_oldid' => $newId,
391 'rc_last_oldid' => 0,
392 'rc_bot' => $bot ?
1 : 0,
393 'rc_moved_to_ns' => 0,
394 'rc_moved_to_title' => '',
396 'rc_patrolled' => intval($patrol),
397 'rc_new' => 1, # obsolete
399 'rc_new_len' => $size,
402 'rc_log_type' => null,
403 'rc_log_action' => '',
408 'prefixedDBkey' => $title->getPrefixedDBkey(),
409 'lastTimestamp' => 0,
417 # Makes an entry in the database corresponding to a rename
418 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
429 $rc = new RecentChange
;
430 $rc->mAttribs
= array(
431 'rc_timestamp' => $timestamp,
432 'rc_cur_time' => $timestamp,
433 'rc_namespace' => $oldTitle->getNamespace(),
434 'rc_title' => $oldTitle->getDBkey(),
435 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
437 'rc_cur_id' => $oldTitle->getArticleID(),
438 'rc_user' => $user->getId(),
439 'rc_user_text' => $user->getName(),
440 'rc_comment' => $comment,
441 'rc_this_oldid' => 0,
442 'rc_last_oldid' => 0,
443 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
444 'rc_moved_to_ns' => $newTitle->getNamespace(),
445 'rc_moved_to_title' => $newTitle->getDBkey(),
447 'rc_new' => 0, # obsolete
449 'rc_old_len' => NULL,
450 'rc_new_len' => NULL,
452 'rc_logid' => 0, # notifyMove not used anymore
453 'rc_log_type' => null,
454 'rc_log_action' => '',
459 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
460 'lastTimestamp' => 0,
461 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
466 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
467 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
470 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
471 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
474 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='',
475 $type, $action, $target, $logComment, $params, $newId=0 )
486 $rc = new RecentChange
;
487 $rc->mAttribs
= array(
488 'rc_timestamp' => $timestamp,
489 'rc_cur_time' => $timestamp,
490 'rc_namespace' => $target->getNamespace(),
491 'rc_title' => $target->getDBkey(),
494 'rc_cur_id' => $target->getArticleID(),
495 'rc_user' => $user->getId(),
496 'rc_user_text' => $user->getName(),
497 'rc_comment' => $logComment,
498 'rc_this_oldid' => 0,
499 'rc_last_oldid' => 0,
500 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
501 'rc_moved_to_ns' => 0,
502 'rc_moved_to_title' => '',
505 'rc_new' => 0, # obsolete
506 'rc_old_len' => NULL,
507 'rc_new_len' => NULL,
509 'rc_logid' => $newId,
510 'rc_log_type' => $type,
511 'rc_log_action' => $action,
512 'rc_params' => $params
515 'prefixedDBkey' => $title->getPrefixedDBkey(),
516 'lastTimestamp' => 0,
517 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
522 # Initialises the members of this object from a mysql row object
523 public function loadFromRow( $row ) {
524 $this->mAttribs
= get_object_vars( $row );
525 $this->mAttribs
['rc_timestamp'] = wfTimestamp(TS_MW
, $this->mAttribs
['rc_timestamp']);
526 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
; // MUST be set
527 $this->mExtra
= array();
530 # Makes a pseudo-RC entry from a cur row
531 public function loadFromCurRow( $row ) {
532 $this->mAttribs
= array(
533 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
534 'rc_cur_time' => $row->rev_timestamp
,
535 'rc_user' => $row->rev_user
,
536 'rc_user_text' => $row->rev_user_text
,
537 'rc_namespace' => $row->page_namespace
,
538 'rc_title' => $row->page_title
,
539 'rc_comment' => $row->rev_comment
,
540 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
541 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
542 'rc_cur_id' => $row->page_id
,
543 'rc_this_oldid' => $row->rev_id
,
544 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
546 'rc_moved_to_ns' => 0,
547 'rc_moved_to_title' => '',
549 'rc_id' => $row->rc_id
,
550 'rc_patrolled' => $row->rc_patrolled
,
551 'rc_new' => $row->page_is_new
, # obsolete
552 'rc_old_len' => $row->rc_old_len
,
553 'rc_new_len' => $row->rc_new_len
,
554 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
555 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
556 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
557 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
558 'rc_deleted' => $row->rc_deleted
// MUST be set
560 $this->mExtra
= array();
564 * Get an attribute value
566 * @param $name Attribute name
569 public function getAttribute( $name ) {
570 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
574 * Gets the end part of the diff URL associated with this object
575 * Blank if no diff link should be displayed
577 public function diffLinkTrail( $forceCur ) {
578 if( $this->mAttribs
['rc_type'] == RC_EDIT
) {
579 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
580 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
582 $trail .= '&diff=0' ;
584 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
592 protected function getIRCLine() {
593 global $wgUseRCPatrol, $wgUseNPPatrol, $wgRC2UDPInterwikiPrefix, $wgLocalInterwiki;
595 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
596 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
597 extract($this->mAttribs
);
598 extract($this->mExtra
);
600 if( $rc_type == RC_LOG
) {
601 $titleObj = Title
::newFromText( "Log/$rc_log_type", NS_SPECIAL
);
603 $titleObj =& $this->getTitle();
605 $title = $titleObj->getPrefixedText();
606 $title = self
::cleanupForIRC( $title );
608 if( $rc_type == RC_LOG
) {
611 if( $rc_type == RC_NEW
) {
612 $url = "oldid=$rc_this_oldid";
614 $url = "diff=$rc_this_oldid&oldid=$rc_last_oldid";
616 if( $wgUseRCPatrol ||
($rc_type == RC_NEW
&& $wgUseNPPatrol) ) {
617 $url .= "&rcid=$rc_id";
619 // XXX: *HACK* this should use getFullURL(), hacked for SSL madness --brion 2005-12-26
620 // XXX: *HACK^2* the preg_replace() undoes much of what getInternalURL() does, but we
621 // XXX: need to call it so that URL paths on the Wikimedia secure server can be fixed
622 // XXX: by a custom GetInternalURL hook --vyznev 2008-12-10
623 $url = preg_replace( '/title=[^&]*&/', '', $titleObj->getInternalURL( $url ) );
626 if( isset( $oldSize ) && isset( $newSize ) ) {
627 $szdiff = $newSize - $oldSize;
629 $szdiff = "\002$szdiff\002";
630 } elseif($szdiff >= 0) {
631 $szdiff = '+' . $szdiff ;
633 $szdiff = '(' . $szdiff . ')' ;
638 $user = self
::cleanupForIRC( $rc_user_text );
640 if( $rc_type == RC_LOG
) {
641 $targetText = $this->getTitle()->getPrefixedText();
642 $comment = self
::cleanupForIRC( str_replace("[[$targetText]]","[[\00302$targetText\00310]]",$actionComment) );
643 $flag = $rc_log_action;
645 $comment = self
::cleanupForIRC( $rc_comment );
646 $flag = ($rc_new ?
"N" : "") . ($rc_minor ?
"M" : "") . ($rc_bot ?
"B" : "");
649 if ( $wgRC2UDPInterwikiPrefix === true ) {
650 $prefix = $wgLocalInterwiki;
651 } elseif ( $wgRC2UDPInterwikiPrefix ) {
652 $prefix = $wgRC2UDPInterwikiPrefix;
656 if ( $prefix !== false ) {
657 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
659 $titleString = "\00314[[\00307$title\00314]]";
662 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
663 # no colour (\003) switches back to the term default
664 $fullString = "$titleString\0034 $flag\00310 " .
665 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
671 * Returns the change size (HTML).
672 * The lengths can be given optionally.
674 public function getCharacterDifference( $old = 0, $new = 0 ) {
676 $old = $this->mAttribs
['rc_old_len'];
679 $new = $this->mAttribs
['rc_new_len'];
681 if( $old === NULL ||
$new === NULL ) {
684 return ChangesList
::showCharacterDifference( $old, $new );