7 * Utility class for creating new RC entries
9 * rc_id id of the row in the recentchanges table
10 * rc_timestamp time the entry was made
11 * rc_cur_time timestamp on the cur row
12 * rc_namespace namespace #
13 * rc_title non-prefixed db key
14 * rc_type is new entry, used to determine whether updating is necessary
16 * rc_cur_id page_id of associated page entry
17 * rc_user user id who made the entry
18 * rc_user_text user name who made the entry
19 * rc_comment edit summary
20 * rc_this_oldid rev_id associated with this entry (or zero)
21 * rc_last_oldid rev_id associated with the entry before this one (or zero)
22 * rc_bot is bot, hidden
23 * rc_ip IP address of the user in dotted quad notation
24 * rc_new obsolete, use rc_type==RC_NEW
25 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
26 * rc_old_len integer byte length of the text before the edit
27 * rc_new_len the same after the edit
30 * prefixedDBkey prefixed db key, used by external app via msg queue
31 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
32 * lang the interwiki prefix, automatically set in save()
33 * oldSize text size before the change
34 * newSize text size after the change
36 * temporary: not stored in the database
37 * notificationtimestamp
38 * numberofWatchingusers
40 * @todo document functions and variables
44 var $mAttribs = array(), $mExtra = array();
45 var $mTitle = false, $mMovedToTitle = false;
46 var $numberofWatchingusers = 0 ; # Dummy to prevent error message in SpecialRecentchangeslinked
50 public static function newFromRow( $row )
52 $rc = new RecentChange
;
53 $rc->loadFromRow( $row );
57 public static function newFromCurRow( $row, $rc_this_oldid = 0 )
59 $rc = new RecentChange
;
60 $rc->loadFromCurRow( $row, $rc_this_oldid );
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 )
113 $this->mAttribs
= $attribs;
116 function setExtra( $extra )
118 $this->mExtra
= $extra;
123 if ( $this->mTitle
=== false ) {
124 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
126 return $this->mTitle
;
129 function getMovedToTitle()
131 if ( $this->mMovedToTitle
=== false ) {
132 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
133 $this->mAttribs
['rc_moved_to_title'] );
135 return $this->mMovedToTitle
;
138 # Writes the data in this object to the database
141 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix;
142 $fname = 'RecentChange::save';
144 $dbw = wfGetDB( DB_MASTER
);
145 if ( !is_array($this->mExtra
) ) {
146 $this->mExtra
= array();
148 $this->mExtra
['lang'] = $wgLocalInterwiki;
150 if ( !$wgPutIPinRC ) {
151 $this->mAttribs
['rc_ip'] = '';
154 ## If our database is strict about IP addresses, use NULL instead of an empty string
155 if ( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
156 unset( $this->mAttribs
['rc_ip'] );
159 # Fixup database timestamps
160 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
161 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
162 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
164 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
165 if ( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
166 unset ( $this->mAttribs
['rc_cur_id'] );
170 $dbw->insert( 'recentchanges', $this->mAttribs
, $fname );
173 $this->mAttribs
['rc_id'] = $dbw->insertId();
175 # Update old rows, if necessary
176 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
177 $lastTime = $this->mExtra
['lastTimestamp'];
178 #$now = $this->mAttribs['rc_timestamp'];
179 #$curId = $this->mAttribs['rc_cur_id'];
181 # Don't bother looking for entries that have probably
182 # been purged, it just locks up the indexes needlessly.
184 $age = time() - wfTimestamp( TS_UNIX
, $lastTime );
185 if( $age < $wgRCMaxAge ) {
186 # live hack, will commit once tested - kate
187 # Update rc_this_oldid for the entries which were current
189 #$oldid = $this->mAttribs['rc_last_oldid'];
190 #$ns = $this->mAttribs['rc_namespace'];
191 #$title = $this->mAttribs['rc_title'];
193 #$dbw->update( 'recentchanges',
195 # 'rc_this_oldid' => $oldid
196 # ), array( /* WHERE */
197 # 'rc_namespace' => $ns,
198 # 'rc_title' => $title,
199 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
205 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
206 # array( 'rc_cur_id' => $curId ), $fname );
209 # Notify external application via UDP
210 if ( $wgRC2UDPAddress ) {
211 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
213 $line = $wgRC2UDPPrefix . $this->getIRCLine();
214 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
215 socket_close( $conn );
219 # E-mail notifications
222 # this would be better as an extension hook
224 $enotif = new EmailNotification
;
225 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
226 $enotif->notifyOnPageChange( $wgUser, $title,
227 $this->mAttribs
['rc_timestamp'],
228 $this->mAttribs
['rc_comment'],
229 $this->mAttribs
['rc_minor'],
230 $this->mAttribs
['rc_last_oldid'] );
234 wfRunHooks( 'RecentChange_save', array( &$this ) );
238 * Mark a given change as patrolled
240 * @param mixed $change RecentChange or corresponding rc_id
242 public static function markPatrolled( $change ) {
243 $rcid = $change instanceof RecentChange
244 ?
$change->mAttribs
['rc_id']
246 $dbw = wfGetDB( DB_MASTER
);
259 # Makes an entry in the database corresponding to an edit
260 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
261 $oldId, $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0,
274 $rc = new RecentChange
;
275 $rc->mAttribs
= array(
276 'rc_timestamp' => $timestamp,
277 'rc_cur_time' => $timestamp,
278 'rc_namespace' => $title->getNamespace(),
279 'rc_title' => $title->getDBkey(),
280 'rc_type' => RC_EDIT
,
281 'rc_minor' => $minor ?
1 : 0,
282 'rc_cur_id' => $title->getArticleID(),
283 'rc_user' => $user->getID(),
284 'rc_user_text' => $user->getName(),
285 'rc_comment' => $comment,
286 'rc_this_oldid' => $newId,
287 'rc_last_oldid' => $oldId,
288 'rc_bot' => $bot ?
1 : 0,
289 'rc_moved_to_ns' => 0,
290 'rc_moved_to_title' => '',
293 'rc_new' => 0, # obsolete
294 'rc_old_len' => $oldSize,
295 'rc_new_len' => $newSize
299 'prefixedDBkey' => $title->getPrefixedDBkey(),
300 'lastTimestamp' => $lastTimestamp,
301 'oldSize' => $oldSize,
302 'newSize' => $newSize,
305 return( $rc->mAttribs
['rc_id'] );
309 * Makes an entry in the database corresponding to page creation
310 * Note: the title object must be loaded with the new id using resetArticleID()
311 * @todo Document parameters and return
313 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
314 $ip='', $size = 0, $newId = 0 )
323 $rc = new RecentChange
;
324 $rc->mAttribs
= array(
325 'rc_timestamp' => $timestamp,
326 'rc_cur_time' => $timestamp,
327 'rc_namespace' => $title->getNamespace(),
328 'rc_title' => $title->getDBkey(),
330 'rc_minor' => $minor ?
1 : 0,
331 'rc_cur_id' => $title->getArticleID(),
332 'rc_user' => $user->getID(),
333 'rc_user_text' => $user->getName(),
334 'rc_comment' => $comment,
335 'rc_this_oldid' => $newId,
336 'rc_last_oldid' => 0,
337 'rc_bot' => $bot ?
1 : 0,
338 'rc_moved_to_ns' => 0,
339 'rc_moved_to_title' => '',
342 'rc_new' => 1, # obsolete
344 'rc_new_len' => $size
348 'prefixedDBkey' => $title->getPrefixedDBkey(),
349 'lastTimestamp' => 0,
354 return( $rc->mAttribs
['rc_id'] );
357 # Makes an entry in the database corresponding to a rename
358 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
369 $rc = new RecentChange
;
370 $rc->mAttribs
= array(
371 'rc_timestamp' => $timestamp,
372 'rc_cur_time' => $timestamp,
373 'rc_namespace' => $oldTitle->getNamespace(),
374 'rc_title' => $oldTitle->getDBkey(),
375 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
377 'rc_cur_id' => $oldTitle->getArticleID(),
378 'rc_user' => $user->getID(),
379 'rc_user_text' => $user->getName(),
380 'rc_comment' => $comment,
381 'rc_this_oldid' => 0,
382 'rc_last_oldid' => 0,
383 'rc_bot' => $user->isAllowed( 'bot' ) ?
$wgRequest->getBool( 'bot' , true ) : 0,
384 'rc_moved_to_ns' => $newTitle->getNamespace(),
385 'rc_moved_to_title' => $newTitle->getDBkey(),
387 'rc_new' => 0, # obsolete
389 'rc_old_len' => NULL,
390 'rc_new_len' => NULL,
394 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
395 'lastTimestamp' => 0,
396 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
401 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
402 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
405 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
406 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
409 # A log entry is different to an edit in that previous revisions are
411 public static function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
412 $type, $action, $target, $logComment, $params )
423 $rc = new RecentChange
;
424 $rc->mAttribs
= array(
425 'rc_timestamp' => $timestamp,
426 'rc_cur_time' => $timestamp,
427 'rc_namespace' => $title->getNamespace(),
428 'rc_title' => $title->getDBkey(),
431 'rc_cur_id' => $title->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' => 0,
439 'rc_moved_to_title' => '',
442 'rc_new' => 0, # obsolete
443 'rc_old_len' => NULL,
444 'rc_new_len' => NULL,
447 'prefixedDBkey' => $title->getPrefixedDBkey(),
448 'lastTimestamp' => 0,
450 'logAction' => $action,
451 'logComment' => $logComment,
452 'logTarget' => $target,
453 'logParams' => $params
458 # Initialises the members of this object from a mysql row object
459 function loadFromRow( $row )
461 $this->mAttribs
= get_object_vars( $row );
462 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
463 $this->mExtra
= array();
466 # Makes a pseudo-RC entry from a cur row
467 function loadFromCurRow( $row )
469 $this->mAttribs
= array(
470 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
471 'rc_cur_time' => $row->rev_timestamp
,
472 'rc_user' => $row->rev_user
,
473 'rc_user_text' => $row->rev_user_text
,
474 'rc_namespace' => $row->page_namespace
,
475 'rc_title' => $row->page_title
,
476 'rc_comment' => $row->rev_comment
,
477 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
478 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
479 'rc_cur_id' => $row->page_id
,
480 'rc_this_oldid' => $row->rev_id
,
481 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
483 'rc_moved_to_ns' => 0,
484 'rc_moved_to_title' => '',
486 'rc_id' => $row->rc_id
,
487 'rc_patrolled' => $row->rc_patrolled
,
488 'rc_new' => $row->page_is_new
, # obsolete
489 'rc_old_len' => $row->rc_old_len
,
490 'rc_new_len' => $row->rc_new_len
,
493 $this->mExtra
= array();
497 * Get an attribute value
499 * @param $name Attribute name
502 public function getAttribute( $name ) {
503 return isset( $this->mAttribs
[$name] ) ?
$this->mAttribs
[$name] : NULL;
507 * Gets the end part of the diff URL associated with this object
508 * Blank if no diff link should be displayed
510 function diffLinkTrail( $forceCur )
512 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
513 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
514 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
516 $trail .= '&diff=0' ;
518 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
526 function cleanupForIRC( $text ) {
527 return str_replace(array("\n", "\r"), array("", ""), $text);
530 function getIRCLine() {
531 global $wgUseRCPatrol;
533 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
534 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
535 extract($this->mAttribs
);
536 extract($this->mExtra
);
538 $titleObj =& $this->getTitle();
539 if ( $rc_type == RC_LOG
) {
540 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
542 $title = $titleObj->getPrefixedText();
544 $title = $this->cleanupForIRC( $title );
546 $bad = array("\n", "\r");
547 $empty = array("", "");
548 $title = $titleObj->getPrefixedText();
549 $title = str_replace($bad, $empty, $title);
551 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
552 if ( $rc_type == RC_LOG
) {
554 } elseif ( $rc_new && $wgUseRCPatrol ) {
555 $url = $titleObj->getInternalURL("rcid=$rc_id");
556 } else if ( $rc_new ) {
557 $url = $titleObj->getInternalURL();
558 } else if ( $wgUseRCPatrol ) {
559 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
561 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
564 if ( isset( $oldSize ) && isset( $newSize ) ) {
565 $szdiff = $newSize - $oldSize;
566 if ($szdiff < -500) {
567 $szdiff = "\002$szdiff\002";
568 } elseif ($szdiff >= 0) {
569 $szdiff = '+' . $szdiff ;
571 $szdiff = '(' . $szdiff . ')' ;
576 $user = $this->cleanupForIRC( $rc_user_text );
578 if ( $rc_type == RC_LOG
) {
579 $logTargetText = $logTarget->getPrefixedText();
580 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
583 $comment = $this->cleanupForIRC( $rc_comment );
584 $flag = ($rc_minor ?
"M" : "") . ($rc_new ?
"N" : "");
586 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
587 # no colour (\003) switches back to the term default
588 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
589 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
594 * Returns the change size (HTML).
595 * The lengths can be given optionally.
597 function getCharacterDifference( $old = 0, $new = 0 ) {
598 global $wgRCChangedSizeThreshold, $wgLang;
601 $old = $this->mAttribs
['rc_old_len'];
604 $new = $this->mAttribs
['rc_new_len'];
607 if( $old === NULL ||
$new === NULL ) {
611 $szdiff = $new - $old;
612 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
613 $wgLang->formatNum($szdiff) );
615 if( $szdiff < $wgRCChangedSizeThreshold ) {
616 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
617 } elseif( $szdiff === 0 ) {
618 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
619 } elseif( $szdiff > 0 ) {
620 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
622 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';