Fix to use the correct css class
[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 );
304
305 $rc->mExtra = array(
306 'prefixedDBkey' => $title->getPrefixedDBkey(),
307 'lastTimestamp' => 0,
308 'oldSize' => 0,
309 'newSize' => $size
310 );
311 $rc->save();
312 return( $rc->mAttribs['rc_id'] );
313 }
314
315 # Makes an entry in the database corresponding to a rename
316 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
317 {
318 if ( !$ip ) {
319 $ip = wfGetIP();
320 if ( !$ip ) {
321 $ip = '';
322 }
323 }
324
325 $rc = new RecentChange;
326 $rc->mAttribs = array(
327 'rc_timestamp' => $timestamp,
328 'rc_cur_time' => $timestamp,
329 'rc_namespace' => $oldTitle->getNamespace(),
330 'rc_title' => $oldTitle->getDBkey(),
331 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
332 'rc_minor' => 0,
333 'rc_cur_id' => $oldTitle->getArticleID(),
334 'rc_user' => $user->getID(),
335 'rc_user_text' => $user->getName(),
336 'rc_comment' => $comment,
337 'rc_this_oldid' => 0,
338 'rc_last_oldid' => 0,
339 'rc_bot' => $user->isAllowed( 'bot' ) ? 1 : 0,
340 'rc_moved_to_ns' => $newTitle->getNamespace(),
341 'rc_moved_to_title' => $newTitle->getDBkey(),
342 'rc_ip' => $ip,
343 'rc_new' => 0, # obsolete
344 'rc_patrolled' => 1,
345 'rc_old_len' => 0,
346 'rc_new_len' => 0,
347 );
348
349 $rc->mExtra = array(
350 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
351 'lastTimestamp' => 0,
352 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
353 );
354 $rc->save();
355 }
356
357 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
358 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
359 }
360
361 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
362 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
363 }
364
365 # A log entry is different to an edit in that previous revisions are
366 # not kept
367 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
368 $type, $action, $target, $logComment, $params )
369 {
370 if ( !$ip ) {
371 $ip = wfGetIP();
372 if ( !$ip ) {
373 $ip = '';
374 }
375 }
376
377 $rc = new RecentChange;
378 $rc->mAttribs = array(
379 'rc_timestamp' => $timestamp,
380 'rc_cur_time' => $timestamp,
381 'rc_namespace' => $title->getNamespace(),
382 'rc_title' => $title->getDBkey(),
383 'rc_type' => RC_LOG,
384 'rc_minor' => 0,
385 'rc_cur_id' => $title->getArticleID(),
386 'rc_user' => $user->getID(),
387 'rc_user_text' => $user->getName(),
388 'rc_comment' => $comment,
389 'rc_this_oldid' => 0,
390 'rc_last_oldid' => 0,
391 'rc_bot' => $user->isAllowed( 'bot' ) ? 1 : 0,
392 'rc_moved_to_ns' => 0,
393 'rc_moved_to_title' => '',
394 'rc_ip' => $ip,
395 'rc_patrolled' => 1,
396 'rc_new' => 0, # obsolete
397 'rc_old_len' => 0,
398 'rc_new_len' => 0,
399 );
400 $rc->mExtra = array(
401 'prefixedDBkey' => $title->getPrefixedDBkey(),
402 'lastTimestamp' => 0,
403 'logType' => $type,
404 'logAction' => $action,
405 'logComment' => $logComment,
406 'logTarget' => $target,
407 'logParams' => $params
408 );
409 $rc->save();
410 }
411
412 # Initialises the members of this object from a mysql row object
413 function loadFromRow( $row )
414 {
415 $this->mAttribs = get_object_vars( $row );
416 $this->mAttribs["rc_timestamp"] = wfTimestamp(TS_MW, $this->mAttribs["rc_timestamp"]);
417 $this->mExtra = array();
418 }
419
420 # Makes a pseudo-RC entry from a cur row, for watchlists and things
421 function loadFromCurRow( $row )
422 {
423 $this->mAttribs = array(
424 'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp),
425 'rc_cur_time' => $row->rev_timestamp,
426 'rc_user' => $row->rev_user,
427 'rc_user_text' => $row->rev_user_text,
428 'rc_namespace' => $row->page_namespace,
429 'rc_title' => $row->page_title,
430 'rc_comment' => $row->rev_comment,
431 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
432 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
433 'rc_cur_id' => $row->page_id,
434 'rc_this_oldid' => $row->rev_id,
435 'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0,
436 'rc_bot' => 0,
437 'rc_moved_to_ns' => 0,
438 'rc_moved_to_title' => '',
439 'rc_ip' => '',
440 'rc_id' => $row->rc_id,
441 'rc_patrolled' => $row->rc_patrolled,
442 'rc_new' => $row->page_is_new, # obsolete
443 'rc_old_len' => 0, # we can't get the text lengts from a cur row
444 'rc_new_len' => 0,
445 );
446
447 $this->mExtra = array();
448 }
449
450
451 /**
452 * Gets the end part of the diff URL associated with this object
453 * Blank if no diff link should be displayed
454 */
455 function diffLinkTrail( $forceCur )
456 {
457 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
458 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
459 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
460 if ( $forceCur ) {
461 $trail .= '&diff=0' ;
462 } else {
463 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
464 }
465 } else {
466 $trail = '';
467 }
468 return $trail;
469 }
470
471 function cleanupForIRC( $text ) {
472 return str_replace(array("\n", "\r"), array("", ""), $text);
473 }
474
475 function getIRCLine() {
476 global $wgUseRCPatrol;
477
478 extract($this->mAttribs);
479 extract($this->mExtra);
480
481 $titleObj =& $this->getTitle();
482 if ( $rc_type == RC_LOG ) {
483 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
484 } else {
485 $title = $titleObj->getPrefixedText();
486 }
487 $title = $this->cleanupForIRC( $title );
488
489 $bad = array("\n", "\r");
490 $empty = array("", "");
491 $title = $titleObj->getPrefixedText();
492 $title = str_replace($bad, $empty, $title);
493
494 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
495 if ( $rc_type == RC_LOG ) {
496 $url = '';
497 } elseif ( $rc_new && $wgUseRCPatrol ) {
498 $url = $titleObj->getInternalURL("rcid=$rc_id");
499 } else if ( $rc_new ) {
500 $url = $titleObj->getInternalURL();
501 } else if ( $wgUseRCPatrol ) {
502 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
503 } else {
504 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
505 }
506
507 if ( isset( $oldSize ) && isset( $newSize ) ) {
508 $szdiff = $newSize - $oldSize;
509 if ($szdiff < -500) {
510 $szdiff = "\002$szdiff\002";
511 } elseif ($szdiff >= 0) {
512 $szdiff = '+' . $szdiff ;
513 }
514 $szdiff = '(' . $szdiff . ')' ;
515 } else {
516 $szdiff = '';
517 }
518
519 $user = $this->cleanupForIRC( $rc_user_text );
520
521 if ( $rc_type == RC_LOG ) {
522 $logTargetText = $logTarget->getPrefixedText();
523 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
524 $flag = $logAction;
525 } else {
526 $comment = $this->cleanupForIRC( $rc_comment );
527 $flag = ($rc_minor ? "M" : "") . ($rc_new ? "N" : "");
528 }
529 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
530 # no colour (\003) switches back to the term default
531 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
532 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
533 return $fullString;
534 }
535
536 function getCharacterDifference() {
537 global $wgRCChangedSizeThreshold;
538 $szdiff = $this->mAttribs['rc_new_len'] - $this->mAttribs['rc_old_len'];
539
540 if( $szdiff < $wgRCChangedSizeThreshold ) {
541 return "<span class='mw-plusminus-bold'>($szdiff)</span>";
542 } elseif( $szdiff === 0 ) {
543 return "<span class='mw-plusminus-null'>($szdiff)</span>";
544 } elseif( $szdiff > 0 ) {
545 return "<span class='mw-plusminus-pos'>(+$szdiff)</span>";
546 } else {
547 return "<span class='mw-pluminus-neg'>($szdiff)</span>";
548 }
549 }
550 }
551 ?>