Skip to content

Commit

Permalink
Migrate to use ConnectionProvider (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
Universal-Omega authored Nov 20, 2024
1 parent 3906658 commit 789ca76
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 34 deletions.
4 changes: 2 additions & 2 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"class": "Miraheze\\MirahezeMagic\\Specials\\SpecialMirahezeSurvey",
"services": [
"ConfigFactory",
"DBLoadBalancerFactory",
"ConnectionProvider",
"UserOptionsLookup"
]
},
Expand Down Expand Up @@ -144,7 +144,7 @@
"services": [
"MainConfig",
"CommentStore",
"DBLoadBalancerFactory",
"ConnectionProvider",
"HttpRequestFactory"
]
},
Expand Down
42 changes: 20 additions & 22 deletions includes/HookHandlers/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
use Throwable;
use Wikimedia\IPUtils;
use Wikimedia\Rdbms\DBConnRef;
use Wikimedia\Rdbms\ILBFactory;
use Wikimedia\Rdbms\IConnectionProvider;

class Main implements
AbuseFilterShouldFilterActionHook,
Expand Down Expand Up @@ -78,42 +78,42 @@ class Main implements
/** @var CommentStore */
private $commentStore;

/** @var ILBFactory */
private $dbLoadBalancerFactory;
/** @var IConnectionProvider */
private $connectionProvider;

/** @var HttpRequestFactory */
private $httpRequestFactory;

/**
* @param ServiceOptions $options
* @param CommentStore $commentStore
* @param ILBFactory $dbLoadBalancerFactory
* @param IConnectionProvider $connectionProvider
* @param HttpRequestFactory $httpRequestFactory
*/
public function __construct(
ServiceOptions $options,
CommentStore $commentStore,
ILBFactory $dbLoadBalancerFactory,
IConnectionProvider $connectionProvider,
HttpRequestFactory $httpRequestFactory
) {
$this->options = $options;
$this->commentStore = $commentStore;
$this->dbLoadBalancerFactory = $dbLoadBalancerFactory;
$this->connectionProvider = $connectionProvider;
$this->httpRequestFactory = $httpRequestFactory;
}

