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