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