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