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 $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
212 static function sendToUDP( $line ) {
213 global $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix;
214 # Notify external application via UDP
215 if( $wgRC2UDPAddress ) {
216 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
218 $line = $wgRC2UDPPrefix . $line;
219 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
220 socket_close( $conn );
226 * Mark a given change as patrolled
228 * @param mixed $change RecentChange or corresponding rc_id
229 * @param bool $auto for automatic patrol
230 * @return See doMarkPatrolled(), or null if $change is not an existing rc_id
232 public static function markPatrolled( $change, $auto = false ) {
233 $change = $change instanceof RecentChange
235 : RecentChange
::newFromId($change);
236 if( !$change instanceof RecentChange
) {
239 return $change->doMarkPatrolled( $auto );
243 * Mark this RecentChange as patrolled
245 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
246 * @param bool $auto for automatic patrol
247 * @return array of permissions errors, see Title::getUserPermissionsErrors()
249 public function doMarkPatrolled( $auto = false ) {
250 global $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
252 // If recentchanges patrol is disabled, only new pages
255 && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) )
257 $errors[] = array('rcpatroldisabled');
260 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
261 $right = $auto ?
'autopatrol' : 'patrol';
262 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $wgUser ) );
263 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$wgUser, false)) )
264 $errors[] = array('hookaborted');
266 // Users without the 'autopatrol' right can't patrol their
268 if( $wgUser->getName() == $this->getAttribute('rc_user_text') && !$wgUser->isAllowed('autopatrol') )
269 $errors[] = array('markedaspatrollederror-noautopatrol');
275 // If the change was patrolled already, do nothing
276 if( $this->getAttribute('rc_patrolled') )
279 // Actually set the 'patrolled' flag in RC
280 $this->reallyMarkPatrolled();
282 // Log this patrol event
283 PatrolLog
::record( $this, $auto );
284 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$wgUser, false) );
289 * Mark this RecentChange patrolled, without error checking
290 * @return int Number of affected rows
292 public function reallyMarkPatrolled() {
293 $dbw = wfGetDB( DB_MASTER
);
300 'rc_id' => $this->getAttribute('rc_id')
304 return $dbw->affectedRows();
307 # Makes an entry in the database corresponding to an edit
308 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
309 $oldId, $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0)
318 $rc = new RecentChange
;
319 $rc->mAttribs
= array(
320 'rc_timestamp' => $timestamp,
321 'rc_cur_time' => $timestamp,
322 'rc_namespace' => $title->getNamespace(),
323 'rc_title' => $title->getDBkey(),
324 'rc_type' => RC_EDIT
,
325 'rc_minor' => $minor ?
1 : 0,
326 'rc_cur_id' => $title->getArticleID(),
327 'rc_user' => $user->getId(),
328 'rc_user_text' => $user->getName(),
329 'rc_comment' => $comment,
330 'rc_this_oldid' => $newId,
331 'rc_last_oldid' => $oldId,
332 'rc_bot' => $bot ?
1 : 0,
333 'rc_moved_to_ns' => 0,
334 'rc_moved_to_title' => '',
337 'rc_new' => 0, # obsolete
338 'rc_old_len' => $oldSize,
339 'rc_new_len' => $newSize,
342 'rc_log_type' => null,
343 'rc_log_action' => '',
348 'prefixedDBkey' => $title->getPrefixedDBkey(),
349 'lastTimestamp' => $lastTimestamp,
350 'oldSize' => $oldSize,
351 'newSize' => $newSize,
358 * Makes an entry in the database corresponding to page creation
359 * Note: the title object must be loaded with the new id using resetArticleID()
360 * @todo Document parameters and return
362 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
363 $ip='', $size = 0, $newId = 0 )
372 $rc = new RecentChange
;
373 $rc->mAttribs
= array(
374 'rc_timestamp' => $timestamp,
375 'rc_cur_time' => $timestamp,
376 'rc_namespace' => $title->getNamespace(),
377 'rc_title' => $title->getDBkey(),
379 'rc_minor' => $minor ?
1 : 0,
380 'rc_cur_id' => $title->getArticleID(),
381 'rc_user' => $user->getId(),
382 'rc_user_text' => $user->getName(),
383 'rc_comment' => $comment,
384 'rc_this_oldid' => $newId,
385 'rc_last_oldid' => 0,
386 'rc_bot' => $bot ?
1 : 0,
387 'rc_moved_to_ns' => 0,
388 'rc_moved_to_title' => '',
391 'rc_new' => 1, # obsolete
393 'rc_new_len' => $size,
396 'rc_log_type' => null,
397 'rc_log_action' => '',
402 'prefixedDBkey' => $title->getPrefixedDBkey(),
403 'lastTimestamp' => 0,
411 # Makes an entry in the database corresponding to a rename
412 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
423 $rc = new RecentChange
;
424 $rc->mAttribs
= array(
425 'rc_timestamp' => $timestamp,
426 'rc_cur_time' => $timestamp,
427 'rc_namespace' => $oldTitle->getNamespace(),
428 'rc_title' => $oldTitle->getDBkey(),
429 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
431 'rc_cur_id' => $oldTitle->getArticleID(),
432 'rc_user' => $user->getId(),
433 'rc_user_text' => $user->getName(),
434 'rc_comment' => $comment,
435 'rc_this_oldid' => 0,
436 'rc_last_oldid' => 0,
437 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
438 'rc_moved_to_ns' => $newTitle->getNamespace(),
439 'rc_moved_to_title' => $newTitle->getDBkey(),
441 'rc_new' => 0, # obsolete
443 'rc_old_len' => NULL,
444 'rc_new_len' => NULL,
446 'rc_logid' => 0, # notifyMove not used anymore
447 'rc_log_type' => null,
448 'rc_log_action' => '',
453 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
454 'lastTimestamp' => 0,
455 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
460 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
461 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
464 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
465 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
468 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='',
469 $type, $action, $target, $logComment, $params, $newId=0 )
480 $rc = new RecentChange
;
481 $rc->mAttribs
= array(
482 'rc_timestamp' => $timestamp,
483 'rc_cur_time' => $timestamp,
484 'rc_namespace' => $target->getNamespace(),
485 'rc_title' => $target->getDBkey(),
488 'rc_cur_id' => $target->getArticleID(),
489 'rc_user' => $user->getId(),
490 'rc_user_text' => $user->getName(),
491 'rc_comment' => $logComment,
492 'rc_this_oldid' => 0,
493 'rc_last_oldid' => 0,
494 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
495 'rc_moved_to_ns' => 0,
496 'rc_moved_to_title' => '',
499 'rc_new' => 0, # obsolete
500 'rc_old_len' => NULL,
501 'rc_new_len' => NULL,
503 'rc_logid' => $newId,
504 'rc_log_type' => $type,
505 'rc_log_action' => $action,
506 'rc_params' => $params
509 'prefixedDBkey' => $title->getPrefixedDBkey(),
510 'lastTimestamp' => 0,
511 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
516 # Initialises the members of this object from a mysql row object
517 function loadFromRow( $row )
519 $this->mAttribs
= get_object_vars( $row );
520 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
521 $this->mExtra
= array();
524 # Makes a pseudo-RC entry from a cur row
525 function loadFromCurRow( $row )
527 $this->mAttribs
= array(
528 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
529 'rc_cur_time' => $row->rev_timestamp
,
530 'rc_user' => $row->rev_user
,
531 'rc_user_text' => $row->rev_user_text
,
532 'rc_namespace' => $row->page_namespace
,
533 'rc_title' => $row->page_title
,
534 'rc_comment' => $row->rev_comment
,
535 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
536 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
537 'rc_cur_id' => $row->page_id
,
538 'rc_this_oldid' => $row->rev_id
,
539 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
541 'rc_moved_to_ns' => 0,
542 'rc_moved_to_title' => '',
544 'rc_id' => $row->rc_id
,
545 'rc_patrolled' => $row->rc_patrolled
,
546 'rc_new' => $row->page_is_new
, # obsolete
547 'rc_old_len' => $row->rc_old_len
,
548 'rc_new_len' => $row->rc_new_len
,
549 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
550 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
551 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
552 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
553 // this one REALLY should be set...
554 'rc_deleted' => isset($row->rc_deleted
) ?
$row->rc_deleted
: 0,
557 $this->mExtra
= array();
561 * Get an attribute value
563 * @param $name Attribute name
566 public function getAttribute( $name ) {
567 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
571 * Gets the end part of the diff URL associated with this object
572 * Blank if no diff link should be displayed
574 function diffLinkTrail( $forceCur )
576 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
577 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
578 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
580 $trail .= '&diff=0' ;
582 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
590 function cleanupForIRC( $text ) {
591 return str_replace(array("\n", "\r"), array("", ""), $text);
594 function getIRCLine() {
595 global $wgUseRCPatrol;
597 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
598 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
599 extract($this->mAttribs
);
600 extract($this->mExtra
);
602 if ( $rc_type == RC_LOG
) {
603 $titleObj = Title
::newFromText( "Log/$rc_log_type", NS_SPECIAL
);
605 $titleObj =& $this->getTitle();
607 $title = $titleObj->getPrefixedText();
608 $title = $this->cleanupForIRC( $title );
610 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
611 if ( $rc_type == RC_LOG
) {
613 } elseif ( $rc_new && $wgUseRCPatrol ) {
614 $url = $titleObj->getInternalURL("rcid=$rc_id");
615 } else if ( $rc_new ) {
616 $url = $titleObj->getInternalURL();
617 } else if ( $wgUseRCPatrol ) {
618 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
620 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
623 if ( isset( $oldSize ) && isset( $newSize ) ) {
624 $szdiff = $newSize - $oldSize;
625 if ($szdiff < -500) {
626 $szdiff = "\002$szdiff\002";
627 } elseif ($szdiff >= 0) {
628 $szdiff = '+' . $szdiff ;
630 $szdiff = '(' . $szdiff . ')' ;
635 $user = $this->cleanupForIRC( $rc_user_text );
637 if ( $rc_type == RC_LOG
) {
638 $logTargetText = $this->getTitle()->getPrefixedText();
639 $comment = $this->cleanupForIRC( str_replace($logTargetText,"\00302$logTargetText\00310",$actionComment) );
640 $flag = $rc_log_action;
642 $comment = $this->cleanupForIRC( $rc_comment );
643 $flag = ($rc_new ?
"N" : "") . ($rc_minor ?
"M" : "") . ($rc_bot ?
"B" : "");
645 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
646 # no colour (\003) switches back to the term default
647 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
648 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
653 * Returns the change size (HTML).
654 * The lengths can be given optionally.
656 function getCharacterDifference( $old = 0, $new = 0 ) {
657 global $wgRCChangedSizeThreshold, $wgLang;
660 $old = $this->mAttribs
['rc_old_len'];
663 $new = $this->mAttribs
['rc_new_len'];
666 if( $old === NULL ||
$new === NULL ) {
670 $szdiff = $new - $old;
671 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
672 $wgLang->formatNum($szdiff) );
674 if( $szdiff < $wgRCChangedSizeThreshold ) {
675 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
676 } elseif( $szdiff === 0 ) {
677 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
678 } elseif( $szdiff > 0 ) {
679 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
681 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';