Add config to use change_tag_def table for Special:Tags
authorAmir Sarabadani <ladsgroup@gmail.com>
Wed, 11 Jul 2018 14:44:28 +0000 (16:44 +0200)
committerAmir Sarabadani <ladsgroup@gmail.com>
Wed, 11 Jul 2018 14:44:28 +0000 (16:44 +0200)
Bug: T199334
Change-Id: Ieb8709caf0d8ee16086296baa75f751c7723c101

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

index 2642884..9f9d8ba 100644 (file)
@@ -8944,6 +8944,18 @@ $wgExpiryWidgetNoDatePicker = false;
  */
 $wgChangeTagsSchemaMigrationStage = MIGRATION_OLD;
 
+/**
+ * Temporarily flag to use change_tag_def table as backend of change tag statistics.
+ * For example in case of Special:Tags. If set to false, it will use change_tag table.
+ * Before setting it to true set $wgChangeTagsSchemaMigrationStage to MIGRATION_WRITE_BOTH and run
+ * PopulateChangeTagDef maintaince script.
+ * It's redundant when $wgChangeTagsSchemaMigrationStage is set to MIGRATION_NEW
+ *
+ * @since 1.32
+ * @var bool
+ */
+$wgTagStatisticsNewTable = false;
+
 /**
  * For really cool vim folding this needs to be at the end:
  * vim: foldmarker=@{,@} foldmethod=marker
index 7874640..3bb777e 100644 (file)
@@ -1507,6 +1507,13 @@ class ChangeTags {
         * @return array Array of string => int
         */
        public static function tagUsageStatistics() {
+               global $wgChangeTagsSchemaMigrationStage, $wgTagStatisticsNewTable;
+               if ( $wgChangeTagsSchemaMigrationStage > MIGRATION_WRITE_BOTH ||
+                       ( $wgTagStatisticsNewTable && $wgChangeTagsSchemaMigrationStage > MIGRATION_OLD )
+               ) {
+                       return self::newTagUsageStatistics();
+               }
+
                $fname = __METHOD__;
                $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
                return $cache->getWithSetCallback(
@@ -1540,6 +1547,29 @@ class ChangeTags {
                );
        }
 
+       /**
+        * Same self::tagUsageStatistics() but uses change_tag_def.
+        *
+        * @return array Array of string => int
+        */
+       private static function newTagUsageStatistics() {
+               $dbr = wfGetDB( DB_REPLICA );
+               $res = $dbr->select(
+                       'change_tag_def',
+                       [ 'ctd_name', 'ctd_count' ],
+                       [],
+                       __METHOD__,
+                       [ 'ORDER BY' => 'ctd_count DESC' ]
+               );
+
+               $out = [];
+               foreach ( $res as $row ) {
+                       $out[$row->ctd_name] = $row->ctd_count;
+               }
+
+               return $out;
+       }
+
        /**
         * Indicate whether change tag editing UI is relevant
         *
index d7c8ec4..03d0579 100644 (file)
@@ -537,4 +537,56 @@ class ChangeTagsTest extends MediaWikiTestCase {
                $this->assertEquals( $expected2, iterator_to_array( $res2, false ) );
        }
 
+       public function testTagUsageStatisticsOldBackend() {
+               $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_OLD );
+               $this->setMwGlobals( 'wgTagStatisticsNewTable', false );
+
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->delete( 'change_tag', '*' );
+               $dbw->delete( 'change_tag_def', '*' );
+
+               $rcId = 123;
+               ChangeTags::updateTags( [ 'tag1', 'tag2' ], [], $rcId );
+
+               $rcId = 124;
+               ChangeTags::updateTags( [ 'tag1' ], [], $rcId );
+
+               $this->assertEquals( [ 'tag1' => 2, 'tag2' => 1 ], ChangeTags::tagUsageStatistics() );
+       }
+
+       public function testTagUsageStatisticsNewMigrationOldBackedn() {
+               $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH );
+               $this->setMwGlobals( 'wgTagStatisticsNewTable', false );
+
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->delete( 'change_tag', '*' );
+               $dbw->delete( 'change_tag_def', '*' );
+               MediaWikiServices::getInstance()->resetServiceForTesting( 'ChangeTagDefStore' );
+
+               $rcId = 123;
+               ChangeTags::updateTags( [ 'tag1', 'tag2' ], [], $rcId );
+
+               $rcId = 124;
+               ChangeTags::updateTags( [ 'tag1' ], [], $rcId );
+
+               $this->assertEquals( [ 'tag1' => 2, 'tag2' => 1 ], ChangeTags::tagUsageStatistics() );
+       }
+
+       public function testTagUsageStatisticsNewBackend() {
+               $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH );
+               $this->setMwGlobals( 'wgTagStatisticsNewTable', true );
+
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->delete( 'change_tag', '*' );
+               $dbw->delete( 'change_tag_def', '*' );
+               MediaWikiServices::getInstance()->resetServiceForTesting( 'ChangeTagDefStore' );
+
+               $rcId = 123;
+               ChangeTags::updateTags( [ 'tag1', 'tag2' ], [], $rcId );
+
+               $rcId = 124;
+               ChangeTags::updateTags( [ 'tag1' ], [], $rcId );
+
+               $this->assertEquals( [ 'tag1' => 2, 'tag2' => 1 ], ChangeTags::tagUsageStatistics() );
+       }
 }