Provide a Taggable interface
[lhc/web/wiklou.git] / includes / changes / RecentChange.php
index 8e8b93f..2d37eac 100644 (file)
@@ -19,6 +19,7 @@
  *
  * @file
  */
+use MediaWiki\ChangeTags\Taggable;
 
 /**
  * Utility class for creating new RC entries
@@ -65,7 +66,7 @@
  *  we're having to include both rc_comment and rc_comment_text/rc_comment_data
  *  so random crap works right.
  */
-class RecentChange {
+class RecentChange implements Taggable {
        // Constants for the rc_source field.  Extensions may also have
        // their own source constants.
        const SRC_EDIT = 'mw.edit';
@@ -78,6 +79,16 @@ class RecentChange {
        const PRC_PATROLLED = 1;
        const PRC_AUTOPATROLLED = 2;
 
+       /**
+        * @var bool For save() - save to the database only, without any events.
+        */
+       const SEND_NONE = true;
+
+       /**
+        * @var bool For save() - do emit the change to RCFeeds (usually public).
+        */
+       const SEND_FEED = false;
+
        public $mAttribs = [];
        public $mExtra = [];
 
@@ -218,13 +229,14 @@ class RecentChange {
                global $wgActorTableSchemaMigrationStage;
 
                wfDeprecated( __METHOD__, '1.31' );
-               if ( $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH ) {
+               if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
                        // If code is using this instead of self::getQueryInfo(), there's a
                        // decent chance it's going to try to directly access
                        // $row->rc_user or $row->rc_user_text and we can't give it
-                       // useful values here once those aren't being written anymore.
+                       // useful values here once those aren't being used anymore.
                        throw new BadMethodCallException(
-                               'Cannot use ' . __METHOD__ . ' when $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH'
+                               'Cannot use ' . __METHOD__
+                                       . ' when $wgActorTableSchemaMigrationStage has SCHEMA_COMPAT_READ_NEW'
                        );
                }
 
@@ -347,11 +359,23 @@ class RecentChange {
 
        /**
         * Writes the data in this object to the database
-        * @param bool $noudp
+        *
+        * For compatibility reasons, the SEND_ constants internally reference a value
+        * that may seem negated from their purpose (none=true, feed=false). This is
+        * because the parameter used to be called "$noudp", defaulting to false.
+        *
+        * @param bool $send self::SEND_FEED or self::SEND_NONE
         */
-       public function save( $noudp = false ) {
+       public function save( $send = self::SEND_FEED ) {
                global $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker;
 
+               if ( is_string( $send ) ) {
+                       // Callers used to pass undocumented strings like 'noudp'
+                       // or 'pleasedontudp' instead of self::SEND_NONE (true).
+                       // @deprecated since 1.31 Use SEND_NONE instead.
+                       $send = self::SEND_NONE;
+               }
+
                $dbw = wfGetDB( DB_MASTER );
                if ( !is_array( $this->mExtra ) ) {
                        $this->mExtra = [];
@@ -398,9 +422,9 @@ class RecentChange {
 
                # Convert mAttribs['rc_user'] etc for ActorMigration
                $user = User::newFromAnyId(
-                       isset( $row['rc_user'] ) ? $row['rc_user'] : null,
-                       isset( $row['rc_user_text'] ) ? $row['rc_user_text'] : null,
-                       isset( $row['rc_actor'] ) ? $row['rc_actor'] : null
+                       $row['rc_user'] ?? null,
+                       $row['rc_user_text'] ?? null,
+                       $row['rc_actor'] ?? null
                );
                unset( $row['rc_user'], $row['rc_user_text'], $row['rc_actor'] );
                $row += ActorMigration::newMigration()->getInsertValues( $dbw, 'rc_user', $user );
@@ -425,8 +449,8 @@ class RecentChange {
                                $this->mAttribs['rc_this_oldid'], $this->mAttribs['rc_logid'], null, $this );
                }
 
-               # Notify external application via UDP
-               if ( !$noudp ) {
+               if ( $send === self::SEND_FEED ) {
+                       // Emit the change to external applications via RCFeeds.
                        $this->notifyRCFeeds();
                }
 
@@ -442,7 +466,7 @@ class RecentChange {
                        ) {
                                // @FIXME: This would be better as an extension hook
                                // Send emails or email jobs once this row is safely committed
-                               $dbw->onTransactionIdle(
+                               $dbw->onTransactionCommitOrIdle(
                                        function () use ( $editor, $title ) {
                                                $enotif = new EmailNotification();
                                                $enotif->notifyOnPageChange(
@@ -468,7 +492,7 @@ class RecentChange {
 
        /**
         * Notify all the feeds about the change.
-        * @param array $feeds Optional feeds to send to, defaults to $wgRCFeeds
+        * @param array|null $feeds Optional feeds to send to, defaults to $wgRCFeeds
         */
        public function notifyRCFeeds( array $feeds = null ) {
                global $wgRCFeeds;
@@ -498,11 +522,7 @@ class RecentChange {
                                continue;
                        }
 
-                       if ( isset( $this->mExtra['actionCommentIRC'] ) ) {
-                               $actionComment = $this->mExtra['actionCommentIRC'];
-                       } else {
-                               $actionComment = null;
-                       }
+                       $actionComment = $this->mExtra['actionCommentIRC'] ?? null;
 
                        $feed = RCFeed::factory( $params );
                        $feed->notify( $this, $actionComment );
@@ -538,7 +558,7 @@ class RecentChange {
         *
         * @param RecentChange|int $change RecentChange or corresponding rc_id
         * @param bool $auto For automatic patrol
-        * @param string|string[] $tags Change tags to add to the patrol log entry
+        * @param string|string[]|null $tags Change tags to add to the patrol log entry
         *   ($user should be able to add the specified tags before this is called)
         * @return array See doMarkPatrolled(), or null if $change is not an existing rc_id
         */
@@ -563,7 +583,7 @@ class RecentChange {
         * 'markedaspatrollederror-noautopatrol' as errors
         * @param User $user User object doing the action
         * @param bool $auto For automatic patrol
-        * @param string|string[] $tags Change tags to add to the patrol log entry
+        * @param string|string[]|null $tags Change tags to add to the patrol log entry
         *   ($user should be able to add the specified tags before this is called)
         * @return array Array of permissions errors, see Title::getUserPermissionsErrors()
         */
@@ -640,9 +660,9 @@ class RecentChange {
         * Makes an entry in the database corresponding to an edit
         *
         * @param string $timestamp
-        * @param Title &$title
+        * @param Title $title
         * @param bool $minor
-        * @param User &$user
+        * @param User $user
         * @param string $comment
         * @param int $oldId
         * @param string $lastTimestamp
@@ -656,7 +676,7 @@ class RecentChange {
         * @return RecentChange
         */
        public static function notifyEdit(
-               $timestamp, &$title, $minor, &$user, $comment, $oldId, $lastTimestamp,
+               $timestamp, $title, $minor, $user, $comment, $oldId, $lastTimestamp,
                $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0,
                $tags = []
        ) {
@@ -717,9 +737,9 @@ class RecentChange {
         * Note: the title object must be loaded with the new id using resetArticleID()
         *
         * @param string $timestamp
-        * @param Title &$title
+        * @param Title $title
         * @param bool $minor
-        * @param User &$user
+        * @param User $user
         * @param string $comment
         * @param bool $bot
         * @param string $ip
@@ -730,7 +750,7 @@ class RecentChange {
         * @return RecentChange
         */
        public static function notifyNew(
-               $timestamp, &$title, $minor, &$user, $comment, $bot,
+               $timestamp, $title, $minor, $user, $comment, $bot,
                $ip = '', $size = 0, $newId = 0, $patrol = 0, $tags = []
        ) {
                $rc = new RecentChange;
@@ -787,8 +807,8 @@ class RecentChange {
 
        /**
         * @param string $timestamp
-        * @param Title &$title
-        * @param User &$user
+        * @param Title $title
+        * @param User $user
         * @param string $actionComment
         * @param string $ip
         * @param string $type
@@ -800,7 +820,7 @@ class RecentChange {
         * @param string $actionCommentIRC
         * @return bool
         */
-       public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
+       public static function notifyLog( $timestamp, $title, $user, $actionComment, $ip, $type,
                $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
        ) {
                global $wgLogRestrictions;
@@ -818,8 +838,8 @@ class RecentChange {
 
        /**
         * @param string $timestamp
-        * @param Title &$title
-        * @param User &$user
+        * @param Title $title
+        * @param User $user
         * @param string $actionComment
         * @param string $ip
         * @param string $type
@@ -833,7 +853,7 @@ class RecentChange {
         * @param bool $isPatrollable Whether this log entry is patrollable
         * @return RecentChange
         */
-       public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
+       public static function newLogEntry( $timestamp, $title, $user, $actionComment, $ip,
                $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '',
                $revId = 0, $isPatrollable = false ) {
                global $wgRequest;
@@ -884,7 +904,7 @@ class RecentChange {
                        'rc_last_oldid' => 0,
                        'rc_bot' => $user->isAllowed( 'bot' ) ? (int)$wgRequest->getBool( 'bot', true ) : 0,
                        'rc_ip' => self::checkIPAddress( $ip ),
-                       'rc_patrolled' => $markPatrolled ? self::PRC_PATROLLED : self::PRC_UNPATROLLED,
+                       'rc_patrolled' => $markPatrolled ? self::PRC_AUTOPATROLLED : self::PRC_UNPATROLLED,
                        'rc_new' => 0, # obsolete
                        'rc_old_len' => null,
                        'rc_new_len' => null,
@@ -914,7 +934,7 @@ class RecentChange {
         *
         * @param string $timestamp Timestamp of the recent change to occur
         * @param Title $categoryTitle Title of the category a page is being added to or removed from
-        * @param User $user User object of the user that made the change
+        * @param User|null $user User object of the user that made the change
         * @param string $comment Change summary
         * @param Title $pageTitle Title of the page that is being added or removed
         * @param int $oldRevId Parent revision ID of this change
@@ -923,7 +943,7 @@ class RecentChange {
         * @param bool $bot true, if the change was made by a bot
         * @param string $ip IP address of the user, if the change was made anonymously
         * @param int $deleted Indicates whether the change has been deleted
-        * @param bool $added true, if the category was added, false for removed
+        * @param bool|null $added true, if the category was added, false for removed
         *
         * @return RecentChange
         */
@@ -970,7 +990,7 @@ class RecentChange {
                        'rc_last_oldid' => $oldRevId,
                        'rc_bot' => $bot ? 1 : 0,
                        'rc_ip' => self::checkIPAddress( $ip ),
-                       'rc_patrolled' => self::PRC_PATROLLED, // Always patrolled, just like log entries
+                       'rc_patrolled' => self::PRC_AUTOPATROLLED, // Always patrolled, just like log entries
                        'rc_new' => 0, # obsolete
                        'rc_old_len' => null,
                        'rc_new_len' => null,
@@ -1002,7 +1022,7 @@ class RecentChange {
         */
        public function getParam( $name ) {
                $params = $this->parseParams();
-               return isset( $params[$name] ) ? $params[$name] : null;
+               return $params[$name] ?? null;
        }
 
        /**
@@ -1033,9 +1053,9 @@ class RecentChange {
                $this->mAttribs['rc_comment_data'] = null;
 
                $user = User::newFromAnyId(
-                       isset( $this->mAttribs['rc_user'] ) ? $this->mAttribs['rc_user'] : null,
-                       isset( $this->mAttribs['rc_user_text'] ) ? $this->mAttribs['rc_user_text'] : null,
-                       isset( $this->mAttribs['rc_actor'] ) ? $this->mAttribs['rc_actor'] : null
+                       $this->mAttribs['rc_user'] ?? null,
+                       $this->mAttribs['rc_user_text'] ?? null,
+                       $this->mAttribs['rc_actor'] ?? null
                );
                $this->mAttribs['rc_user'] = $user->getId();
                $this->mAttribs['rc_user_text'] = $user->getName();
@@ -1056,9 +1076,9 @@ class RecentChange {
 
                if ( $name === 'rc_user' || $name === 'rc_user_text' || $name === 'rc_actor' ) {
                        $user = User::newFromAnyId(
-                               isset( $this->mAttribs['rc_user'] ) ? $this->mAttribs['rc_user'] : null,
-                               isset( $this->mAttribs['rc_user_text'] ) ? $this->mAttribs['rc_user_text'] : null,
-                               isset( $this->mAttribs['rc_actor'] ) ? $this->mAttribs['rc_actor'] : null
+                               $this->mAttribs['rc_user'] ?? null,
+                               $this->mAttribs['rc_user_text'] ?? null,
+                               $this->mAttribs['rc_actor'] ?? null
                        );
                        if ( $name === 'rc_user' ) {
                                return $user->getId();
@@ -1071,7 +1091,7 @@ class RecentChange {
                        }
                }
 
-               return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : null;
+               return $this->mAttribs[$name] ?? null;
        }
 
        /**
@@ -1179,7 +1199,7 @@ class RecentChange {
         *
         * @since 1.28
         *
-        * @param string|array $tags
+        * @param string|string[] $tags
         */
        public function addTags( $tags ) {
                if ( is_string( $tags ) ) {