*Merge in phase3_rev_deleted/includes
[lhc/web/wiklou.git] / includes / RecentChange.php
1 <?php
2 /**
3 *
4 */
5
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
28 * rc_deleted partial deletion
29 * rc_logid the log_id value for this log entry (or zero)
30 * rc_log_type the log type (or null)
31 * rc_log_action the log action (or null)
32 * rc_params log params
33 *
34 * mExtra:
35 * prefixedDBkey prefixed db key, used by external app via msg queue
36 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
37 * lang the interwiki prefix, automatically set in save()
38 * oldSize text size before the change
39 * newSize text size after the change
40 *
41 * temporary: not stored in the database
42 * notificationtimestamp
43 * numberofWatchingusers
44 *
45 * @todo document functions and variables
46 */
47 class RecentChange
48 {
49 var $mAttribs = array(), $mExtra = array();
50 var $mTitle = false, $mMovedToTitle = false;
51 var $numberofWatchingusers = 0 ; # Dummy to prevent error message in SpecialRecentchangeslinked
52
53 # Factory methods
54
55 public static function newFromRow( $row )
56 {
57 $rc = new RecentChange;
58 $rc->loadFromRow( $row );
59 return $rc;
60 }
61
62 public static function newFromCurRow( $row, $rc_this_oldid = 0 )
63 {
64 $rc = new RecentChange;
65 $rc->loadFromCurRow( $row, $rc_this_oldid );
66 $rc->notificationtimestamp = false;
67 $rc->numberofWatchingusers = false;
68 return $rc;
69 }
70
71 /**
72 * Obtain the recent change with a given rc_id value
73 *
74 * @param $rcid rc_id value to retrieve
75 * @return RecentChange
76 */
77 public static function newFromId( $rcid ) {
78 $dbr = wfGetDB( DB_SLAVE );
79 $res = $dbr->select( 'recentchanges', '*', array( 'rc_id' => $rcid ), __METHOD__ );
80 if( $res && $dbr->numRows( $res ) > 0 ) {
81 $row = $dbr->fetchObject( $res );
82 $dbr->freeResult( $res );
83 return self::newFromRow( $row );
84 } else {
85 return NULL;
86 }
87 }
88
89 # Accessors
90
91 function setAttribs( $attribs )
92 {
93 $this->mAttribs = $attribs;
94 }
95
96 function setExtra( $extra )
97 {
98 $this->mExtra = $extra;
99 }
100
101 function &getTitle()
102 {
103 if ( $this->mTitle === false ) {
104 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
105 }
106 return $this->mTitle;
107 }
108
109 function getMovedToTitle()
110 {
111 if ( $this->mMovedToTitle === false ) {
112 $this->mMovedToTitle = Title::makeTitle( $this->mAttribs['rc_moved_to_ns'],
113 $this->mAttribs['rc_moved_to_title'] );
114 }
115 return $this->mMovedToTitle;
116 }
117
118 # Writes the data in this object to the database
119 function save()
120 {
121 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix;
122 $fname = 'RecentChange::save';
123
124 $dbw = wfGetDB( DB_MASTER );
125 if ( !is_array($this->mExtra) ) {
126 $this->mExtra = array();
127 }
128 $this->mExtra['lang'] = $wgLocalInterwiki;
129
130 if ( !$wgPutIPinRC ) {
131 $this->mAttribs['rc_ip'] = '';
132 }
133
134 ## If our database is strict about IP addresses, use NULL instead of an empty string
135 if ( $dbw->strictIPs() and $this->mAttribs['rc_ip'] == '' ) {
136 unset( $this->mAttribs['rc_ip'] );
137 }
138
139 # Fixup database timestamps
140 $this->mAttribs['rc_timestamp'] = $dbw->timestamp($this->mAttribs['rc_timestamp']);
141 $this->mAttribs['rc_cur_time'] = $dbw->timestamp($this->mAttribs['rc_cur_time']);
142 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
143
144 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
145 if ( $dbw->cascadingDeletes() and $this->mAttribs['rc_cur_id']==0 ) {
146 unset ( $this->mAttribs['rc_cur_id'] );
147 }
148
149 # Insert new row
150 $dbw->insert( 'recentchanges', $this->mAttribs, $fname );
151
152 # Set the ID
153 $this->mAttribs['rc_id'] = $dbw->insertId();
154
155 # Update old rows, if necessary
156 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
157 $lastTime = $this->mExtra['lastTimestamp'];
158 #$now = $this->mAttribs['rc_timestamp'];
159 #$curId = $this->mAttribs['rc_cur_id'];
160
161 # Don't bother looking for entries that have probably
162 # been purged, it just locks up the indexes needlessly.
163 global $wgRCMaxAge;
164 $age = time() - wfTimestamp( TS_UNIX, $lastTime );
165 if( $age < $wgRCMaxAge ) {
166 # live hack, will commit once tested - kate
167 # Update rc_this_oldid for the entries which were current
168 #
169 #$oldid = $this->mAttribs['rc_last_oldid'];
170 #$ns = $this->mAttribs['rc_namespace'];
171 #$title = $this->mAttribs['rc_title'];
172 #
173 #$dbw->update( 'recentchanges',
174 # array( /* SET */
175 # 'rc_this_oldid' => $oldid
176 # ), array( /* WHERE */
177 # 'rc_namespace' => $ns,
178 # 'rc_title' => $title,
179 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
180 # ), $fname
181 #);
182 }
183
184 # Update rc_cur_time
185 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
186 # array( 'rc_cur_id' => $curId ), $fname );
187 }
188
189 # Notify external application via UDP
190 if ( $wgRC2UDPAddress ) {
191 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
192 if ( $conn ) {
193 $line = $wgRC2UDPPrefix . $this->getIRCLine();
194 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
195 socket_close( $conn );
196 }
197 }
198
199 # E-mail notifications
200 global $wgUseEnotif;
201 if( $wgUseEnotif ) {
202 # this would be better as an extension hook
203 include_once( "UserMailer.php" );
204 $enotif = new EmailNotification();
205 $title = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
206 $enotif->notifyOnPageChange( $title,
207 $this->mAttribs['rc_timestamp'],
208 $this->mAttribs['rc_comment'],
209 $this->mAttribs['rc_minor'],
210 $this->mAttribs['rc_last_oldid'] );
211 }
212
213 # Notify extensions
214 wfRunHooks( 'RecentChange_save', array( &$this ) );
215 }
216
217 # Marks a certain row as patrolled
218 function markPatrolled( $rcid )
219 {
220 $fname = 'RecentChange::markPatrolled';
221
222 $dbw = wfGetDB( DB_MASTER );
223
224 $dbw->update( 'recentchanges',
225 array( /* SET */
226 'rc_patrolled' => 1
227 ), array( /* WHERE */
228 'rc_id' => $rcid
229 ), $fname
230 );
231 }
232
233 # Makes an entry in the database corresponding to an edit
234 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
235 $oldId, $lastTimestamp, $bot="default", $ip='', $oldSize=0, $newSize=0, $newId=0)
236 {
237
238 if ( $bot === 'default' ) {
239 $bot = $user->isAllowed( 'bot' );
240 }
241
242 if ( !$ip ) {
243 $ip = wfGetIP();
244 if ( !$ip ) {
245 $ip = '';
246 }
247 }
248
249 $rc = new RecentChange;
250 $rc->mAttribs = array(
251 'rc_timestamp' => $timestamp,
252 'rc_cur_time' => $timestamp,
253 'rc_namespace' => $title->getNamespace(),
254 'rc_title' => $title->getDBkey(),
255 'rc_type' => RC_EDIT,
256 'rc_minor' => $minor ? 1 : 0,
257 'rc_cur_id' => $title->getArticleID(),
258 'rc_user' => $user->getID(),
259 'rc_user_text' => $user->getName(),
260 'rc_comment' => $comment,
261 'rc_this_oldid' => $newId,
262 'rc_last_oldid' => $oldId,
263 'rc_bot' => $bot ? 1 : 0,
264 'rc_moved_to_ns' => 0,
265 'rc_moved_to_title' => '',
266 'rc_ip' => $ip,
267 'rc_patrolled' => 0,
268 'rc_new' => 0, # obsolete
269 'rc_old_len' => $oldSize,
270 'rc_new_len' => $newSize,
271 'rc_deleted' => 0,
272 'rc_logid' => 0,
273 'rc_log_type' => null,
274 'rc_log_action' => '',
275 'rc_params' => ''
276 );
277
278 $rc->mExtra = array(
279 'prefixedDBkey' => $title->getPrefixedDBkey(),
280 'lastTimestamp' => $lastTimestamp,
281 'oldSize' => $oldSize,
282 'newSize' => $newSize,
283 );
284 $rc->save();
285 return( $rc->mAttribs['rc_id'] );
286 }
287
288 /**
289 * Makes an entry in the database corresponding to page creation
290 * Note: the title object must be loaded with the new id using resetArticleID()
291 * @todo Document parameters and return
292 * @public
293 * @static
294 */
295 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default",
296 $ip='', $size=0, $newId=0 )
297 {
298 if ( !$ip ) {
299 $ip = wfGetIP();
300 if ( !$ip ) {
301 $ip = '';
302 }
303 }
304
305 if ( $bot == 'default' ) {
306 $bot = $user->isAllowed( 'bot' );
307 }
308
309 $rc = new RecentChange;
310 $rc->mAttribs = array(
311 'rc_timestamp' => $timestamp,
312 'rc_cur_time' => $timestamp,
313 'rc_namespace' => $title->getNamespace(),
314 'rc_title' => $title->getDBkey(),
315 'rc_type' => RC_NEW,
316 'rc_minor' => $minor ? 1 : 0,
317 'rc_cur_id' => $title->getArticleID(),
318 'rc_user' => $user->getID(),
319 'rc_user_text' => $user->getName(),
320 'rc_comment' => $comment,
321 'rc_this_oldid' => $newId,
322 'rc_last_oldid' => 0,
323 'rc_bot' => $bot ? 1 : 0,
324 'rc_moved_to_ns' => 0,
325 'rc_moved_to_title' => '',
326 'rc_ip' => $ip,
327 'rc_patrolled' => 0,
328 'rc_new' => 1, # obsolete
329 'rc_old_len' => 0,
330 'rc_new_len' => $size,
331 'rc_deleted' => 0,
332 'rc_logid' => 0,
333 'rc_log_type' => null,
334 'rc_log_action' => '',
335 'rc_params' => ''
336 );
337
338 $rc->mExtra = array(
339 'prefixedDBkey' => $title->getPrefixedDBkey(),
340 'lastTimestamp' => 0,
341 'oldSize' => 0,
342 'newSize' => $size
343 );
344 $rc->save();
345 return( $rc->mAttribs['rc_id'] );
346 }
347
348 # Makes an entry in the database corresponding to a rename
349 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
350 {
351 if ( !$ip ) {
352 $ip = wfGetIP();
353 if ( !$ip ) {
354 $ip = '';
355 }
356 }
357
358 $rc = new RecentChange;
359 $rc->mAttribs = array(
360 'rc_timestamp' => $timestamp,
361 'rc_cur_time' => $timestamp,
362 'rc_namespace' => $oldTitle->getNamespace(),
363 'rc_title' => $oldTitle->getDBkey(),
364 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
365 'rc_minor' => 0,
366 'rc_cur_id' => $oldTitle->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->isAllowed( 'bot' ) ? 1 : 0,
373 'rc_moved_to_ns' => $newTitle->getNamespace(),
374 'rc_moved_to_title' => $newTitle->getDBkey(),
375 'rc_ip' => $ip,
376 'rc_new' => 0, # obsolete
377 'rc_patrolled' => 1,
378 'rc_old_len' => NULL,
379 'rc_new_len' => NULL,
380 'rc_deleted' => 0,
381 'rc_logid' => 0, # notifyMove not used anymore
382 'rc_log_type' => null,
383 'rc_log_action' => '',
384 'rc_params' => ''
385 );
386
387 $rc->mExtra = array(
388 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
389 'lastTimestamp' => 0,
390 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
391 );
392 $rc->save();
393 }
394
395 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
396 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
397 }
398
399 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
400 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
401 }
402
403 # A log entry is different to an edit in that previous revisions are not kept
404 /*static*/ function notifyLog( $timestamp, &$title, &$user, $actionText = null, $ip='',
405 $type, $action, $target, $logComment, $params, $newId=0 )
406 {
407 if ( !$ip ) {
408 $ip = wfGetIP();
409 if ( !$ip ) $ip = '';
410 }
411 $rc = new RecentChange;
412 $rc->mAttribs = array(
413 'rc_timestamp' => $timestamp,
414 'rc_cur_time' => $timestamp,
415 'rc_namespace' => $title->getNamespace(),
416 'rc_title' => $title->getDBkey(),
417 'rc_type' => RC_LOG,
418 'rc_minor' => 0,
419 'rc_cur_id' => $title->getArticleID(),
420 'rc_user' => $user->getID(),
421 'rc_user_text' => $user->getName(),
422 'rc_comment' => $logComment,
423 'rc_this_oldid' => 0,
424 'rc_last_oldid' => 0,
425 'rc_bot' => $user->isAllowed( 'bot' ) ? 1 : 0,
426 'rc_moved_to_ns' => 0,
427 'rc_moved_to_title' => '',
428 'rc_ip' => $ip,
429 'rc_patrolled' => 1,
430 'rc_new' => 0, # obsolete
431 'rc_old_len' => NULL,
432 'rc_new_len' => NULL,
433 'rc_deleted' => 0,
434 'rc_logid' => $newId,
435 'rc_log_type' => $type,
436 'rc_log_action' => $action,
437 'rc_params' => $params
438 );
439 $rc->mExtra = array(
440 'prefixedDBkey' => $title->getPrefixedDBkey(),
441 'lastTimestamp' => 0,
442 'logType' => $type,
443 'logAction' => $action,
444 'logComment' => $logComment,
445 'logTarget' => $target,
446 'logParams' => $params
447 );
448 $rc->save();
449 }
450
451 # Initialises the members of this object from a mysql row object
452 function loadFromRow( $row )
453 {
454 $this->mAttribs = get_object_vars( $row );
455 $this->mAttribs["rc_timestamp"] = wfTimestamp(TS_MW, $this->mAttribs["rc_timestamp"]);
456 $this->mExtra = array();
457 }
458
459 # Makes a pseudo-RC entry from a cur row
460 function loadFromCurRow( $row )
461 {
462 $this->mAttribs = array(
463 'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp),
464 'rc_cur_time' => $row->rev_timestamp,
465 'rc_user' => $row->rev_user,
466 'rc_user_text' => $row->rev_user_text,
467 'rc_namespace' => $row->page_namespace,
468 'rc_title' => $row->page_title,
469 'rc_comment' => $row->rev_comment,
470 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
471 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
472 'rc_cur_id' => $row->page_id,
473 'rc_this_oldid' => $row->rev_id,
474 'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0,
475 'rc_bot' => 0,
476 'rc_moved_to_ns' => 0,
477 'rc_moved_to_title' => '',
478 'rc_ip' => '',
479 'rc_id' => $row->rc_id,
480 'rc_patrolled' => $row->rc_patrolled,
481 'rc_new' => $row->page_is_new, # obsolete
482 'rc_old_len' => $row->rc_old_len,
483 'rc_new_len' => $row->rc_new_len,
484 'rc_deleted' => $row->rc_deleted,
485 'rc_logid' => $row->rc_logid,
486 'rc_log_type' => $row->rc_log_type,
487 'rc_log_action' => $row->rc_log_action,
488 'rc_params' => $row->rc_params
489 );
490
491 $this->mExtra = array();
492 }
493
494 /**
495 * Get an attribute value
496 *
497 * @param $name Attribute name
498 * @return mixed
499 */
500 public function getAttribute( $name ) {
501 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : NULL;
502 }
503
504 /**
505 * Gets the end part of the diff URL associated with this object
506 * Blank if no diff link should be displayed
507 */
508 function diffLinkTrail( $forceCur )
509 {
510 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
511 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
512 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
513 if ( $forceCur ) {
514 $trail .= '&diff=0' ;
515 } else {
516 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
517 }
518 } else {
519 $trail = '';
520 }
521 return $trail;
522 }
523
524 function cleanupForIRC( $text ) {
525 return str_replace(array("\n", "\r"), array("", ""), $text);
526 }
527
528 function getIRCLine() {
529 global $wgUseRCPatrol;
530
531 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
532 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
533 extract($this->mAttribs);
534 extract($this->mExtra);
535
536 $titleObj =& $this->getTitle();
537 if ( $rc_type == RC_LOG ) {
538 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
539 } else {
540 $title = $titleObj->getPrefixedText();
541 }
542 $title = $this->cleanupForIRC( $title );
543
544 $bad = array("\n", "\r");
545 $empty = array("", "");
546 $title = $titleObj->getPrefixedText();
547 $title = str_replace($bad, $empty, $title);
548
549 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
550 if ( $rc_type == RC_LOG ) {
551 $url = '';
552 } elseif ( $rc_new && $wgUseRCPatrol ) {
553 $url = $titleObj->getInternalURL("rcid=$rc_id");
554 } else if ( $rc_new ) {
555 $url = $titleObj->getInternalURL();
556 } else if ( $wgUseRCPatrol ) {
557 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
558 } else {
559 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
560 }
561
562 if ( isset( $oldSize ) && isset( $newSize ) ) {
563 $szdiff = $newSize - $oldSize;
564 if ($szdiff < -500) {
565 $szdiff = "\002$szdiff\002";
566 } elseif ($szdiff >= 0) {
567 $szdiff = '+' . $szdiff ;
568 }
569 $szdiff = '(' . $szdiff . ')' ;
570 } else {
571 $szdiff = '';
572 }
573
574 $user = $this->cleanupForIRC( $rc_user_text );
575
576 if ( $rc_type == RC_LOG ) {
577 $logTargetText = $logTarget->getPrefixedText();
578 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
579 $flag = $logAction;
580 } else {
581 $comment = $this->cleanupForIRC( $rc_comment );
582 $flag = ($rc_minor ? "M" : "") . ($rc_new ? "N" : "");
583 }
584 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
585 # no colour (\003) switches back to the term default
586 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
587 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
588 return $fullString;
589 }
590
591 /**
592 * Returns the change size (HTML).
593 * The lengths can be given optionally.
594 */
595 function getCharacterDifference( $old = 0, $new = 0 ) {
596 global $wgRCChangedSizeThreshold, $wgLang;
597
598 if( $old === 0 ) {
599 $old = $this->mAttribs['rc_old_len'];
600 }
601 if( $new === 0 ) {
602 $new = $this->mAttribs['rc_new_len'];
603 }
604
605 if( $old === NULL || $new === NULL ) {
606 return '';
607 }
608
609 $szdiff = $new - $old;
610 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
611 $wgLang->formatNum($szdiff) );
612
613 if( $szdiff < $wgRCChangedSizeThreshold ) {
614 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
615 } elseif( $szdiff === 0 ) {
616 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
617 } elseif( $szdiff > 0 ) {
618 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
619 } else {
620 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';
621 }
622 }
623 }
624 ?>