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 UDP
::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 # FIXME: this would be better as an extension hook
188 $enotif = new EmailNotification();
189 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
190 $enotif->notifyOnPageChange( $editor, $title,
191 $this->mAttribs
['rc_timestamp'],
192 $this->mAttribs
['rc_comment'],
193 $this->mAttribs
['rc_minor'],
194 $this->mAttribs
['rc_last_oldid'] );
198 wfRunHooks( 'RecentChange_save', array( &$this ) );
202 * Mark a given change as patrolled
204 * @param mixed $change RecentChange or corresponding rc_id
205 * @param bool $auto for automatic patrol
206 * @return See doMarkPatrolled(), or null if $change is not an existing rc_id
208 public static function markPatrolled( $change, $auto = false ) {
209 $change = $change instanceof RecentChange
211 : RecentChange
::newFromId($change);
212 if( !$change instanceof RecentChange
) {
215 return $change->doMarkPatrolled( $auto );
219 * Mark this RecentChange as patrolled
221 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
222 * @param bool $auto for automatic patrol
223 * @return array of permissions errors, see Title::getUserPermissionsErrors()
225 public function doMarkPatrolled( $auto = false ) {
226 global $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
228 // If recentchanges patrol is disabled, only new pages
230 if( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute('rc_type') != RC_NEW
) ) {
231 $errors[] = array('rcpatroldisabled');
233 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
234 $right = $auto ?
'autopatrol' : 'patrol';
235 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $wgUser ) );
236 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$wgUser, false)) ) {
237 $errors[] = array('hookaborted');
239 // Users without the 'autopatrol' right can't patrol their
241 if( $wgUser->getName() == $this->getAttribute('rc_user_text') && !$wgUser->isAllowed('autopatrol') ) {
242 $errors[] = array('markedaspatrollederror-noautopatrol');
247 // If the change was patrolled already, do nothing
248 if( $this->getAttribute('rc_patrolled') ) {
251 // Actually set the 'patrolled' flag in RC
252 $this->reallyMarkPatrolled();
253 // Log this patrol event
254 PatrolLog
::record( $this, $auto );
255 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$wgUser, false) );
260 * Mark this RecentChange patrolled, without error checking
261 * @return int Number of affected rows
263 public function reallyMarkPatrolled() {
264 $dbw = wfGetDB( DB_MASTER
);
271 'rc_id' => $this->getAttribute('rc_id')
275 return $dbw->affectedRows();
278 # Makes an entry in the database corresponding to an edit
279 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
280 $oldId, $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0)
289 $rc = new RecentChange
;
290 $rc->mAttribs
= array(
291 'rc_timestamp' => $timestamp,
292 'rc_cur_time' => $timestamp,
293 'rc_namespace' => $title->getNamespace(),
294 'rc_title' => $title->getDBkey(),
295 'rc_type' => RC_EDIT
,
296 'rc_minor' => $minor ?
1 : 0,
297 'rc_cur_id' => $title->getArticleID(),
298 'rc_user' => $user->getId(),
299 'rc_user_text' => $user->getName(),
300 'rc_comment' => $comment,
301 'rc_this_oldid' => $newId,
302 'rc_last_oldid' => $oldId,
303 'rc_bot' => $bot ?
1 : 0,
304 'rc_moved_to_ns' => 0,
305 'rc_moved_to_title' => '',
308 'rc_new' => 0, # obsolete
309 'rc_old_len' => $oldSize,
310 'rc_new_len' => $newSize,
313 'rc_log_type' => null,
314 'rc_log_action' => '',
319 'prefixedDBkey' => $title->getPrefixedDBkey(),
320 'lastTimestamp' => $lastTimestamp,
321 'oldSize' => $oldSize,
322 'newSize' => $newSize,
329 * Makes an entry in the database corresponding to page creation
330 * Note: the title object must be loaded with the new id using resetArticleID()
331 * @todo Document parameters and return
333 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
334 $ip='', $size = 0, $newId = 0 )
343 $rc = new RecentChange
;
344 $rc->mAttribs
= array(
345 'rc_timestamp' => $timestamp,
346 'rc_cur_time' => $timestamp,
347 'rc_namespace' => $title->getNamespace(),
348 'rc_title' => $title->getDBkey(),
350 'rc_minor' => $minor ?
1 : 0,
351 'rc_cur_id' => $title->getArticleID(),
352 'rc_user' => $user->getId(),
353 'rc_user_text' => $user->getName(),
354 'rc_comment' => $comment,
355 'rc_this_oldid' => $newId,
356 'rc_last_oldid' => 0,
357 'rc_bot' => $bot ?
1 : 0,
358 'rc_moved_to_ns' => 0,
359 'rc_moved_to_title' => '',
362 'rc_new' => 1, # obsolete
364 'rc_new_len' => $size,
367 'rc_log_type' => null,
368 'rc_log_action' => '',
373 'prefixedDBkey' => $title->getPrefixedDBkey(),
374 'lastTimestamp' => 0,
382 # Makes an entry in the database corresponding to a rename
383 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
394 $rc = new RecentChange
;
395 $rc->mAttribs
= array(
396 'rc_timestamp' => $timestamp,
397 'rc_cur_time' => $timestamp,
398 'rc_namespace' => $oldTitle->getNamespace(),
399 'rc_title' => $oldTitle->getDBkey(),
400 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
402 'rc_cur_id' => $oldTitle->getArticleID(),
403 'rc_user' => $user->getId(),
404 'rc_user_text' => $user->getName(),
405 'rc_comment' => $comment,
406 'rc_this_oldid' => 0,
407 'rc_last_oldid' => 0,
408 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
409 'rc_moved_to_ns' => $newTitle->getNamespace(),
410 'rc_moved_to_title' => $newTitle->getDBkey(),
412 'rc_new' => 0, # obsolete
414 'rc_old_len' => NULL,
415 'rc_new_len' => NULL,
417 'rc_logid' => 0, # notifyMove not used anymore
418 'rc_log_type' => null,
419 'rc_log_action' => '',
424 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
425 'lastTimestamp' => 0,
426 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
431 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
432 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
435 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
436 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
439 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='',
440 $type, $action, $target, $logComment, $params, $newId=0 )
451 $rc = new RecentChange
;
452 $rc->mAttribs
= array(
453 'rc_timestamp' => $timestamp,
454 'rc_cur_time' => $timestamp,
455 'rc_namespace' => $target->getNamespace(),
456 'rc_title' => $target->getDBkey(),
459 'rc_cur_id' => $target->getArticleID(),
460 'rc_user' => $user->getId(),
461 'rc_user_text' => $user->getName(),
462 'rc_comment' => $logComment,
463 'rc_this_oldid' => 0,
464 'rc_last_oldid' => 0,
465 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
466 'rc_moved_to_ns' => 0,
467 'rc_moved_to_title' => '',
470 'rc_new' => 0, # obsolete
471 'rc_old_len' => NULL,
472 'rc_new_len' => NULL,
474 'rc_logid' => $newId,
475 'rc_log_type' => $type,
476 'rc_log_action' => $action,
477 'rc_params' => $params
480 'prefixedDBkey' => $title->getPrefixedDBkey(),
481 'lastTimestamp' => 0,
482 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
487 # Initialises the members of this object from a mysql row object
488 public function loadFromRow( $row ) {
489 $this->mAttribs
= get_object_vars( $row );
490 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
491 $this->mExtra
= array();
494 # Makes a pseudo-RC entry from a cur row
495 public function loadFromCurRow( $row ) {
496 $this->mAttribs
= array(
497 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
498 'rc_cur_time' => $row->rev_timestamp
,
499 'rc_user' => $row->rev_user
,
500 'rc_user_text' => $row->rev_user_text
,
501 'rc_namespace' => $row->page_namespace
,
502 'rc_title' => $row->page_title
,
503 'rc_comment' => $row->rev_comment
,
504 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
505 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
506 'rc_cur_id' => $row->page_id
,
507 'rc_this_oldid' => $row->rev_id
,
508 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
510 'rc_moved_to_ns' => 0,
511 'rc_moved_to_title' => '',
513 'rc_id' => $row->rc_id
,
514 'rc_patrolled' => $row->rc_patrolled
,
515 'rc_new' => $row->page_is_new
, # obsolete
516 'rc_old_len' => $row->rc_old_len
,
517 'rc_new_len' => $row->rc_new_len
,
518 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
519 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
520 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
521 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
522 // this one REALLY should be set...
523 'rc_deleted' => isset($row->rc_deleted
) ?
$row->rc_deleted
: 0,
525 $this->mExtra
= array();
529 * Get an attribute value
531 * @param $name Attribute name
534 public function getAttribute( $name ) {
535 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
539 * Gets the end part of the diff URL associated with this object
540 * Blank if no diff link should be displayed
542 public function diffLinkTrail( $forceCur ) {
543 if( $this->mAttribs
['rc_type'] == RC_EDIT
) {
544 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
545 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
547 $trail .= '&diff=0' ;
549 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
557 protected function getIRCLine() {
558 global $wgUseRCPatrol;
560 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
561 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
562 extract($this->mAttribs
);
563 extract($this->mExtra
);
565 if( $rc_type == RC_LOG
) {
566 $titleObj = Title
::newFromText( "Log/$rc_log_type", NS_SPECIAL
);
568 $titleObj =& $this->getTitle();
570 $title = $titleObj->getPrefixedText();
571 $title = UDP
::cleanupForIRC( $title );
573 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
574 if( $rc_type == RC_LOG
) {
576 } elseif( $rc_new && $wgUseRCPatrol ) {
577 $url = $titleObj->getInternalURL("rcid=$rc_id");
578 } else if( $rc_new ) {
579 $url = $titleObj->getInternalURL();
580 } else if( $wgUseRCPatrol ) {
581 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
583 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
586 if( isset( $oldSize ) && isset( $newSize ) ) {
587 $szdiff = $newSize - $oldSize;
589 $szdiff = "\002$szdiff\002";
590 } elseif($szdiff >= 0) {
591 $szdiff = '+' . $szdiff ;
593 $szdiff = '(' . $szdiff . ')' ;
598 $user = UDP
::cleanupForIRC( $rc_user_text );
600 if( $rc_type == RC_LOG
) {
601 $targetText = $this->getTitle()->getPrefixedText();
602 $comment = UDP
::cleanupForIRC( str_replace("[[$targetText]]","[[\00302$targetText\00310]]",$actionComment) );
603 $flag = $rc_log_action;
605 $comment = UDP
::cleanupForIRC( $rc_comment );
606 $flag = ($rc_new ?
"N" : "") . ($rc_minor ?
"M" : "") . ($rc_bot ?
"B" : "");
608 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
609 # no colour (\003) switches back to the term default
610 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
611 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
616 * Returns the change size (HTML).
617 * The lengths can be given optionally.
619 public function getCharacterDifference( $old = 0, $new = 0 ) {
620 global $wgRCChangedSizeThreshold, $wgLang;
623 $old = $this->mAttribs
['rc_old_len'];
626 $new = $this->mAttribs
['rc_new_len'];
629 if( $old === NULL ||
$new === NULL ) {
633 $szdiff = $new - $old;
634 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
635 $wgLang->formatNum($szdiff) );
638 if( abs( $szdiff ) > abs( $wgRCChangedSizeThreshold ) ) {
645 if( $szdiff === 0 ) {
646 return "<$tag class='mw-plusminus-null'>($formatedSize)</$tag>";
647 } elseif( $szdiff > 0 ) {
648 return "<$tag class='mw-plusminus-pos'>(+$formatedSize)</$tag>";
650 return "<$tag class='mw-plusminus-neg'>($formatedSize)</$tag>";