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