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
22 use MediaWiki\ChangeTags\Taggable
;
23 use MediaWiki\MediaWikiServices
;
26 * Utility class for creating new RC entries
29 * rc_id id of the row in the recentchanges table
30 * rc_timestamp time the entry was made
31 * rc_namespace namespace #
32 * rc_title non-prefixed db key
33 * rc_type is new entry, used to determine whether updating is necessary
34 * rc_source string representation of change source
36 * rc_cur_id page_id of associated page entry
37 * rc_user user id who made the entry
38 * rc_user_text user name who made the entry
39 * rc_actor actor id who made the entry
40 * rc_comment edit summary
41 * rc_this_oldid rev_id associated with this entry (or zero)
42 * rc_last_oldid rev_id associated with the entry before this one (or zero)
43 * rc_bot is bot, hidden
44 * rc_ip IP address of the user in dotted quad notation
45 * rc_new obsolete, use rc_type==RC_NEW
46 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
47 * rc_old_len integer byte length of the text before the edit
48 * rc_new_len the same after the edit
49 * rc_deleted partial deletion
50 * rc_logid the log_id value for this log entry (or zero)
51 * rc_log_type the log type (or null)
52 * rc_log_action the log action (or null)
53 * rc_params log params
56 * prefixedDBkey prefixed db key, used by external app via msg queue
57 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
58 * oldSize text size before the change
59 * newSize text size after the change
60 * pageStatus status of the page: created, deleted, moved, restored, changed
62 * temporary: not stored in the database
63 * notificationtimestamp
64 * numberofWatchingusers
66 * @todo Deprecate access to mAttribs (direct or via getAttributes). Right now
67 * we're having to include both rc_comment and rc_comment_text/rc_comment_data
68 * so random crap works right.
70 class RecentChange
implements Taggable
{
71 // Constants for the rc_source field. Extensions may also have
72 // their own source constants.
73 const SRC_EDIT
= 'mw.edit';
74 const SRC_NEW
= 'mw.new';
75 const SRC_LOG
= 'mw.log';
76 const SRC_EXTERNAL
= 'mw.external'; // obsolete
77 const SRC_CATEGORIZE
= 'mw.categorize';
79 const PRC_UNPATROLLED
= 0;
80 const PRC_PATROLLED
= 1;
81 const PRC_AUTOPATROLLED
= 2;
84 * @var bool For save() - save to the database only, without any events.
86 const SEND_NONE
= true;
89 * @var bool For save() - do emit the change to RCFeeds (usually public).
91 const SEND_FEED
= false;
94 public $mAttribs = [];
100 public $mTitle = false;
105 private $mPerformer = false;
107 public $numberofWatchingusers = 0; # Dummy to prevent error message in SpecialRecentChangesLinked
108 public $notificationtimestamp;
111 * @var int Line number of recent change. Default -1.
113 public $counter = -1;
116 * @var array List of tags to apply
121 * @var array Array of change types
123 private static $changeTypes = [
127 'external' => RC_EXTERNAL
,
128 'categorize' => RC_CATEGORIZE
,
135 * @return RecentChange
137 public static function newFromRow( $row ) {
138 $rc = new RecentChange
;
139 $rc->loadFromRow( $row );
145 * Parsing text to RC_* constants
147 * @param string|array $type
148 * @throws MWException
149 * @return int|array RC_TYPE
151 public static function parseToRCType( $type ) {
152 if ( is_array( $type ) ) {
154 foreach ( $type as $t ) {
155 $retval[] = self
::parseToRCType( $t );
161 if ( !array_key_exists( $type, self
::$changeTypes ) ) {
162 throw new MWException( "Unknown type '$type'" );
164 return self
::$changeTypes[$type];
168 * Parsing RC_* constants to human-readable test
171 * @return string $type
173 public static function parseFromRCType( $rcType ) {
174 return array_search( $rcType, self
::$changeTypes, true ) ?
: "$rcType";
178 * Get an array of all change types
184 public static function getChangeTypes() {
185 return array_keys( self
::$changeTypes );
189 * Obtain the recent change with a given rc_id value
191 * @param int $rcid The rc_id value to retrieve
192 * @return RecentChange|null
194 public static function newFromId( $rcid ) {
195 return self
::newFromConds( [ 'rc_id' => $rcid ], __METHOD__
);
199 * Find the first recent change matching some specific conditions
201 * @param array $conds Array of conditions
202 * @param mixed $fname Override the method name in profiling/logs
203 * @param int $dbType DB_* constant
205 * @return RecentChange|null
207 public static function newFromConds(
212 $db = wfGetDB( $dbType );
213 $rcQuery = self
::getQueryInfo();
214 $row = $db->selectRow(
215 $rcQuery['tables'], $rcQuery['fields'], $conds, $fname, [], $rcQuery['joins']
217 if ( $row !== false ) {
218 return self
::newFromRow( $row );
225 * Return the list of recentchanges fields that should be selected to create
226 * a new recentchanges object.
227 * @deprecated since 1.31, use self::getQueryInfo() instead.
230 public static function selectFields() {
231 global $wgActorTableSchemaMigrationStage;
233 wfDeprecated( __METHOD__
, '1.31' );
234 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW
) {
235 // If code is using this instead of self::getQueryInfo(), there's a
236 // decent chance it's going to try to directly access
237 // $row->rc_user or $row->rc_user_text and we can't give it
238 // useful values here once those aren't being used anymore.
239 throw new BadMethodCallException(
240 'Cannot use ' . __METHOD__
241 . ' when $wgActorTableSchemaMigrationStage has SCHEMA_COMPAT_READ_NEW'
250 'rc_actor' => 'NULL',
270 ] + CommentStore
::getStore()->getFields( 'rc_comment' );
274 * Return the tables, fields, and join conditions to be selected to create
275 * a new recentchanges object.
277 * @return array With three keys:
278 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
279 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
280 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
282 public static function getQueryInfo() {
283 $commentQuery = CommentStore
::getStore()->getJoin( 'rc_comment' );
284 $actorQuery = ActorMigration
::newMigration()->getJoin( 'rc_user' );
286 'tables' => [ 'recentchanges' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
309 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
310 'joins' => $commentQuery['joins'] +
$actorQuery['joins'],
317 * @param array $attribs
319 public function setAttribs( $attribs ) {
320 $this->mAttribs
= $attribs;
324 * @param array $extra
326 public function setExtra( $extra ) {
327 $this->mExtra
= $extra;
333 public function &getTitle() {
334 if ( $this->mTitle
=== false ) {
335 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
338 return $this->mTitle
;
342 * Get the User object of the person who performed this change.
346 public function getPerformer() {
347 if ( $this->mPerformer
=== false ) {
348 if ( !empty( $this->mAttribs
['rc_actor'] ) ) {
349 $this->mPerformer
= User
::newFromActorId( $this->mAttribs
['rc_actor'] );
350 } elseif ( !empty( $this->mAttribs
['rc_user'] ) ) {
351 $this->mPerformer
= User
::newFromId( $this->mAttribs
['rc_user'] );
352 } elseif ( !empty( $this->mAttribs
['rc_user_text'] ) ) {
353 $this->mPerformer
= User
::newFromName( $this->mAttribs
['rc_user_text'], false );
355 throw new MWException( 'RecentChange object lacks rc_actor, rc_user, and rc_user_text' );
359 return $this->mPerformer
;
363 * Writes the data in this object to the database
365 * For compatibility reasons, the SEND_ constants internally reference a value
366 * that may seem negated from their purpose (none=true, feed=false). This is
367 * because the parameter used to be called "$noudp", defaulting to false.
369 * @param bool $send self::SEND_FEED or self::SEND_NONE
371 public function save( $send = self
::SEND_FEED
) {
372 global $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker;
374 $dbw = wfGetDB( DB_MASTER
);
375 if ( !is_array( $this->mExtra
) ) {
379 if ( !$wgPutIPinRC ) {
380 $this->mAttribs
['rc_ip'] = '';
383 # Strict mode fixups (not-NULL fields)
384 foreach ( [ 'minor', 'bot', 'new', 'patrolled', 'deleted' ] as $field ) {
385 $this->mAttribs
["rc_$field"] = (int)$this->mAttribs
["rc_$field"];
387 # ...more fixups (NULL fields)
388 foreach ( [ 'old_len', 'new_len' ] as $field ) {
389 $this->mAttribs
["rc_$field"] = isset( $this->mAttribs
["rc_$field"] )
390 ?
(int)$this->mAttribs
["rc_$field"]
394 # If our database is strict about IP addresses, use NULL instead of an empty string
395 $strictIPs = $dbw->getType() === 'postgres'; // legacy
396 if ( $strictIPs && $this->mAttribs
['rc_ip'] == '' ) {
397 unset( $this->mAttribs
['rc_ip'] );
400 # Trim spaces on user supplied text
401 $this->mAttribs
['rc_comment'] = trim( $this->mAttribs
['rc_comment'] );
403 # Fixup database timestamps
404 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp( $this->mAttribs
['rc_timestamp'] );
406 # # If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
407 if ( $this->mAttribs
['rc_cur_id'] == 0 ) {
408 unset( $this->mAttribs
['rc_cur_id'] );
411 $row = $this->mAttribs
;
413 # Convert mAttribs['rc_comment'] for CommentStore
414 $comment = $row['rc_comment'];
415 unset( $row['rc_comment'], $row['rc_comment_text'], $row['rc_comment_data'] );
416 $row +
= CommentStore
::getStore()->insert( $dbw, 'rc_comment', $comment );
418 # Convert mAttribs['rc_user'] etc for ActorMigration
419 $user = User
::newFromAnyId(
420 $row['rc_user'] ??
null,
421 $row['rc_user_text'] ??
null,
422 $row['rc_actor'] ??
null
424 unset( $row['rc_user'], $row['rc_user_text'], $row['rc_actor'] );
425 $row +
= ActorMigration
::newMigration()->getInsertValues( $dbw, 'rc_user', $user );
427 # Don't reuse an existing rc_id for the new row, if one happens to be
428 # set for some reason.
429 unset( $row['rc_id'] );
432 $dbw->insert( 'recentchanges', $row, __METHOD__
);
435 $this->mAttribs
['rc_id'] = $dbw->insertId();
438 // Avoid PHP 7.1 warning from passing $this by reference
440 Hooks
::run( 'RecentChange_save', [ &$rc ] );
442 if ( count( $this->tags
) ) {
443 ChangeTags
::addTags( $this->tags
, $this->mAttribs
['rc_id'],
444 $this->mAttribs
['rc_this_oldid'], $this->mAttribs
['rc_logid'], null, $this );
447 if ( $send === self
::SEND_FEED
) {
448 // Emit the change to external applications via RCFeeds.
449 $this->notifyRCFeeds();
452 # E-mail notifications
453 if ( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
454 $editor = $this->getPerformer();
455 $title = $this->getTitle();
457 // Never send an RC notification email about categorization changes
459 Hooks
::run( 'AbortEmailNotification', [ $editor, $title, $this ] ) &&
460 $this->mAttribs
['rc_type'] != RC_CATEGORIZE
462 // @FIXME: This would be better as an extension hook
463 // Send emails or email jobs once this row is safely committed
464 $dbw->onTransactionCommitOrIdle(
465 function () use ( $editor, $title ) {
466 $enotif = new EmailNotification();
467 $enotif->notifyOnPageChange(
470 $this->mAttribs
['rc_timestamp'],
471 $this->mAttribs
['rc_comment'],
472 $this->mAttribs
['rc_minor'],
473 $this->mAttribs
['rc_last_oldid'],
474 $this->mExtra
['pageStatus']
482 // Update the cached list of active users
483 if ( $this->mAttribs
['rc_user'] > 0 ) {
484 JobQueueGroup
::singleton()->lazyPush( RecentChangesUpdateJob
::newCacheUpdateJob() );
489 * Notify all the feeds about the change.
490 * @param array|null $feeds Optional feeds to send to, defaults to $wgRCFeeds
492 public function notifyRCFeeds( array $feeds = null ) {
494 if ( $feeds === null ) {
498 $performer = $this->getPerformer();
500 foreach ( $feeds as $params ) {
502 'omit_bots' => false,
503 'omit_anon' => false,
504 'omit_user' => false,
505 'omit_minor' => false,
506 'omit_patrolled' => false,
510 ( $params['omit_bots'] && $this->mAttribs
['rc_bot'] ) ||
511 ( $params['omit_anon'] && $performer->isAnon() ) ||
512 ( $params['omit_user'] && !$performer->isAnon() ) ||
513 ( $params['omit_minor'] && $this->mAttribs
['rc_minor'] ) ||
514 ( $params['omit_patrolled'] && $this->mAttribs
['rc_patrolled'] ) ||
515 $this->mAttribs
['rc_type'] == RC_EXTERNAL
520 $actionComment = $this->mExtra
['actionCommentIRC'] ??
null;
522 $feed = RCFeed
::factory( $params );
523 $feed->notify( $this, $actionComment );
529 * @deprecated since 1.29 Use RCFeed::factory() instead
530 * @param string $uri URI to get the engine object for
531 * @param array $params
532 * @return RCFeedEngine The engine object
533 * @throws MWException
535 public static function getEngine( $uri, $params = [] ) {
536 // TODO: Merge into RCFeed::factory().
538 $scheme = parse_url( $uri, PHP_URL_SCHEME
);
540 throw new MWException( "Invalid RCFeed uri: '$uri'" );
542 if ( !isset( $wgRCEngines[$scheme] ) ) {
543 throw new MWException( "Unknown RCFeedEngine scheme: '$scheme'" );
545 if ( defined( 'MW_PHPUNIT_TEST' ) && is_object( $wgRCEngines[$scheme] ) ) {
546 return $wgRCEngines[$scheme];
548 return new $wgRCEngines[$scheme]( $params );
552 * Mark a given change as patrolled
554 * @param RecentChange|int $change RecentChange or corresponding rc_id
555 * @param bool $auto For automatic patrol
556 * @param string|string[]|null $tags Change tags to add to the patrol log entry
557 * ($user should be able to add the specified tags before this is called)
558 * @return array See doMarkPatrolled(), or null if $change is not an existing rc_id
560 public static function markPatrolled( $change, $auto = false, $tags = null ) {
563 $change = $change instanceof RecentChange
565 : self
::newFromId( $change );
567 if ( !$change instanceof RecentChange
) {
571 return $change->doMarkPatrolled( $wgUser, $auto, $tags );
575 * Mark this RecentChange as patrolled
577 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
578 * 'markedaspatrollederror-noautopatrol' as errors
579 * @param User $user User object doing the action
580 * @param bool $auto For automatic patrol
581 * @param string|string[]|null $tags Change tags to add to the patrol log entry
582 * ($user should be able to add the specified tags before this is called)
583 * @return array Array of permissions errors, see Title::getUserPermissionsErrors()
585 public function doMarkPatrolled( User
$user, $auto = false, $tags = null ) {
586 global $wgUseRCPatrol, $wgUseNPPatrol, $wgUseFilePatrol;
588 // Fix up $tags so that the MarkPatrolled hook below always gets an array
589 if ( $tags === null ) {
591 } elseif ( is_string( $tags ) ) {
596 // If recentchanges patrol is disabled, only new pages or new file versions
597 // can be patrolled, provided the appropriate config variable is set
598 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) &&
599 ( !$wgUseFilePatrol ||
!( $this->getAttribute( 'rc_type' ) == RC_LOG
&&
600 $this->getAttribute( 'rc_log_type' ) == 'upload' ) ) ) {
601 $errors[] = [ 'rcpatroldisabled' ];
603 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
604 $right = $auto ?
'autopatrol' : 'patrol';
605 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
606 if ( !Hooks
::run( 'MarkPatrolled',
607 [ $this->getAttribute( 'rc_id' ), &$user, false, $auto, &$tags ] )
609 $errors[] = [ 'hookaborted' ];
611 // Users without the 'autopatrol' right can't patrol their
613 if ( $user->getName() === $this->getAttribute( 'rc_user_text' ) &&
614 !MediaWikiServices
::getInstance()->getPermissionManager()
615 ->userHasRight( $user, 'autopatrol' )
617 $errors[] = [ 'markedaspatrollederror-noautopatrol' ];
622 // If the change was patrolled already, do nothing
623 if ( $this->getAttribute( 'rc_patrolled' ) ) {
626 // Actually set the 'patrolled' flag in RC
627 $this->reallyMarkPatrolled();
628 // Log this patrol event
629 PatrolLog
::record( $this, $auto, $user, $tags );
632 'MarkPatrolledComplete',
633 [ $this->getAttribute( 'rc_id' ), &$user, false, $auto ]
640 * Mark this RecentChange patrolled, without error checking
641 * @return int Number of affected rows
643 public function reallyMarkPatrolled() {
644 $dbw = wfGetDB( DB_MASTER
);
648 'rc_patrolled' => self
::PRC_PATROLLED
651 'rc_id' => $this->getAttribute( 'rc_id' )
655 // Invalidate the page cache after the page has been patrolled
656 // to make sure that the Patrol link isn't visible any longer!
657 $this->getTitle()->invalidateCache();
659 return $dbw->affectedRows();
663 * Makes an entry in the database corresponding to an edit
665 * @param string $timestamp
666 * @param Title $title
669 * @param string $comment
671 * @param string $lastTimestamp
674 * @param int $oldSize
675 * @param int $newSize
679 * @return RecentChange
681 public static function notifyEdit(
682 $timestamp, $title, $minor, $user, $comment, $oldId, $lastTimestamp,
683 $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0,
686 $rc = new RecentChange
;
687 $rc->mTitle
= $title;
688 $rc->mPerformer
= $user;
690 'rc_timestamp' => $timestamp,
691 'rc_namespace' => $title->getNamespace(),
692 'rc_title' => $title->getDBkey(),
693 'rc_type' => RC_EDIT
,
694 'rc_source' => self
::SRC_EDIT
,
695 'rc_minor' => $minor ?
1 : 0,
696 'rc_cur_id' => $title->getArticleID(),
697 'rc_user' => $user->getId(),
698 'rc_user_text' => $user->getName(),
699 'rc_actor' => $user->getActorId(),
700 'rc_comment' => &$comment,
701 'rc_comment_text' => &$comment,
702 'rc_comment_data' => null,
703 'rc_this_oldid' => $newId,
704 'rc_last_oldid' => $oldId,
705 'rc_bot' => $bot ?
1 : 0,
706 'rc_ip' => self
::checkIPAddress( $ip ),
707 'rc_patrolled' => intval( $patrol ),
708 'rc_new' => 0, # obsolete
709 'rc_old_len' => $oldSize,
710 'rc_new_len' => $newSize,
713 'rc_log_type' => null,
714 'rc_log_action' => '',
719 'prefixedDBkey' => $title->getPrefixedDBkey(),
720 'lastTimestamp' => $lastTimestamp,
721 'oldSize' => $oldSize,
722 'newSize' => $newSize,
723 'pageStatus' => 'changed'
726 DeferredUpdates
::addCallableUpdate(
727 function () use ( $rc, $tags ) {
728 $rc->addTags( $tags );
731 DeferredUpdates
::POSTSEND
,
739 * Makes an entry in the database corresponding to page creation
740 * Note: the title object must be loaded with the new id using resetArticleID()
742 * @param string $timestamp
743 * @param Title $title
746 * @param string $comment
753 * @return RecentChange
755 public static function notifyNew(
756 $timestamp, $title, $minor, $user, $comment, $bot,
757 $ip = '', $size = 0, $newId = 0, $patrol = 0, $tags = []
759 $rc = new RecentChange
;
760 $rc->mTitle
= $title;
761 $rc->mPerformer
= $user;
763 'rc_timestamp' => $timestamp,
764 'rc_namespace' => $title->getNamespace(),
765 'rc_title' => $title->getDBkey(),
767 'rc_source' => self
::SRC_NEW
,
768 'rc_minor' => $minor ?
1 : 0,
769 'rc_cur_id' => $title->getArticleID(),
770 'rc_user' => $user->getId(),
771 'rc_user_text' => $user->getName(),
772 'rc_actor' => $user->getActorId(),
773 'rc_comment' => &$comment,
774 'rc_comment_text' => &$comment,
775 'rc_comment_data' => null,
776 'rc_this_oldid' => $newId,
777 'rc_last_oldid' => 0,
778 'rc_bot' => $bot ?
1 : 0,
779 'rc_ip' => self
::checkIPAddress( $ip ),
780 'rc_patrolled' => intval( $patrol ),
781 'rc_new' => 1, # obsolete
783 'rc_new_len' => $size,
786 'rc_log_type' => null,
787 'rc_log_action' => '',
792 'prefixedDBkey' => $title->getPrefixedDBkey(),
793 'lastTimestamp' => 0,
796 'pageStatus' => 'created'
799 DeferredUpdates
::addCallableUpdate(
800 function () use ( $rc, $tags ) {
801 $rc->addTags( $tags );
804 DeferredUpdates
::POSTSEND
,
812 * @param string $timestamp
813 * @param Title $title
815 * @param string $actionComment
817 * @param string $type
818 * @param string $action
819 * @param Title $target
820 * @param string $logComment
821 * @param string $params
823 * @param string $actionCommentIRC
826 public static function notifyLog( $timestamp, $title, $user, $actionComment, $ip, $type,
827 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
829 global $wgLogRestrictions;
831 # Don't add private logs to RC!
832 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
835 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
836 $target, $logComment, $params, $newId, $actionCommentIRC );
843 * @param string $timestamp
844 * @param Title $title
846 * @param string $actionComment
848 * @param string $type
849 * @param string $action
850 * @param Title $target
851 * @param string $logComment
852 * @param string $params
854 * @param string $actionCommentIRC
855 * @param int $revId Id of associated revision, if any
856 * @param bool $isPatrollable Whether this log entry is patrollable
857 * @return RecentChange
859 public static function newLogEntry( $timestamp, $title, $user, $actionComment, $ip,
860 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '',
861 $revId = 0, $isPatrollable = false ) {
863 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
865 # # Get pageStatus for email notification
866 switch ( $type . '-' . $action ) {
867 case 'delete-delete':
868 case 'delete-delete_redir':
869 $pageStatus = 'deleted';
872 case 'move-move_redir':
873 $pageStatus = 'moved';
875 case 'delete-restore':
876 $pageStatus = 'restored';
878 case 'upload-upload':
879 $pageStatus = 'created';
881 case 'upload-overwrite':
883 $pageStatus = 'changed';
887 // Allow unpatrolled status for patrollable log entries
888 $canAutopatrol = $permissionManager->userHasRight( $user, 'autopatrol' );
889 $markPatrolled = $isPatrollable ?
$canAutopatrol : true;
891 $rc = new RecentChange
;
892 $rc->mTitle
= $target;
893 $rc->mPerformer
= $user;
895 'rc_timestamp' => $timestamp,
896 'rc_namespace' => $target->getNamespace(),
897 'rc_title' => $target->getDBkey(),
899 'rc_source' => self
::SRC_LOG
,
901 'rc_cur_id' => $target->getArticleID(),
902 'rc_user' => $user->getId(),
903 'rc_user_text' => $user->getName(),
904 'rc_actor' => $user->getActorId(),
905 'rc_comment' => &$logComment,
906 'rc_comment_text' => &$logComment,
907 'rc_comment_data' => null,
908 'rc_this_oldid' => $revId,
909 'rc_last_oldid' => 0,
910 'rc_bot' => $permissionManager->userHasRight( $user, 'bot' ) ?
911 (int)$wgRequest->getBool( 'bot', true ) : 0,
912 'rc_ip' => self
::checkIPAddress( $ip ),
913 'rc_patrolled' => $markPatrolled ? self
::PRC_AUTOPATROLLED
: self
::PRC_UNPATROLLED
,
914 'rc_new' => 0, # obsolete
915 'rc_old_len' => null,
916 'rc_new_len' => null,
918 'rc_logid' => $newId,
919 'rc_log_type' => $type,
920 'rc_log_action' => $action,
921 'rc_params' => $params
925 'prefixedDBkey' => $title->getPrefixedDBkey(),
926 'lastTimestamp' => 0,
927 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
928 'pageStatus' => $pageStatus,
929 'actionCommentIRC' => $actionCommentIRC
936 * Constructs a RecentChange object for the given categorization
937 * This does not call save() on the object and thus does not write to the db
941 * @param string $timestamp Timestamp of the recent change to occur
942 * @param Title $categoryTitle Title of the category a page is being added to or removed from
943 * @param User|null $user User object of the user that made the change
944 * @param string $comment Change summary
945 * @param Title $pageTitle Title of the page that is being added or removed
946 * @param int $oldRevId Parent revision ID of this change
947 * @param int $newRevId Revision ID of this change
948 * @param string $lastTimestamp Parent revision timestamp of this change
949 * @param bool $bot true, if the change was made by a bot
950 * @param string $ip IP address of the user, if the change was made anonymously
951 * @param int $deleted Indicates whether the change has been deleted
952 * @param bool|null $added true, if the category was added, false for removed
954 * @return RecentChange
956 public static function newForCategorization(
958 Title
$categoryTitle,
970 // Done in a backwards compatible way.
972 'hidden-cat' => WikiCategoryPage
::factory( $categoryTitle )->isHidden()
974 if ( $added !== null ) {
975 $params['added'] = $added;
978 $rc = new RecentChange
;
979 $rc->mTitle
= $categoryTitle;
980 $rc->mPerformer
= $user;
982 'rc_timestamp' => $timestamp,
983 'rc_namespace' => $categoryTitle->getNamespace(),
984 'rc_title' => $categoryTitle->getDBkey(),
985 'rc_type' => RC_CATEGORIZE
,
986 'rc_source' => self
::SRC_CATEGORIZE
,
988 'rc_cur_id' => $pageTitle->getArticleID(),
989 'rc_user' => $user ?
$user->getId() : 0,
990 'rc_user_text' => $user ?
$user->getName() : '',
991 'rc_actor' => $user ?
$user->getActorId() : null,
992 'rc_comment' => &$comment,
993 'rc_comment_text' => &$comment,
994 'rc_comment_data' => null,
995 'rc_this_oldid' => $newRevId,
996 'rc_last_oldid' => $oldRevId,
997 'rc_bot' => $bot ?
1 : 0,
998 'rc_ip' => self
::checkIPAddress( $ip ),
999 'rc_patrolled' => self
::PRC_AUTOPATROLLED
, // Always patrolled, just like log entries
1000 'rc_new' => 0, # obsolete
1001 'rc_old_len' => null,
1002 'rc_new_len' => null,
1003 'rc_deleted' => $deleted,
1005 'rc_log_type' => null,
1006 'rc_log_action' => '',
1007 'rc_params' => serialize( $params )
1011 'prefixedDBkey' => $categoryTitle->getPrefixedDBkey(),
1012 'lastTimestamp' => $lastTimestamp,
1015 'pageStatus' => 'changed'
1022 * Get a parameter value
1026 * @param string $name parameter name
1029 public function getParam( $name ) {
1030 $params = $this->parseParams();
1031 return $params[$name] ??
null;
1035 * Initialises the members of this object from a mysql row object
1039 public function loadFromRow( $row ) {
1040 $this->mAttribs
= get_object_vars( $row );
1041 $this->mAttribs
['rc_timestamp'] = wfTimestamp( TS_MW
, $this->mAttribs
['rc_timestamp'] );
1042 // rc_deleted MUST be set
1043 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
;
1045 if ( isset( $this->mAttribs
['rc_ip'] ) ) {
1046 // Clean up CIDRs for Postgres per T164898. ("127.0.0.1" casts to "127.0.0.1/32")
1047 $n = strpos( $this->mAttribs
['rc_ip'], '/' );
1048 if ( $n !== false ) {
1049 $this->mAttribs
['rc_ip'] = substr( $this->mAttribs
['rc_ip'], 0, $n );
1053 $comment = CommentStore
::getStore()
1054 // Legacy because $row may have come from self::selectFields()
1055 ->getCommentLegacy( wfGetDB( DB_REPLICA
), 'rc_comment', $row, true )
1057 $this->mAttribs
['rc_comment'] = &$comment;
1058 $this->mAttribs
['rc_comment_text'] = &$comment;
1059 $this->mAttribs
['rc_comment_data'] = null;
1061 $user = User
::newFromAnyId(
1062 $this->mAttribs
['rc_user'] ??
null,
1063 $this->mAttribs
['rc_user_text'] ??
null,
1064 $this->mAttribs
['rc_actor'] ??
null
1066 $this->mAttribs
['rc_user'] = $user->getId();
1067 $this->mAttribs
['rc_user_text'] = $user->getName();
1068 $this->mAttribs
['rc_actor'] = $user->getActorId();
1072 * Get an attribute value
1074 * @param string $name Attribute name
1077 public function getAttribute( $name ) {
1078 if ( $name === 'rc_comment' ) {
1079 return CommentStore
::getStore()
1080 ->getComment( 'rc_comment', $this->mAttribs
, true )->text
;
1083 if ( $name === 'rc_user' ||
$name === 'rc_user_text' ||
$name === 'rc_actor' ) {
1084 $user = User
::newFromAnyId(
1085 $this->mAttribs
['rc_user'] ??
null,
1086 $this->mAttribs
['rc_user_text'] ??
null,
1087 $this->mAttribs
['rc_actor'] ??
null
1089 if ( $name === 'rc_user' ) {
1090 return $user->getId();
1092 if ( $name === 'rc_user_text' ) {
1093 return $user->getName();
1095 if ( $name === 'rc_actor' ) {
1096 return $user->getActorId();
1100 return $this->mAttribs
[$name] ??
null;
1106 public function getAttributes() {
1107 return $this->mAttribs
;
1111 * Gets the end part of the diff URL associated with this object
1112 * Blank if no diff link should be displayed
1113 * @param bool $forceCur
1116 public function diffLinkTrail( $forceCur ) {
1117 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
1118 $trail = "curid=" . (int)( $this->mAttribs
['rc_cur_id'] ) .
1119 "&oldid=" . (int)( $this->mAttribs
['rc_last_oldid'] );
1121 $trail .= '&diff=0';
1123 $trail .= '&diff=' . (int)( $this->mAttribs
['rc_this_oldid'] );
1133 * Returns the change size (HTML).
1134 * The lengths can be given optionally.
1139 public function getCharacterDifference( $old = 0, $new = 0 ) {
1141 $old = $this->mAttribs
['rc_old_len'];
1144 $new = $this->mAttribs
['rc_new_len'];
1146 if ( $old === null ||
$new === null ) {
1150 return ChangesList
::showCharacterDifference( $old, $new );
1153 private static function checkIPAddress( $ip ) {
1156 if ( !IP
::isIPAddress( $ip ) ) {
1157 throw new MWException( "Attempt to write \"" . $ip .
1158 "\" as an IP address into recent changes" );
1161 $ip = $wgRequest->getIP();
1171 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
1172 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
1173 * or rows which will be deleted soon shouldn't be included.
1175 * @param mixed $timestamp MWTimestamp compatible timestamp
1176 * @param int $tolerance Tolerance in seconds
1179 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
1182 return wfTimestamp( TS_UNIX
, $timestamp ) > time() - $tolerance - $wgRCMaxAge;
1186 * Parses and returns the rc_params attribute
1190 * @return mixed|bool false on failed unserialization
1192 public function parseParams() {
1193 $rcParams = $this->getAttribute( 'rc_params' );
1195 Wikimedia\
suppressWarnings();
1196 $unserializedParams = unserialize( $rcParams );
1197 Wikimedia\restoreWarnings
();
1199 return $unserializedParams;
1203 * Tags to append to the recent change,
1204 * and associated revision/log
1208 * @param string|string[] $tags
1210 public function addTags( $tags ) {
1211 if ( is_string( $tags ) ) {
1212 $this->tags
[] = $tags;
1214 $this->tags
= array_merge( $tags, $this->tags
);