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;
93 public $mAttribs = [];
99 public $mTitle = false;
104 private $mPerformer = false;
106 public $numberofWatchingusers = 0; # Dummy to prevent error message in SpecialRecentChangesLinked
107 public $notificationtimestamp;
110 * @var int Line number of recent change. Default -1.
112 public $counter = -1;
115 * @var array List of tags to apply
120 * @var array Array of change types
122 private static $changeTypes = [
126 'external' => RC_EXTERNAL
,
127 'categorize' => RC_CATEGORIZE
,
134 * @return RecentChange
136 public static function newFromRow( $row ) {
137 $rc = new RecentChange
;
138 $rc->loadFromRow( $row );
144 * Parsing text to RC_* constants
146 * @param string|array $type
147 * @throws MWException
148 * @return int|array RC_TYPE
150 public static function parseToRCType( $type ) {
151 if ( is_array( $type ) ) {
153 foreach ( $type as $t ) {
154 $retval[] = self
::parseToRCType( $t );
160 if ( !array_key_exists( $type, self
::$changeTypes ) ) {
161 throw new MWException( "Unknown type '$type'" );
163 return self
::$changeTypes[$type];
167 * Parsing RC_* constants to human-readable test
170 * @return string $type
172 public static function parseFromRCType( $rcType ) {
173 return array_search( $rcType, self
::$changeTypes, true ) ?
: "$rcType";
177 * Get an array of all change types
183 public static function getChangeTypes() {
184 return array_keys( self
::$changeTypes );
188 * Obtain the recent change with a given rc_id value
190 * @param int $rcid The rc_id value to retrieve
191 * @return RecentChange|null
193 public static function newFromId( $rcid ) {
194 return self
::newFromConds( [ 'rc_id' => $rcid ], __METHOD__
);
198 * Find the first recent change matching some specific conditions
200 * @param array $conds Array of conditions
201 * @param mixed $fname Override the method name in profiling/logs
202 * @param int $dbType DB_* constant
204 * @return RecentChange|null
206 public static function newFromConds(
211 $db = wfGetDB( $dbType );
212 $rcQuery = self
::getQueryInfo();
213 $row = $db->selectRow(
214 $rcQuery['tables'], $rcQuery['fields'], $conds, $fname, [], $rcQuery['joins']
216 if ( $row !== false ) {
217 return self
::newFromRow( $row );
224 * Return the list of recentchanges fields that should be selected to create
225 * a new recentchanges object.
226 * @deprecated since 1.31, use self::getQueryInfo() instead.
229 public static function selectFields() {
230 global $wgActorTableSchemaMigrationStage;
232 wfDeprecated( __METHOD__
, '1.31' );
233 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW
) {
234 // If code is using this instead of self::getQueryInfo(), there's a
235 // decent chance it's going to try to directly access
236 // $row->rc_user or $row->rc_user_text and we can't give it
237 // useful values here once those aren't being used anymore.
238 throw new BadMethodCallException(
239 'Cannot use ' . __METHOD__
240 . ' when $wgActorTableSchemaMigrationStage has SCHEMA_COMPAT_READ_NEW'
249 'rc_actor' => 'NULL',
269 ] + CommentStore
::getStore()->getFields( 'rc_comment' );
273 * Return the tables, fields, and join conditions to be selected to create
274 * a new recentchanges object.
276 * @return array With three keys:
277 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
278 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
279 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
281 public static function getQueryInfo() {
282 $commentQuery = CommentStore
::getStore()->getJoin( 'rc_comment' );
283 $actorQuery = ActorMigration
::newMigration()->getJoin( 'rc_user' );
285 'tables' => [ 'recentchanges' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
308 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
309 'joins' => $commentQuery['joins'] +
$actorQuery['joins'],
316 * @param array $attribs
318 public function setAttribs( $attribs ) {
319 $this->mAttribs
= $attribs;
323 * @param array $extra
325 public function setExtra( $extra ) {
326 $this->mExtra
= $extra;
332 public function &getTitle() {
333 if ( $this->mTitle
=== false ) {
334 $this->mTitle
= Title
::makeTitle( $this->mAttribs
['rc_namespace'], $this->mAttribs
['rc_title'] );
337 return $this->mTitle
;
341 * Get the User object of the person who performed this change.
345 public function getPerformer() {
346 if ( $this->mPerformer
=== false ) {
347 if ( !empty( $this->mAttribs
['rc_actor'] ) ) {
348 $this->mPerformer
= User
::newFromActorId( $this->mAttribs
['rc_actor'] );
349 } elseif ( !empty( $this->mAttribs
['rc_user'] ) ) {
350 $this->mPerformer
= User
::newFromId( $this->mAttribs
['rc_user'] );
351 } elseif ( !empty( $this->mAttribs
['rc_user_text'] ) ) {
352 $this->mPerformer
= User
::newFromName( $this->mAttribs
['rc_user_text'], false );
354 throw new MWException( 'RecentChange object lacks rc_actor, rc_user, and rc_user_text' );
358 return $this->mPerformer
;
362 * Writes the data in this object to the database
364 * For compatibility reasons, the SEND_ constants internally reference a value
365 * that may seem negated from their purpose (none=true, feed=false). This is
366 * because the parameter used to be called "$noudp", defaulting to false.
368 * @param bool $send self::SEND_FEED or self::SEND_NONE
370 public function save( $send = self
::SEND_FEED
) {
371 global $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker;
373 $dbw = wfGetDB( DB_MASTER
);
374 if ( !is_array( $this->mExtra
) ) {
378 if ( !$wgPutIPinRC ) {
379 $this->mAttribs
['rc_ip'] = '';
382 # Strict mode fixups (not-NULL fields)
383 foreach ( [ 'minor', 'bot', 'new', 'patrolled', 'deleted' ] as $field ) {
384 $this->mAttribs
["rc_$field"] = (int)$this->mAttribs
["rc_$field"];
386 # ...more fixups (NULL fields)
387 foreach ( [ 'old_len', 'new_len' ] as $field ) {
388 $this->mAttribs
["rc_$field"] = isset( $this->mAttribs
["rc_$field"] )
389 ?
(int)$this->mAttribs
["rc_$field"]
393 # If our database is strict about IP addresses, use NULL instead of an empty string
394 $strictIPs = $dbw->getType() === 'postgres'; // legacy
395 if ( $strictIPs && $this->mAttribs
['rc_ip'] == '' ) {
396 unset( $this->mAttribs
['rc_ip'] );
399 # Trim spaces on user supplied text
400 $this->mAttribs
['rc_comment'] = trim( $this->mAttribs
['rc_comment'] );
402 # Fixup database timestamps
403 $this->mAttribs
['rc_timestamp'] = $dbw->timestamp( $this->mAttribs
['rc_timestamp'] );
405 # # If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
406 if ( $this->mAttribs
['rc_cur_id'] == 0 ) {
407 unset( $this->mAttribs
['rc_cur_id'] );
410 $row = $this->mAttribs
;
412 # Convert mAttribs['rc_comment'] for CommentStore
413 $comment = $row['rc_comment'];
414 unset( $row['rc_comment'], $row['rc_comment_text'], $row['rc_comment_data'] );
415 $row +
= CommentStore
::getStore()->insert( $dbw, 'rc_comment', $comment );
417 # Convert mAttribs['rc_user'] etc for ActorMigration
418 $user = User
::newFromAnyId(
419 $row['rc_user'] ??
null,
420 $row['rc_user_text'] ??
null,
421 $row['rc_actor'] ??
null
423 unset( $row['rc_user'], $row['rc_user_text'], $row['rc_actor'] );
424 $row +
= ActorMigration
::newMigration()->getInsertValues( $dbw, 'rc_user', $user );
426 # Don't reuse an existing rc_id for the new row, if one happens to be
427 # set for some reason.
428 unset( $row['rc_id'] );
431 $dbw->insert( 'recentchanges', $row, __METHOD__
);
434 $this->mAttribs
['rc_id'] = $dbw->insertId();
437 // Avoid PHP 7.1 warning from passing $this by reference
439 Hooks
::run( 'RecentChange_save', [ &$rc ] );
441 if ( count( $this->tags
) ) {
442 ChangeTags
::addTags( $this->tags
, $this->mAttribs
['rc_id'],
443 $this->mAttribs
['rc_this_oldid'], $this->mAttribs
['rc_logid'], null, $this );
446 if ( $send === self
::SEND_FEED
) {
447 // Emit the change to external applications via RCFeeds.
448 $this->notifyRCFeeds();
451 # E-mail notifications
452 if ( $wgUseEnotif ||
$wgShowUpdatedMarker ) {
453 $editor = $this->getPerformer();
454 $title = $this->getTitle();
456 // Never send an RC notification email about categorization changes
458 Hooks
::run( 'AbortEmailNotification', [ $editor, $title, $this ] ) &&
459 $this->mAttribs
['rc_type'] != RC_CATEGORIZE
461 // @FIXME: This would be better as an extension hook
462 // Send emails or email jobs once this row is safely committed
463 $dbw->onTransactionCommitOrIdle(
464 function () use ( $editor, $title ) {
465 $enotif = new EmailNotification();
466 $enotif->notifyOnPageChange(
469 $this->mAttribs
['rc_timestamp'],
470 $this->mAttribs
['rc_comment'],
471 $this->mAttribs
['rc_minor'],
472 $this->mAttribs
['rc_last_oldid'],
473 $this->mExtra
['pageStatus']
481 // Update the cached list of active users
482 if ( $this->mAttribs
['rc_user'] > 0 ) {
483 JobQueueGroup
::singleton()->lazyPush( RecentChangesUpdateJob
::newCacheUpdateJob() );
488 * Notify all the feeds about the change.
489 * @param array|null $feeds Optional feeds to send to, defaults to $wgRCFeeds
491 public function notifyRCFeeds( array $feeds = null ) {
493 if ( $feeds === null ) {
497 $performer = $this->getPerformer();
499 foreach ( $feeds as $params ) {
501 'omit_bots' => false,
502 'omit_anon' => false,
503 'omit_user' => false,
504 'omit_minor' => false,
505 'omit_patrolled' => false,
509 ( $params['omit_bots'] && $this->mAttribs
['rc_bot'] ) ||
510 ( $params['omit_anon'] && $performer->isAnon() ) ||
511 ( $params['omit_user'] && !$performer->isAnon() ) ||
512 ( $params['omit_minor'] && $this->mAttribs
['rc_minor'] ) ||
513 ( $params['omit_patrolled'] && $this->mAttribs
['rc_patrolled'] ) ||
514 $this->mAttribs
['rc_type'] == RC_EXTERNAL
519 $actionComment = $this->mExtra
['actionCommentIRC'] ??
null;
521 $feed = RCFeed
::factory( $params );
522 $feed->notify( $this, $actionComment );
528 * @deprecated since 1.29 Use RCFeed::factory() instead
529 * @param string $uri URI to get the engine object for
530 * @param array $params
531 * @return RCFeedEngine The engine object
532 * @throws MWException
534 public static function getEngine( $uri, $params = [] ) {
535 // TODO: Merge into RCFeed::factory().
537 $scheme = parse_url( $uri, PHP_URL_SCHEME
);
539 throw new MWException( "Invalid RCFeed uri: '$uri'" );
541 if ( !isset( $wgRCEngines[$scheme] ) ) {
542 throw new MWException( "Unknown RCFeedEngine scheme: '$scheme'" );
544 if ( defined( 'MW_PHPUNIT_TEST' ) && is_object( $wgRCEngines[$scheme] ) ) {
545 return $wgRCEngines[$scheme];
547 return new $wgRCEngines[$scheme]( $params );
551 * Mark a given change as patrolled
553 * @param RecentChange|int $change RecentChange or corresponding rc_id
554 * @param bool $auto For automatic patrol
555 * @param string|string[]|null $tags Change tags to add to the patrol log entry
556 * ($user should be able to add the specified tags before this is called)
557 * @return array See doMarkPatrolled(), or null if $change is not an existing rc_id
559 public static function markPatrolled( $change, $auto = false, $tags = null ) {
562 $change = $change instanceof RecentChange
564 : self
::newFromId( $change );
566 if ( !$change instanceof RecentChange
) {
570 return $change->doMarkPatrolled( $wgUser, $auto, $tags );
574 * Mark this RecentChange as patrolled
576 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
577 * 'markedaspatrollederror-noautopatrol' as errors
578 * @param User $user User object doing the action
579 * @param bool $auto For automatic patrol
580 * @param string|string[]|null $tags Change tags to add to the patrol log entry
581 * ($user should be able to add the specified tags before this is called)
582 * @return array Array of permissions errors, see Title::getUserPermissionsErrors()
584 public function doMarkPatrolled( User
$user, $auto = false, $tags = null ) {
585 global $wgUseRCPatrol, $wgUseNPPatrol, $wgUseFilePatrol;
587 // Fix up $tags so that the MarkPatrolled hook below always gets an array
588 if ( $tags === null ) {
590 } elseif ( is_string( $tags ) ) {
595 // If recentchanges patrol is disabled, only new pages or new file versions
596 // can be patrolled, provided the appropriate config variable is set
597 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol ||
$this->getAttribute( 'rc_type' ) != RC_NEW
) &&
598 ( !$wgUseFilePatrol ||
!( $this->getAttribute( 'rc_type' ) == RC_LOG
&&
599 $this->getAttribute( 'rc_log_type' ) == 'upload' ) ) ) {
600 $errors[] = [ 'rcpatroldisabled' ];
602 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
603 $right = $auto ?
'autopatrol' : 'patrol';
604 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
605 if ( !Hooks
::run( 'MarkPatrolled',
606 [ $this->getAttribute( 'rc_id' ), &$user, false, $auto, &$tags ] )
608 $errors[] = [ 'hookaborted' ];
610 // Users without the 'autopatrol' right can't patrol their
612 if ( $user->getName() === $this->getAttribute( 'rc_user_text' ) &&
613 !MediaWikiServices
::getInstance()->getPermissionManager()
614 ->userHasRight( $user, 'autopatrol' )
616 $errors[] = [ 'markedaspatrollederror-noautopatrol' ];
621 // If the change was patrolled already, do nothing
622 if ( $this->getAttribute( 'rc_patrolled' ) ) {
625 // Actually set the 'patrolled' flag in RC
626 $this->reallyMarkPatrolled();
627 // Log this patrol event
628 PatrolLog
::record( $this, $auto, $user, $tags );
631 'MarkPatrolledComplete',
632 [ $this->getAttribute( 'rc_id' ), &$user, false, $auto ]
639 * Mark this RecentChange patrolled, without error checking
640 * @return int Number of affected rows
642 public function reallyMarkPatrolled() {
643 $dbw = wfGetDB( DB_MASTER
);
647 'rc_patrolled' => self
::PRC_PATROLLED
650 'rc_id' => $this->getAttribute( 'rc_id' )
654 // Invalidate the page cache after the page has been patrolled
655 // to make sure that the Patrol link isn't visible any longer!
656 $this->getTitle()->invalidateCache();
658 return $dbw->affectedRows();
662 * Makes an entry in the database corresponding to an edit
664 * @param string $timestamp
665 * @param Title $title
668 * @param string $comment
670 * @param string $lastTimestamp
673 * @param int $oldSize
674 * @param int $newSize
678 * @return RecentChange
680 public static function notifyEdit(
681 $timestamp, $title, $minor, $user, $comment, $oldId, $lastTimestamp,
682 $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0,
685 $rc = new RecentChange
;
686 $rc->mTitle
= $title;
687 $rc->mPerformer
= $user;
689 'rc_timestamp' => $timestamp,
690 'rc_namespace' => $title->getNamespace(),
691 'rc_title' => $title->getDBkey(),
692 'rc_type' => RC_EDIT
,
693 'rc_source' => self
::SRC_EDIT
,
694 'rc_minor' => $minor ?
1 : 0,
695 'rc_cur_id' => $title->getArticleID(),
696 'rc_user' => $user->getId(),
697 'rc_user_text' => $user->getName(),
698 'rc_actor' => $user->getActorId(),
699 'rc_comment' => &$comment,
700 'rc_comment_text' => &$comment,
701 'rc_comment_data' => null,
702 'rc_this_oldid' => $newId,
703 'rc_last_oldid' => $oldId,
704 'rc_bot' => $bot ?
1 : 0,
705 'rc_ip' => self
::checkIPAddress( $ip ),
706 'rc_patrolled' => intval( $patrol ),
707 'rc_new' => 0, # obsolete
708 'rc_old_len' => $oldSize,
709 'rc_new_len' => $newSize,
712 'rc_log_type' => null,
713 'rc_log_action' => '',
718 'prefixedDBkey' => $title->getPrefixedDBkey(),
719 'lastTimestamp' => $lastTimestamp,
720 'oldSize' => $oldSize,
721 'newSize' => $newSize,
722 'pageStatus' => 'changed'
725 DeferredUpdates
::addCallableUpdate(
726 function () use ( $rc, $tags ) {
727 $rc->addTags( $tags );
730 DeferredUpdates
::POSTSEND
,
738 * Makes an entry in the database corresponding to page creation
739 * Note: the title object must be loaded with the new id using resetArticleID()
741 * @param string $timestamp
742 * @param Title $title
745 * @param string $comment
752 * @return RecentChange
754 public static function notifyNew(
755 $timestamp, $title, $minor, $user, $comment, $bot,
756 $ip = '', $size = 0, $newId = 0, $patrol = 0, $tags = []
758 $rc = new RecentChange
;
759 $rc->mTitle
= $title;
760 $rc->mPerformer
= $user;
762 'rc_timestamp' => $timestamp,
763 'rc_namespace' => $title->getNamespace(),
764 'rc_title' => $title->getDBkey(),
766 'rc_source' => self
::SRC_NEW
,
767 'rc_minor' => $minor ?
1 : 0,
768 'rc_cur_id' => $title->getArticleID(),
769 'rc_user' => $user->getId(),
770 'rc_user_text' => $user->getName(),
771 'rc_actor' => $user->getActorId(),
772 'rc_comment' => &$comment,
773 'rc_comment_text' => &$comment,
774 'rc_comment_data' => null,
775 'rc_this_oldid' => $newId,
776 'rc_last_oldid' => 0,
777 'rc_bot' => $bot ?
1 : 0,
778 'rc_ip' => self
::checkIPAddress( $ip ),
779 'rc_patrolled' => intval( $patrol ),
780 'rc_new' => 1, # obsolete
782 'rc_new_len' => $size,
785 'rc_log_type' => null,
786 'rc_log_action' => '',
791 'prefixedDBkey' => $title->getPrefixedDBkey(),
792 'lastTimestamp' => 0,
795 'pageStatus' => 'created'
798 DeferredUpdates
::addCallableUpdate(
799 function () use ( $rc, $tags ) {
800 $rc->addTags( $tags );
803 DeferredUpdates
::POSTSEND
,
811 * @param string $timestamp
812 * @param Title $title
814 * @param string $actionComment
816 * @param string $type
817 * @param string $action
818 * @param Title $target
819 * @param string $logComment
820 * @param string $params
822 * @param string $actionCommentIRC
825 public static function notifyLog( $timestamp, $title, $user, $actionComment, $ip, $type,
826 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
828 global $wgLogRestrictions;
830 # Don't add private logs to RC!
831 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
834 $rc = self
::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
835 $target, $logComment, $params, $newId, $actionCommentIRC );
842 * @param string $timestamp
843 * @param Title $title
845 * @param string $actionComment
847 * @param string $type
848 * @param string $action
849 * @param Title $target
850 * @param string $logComment
851 * @param string $params
853 * @param string $actionCommentIRC
854 * @param int $revId Id of associated revision, if any
855 * @param bool $isPatrollable Whether this log entry is patrollable
856 * @return RecentChange
858 public static function newLogEntry( $timestamp, $title, $user, $actionComment, $ip,
859 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '',
860 $revId = 0, $isPatrollable = false ) {
862 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
864 # # Get pageStatus for email notification
865 switch ( $type . '-' . $action ) {
866 case 'delete-delete':
867 case 'delete-delete_redir':
868 $pageStatus = 'deleted';
871 case 'move-move_redir':
872 $pageStatus = 'moved';
874 case 'delete-restore':
875 $pageStatus = 'restored';
877 case 'upload-upload':
878 $pageStatus = 'created';
880 case 'upload-overwrite':
882 $pageStatus = 'changed';
886 // Allow unpatrolled status for patrollable log entries
887 $canAutopatrol = $permissionManager->userHasRight( $user, 'autopatrol' );
888 $markPatrolled = $isPatrollable ?
$canAutopatrol : true;
890 $rc = new RecentChange
;
891 $rc->mTitle
= $target;
892 $rc->mPerformer
= $user;
894 'rc_timestamp' => $timestamp,
895 'rc_namespace' => $target->getNamespace(),
896 'rc_title' => $target->getDBkey(),
898 'rc_source' => self
::SRC_LOG
,
900 'rc_cur_id' => $target->getArticleID(),
901 'rc_user' => $user->getId(),
902 'rc_user_text' => $user->getName(),
903 'rc_actor' => $user->getActorId(),
904 'rc_comment' => &$logComment,
905 'rc_comment_text' => &$logComment,
906 'rc_comment_data' => null,
907 'rc_this_oldid' => $revId,
908 'rc_last_oldid' => 0,
909 'rc_bot' => $permissionManager->userHasRight( $user, 'bot' ) ?
910 (int)$wgRequest->getBool( 'bot', true ) : 0,
911 'rc_ip' => self
::checkIPAddress( $ip ),
912 'rc_patrolled' => $markPatrolled ? self
::PRC_AUTOPATROLLED
: self
::PRC_UNPATROLLED
,
913 'rc_new' => 0, # obsolete
914 'rc_old_len' => null,
915 'rc_new_len' => null,
917 'rc_logid' => $newId,
918 'rc_log_type' => $type,
919 'rc_log_action' => $action,
920 'rc_params' => $params
924 'prefixedDBkey' => $title->getPrefixedDBkey(),
925 'lastTimestamp' => 0,
926 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
927 'pageStatus' => $pageStatus,
928 'actionCommentIRC' => $actionCommentIRC
935 * Constructs a RecentChange object for the given categorization
936 * This does not call save() on the object and thus does not write to the db
940 * @param string $timestamp Timestamp of the recent change to occur
941 * @param Title $categoryTitle Title of the category a page is being added to or removed from
942 * @param User|null $user User object of the user that made the change
943 * @param string $comment Change summary
944 * @param Title $pageTitle Title of the page that is being added or removed
945 * @param int $oldRevId Parent revision ID of this change
946 * @param int $newRevId Revision ID of this change
947 * @param string $lastTimestamp Parent revision timestamp of this change
948 * @param bool $bot true, if the change was made by a bot
949 * @param string $ip IP address of the user, if the change was made anonymously
950 * @param int $deleted Indicates whether the change has been deleted
951 * @param bool|null $added true, if the category was added, false for removed
953 * @return RecentChange
955 public static function newForCategorization(
957 Title
$categoryTitle,
969 // Done in a backwards compatible way.
971 'hidden-cat' => WikiCategoryPage
::factory( $categoryTitle )->isHidden()
973 if ( $added !== null ) {
974 $params['added'] = $added;
977 $rc = new RecentChange
;
978 $rc->mTitle
= $categoryTitle;
979 $rc->mPerformer
= $user;
981 'rc_timestamp' => $timestamp,
982 'rc_namespace' => $categoryTitle->getNamespace(),
983 'rc_title' => $categoryTitle->getDBkey(),
984 'rc_type' => RC_CATEGORIZE
,
985 'rc_source' => self
::SRC_CATEGORIZE
,
987 'rc_cur_id' => $pageTitle->getArticleID(),
988 'rc_user' => $user ?
$user->getId() : 0,
989 'rc_user_text' => $user ?
$user->getName() : '',
990 'rc_actor' => $user ?
$user->getActorId() : null,
991 'rc_comment' => &$comment,
992 'rc_comment_text' => &$comment,
993 'rc_comment_data' => null,
994 'rc_this_oldid' => $newRevId,
995 'rc_last_oldid' => $oldRevId,
996 'rc_bot' => $bot ?
1 : 0,
997 'rc_ip' => self
::checkIPAddress( $ip ),
998 'rc_patrolled' => self
::PRC_AUTOPATROLLED
, // Always patrolled, just like log entries
999 'rc_new' => 0, # obsolete
1000 'rc_old_len' => null,
1001 'rc_new_len' => null,
1002 'rc_deleted' => $deleted,
1004 'rc_log_type' => null,
1005 'rc_log_action' => '',
1006 'rc_params' => serialize( $params )
1010 'prefixedDBkey' => $categoryTitle->getPrefixedDBkey(),
1011 'lastTimestamp' => $lastTimestamp,
1014 'pageStatus' => 'changed'
1021 * Get a parameter value
1025 * @param string $name parameter name
1028 public function getParam( $name ) {
1029 $params = $this->parseParams();
1030 return $params[$name] ??
null;
1034 * Initialises the members of this object from a mysql row object
1038 public function loadFromRow( $row ) {
1039 $this->mAttribs
= get_object_vars( $row );
1040 $this->mAttribs
['rc_timestamp'] = wfTimestamp( TS_MW
, $this->mAttribs
['rc_timestamp'] );
1041 // rc_deleted MUST be set
1042 $this->mAttribs
['rc_deleted'] = $row->rc_deleted
;
1044 if ( isset( $this->mAttribs
['rc_ip'] ) ) {
1045 // Clean up CIDRs for Postgres per T164898. ("127.0.0.1" casts to "127.0.0.1/32")
1046 $n = strpos( $this->mAttribs
['rc_ip'], '/' );
1047 if ( $n !== false ) {
1048 $this->mAttribs
['rc_ip'] = substr( $this->mAttribs
['rc_ip'], 0, $n );
1052 $comment = CommentStore
::getStore()
1053 // Legacy because $row may have come from self::selectFields()
1054 ->getCommentLegacy( wfGetDB( DB_REPLICA
), 'rc_comment', $row, true )
1056 $this->mAttribs
['rc_comment'] = &$comment;
1057 $this->mAttribs
['rc_comment_text'] = &$comment;
1058 $this->mAttribs
['rc_comment_data'] = null;
1060 $user = User
::newFromAnyId(
1061 $this->mAttribs
['rc_user'] ??
null,
1062 $this->mAttribs
['rc_user_text'] ??
null,
1063 $this->mAttribs
['rc_actor'] ??
null
1065 $this->mAttribs
['rc_user'] = $user->getId();
1066 $this->mAttribs
['rc_user_text'] = $user->getName();
1067 $this->mAttribs
['rc_actor'] = $user->getActorId();
1071 * Get an attribute value
1073 * @param string $name Attribute name
1076 public function getAttribute( $name ) {
1077 if ( $name === 'rc_comment' ) {
1078 return CommentStore
::getStore()
1079 ->getComment( 'rc_comment', $this->mAttribs
, true )->text
;
1082 if ( $name === 'rc_user' ||
$name === 'rc_user_text' ||
$name === 'rc_actor' ) {
1083 $user = User
::newFromAnyId(
1084 $this->mAttribs
['rc_user'] ??
null,
1085 $this->mAttribs
['rc_user_text'] ??
null,
1086 $this->mAttribs
['rc_actor'] ??
null
1088 if ( $name === 'rc_user' ) {
1089 return $user->getId();
1091 if ( $name === 'rc_user_text' ) {
1092 return $user->getName();
1094 if ( $name === 'rc_actor' ) {
1095 return $user->getActorId();
1099 return $this->mAttribs
[$name] ??
null;
1105 public function getAttributes() {
1106 return $this->mAttribs
;
1110 * Gets the end part of the diff URL associated with this object
1111 * Blank if no diff link should be displayed
1112 * @param bool $forceCur
1115 public function diffLinkTrail( $forceCur ) {
1116 if ( $this->mAttribs
['rc_type'] == RC_EDIT
) {
1117 $trail = "curid=" . (int)( $this->mAttribs
['rc_cur_id'] ) .
1118 "&oldid=" . (int)( $this->mAttribs
['rc_last_oldid'] );
1120 $trail .= '&diff=0';
1122 $trail .= '&diff=' . (int)( $this->mAttribs
['rc_this_oldid'] );
1132 * Returns the change size (HTML).
1133 * The lengths can be given optionally.
1138 public function getCharacterDifference( $old = 0, $new = 0 ) {
1140 $old = $this->mAttribs
['rc_old_len'];
1143 $new = $this->mAttribs
['rc_new_len'];
1145 if ( $old === null ||
$new === null ) {
1149 return ChangesList
::showCharacterDifference( $old, $new );
1152 private static function checkIPAddress( $ip ) {
1155 if ( !IP
::isIPAddress( $ip ) ) {
1156 throw new MWException( "Attempt to write \"" . $ip .
1157 "\" as an IP address into recent changes" );
1160 $ip = $wgRequest->getIP();
1170 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
1171 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
1172 * or rows which will be deleted soon shouldn't be included.
1174 * @param mixed $timestamp MWTimestamp compatible timestamp
1175 * @param int $tolerance Tolerance in seconds
1178 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
1181 return wfTimestamp( TS_UNIX
, $timestamp ) > time() - $tolerance - $wgRCMaxAge;
1185 * Parses and returns the rc_params attribute
1189 * @return mixed|bool false on failed unserialization
1191 public function parseParams() {
1192 $rcParams = $this->getAttribute( 'rc_params' );
1194 Wikimedia\
suppressWarnings();
1195 $unserializedParams = unserialize( $rcParams );
1196 Wikimedia\restoreWarnings
();
1198 return $unserializedParams;
1202 * Tags to append to the recent change,
1203 * and associated revision/log
1207 * @param string|string[] $tags
1209 public function addTags( $tags ) {
1210 if ( is_string( $tags ) ) {
1211 $this->tags
[] = $tags;
1213 $this->tags
= array_merge( $tags, $this->tags
);