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 )
54 $rc = new RecentChange
;
55 $rc->loadFromRow( $row );
59 public static function newFromCurRow( $row )
61 $rc = new RecentChange
;
62 $rc->loadFromCurRow( $row );
63 $rc->notificationtimestamp
= false;
64 $rc->numberofWatchingusers
= false;
69 * Obtain the recent change with a given rc_id value
71 * @param $rcid rc_id value to retrieve
72 * @return RecentChange
74 public static function newFromId( $rcid ) {
75 $dbr = wfGetDB( DB_SLAVE
);
76 $res = $dbr->select( 'recentchanges', '*', array( 'rc_id' => $rcid ), __METHOD__
);
77 if( $res && $dbr->numRows( $res ) > 0 ) {
78 $row = $dbr->fetchObject( $res );
79 $dbr->freeResult( $res );
80 return self
::newFromRow( $row );
87 * Find the first recent change matching some specific conditions
89 * @param array $conds Array of conditions
90 * @param mixed $fname Override the method name in profiling/logs
91 * @return RecentChange
93 public static function newFromConds( $conds, $fname = false ) {
94 if( $fname === false )
96 $dbr = wfGetDB( DB_SLAVE
);
103 if( $res instanceof ResultWrapper
&& $res->numRows() > 0 ) {
104 $row = $res->fetchObject();
106 return self
::newFromRow( $row );
113 function setAttribs( $attribs )
115 $this->mAttribs
= $attribs;
118 function setExtra( $extra )
120 $this->mExtra
= $extra;
125 if ( $this->mTitle
=== false ) {
126 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
128 return $this->mTitle
;
131 function getMovedToTitle()
133 if ( $this->mMovedToTitle
=== false ) {
134 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
135 $this->mAttribs
['rc_moved_to_title'] );
137 return $this->mMovedToTitle
;
140 # Writes the data in this object to the database
143 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPOmitBots;
144 $fname = 'RecentChange::save';
146 $dbw = wfGetDB( DB_MASTER
);
147 if ( !is_array($this->mExtra
) ) {
148 $this->mExtra
= array();
150 $this->mExtra
['lang'] = $wgLocalInterwiki;
152 if ( !$wgPutIPinRC ) {
153 $this->mAttribs
['rc_ip'] = '';
156 # If our database is strict about IP addresses, use NULL instead of an empty string
157 if ( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
158 unset( $this->mAttribs
['rc_ip'] );
161 # Fixup database timestamps
162 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
163 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
164 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
166 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
167 if ( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
168 unset ( $this->mAttribs
['rc_cur_id'] );
172 $dbw->insert( 'recentchanges', $this->mAttribs
, $fname );
175 $this->mAttribs
['rc_id'] = $dbw->insertId();
177 # Notify external application via UDP
178 if ( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
179 self
::sendToUDP( $this->getIRCLine() );
182 # E-mail notifications
183 global $wgUseEnotif, $wgShowUpdatedMarker, $wgUser;
184 if( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
186 if( $this->mAttribs
['rc_user'] ) {
187 $editor = ($wgUser->getId() == $this->mAttribs
['rc_user']) ?
188 $wgUser : User
::newFromID( $this->mAttribs
['rc_user'] );
191 $editor = ($wgUser->getName() == $this->mAttribs
['rc_user_text']) ?
192 $wgUser : User
::newFromName( $this->mAttribs
['rc_user_text'], false );
194 # FIXME: this would be better as an extension hook
195 ## FIXME: Why is this treating log entries as changes to the page?
196 ## This causes problems when pages are deleted.
197 $enotif = new EmailNotification();
198 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
199 $enotif->notifyOnPageChange( $editor, $title,
200 $this->mAttribs
['rc_timestamp'],
201 $this->mAttribs
['rc_comment'],
202 $this->mAttribs
['rc_minor'],
203 $this->mAttribs
['rc_last_oldid'] );
207 wfRunHooks( 'RecentChange_save', array( &$this ) );
211 * Send some text to UDP
212 * @param string $line
213 * @param string $prefix
214 * @param string $address
215 * @return bool success
217 public static function sendToUDP( $line, $address = '', $prefix = '' ) {
218 global $wgRC2UDPAddress, $wgRC2UDPPrefix, $wgRC2UDPPort;
219 # Assume default for standard RC case
220 $address = $address ?
$address : $wgRC2UDPAddress;
221 $prefix = $prefix ?
$prefix : $wgRC2UDPPrefix;
222 # Notify external application via UDP
224 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
226 $line = $prefix . $line;
227 socket_sendto( $conn, $line, strlen($line), 0, $address, $wgRC2UDPPort );
228 socket_close( $conn );
236 * Remove newlines and carriage returns
237 * @param string $line
240 public static function cleanupForIRC( $text ) {
241 return str_replace(array("\n", "\r"), array("", ""), $text);
245 * Mark a given change as patrolled
247 * @param mixed $change RecentChange or corresponding rc_id
248 * @param bool $auto for automatic patrol
249 * @return See doMarkPatrolled(), or null if $change is not an existing rc_id
251 public static function markPatrolled( $change, $auto = false ) {
252 $change = $change instanceof RecentChange
254 : RecentChange
::newFromId($change);
255 if( !$change instanceof RecentChange
) {
258 return $change->doMarkPatrolled( $auto );
262 * Mark this RecentChange as patrolled
264 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
265 * @param bool $auto for automatic patrol
266 * @return array of permissions errors, see Title::getUserPermissionsErrors()
268 public function doMarkPatrolled( $auto = false ) {
269 global $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
271 // If recentchanges patrol is disabled, only new pages
274 && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) )
276 $errors[] = array('rcpatroldisabled');
279 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
280 $right = $auto ?
'autopatrol' : 'patrol';
281 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $wgUser ) );
282 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$wgUser, false)) )
283 $errors[] = array('hookaborted');
285 // Users without the 'autopatrol' right can't patrol their
287 if( $wgUser->getName() == $this->getAttribute('rc_user_text') && !$wgUser->isAllowed('autopatrol') )
288 $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();
301 // Log this patrol event
302 PatrolLog
::record( $this, $auto );
303 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$wgUser, false) );
308 * Mark this RecentChange patrolled, without error checking
309 * @return int Number of affected rows
311 public function reallyMarkPatrolled() {
312 $dbw = wfGetDB( DB_MASTER
);
319 'rc_id' => $this->getAttribute('rc_id')
323 return $dbw->affectedRows();
326 # Makes an entry in the database corresponding to an edit
327 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
328 $oldId, $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0)
337 $rc = new RecentChange
;
338 $rc->mAttribs
= array(
339 'rc_timestamp' => $timestamp,
340 'rc_cur_time' => $timestamp,
341 'rc_namespace' => $title->getNamespace(),
342 'rc_title' => $title->getDBkey(),
343 'rc_type' => RC_EDIT
,
344 'rc_minor' => $minor ?
1 : 0,
345 'rc_cur_id' => $title->getArticleID(),
346 'rc_user' => $user->getId(),
347 'rc_user_text' => $user->getName(),
348 'rc_comment' => $comment,
349 'rc_this_oldid' => $newId,
350 'rc_last_oldid' => $oldId,
351 'rc_bot' => $bot ?
1 : 0,
352 'rc_moved_to_ns' => 0,
353 'rc_moved_to_title' => '',
356 'rc_new' => 0, # obsolete
357 'rc_old_len' => $oldSize,
358 'rc_new_len' => $newSize,
361 'rc_log_type' => null,
362 'rc_log_action' => '',
367 'prefixedDBkey' => $title->getPrefixedDBkey(),
368 'lastTimestamp' => $lastTimestamp,
369 'oldSize' => $oldSize,
370 'newSize' => $newSize,
377 * Makes an entry in the database corresponding to page creation
378 * Note: the title object must be loaded with the new id using resetArticleID()
379 * @todo Document parameters and return
381 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
382 $ip='', $size = 0, $newId = 0 )
391 $rc = new RecentChange
;
392 $rc->mAttribs
= array(
393 'rc_timestamp' => $timestamp,
394 'rc_cur_time' => $timestamp,
395 'rc_namespace' => $title->getNamespace(),
396 'rc_title' => $title->getDBkey(),
398 'rc_minor' => $minor ?
1 : 0,
399 'rc_cur_id' => $title->getArticleID(),
400 'rc_user' => $user->getId(),
401 'rc_user_text' => $user->getName(),
402 'rc_comment' => $comment,
403 'rc_this_oldid' => $newId,
404 'rc_last_oldid' => 0,
405 'rc_bot' => $bot ?
1 : 0,
406 'rc_moved_to_ns' => 0,
407 'rc_moved_to_title' => '',
410 'rc_new' => 1, # obsolete
412 'rc_new_len' => $size,
415 'rc_log_type' => null,
416 'rc_log_action' => '',
421 'prefixedDBkey' => $title->getPrefixedDBkey(),
422 'lastTimestamp' => 0,
430 # Makes an entry in the database corresponding to a rename
431 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
442 $rc = new RecentChange
;
443 $rc->mAttribs
= array(
444 'rc_timestamp' => $timestamp,
445 'rc_cur_time' => $timestamp,
446 'rc_namespace' => $oldTitle->getNamespace(),
447 'rc_title' => $oldTitle->getDBkey(),
448 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
450 'rc_cur_id' => $oldTitle->getArticleID(),
451 'rc_user' => $user->getId(),
452 'rc_user_text' => $user->getName(),
453 'rc_comment' => $comment,
454 'rc_this_oldid' => 0,
455 'rc_last_oldid' => 0,
456 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
457 'rc_moved_to_ns' => $newTitle->getNamespace(),
458 'rc_moved_to_title' => $newTitle->getDBkey(),
460 'rc_new' => 0, # obsolete
462 'rc_old_len' => NULL,
463 'rc_new_len' => NULL,
465 'rc_logid' => 0, # notifyMove not used anymore
466 'rc_log_type' => null,
467 'rc_log_action' => '',
472 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
473 'lastTimestamp' => 0,
474 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
479 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
480 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
483 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
484 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
487 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='',
488 $type, $action, $target, $logComment, $params, $newId=0 )
499 $rc = new RecentChange
;
500 $rc->mAttribs
= array(
501 'rc_timestamp' => $timestamp,
502 'rc_cur_time' => $timestamp,
503 'rc_namespace' => $target->getNamespace(),
504 'rc_title' => $target->getDBkey(),
507 'rc_cur_id' => $target->getArticleID(),
508 'rc_user' => $user->getId(),
509 'rc_user_text' => $user->getName(),
510 'rc_comment' => $logComment,
511 'rc_this_oldid' => 0,
512 'rc_last_oldid' => 0,
513 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
514 'rc_moved_to_ns' => 0,
515 'rc_moved_to_title' => '',
518 'rc_new' => 0, # obsolete
519 'rc_old_len' => NULL,
520 'rc_new_len' => NULL,
522 'rc_logid' => $newId,
523 'rc_log_type' => $type,
524 'rc_log_action' => $action,
525 'rc_params' => $params
528 'prefixedDBkey' => $title->getPrefixedDBkey(),
529 'lastTimestamp' => 0,
530 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
535 # Initialises the members of this object from a mysql row object
536 function loadFromRow( $row )
538 $this->mAttribs
= get_object_vars( $row );
539 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
540 $this->mExtra
= array();
543 # Makes a pseudo-RC entry from a cur row
544 function loadFromCurRow( $row )
546 $this->mAttribs
= array(
547 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
548 'rc_cur_time' => $row->rev_timestamp
,
549 'rc_user' => $row->rev_user
,
550 'rc_user_text' => $row->rev_user_text
,
551 'rc_namespace' => $row->page_namespace
,
552 'rc_title' => $row->page_title
,
553 'rc_comment' => $row->rev_comment
,
554 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
555 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
556 'rc_cur_id' => $row->page_id
,
557 'rc_this_oldid' => $row->rev_id
,
558 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
560 'rc_moved_to_ns' => 0,
561 'rc_moved_to_title' => '',
563 'rc_id' => $row->rc_id
,
564 'rc_patrolled' => $row->rc_patrolled
,
565 'rc_new' => $row->page_is_new
, # obsolete
566 'rc_old_len' => $row->rc_old_len
,
567 'rc_new_len' => $row->rc_new_len
,
568 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
569 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
570 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
571 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
572 // this one REALLY should be set...
573 'rc_deleted' => isset($row->rc_deleted
) ?
$row->rc_deleted
: 0,
576 $this->mExtra
= array();
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;
590 * Gets the end part of the diff URL associated with this object
591 * Blank if no diff link should be displayed
593 function diffLinkTrail( $forceCur )
595 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
596 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
597 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
599 $trail .= '&diff=0' ;
601 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
609 function getIRCLine() {
610 global $wgUseRCPatrol;
612 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
613 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
614 extract($this->mAttribs
);
615 extract($this->mExtra
);
617 if ( $rc_type == RC_LOG
) {
618 $titleObj = Title
::newFromText( "Log/$rc_log_type", NS_SPECIAL
);
620 $titleObj =& $this->getTitle();
622 $title = $titleObj->getPrefixedText();
623 $title = self
::cleanupForIRC( $title );
625 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
626 if ( $rc_type == RC_LOG
) {
628 } elseif ( $rc_new && $wgUseRCPatrol ) {
629 $url = $titleObj->getInternalURL("rcid=$rc_id");
630 } else if ( $rc_new ) {
631 $url = $titleObj->getInternalURL();
632 } else if ( $wgUseRCPatrol ) {
633 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
635 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
638 if ( isset( $oldSize ) && isset( $newSize ) ) {
639 $szdiff = $newSize - $oldSize;
640 if ($szdiff < -500) {
641 $szdiff = "\002$szdiff\002";
642 } elseif ($szdiff >= 0) {
643 $szdiff = '+' . $szdiff ;
645 $szdiff = '(' . $szdiff . ')' ;
650 $user = self
::cleanupForIRC( $rc_user_text );
652 if ( $rc_type == RC_LOG
) {
653 $targetText = $this->getTitle()->getPrefixedText();
654 $comment = self
::cleanupForIRC( str_replace("[[$targetText]]","[[\00302$targetText\00310]]",$actionComment) );
655 $flag = $rc_log_action;
657 $comment = self
::cleanupForIRC( $rc_comment );
658 $flag = ($rc_new ?
"N" : "") . ($rc_minor ?
"M" : "") . ($rc_bot ?
"B" : "");
660 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
661 # no colour (\003) switches back to the term default
662 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
663 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
668 * Returns the change size (HTML).
669 * The lengths can be given optionally.
671 function getCharacterDifference( $old = 0, $new = 0 ) {
672 global $wgRCChangedSizeThreshold, $wgLang;
675 $old = $this->mAttribs
['rc_old_len'];
678 $new = $this->mAttribs
['rc_new_len'];
681 if( $old === NULL ||
$new === NULL ) {
685 $szdiff = $new - $old;
686 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
687 $wgLang->formatNum($szdiff) );
689 if( $szdiff < $wgRCChangedSizeThreshold ) {
690 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
691 } elseif( $szdiff === 0 ) {
692 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
693 } elseif( $szdiff > 0 ) {
694 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
696 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';