Code to read from change_tag_def table instead of valid_tag
authorAmir Sarabadani <ladsgroup@gmail.com>
Tue, 11 Sep 2018 08:10:41 +0000 (10:10 +0200)
committerAmir Sarabadani <ladsgroup@gmail.com>
Fri, 14 Sep 2018 11:50:59 +0000 (13:50 +0200)
This table will be dropped once the migration is completed

Bug: T194162
Change-Id: I2575b22fcdf4812dd62055f7cf157255c37a2c1f

includes/changetags/ChangeTags.php
tests/phpunit/includes/changetags/ChangeTagsTest.php

index 008a2f6..8955028 100644 (file)
@@ -927,13 +927,14 @@ class ChangeTags {
                        );
                }
 
-               $dbw->replace(
-                       'valid_tag',
-                       [ 'vt_tag' ],
-                       [ 'vt_tag' => $tag ],
-                       __METHOD__
-               );
-
+               if ( $wgChangeTagsSchemaMigrationStage < MIGRATION_NEW ) {
+                       $dbw->replace(
+                               'valid_tag',
+                               [ 'vt_tag' ],
+                               [ 'vt_tag' => $tag ],
+                               __METHOD__
+                       );
+               }
                // clear the memcache of defined tags
                self::purgeTagCacheAll();
        }
@@ -966,7 +967,9 @@ class ChangeTags {
                        );
                }
 
-               $dbw->delete( 'valid_tag', [ 'vt_tag' => $tag ], __METHOD__ );
+               if ( $wgChangeTagsSchemaMigrationStage < MIGRATION_NEW ) {
+                       $dbw->delete( 'valid_tag', [ 'vt_tag' => $tag ], __METHOD__ );
+               }
 
                // clear the memcache of defined tags
                self::purgeTagCacheAll();
