Merge "Allow ResourceLoaderImage modules without data URIs"
[lhc/web/wiklou.git] / includes / changetags / ChangeTags.php
index 429a3b6..7874640 100644 (file)
@@ -213,7 +213,7 @@ class ChangeTags {
         * @param int|null $rc_id The rc_id of the change to add the tags to
         * @param int|null $rev_id The rev_id of the change to add the tags to
         * @param int|null $log_id The log_id of the change to add the tags to
-        * @param string $params Params to put in the ct_params field of table 'change_tag'
+        * @param string|null $params Params to put in the ct_params field of table 'change_tag'
         * @param RecentChange|null $rc Recent change, in case the tagging accompanies the action
         * (this should normally be the case)
         *
@@ -244,7 +244,7 @@ class ChangeTags {
         * Pass a variable whose value is null if the rev_id is not relevant or unknown.
         * @param int|null &$log_id The log_id of the change to add the tags to.
         * Pass a variable whose value is null if the log_id is not relevant or unknown.
-        * @param string $params Params to put in the ct_params field of table
+        * @param string|null $params Params to put in the ct_params field of table
         * 'change_tag' when adding tags
         * @param RecentChange|null $rc Recent change being tagged, in case the tagging accompanies
         * the action
@@ -261,6 +261,8 @@ class ChangeTags {
                &$rev_id = null, &$log_id = null, $params = null, RecentChange $rc = null,
                User $user = null
        ) {
+               global $wgChangeTagsSchemaMigrationStage;
+
                $tagsToAdd = array_filter( (array)$tagsToAdd ); // Make sure we're submitting all tags...
                $tagsToRemove = array_filter( (array)$tagsToRemove );
 
@@ -342,6 +344,22 @@ class ChangeTags {
 
                // insert a row into change_tag for each new tag
                if ( count( $tagsToAdd ) ) {
+                       $changeTagMapping = [];
+                       if ( $wgChangeTagsSchemaMigrationStage > MIGRATION_OLD ) {
+                               $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
+
+                               foreach ( $tagsToAdd as $tag ) {
+                                       $changeTagMapping[$tag] = $changeTagDefStore->acquireId( $tag );
+                               }
+
+                               $dbw->update(
+                                       'change_tag_def',
+                                       [ 'ctd_count = ctd_count + 1' ],
+                                       [ 'ctd_name' => $tagsToAdd ],
+                                       __METHOD__
+                               );
+                       }
+
                        $tagsRows = [];
                        foreach ( $tagsToAdd as $tag ) {
                                // Filter so we don't insert NULLs as zero accidentally.
@@ -354,9 +372,11 @@ class ChangeTags {
                                                'ct_rc_id' => $rc_id,
                                                'ct_log_id' => $log_id,
                                                'ct_rev_id' => $rev_id,
-                                               'ct_params' => $params
+                                               'ct_params' => $params,
+                                               'ct_tag_id' => $changeTagMapping[$tag] ?? null,
                                        ]
                                );
+
                        }
 
                        $dbw->insert( 'change_tag', $tagsRows, __METHOD__, [ 'IGNORE' ] );
@@ -374,6 +394,20 @@ class ChangeTags {
                                        ]
                                );
                                $dbw->delete( 'change_tag', $conds, __METHOD__ );
+                               if ( $dbw->affectedRows() && $wgChangeTagsSchemaMigrationStage > MIGRATION_OLD ) {
+                                       $dbw->update(
+                                               'change_tag_def',
+                                               [ 'ctd_count = ctd_count - 1' ],
+                                               [ 'ctd_name' => $tag ],
+                                               __METHOD__
+                                       );
+
+                                       $dbw->delete(
+                                               'change_tag_def',
+                                               [ 'ctd_name' => $tag, 'ctd_count' => 0, 'ctd_user_defined' => 0 ],
+                                               __METHOD__
+                                       );
+                               }
                        }
                }
 
@@ -419,7 +453,7 @@ class ChangeTags {
                // $prevTags can be out of date on replica DBs, especially when addTags is called consecutively,
                // causing loss of tags added recently in tag_summary table.
                $prevTags = $dbw->selectField( 'tag_summary', 'ts_tags', $tsConds, __METHOD__ );
-               $prevTags = $prevTags ? $prevTags : '';
+               $prevTags = $prevTags ?: '';
                $prevTags = array_filter( explode( ',', $prevTags ) );
 
                // add tags
@@ -828,8 +862,8 @@ class ChangeTags {
        }
 
        /**
-        * Defines a tag in the valid_tag table, without checking that the tag name
-        * is valid.
+        * Defines a tag in the valid_tag table and/or update ctd_user_defined field in change_tag_def,
+        * without checking that the tag name is valid.
         * Extensions should NOT use this function; they can use the ListDefinedTags
         * hook instead.
         *
@@ -837,26 +871,63 @@ class ChangeTags {
         * @since 1.25
         */
        public static function defineTag( $tag ) {
+               global $wgChangeTagsSchemaMigrationStage;
+
                $dbw = wfGetDB( DB_MASTER );
-               $dbw->replace( 'valid_tag',
+               if ( $wgChangeTagsSchemaMigrationStage > MIGRATION_OLD ) {
+                       $tagDef = [
+                               'ctd_name' => $tag,
+                               'ctd_user_defined' => 1,
+                               'ctd_count' => 0
+                       ];
+                       $dbw->upsert(
+                               'change_tag_def',
+                               $tagDef,
+                               [ 'ctd_name' ],
+                               [ 'ctd_user_defined' => 1 ],
+                               __METHOD__
+                       );
+               }
+
+               $dbw->replace(
+                       'valid_tag',
                        [ 'vt_tag' ],
                        [ 'vt_tag' => $tag ],
-                       __METHOD__ );
+                       __METHOD__
+               );
 
                // clear the memcache of defined tags
                self::purgeTagCacheAll();
        }
 
        /**
-        * Removes a tag from the valid_tag table. The tag may remain in use by
-        * extensions, and may still show up as 'defined' if an extension is setting
-        * it from the ListDefinedTags hook.
+        * Removes a tag from the valid_tag table and/or update ctd_user_defined field in change_tag_def.
+        * The tag may remain in use by extensions, and may still show up as 'defined'
+        * if an extension is setting it from the ListDefinedTags hook.
         *
         * @param string $tag Tag to remove
         * @since 1.25
         */
        public static function undefineTag( $tag ) {
+               global $wgChangeTagsSchemaMigrationStage;
+
                $dbw = wfGetDB( DB_MASTER );
+
+               if ( $wgChangeTagsSchemaMigrationStage > MIGRATION_OLD ) {
+                       $dbw->update(
+                               'change_tag_def',
+                               [ 'ctd_name' => $tag ],
+                               [ 'ctd_user_defined' => 0 ],
+                               __METHOD__
+                       );
+
+                       $dbw->delete(
+                               'change_tag_def',
+                               [ 'ctd_name' => $tag, 'ctd_count' => 0 ],
+                               __METHOD__
+                       );
+               }
+
                $dbw->delete( 'valid_tag', [ 'vt_tag' => $tag ], __METHOD__ );
 
                // clear the memcache of defined tags
@@ -870,7 +941,7 @@ class ChangeTags {
         * @param string $tag
         * @param string $reason
         * @param User $user Who to attribute the action to
-        * @param int $tagCount For deletion only, how many usages the tag had before
+        * @param int|null $tagCount For deletion only, how many usages the tag had before
         * it was deleted.
         * @param array $logEntryTags Change tags to apply to the entry
         * that will be created in the tag management log
@@ -1108,6 +1179,7 @@ class ChangeTags {
 
        /**
         * Creates a tag by adding a row to the `valid_tag` table.
+        * and/or add it to `change_tag_def` table.
         *
         * Extensions should NOT use this function; they can use the ListDefinedTags
         * hook instead.
@@ -1158,10 +1230,11 @@ class ChangeTags {
         * @since 1.25
         */
        public static function deleteTagEverywhere( $tag ) {
+               global $wgChangeTagsSchemaMigrationStage;
                $dbw = wfGetDB( DB_MASTER );
                $dbw->startAtomic( __METHOD__ );
 
-               // delete from valid_tag
+               // delete from valid_tag and/or set ctd_user_defined = 0
                self::undefineTag( $tag );
 
                // find out which revisions use this tag, so we can delete from tag_summary
@@ -1180,6 +1253,10 @@ class ChangeTags {
                // delete from change_tag
                $dbw->delete( 'change_tag', [ 'ct_tag' => $tag ], __METHOD__ );
 
+               if ( $wgChangeTagsSchemaMigrationStage > MIGRATION_OLD ) {
+                       $dbw->delete( 'change_tag_def', [ 'ctd_name' => $tag ], __METHOD__ );
+               }
+
                $dbw->endAtomic( __METHOD__ );
 
                // give extensions a chance