/**
* @param Config $mainConfig
* @param CommentStore $commentStore
* @param ILBFactory $dbLoadBalancerFactory
* @param IConnectionProvider $connectionProvider
* @param HttpRequestFactory $httpRequestFactory
*
* @return self
*/
public static function factory(
Config $mainConfig,
CommentStore $commentStore,
ILBFactory $dbLoadBalancerFactory,
IConnectionProvider $connectionProvider,
HttpRequestFactory $httpRequestFactory
): self {
return new self(
Expand All @@ -135,7 +135,7 @@ public static function factory(
$mainConfig
),
$commentStore,
$dbLoadBalancerFactory,
$connectionProvider,
$httpRequestFactory
);
}
Expand Down Expand Up @@ -172,17 +172,16 @@ public function onAbuseFilterShouldFilterAction(
public function onCreateWikiDeletion( DBConnRef $cwdb, string $dbname ): void {
global $wmgSwiftPassword, $wgGlobalUsageDatabase;

$echoSharedTrackingDB = $this->options->get( 'EchoSharedTrackingDB' );
$dbw = $this->dbLoadBalancerFactory->getMainLB(
$echoSharedTrackingDB
)->getMaintenanceConnectionRef( DB_PRIMARY, [], $echoSharedTrackingDB );
$dbw = $this->connectionProvider->getPrimaryDatabase(
$this->options->get( 'EchoSharedTrackingDB' )
);

$dbw->delete( 'echo_unread_wikis', [ 'euw_wiki' => $dbname ] );

if ( $wgGlobalUsageDatabase ) {
$gudDb = $this->dbLoadBalancerFactory->getMainLB(
$gudDb = $this->connectionProvider->getPrimaryDatabase(
$wgGlobalUsageDatabase
)->getMaintenanceConnectionRef( DB_PRIMARY, [], $wgGlobalUsageDatabase );
);

$gudDb->delete( 'globalimagelinks', [ 'gil_wiki' => $dbname ] );
}
Expand Down Expand Up @@ -248,17 +247,16 @@ public function onCreateWikiRename(
): void {
global $wmgSwiftPassword, $wgGlobalUsageDatabase;

$echoSharedTrackingDB = $this->options->get( 'EchoSharedTrackingDB' );
$dbw = $this->dbLoadBalancerFactory->getMainLB(
$echoSharedTrackingDB
)->getMaintenanceConnectionRef( DB_PRIMARY, [], $echoSharedTrackingDB );
$dbw = $this->connectionProvider->getPrimaryDatabase(
$this->options->get( 'EchoSharedTrackingDB' )
);

$dbw->update( 'echo_unread_wikis', [ 'euw_wiki' => $newDbName ], [ 'euw_wiki' => $oldDbName ] );

if ( $wgGlobalUsageDatabase ) {
$gudDb = $this->dbLoadBalancerFactory->getMainLB(
$gudDb = $this->connectionProvider->getPrimaryDatabase(
$wgGlobalUsageDatabase
)->getMaintenanceConnectionRef( DB_PRIMARY, [], $wgGlobalUsageDatabase );
);

$gudDb->update( 'globalimagelinks', [ 'gil_wiki' => $newDbName ], [ 'gil_wiki' => $oldDbName ] );
}
Expand Down Expand Up @@ -464,7 +462,7 @@ public function onImportDumpJobAfterImport( $filePath, $importDumpRequestManager
public function onImportDumpJobGetFile( &$filePath, $importDumpRequestManager ): void {
global $wmgSwiftPassword;

$dbr = $this->dbLoadBalancerFactory->getReplicaDatabase( 'virtual-importdump' );
$dbr = $this->connectionProvider->getReplicaDatabase( 'virtual-importdump' );

$container = $dbr->getDomainID() === 'metawikibeta' ?
'miraheze-metawikibeta-local-public' :
Expand Down
17 changes: 8 additions & 9 deletions includes/Specials/SpecialMirahezeSurvey.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,34 @@
use MediaWiki\SpecialPage\FormSpecialPage;
use MediaWiki\User\Options\UserOptionsLookup;
use stdClass;
use Wikimedia\Rdbms\DBConnRef;
use Wikimedia\Rdbms\ILBFactory;
use Wikimedia\Rdbms\IConnectionProvider;
use Wikimedia\Rdbms\IDatabase;

class SpecialMirahezeSurvey extends FormSpecialPage {

/** @var Config */
private $config;

/** @var ILBFactory */
private $dbLoadBalancerFactory;
/** @var IConnectionProvider */
private $connectionProvider;

/** @var UserOptionsLookup */
private $userOptionsLookup;

/** @var DBConnRef */
/** @var IDatabase */
private $dbw;

/** @var stdClass|bool */
private $row;

public function __construct(
ConfigFactory $configFactory,
ILBFactory $dbLoadBalancerFactory,
IConnectionProvider $connectionProvider,
UserOptionsLookup $userOptionsLookup
) {
parent::__construct( 'MirahezeSurvey' );

$this->dbLoadBalancerFactory = $dbLoadBalancerFactory;
$this->connectionProvider = $connectionProvider;
$this->userOptionsLookup = $userOptionsLookup;

$this->config = $configFactory->makeConfig( 'MirahezeMagic' );
Expand All @@ -51,8 +51,7 @@ public function execute( $par ) {
return $out->addHTML( Html::errorBox( $this->msg( 'miraheze-survey-disabled' )->parse() ) );
}

$this->dbw = $this->dbLoadBalancerFactory->getMainLB( 'survey' )
->getConnection( DB_PRIMARY, [], 'survey' );
$this->dbw = $this->connectionProvider->getPrimaryDatabase( 'survey' );

$this->row = $this->dbw->selectRow(
'survey',
Expand Down
1 change: 0 additions & 1 deletion maintenance/checkWikiDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ private function checkGlobalTableEntriesWithoutDatabase( array $wikiDatabases ):
}

private function dropDatabases( array $databases, array $clusters ): void {
$dbLoadBalancerFactory = $this->getServiceContainer()->getDBLoadBalancerFactory();
$this->output( "Dropping the following databases:\n" );
foreach ( $databases as $dbName => $cluster ) {
$this->output( " - Dropping $dbName...\n" );
Expand Down

0 comments on commit 789ca76

Please sign in to comment.