@@ -1457,7 +1460,7 @@ class ChangeTags {
        /**
         * Lists tags explicitly defined in the `valid_tag` table of the database.
         * Tags in table 'change_tag' which are not in table 'valid_tag' are not
-        * included.
+        * included. In case of new backend loads the data from `change_tag_def` table.
         *
         * Tries memcached first.
         *
@@ -1472,11 +1475,16 @@ class ChangeTags {
                        $cache->makeKey( 'valid-tags-db' ),
                        WANObjectCache::TTL_MINUTE * 5,
                        function ( $oldValue, &$ttl, array &$setOpts ) use ( $fname ) {
+                               global $wgChangeTagsSchemaMigrationStage;
                                $dbr = wfGetDB( DB_REPLICA );
 
                                $setOpts += Database::getCacheSetOptions( $dbr );
 
-                               $tags = $dbr->selectFieldValues( 'valid_tag', 'vt_tag', [], $fname );
+                               if ( $wgChangeTagsSchemaMigrationStage > MIGRATION_WRITE_BOTH ) {
+                                       $tags = self::listExplicitlyDefinedTagsNewBackend();
+                               } else {
+                                       $tags = $dbr->selectFieldValues( 'valid_tag', 'vt_tag', [], $fname );
+                               }
 
                                return array_filter( array_unique( $tags ) );
                        },
@@ -1488,6 +1496,22 @@ class ChangeTags {
                );
        }
 
+       /**
+        * Lists tags explicitly user defined tags. When ctd_user_defined is true.
+        *
+        * @return string[] Array of strings: tags
+        * @since 1.25
+        */
+       private static function listExplicitlyDefinedTagsNewBackend() {
+               $dbr = wfGetDB( DB_REPLICA );
+               return $dbr->selectFieldValues(
+                       'change_tag_def',
+                       'ctd_name',
+                       [ 'ctd_user_defined' => 1 ],
+                       __METHOD__
+               );
+       }
+
        /**
         * Lists tags defined by core or extensions using the ListDefinedTags hook.
         * Extensions need only define those tags they deem to be in active use.
index c770029..64c3224 100644 (file)
@@ -14,6 +14,7 @@ class ChangeTagsTest extends MediaWikiTestCase {
                $this->tablesUsed[] = 'change_tag';
                $this->tablesUsed[] = 'change_tag_def';
                $this->tablesUsed[] = 'tag_summary';
+               $this->tablesUsed[] = 'valid_tag';
        }
 
        // TODO only modifyDisplayQuery and getSoftwareTags are tested, nothing else is
@@ -592,4 +593,83 @@ class ChangeTagsTest extends MediaWikiTestCase {
 
                $this->assertEquals( [ 'tag1' => 2, 'tag2' => 1 ], ChangeTags::tagUsageStatistics() );
        }
+
+       public function testListExplicitlyDefinedTagsOld() {
+               $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_OLD );
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->delete( 'change_tag', '*' );
+               $dbw->delete( 'change_tag_def', '*' );
+               $dbw->delete( 'valid_tag', '*' );
+
+               $rcId = 123;
+               ChangeTags::updateTags( [ 'tag1', 'tag2' ], [], $rcId );
+               ChangeTags::defineTag( 'tag2' );
+
+               $this->assertEquals( [ 'tag2' ], ChangeTags::listExplicitlyDefinedTags() );
+               $dbr = wfGetDB( DB_REPLICA );
+               $res = $dbr->select( 'change_tag_def', [ 'ctd_name', 'ctd_user_defined' ], '' );
+               $this->assertEquals( [], iterator_to_array( $res, false ) );
+
+               $this->assertEquals( [ 'tag2' ], $dbr->selectFieldValues( 'valid_tag', 'vt_tag', '' ) );
+       }
+
+       public function testListExplicitlyDefinedTagsWriteBoth() {
+               $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH );
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->delete( 'change_tag', '*' );
+               $dbw->delete( 'change_tag_def', '*' );
+               $dbw->delete( 'valid_tag', '*' );
+
+               $rcId = 123;
+               ChangeTags::updateTags( [ 'tag1', 'tag2' ], [], $rcId );
+               ChangeTags::defineTag( 'tag2' );
+
+               $this->assertEquals( [ 'tag2' ], ChangeTags::listExplicitlyDefinedTags() );
+               $dbr = wfGetDB( DB_REPLICA );
+
+               $expected = [
+                       (object)[
+                               'ctd_name' => 'tag1',
+                               'ctd_user_defined' => 0
+                       ],
+                       (object)[
+                               'ctd_name' => 'tag2',
+                               'ctd_user_defined' => 1
+                       ],
+               ];
+               $res = $dbr->select( 'change_tag_def', [ 'ctd_name', 'ctd_user_defined' ], '' );
+               $this->assertEquals( $expected, iterator_to_array( $res, false ) );
+
+               $this->assertEquals( [ 'tag2' ], $dbr->selectFieldValues( 'valid_tag', 'vt_tag', '' ) );
+       }
+
+       public function testListExplicitlyDefinedTagsNew() {
+               $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_NEW );
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->delete( 'change_tag', '*' );
+               $dbw->delete( 'change_tag_def', '*' );
+               $dbw->delete( 'valid_tag', '*' );
+
+               $rcId = 123;
+               ChangeTags::updateTags( [ 'tag1', 'tag2' ], [], $rcId );
+               ChangeTags::defineTag( 'tag2' );
+
+               $this->assertEquals( [ 'tag2' ], ChangeTags::listExplicitlyDefinedTags() );
+               $dbr = wfGetDB( DB_REPLICA );
+
+               $expected = [
+                       (object)[
+                               'ctd_name' => 'tag1',
+                               'ctd_user_defined' => 0
+                       ],
+                       (object)[
+                               'ctd_name' => 'tag2',
+                               'ctd_user_defined' => 1
+                       ],
+               ];
+               $res = $dbr->select( 'change_tag_def', [ 'ctd_name', 'ctd_user_defined' ], '' );
+               $this->assertEquals( $expected, iterator_to_array( $res, false ) );
+
+               $this->assertEquals( [], $dbr->selectFieldValues( 'valid_tag', 'vt_tag', '' ) );
+       }
 }