8 * Utility class for creating new RC entries
10 * rc_id id of the row in the recentchanges table
11 * rc_timestamp time the entry was made
12 * rc_cur_time timestamp on the cur row
13 * rc_namespace namespace #
14 * rc_title non-prefixed db key
15 * rc_type is new entry, used to determine whether updating is necessary
17 * rc_cur_id page_id of associated page entry
18 * rc_user user id who made the entry
19 * rc_user_text user name who made the entry
20 * rc_comment edit summary
21 * rc_this_oldid rev_id associated with this entry (or zero)
22 * rc_last_oldid rev_id associated with the entry before this one (or zero)
23 * rc_bot is bot, hidden
24 * rc_ip IP address of the user in dotted quad notation
25 * rc_new obsolete, use rc_type==RC_NEW
26 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
29 * prefixedDBkey prefixed db key, used by external app via msg queue
30 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
31 * lang the interwiki prefix, automatically set in save()
32 * oldSize text size before the change
33 * newSize text size after the change
35 * temporary: not stored in the database
36 * notificationtimestamp
37 * numberofWatchingusers
39 * @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 /* static */ function newFromRow( $row )
52 $rc = new RecentChange
;
53 $rc->loadFromRow( $row );
57 /* 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;
68 function setAttribs( $attribs )
70 $this->mAttribs
= $attribs;
73 function setExtra( $extra )
75 $this->mExtra
= $extra;
80 if ( $this->mTitle
=== false ) {
81 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
86 function getMovedToTitle()
88 if ( $this->mMovedToTitle
=== false ) {
89 $this->mMovedToTitle
= Title
::makeTitle( $this->mAttribs
['rc_moved_to_ns'],
90 $this->mAttribs
['rc_moved_to_title'] );
92 return $this->mMovedToTitle
;
95 # Writes the data in this object to the database
98 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix, $wgUseRCPatrol;
99 $fname = 'RecentChange::save';
101 $dbw =& wfGetDB( DB_MASTER
);
102 if ( !is_array($this->mExtra
) ) {
103 $this->mExtra
= array();
105 $this->mExtra
['lang'] = $wgLocalInterwiki;
107 if ( !$wgPutIPinRC ) {
108 $this->mAttribs
['rc_ip'] = '';
111 ## If our database is strict about IP addresses, use NULL instead of an empty string
112 if ( $dbw->strictIPs() and $this->mAttribs
['rc_ip'] == '' ) {
113 unset( $this->mAttribs
['rc_ip'] );
116 # Fixup database timestamps
117 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp($this->mAttribs
['rc_timestamp']);
118 $this->mAttribs
['rc_cur_time'] = $dbw->timestamp($this->mAttribs
['rc_cur_time']);
119 $this->mAttribs
['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
121 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
122 if ( $dbw->cascadingDeletes() and $this->mAttribs
['rc_cur_id']==0 ) {
123 unset ( $this->mAttribs
['rc_cur_id'] );
127 $dbw->insert( 'recentchanges', $this->mAttribs
, $fname );
130 $this->mAttribs
['rc_id'] = $dbw->insertId();
132 # Update old rows, if necessary
133 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
134 $lastTime = $this->mExtra
['lastTimestamp'];
135 #$now = $this->mAttribs['rc_timestamp'];
136 #$curId = $this->mAttribs['rc_cur_id'];
138 # Don't bother looking for entries that have probably
139 # been purged, it just locks up the indexes needlessly.
141 $age = time() - wfTimestamp( TS_UNIX
, $lastTime );
142 if( $age < $wgRCMaxAge ) {
143 # live hack, will commit once tested - kate
144 # Update rc_this_oldid for the entries which were current
146 #$oldid = $this->mAttribs['rc_last_oldid'];
147 #$ns = $this->mAttribs['rc_namespace'];
148 #$title = $this->mAttribs['rc_title'];
150 #$dbw->update( 'recentchanges',
152 # 'rc_this_oldid' => $oldid
153 # ), array( /* WHERE */
154 # 'rc_namespace' => $ns,
155 # 'rc_title' => $title,
156 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
162 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
163 # array( 'rc_cur_id' => $curId ), $fname );
166 # Notify external application via UDP
167 if ( $wgRC2UDPAddress ) {
168 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
170 $line = $wgRC2UDPPrefix . $this->getIRCLine();
171 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
172 socket_close( $conn );
176 # E-mail notifications
179 # this would be better as an extension hook
180 include_once( "UserMailer.php" );
181 $enotif = new EmailNotification();
182 $title = Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
183 $enotif->notifyOnPageChange( $title,
184 $this->mAttribs
['rc_timestamp'],
185 $this->mAttribs
['rc_comment'],
186 $this->mAttribs
['rc_minor'],
187 $this->mAttribs
['rc_last_oldid'] );
191 wfRunHooks( 'RecentChange_save', array( &$this ) );
194 # Marks a certain row as patrolled
195 function markPatrolled( $rcid )
197 $fname = 'RecentChange::markPatrolled';
199 $dbw =& wfGetDB( DB_MASTER
);
201 $dbw->update( 'recentchanges',
204 ), array( /* WHERE */
210 # Makes an entry in the database corresponding to an edit
211 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
212 $oldId, $lastTimestamp, $bot = "default", $ip = '', $oldSize = 0, $newSize = 0,
215 if ( $bot === 'default' ) {
216 $bot = $user->isAllowed( 'bot' );
226 $rc = new RecentChange
;
227 $rc->mAttribs
= array(
228 'rc_timestamp' => $timestamp,
229 'rc_cur_time' => $timestamp,
230 'rc_namespace' => $title->getNamespace(),
231 'rc_title' => $title->getDBkey(),
232 'rc_type' => RC_EDIT
,
233 'rc_minor' => $minor ?
1 : 0,
234 'rc_cur_id' => $title->getArticleID(),
235 'rc_user' => $user->getID(),
236 'rc_user_text' => $user->getName(),
237 'rc_comment' => $comment,
238 'rc_this_oldid' => $newId,
239 'rc_last_oldid' => $oldId,
240 'rc_bot' => $bot ?
1 : 0,
241 'rc_moved_to_ns' => 0,
242 'rc_moved_to_title' => '',
245 'rc_new' => 0 # obsolete
249 'prefixedDBkey' => $title->getPrefixedDBkey(),
250 'lastTimestamp' => $lastTimestamp,
251 'oldSize' => $oldSize,
252 'newSize' => $newSize,
255 return( $rc->mAttribs
['rc_id'] );
259 * Makes an entry in the database corresponding to page creation
260 * Note: the title object must be loaded with the new id using resetArticleID()
261 * @todo Document parameters and return
265 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default",
266 $ip='', $size = 0, $newId = 0 )
274 if ( $bot == 'default' ) {
275 $bot = $user->isAllowed( 'bot' );
278 $rc = new RecentChange
;
279 $rc->mAttribs
= array(
280 'rc_timestamp' => $timestamp,
281 'rc_cur_time' => $timestamp,
282 'rc_namespace' => $title->getNamespace(),
283 'rc_title' => $title->getDBkey(),
285 'rc_minor' => $minor ?
1 : 0,
286 'rc_cur_id' => $title->getArticleID(),
287 'rc_user' => $user->getID(),
288 'rc_user_text' => $user->getName(),
289 'rc_comment' => $comment,
290 'rc_this_oldid' => $newId,
291 'rc_last_oldid' => 0,
292 'rc_bot' => $bot ?
1 : 0,
293 'rc_moved_to_ns' => 0,
294 'rc_moved_to_title' => '',
297 'rc_new' => 1 # obsolete
301 'prefixedDBkey' => $title->getPrefixedDBkey(),
302 'lastTimestamp' => 0,
307 return( $rc->mAttribs
['rc_id'] );
310 # Makes an entry in the database corresponding to a rename
311 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
320 $rc = new RecentChange
;
321 $rc->mAttribs
= array(
322 'rc_timestamp' => $timestamp,
323 'rc_cur_time' => $timestamp,
324 'rc_namespace' => $oldTitle->getNamespace(),
325 'rc_title' => $oldTitle->getDBkey(),
326 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT
: RC_MOVE
,
328 'rc_cur_id' => $oldTitle->getArticleID(),
329 'rc_user' => $user->getID(),
330 'rc_user_text' => $user->getName(),
331 'rc_comment' => $comment,
332 'rc_this_oldid' => 0,
333 'rc_last_oldid' => 0,
334 'rc_bot' => $user->isAllowed( 'bot' ) ?
1 : 0,
335 'rc_moved_to_ns' => $newTitle->getNamespace(),
336 'rc_moved_to_title' => $newTitle->getDBkey(),
338 'rc_new' => 0, # obsolete
343 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
344 'lastTimestamp' => 0,
345 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
350 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
351 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
354 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
355 RecentChange
::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
358 # A log entry is different to an edit in that previous revisions are
360 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
361 $type, $action, $target, $logComment, $params )
370 $rc = new RecentChange
;
371 $rc->mAttribs
= array(
372 'rc_timestamp' => $timestamp,
373 'rc_cur_time' => $timestamp,
374 'rc_namespace' => $title->getNamespace(),
375 'rc_title' => $title->getDBkey(),
378 'rc_cur_id' => $title->getArticleID(),
379 'rc_user' => $user->getID(),
380 'rc_user_text' => $user->getName(),
381 'rc_comment' => $comment,
382 'rc_this_oldid' => 0,
383 'rc_last_oldid' => 0,
384 'rc_bot' => $user->isAllowed( 'bot' ) ?
1 : 0,
385 'rc_moved_to_ns' => 0,
386 'rc_moved_to_title' => '',
389 'rc_new' => 0 # obsolete
392 'prefixedDBkey' => $title->getPrefixedDBkey(),
393 'lastTimestamp' => 0,
395 'logAction' => $action,
396 'logComment' => $logComment,
397 'logTarget' => $target,
398 'logParams' => $params
403 # Initialises the members of this object from a mysql row object
404 function loadFromRow( $row )
406 $this->mAttribs
= get_object_vars( $row );
407 $this->mAttribs
["rc_timestamp"] = wfTimestamp(TS_MW
, $this->mAttribs
["rc_timestamp"]);
408 $this->mExtra
= array();
411 # Makes a pseudo-RC entry from a cur row, for watchlists and things
412 function loadFromCurRow( $row )
414 $this->mAttribs
= array(
415 'rc_timestamp' => wfTimestamp(TS_MW
, $row->rev_timestamp
),
416 'rc_cur_time' => $row->rev_timestamp
,
417 'rc_user' => $row->rev_user
,
418 'rc_user_text' => $row->rev_user_text
,
419 'rc_namespace' => $row->page_namespace
,
420 'rc_title' => $row->page_title
,
421 'rc_comment' => $row->rev_comment
,
422 'rc_minor' => $row->rev_minor_edit ?
1 : 0,
423 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
424 'rc_cur_id' => $row->page_id
,
425 'rc_this_oldid' => $row->rev_id
,
426 'rc_last_oldid' => isset($row->rc_last_oldid
) ?
$row->rc_last_oldid
: 0,
428 'rc_moved_to_ns' => 0,
429 'rc_moved_to_title' => '',
431 'rc_id' => $row->rc_id
,
432 'rc_patrolled' => $row->rc_patrolled
,
433 'rc_new' => $row->page_is_new
# obsolete
436 $this->mExtra
= array();
441 * Gets the end part of the diff URL associated with this object
442 * Blank if no diff link should be displayed
444 function diffLinkTrail( $forceCur )
446 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
447 $trail = "curid=" . (int)($this->mAttribs
['rc_cur_id']) .
448 "&oldid=" . (int)($this->mAttribs
['rc_last_oldid']);
450 $trail .= '&diff=0' ;
452 $trail .= '&diff=' . (int)($this->mAttribs
['rc_this_oldid']);
460 function cleanupForIRC( $text ) {
461 return str_replace(array("\n", "\r"), array("", ""), $text);
464 function getIRCLine() {
465 global $wgUseRCPatrol;
467 extract($this->mAttribs
);
468 extract($this->mExtra
);
470 $titleObj =& $this->getTitle();
471 if ( $rc_type == RC_LOG
) {
472 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
474 $title = $titleObj->getPrefixedText();
476 $title = $this->cleanupForIRC( $title );
478 $bad = array("\n", "\r");
479 $empty = array("", "");
480 $title = $titleObj->getPrefixedText();
481 $title = str_replace($bad, $empty, $title);
483 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
484 if ( $rc_type == RC_LOG
) {
486 } elseif ( $rc_new && $wgUseRCPatrol ) {
487 $url = $titleObj->getInternalURL("rcid=$rc_id");
488 } else if ( $rc_new ) {
489 $url = $titleObj->getInternalURL();
490 } else if ( $wgUseRCPatrol ) {
491 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
493 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
496 if ( isset( $oldSize ) && isset( $newSize ) ) {
497 $szdiff = $newSize - $oldSize;
498 if ($szdiff < -500) {
499 $szdiff = "\002$szdiff\002";
500 } elseif ($szdiff >= 0) {
501 $szdiff = '+' . $szdiff ;
503 $szdiff = '(' . $szdiff . ')' ;
508 $user = $this->cleanupForIRC( $rc_user_text );
510 if ( $rc_type == RC_LOG
) {
511 $logTargetText = $logTarget->getPrefixedText();
512 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
515 $comment = $this->cleanupForIRC( $rc_comment );
516 $flag = ($rc_minor ?
"M" : "") . ($rc_new ?
"N" : "");
518 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
519 # no colour (\003) switches back to the term default
520 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
521 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";