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