From 9fed3f0fd3aeb66f06097a40a7cad9a34fb5020a Mon Sep 17 00:00:00 2001 From: Amir Sarabadani Date: Wed, 11 Jul 2018 16:44:28 +0200 Subject: [PATCH] Add config to use change_tag_def table for Special:Tags Bug: T199334 Change-Id: Ieb8709caf0d8ee16086296baa75f751c7723c101 --- includes/DefaultSettings.php | 12 +++++ includes/changetags/ChangeTags.php | 30 +++++++++++ .../includes/changetags/ChangeTagsTest.php | 52 +++++++++++++++++++ 3 files changed, 94 insertions(+) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 26428842f9..9f9d8ba08f 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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 diff --git a/includes/changetags/ChangeTags.php b/includes/changetags/ChangeTags.php index 7874640849..3bb777ec73 100644 --- a/includes/changetags/ChangeTags.php +++ b/includes/changetags/ChangeTags.php @@ -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 * diff --git a/tests/phpunit/includes/changetags/ChangeTagsTest.php b/tests/phpunit/includes/changetags/ChangeTagsTest.php index d7c8ec4520..03d0579a19 100644 --- a/tests/phpunit/includes/changetags/ChangeTagsTest.php +++ b/tests/phpunit/includes/changetags/ChangeTagsTest.php @@ -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() ); + } } -- 2.20.1