Fix regression in even sizing of diff columns; forgot to restore a couple bits I...
[mediawiki.git] / includes / RecentChange.php
blob88a4598f93e8d80eddd596e238f5accf67db30a8
1 <?php
2 /**
4 */
6 /**
7 * Utility class for creating new RC entries
8 * mAttribs:
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
15 * rc_minor is minor
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
29 * mExtra:
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
42 class RecentChange
44 var $mAttribs = array(), $mExtra = array();
45 var $mTitle = false, $mMovedToTitle = false;
46 var $numberofWatchingusers = 0 ; # Dummy to prevent error message in SpecialRecentchangeslinked
48 # Factory methods
50 public static function newFromRow( $row )
52 $rc = new RecentChange;
53 $rc->loadFromRow( $row );
54 return $rc;
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;
63 return $rc;
66 /**
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 );
79 } else {
80 return NULL;
84 # Accessors
86 function setAttribs( $attribs )
88 $this->mAttribs = $attribs;
91 function setExtra( $extra )
93 $this->mExtra = $extra;
96 function &getTitle()
98 if ( $this->mTitle === false ) {
99 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
101 return $this->mTitle;
104 function getMovedToTitle()
106 if ( $this->mMovedToTitle === false ) {
107 $this->mMovedToTitle = Title::makeTitle( $this->mAttribs['rc_moved_to_ns'],
108 $this->mAttribs['rc_moved_to_title'] );
110 return $this->mMovedToTitle;
113 # Writes the data in this object to the database
114 function save()
116 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix;
117 $fname = 'RecentChange::save';
119 $dbw = wfGetDB( DB_MASTER );
120 if ( !is_array($this->mExtra) ) {
121 $this->mExtra = array();
123 $this->mExtra['lang'] = $wgLocalInterwiki;
125 if ( !$wgPutIPinRC ) {
126 $this->mAttribs['rc_ip'] = '';
129 ## If our database is strict about IP addresses, use NULL instead of an empty string
130 if ( $dbw->strictIPs() and $this->mAttribs['rc_ip'] == '' ) {
131 unset( $this->mAttribs['rc_ip'] );
134 # Fixup database timestamps
135 $this->mAttribs['rc_timestamp'] = $dbw->timestamp($this->mAttribs['rc_timestamp']);
136 $this->mAttribs['rc_cur_time'] = $dbw->timestamp($this->mAttribs['rc_cur_time']);
137 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
139 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
140 if ( $dbw->cascadingDeletes() and $this->mAttribs['rc_cur_id']==0 ) {
141 unset ( $this->mAttribs['rc_cur_id'] );
144 # Insert new row
145 $dbw->insert( 'recentchanges', $this->mAttribs, $fname );
147 # Set the ID
148 $this->mAttribs['rc_id'] = $dbw->insertId();
150 # Update old rows, if necessary
151 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
152 $lastTime = $this->mExtra['lastTimestamp'];
153 #$now = $this->mAttribs['rc_timestamp'];
154 #$curId = $this->mAttribs['rc_cur_id'];
156 # Don't bother looking for entries that have probably
157 # been purged, it just locks up the indexes needlessly.
158 global $wgRCMaxAge;
159 $age = time() - wfTimestamp( TS_UNIX, $lastTime );
160 if( $age < $wgRCMaxAge ) {
161 # live hack, will commit once tested - kate
162 # Update rc_this_oldid for the entries which were current
164 #$oldid = $this->mAttribs['rc_last_oldid'];
165 #$ns = $this->mAttribs['rc_namespace'];
166 #$title = $this->mAttribs['rc_title'];
168 #$dbw->update( 'recentchanges',
169 # array( /* SET */
170 # 'rc_this_oldid' => $oldid
171 # ), array( /* WHERE */
172 # 'rc_namespace' => $ns,
173 # 'rc_title' => $title,
174 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
175 # ), $fname
179 # Update rc_cur_time
180 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
181 # array( 'rc_cur_id' => $curId ), $fname );
184 # Notify external application via UDP
185 if ( $wgRC2UDPAddress ) {
186 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
187 if ( $conn ) {
188 $line = $wgRC2UDPPrefix . $this->getIRCLine();
189 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
190 socket_close( $conn );
194 # E-mail notifications
195 global $wgUseEnotif;
196 if( $wgUseEnotif ) {
197 # this would be better as an extension hook
198 global $wgUser;
199 include_once( "UserMailer.php" );
200 $enotif = new EmailNotification();
201 $title = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
202 $enotif->notifyOnPageChange( $wgUser, $title,
203 $this->mAttribs['rc_timestamp'],
204 $this->mAttribs['rc_comment'],
205 $this->mAttribs['rc_minor'],
206 $this->mAttribs['rc_last_oldid'] );
209 # Notify extensions
210 wfRunHooks( 'RecentChange_save', array( &$this ) );
213 # Marks a certain row as patrolled
214 function markPatrolled( $rcid )
216 $fname = 'RecentChange::markPatrolled';
218 $dbw = wfGetDB( DB_MASTER );
220 $dbw->update( 'recentchanges',
221 array( /* SET */
222 'rc_patrolled' => 1
223 ), array( /* WHERE */
224 'rc_id' => $rcid
225 ), $fname
229 # Makes an entry in the database corresponding to an edit
230 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
231 $oldId, $lastTimestamp, $bot = "default", $ip = '', $oldSize = 0, $newSize = 0,
232 $newId = 0)
235 if ( $bot === 'default' ) {
236 $bot = $user->isAllowed( 'bot' );
239 if ( !$ip ) {
240 $ip = wfGetIP();
241 if ( !$ip ) {
242 $ip = '';
246 $rc = new RecentChange;
247 $rc->mAttribs = array(
248 'rc_timestamp' => $timestamp,
249 'rc_cur_time' => $timestamp,
250 'rc_namespace' => $title->getNamespace(),
251 'rc_title' => $title->getDBkey(),
252 'rc_type' => RC_EDIT,
253 'rc_minor' => $minor ? 1 : 0,
254 'rc_cur_id' => $title->getArticleID(),
255 'rc_user' => $user->getID(),
256 'rc_user_text' => $user->getName(),
257 'rc_comment' => $comment,
258 'rc_this_oldid' => $newId,
259 'rc_last_oldid' => $oldId,
260 'rc_bot' => $bot ? 1 : 0,
261 'rc_moved_to_ns' => 0,
262 'rc_moved_to_title' => '',
263 'rc_ip' => $ip,
264 'rc_patrolled' => 0,
265 'rc_new' => 0, # obsolete
266 'rc_old_len' => $oldSize,
267 'rc_new_len' => $newSize
270 $rc->mExtra = array(
271 'prefixedDBkey' => $title->getPrefixedDBkey(),
272 'lastTimestamp' => $lastTimestamp,
273 'oldSize' => $oldSize,
274 'newSize' => $newSize,
276 $rc->save();
277 return( $rc->mAttribs['rc_id'] );
281 * Makes an entry in the database corresponding to page creation
282 * Note: the title object must be loaded with the new id using resetArticleID()
283 * @todo Document parameters and return
285 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = 'default',
286 $ip='', $size = 0, $newId = 0 )
288 if ( !$ip ) {
289 $ip = wfGetIP();
290 if ( !$ip ) {
291 $ip = '';
294 if ( $bot === 'default' ) {
295 $bot = $user->isAllowed( 'bot' );
298 $rc = new RecentChange;
299 $rc->mAttribs = array(
300 'rc_timestamp' => $timestamp,
301 'rc_cur_time' => $timestamp,
302 'rc_namespace' => $title->getNamespace(),
303 'rc_title' => $title->getDBkey(),
304 'rc_type' => RC_NEW,
305 'rc_minor' => $minor ? 1 : 0,
306 'rc_cur_id' => $title->getArticleID(),
307 'rc_user' => $user->getID(),
308 'rc_user_text' => $user->getName(),
309 'rc_comment' => $comment,
310 'rc_this_oldid' => $newId,
311 'rc_last_oldid' => 0,
312 'rc_bot' => $bot ? 1 : 0,
313 'rc_moved_to_ns' => 0,
314 'rc_moved_to_title' => '',
315 'rc_ip' => $ip,
316 'rc_patrolled' => 0,
317 'rc_new' => 1, # obsolete
318 'rc_old_len' => 0,
319 'rc_new_len' => $size
322 $rc->mExtra = array(
323 'prefixedDBkey' => $title->getPrefixedDBkey(),
324 'lastTimestamp' => 0,
325 'oldSize' => 0,
326 'newSize' => $size
328 $rc->save();
329 return( $rc->mAttribs['rc_id'] );
332 # Makes an entry in the database corresponding to a rename
333 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
335 if ( !$ip ) {
336 $ip = wfGetIP();
337 if ( !$ip ) {
338 $ip = '';
342 $rc = new RecentChange;
343 $rc->mAttribs = array(
344 'rc_timestamp' => $timestamp,
345 'rc_cur_time' => $timestamp,
346 'rc_namespace' => $oldTitle->getNamespace(),
347 'rc_title' => $oldTitle->getDBkey(),
348 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
349 'rc_minor' => 0,
350 'rc_cur_id' => $oldTitle->getArticleID(),
351 'rc_user' => $user->getID(),
352 'rc_user_text' => $user->getName(),
353 'rc_comment' => $comment,
354 'rc_this_oldid' => 0,
355 'rc_last_oldid' => 0,
356 'rc_bot' => $user->isAllowed( 'bot' ) ? 1 : 0,
357 'rc_moved_to_ns' => $newTitle->getNamespace(),
358 'rc_moved_to_title' => $newTitle->getDBkey(),
359 'rc_ip' => $ip,
360 'rc_new' => 0, # obsolete
361 'rc_patrolled' => 1,
362 'rc_old_len' => NULL,
363 'rc_new_len' => NULL,
366 $rc->mExtra = array(
367 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
368 'lastTimestamp' => 0,
369 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
371 $rc->save();
374 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
375 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
378 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
379 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
382 # A log entry is different to an edit in that previous revisions are
383 # not kept
384 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
385 $type, $action, $target, $logComment, $params )
387 if ( !$ip ) {
388 $ip = wfGetIP();
389 if ( !$ip ) {
390 $ip = '';
394 $rc = new RecentChange;
395 $rc->mAttribs = array(
396 'rc_timestamp' => $timestamp,
397 'rc_cur_time' => $timestamp,
398 'rc_namespace' => $title->getNamespace(),
399 'rc_title' => $title->getDBkey(),
400 'rc_type' => RC_LOG,
401 'rc_minor' => 0,
402 'rc_cur_id' => $title->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' ) ? 1 : 0,
409 'rc_moved_to_ns' => 0,
410 'rc_moved_to_title' => '',
411 'rc_ip' => $ip,
412 'rc_patrolled' => 1,
413 'rc_new' => 0, # obsolete
414 'rc_old_len' => NULL,
415 'rc_new_len' => NULL,
417 $rc->mExtra = array(
418 'prefixedDBkey' => $title->getPrefixedDBkey(),
419 'lastTimestamp' => 0,
420 'logType' => $type,
421 'logAction' => $action,
422 'logComment' => $logComment,
423 'logTarget' => $target,
424 'logParams' => $params
426 $rc->save();
429 # Initialises the members of this object from a mysql row object
430 function loadFromRow( $row )
432 $this->mAttribs = get_object_vars( $row );
433 $this->mAttribs["rc_timestamp"] = wfTimestamp(TS_MW, $this->mAttribs["rc_timestamp"]);
434 $this->mExtra = array();
437 # Makes a pseudo-RC entry from a cur row
438 function loadFromCurRow( $row )
440 $this->mAttribs = array(
441 'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp),
442 'rc_cur_time' => $row->rev_timestamp,
443 'rc_user' => $row->rev_user,
444 'rc_user_text' => $row->rev_user_text,
445 'rc_namespace' => $row->page_namespace,
446 'rc_title' => $row->page_title,
447 'rc_comment' => $row->rev_comment,
448 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
449 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
450 'rc_cur_id' => $row->page_id,
451 'rc_this_oldid' => $row->rev_id,
452 'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0,
453 'rc_bot' => 0,
454 'rc_moved_to_ns' => 0,
455 'rc_moved_to_title' => '',
456 'rc_ip' => '',
457 'rc_id' => $row->rc_id,
458 'rc_patrolled' => $row->rc_patrolled,
459 'rc_new' => $row->page_is_new, # obsolete
460 'rc_old_len' => $row->rc_old_len,
461 'rc_new_len' => $row->rc_new_len,
464 $this->mExtra = array();
468 * Get an attribute value
470 * @param $name Attribute name
471 * @return mixed
473 public function getAttribute( $name ) {
474 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : NULL;
478 * Gets the end part of the diff URL associated with this object
479 * Blank if no diff link should be displayed
481 function diffLinkTrail( $forceCur )
483 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
484 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
485 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
486 if ( $forceCur ) {
487 $trail .= '&diff=0' ;
488 } else {
489 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
491 } else {
492 $trail = '';
494 return $trail;
497 function cleanupForIRC( $text ) {
498 return str_replace(array("\n", "\r"), array("", ""), $text);
501 function getIRCLine() {
502 global $wgUseRCPatrol;
504 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
505 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
506 extract($this->mAttribs);
507 extract($this->mExtra);
509 $titleObj =& $this->getTitle();
510 if ( $rc_type == RC_LOG ) {
511 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
512 } else {
513 $title = $titleObj->getPrefixedText();
515 $title = $this->cleanupForIRC( $title );
517 $bad = array("\n", "\r");
518 $empty = array("", "");
519 $title = $titleObj->getPrefixedText();
520 $title = str_replace($bad, $empty, $title);
522 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
523 if ( $rc_type == RC_LOG ) {
524 $url = '';
525 } elseif ( $rc_new && $wgUseRCPatrol ) {
526 $url = $titleObj->getInternalURL("rcid=$rc_id");
527 } else if ( $rc_new ) {
528 $url = $titleObj->getInternalURL();
529 } else if ( $wgUseRCPatrol ) {
530 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
531 } else {
532 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
535 if ( isset( $oldSize ) && isset( $newSize ) ) {
536 $szdiff = $newSize - $oldSize;
537 if ($szdiff < -500) {
538 $szdiff = "\002$szdiff\002";
539 } elseif ($szdiff >= 0) {
540 $szdiff = '+' . $szdiff ;
542 $szdiff = '(' . $szdiff . ')' ;
543 } else {
544 $szdiff = '';
547 $user = $this->cleanupForIRC( $rc_user_text );
549 if ( $rc_type == RC_LOG ) {
550 $logTargetText = $logTarget->getPrefixedText();
551 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
552 $flag = $logAction;
553 } else {
554 $comment = $this->cleanupForIRC( $rc_comment );
555 $flag = ($rc_minor ? "M" : "") . ($rc_new ? "N" : "");
557 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
558 # no colour (\003) switches back to the term default
559 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
560 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
561 return $fullString;
565 * Returns the change size (HTML).
566 * The lengths can be given optionally.
568 function getCharacterDifference( $old = 0, $new = 0 ) {
569 global $wgRCChangedSizeThreshold, $wgLang;
571 if( $old === 0 ) {
572 $old = $this->mAttribs['rc_old_len'];
574 if( $new === 0 ) {
575 $new = $this->mAttribs['rc_new_len'];
578 if( $old === NULL || $new === NULL ) {
579 return '';
582 $szdiff = $new - $old;
583 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
584 $wgLang->formatNum($szdiff) );
586 if( $szdiff < $wgRCChangedSizeThreshold ) {
587 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
588 } elseif( $szdiff === 0 ) {
589 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
590 } elseif( $szdiff > 0 ) {
591 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
592 } else {
593 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';