Followup-To: r113888 Remove extra wfDebug()
[lhc/web/wiklou.git] / includes / RecentChange.php
1 <?php
2
3 /**
4 * Utility class for creating new RC entries
5 *
6 * mAttribs:
7 * rc_id id of the row in the recentchanges table
8 * rc_timestamp time the entry was made
9 * rc_cur_time timestamp on the cur row
10 * rc_namespace namespace #
11 * rc_title non-prefixed db key
12 * rc_type is new entry, used to determine whether updating is necessary
13 * rc_minor is minor
14 * rc_cur_id page_id of associated page entry
15 * rc_user user id who made the entry
16 * rc_user_text user name who made the entry
17 * rc_comment edit summary
18 * rc_this_oldid rev_id associated with this entry (or zero)
19 * rc_last_oldid rev_id associated with the entry before this one (or zero)
20 * rc_bot is bot, hidden
21 * rc_ip IP address of the user in dotted quad notation
22 * rc_new obsolete, use rc_type==RC_NEW
23 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
24 * rc_old_len integer byte length of the text before the edit
25 * rc_new_len the same after the edit
26 * rc_deleted partial deletion
27 * rc_logid the log_id value for this log entry (or zero)
28 * rc_log_type the log type (or null)
29 * rc_log_action the log action (or null)
30 * rc_params log params
31 *
32 * mExtra:
33 * prefixedDBkey prefixed db key, used by external app via msg queue
34 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
35 * lang the interwiki prefix, automatically set in save()
36 * oldSize text size before the change
37 * newSize text size after the change
38 *
39 * temporary: not stored in the database
40 * notificationtimestamp
41 * numberofWatchingusers
42 *
43 * @todo document functions and variables
44 */
45 class RecentChange {
46 var $mAttribs = array(), $mExtra = array();
47
48 /**
49 * @var Title
50 */
51 var $mTitle = false;
52
53 /**
54 * @var Title
55 */
56 var $mMovedToTitle = false;
57 var $numberofWatchingusers = 0 ; # Dummy to prevent error message in SpecialRecentchangeslinked
58 var $notificationtimestamp;
59
60 # Factory methods
61
62 /**
63 * @param $row
64 * @return RecentChange
65 */
66 public static function newFromRow( $row ) {
67 $rc = new RecentChange;
68 $rc->loadFromRow( $row );
69 return $rc;
70 }
71
72 /**
73 * @param $row
74 * @return RecentChange
75 */
76 public static function newFromCurRow( $row ) {
77 $rc = new RecentChange;
78 $rc->loadFromCurRow( $row );
79 $rc->notificationtimestamp = false;
80 $rc->numberofWatchingusers = false;
81 return $rc;
82 }
83
84 /**
85 * Obtain the recent change with a given rc_id value
86 *
87 * @param $rcid Int rc_id value to retrieve
88 * @return RecentChange
89 */
90 public static function newFromId( $rcid ) {
91 $dbr = wfGetDB( DB_SLAVE );
92 $row = $dbr->selectRow( 'recentchanges', '*', array( 'rc_id' => $rcid ), __METHOD__ );
93 if( $row !== false ) {
94 return self::newFromRow( $row );
95 } else {
96 return null;
97 }
98 }
99
100 /**
101 * Find the first recent change matching some specific conditions
102 *
103 * @param $conds Array of conditions
104 * @param $fname Mixed: override the method name in profiling/logs
105 * @return RecentChange
106 */
107 public static function newFromConds( $conds, $fname = __METHOD__ ) {
108 $dbr = wfGetDB( DB_SLAVE );
109 $res = $dbr->select(
110 'recentchanges',
111 '*',
112 $conds,
113 $fname
114 );
115 if( $res instanceof ResultWrapper && $res->numRows() > 0 ) {
116 $row = $res->fetchObject();
117 $res->free();
118 return self::newFromRow( $row );
119 }
120 return null;
121 }
122
123 # Accessors
124
125 /**
126 * @param $attribs array
127 */
128 public function setAttribs( $attribs ) {
129 $this->mAttribs = $attribs;
130 }
131
132 /**
133 * @param $extra array
134 */
135 public function setExtra( $extra ) {
136 $this->mExtra = $extra;
137 }
138
139 /**
140 *
141 * @return Title
142 */
143 public function &getTitle() {
144 if( $this->mTitle === false ) {
145 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
146 # Make sure the correct page ID is process cached
147 $this->mTitle->resetArticleID( $this->mAttribs['rc_cur_id'] );
148 }
149 return $this->mTitle;
150 }
151
152 /**
153 * @return bool|\Title
154 */
155 public function getMovedToTitle() {
156 if( $this->mMovedToTitle === false ) {
157 $this->mMovedToTitle = Title::makeTitle( $this->mAttribs['rc_moved_to_ns'],
158 $this->mAttribs['rc_moved_to_title'] );
159 }
160 return $this->mMovedToTitle;
161 }
162
163 /**
164 * Writes the data in this object to the database
165 * @param $noudp bool
166 */
167 public function save( $noudp = false ) {
168 global $wgLocalInterwiki, $wgPutIPinRC, $wgContLang;
169
170 $dbw = wfGetDB( DB_MASTER );
171 if( !is_array($this->mExtra) ) {
172 $this->mExtra = array();
173 }
174 $this->mExtra['lang'] = $wgLocalInterwiki;
175
176 if( !$wgPutIPinRC ) {
177 $this->mAttribs['rc_ip'] = '';
178 }
179
180 # If our database is strict about IP addresses, use NULL instead of an empty string
181 if( $dbw->strictIPs() and $this->mAttribs['rc_ip'] == '' ) {
182 unset( $this->mAttribs['rc_ip'] );
183 }
184
185 # Make sure summary is truncated (whole multibyte characters)
186 $this->mAttribs['rc_comment'] = $wgContLang->truncate( $this->mAttribs['rc_comment'], 255 );
187
188 # Fixup database timestamps
189 $this->mAttribs['rc_timestamp'] = $dbw->timestamp($this->mAttribs['rc_timestamp']);
190 $this->mAttribs['rc_cur_time'] = $dbw->timestamp($this->mAttribs['rc_cur_time']);
191 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
192
193 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
194 if( $dbw->cascadingDeletes() and $this->mAttribs['rc_cur_id']==0 ) {
195 unset( $this->mAttribs['rc_cur_id'] );
196 }
197
198 # Insert new row
199 $dbw->insert( 'recentchanges', $this->mAttribs, __METHOD__ );
200
201 # Set the ID
202 $this->mAttribs['rc_id'] = $dbw->insertId();
203
204 # Notify extensions
205 wfRunHooks( 'RecentChange_save', array( &$this ) );
206
207 # Notify external application via UDP
208 if ( !$noudp ) {
209 $this->notifyRC2UDP();
210 }
211
212 # E-mail notifications
213 global $wgUseEnotif, $wgShowUpdatedMarker, $wgUser;
214 if( $wgUseEnotif || $wgShowUpdatedMarker ) {
215 // Users
216 if( $this->mAttribs['rc_user'] ) {
217 $editor = ($wgUser->getId() == $this->mAttribs['rc_user']) ?
218 $wgUser : User::newFromID( $this->mAttribs['rc_user'] );
219 // Anons
220 } else {
221 $editor = ($wgUser->getName() == $this->mAttribs['rc_user_text']) ?
222 $wgUser : User::newFromName( $this->mAttribs['rc_user_text'], false );
223 }
224 $title = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
225
226 # @todo FIXME: This would be better as an extension hook
227 $enotif = new EmailNotification();
228 $status = $enotif->notifyOnPageChange( $editor, $title,
229 $this->mAttribs['rc_timestamp'],
230 $this->mAttribs['rc_comment'],
231 $this->mAttribs['rc_minor'],
232 $this->mAttribs['rc_last_oldid'] );
233 }
234 }
235
236 public function notifyRC2UDP() {
237 global $wgRC2UDPAddress, $wgRC2UDPOmitBots;
238 # Notify external application via UDP
239 if( $wgRC2UDPAddress && ( !$this->mAttribs['rc_bot'] || !$wgRC2UDPOmitBots ) ) {
240 self::sendToUDP( $this->getIRCLine() );
241 }
242 }
243
244 /**
245 * Send some text to UDP.
246 * @see RecentChange::cleanupForIRC
247 * @param $line String: text to send
248 * @param $address String: defaults to $wgRC2UDPAddress.
249 * @param $prefix String: defaults to $wgRC2UDPPrefix.
250 * @param $port Int: defaults to $wgRC2UDPPort. (Since 1.17)
251 * @return Boolean: success
252 */
253 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) {
254 global $wgRC2UDPAddress, $wgRC2UDPPrefix, $wgRC2UDPPort;
255 # Assume default for standard RC case
256 $address = $address ? $address : $wgRC2UDPAddress;
257 $prefix = $prefix ? $prefix : $wgRC2UDPPrefix;
258 $port = $port ? $port : $wgRC2UDPPort;
259 # Notify external application via UDP
260 if( $address ) {
261 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
262 if( $conn ) {
263 $line = $prefix . $line;
264 wfDebug( __METHOD__ . ": sending UDP line: $line\n" );
265 socket_sendto( $conn, $line, strlen($line), 0, $address, $port );
266 socket_close( $conn );
267 return true;
268 } else {
269 wfDebug( __METHOD__ . ": failed to create UDP socket\n" );
270 }
271 }
272 return false;
273 }
274
275 /**
276 * Remove newlines, carriage returns and decode html entites
277 * @param $text String
278 * @return String
279 */
280 public static function cleanupForIRC( $text ) {
281 return Sanitizer::decodeCharReferences( str_replace( array( "\n", "\r" ), array( "", "" ), $text ) );
282 }
283
284 /**
285 * Mark a given change as patrolled
286 *
287 * @param $change Mixed: RecentChange or corresponding rc_id
288 * @param $auto Boolean: for automatic patrol
289 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
290 */
291 public static function markPatrolled( $change, $auto = false ) {
292 global $wgUser;
293
294 $change = $change instanceof RecentChange
295 ? $change
296 : RecentChange::newFromId($change);
297
298 if( !$change instanceof RecentChange ) {
299 return null;
300 }
301 return $change->doMarkPatrolled( $wgUser, $auto );
302 }
303
304 /**
305 * Mark this RecentChange as patrolled
306 *
307 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
308 * @param $user User object doing the action
309 * @param $auto Boolean: for automatic patrol
310 * @return array of permissions errors, see Title::getUserPermissionsErrors()
311 */
312 public function doMarkPatrolled( User $user, $auto = false ) {
313 global $wgUseRCPatrol, $wgUseNPPatrol;
314 $errors = array();
315 // If recentchanges patrol is disabled, only new pages
316 // can be patrolled
317 if( !$wgUseRCPatrol && ( !$wgUseNPPatrol || $this->getAttribute('rc_type') != RC_NEW ) ) {
318 $errors[] = array('rcpatroldisabled');
319 }
320 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
321 $right = $auto ? 'autopatrol' : 'patrol';
322 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
323 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$user, false)) ) {
324 $errors[] = array('hookaborted');
325 }
326 // Users without the 'autopatrol' right can't patrol their
327 // own revisions
328 if( $user->getName() == $this->getAttribute('rc_user_text') && !$user->isAllowed('autopatrol') ) {
329 $errors[] = array('markedaspatrollederror-noautopatrol');
330 }
331 if( $errors ) {
332 return $errors;
333 }
334 // If the change was patrolled already, do nothing
335 if( $this->getAttribute('rc_patrolled') ) {
336 return array();
337 }
338 // Actually set the 'patrolled' flag in RC
339 $this->reallyMarkPatrolled();
340 // Log this patrol event
341 PatrolLog::record( $this, $auto, $user );
342 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$user, false) );
343 return array();
344 }
345
346 /**
347 * Mark this RecentChange patrolled, without error checking
348 * @return Integer: number of affected rows
349 */
350 public function reallyMarkPatrolled() {
351 $dbw = wfGetDB( DB_MASTER );
352 $dbw->update(
353 'recentchanges',
354 array(
355 'rc_patrolled' => 1
356 ),
357 array(
358 'rc_id' => $this->getAttribute('rc_id')
359 ),
360 __METHOD__
361 );
362 return $dbw->affectedRows();
363 }
364
365 /**
366 * Makes an entry in the database corresponding to an edit
367 *
368 * @param $timestamp
369 * @param $title Title
370 * @param $minor
371 * @param $user User
372 * @param $comment
373 * @param $oldId
374 * @param $lastTimestamp
375 * @param $bot
376 * @param $ip string
377 * @param $oldSize int
378 * @param $newSize int
379 * @param $newId int
380 * @param $patrol int
381 * @return RecentChange
382 */
383 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
384 $lastTimestamp, $bot, $ip='', $oldSize=0, $newSize=0, $newId=0, $patrol=0 ) {
385 $rc = new RecentChange;
386 $rc->mAttribs = array(
387 'rc_timestamp' => $timestamp,
388 'rc_cur_time' => $timestamp,
389 'rc_namespace' => $title->getNamespace(),
390 'rc_title' => $title->getDBkey(),
391 'rc_type' => RC_EDIT,
392 'rc_minor' => $minor ? 1 : 0,
393 'rc_cur_id' => $title->getArticleID(),
394 'rc_user' => $user->getId(),
395 'rc_user_text' => $user->getName(),
396 'rc_comment' => $comment,
397 'rc_this_oldid' => $newId,
398 'rc_last_oldid' => $oldId,
399 'rc_bot' => $bot ? 1 : 0,
400 'rc_moved_to_ns' => 0,
401 'rc_moved_to_title' => '',
402 'rc_ip' => self::checkIPAddress( $ip ),
403 'rc_patrolled' => intval($patrol),
404 'rc_new' => 0, # obsolete
405 'rc_old_len' => $oldSize,
406 'rc_new_len' => $newSize,
407 'rc_deleted' => 0,
408 'rc_logid' => 0,
409 'rc_log_type' => null,
410 'rc_log_action' => '',
411 'rc_params' => ''
412 );
413
414 $rc->mExtra = array(
415 'prefixedDBkey' => $title->getPrefixedDBkey(),
416 'lastTimestamp' => $lastTimestamp,
417 'oldSize' => $oldSize,
418 'newSize' => $newSize,
419 );
420 $rc->save();
421 return $rc;
422 }
423
424 /**
425 * Makes an entry in the database corresponding to page creation
426 * Note: the title object must be loaded with the new id using resetArticleID()
427 * @todo Document parameters and return
428 *
429 * @param $timestamp
430 * @param $title Title
431 * @param $minor
432 * @param $user User
433 * @param $comment
434 * @param $bot
435 * @param $ip string
436 * @param $size int
437 * @param $newId int
438 * @param $patrol int
439 * @return RecentChange
440 */
441 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
442 $ip='', $size=0, $newId=0, $patrol=0 ) {
443 $rc = new RecentChange;
444 $rc->mAttribs = array(
445 'rc_timestamp' => $timestamp,
446 'rc_cur_time' => $timestamp,
447 'rc_namespace' => $title->getNamespace(),
448 'rc_title' => $title->getDBkey(),
449 'rc_type' => RC_NEW,
450 'rc_minor' => $minor ? 1 : 0,
451 'rc_cur_id' => $title->getArticleID(),
452 'rc_user' => $user->getId(),
453 'rc_user_text' => $user->getName(),
454 'rc_comment' => $comment,
455 'rc_this_oldid' => $newId,
456 'rc_last_oldid' => 0,
457 'rc_bot' => $bot ? 1 : 0,
458 'rc_moved_to_ns' => 0,
459 'rc_moved_to_title' => '',
460 'rc_ip' => self::checkIPAddress( $ip ),
461 'rc_patrolled' => intval($patrol),
462 'rc_new' => 1, # obsolete
463 'rc_old_len' => 0,
464 'rc_new_len' => $size,
465 'rc_deleted' => 0,
466 'rc_logid' => 0,
467 'rc_log_type' => null,
468 'rc_log_action' => '',
469 'rc_params' => ''
470 );
471
472 $rc->mExtra = array(
473 'prefixedDBkey' => $title->getPrefixedDBkey(),
474 'lastTimestamp' => 0,
475 'oldSize' => 0,
476 'newSize' => $size
477 );
478 $rc->save();
479 return $rc;
480 }
481
482 /**
483 * @param $timestamp
484 * @param $title
485 * @param $user
486 * @param $actionComment
487 * @param $ip string
488 * @param $type
489 * @param $action
490 * @param $target
491 * @param $logComment
492 * @param $params
493 * @param $newId int
494 * @param $actionCommentIRC string
495 * @return bool
496 */
497 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
498 $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='' )
499 {
500 global $wgLogRestrictions;
501 # Don't add private logs to RC!
502 if( isset($wgLogRestrictions[$type]) && $wgLogRestrictions[$type] != '*' ) {
503 return false;
504 }
505 $rc = self::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
506 $target, $logComment, $params, $newId, $actionCommentIRC );
507 $rc->save();
508 return true;
509 }
510
511 /**
512 * @param $timestamp
513 * @param $title Title
514 * @param $user User
515 * @param $actionComment
516 * @param $ip string
517 * @param $type
518 * @param $action
519 * @param $target Title
520 * @param $logComment
521 * @param $params
522 * @param $newId int
523 * @param $actionCommentIRC string
524 * @return RecentChange
525 */
526 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
527 $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='' ) {
528 global $wgRequest;
529
530 $rc = new RecentChange;
531 $rc->mAttribs = array(
532 'rc_timestamp' => $timestamp,
533 'rc_cur_time' => $timestamp,
534 'rc_namespace' => $target->getNamespace(),
535 'rc_title' => $target->getDBkey(),
536 'rc_type' => RC_LOG,
537 'rc_minor' => 0,
538 'rc_cur_id' => $target->getArticleID(),
539 'rc_user' => $user->getId(),
540 'rc_user_text' => $user->getName(),
541 'rc_comment' => $logComment,
542 'rc_this_oldid' => 0,
543 'rc_last_oldid' => 0,
544 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot', true ) : 0,
545 'rc_moved_to_ns' => 0,
546 'rc_moved_to_title' => '',
547 'rc_ip' => self::checkIPAddress( $ip ),
548 'rc_patrolled' => 1,
549 'rc_new' => 0, # obsolete
550 'rc_old_len' => null,
551 'rc_new_len' => null,
552 'rc_deleted' => 0,
553 'rc_logid' => $newId,
554 'rc_log_type' => $type,
555 'rc_log_action' => $action,
556 'rc_params' => $params
557 );
558
559 $rc->mExtra = array(
560 'prefixedDBkey' => $title->getPrefixedDBkey(),
561 'lastTimestamp' => 0,
562 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
563 'actionCommentIRC' => $actionCommentIRC
564 );
565 return $rc;
566 }
567
568 /**
569 * Initialises the members of this object from a mysql row object
570 *
571 * @param $row
572 */
573 public function loadFromRow( $row ) {
574 $this->mAttribs = get_object_vars( $row );
575 $this->mAttribs['rc_timestamp'] = wfTimestamp(TS_MW, $this->mAttribs['rc_timestamp']);
576 $this->mAttribs['rc_deleted'] = $row->rc_deleted; // MUST be set
577 }
578
579 /**
580 * Makes a pseudo-RC entry from a cur row
581 *
582 * @param $row
583 */
584 public function loadFromCurRow( $row ) {
585 $this->mAttribs = array(
586 'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp),
587 'rc_cur_time' => $row->rev_timestamp,
588 'rc_user' => $row->rev_user,
589 'rc_user_text' => $row->rev_user_text,
590 'rc_namespace' => $row->page_namespace,
591 'rc_title' => $row->page_title,
592 'rc_comment' => $row->rev_comment,
593 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
594 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
595 'rc_cur_id' => $row->page_id,
596 'rc_this_oldid' => $row->rev_id,
597 'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0,
598 'rc_bot' => 0,
599 'rc_moved_to_ns' => 0,
600 'rc_moved_to_title' => '',
601 'rc_ip' => '',
602 'rc_id' => $row->rc_id,
603 'rc_patrolled' => $row->rc_patrolled,
604 'rc_new' => $row->page_is_new, # obsolete
605 'rc_old_len' => $row->rc_old_len,
606 'rc_new_len' => $row->rc_new_len,
607 'rc_params' => isset($row->rc_params) ? $row->rc_params : '',
608 'rc_log_type' => isset($row->rc_log_type) ? $row->rc_log_type : null,
609 'rc_log_action' => isset($row->rc_log_action) ? $row->rc_log_action : null,
610 'rc_log_id' => isset($row->rc_log_id) ? $row->rc_log_id: 0,
611 'rc_deleted' => $row->rc_deleted // MUST be set
612 );
613 }
614
615 /**
616 * Get an attribute value
617 *
618 * @param $name String Attribute name
619 * @return mixed
620 */
621 public function getAttribute( $name ) {
622 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : null;
623 }
624
625 /**
626 * @return array
627 */
628 public function getAttributes() {
629 return $this->mAttribs;
630 }
631
632 /**
633 * Gets the end part of the diff URL associated with this object
634 * Blank if no diff link should be displayed
635 * @param $forceCur
636 * @return string
637 */
638 public function diffLinkTrail( $forceCur ) {
639 if( $this->mAttribs['rc_type'] == RC_EDIT ) {
640 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
641 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
642 if( $forceCur ) {
643 $trail .= '&diff=0' ;
644 } else {
645 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
646 }
647 } else {
648 $trail = '';
649 }
650 return $trail;
651 }
652
653 /**
654 * @return string
655 */
656 public function getIRCLine() {
657 global $wgUseRCPatrol, $wgUseNPPatrol, $wgRC2UDPInterwikiPrefix, $wgLocalInterwiki,
658 $wgCanonicalServer, $wgScript;
659
660 if( $this->mAttribs['rc_type'] == RC_LOG ) {
661 $titleObj = SpecialPage::getTitleFor( 'Log', $this->mAttribs['rc_log_type'] );
662 } else {
663 $titleObj =& $this->getTitle();
664 }
665 $title = $titleObj->getPrefixedText();
666 $title = self::cleanupForIRC( $title );
667
668 if( $this->mAttribs['rc_type'] == RC_LOG ) {
669 $url = '';
670 } else {
671 $url = $wgCanonicalServer . $wgScript;
672 if( $this->mAttribs['rc_type'] == RC_NEW ) {
673 $query = '?oldid=' . $this->mAttribs['rc_this_oldid'];
674 } else {
675 $query = '?diff=' . $this->mAttribs['rc_this_oldid'] . '&oldid=' . $this->mAttribs['rc_last_oldid'];
676 }
677 if ( $wgUseRCPatrol || ( $this->mAttribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) ) {
678 $query .= '&rcid=' . $this->mAttribs['rc_id'];
679 }
680 // HACK: We need this hook for WMF's secure server setup
681 wfRunHooks( 'IRCLineURL', array( &$url, &$query ) );
682 $url .= $query;
683 }
684
685 if( $this->mAttribs['rc_old_len'] !== null && $this->mAttribs['rc_new_len'] !== null ) {
686 $szdiff = $this->mAttribs['rc_new_len'] - $this->mAttribs['rc_old_len'];
687 if($szdiff < -500) {
688 $szdiff = "\002$szdiff\002";
689 } elseif($szdiff >= 0) {
690 $szdiff = '+' . $szdiff ;
691 }
692 // @todo i18n with parentheses in content language?
693 $szdiff = '(' . $szdiff . ')' ;
694 } else {
695 $szdiff = '';
696 }
697
698 $user = self::cleanupForIRC( $this->mAttribs['rc_user_text'] );
699
700 if ( $this->mAttribs['rc_type'] == RC_LOG ) {
701 $targetText = $this->getTitle()->getPrefixedText();
702 $comment = self::cleanupForIRC( str_replace( "[[$targetText]]", "[[\00302$targetText\00310]]", $this->mExtra['actionCommentIRC'] ) );
703 $flag = $this->mAttribs['rc_log_action'];
704 } else {
705 $comment = self::cleanupForIRC( $this->mAttribs['rc_comment'] );
706 $flag = '';
707 if ( !$this->mAttribs['rc_patrolled'] && ( $wgUseRCPatrol || $this->mAttribs['rc_new'] && $wgUseNPPatrol ) ) {
708 $flag .= '!';
709 }
710 $flag .= ( $this->mAttribs['rc_new'] ? "N" : "" ) . ( $this->mAttribs['rc_minor'] ? "M" : "" ) . ( $this->mAttribs['rc_bot'] ? "B" : "" );
711 }
712
713 if ( $wgRC2UDPInterwikiPrefix === true && $wgLocalInterwiki !== false ) {
714 $prefix = $wgLocalInterwiki;
715 } elseif ( $wgRC2UDPInterwikiPrefix ) {
716 $prefix = $wgRC2UDPInterwikiPrefix;
717 } else {
718 $prefix = false;
719 }
720 if ( $prefix !== false ) {
721 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
722 } else {
723 $titleString = "\00314[[\00307$title\00314]]";
724 }
725
726 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
727 # no colour (\003) switches back to the term default
728 $fullString = "$titleString\0034 $flag\00310 " .
729 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
730
731 return $fullString;
732 }
733
734 /**
735 * Returns the change size (HTML).
736 * The lengths can be given optionally.
737 * @param $old int
738 * @param $new int
739 * @return string
740 */
741 public function getCharacterDifference( $old = 0, $new = 0 ) {
742 if( $old === 0 ) {
743 $old = $this->mAttribs['rc_old_len'];
744 }
745 if( $new === 0 ) {
746 $new = $this->mAttribs['rc_new_len'];
747 }
748 if( $old === null || $new === null ) {
749 return '';
750 }
751 return ChangesList::showCharacterDifference( $old, $new );
752 }
753
754 public static function checkIPAddress( $ip ) {
755 global $wgRequest;
756 if ( $ip ) {
757 if ( !IP::isIPAddress( $ip ) ) {
758 throw new MWException( "Attempt to write \"" . $ip . "\" as an IP address into recent changes" );
759 }
760 } else {
761 $ip = $wgRequest->getIP();
762 if( !$ip )
763 $ip = '';
764 }
765 return $ip;
766 }
767 }