From 152db8a06f857f29ce5fc5818f4e1e6a8a5d89f2 Mon Sep 17 00:00:00 2001 From: Sergio Gimeno Date: Tue, 13 Feb 2024 19:55:34 +0100 Subject: [PATCH] Replace deprecated usage of wfGetDB Bug: T330641 Change-Id: I863dc67e3196ea20c4f6145c5d3bb68b445b5195 --- ServiceWiring.php | 4 +++- includes/Cache/RevisionLocalCache.php | 13 ++++++++++++- includes/DbFactory.php | 2 +- includes/UserLocator.php | 4 ++-- maintenance/backfillUnreadWikis.php | 2 +- maintenance/recomputeNotifCounts.php | 5 +++-- tests/phpunit/UserLocatorTest.php | 3 ++- 7 files changed, 24 insertions(+), 9 deletions(-) diff --git a/ServiceWiring.php b/ServiceWiring.php index 8d543aee4..098ff7806 100644 --- a/ServiceWiring.php +++ b/ServiceWiring.php @@ -95,7 +95,9 @@ return [ }, 'EchoRevisionLocalCache' => static function ( MediaWikiServices $services ): RevisionLocalCache { - return new RevisionLocalCache(); + return new RevisionLocalCache( + $services->getConnectionProvider() + ); } ]; diff --git a/includes/Cache/RevisionLocalCache.php b/includes/Cache/RevisionLocalCache.php index c0d9bd599..c9b28e0a7 100644 --- a/includes/Cache/RevisionLocalCache.php +++ b/includes/Cache/RevisionLocalCache.php @@ -3,18 +3,29 @@ namespace MediaWiki\Extension\Notifications\Cache; use MediaWiki\MediaWikiServices; +use Wikimedia\Rdbms\IConnectionProvider; /** * Cache class that maps revision id to RevisionStore object * @xxx Like TitleLocalCache, this class shouldn't need to exist. */ class RevisionLocalCache extends LocalCache { + private IConnectionProvider $dbProvider; + + /** + * @param IConnectionProvider $dbProvider + */ + public function __construct( IConnectionProvider $dbProvider ) { + parent::__construct(); + $this->dbProvider = $dbProvider; + } + /** * @inheritDoc */ protected function resolve( array $lookups ) { $store = MediaWikiServices::getInstance()->getRevisionStore(); - $dbr = wfGetDB( DB_REPLICA ); + $dbr = $this->dbProvider->getReplicaDatabase(); $revQuery = $store->getQueryInfo( [ 'page', 'user' ] ); $res = $dbr->select( $revQuery['tables'], diff --git a/includes/DbFactory.php b/includes/DbFactory.php index 3610ac201..3d3555c42 100644 --- a/includes/DbFactory.php +++ b/includes/DbFactory.php @@ -108,7 +108,7 @@ class DbFactory { } /** - * Wrapper function for wfGetDB, some extensions like MobileFrontend is + * Wrapper function for LBFactory::getExternalLB/getMainLB, some extensions like MobileFrontend is * using this to issue sql queries against Echo database directly. This * is totally not accepted and should be updated to use Echo database access * objects diff --git a/includes/UserLocator.php b/includes/UserLocator.php index f13ef4d56..77db6aeac 100644 --- a/includes/UserLocator.php +++ b/includes/UserLocator.php @@ -26,9 +26,9 @@ class UserLocator { if ( !$title ) { return []; } - + $provider = MediaWikiServices::getInstance()->getConnectionProvider(); $batchRowIt = new BatchRowIterator( - wfGetDB( DB_REPLICA, 'watchlist' ), + $provider->getReplicaDatabase( false, 'watchlist' ), /* $table = */ 'watchlist', /* $primaryKeys = */ [ 'wl_user' ], $batchSize diff --git a/maintenance/backfillUnreadWikis.php b/maintenance/backfillUnreadWikis.php index 34ec6903d..b32764c85 100644 --- a/maintenance/backfillUnreadWikis.php +++ b/maintenance/backfillUnreadWikis.php @@ -40,7 +40,7 @@ class BackfillUnreadWikis extends Maintenance { } else { $userQuery = User::getQueryInfo(); $iterator = new BatchRowIterator( - wfGetDB( DB_REPLICA ), $userQuery['tables'], 'user_id', $this->getBatchSize() + $this->getReplicaDB(), $userQuery['tables'], 'user_id', $this->getBatchSize() ); $iterator->setFetchColumns( $userQuery['fields'] ); $iterator->addJoinConditions( $userQuery['joins'] ); diff --git a/maintenance/recomputeNotifCounts.php b/maintenance/recomputeNotifCounts.php index 4c1d066f7..eaaee0a80 100644 --- a/maintenance/recomputeNotifCounts.php +++ b/maintenance/recomputeNotifCounts.php @@ -38,7 +38,6 @@ class RecomputeNotifCounts extends Maintenance { public function execute() { $dbFactory = DbFactory::newFromDefault(); $dbrEcho = $dbFactory->getEchoDb( DB_REPLICA ); - $dbr = wfGetDB( DB_REPLICA ); $userIDs = $this->getOption( 'user-ids' ); $userIDs = $userIDs ? explode( ',', $userIDs ) : null; @@ -67,7 +66,9 @@ class RecomputeNotifCounts extends Maintenance { $userIterator->setCaller( __METHOD__ ); } else { $userQuery = User::getQueryInfo(); - $userIterator = new BatchRowIterator( $dbr, $userQuery['tables'], 'user_id', $this->getBatchSize() ); + $userIterator = new BatchRowIterator( + $this->getReplicaDB(), $userQuery['tables'], 'user_id', $this->getBatchSize() + ); $userIterator->setFetchColumns( $userQuery['fields'] ); $userIterator->addJoinConditions( $userQuery['joins'] ); $userIterator->setCaller( __METHOD__ ); diff --git a/tests/phpunit/UserLocatorTest.php b/tests/phpunit/UserLocatorTest.php index 526431718..52276c810 100644 --- a/tests/phpunit/UserLocatorTest.php +++ b/tests/phpunit/UserLocatorTest.php @@ -27,7 +27,8 @@ class UserLocatorTest extends MediaWikiIntegrationTestCase { 'wl_title' => $key ]; } - wfGetDB( DB_PRIMARY )->insert( 'watchlist', $rows, __METHOD__ ); + + $this->getDb()->insert( 'watchlist', $rows, __METHOD__ ); $event = $this->createMock( Event::class ); $event->method( 'getTitle' )