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