From a005f1de98fa4ee0c8d2ce37da61ff1ee5c96ba2 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Mon, 25 Sep 2017 12:37:13 +0200 Subject: [PATCH] Clean up wiki ID and DB domain ID handling Bug: T174017 Change-Id: I42299a393c6691d39817db1d83a176a17df2474b --- includes/Revision.php | 4 ++-- includes/WatchedItemStore.php | 2 +- includes/WikiMap.php | 19 +++++++++++++++++++ includes/deferred/LinksDeletionUpdate.php | 8 ++++---- includes/deferred/LinksUpdate.php | 18 +++++++++++------- includes/deferred/WANCacheReapUpdate.php | 2 +- includes/libs/rdbms/database/IDatabase.php | 1 + .../ResourceLoaderWikiModule.php | 9 ++++++--- includes/user/UserRightsProxy.php | 6 +++--- maintenance/populateContentModel.php | 6 +++--- tests/phpunit/includes/WikiMapTest.php | 17 +++++++++++++++++ 11 files changed, 68 insertions(+), 24 deletions(-) diff --git a/includes/Revision.php b/includes/Revision.php index 006e700645..bcfbe638b4 100644 --- a/includes/Revision.php +++ b/includes/Revision.php @@ -362,7 +362,7 @@ class Revision implements IDBAccessObject { $row = self::fetchFromConds( $db, $conditions, $flags ); if ( $row ) { $rev = new Revision( $row ); - $rev->mWiki = $db->getWikiID(); + $rev->mWiki = $db->getDomainID(); return $rev; } @@ -1941,7 +1941,7 @@ class Revision implements IDBAccessObject { $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); return $cache->getWithSetCallback( // Page/rev IDs passed in from DB to reflect history merges - $cache->makeGlobalKey( 'revision', $db->getWikiID(), $pageId, $revId ), + $cache->makeGlobalKey( 'revision', $db->getDomainID(), $pageId, $revId ), $cache::TTL_WEEK, function ( $curValue, &$ttl, array &$setOpts ) use ( $db, $pageId, $revId ) { $setOpts += Database::getCacheSetOptions( $db ); diff --git a/includes/WatchedItemStore.php b/includes/WatchedItemStore.php index 69a9df2d57..60d8b7699c 100644 --- a/includes/WatchedItemStore.php +++ b/includes/WatchedItemStore.php @@ -766,7 +766,7 @@ class WatchedItemStore implements StatsdAwareInterface { ); if ( count( $watchersChunks ) > 1 ) { $factory->commitAndWaitForReplication( - __METHOD__, $ticket, [ 'wiki' => $dbw->getWikiID() ] + __METHOD__, $ticket, [ 'domain' => $dbw->getDomainID() ] ); } } diff --git a/includes/WikiMap.php b/includes/WikiMap.php index 4f3c461699..8bb37b5c88 100644 --- a/includes/WikiMap.php +++ b/includes/WikiMap.php @@ -21,6 +21,7 @@ */ use MediaWiki\MediaWikiServices; +use Wikimedia\Rdbms\DatabaseDomain; /** * Helper tools for dealing with other locally-hosted wikis. @@ -239,4 +240,22 @@ class WikiMap { return false; } + + /** + * Get the wiki ID of a database domain + * + * This is like DatabaseDomain::getId() without encoding (for legacy reasons) + * + * @param string|DatabaseDomain $domain + * @return string + */ + public static function getWikiIdFromDomain( $domain ) { + if ( !( $domain instanceof DatabaseDomain ) ) { + $domain = DatabaseDomain::newFromId( $domain ); + } + + return strlen( $domain->getTablePrefix() ) + ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}" + : $domain->getDatabase(); + } } diff --git a/includes/deferred/LinksDeletionUpdate.php b/includes/deferred/LinksDeletionUpdate.php index f579a1f12f..52e996a047 100644 --- a/includes/deferred/LinksDeletionUpdate.php +++ b/includes/deferred/LinksDeletionUpdate.php @@ -91,7 +91,7 @@ class LinksDeletionUpdate extends DataUpdate implements EnqueueableDataUpdate { $this->page->updateCategoryCounts( [], $catBatch, $id ); if ( count( $catBatches ) > 1 ) { $lbFactory->commitAndWaitForReplication( - __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ] + __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ] ); } } @@ -187,7 +187,7 @@ class LinksDeletionUpdate extends DataUpdate implements EnqueueableDataUpdate { $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__ ); if ( count( $rcIdBatches ) > 1 ) { $lbFactory->commitAndWaitForReplication( - __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ] + __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ] ); } } @@ -209,7 +209,7 @@ class LinksDeletionUpdate extends DataUpdate implements EnqueueableDataUpdate { if ( count( $pkDeleteConds ) >= $bSize ) { $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ ); $lbFactory->commitAndWaitForReplication( - __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ] + __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ] ); $pkDeleteConds = []; } @@ -230,7 +230,7 @@ class LinksDeletionUpdate extends DataUpdate implements EnqueueableDataUpdate { public function getAsJobSpecification() { return [ - 'wiki' => $this->getDB()->getWikiID(), + 'wiki' => WikiMap::getWikiIdFromDomain( $this->getDB()->getDomainID() ), 'job' => new JobSpecification( 'deleteLinks', [ 'pageId' => $this->pageId, 'timestamp' => $this->timestamp ], diff --git a/includes/deferred/LinksUpdate.php b/includes/deferred/LinksUpdate.php index 33d4e70826..dfe89ba3fa 100644 --- a/includes/deferred/LinksUpdate.php +++ b/includes/deferred/LinksUpdate.php @@ -366,20 +366,22 @@ class LinksUpdate extends DataUpdate implements EnqueueableDataUpdate { return; } - $wikiId = $this->getDB()->getWikiID(); + $domainId = $this->getDB()->getDomainID(); $wp = WikiPage::factory( $this->mTitle ); $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); // T163801: try to release any row locks to reduce contention - $lbf->commitAndWaitForReplication( __METHOD__, $this->ticket, [ 'wiki' => $wikiId ] ); + $lbf->commitAndWaitForReplication( __METHOD__, $this->ticket, [ 'domain' => $domainId ] ); foreach ( array_chunk( array_keys( $added ), $wgUpdateRowsPerQuery ) as $addBatch ) { $wp->updateCategoryCounts( $addBatch, [], $this->mId ); - $lbf->commitAndWaitForReplication( __METHOD__, $this->ticket, [ 'wiki' => $wikiId ] ); + $lbf->commitAndWaitForReplication( + __METHOD__, $this->ticket, [ 'domain' => $domainId ] ); } foreach ( array_chunk( array_keys( $deleted ), $wgUpdateRowsPerQuery ) as $deleteBatch ) { $wp->updateCategoryCounts( [], $deleteBatch, $this->mId ); - $lbf->commitAndWaitForReplication( __METHOD__, $this->ticket, [ 'wiki' => $wikiId ] ); + $lbf->commitAndWaitForReplication( + __METHOD__, $this->ticket, [ 'domain' => $domainId ] ); } } @@ -450,10 +452,12 @@ class LinksUpdate extends DataUpdate implements EnqueueableDataUpdate { } } + $domainId = $this->getDB()->getDomainID(); + foreach ( $deleteWheres as $deleteWhere ) { $this->getDB()->delete( $table, $deleteWhere, __METHOD__ ); $lbf->commitAndWaitForReplication( - __METHOD__, $this->ticket, [ 'wiki' => $this->getDB()->getWikiID() ] + __METHOD__, $this->ticket, [ 'domain' => $domainId ] ); } @@ -461,7 +465,7 @@ class LinksUpdate extends DataUpdate implements EnqueueableDataUpdate { foreach ( $insertBatches as $insertBatch ) { $this->getDB()->insert( $table, $insertBatch, __METHOD__, 'IGNORE' ); $lbf->commitAndWaitForReplication( - __METHOD__, $this->ticket, [ 'wiki' => $this->getDB()->getWikiID() ] + __METHOD__, $this->ticket, [ 'domain' => $domainId ] ); } @@ -1143,7 +1147,7 @@ class LinksUpdate extends DataUpdate implements EnqueueableDataUpdate { } return [ - 'wiki' => $this->getDB()->getWikiID(), + 'wiki' => WikiMap::getWikiIdFromDomain( $this->getDB()->getDomainID() ), 'job' => new JobSpecification( 'refreshLinksPrioritized', [ diff --git a/includes/deferred/WANCacheReapUpdate.php b/includes/deferred/WANCacheReapUpdate.php index 2e5298cc97..5ffc938879 100644 --- a/includes/deferred/WANCacheReapUpdate.php +++ b/includes/deferred/WANCacheReapUpdate.php @@ -43,7 +43,7 @@ class WANCacheReapUpdate implements DeferrableUpdate { [ $this, 'getTitleChangeEvents' ], [ $this, 'getEventAffectedKeys' ], [ - 'channel' => 'table:recentchanges:' . $this->db->getWikiID(), + 'channel' => 'table:recentchanges:' . $this->db->getDomainID(), 'logger' => $this->logger ] ); diff --git a/includes/libs/rdbms/database/IDatabase.php b/includes/libs/rdbms/database/IDatabase.php index 78889e52e0..5d0e03fc24 100644 --- a/includes/libs/rdbms/database/IDatabase.php +++ b/includes/libs/rdbms/database/IDatabase.php @@ -346,6 +346,7 @@ interface IDatabase { * Alias for getDomainID() * * @return string + * @deprecated 1.30 */ public function getWikiID(); diff --git a/includes/resourceloader/ResourceLoaderWikiModule.php b/includes/resourceloader/ResourceLoaderWikiModule.php index fd31eac778..bebc1887dc 100644 --- a/includes/resourceloader/ResourceLoaderWikiModule.php +++ b/includes/resourceloader/ResourceLoaderWikiModule.php @@ -373,7 +373,7 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule { if ( $module instanceof self ) { $mDB = $module->getDB(); // Subclasses may disable getDB and implement getTitleInfo differently - if ( $mDB && $mDB->getWikiID() === $db->getWikiID() ) { + if ( $mDB && $mDB->getDomainID() === $db->getDomainID() ) { $wikiModules[] = $module; $allPages += $module->getPages( $context ); } @@ -395,14 +395,17 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule { $cache = ObjectCache::getMainWANInstance(); $allInfo = $cache->getWithSetCallback( - $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getWikiID(), $hash ), + $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getDomainID(), $hash ), $cache::TTL_HOUR, function ( $curVal, &$ttl, array &$setOpts ) use ( $func, $pageNames, $db, $fname ) { $setOpts += Database::getCacheSetOptions( $db ); return call_user_func( $func, $db, $pageNames, $fname ); }, - [ 'checkKeys' => [ $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getWikiID() ) ] ] + [ + 'checkKeys' => [ + $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getDomainID() ) ] + ] ); foreach ( $wikiModules as $wikiModule ) { diff --git a/includes/user/UserRightsProxy.php b/includes/user/UserRightsProxy.php index 98586e76c7..3c2731a790 100644 --- a/includes/user/UserRightsProxy.php +++ b/includes/user/UserRightsProxy.php @@ -277,11 +277,11 @@ class UserRightsProxy { __METHOD__ ); - $wikiId = $this->db->getWikiID(); + $domainId = $this->db->getDomainID(); $userId = $this->id; $this->db->onTransactionPreCommitOrIdle( - function () use ( $wikiId, $userId ) { - User::purge( $wikiId, $userId ); + function () use ( $domainId, $userId ) { + User::purge( $domainId, $userId ); }, __METHOD__ ); diff --git a/maintenance/populateContentModel.php b/maintenance/populateContentModel.php index 7d83180eed..74a918ae12 100644 --- a/maintenance/populateContentModel.php +++ b/maintenance/populateContentModel.php @@ -24,6 +24,7 @@ require_once __DIR__ . '/Maintenance.php'; use Wikimedia\Rdbms\IDatabase; +use MediaWiki\MediaWikiServices; /** * Usage: @@ -45,9 +46,8 @@ class PopulateContentModel extends Maintenance { public function execute() { $dbw = $this->getDB( DB_MASTER ); - $this->wikiId = $dbw->getWikiID(); - - $this->wanCache = ObjectCache::getMainWANInstance(); + $this->wikiId = $dbw->getDomainID(); + $this->wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache(); $ns = $this->getOption( 'ns' ); if ( !ctype_digit( $ns ) && $ns !== 'all' ) { diff --git a/tests/phpunit/includes/WikiMapTest.php b/tests/phpunit/includes/WikiMapTest.php index 186ffdbcc3..53e0b10b88 100644 --- a/tests/phpunit/includes/WikiMapTest.php +++ b/tests/phpunit/includes/WikiMapTest.php @@ -1,4 +1,5 @@ assertEquals( $wiki, WikiMap::getWikiFromUrl( $url ) ); } + + /** + * @dataProvider provideGetWikiIdFromDomain + * @covers WikiMap::getWikiIdFromDomain() + */ + public function testGetWikiIdFromDomain( $domain, $wikiId ) { + $this->assertEquals( $wikiId, WikiMap::getWikiIdFromDomain( $domain ) ); + } + + public function provideGetWikiIdFromDomain() { + return [ + [ 'db-prefix', 'db-prefix' ], + [ wfWikiID(), wfWikiID() ], + [ new DatabaseDomain( 'db-dash', null, 'prefix' ), 'db-dash-prefix' ] + ]; + } } -- 2.20.1