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 function setAttribs( $attribs ) {
112 $this->mAttribs
= $attribs;
115 function setExtra( $extra ) {
116 $this->mExtra
= $extra;
119 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 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
136 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress,
137 $wgRC2UDPPort, $wgRC2UDPPrefix, $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( 'rc_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();
171 # Update old rows, if necessary
172 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
173 $lastTime = $this->mExtra
['lastTimestamp'];
176 # Notify external application via UDP
177 if ( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
178 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
180 $line = $wgRC2UDPPrefix . $this->getIRCLine();
181 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
182 socket_close( $conn );
186 # E-mail notifications
187 global $wgUseEnotif, $wgShowUpdatedMarker, $wgUser;
188 if( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
190 if( $this->mAttribs
['rc_user'] ) {
191 $editor = ($wgUser->getId() == $this->mAttribs
['rc_user']) ?
192 $wgUser : User
::newFromID( $this->mAttribs
['rc_user'] );
195 $editor = ($wgUser->getName() == $this->mAttribs
['rc_user_text']) ?
196 $wgUser : User
::newFromName( $this->mAttribs
['rc_user_text'], false );
198 # FIXME: this would be better as an extension hook
199 $enotif = new EmailNotification();
200 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
201 $enotif->notifyOnPageChange( $editor, $title,
202 $this->mAttribs
['rc_timestamp'],
203 $this->mAttribs
['rc_comment'],
204 $this->mAttribs
['rc_minor'],
205 $this->mAttribs
['rc_last_oldid'] );
209 wfRunHooks( 'RecentChange_save', array( &$this ) );
213 * Mark a given change as patrolled
215 * @param mixed $change RecentChange or corresponding rc_id
216 * @return See doMarkPatrolled(), or null if $change is not an existing rc_id
218 public static function markPatrolled( $change ) {
219 $change = $change instanceof RecentChange
221 : RecentChange
::newFromId($change);
222 if(!$change instanceof RecentChange
)
224 return $change->doMarkPatrolled();
228 * Mark this RecentChange as patrolled
230 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
231 * @return array of permissions errors, see Title::getUserPermissionsErrors()
233 public function doMarkPatrolled() {
234 global $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
236 // If recentchanges patrol is disabled, only new pages
238 if(!$wgUseRCPatrol && (!$wgUseNPPatrol ||
$this->getAttribute('rc_type') != RC_NEW
))
239 $errors[] = array('rcpatroldisabled');
240 $errors = array_merge($errors, $this->getTitle()->getUserPermissionsErrors('patrol', $wgUser));
241 if(!wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$wgUser, false)))
242 $errors[] = array('hookaborted');
243 // Users without the 'autopatrol' right can't patrol their
245 if($wgUser->getName() == $this->getAttribute('rc_user_text') &&
246 !$wgUser->isAllowed('autopatrol'))
247 $errors[] = array('markedaspatrollederror-noautopatrol');
251 // If the change was patrolled already, do nothing
252 if($this->getAttribute('rc_patrolled'))
254 $this->reallyMarkPatrolled();
255 PatrolLog
::record($this);
256 wfRunHooks('MarkPatrolledComplete',
257 array($this->getAttribute('rc_id'), &$wgUser, false));
262 * Mark this RecentChange patrolled, without error checking
263 * @return int Number of affected rows
265 public function reallyMarkPatrolled() {
266 $dbw = wfGetDB( DB_MASTER
);
273 'rc_id' => $this->getAttribute('rc_id')
277 return $dbw->affectedRows();
280 # Makes an entry in the database corresponding to an edit
281 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
282 $oldId, $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0,
292 $rc = new RecentChange
;
293 $rc->mAttribs
= array(
294 'rc_timestamp' => $timestamp,
295 'rc_cur_time' => $timestamp,
296 'rc_namespace' => $title->getNamespace(),
297 'rc_title' => $title->getDBkey(),
298 'rc_type' => RC_EDIT
,
299 'rc_minor' => $minor ?
1 : 0,
300 'rc_cur_id' => $title->getArticleID(),
301 'rc_user' => $user->getId(),
302 'rc_user_text' => $user->getName(),
303 'rc_comment' => $comment,
304 'rc_this_oldid' => $newId,
305 'rc_last_oldid' => $oldId,
306 'rc_bot' => $bot ?
1 : 0,
307 'rc_moved_to_ns' => 0,
308 'rc_moved_to_title' => '',
311 'rc_new' => 0, # obsolete
312 'rc_old_len' => $oldSize,
313 'rc_new_len' => $newSize,
316 'rc_log_type' => null,
317 'rc_log_action' => '',
322 'prefixedDBkey' => $title->getPrefixedDBkey(),
323 'lastTimestamp' => $lastTimestamp,
324 'oldSize' => $oldSize,
325 'newSize' => $newSize,
328 return( $rc->mAttribs
['rc_id'] );
332 * Makes an entry in the database corresponding to page creation
333 * Note: the title object must be loaded with the new id using resetArticleID()
334 * @todo Document parameters and return
336 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
337 $ip='', $size = 0, $newId = 0 )
346 $rc = new RecentChange
;
347 $rc->mAttribs
= array(
348 'rc_timestamp' => $timestamp,
349 'rc_cur_time' => $timestamp,
350 'rc_namespace' => $title->getNamespace(),
351 'rc_title' => $title->getDBkey(),
353 'rc_minor' => $minor ?
1 : 0,
354 'rc_cur_id' => $title->getArticleID(),
355 'rc_user' => $user->getId(),
356 'rc_user_text' => $user->getName(),
357 'rc_comment' => $comment,
358 'rc_this_oldid' => $newId,
359 'rc_last_oldid' => 0,
360 'rc_bot' => $bot ?
1 : 0,
361 'rc_moved_to_ns' => 0,
362 'rc_moved_to_title' => '',
365 'rc_new' => 1, # obsolete
367 'rc_new_len' => $size,
370 'rc_log_type' => null,
371 'rc_log_action' => '',
376 'prefixedDBkey' => $title->getPrefixedDBkey(),
377 'lastTimestamp' => 0,
382 return( $rc->mAttribs
['rc_id'] );
385 # Makes an entry in the database corresponding to a rename
386 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
397 $rc = new RecentChange
;
398 $rc->mAttribs
= array(
399 'rc_timestamp' => $timestamp,
400 'rc_cur_time' => $timestamp,
401 'rc_namespace' => $oldTitle->getNamespace(),
402 'rc_title' => $oldTitle->getDBkey(),
403 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
405 'rc_cur_id' => $oldTitle->getArticleID(),
406 'rc_user' => $user->getId(),
407 'rc_user_text' => $user->getName(),
408 'rc_comment' => $comment,
409 'rc_this_oldid' => 0,
410 'rc_last_oldid' => 0,
411 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
412 'rc_moved_to_ns' => $newTitle->getNamespace(),
413 'rc_moved_to_title' => $newTitle->getDBkey(),
415 'rc_new' => 0, # obsolete
417 'rc_old_len' => NULL,
418 'rc_new_len' => NULL,
420 'rc_logid' => 0, # notifyMove not used anymore
421 'rc_log_type' => null,
422 'rc_log_action' => '',
427 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
428 'lastTimestamp' => 0,
429 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
434 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
435 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
438 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
439 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
442 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='',
443 $type, $action, $target, $logComment, $params, $newId=0 )
454 $rc = new RecentChange
;
455 $rc->mAttribs
= array(
456 'rc_timestamp' => $timestamp,
457 'rc_cur_time' => $timestamp,
458 'rc_namespace' => $target->getNamespace(),
459 'rc_title' => $target->getDBkey(),
462 'rc_cur_id' => $target->getArticleID(),
463 'rc_user' => $user->getId(),
464 'rc_user_text' => $user->getName(),
465 'rc_comment' => $logComment,
466 'rc_this_oldid' => 0,
467 'rc_last_oldid' => 0,
468 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
469 'rc_moved_to_ns' => 0,
470 'rc_moved_to_title' => '',
473 'rc_new' => 0, # obsolete
474 'rc_old_len' => NULL,
475 'rc_new_len' => NULL,
477 'rc_logid' => $newId,
478 'rc_log_type' => $type,
479 'rc_log_action' => $action,
480 'rc_params' => $params
483 'prefixedDBkey' => $title->getPrefixedDBkey(),
484 'lastTimestamp' => 0,
485 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
490 # Initialises the members of this object from a mysql row object
491 function loadFromRow( $row )
493 $this->mAttribs
= get_object_vars( $row );
494 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
495 $this->mExtra
= array();
498 # Makes a pseudo-RC entry from a cur row
499 function loadFromCurRow( $row )
501 $this->mAttribs
= array(
502 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
503 'rc_cur_time' => $row->rev_timestamp
,
504 'rc_user' => $row->rev_user
,
505 'rc_user_text' => $row->rev_user_text
,
506 'rc_namespace' => $row->page_namespace
,
507 'rc_title' => $row->page_title
,
508 'rc_comment' => $row->rev_comment
,
509 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
510 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
511 'rc_cur_id' => $row->page_id
,
512 'rc_this_oldid' => $row->rev_id
,
513 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
515 'rc_moved_to_ns' => 0,
516 'rc_moved_to_title' => '',
518 'rc_id' => $row->rc_id
,
519 'rc_patrolled' => $row->rc_patrolled
,
520 'rc_new' => $row->page_is_new
, # obsolete
521 'rc_old_len' => $row->rc_old_len
,
522 'rc_new_len' => $row->rc_new_len
,
523 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
524 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
525 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
526 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
527 // this one REALLY should be set...
528 'rc_deleted' => isset($row->rc_deleted
) ?
$row->rc_deleted
: 0,
531 $this->mExtra
= array();
535 * Get an attribute value
537 * @param $name Attribute name
540 public function getAttribute( $name ) {
541 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
545 * Get RC select fields for changes lists
547 public static function getSelectFields() {
571 'rc_deleted' // this one REALLY should be set...
576 * Gets the end part of the diff URL associated with this object
577 * Blank if no diff link should be displayed
579 function diffLinkTrail( $forceCur ) {
580 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
581 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
582 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
584 $trail .= '&diff=0' ;
586 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
594 function cleanupForIRC( $text ) {
595 return str_replace(array("\n", "\r"), array("", ""), $text);
598 function getIRCLine() {
599 global $wgUseRCPatrol;
601 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
602 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
603 extract($this->mAttribs
);
604 extract($this->mExtra
);
606 if ( $rc_type == RC_LOG
) {
607 $titleObj = Title
::newFromText( "Log/$rc_log_type", NS_SPECIAL
);
609 $titleObj =& $this->getTitle();
611 $title = $titleObj->getPrefixedText();
612 $title = $this->cleanupForIRC( $title );
614 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
615 if ( $rc_type == RC_LOG
) {
617 } elseif ( $rc_new && $wgUseRCPatrol ) {
618 $url = $titleObj->getInternalURL("rcid=$rc_id");
619 } else if ( $rc_new ) {
620 $url = $titleObj->getInternalURL();
621 } else if ( $wgUseRCPatrol ) {
622 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
624 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
627 if ( isset( $oldSize ) && isset( $newSize ) ) {
628 $szdiff = $newSize - $oldSize;
629 if ($szdiff < -500) {
630 $szdiff = "\002$szdiff\002";
631 } elseif ($szdiff >= 0) {
632 $szdiff = '+' . $szdiff ;
634 $szdiff = '(' . $szdiff . ')' ;
639 $user = $this->cleanupForIRC( $rc_user_text );
641 if ( $rc_type == RC_LOG
) {
642 $logTargetText = $this->getTitle()->getPrefixedText();
643 $comment = $this->cleanupForIRC( str_replace($logTargetText,"\00302$logTargetText\00310",$actionComment) );
644 $flag = $rc_log_action;
646 $comment = $this->cleanupForIRC( $rc_comment );
647 $flag = ($rc_new ?
"N" : "") . ($rc_minor ?
"M" : "") . ($rc_bot ?
"B" : "");
649 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
650 # no colour (\003) switches back to the term default
651 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
652 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
657 * Returns the change size (HTML).
658 * The lengths can be given optionally.
660 function getCharacterDifference( $old = 0, $new = 0 ) {
661 global $wgRCChangedSizeThreshold, $wgLang;
664 $old = $this->mAttribs
['rc_old_len'];
667 $new = $this->mAttribs
['rc_new_len'];
670 if( $old === NULL ||
$new === NULL ) {
674 $szdiff = $new - $old;
675 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
676 $wgLang->formatNum($szdiff) );
678 if( $szdiff < $wgRCChangedSizeThreshold ) {
679 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
680 } elseif( $szdiff === 0 ) {
681 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
682 } elseif( $szdiff > 0 ) {
683 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
685 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';