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 UDP
::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 * Mark a given change as patrolled
211 * @param mixed $change RecentChange or corresponding rc_id
212 * @param bool $auto for automatic patrol
213 * @return See doMarkPatrolled(), or null if $change is not an existing rc_id
215 public static function markPatrolled( $change, $auto = false ) {
216 $change = $change instanceof RecentChange
218 : RecentChange
::newFromId($change);
219 if( !$change instanceof RecentChange
) {
222 return $change->doMarkPatrolled( $auto );
226 * Mark this RecentChange as patrolled
228 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
229 * @param bool $auto for automatic patrol
230 * @return array of permissions errors, see Title::getUserPermissionsErrors()
232 public function doMarkPatrolled( $auto = false ) {
233 global $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
235 // If recentchanges patrol is disabled, only new pages
238 && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) )
240 $errors[] = array('rcpatroldisabled');
243 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
244 $right = $auto ?
'autopatrol' : 'patrol';
245 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $wgUser ) );
246 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$wgUser, false)) )
247 $errors[] = array('hookaborted');
249 // Users without the 'autopatrol' right can't patrol their
251 if( $wgUser->getName() == $this->getAttribute('rc_user_text') && !$wgUser->isAllowed('autopatrol') )
252 $errors[] = array('markedaspatrollederror-noautopatrol');
258 // If the change was patrolled already, do nothing
259 if( $this->getAttribute('rc_patrolled') )
262 // Actually set the 'patrolled' flag in RC
263 $this->reallyMarkPatrolled();
265 // Log this patrol event
266 PatrolLog
::record( $this, $auto );
267 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$wgUser, false) );
272 * Mark this RecentChange patrolled, without error checking
273 * @return int Number of affected rows
275 public function reallyMarkPatrolled() {
276 $dbw = wfGetDB( DB_MASTER
);
283 'rc_id' => $this->getAttribute('rc_id')
287 return $dbw->affectedRows();
290 # Makes an entry in the database corresponding to an edit
291 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
292 $oldId, $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0)
301 $rc = new RecentChange
;
302 $rc->mAttribs
= array(
303 'rc_timestamp' => $timestamp,
304 'rc_cur_time' => $timestamp,
305 'rc_namespace' => $title->getNamespace(),
306 'rc_title' => $title->getDBkey(),
307 'rc_type' => RC_EDIT
,
308 'rc_minor' => $minor ?
1 : 0,
309 'rc_cur_id' => $title->getArticleID(),
310 'rc_user' => $user->getId(),
311 'rc_user_text' => $user->getName(),
312 'rc_comment' => $comment,
313 'rc_this_oldid' => $newId,
314 'rc_last_oldid' => $oldId,
315 'rc_bot' => $bot ?
1 : 0,
316 'rc_moved_to_ns' => 0,
317 'rc_moved_to_title' => '',
320 'rc_new' => 0, # obsolete
321 'rc_old_len' => $oldSize,
322 'rc_new_len' => $newSize,
325 'rc_log_type' => null,
326 'rc_log_action' => '',
331 'prefixedDBkey' => $title->getPrefixedDBkey(),
332 'lastTimestamp' => $lastTimestamp,
333 'oldSize' => $oldSize,
334 'newSize' => $newSize,
341 * Makes an entry in the database corresponding to page creation
342 * Note: the title object must be loaded with the new id using resetArticleID()
343 * @todo Document parameters and return
345 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
346 $ip='', $size = 0, $newId = 0 )
355 $rc = new RecentChange
;
356 $rc->mAttribs
= array(
357 'rc_timestamp' => $timestamp,
358 'rc_cur_time' => $timestamp,
359 'rc_namespace' => $title->getNamespace(),
360 'rc_title' => $title->getDBkey(),
362 'rc_minor' => $minor ?
1 : 0,
363 'rc_cur_id' => $title->getArticleID(),
364 'rc_user' => $user->getId(),
365 'rc_user_text' => $user->getName(),
366 'rc_comment' => $comment,
367 'rc_this_oldid' => $newId,
368 'rc_last_oldid' => 0,
369 'rc_bot' => $bot ?
1 : 0,
370 'rc_moved_to_ns' => 0,
371 'rc_moved_to_title' => '',
374 'rc_new' => 1, # obsolete
376 'rc_new_len' => $size,
379 'rc_log_type' => null,
380 'rc_log_action' => '',
385 'prefixedDBkey' => $title->getPrefixedDBkey(),
386 'lastTimestamp' => 0,
394 # Makes an entry in the database corresponding to a rename
395 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
406 $rc = new RecentChange
;
407 $rc->mAttribs
= array(
408 'rc_timestamp' => $timestamp,
409 'rc_cur_time' => $timestamp,
410 'rc_namespace' => $oldTitle->getNamespace(),
411 'rc_title' => $oldTitle->getDBkey(),
412 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
414 'rc_cur_id' => $oldTitle->getArticleID(),
415 'rc_user' => $user->getId(),
416 'rc_user_text' => $user->getName(),
417 'rc_comment' => $comment,
418 'rc_this_oldid' => 0,
419 'rc_last_oldid' => 0,
420 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
421 'rc_moved_to_ns' => $newTitle->getNamespace(),
422 'rc_moved_to_title' => $newTitle->getDBkey(),
424 'rc_new' => 0, # obsolete
426 'rc_old_len' => NULL,
427 'rc_new_len' => NULL,
429 'rc_logid' => 0, # notifyMove not used anymore
430 'rc_log_type' => null,
431 'rc_log_action' => '',
436 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
437 'lastTimestamp' => 0,
438 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
443 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
444 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
447 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
448 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
451 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='',
452 $type, $action, $target, $logComment, $params, $newId=0 )
463 $rc = new RecentChange
;
464 $rc->mAttribs
= array(
465 'rc_timestamp' => $timestamp,
466 'rc_cur_time' => $timestamp,
467 'rc_namespace' => $target->getNamespace(),
468 'rc_title' => $target->getDBkey(),
471 'rc_cur_id' => $target->getArticleID(),
472 'rc_user' => $user->getId(),
473 'rc_user_text' => $user->getName(),
474 'rc_comment' => $logComment,
475 'rc_this_oldid' => 0,
476 'rc_last_oldid' => 0,
477 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot', true ) : 0,
478 'rc_moved_to_ns' => 0,
479 'rc_moved_to_title' => '',
482 'rc_new' => 0, # obsolete
483 'rc_old_len' => NULL,
484 'rc_new_len' => NULL,
486 'rc_logid' => $newId,
487 'rc_log_type' => $type,
488 'rc_log_action' => $action,
489 'rc_params' => $params
492 'prefixedDBkey' => $title->getPrefixedDBkey(),
493 'lastTimestamp' => 0,
494 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
499 # Initialises the members of this object from a mysql row object
500 function loadFromRow( $row )
502 $this->mAttribs
= get_object_vars( $row );
503 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
504 $this->mExtra
= array();
507 # Makes a pseudo-RC entry from a cur row
508 function loadFromCurRow( $row )
510 $this->mAttribs
= array(
511 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
512 'rc_cur_time' => $row->rev_timestamp
,
513 'rc_user' => $row->rev_user
,
514 'rc_user_text' => $row->rev_user_text
,
515 'rc_namespace' => $row->page_namespace
,
516 'rc_title' => $row->page_title
,
517 'rc_comment' => $row->rev_comment
,
518 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
519 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
520 'rc_cur_id' => $row->page_id
,
521 'rc_this_oldid' => $row->rev_id
,
522 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
524 'rc_moved_to_ns' => 0,
525 'rc_moved_to_title' => '',
527 'rc_id' => $row->rc_id
,
528 'rc_patrolled' => $row->rc_patrolled
,
529 'rc_new' => $row->page_is_new
, # obsolete
530 'rc_old_len' => $row->rc_old_len
,
531 'rc_new_len' => $row->rc_new_len
,
532 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
533 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
534 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
535 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
536 // this one REALLY should be set...
537 'rc_deleted' => isset($row->rc_deleted
) ?
$row->rc_deleted
: 0,
540 $this->mExtra
= array();
544 * Get an attribute value
546 * @param $name Attribute name
549 public function getAttribute( $name ) {
550 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
554 * Gets the end part of the diff URL associated with this object
555 * Blank if no diff link should be displayed
557 function diffLinkTrail( $forceCur )
559 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
560 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
561 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
563 $trail .= '&diff=0' ;
565 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
573 function getIRCLine() {
574 global $wgUseRCPatrol;
576 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
577 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
578 extract($this->mAttribs
);
579 extract($this->mExtra
);
581 if ( $rc_type == RC_LOG
) {
582 $titleObj = Title
::newFromText( "Log/$rc_log_type", NS_SPECIAL
);
584 $titleObj =& $this->getTitle();
586 $title = $titleObj->getPrefixedText();
587 $title = UDP
::cleanupForIRC( $title );
589 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
590 if ( $rc_type == RC_LOG
) {
592 } elseif ( $rc_new && $wgUseRCPatrol ) {
593 $url = $titleObj->getInternalURL("rcid=$rc_id");
594 } else if ( $rc_new ) {
595 $url = $titleObj->getInternalURL();
596 } else if ( $wgUseRCPatrol ) {
597 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
599 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
602 if ( isset( $oldSize ) && isset( $newSize ) ) {
603 $szdiff = $newSize - $oldSize;
604 if ($szdiff < -500) {
605 $szdiff = "\002$szdiff\002";
606 } elseif ($szdiff >= 0) {
607 $szdiff = '+' . $szdiff ;
609 $szdiff = '(' . $szdiff . ')' ;
614 $user = UDP
::cleanupForIRC( $rc_user_text );
616 if ( $rc_type == RC_LOG
) {
617 $targetText = $this->getTitle()->getPrefixedText();
618 $comment = UDP
::cleanupForIRC( str_replace("[[$targetText]]","[[\00302$targetText\00310]]",$actionComment) );
619 $flag = $rc_log_action;
621 $comment = UDP
::cleanupForIRC( $rc_comment );
622 $flag = ($rc_new ?
"N" : "") . ($rc_minor ?
"M" : "") . ($rc_bot ?
"B" : "");
624 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
625 # no colour (\003) switches back to the term default
626 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
627 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
632 * Returns the change size (HTML).
633 * The lengths can be given optionally.
635 function getCharacterDifference( $old = 0, $new = 0 ) {
636 global $wgRCChangedSizeThreshold, $wgLang;
639 $old = $this->mAttribs
['rc_old_len'];
642 $new = $this->mAttribs
['rc_new_len'];
645 if( $old === NULL ||
$new === NULL ) {
649 $szdiff = $new - $old;
650 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
651 $wgLang->formatNum($szdiff) );
654 if( abs( $szdiff ) > abs( $wgRCChangedSizeThreshold ) ) {
661 if( $szdiff === 0 ) {
662 return "<$tag class='mw-plusminus-null'>($formatedSize)</$tag>";
663 } elseif( $szdiff > 0 ) {
664 return "<$tag class='mw-plusminus-pos'>(+$formatedSize)</$tag>";
666 return "<$tag class='mw-plusminus-neg'>($formatedSize)</$tag>";