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