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, $wgRC2UDPPrefix, $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( $wgRC2UDPPrefix, $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 $enotif = new EmailNotification();
196 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
197 $enotif->notifyOnPageChange( $editor, $title,
198 $this->mAttribs
['rc_timestamp'],
199 $this->mAttribs
['rc_comment'],
200 $this->mAttribs
['rc_minor'],
201 $this->mAttribs
['rc_last_oldid'] );
205 wfRunHooks( 'RecentChange_save', array( &$this ) );
209 * Send some text to UDP
210 * @param string $line
211 * @return bool success
213 public static function sendToUDP( $prefix, $line ) {
214 global $wgRC2UDPAddress, $wgRC2UDPPort;
215 # Notify external application via UDP
216 if( $wgRC2UDPAddress ) {
217 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
219 $line = $prefix . $line;
220 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
221 socket_close( $conn );
229 * Remove newlines and carriage returns
230 * @param string $line
233 public static function cleanupForIRC( $text ) {
234 return str_replace(array("\n", "\r"), array("", ""), $text);
238 * Mark a given change as patrolled
240 * @param mixed $change RecentChange or corresponding rc_id
241 * @param bool $auto for automatic patrol
242 * @return See doMarkPatrolled(), or null if $change is not an existing rc_id
244 public static function markPatrolled( $change, $auto = false ) {
245 $change = $change instanceof RecentChange
247 : RecentChange
::newFromId($change);
248 if( !$change instanceof RecentChange
) {
251 return $change->doMarkPatrolled( $auto );
255 * Mark this RecentChange as patrolled
257 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
258 * @param bool $auto for automatic patrol
259 * @return array of permissions errors, see Title::getUserPermissionsErrors()
261 public function doMarkPatrolled( $auto = false ) {
262 global $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
264 // If recentchanges patrol is disabled, only new pages
267 && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) )
269 $errors[] = array('rcpatroldisabled');
272 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
273 $right = $auto ?
'autopatrol' : 'patrol';
274 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $wgUser ) );
275 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$wgUser, false)) )
276 $errors[] = array('hookaborted');
278 // Users without the 'autopatrol' right can't patrol their
280 if( $wgUser->getName() == $this->getAttribute('rc_user_text') && !$wgUser->isAllowed('autopatrol') )
281 $errors[] = array('markedaspatrollederror-noautopatrol');
287 // If the change was patrolled already, do nothing
288 if( $this->getAttribute('rc_patrolled') )
291 // Actually set the 'patrolled' flag in RC
292 $this->reallyMarkPatrolled();
294 // Log this patrol event
295 PatrolLog
::record( $this, $auto );
296 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$wgUser, false) );
301 * Mark this RecentChange patrolled, without error checking
302 * @return int Number of affected rows
304 public function reallyMarkPatrolled() {
305 $dbw = wfGetDB( DB_MASTER
);
312 'rc_id' => $this->getAttribute('rc_id')
316 return $dbw->affectedRows();
319 # Makes an entry in the database corresponding to an edit
320 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
321 $oldId, $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0)
330 $rc = new RecentChange
;
331 $rc->mAttribs
= array(
332 'rc_timestamp' => $timestamp,
333 'rc_cur_time' => $timestamp,
334 'rc_namespace' => $title->getNamespace(),
335 'rc_title' => $title->getDBkey(),
336 'rc_type' => RC_EDIT
,
337 'rc_minor' => $minor ?
1 : 0,
338 'rc_cur_id' => $title->getArticleID(),
339 'rc_user' => $user->getId(),
340 'rc_user_text' => $user->getName(),
341 'rc_comment' => $comment,
342 'rc_this_oldid' => $newId,
343 'rc_last_oldid' => $oldId,
344 'rc_bot' => $bot ?
1 : 0,
345 'rc_moved_to_ns' => 0,
346 'rc_moved_to_title' => '',
349 'rc_new' => 0, # obsolete
350 'rc_old_len' => $oldSize,
351 'rc_new_len' => $newSize,
354 'rc_log_type' => null,
355 'rc_log_action' => '',
360 'prefixedDBkey' => $title->getPrefixedDBkey(),
361 'lastTimestamp' => $lastTimestamp,
362 'oldSize' => $oldSize,
363 'newSize' => $newSize,
370 * Makes an entry in the database corresponding to page creation
371 * Note: the title object must be loaded with the new id using resetArticleID()
372 * @todo Document parameters and return
374 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
375 $ip='', $size = 0, $newId = 0 )
384 $rc = new RecentChange
;
385 $rc->mAttribs
= array(
386 'rc_timestamp' => $timestamp,
387 'rc_cur_time' => $timestamp,
388 'rc_namespace' => $title->getNamespace(),
389 'rc_title' => $title->getDBkey(),
391 'rc_minor' => $minor ?
1 : 0,
392 'rc_cur_id' => $title->getArticleID(),
393 'rc_user' => $user->getId(),
394 'rc_user_text' => $user->getName(),
395 'rc_comment' => $comment,
396 'rc_this_oldid' => $newId,
397 'rc_last_oldid' => 0,
398 'rc_bot' => $bot ?
1 : 0,
399 'rc_moved_to_ns' => 0,
400 'rc_moved_to_title' => '',
403 'rc_new' => 1, # obsolete
405 'rc_new_len' => $size,
408 'rc_log_type' => null,
409 'rc_log_action' => '',
414 'prefixedDBkey' => $title->getPrefixedDBkey(),
415 'lastTimestamp' => 0,
423 # Makes an entry in the database corresponding to a rename
424 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
435 $rc = new RecentChange
;
436 $rc->mAttribs
= array(
437 'rc_timestamp' => $timestamp,
438 'rc_cur_time' => $timestamp,
439 'rc_namespace' => $oldTitle->getNamespace(),
440 'rc_title' => $oldTitle->getDBkey(),
441 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
443 'rc_cur_id' => $oldTitle->getArticleID(),
444 'rc_user' => $user->getId(),
445 'rc_user_text' => $user->getName(),
446 'rc_comment' => $comment,
447 'rc_this_oldid' => 0,
448 'rc_last_oldid' => 0,
449 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
450 'rc_moved_to_ns' => $newTitle->getNamespace(),
451 'rc_moved_to_title' => $newTitle->getDBkey(),
453 'rc_new' => 0, # obsolete
455 'rc_old_len' => NULL,
456 'rc_new_len' => NULL,
458 'rc_logid' => 0, # notifyMove not used anymore
459 'rc_log_type' => null,
460 'rc_log_action' => '',
465 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
466 'lastTimestamp' => 0,
467 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
472 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
473 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
476 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
477 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
480 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='',
481 $type, $action, $target, $logComment, $params, $newId=0 )
492 $rc = new RecentChange
;
493 $rc->mAttribs
= array(
494 'rc_timestamp' => $timestamp,
495 'rc_cur_time' => $timestamp,
496 'rc_namespace' => $target->getNamespace(),
497 'rc_title' => $target->getDBkey(),
500 'rc_cur_id' => $target->getArticleID(),
501 'rc_user' => $user->getId(),
502 'rc_user_text' => $user->getName(),
503 'rc_comment' => $logComment,
504 'rc_this_oldid' => 0,
505 'rc_last_oldid' => 0,
506 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
507 'rc_moved_to_ns' => 0,
508 'rc_moved_to_title' => '',
511 'rc_new' => 0, # obsolete
512 'rc_old_len' => NULL,
513 'rc_new_len' => NULL,
515 'rc_logid' => $newId,
516 'rc_log_type' => $type,
517 'rc_log_action' => $action,
518 'rc_params' => $params
521 'prefixedDBkey' => $title->getPrefixedDBkey(),
522 'lastTimestamp' => 0,
523 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
528 # Initialises the members of this object from a mysql row object
529 function loadFromRow( $row )
531 $this->mAttribs
= get_object_vars( $row );
532 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
533 $this->mExtra
= array();
536 # Makes a pseudo-RC entry from a cur row
537 function loadFromCurRow( $row )
539 $this->mAttribs
= array(
540 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
541 'rc_cur_time' => $row->rev_timestamp
,
542 'rc_user' => $row->rev_user
,
543 'rc_user_text' => $row->rev_user_text
,
544 'rc_namespace' => $row->page_namespace
,
545 'rc_title' => $row->page_title
,
546 'rc_comment' => $row->rev_comment
,
547 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
548 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
549 'rc_cur_id' => $row->page_id
,
550 'rc_this_oldid' => $row->rev_id
,
551 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
553 'rc_moved_to_ns' => 0,
554 'rc_moved_to_title' => '',
556 'rc_id' => $row->rc_id
,
557 'rc_patrolled' => $row->rc_patrolled
,
558 'rc_new' => $row->page_is_new
, # obsolete
559 'rc_old_len' => $row->rc_old_len
,
560 'rc_new_len' => $row->rc_new_len
,
561 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
562 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
563 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
564 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
565 // this one REALLY should be set...
566 'rc_deleted' => isset($row->rc_deleted
) ?
$row->rc_deleted
: 0,
569 $this->mExtra
= array();
573 * Get an attribute value
575 * @param $name Attribute name
578 public function getAttribute( $name ) {
579 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
583 * Gets the end part of the diff URL associated with this object
584 * Blank if no diff link should be displayed
586 function diffLinkTrail( $forceCur )
588 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
589 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
590 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
592 $trail .= '&diff=0' ;
594 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
602 function getIRCLine() {
603 global $wgUseRCPatrol;
605 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
606 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
607 extract($this->mAttribs
);
608 extract($this->mExtra
);
610 if ( $rc_type == RC_LOG
) {
611 $titleObj = Title
::newFromText( "Log/$rc_log_type", NS_SPECIAL
);
613 $titleObj =& $this->getTitle();
615 $title = $titleObj->getPrefixedText();
616 $title = self
::cleanupForIRC( $title );
618 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
619 if ( $rc_type == RC_LOG
) {
621 } elseif ( $rc_new && $wgUseRCPatrol ) {
622 $url = $titleObj->getInternalURL("rcid=$rc_id");
623 } else if ( $rc_new ) {
624 $url = $titleObj->getInternalURL();
625 } else if ( $wgUseRCPatrol ) {
626 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
628 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
631 if ( isset( $oldSize ) && isset( $newSize ) ) {
632 $szdiff = $newSize - $oldSize;
633 if ($szdiff < -500) {
634 $szdiff = "\002$szdiff\002";
635 } elseif ($szdiff >= 0) {
636 $szdiff = '+' . $szdiff ;
638 $szdiff = '(' . $szdiff . ')' ;
643 $user = self
::cleanupForIRC( $rc_user_text );
645 if ( $rc_type == RC_LOG
) {
646 $targetText = $this->getTitle()->getPrefixedText();
647 $comment = self
::cleanupForIRC( str_replace("[[$targetText]]","[[\00302$targetText\00310]]",$actionComment) );
648 $flag = $rc_log_action;
650 $comment = self
::cleanupForIRC( $rc_comment );
651 $flag = ($rc_new ?
"N" : "") . ($rc_minor ?
"M" : "") . ($rc_bot ?
"B" : "");
653 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
654 # no colour (\003) switches back to the term default
655 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
656 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
661 * Returns the change size (HTML).
662 * The lengths can be given optionally.
664 function getCharacterDifference( $old = 0, $new = 0 ) {
665 global $wgRCChangedSizeThreshold, $wgLang;
668 $old = $this->mAttribs
['rc_old_len'];
671 $new = $this->mAttribs
['rc_new_len'];
674 if( $old === NULL ||
$new === NULL ) {
678 $szdiff = $new - $old;
679 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
680 $wgLang->formatNum($szdiff) );
682 if( $szdiff < $wgRCChangedSizeThreshold ) {
683 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
684 } elseif( $szdiff === 0 ) {
685 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
686 } elseif( $szdiff > 0 ) {
687 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
689 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';