*/
$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
* @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(
);
}
+ /**
+ * 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
*
$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() );
+ }
}