Specify max compatible kss version
[mediawiki.git] / includes / changes / RecentChange.php
blobb430bab91ca12b10a3de6dc9ca31471aa077d110
1 <?php
2 /**
3 * Utility class for creating and accessing recent change entries.
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.
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.
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
20 * @file
23 /**
24 * Utility class for creating new RC entries
26 * mAttribs:
27 * rc_id id of the row in the recentchanges table
28 * rc_timestamp time the entry was made
29 * rc_namespace namespace #
30 * rc_title non-prefixed db key
31 * rc_type is new entry, used to determine whether updating is necessary
32 * rc_source string representation of change source
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
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 * oldSize text size before the change
56 * newSize text size after the change
57 * pageStatus status of the page: created, deleted, moved, restored, changed
59 * temporary: not stored in the database
60 * notificationtimestamp
61 * numberofWatchingusers
63 class RecentChange {
64 // Constants for the rc_source field. Extensions may also have
65 // their own source constants.
66 const SRC_EDIT = 'mw.edit';
67 const SRC_NEW = 'mw.new';
68 const SRC_LOG = 'mw.log';
69 const SRC_EXTERNAL = 'mw.external'; // obsolete
71 public $mAttribs = array();
72 public $mExtra = array();
74 /**
75 * @var Title
77 public $mTitle = false;
79 /**
80 * @var User
82 private $mPerformer = false;
84 public $numberofWatchingusers = 0; # Dummy to prevent error message in SpecialRecentChangesLinked
85 public $notificationtimestamp;
87 /**
88 * @var int Line number of recent change. Default -1.
90 public $counter = -1;
92 # Factory methods
94 /**
95 * @param mixed $row
96 * @return RecentChange
98 public static function newFromRow( $row ) {
99 $rc = new RecentChange;
100 $rc->loadFromRow( $row );
102 return $rc;
106 * Parsing text to RC_* constants
107 * @since 1.24
108 * @param string|array $type
109 * @throws MWException
110 * @return int|array RC_TYPE
112 public static function parseToRCType( $type ) {
113 if ( is_array( $type ) ) {
114 $retval = array();
115 foreach ( $type as $t ) {
116 $retval[] = RecentChange::parseToRCType( $t );
119 return $retval;
122 switch ( $type ) {
123 case 'edit':
124 return RC_EDIT;
125 case 'new':
126 return RC_NEW;
127 case 'log':
128 return RC_LOG;
129 case 'external':
130 return RC_EXTERNAL;
131 default:
132 throw new MWException( "Unknown type '$type'" );
137 * Parsing RC_* constants to human-readable test
138 * @since 1.24
139 * @param int $rcType
140 * @return string $type
142 public static function parseFromRCType( $rcType ) {
143 switch ( $rcType ) {
144 case RC_EDIT:
145 $type = 'edit';
146 break;
147 case RC_NEW:
148 $type = 'new';
149 break;
150 case RC_LOG:
151 $type = 'log';
152 break;
153 case RC_EXTERNAL:
154 $type = 'external';
155 break;
156 default:
157 $type = "$rcType";
160 return $type;
164 * Obtain the recent change with a given rc_id value
166 * @param int $rcid The rc_id value to retrieve
167 * @return RecentChange
169 public static function newFromId( $rcid ) {
170 return self::newFromConds( array( 'rc_id' => $rcid ), __METHOD__ );
174 * Find the first recent change matching some specific conditions
176 * @param array $conds Array of conditions
177 * @param mixed $fname Override the method name in profiling/logs
178 * @param array $options Query options
179 * @return RecentChange
181 public static function newFromConds( $conds, $fname = __METHOD__, $options = array() ) {
182 $dbr = wfGetDB( DB_SLAVE );
183 $row = $dbr->selectRow( 'recentchanges', self::selectFields(), $conds, $fname, $options );
184 if ( $row !== false ) {
185 return self::newFromRow( $row );
186 } else {
187 return null;
192 * Return the list of recentchanges fields that should be selected to create
193 * a new recentchanges object.
194 * @return array
196 public static function selectFields() {
197 return array(
198 'rc_id',
199 'rc_timestamp',
200 'rc_user',
201 'rc_user_text',
202 'rc_namespace',
203 'rc_title',
204 'rc_comment',
205 'rc_minor',
206 'rc_bot',
207 'rc_new',
208 'rc_cur_id',
209 'rc_this_oldid',
210 'rc_last_oldid',
211 'rc_type',
212 'rc_source',
213 'rc_patrolled',
214 'rc_ip',
215 'rc_old_len',
216 'rc_new_len',
217 'rc_deleted',
218 'rc_logid',
219 'rc_log_type',
220 'rc_log_action',
221 'rc_params',
225 # Accessors
228 * @param array $attribs
230 public function setAttribs( $attribs ) {
231 $this->mAttribs = $attribs;
235 * @param array $extra
237 public function setExtra( $extra ) {
238 $this->mExtra = $extra;
242 * @return Title
244 public function &getTitle() {
245 if ( $this->mTitle === false ) {
246 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
249 return $this->mTitle;
253 * Get the User object of the person who performed this change.
255 * @return User
257 public function getPerformer() {
258 if ( $this->mPerformer === false ) {
259 if ( $this->mAttribs['rc_user'] ) {
260 $this->mPerformer = User::newFromId( $this->mAttribs['rc_user'] );
261 } else {
262 $this->mPerformer = User::newFromName( $this->mAttribs['rc_user_text'], false );
266 return $this->mPerformer;
270 * Writes the data in this object to the database
271 * @param bool $noudp
273 public function save( $noudp = false ) {
274 global $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker, $wgContLang;
276 $dbw = wfGetDB( DB_MASTER );
277 if ( !is_array( $this->mExtra ) ) {
278 $this->mExtra = array();
281 if ( !$wgPutIPinRC ) {
282 $this->mAttribs['rc_ip'] = '';
285 # If our database is strict about IP addresses, use NULL instead of an empty string
286 if ( $dbw->strictIPs() && $this->mAttribs['rc_ip'] == '' ) {
287 unset( $this->mAttribs['rc_ip'] );
290 # Trim spaces on user supplied text
291 $this->mAttribs['rc_comment'] = trim( $this->mAttribs['rc_comment'] );
293 # Make sure summary is truncated (whole multibyte characters)
294 $this->mAttribs['rc_comment'] = $wgContLang->truncate( $this->mAttribs['rc_comment'], 255 );
296 # Fixup database timestamps
297 $this->mAttribs['rc_timestamp'] = $dbw->timestamp( $this->mAttribs['rc_timestamp'] );
298 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
300 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
301 if ( $dbw->cascadingDeletes() && $this->mAttribs['rc_cur_id'] == 0 ) {
302 unset( $this->mAttribs['rc_cur_id'] );
305 # Insert new row
306 $dbw->insert( 'recentchanges', $this->mAttribs, __METHOD__ );
308 # Set the ID
309 $this->mAttribs['rc_id'] = $dbw->insertId();
311 # Notify extensions
312 Hooks::run( 'RecentChange_save', array( &$this ) );
314 # Notify external application via UDP
315 if ( !$noudp ) {
316 $this->notifyRCFeeds();
319 # E-mail notifications
320 if ( $wgUseEnotif || $wgShowUpdatedMarker ) {
321 $editor = $this->getPerformer();
322 $title = $this->getTitle();
324 if ( Hooks::run( 'AbortEmailNotification', array( $editor, $title, $this ) ) ) {
325 # @todo FIXME: This would be better as an extension hook
326 $enotif = new EmailNotification();
327 $enotif->notifyOnPageChange( $editor, $title,
328 $this->mAttribs['rc_timestamp'],
329 $this->mAttribs['rc_comment'],
330 $this->mAttribs['rc_minor'],
331 $this->mAttribs['rc_last_oldid'],
332 $this->mExtra['pageStatus'] );
338 * Notify all the feeds about the change.
339 * @param array $feeds Optional feeds to send to, defaults to $wgRCFeeds
341 public function notifyRCFeeds( array $feeds = null ) {
342 global $wgRCFeeds;
343 if ( $feeds === null ) {
344 $feeds = $wgRCFeeds;
347 $performer = $this->getPerformer();
349 foreach ( $feeds as $feed ) {
350 $feed += array(
351 'omit_bots' => false,
352 'omit_anon' => false,
353 'omit_user' => false,
354 'omit_minor' => false,
355 'omit_patrolled' => false,
358 if (
359 ( $feed['omit_bots'] && $this->mAttribs['rc_bot'] ) ||
360 ( $feed['omit_anon'] && $performer->isAnon() ) ||
361 ( $feed['omit_user'] && !$performer->isAnon() ) ||
362 ( $feed['omit_minor'] && $this->mAttribs['rc_minor'] ) ||
363 ( $feed['omit_patrolled'] && $this->mAttribs['rc_patrolled'] ) ||
364 $this->mAttribs['rc_type'] == RC_EXTERNAL
366 continue;
369 $engine = self::getEngine( $feed['uri'] );
371 if ( isset( $this->mExtra['actionCommentIRC'] ) ) {
372 $actionComment = $this->mExtra['actionCommentIRC'];
373 } else {
374 $actionComment = null;
377 /** @var $formatter RCFeedFormatter */
378 $formatter = is_object( $feed['formatter'] ) ? $feed['formatter'] : new $feed['formatter']();
379 $line = $formatter->getLine( $feed, $this, $actionComment );
381 $engine->send( $feed, $line );
386 * Gets the stream engine object for a given URI from $wgRCEngines
388 * @param string $uri URI to get the engine object for
389 * @throws MWException
390 * @return RCFeedEngine The engine object
392 public static function getEngine( $uri ) {
393 global $wgRCEngines;
395 $scheme = parse_url( $uri, PHP_URL_SCHEME );
396 if ( !$scheme ) {
397 throw new MWException( __FUNCTION__ . ": Invalid stream logger URI: '$uri'" );
400 if ( !isset( $wgRCEngines[$scheme] ) ) {
401 throw new MWException( __FUNCTION__ . ": Unknown stream logger URI scheme: $scheme" );
404 return new $wgRCEngines[$scheme];
408 * Mark a given change as patrolled
410 * @param RecentChange|int $change RecentChange or corresponding rc_id
411 * @param bool $auto For automatic patrol
412 * @return array See doMarkPatrolled(), or null if $change is not an existing rc_id
414 public static function markPatrolled( $change, $auto = false ) {
415 global $wgUser;
417 $change = $change instanceof RecentChange
418 ? $change
419 : RecentChange::newFromId( $change );
421 if ( !$change instanceof RecentChange ) {
422 return null;
425 return $change->doMarkPatrolled( $wgUser, $auto );
429 * Mark this RecentChange as patrolled
431 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
432 * 'markedaspatrollederror-noautopatrol' as errors
433 * @param User $user User object doing the action
434 * @param bool $auto For automatic patrol
435 * @return array Array of permissions errors, see Title::getUserPermissionsErrors()
437 public function doMarkPatrolled( User $user, $auto = false ) {
438 global $wgUseRCPatrol, $wgUseNPPatrol;
439 $errors = array();
440 // If recentchanges patrol is disabled, only new pages
441 // can be patrolled
442 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol || $this->getAttribute( 'rc_type' ) != RC_NEW ) ) {
443 $errors[] = array( 'rcpatroldisabled' );
445 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
446 $right = $auto ? 'autopatrol' : 'patrol';
447 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
448 if ( !Hooks::run( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
449 $errors[] = array( 'hookaborted' );
451 // Users without the 'autopatrol' right can't patrol their
452 // own revisions
453 if ( $user->getName() === $this->getAttribute( 'rc_user_text' )
454 && !$user->isAllowed( 'autopatrol' )
456 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
458 if ( $errors ) {
459 return $errors;
461 // If the change was patrolled already, do nothing
462 if ( $this->getAttribute( 'rc_patrolled' ) ) {
463 return array();
465 // Actually set the 'patrolled' flag in RC
466 $this->reallyMarkPatrolled();
467 // Log this patrol event
468 PatrolLog::record( $this, $auto, $user );
469 Hooks::run( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
471 return array();
475 * Mark this RecentChange patrolled, without error checking
476 * @return int Number of affected rows
478 public function reallyMarkPatrolled() {
479 $dbw = wfGetDB( DB_MASTER );
480 $dbw->update(
481 'recentchanges',
482 array(
483 'rc_patrolled' => 1
485 array(
486 'rc_id' => $this->getAttribute( 'rc_id' )
488 __METHOD__
490 // Invalidate the page cache after the page has been patrolled
491 // to make sure that the Patrol link isn't visible any longer!
492 $this->getTitle()->invalidateCache();
494 return $dbw->affectedRows();
498 * Makes an entry in the database corresponding to an edit
500 * @param string $timestamp
501 * @param Title $title
502 * @param bool $minor
503 * @param User $user
504 * @param string $comment
505 * @param int $oldId
506 * @param string $lastTimestamp
507 * @param bool $bot
508 * @param string $ip
509 * @param int $oldSize
510 * @param int $newSize
511 * @param int $newId
512 * @param int $patrol
513 * @return RecentChange
515 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
516 $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) {
517 $rc = new RecentChange;
518 $rc->mTitle = $title;
519 $rc->mPerformer = $user;
520 $rc->mAttribs = array(
521 'rc_timestamp' => $timestamp,
522 'rc_namespace' => $title->getNamespace(),
523 'rc_title' => $title->getDBkey(),
524 'rc_type' => RC_EDIT,
525 'rc_source' => self::SRC_EDIT,
526 'rc_minor' => $minor ? 1 : 0,
527 'rc_cur_id' => $title->getArticleID(),
528 'rc_user' => $user->getId(),
529 'rc_user_text' => $user->getName(),
530 'rc_comment' => $comment,
531 'rc_this_oldid' => $newId,
532 'rc_last_oldid' => $oldId,
533 'rc_bot' => $bot ? 1 : 0,
534 'rc_ip' => self::checkIPAddress( $ip ),
535 'rc_patrolled' => intval( $patrol ),
536 'rc_new' => 0, # obsolete
537 'rc_old_len' => $oldSize,
538 'rc_new_len' => $newSize,
539 'rc_deleted' => 0,
540 'rc_logid' => 0,
541 'rc_log_type' => null,
542 'rc_log_action' => '',
543 'rc_params' => ''
546 $rc->mExtra = array(
547 'prefixedDBkey' => $title->getPrefixedDBkey(),
548 'lastTimestamp' => $lastTimestamp,
549 'oldSize' => $oldSize,
550 'newSize' => $newSize,
551 'pageStatus' => 'changed'
553 $rc->save();
555 return $rc;
559 * Makes an entry in the database corresponding to page creation
560 * Note: the title object must be loaded with the new id using resetArticleID()
562 * @param string $timestamp
563 * @param Title $title
564 * @param bool $minor
565 * @param User $user
566 * @param string $comment
567 * @param bool $bot
568 * @param string $ip
569 * @param int $size
570 * @param int $newId
571 * @param int $patrol
572 * @return RecentChange
574 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
575 $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
576 $rc = new RecentChange;
577 $rc->mTitle = $title;
578 $rc->mPerformer = $user;
579 $rc->mAttribs = array(
580 'rc_timestamp' => $timestamp,
581 'rc_namespace' => $title->getNamespace(),
582 'rc_title' => $title->getDBkey(),
583 'rc_type' => RC_NEW,
584 'rc_source' => self::SRC_NEW,
585 'rc_minor' => $minor ? 1 : 0,
586 'rc_cur_id' => $title->getArticleID(),
587 'rc_user' => $user->getId(),
588 'rc_user_text' => $user->getName(),
589 'rc_comment' => $comment,
590 'rc_this_oldid' => $newId,
591 'rc_last_oldid' => 0,
592 'rc_bot' => $bot ? 1 : 0,
593 'rc_ip' => self::checkIPAddress( $ip ),
594 'rc_patrolled' => intval( $patrol ),
595 'rc_new' => 1, # obsolete
596 'rc_old_len' => 0,
597 'rc_new_len' => $size,
598 'rc_deleted' => 0,
599 'rc_logid' => 0,
600 'rc_log_type' => null,
601 'rc_log_action' => '',
602 'rc_params' => ''
605 $rc->mExtra = array(
606 'prefixedDBkey' => $title->getPrefixedDBkey(),
607 'lastTimestamp' => 0,
608 'oldSize' => 0,
609 'newSize' => $size,
610 'pageStatus' => 'created'
612 $rc->save();
614 return $rc;
618 * @param string $timestamp
619 * @param Title $title
620 * @param User $user
621 * @param string $actionComment
622 * @param string $ip
623 * @param string $type
624 * @param string $action
625 * @param Title $target
626 * @param string $logComment
627 * @param string $params
628 * @param int $newId
629 * @param string $actionCommentIRC
630 * @return bool
632 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
633 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
635 global $wgLogRestrictions;
637 # Don't add private logs to RC!
638 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
639 return false;
641 $rc = self::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
642 $target, $logComment, $params, $newId, $actionCommentIRC );
643 $rc->save();
645 return true;
649 * @param string $timestamp
650 * @param Title $title
651 * @param User $user
652 * @param string $actionComment
653 * @param string $ip
654 * @param string $type
655 * @param string $action
656 * @param Title $target
657 * @param string $logComment
658 * @param string $params
659 * @param int $newId
660 * @param string $actionCommentIRC
661 * @return RecentChange
663 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
664 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
665 global $wgRequest;
667 ## Get pageStatus for email notification
668 switch ( $type . '-' . $action ) {
669 case 'delete-delete':
670 $pageStatus = 'deleted';
671 break;
672 case 'move-move':
673 case 'move-move_redir':
674 $pageStatus = 'moved';
675 break;
676 case 'delete-restore':
677 $pageStatus = 'restored';
678 break;
679 case 'upload-upload':
680 $pageStatus = 'created';
681 break;
682 case 'upload-overwrite':
683 default:
684 $pageStatus = 'changed';
685 break;
688 $rc = new RecentChange;
689 $rc->mTitle = $target;
690 $rc->mPerformer = $user;
691 $rc->mAttribs = array(
692 'rc_timestamp' => $timestamp,
693 'rc_namespace' => $target->getNamespace(),
694 'rc_title' => $target->getDBkey(),
695 'rc_type' => RC_LOG,
696 'rc_source' => self::SRC_LOG,
697 'rc_minor' => 0,
698 'rc_cur_id' => $target->getArticleID(),
699 'rc_user' => $user->getId(),
700 'rc_user_text' => $user->getName(),
701 'rc_comment' => $logComment,
702 'rc_this_oldid' => 0,
703 'rc_last_oldid' => 0,
704 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot', true ) : 0,
705 'rc_ip' => self::checkIPAddress( $ip ),
706 'rc_patrolled' => 1,
707 'rc_new' => 0, # obsolete
708 'rc_old_len' => null,
709 'rc_new_len' => null,
710 'rc_deleted' => 0,
711 'rc_logid' => $newId,
712 'rc_log_type' => $type,
713 'rc_log_action' => $action,
714 'rc_params' => $params
717 $rc->mExtra = array(
718 'prefixedDBkey' => $title->getPrefixedDBkey(),
719 'lastTimestamp' => 0,
720 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
721 'pageStatus' => $pageStatus,
722 'actionCommentIRC' => $actionCommentIRC
725 return $rc;
729 * Initialises the members of this object from a mysql row object
731 * @param mixed $row
733 public function loadFromRow( $row ) {
734 $this->mAttribs = get_object_vars( $row );
735 $this->mAttribs['rc_timestamp'] = wfTimestamp( TS_MW, $this->mAttribs['rc_timestamp'] );
736 $this->mAttribs['rc_deleted'] = $row->rc_deleted; // MUST be set
740 * Get an attribute value
742 * @param string $name Attribute name
743 * @return mixed
745 public function getAttribute( $name ) {
746 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : null;
750 * @return array
752 public function getAttributes() {
753 return $this->mAttribs;
757 * Gets the end part of the diff URL associated with this object
758 * Blank if no diff link should be displayed
759 * @param bool $forceCur
760 * @return string
762 public function diffLinkTrail( $forceCur ) {
763 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
764 $trail = "curid=" . (int)( $this->mAttribs['rc_cur_id'] ) .
765 "&oldid=" . (int)( $this->mAttribs['rc_last_oldid'] );
766 if ( $forceCur ) {
767 $trail .= '&diff=0';
768 } else {
769 $trail .= '&diff=' . (int)( $this->mAttribs['rc_this_oldid'] );
771 } else {
772 $trail = '';
775 return $trail;
779 * Returns the change size (HTML).
780 * The lengths can be given optionally.
781 * @param int $old
782 * @param int $new
783 * @return string
785 public function getCharacterDifference( $old = 0, $new = 0 ) {
786 if ( $old === 0 ) {
787 $old = $this->mAttribs['rc_old_len'];
789 if ( $new === 0 ) {
790 $new = $this->mAttribs['rc_new_len'];
792 if ( $old === null || $new === null ) {
793 return '';
796 return ChangesList::showCharacterDifference( $old, $new );
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 .
804 "\" as an IP address into recent changes" );
806 } else {
807 $ip = $wgRequest->getIP();
808 if ( !$ip ) {
809 $ip = '';
813 return $ip;
817 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
818 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
819 * or rows which will be deleted soon shouldn't be included.
821 * @param mixed $timestamp MWTimestamp compatible timestamp
822 * @param int $tolerance Tolerance in seconds
823 * @return bool
825 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
826 global $wgRCMaxAge;
828 return wfTimestamp( TS_UNIX, $timestamp ) > time() - $tolerance - $wgRCMaxAge;