diff --git a/includes/SubscriptionStore.php b/includes/SubscriptionStore.php index b7c712215..8b071ea77 100644 --- a/includes/SubscriptionStore.php +++ b/includes/SubscriptionStore.php @@ -11,9 +11,8 @@ use ReadOnlyMode; use stdClass; use TitleValue; use Wikimedia\Rdbms\FakeResultWrapper; -use Wikimedia\Rdbms\IDatabase; use Wikimedia\Rdbms\ILBFactory; -use Wikimedia\Rdbms\ILoadBalancer; +use Wikimedia\Rdbms\IReadableDatabase; use Wikimedia\Rdbms\IResultWrapper; class SubscriptionStore { @@ -26,7 +25,7 @@ class SubscriptionStore { public const STATE_AUTOSUBSCRIBED = 2; private Config $config; - private ILoadBalancer $loadBalancer; + private ILBFactory $lbFactory; private ReadOnlyMode $readOnlyMode; private UserFactory $userFactory; @@ -37,20 +36,20 @@ class SubscriptionStore { UserFactory $userFactory ) { $this->config = $configFactory->makeConfig( 'discussiontools' ); - $this->loadBalancer = $lbFactory->getMainLB(); + $this->lbFactory = $lbFactory; $this->readOnlyMode = $readOnlyMode; $this->userFactory = $userFactory; } /** - * @param IDatabase $db + * @param IReadableDatabase $db * @param UserIdentity|null $user * @param array|null $itemNames * @param int|int[]|null $state One of (or an array of) SubscriptionStore::STATE_* constants * @return IResultWrapper|false */ private function fetchSubscriptions( - IDatabase $db, + IReadableDatabase $db, ?UserIdentity $user = null, ?array $itemNames = null, $state = null @@ -104,7 +103,11 @@ class SubscriptionStore { } $options += [ 'forWrite' => false ]; - $db = $this->loadBalancer->getConnection( $options['forWrite'] ? DB_PRIMARY : DB_REPLICA ); + if ( $options['forWrite'] ) { + $db = $this->lbFactory->getPrimaryDatabase(); + } else { + $db = $this->lbFactory->getReplicaDatabase(); + } $rows = $this->fetchSubscriptions( $db, @@ -138,7 +141,11 @@ class SubscriptionStore { array $options = [] ): array { $options += [ 'forWrite' => false ]; - $db = $this->loadBalancer->getConnection( $options['forWrite'] ? DB_PRIMARY : DB_REPLICA ); + if ( $options['forWrite'] ) { + $db = $this->lbFactory->getPrimaryDatabase(); + } else { + $db = $this->lbFactory->getReplicaDatabase(); + } $rows = $this->fetchSubscriptions( $db, @@ -203,7 +210,7 @@ class SubscriptionStore { if ( !$user->isRegistered() ) { return false; } - $dbw = $this->loadBalancer->getConnection( DB_PRIMARY ); + $dbw = $this->lbFactory->getPrimaryDatabase(); $dbw->upsert( 'discussiontools_subscription', [ @@ -240,7 +247,7 @@ class SubscriptionStore { if ( !$user->isRegistered() ) { return false; } - $dbw = $this->loadBalancer->getConnection( DB_PRIMARY ); + $dbw = $this->lbFactory->getPrimaryDatabase(); $dbw->update( 'discussiontools_subscription', [ 'sub_state' => static::STATE_UNSUBSCRIBED ], @@ -297,7 +304,7 @@ class SubscriptionStore { if ( $this->readOnlyMode->isReadOnly() ) { return false; } - $dbw = $this->loadBalancer->getConnection( DB_PRIMARY ); + $dbw = $this->lbFactory->getPrimaryDatabase(); $conditions = [ 'sub_item' => $itemName, diff --git a/includes/ThreadItemStore.php b/includes/ThreadItemStore.php index c7d828cc1..89d97040d 100644 --- a/includes/ThreadItemStore.php +++ b/includes/ThreadItemStore.php @@ -20,7 +20,6 @@ use stdClass; use TitleFormatter; use Wikimedia\Rdbms\DBError; use Wikimedia\Rdbms\ILBFactory; -use Wikimedia\Rdbms\ILoadBalancer; use Wikimedia\Rdbms\IResultWrapper; use Wikimedia\Rdbms\SelectQueryBuilder; use Wikimedia\Timestamp\TimestampException; @@ -31,7 +30,7 @@ use Wikimedia\Timestamp\TimestampException; class ThreadItemStore { private Config $config; - private ILoadBalancer $loadBalancer; + private ILBFactory $dbConProvider; private ReadOnlyMode $readOnlyMode; private PageStore $pageStore; private RevisionStore $revStore; @@ -40,7 +39,7 @@ class ThreadItemStore { public function __construct( ConfigFactory $configFactory, - ILBFactory $lbFactory, + ILBFactory $dbConProvider, ReadOnlyMode $readOnlyMode, PageStore $pageStore, RevisionStore $revStore, @@ -48,7 +47,7 @@ class ThreadItemStore { ActorStore $actorStore ) { $this->config = $configFactory->makeConfig( 'discussiontools' ); - $this->loadBalancer = $lbFactory->getMainLB(); + $this->dbConProvider = $dbConProvider; $this->readOnlyMode = $readOnlyMode; $this->pageStore = $pageStore; $this->revStore = $revStore; @@ -195,7 +194,7 @@ class ThreadItemStore { foreach ( $result as $row ) { $revs[ $row->itr_revision_id ] = null; } - $revQueryBuilder = $this->loadBalancer->getConnection( DB_REPLICA )->newSelectQueryBuilder() + $revQueryBuilder = $this->dbConProvider->getReplicaDatabase()->newSelectQueryBuilder() ->queryInfo( $this->revStore->getQueryInfo( [ 'page' ] ) ) ->fields( $this->pageStore->getSelectFields() ) ->where( $revs ? [ 'rev_id' => array_keys( $revs ) ] : '0=1' ); @@ -314,7 +313,7 @@ class ThreadItemStore { * @return SelectQueryBuilder */ private function getIdsNamesBuilder(): SelectQueryBuilder { - $dbr = $this->loadBalancer->getConnection( DB_REPLICA ); + $dbr = $this->dbConProvider->getReplicaDatabase(); $queryBuilder = $dbr->newSelectQueryBuilder() ->from( 'discussiontools_items' ) @@ -344,7 +343,7 @@ class ThreadItemStore { return false; } - $dbw = $this->loadBalancer->getConnection( DB_PRIMARY ); + $dbw = $this->dbConProvider->getPrimaryDatabase(); $didInsert = false; $method = __METHOD__;