* (bug 6627) Fix regression in Special:Ipblocklist with table prefix
[mediawiki.git] / includes / RecentChange.php
blob1f035d367f6468fced80a9f4a7e2d4f1513be82d
1 <?php
2 /**
4 * @package MediaWiki
5 */
7 /**
8 * Utility class for creating new RC entries
9 * mAttribs:
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
16 * rc_minor is minor
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
28 * mExtra:
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
40 * @package MediaWiki
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 /* static */ function newFromRow( $row )
52 $rc = new RecentChange;
53 $rc->loadFromRow( $row );
54 return $rc;
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;
63 return $rc;
66 # Accessors
68 function setAttribs( $attribs )
70 $this->mAttribs = $attribs;
73 function setExtra( $extra )
75 $this->mExtra = $extra;
78 function &getTitle()
80 if ( $this->mTitle === false ) {
81 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
83 return $this->mTitle;
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
96 function save()
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 # Fixup database timestamps
112 $this->mAttribs['rc_timestamp'] = $dbw->timestamp($this->mAttribs['rc_timestamp']);
113 $this->mAttribs['rc_cur_time'] = $dbw->timestamp($this->mAttribs['rc_cur_time']);
114 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
116 # Insert new row
117 $dbw->insert( 'recentchanges', $this->mAttribs, $fname );
119 # Set the ID
120 $this->mAttribs['rc_id'] = $dbw->insertId();
122 # Update old rows, if necessary
123 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
124 $lastTime = $this->mExtra['lastTimestamp'];
125 #$now = $this->mAttribs['rc_timestamp'];
126 #$curId = $this->mAttribs['rc_cur_id'];
128 # Don't bother looking for entries that have probably
129 # been purged, it just locks up the indexes needlessly.
130 global $wgRCMaxAge;
131 $age = time() - wfTimestamp( TS_UNIX, $lastTime );
132 if( $age < $wgRCMaxAge ) {
133 # live hack, will commit once tested - kate
134 # Update rc_this_oldid for the entries which were current
136 #$oldid = $this->mAttribs['rc_last_oldid'];
137 #$ns = $this->mAttribs['rc_namespace'];
138 #$title = $this->mAttribs['rc_title'];
140 #$dbw->update( 'recentchanges',
141 # array( /* SET */
142 # 'rc_this_oldid' => $oldid
143 # ), array( /* WHERE */
144 # 'rc_namespace' => $ns,
145 # 'rc_title' => $title,
146 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
147 # ), $fname
151 # Update rc_cur_time
152 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
153 # array( 'rc_cur_id' => $curId ), $fname );
156 # Notify external application via UDP
157 if ( $wgRC2UDPAddress ) {
158 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
159 if ( $conn ) {
160 $line = $wgRC2UDPPrefix . $this->getIRCLine();
161 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
162 socket_close( $conn );
166 // E-mail notifications
167 global $wgUseEnotif;
168 if( $wgUseEnotif ) {
169 # this would be better as an extension hook
170 include_once( "UserMailer.php" );
171 $enotif = new EmailNotification();
172 $title = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
173 $enotif->notifyOnPageChange( $title,
174 $this->mAttribs['rc_timestamp'],
175 $this->mAttribs['rc_comment'],
176 $this->mAttribs['rc_minor'],
177 $this->mAttribs['rc_last_oldid'] );
182 # Marks a certain row as patrolled
183 function markPatrolled( $rcid )
185 $fname = 'RecentChange::markPatrolled';
187 $dbw =& wfGetDB( DB_MASTER );
189 $dbw->update( 'recentchanges',
190 array( /* SET */
191 'rc_patrolled' => 1
192 ), array( /* WHERE */
193 'rc_id' => $rcid
194 ), $fname
198 # Makes an entry in the database corresponding to an edit
199 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
200 $oldId, $lastTimestamp, $bot = "default", $ip = '', $oldSize = 0, $newSize = 0,
201 $newId = 0)
203 if ( $bot == 'default' ) {
204 $bot = $user->isBot();
207 if ( !$ip ) {
208 $ip = wfGetIP();
209 if ( !$ip ) {
210 $ip = '';
214 $rc = new RecentChange;
215 $rc->mAttribs = array(
216 'rc_timestamp' => $timestamp,
217 'rc_cur_time' => $timestamp,
218 'rc_namespace' => $title->getNamespace(),
219 'rc_title' => $title->getDBkey(),
220 'rc_type' => RC_EDIT,
221 'rc_minor' => $minor ? 1 : 0,
222 'rc_cur_id' => $title->getArticleID(),
223 'rc_user' => $user->getID(),
224 'rc_user_text' => $user->getName(),
225 'rc_comment' => $comment,
226 'rc_this_oldid' => $newId,
227 'rc_last_oldid' => $oldId,
228 'rc_bot' => $bot ? 1 : 0,
229 'rc_moved_to_ns' => 0,
230 'rc_moved_to_title' => '',
231 'rc_ip' => $ip,
232 'rc_patrolled' => 0,
233 'rc_new' => 0 # obsolete
236 $rc->mExtra = array(
237 'prefixedDBkey' => $title->getPrefixedDBkey(),
238 'lastTimestamp' => $lastTimestamp,
239 'oldSize' => $oldSize,
240 'newSize' => $newSize,
242 $rc->save();
243 return( $rc->mAttribs['rc_id'] );
247 * Makes an entry in the database corresponding to page creation
248 * Note: the title object must be loaded with the new id using resetArticleID()
249 * @todo Document parameters and return
250 * @public
251 * @static
253 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default",
254 $ip='', $size = 0, $newId = 0 )
256 if ( !$ip ) {
257 $ip = wfGetIP();
258 if ( !$ip ) {
259 $ip = '';
262 if ( $bot == 'default' ) {
263 $bot = $user->isBot();
266 $rc = new RecentChange;
267 $rc->mAttribs = array(
268 'rc_timestamp' => $timestamp,
269 'rc_cur_time' => $timestamp,
270 'rc_namespace' => $title->getNamespace(),
271 'rc_title' => $title->getDBkey(),
272 'rc_type' => RC_NEW,
273 'rc_minor' => $minor ? 1 : 0,
274 'rc_cur_id' => $title->getArticleID(),
275 'rc_user' => $user->getID(),
276 'rc_user_text' => $user->getName(),
277 'rc_comment' => $comment,
278 'rc_this_oldid' => $newId,
279 'rc_last_oldid' => 0,
280 'rc_bot' => $bot ? 1 : 0,
281 'rc_moved_to_ns' => 0,
282 'rc_moved_to_title' => '',
283 'rc_ip' => $ip,
284 'rc_patrolled' => 0,
285 'rc_new' => 1 # obsolete
288 $rc->mExtra = array(
289 'prefixedDBkey' => $title->getPrefixedDBkey(),
290 'lastTimestamp' => 0,
291 'oldSize' => 0,
292 'newSize' => $size
294 $rc->save();
295 return( $rc->mAttribs['rc_id'] );
298 # Makes an entry in the database corresponding to a rename
299 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
301 if ( !$ip ) {
302 $ip = wfGetIP();
303 if ( !$ip ) {
304 $ip = '';
308 $rc = new RecentChange;
309 $rc->mAttribs = array(
310 'rc_timestamp' => $timestamp,
311 'rc_cur_time' => $timestamp,
312 'rc_namespace' => $oldTitle->getNamespace(),
313 'rc_title' => $oldTitle->getDBkey(),
314 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
315 'rc_minor' => 0,
316 'rc_cur_id' => $oldTitle->getArticleID(),
317 'rc_user' => $user->getID(),
318 'rc_user_text' => $user->getName(),
319 'rc_comment' => $comment,
320 'rc_this_oldid' => 0,
321 'rc_last_oldid' => 0,
322 'rc_bot' => $user->isBot() ? 1 : 0,
323 'rc_moved_to_ns' => $newTitle->getNamespace(),
324 'rc_moved_to_title' => $newTitle->getDBkey(),
325 'rc_ip' => $ip,
326 'rc_new' => 0, # obsolete
327 'rc_patrolled' => 1
330 $rc->mExtra = array(
331 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
332 'lastTimestamp' => 0,
333 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
335 $rc->save();
338 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
339 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
342 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
343 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
346 # A log entry is different to an edit in that previous revisions are
347 # not kept
348 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
349 $type, $action, $target, $logComment, $params )
351 if ( !$ip ) {
352 $ip = wfGetIP();
353 if ( !$ip ) {
354 $ip = '';
358 $rc = new RecentChange;
359 $rc->mAttribs = array(
360 'rc_timestamp' => $timestamp,
361 'rc_cur_time' => $timestamp,
362 'rc_namespace' => $title->getNamespace(),
363 'rc_title' => $title->getDBkey(),
364 'rc_type' => RC_LOG,
365 'rc_minor' => 0,
366 'rc_cur_id' => $title->getArticleID(),
367 'rc_user' => $user->getID(),
368 'rc_user_text' => $user->getName(),
369 'rc_comment' => $comment,
370 'rc_this_oldid' => 0,
371 'rc_last_oldid' => 0,
372 'rc_bot' => $user->isBot() ? 1 : 0,
373 'rc_moved_to_ns' => 0,
374 'rc_moved_to_title' => '',
375 'rc_ip' => $ip,
376 'rc_patrolled' => 1,
377 'rc_new' => 0 # obsolete
379 $rc->mExtra = array(
380 'prefixedDBkey' => $title->getPrefixedDBkey(),
381 'lastTimestamp' => 0,
382 'logType' => $type,
383 'logAction' => $action,
384 'logComment' => $logComment,
385 'logTarget' => $target,
386 'logParams' => $params
388 $rc->save();
391 # Initialises the members of this object from a mysql row object
392 function loadFromRow( $row )
394 $this->mAttribs = get_object_vars( $row );
395 $this->mAttribs["rc_timestamp"] = wfTimestamp(TS_MW, $this->mAttribs["rc_timestamp"]);
396 $this->mExtra = array();
399 # Makes a pseudo-RC entry from a cur row, for watchlists and things
400 function loadFromCurRow( $row )
402 $this->mAttribs = array(
403 'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp),
404 'rc_cur_time' => $row->rev_timestamp,
405 'rc_user' => $row->rev_user,
406 'rc_user_text' => $row->rev_user_text,
407 'rc_namespace' => $row->page_namespace,
408 'rc_title' => $row->page_title,
409 'rc_comment' => $row->rev_comment,
410 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
411 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
412 'rc_cur_id' => $row->page_id,
413 'rc_this_oldid' => $row->rev_id,
414 'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0,
415 'rc_bot' => 0,
416 'rc_moved_to_ns' => 0,
417 'rc_moved_to_title' => '',
418 'rc_ip' => '',
419 'rc_id' => $row->rc_id,
420 'rc_patrolled' => $row->rc_patrolled,
421 'rc_new' => $row->page_is_new # obsolete
424 $this->mExtra = array();
429 * Gets the end part of the diff URL associated with this object
430 * Blank if no diff link should be displayed
432 function diffLinkTrail( $forceCur )
434 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
435 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
436 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
437 if ( $forceCur ) {
438 $trail .= '&diff=0' ;
439 } else {
440 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
442 } else {
443 $trail = '';
445 return $trail;
448 function cleanupForIRC( $text ) {
449 return str_replace(array("\n", "\r"), array("", ""), $text);
452 function getIRCLine() {
453 global $wgUseRCPatrol;
455 extract($this->mAttribs);
456 extract($this->mExtra);
458 $titleObj =& $this->getTitle();
459 if ( $rc_type == RC_LOG ) {
460 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
461 } else {
462 $title = $titleObj->getPrefixedText();
464 $title = $this->cleanupForIRC( $title );
466 $bad = array("\n", "\r");
467 $empty = array("", "");
468 $title = $titleObj->getPrefixedText();
469 $title = str_replace($bad, $empty, $title);
471 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
472 if ( $rc_type == RC_LOG ) {
473 $url = '';
474 } elseif ( $rc_new && $wgUseRCPatrol ) {
475 $url = $titleObj->getInternalURL("rcid=$rc_id");
476 } else if ( $rc_new ) {
477 $url = $titleObj->getInternalURL();
478 } else if ( $wgUseRCPatrol ) {
479 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
480 } else {
481 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
484 if ( isset( $oldSize ) && isset( $newSize ) ) {
485 $szdiff = $newSize - $oldSize;
486 if ($szdiff < -500) {
487 $szdiff = "\002$szdiff\002";
488 } elseif ($szdiff >= 0) {
489 $szdiff = '+' . $szdiff ;
491 $szdiff = '(' . $szdiff . ')' ;
492 } else {
493 $szdiff = '';
496 $user = $this->cleanupForIRC( $rc_user_text );
498 if ( $rc_type == RC_LOG ) {
499 $logTargetText = $logTarget->getPrefixedText();
500 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
501 $flag = $logAction;
502 } else {
503 $comment = $this->cleanupForIRC( $rc_comment );
504 $flag = ($rc_minor ? "M" : "") . ($rc_new ? "N" : "");
506 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
507 # no colour (\003) switches back to the term default
508 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
509 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
510 return $fullString;