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