b1c70d8d5ab3a2a5d3e0e436aa78a8f9c19cd394
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 # Update old rows, if necessary
178 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
179 $lastTime = $this->mExtra
['lastTimestamp'];
180 #$now = $this->mAttribs['rc_timestamp'];
181 #$curId = $this->mAttribs['rc_cur_id'];
183 # Don't bother looking for entries that have probably
184 # been purged, it just locks up the indexes needlessly.
186 $age = time() - wfTimestamp( TS_UNIX
, $lastTime );
187 if( $age < $wgRCMaxAge ) {
188 # live hack, will commit once tested - kate
189 # Update rc_this_oldid for the entries which were current
191 #$oldid = $this->mAttribs['rc_last_oldid'];
192 #$ns = $this->mAttribs['rc_namespace'];
193 #$title = $this->mAttribs['rc_title'];
195 #$dbw->update( 'recentchanges',
197 # 'rc_this_oldid' => $oldid
198 # ), array( /* WHERE */
199 # 'rc_namespace' => $ns,
200 # 'rc_title' => $title,
201 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
207 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
208 # array( 'rc_cur_id' => $curId ), $fname );
211 # Notify external application via UDP
212 if ( $wgRC2UDPAddress && ( !$this->mAttribs
['rc_bot'] ||
!$wgRC2UDPOmitBots ) ) {
213 wfRecentChange2UDP( $this->getIRCLine() );
216 # E-mail notifications
217 global $wgUseEnotif, $wgShowUpdatedMarker, $wgUser;
218 if( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
220 if( $this->mAttribs
['rc_user'] ) {
221 $editor = ($wgUser->getId() == $this->mAttribs
['rc_user']) ?
222 $wgUser : User
::newFromID( $this->mAttribs
['rc_user'] );
225 $editor = ($wgUser->getName() == $this->mAttribs
['rc_user_text']) ?
226 $wgUser : User
::newFromName( $this->mAttribs
['rc_user_text'], false );
228 # FIXME: this would be better as an extension hook
229 $enotif = new EmailNotification();
230 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
231 $enotif->notifyOnPageChange( $editor, $title,
232 $this->mAttribs
['rc_timestamp'],
233 $this->mAttribs
['rc_comment'],
234 $this->mAttribs
['rc_minor'],
235 $this->mAttribs
['rc_last_oldid'] );
239 wfRunHooks( 'RecentChange_save', array( &$this ) );
243 * Mark a given change as patrolled
245 * @param mixed $change RecentChange or corresponding rc_id
246 * @param bool $auto for automatic patrol
247 * @return See doMarkPatrolled(), or null if $change is not an existing rc_id
249 public static function markPatrolled( $change, $auto = false ) {
250 $change = $change instanceof RecentChange
252 : RecentChange
::newFromId($change);
253 if( !$change instanceof RecentChange
) {
256 return $change->doMarkPatrolled( $auto );
260 * Mark this RecentChange as patrolled
262 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
263 * @param bool $auto for automatic patrol
264 * @return array of permissions errors, see Title::getUserPermissionsErrors()
266 public function doMarkPatrolled( $auto = false ) {
267 global $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
269 // If recentchanges patrol is disabled, only new pages
272 && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) )
274 $errors[] = array('rcpatroldisabled');
277 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
278 $right = $auto ?
'autopatrol' : 'patrol';
279 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $wgUser ) );
280 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$wgUser, false)) )
281 $errors[] = array('hookaborted');
283 // Users without the 'autopatrol' right can't patrol their
285 if( $wgUser->getName() == $this->getAttribute('rc_user_text') && !$wgUser->isAllowed('autopatrol') )
286 $errors[] = array('markedaspatrollederror-noautopatrol');
292 // If the change was patrolled already, do nothing
293 if( $this->getAttribute('rc_patrolled') )
296 // Actually set the 'patrolled' flag in RC
297 $this->reallyMarkPatrolled();
299 // Log this patrol event
300 PatrolLog
::record( $this, $auto );
301 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$wgUser, false) );
306 * Mark this RecentChange patrolled, without error checking
307 * @return int Number of affected rows
309 public function reallyMarkPatrolled() {
310 $dbw = wfGetDB( DB_MASTER
);
317 'rc_id' => $this->getAttribute('rc_id')
321 return $dbw->affectedRows();
324 # Makes an entry in the database corresponding to an edit
325 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
326 $oldId, $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0)
335 $rc = new RecentChange
;
336 $rc->mAttribs
= array(
337 'rc_timestamp' => $timestamp,
338 'rc_cur_time' => $timestamp,
339 'rc_namespace' => $title->getNamespace(),
340 'rc_title' => $title->getDBkey(),
341 'rc_type' => RC_EDIT
,
342 'rc_minor' => $minor ?
1 : 0,
343 'rc_cur_id' => $title->getArticleID(),
344 'rc_user' => $user->getId(),
345 'rc_user_text' => $user->getName(),
346 'rc_comment' => $comment,
347 'rc_this_oldid' => $newId,
348 'rc_last_oldid' => $oldId,
349 'rc_bot' => $bot ?
1 : 0,
350 'rc_moved_to_ns' => 0,
351 'rc_moved_to_title' => '',
354 'rc_new' => 0, # obsolete
355 'rc_old_len' => $oldSize,
356 'rc_new_len' => $newSize,
359 'rc_log_type' => null,
360 'rc_log_action' => '',
365 'prefixedDBkey' => $title->getPrefixedDBkey(),
366 'lastTimestamp' => $lastTimestamp,
367 'oldSize' => $oldSize,
368 'newSize' => $newSize,
375 * Makes an entry in the database corresponding to page creation
376 * Note: the title object must be loaded with the new id using resetArticleID()
377 * @todo Document parameters and return
379 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
380 $ip='', $size = 0, $newId = 0 )
389 $rc = new RecentChange
;
390 $rc->mAttribs
= array(
391 'rc_timestamp' => $timestamp,
392 'rc_cur_time' => $timestamp,
393 'rc_namespace' => $title->getNamespace(),
394 'rc_title' => $title->getDBkey(),
396 'rc_minor' => $minor ?
1 : 0,
397 'rc_cur_id' => $title->getArticleID(),
398 'rc_user' => $user->getId(),
399 'rc_user_text' => $user->getName(),
400 'rc_comment' => $comment,
401 'rc_this_oldid' => $newId,
402 'rc_last_oldid' => 0,
403 'rc_bot' => $bot ?
1 : 0,
404 'rc_moved_to_ns' => 0,
405 'rc_moved_to_title' => '',
408 'rc_new' => 1, # obsolete
410 'rc_new_len' => $size,
413 'rc_log_type' => null,
414 'rc_log_action' => '',
419 'prefixedDBkey' => $title->getPrefixedDBkey(),
420 'lastTimestamp' => 0,
428 # Makes an entry in the database corresponding to a rename
429 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
440 $rc = new RecentChange
;
441 $rc->mAttribs
= array(
442 'rc_timestamp' => $timestamp,
443 'rc_cur_time' => $timestamp,
444 'rc_namespace' => $oldTitle->getNamespace(),
445 'rc_title' => $oldTitle->getDBkey(),
446 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
448 'rc_cur_id' => $oldTitle->getArticleID(),
449 'rc_user' => $user->getId(),
450 'rc_user_text' => $user->getName(),
451 'rc_comment' => $comment,
452 'rc_this_oldid' => 0,
453 'rc_last_oldid' => 0,
454 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
455 'rc_moved_to_ns' => $newTitle->getNamespace(),
456 'rc_moved_to_title' => $newTitle->getDBkey(),
458 'rc_new' => 0, # obsolete
460 'rc_old_len' => NULL,
461 'rc_new_len' => NULL,
463 'rc_logid' => 0, # notifyMove not used anymore
464 'rc_log_type' => null,
465 'rc_log_action' => '',
470 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
471 'lastTimestamp' => 0,
472 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
477 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
478 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
481 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
482 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
485 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip='',
486 $type, $action, $target, $logComment, $params, $newId=0 )
497 $rc = new RecentChange
;
498 $rc->mAttribs
= array(
499 'rc_timestamp' => $timestamp,
500 'rc_cur_time' => $timestamp,
501 'rc_namespace' => $target->getNamespace(),
502 'rc_title' => $target->getDBkey(),
505 'rc_cur_id' => $target->getArticleID(),
506 'rc_user' => $user->getId(),
507 'rc_user_text' => $user->getName(),
508 'rc_comment' => $logComment,
509 'rc_this_oldid' => 0,
510 'rc_last_oldid' => 0,
511 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
512 'rc_moved_to_ns' => 0,
513 'rc_moved_to_title' => '',
516 'rc_new' => 0, # obsolete
517 'rc_old_len' => NULL,
518 'rc_new_len' => NULL,
520 'rc_logid' => $newId,
521 'rc_log_type' => $type,
522 'rc_log_action' => $action,
523 'rc_params' => $params
526 'prefixedDBkey' => $title->getPrefixedDBkey(),
527 'lastTimestamp' => 0,
528 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
533 # Initialises the members of this object from a mysql row object
534 function loadFromRow( $row )
536 $this->mAttribs
= get_object_vars( $row );
537 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
538 $this->mExtra
= array();
541 # Makes a pseudo-RC entry from a cur row
542 function loadFromCurRow( $row )
544 $this->mAttribs
= array(
545 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
546 'rc_cur_time' => $row->rev_timestamp
,
547 'rc_user' => $row->rev_user
,
548 'rc_user_text' => $row->rev_user_text
,
549 'rc_namespace' => $row->page_namespace
,
550 'rc_title' => $row->page_title
,
551 'rc_comment' => $row->rev_comment
,
552 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
553 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
554 'rc_cur_id' => $row->page_id
,
555 'rc_this_oldid' => $row->rev_id
,
556 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
558 'rc_moved_to_ns' => 0,
559 'rc_moved_to_title' => '',
561 'rc_id' => $row->rc_id
,
562 'rc_patrolled' => $row->rc_patrolled
,
563 'rc_new' => $row->page_is_new
, # obsolete
564 'rc_old_len' => $row->rc_old_len
,
565 'rc_new_len' => $row->rc_new_len
,
566 'rc_params' => isset($row->rc_params
) ?
$row->rc_params
: '',
567 'rc_log_type' => isset($row->rc_log_type
) ?
$row->rc_log_type
: null,
568 'rc_log_action' => isset($row->rc_log_action
) ?
$row->rc_log_action
: null,
569 'rc_log_id' => isset($row->rc_log_id
) ?
$row->rc_log_id
: 0,
570 // this one REALLY should be set...
571 'rc_deleted' => isset($row->rc_deleted
) ?
$row->rc_deleted
: 0,
574 $this->mExtra
= array();
578 * Get an attribute value
580 * @param $name Attribute name
583 public function getAttribute( $name ) {
584 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
588 * Gets the end part of the diff URL associated with this object
589 * Blank if no diff link should be displayed
591 function diffLinkTrail( $forceCur )
593 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
594 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
595 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
597 $trail .= '&diff=0' ;
599 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
607 function cleanupForIRC( $text ) {
608 return str_replace(array("\n", "\r"), array("", ""), $text);
611 function getIRCLine() {
612 global $wgUseRCPatrol;
614 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
615 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
616 extract($this->mAttribs
);
617 extract($this->mExtra
);
619 if ( $rc_type == RC_LOG
) {
620 $titleObj = Title
::newFromText( "Log/$rc_log_type", NS_SPECIAL
);
622 $titleObj =& $this->getTitle();
624 $title = $titleObj->getPrefixedText();
625 $title = $this->cleanupForIRC( $title );
627 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
628 if ( $rc_type == RC_LOG
) {
630 } elseif ( $rc_new && $wgUseRCPatrol ) {
631 $url = $titleObj->getInternalURL("rcid=$rc_id");
632 } else if ( $rc_new ) {
633 $url = $titleObj->getInternalURL();
634 } else if ( $wgUseRCPatrol ) {
635 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
637 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
640 if ( isset( $oldSize ) && isset( $newSize ) ) {
641 $szdiff = $newSize - $oldSize;
642 if ($szdiff < -500) {
643 $szdiff = "\002$szdiff\002";
644 } elseif ($szdiff >= 0) {
645 $szdiff = '+' . $szdiff ;
647 $szdiff = '(' . $szdiff . ')' ;
652 $user = $this->cleanupForIRC( $rc_user_text );
654 if ( $rc_type == RC_LOG
) {
655 $logTargetText = $this->getTitle()->getPrefixedText();
656 $comment = $this->cleanupForIRC( str_replace($logTargetText,"\00302$logTargetText\00310",$actionComment) );
657 $flag = $rc_log_action;
659 $comment = $this->cleanupForIRC( $rc_comment );
660 $flag = ($rc_new ?
"N" : "") . ($rc_minor ?
"M" : "") . ($rc_bot ?
"B" : "");
662 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
663 # no colour (\003) switches back to the term default
664 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
665 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
670 * Returns the change size (HTML).
671 * The lengths can be given optionally.
673 function getCharacterDifference( $old = 0, $new = 0 ) {
674 global $wgRCChangedSizeThreshold, $wgLang;
677 $old = $this->mAttribs
['rc_old_len'];
680 $new = $this->mAttribs
['rc_new_len'];
683 if( $old === NULL ||
$new === NULL ) {
687 $szdiff = $new - $old;
688 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
689 $wgLang->formatNum($szdiff) );
691 if( $szdiff < $wgRCChangedSizeThreshold ) {
692 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
693 } elseif( $szdiff === 0 ) {
694 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
695 } elseif( $szdiff > 0 ) {
696 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
698 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';