From 97e273005bbf516735b0bbd4ba75c961f95f37cb Mon Sep 17 00:00:00 2001 From: CosmicAlpha Date: Wed, 20 Nov 2024 11:44:19 -0700 Subject: [PATCH] Migrate to use DatabaseVirtualDomains (#59) --- extension.json | 17 ++++---- includes/Hooks/Handlers/Installer.php | 8 +++- includes/Hooks/Handlers/Main.php | 25 +++++------- includes/RequestSSLManager.php | 27 +++++-------- includes/RequestSSLQueuePager.php | 15 ++----- includes/ServiceWiring.php | 2 +- includes/Specials/SpecialRequestSSL.php | 33 +++++----------- includes/Specials/SpecialRequestSSLQueue.php | 21 ++++++---- tests/phpunit/RequestSSLManagerTest.php | 41 ++++++++++---------- 9 files changed, 81 insertions(+), 108 deletions(-) diff --git a/extension.json b/extension.json index e8ad5ed..8fa7807 100644 --- a/extension.json +++ b/extension.json @@ -12,7 +12,7 @@ "license-name": "GPL-3.0-or-later", "type": "specialpage", "requires": { - "MediaWiki": ">= 1.39.0" + "MediaWiki": ">= 1.42.0" }, "MessagesDirs": { "RequestSSL": [ @@ -79,7 +79,7 @@ "RequestSSL": { "class": "Miraheze\\RequestSSL\\Specials\\SpecialRequestSSL", "services": [ - "DBLoadBalancerFactory", + "ConnectionProvider", "MimeAnalyzer", "RemoteWikiFactory", "RepoGroup", @@ -89,7 +89,7 @@ "RequestSSLQueue": { "class": "Miraheze\\RequestSSL\\Specials\\SpecialRequestSSLQueue", "services": [ - "DBLoadBalancerFactory", + "ConnectionProvider", "RequestSSLManager", "PermissionManager", "UserFactory" @@ -117,13 +117,12 @@ "Main": { "class": "Miraheze\\RequestSSL\\Hooks\\Handlers\\Main", "services": [ - "ConfigFactory" + "ConnectionProvider" ] } }, "ResourceModules": { "ext.requestssl.oouiform": { - "targets": [ "desktop", "mobile" ], "scripts": "ext.requestssl.oouiform.ooui.js", "dependencies": [ "mediawiki.storage", @@ -131,7 +130,6 @@ ] }, "ext.requestssl.oouiform.styles": { - "targets": [ "desktop", "mobile" ], "styles": "ext.requestssl.oouiform.ooui.less" } }, @@ -140,10 +138,6 @@ "remoteExtPath": "RequestSSL/modules" }, "config": { - "RequestSSLCentralWiki": { - "value": "", - "description": "If set, only allow users to request a SSL certificate on this wiki." - }, "RequestSSLHelpUrl": { "value": "", "description": "Full URL. If set, adds a help URL to Special:RequestSSL." @@ -183,5 +177,8 @@ "ServiceWiringFiles": [ "includes/ServiceWiring.php" ], + "DatabaseVirtualDomains": [ + "virtual-requestssl" + ], "manifest_version": 2 } diff --git a/includes/Hooks/Handlers/Installer.php b/includes/Hooks/Handlers/Installer.php index 00b2c04..832d7ab 100644 --- a/includes/Hooks/Handlers/Installer.php +++ b/includes/Hooks/Handlers/Installer.php @@ -14,6 +14,12 @@ public function onLoadExtensionSchemaUpdates( $updater ) { $dbType = $updater->getDB()->getType(); $dir = __DIR__ . '/../../../sql'; - $updater->addExtensionTable( 'requestssl_requests', "$dir/$dbType/tables-generated.sql" ); + $updater->addExtensionUpdateOnVirtualDomain( [ + 'virtual-requestssl', + 'addTable', + 'requestssl_requests', + "$dir/$dbType/tables-generated.sql", + true, + ] ); } } diff --git a/includes/Hooks/Handlers/Main.php b/includes/Hooks/Handlers/Main.php index e3807f9..0e6f4eb 100644 --- a/includes/Hooks/Handlers/Main.php +++ b/includes/Hooks/Handlers/Main.php @@ -2,8 +2,6 @@ namespace Miraheze\RequestSSL\Hooks\Handlers; -use Config; -use ConfigFactory; use EchoAttributeManager; use MediaWiki\Block\Hook\GetAllBlockActionsHook; use MediaWiki\User\Hook\UserGetReservedNamesHook; @@ -11,20 +9,21 @@ use Miraheze\RequestSSL\Notifications\EchoRequestCommentPresentationModel; use Miraheze\RequestSSL\Notifications\EchoRequestStatusUpdatePresentationModel; use WikiMap; +use Wikimedia\Rdbms\IConnectionProvider; class Main implements GetAllBlockActionsHook, UserGetReservedNamesHook { - /** @var Config */ - private $config; + /** @var IConnectionProvider */ + private $connectionProvider; /** - * @param ConfigFactory $configFactory + * @param IConnectionProvider $connectionProvider */ - public function __construct( ConfigFactory $configFactory ) { - $this->config = $configFactory->makeConfig( 'RequestSSL' ); + public function __construct( IConnectionProvider $connectionProvider ) { + $this->connectionProvider = $connectionProvider; } /** @@ -39,10 +38,8 @@ public function onUserGetReservedNames( &$reservedUsernames ) { * @param array &$actions */ public function onGetAllBlockActions( &$actions ) { - if ( - $this->config->get( 'RequestSSLCentralWiki' ) && - !WikiMap::isCurrentWikiId( $this->config->get( 'RequestSSLCentralWiki' ) ) - ) { + $dbr = $this->connectionProvider->getReplicaDatabase( 'virtual-requestssl' ); + if ( !WikiMap::isCurrentWikiDbDomain( $dbr->getDomainID() ) ) { return; } @@ -55,10 +52,8 @@ public function onGetAllBlockActions( &$actions ) { * @param array &$icons */ public function onBeforeCreateEchoEvent( &$notifications, &$notificationCategories, &$icons ) { - if ( - $this->config->get( 'RequestSSLCentralWiki' ) && - !WikiMap::isCurrentWikiId( $this->config->get( 'RequestSSLCentralWiki' ) ) - ) { + $dbr = $this->connectionProvider->getReplicaDatabase( 'virtual-requestssl' ); + if ( !WikiMap::isCurrentWikiDbDomain( $dbr->getDomainID() ) ) { return; } diff --git a/includes/RequestSSLManager.php b/includes/RequestSSLManager.php index 50e87e8..1e12f0a 100644 --- a/includes/RequestSSLManager.php +++ b/includes/RequestSSLManager.php @@ -18,8 +18,8 @@ use SpecialPage; use stdClass; use User; -use Wikimedia\Rdbms\DBConnRef; -use Wikimedia\Rdbms\ILBFactory; +use Wikimedia\Rdbms\IConnectionProvider; +use Wikimedia\Rdbms\IDatabase; use Wikimedia\Rdbms\SelectQueryBuilder; class RequestSSLManager { @@ -29,14 +29,13 @@ class RequestSSLManager { ]; public const CONSTRUCTOR_OPTIONS = [ - 'RequestSSLCentralWiki', 'RequestSSLScriptCommand', ]; /** @var Config */ private $config; - /** @var DBConnRef */ + /** @var IDatabase */ private $dbw; /** @var int */ @@ -45,8 +44,8 @@ class RequestSSLManager { /** @var ActorStoreFactory */ private $actorStoreFactory; - /** @var ILBFactory */ - private $dbLoadBalancerFactory; + /** @var IConnectionProvider */ + private $connectionProvider; /** @var MessageLocalizer */ private $messageLocalizer; @@ -75,7 +74,7 @@ class RequestSSLManager { /** * @param Config $config * @param ActorStoreFactory $actorStoreFactory - * @param ILBFactory $dbLoadBalancerFactory + * @param IConnectionProvider $connectionProvider * @param LinkRenderer $linkRenderer * @param RemoteWikiFactory $remoteWikiFactory * @param RepoGroup $repoGroup @@ -87,7 +86,7 @@ class RequestSSLManager { public function __construct( Config $config, ActorStoreFactory $actorStoreFactory, - ILBFactory $dbLoadBalancerFactory, + IConnectionProvider $connectionProvider, LinkRenderer $linkRenderer, RemoteWikiFactory $remoteWikiFactory, RepoGroup $repoGroup, @@ -100,7 +99,7 @@ public function __construct( $this->config = $config; $this->actorStoreFactory = $actorStoreFactory; - $this->dbLoadBalancerFactory = $dbLoadBalancerFactory; + $this->connectionProvider = $connectionProvider; $this->linkRenderer = $linkRenderer; $this->messageLocalizer = $messageLocalizer; $this->options = $options; @@ -114,17 +113,9 @@ public function __construct( * @param int $requestID */ public function fromID( int $requestID ) { + $this->dbw = $this->connectionProvider->getPrimaryDatabase( 'virtual-requestssl' ); $this->ID = $requestID; - $centralWiki = $this->options->get( 'RequestSSLCentralWiki' ); - if ( $centralWiki ) { - $this->dbw = $this->dbLoadBalancerFactory->getMainLB( - $centralWiki - )->getConnection( DB_PRIMARY, [], $centralWiki ); - } else { - $this->dbw = $this->dbLoadBalancerFactory->getMainLB()->getConnection( DB_PRIMARY ); - } - $this->row = $this->dbw->newSelectQueryBuilder() ->table( 'requestssl_requests' ) ->field( '*' ) diff --git a/includes/RequestSSLQueuePager.php b/includes/RequestSSLQueuePager.php index b9d288d..dd61679 100644 --- a/includes/RequestSSLQueuePager.php +++ b/includes/RequestSSLQueuePager.php @@ -8,7 +8,7 @@ use MediaWiki\User\UserFactory; use SpecialPage; use TablePager; -use Wikimedia\Rdbms\ILBFactory; +use Wikimedia\Rdbms\IConnectionProvider; class RequestSSLQueuePager extends TablePager { @@ -30,7 +30,7 @@ class RequestSSLQueuePager extends TablePager { /** * @param Config $config * @param IContextSource $context - * @param ILBFactory $dbLoadBalancerFactory + * @param IConnectionProvider $connectionProvider * @param LinkRenderer $linkRenderer * @param UserFactory $userFactory * @param string $requester @@ -40,7 +40,7 @@ class RequestSSLQueuePager extends TablePager { public function __construct( Config $config, IContextSource $context, - ILBFactory $dbLoadBalancerFactory, + IConnectionProvider $connectionProvider, LinkRenderer $linkRenderer, UserFactory $userFactory, string $requester, @@ -49,14 +49,7 @@ public function __construct( ) { parent::__construct( $context, $linkRenderer ); - $centralWiki = $config->get( 'RequestSSLCentralWiki' ); - if ( $centralWiki ) { - $this->mDb = $dbLoadBalancerFactory->getMainLB( - $centralWiki - )->getConnection( DB_REPLICA, [], $centralWiki ); - } else { - $this->mDb = $dbLoadBalancerFactory->getMainLB()->getConnection( DB_REPLICA ); - } + $this->mDb = $connectionProvider->getReplicaDatabase( 'virtual-requestssl' ); $this->linkRenderer = $linkRenderer; $this->userFactory = $userFactory; diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index 1ba8a40..613bb84 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -9,7 +9,7 @@ return new RequestSSLManager( $services->getConfigFactory()->makeConfig( 'RequestSSL' ), $services->getActorStoreFactory(), - $services->getDBLoadBalancerFactory(), + $services->getConnectionProvider(), $services->getLinkRenderer(), $services->get( 'RemoteWikiFactory' ), $services->getRepoGroup(), diff --git a/includes/Specials/SpecialRequestSSL.php b/includes/Specials/SpecialRequestSSL.php index 0a1b501..ac66668 100644 --- a/includes/Specials/SpecialRequestSSL.php +++ b/includes/Specials/SpecialRequestSSL.php @@ -19,12 +19,12 @@ use UserBlockedError; use UserNotLoggedIn; use WikiMap; -use Wikimedia\Rdbms\ILBFactory; +use Wikimedia\Rdbms\IConnectionProvider; class SpecialRequestSSL extends FormSpecialPage { - /** @var ILBFactory */ - private $dbLoadBalancerFactory; + /** @var IConnectionProvider */ + private $connectionProvider; /** @var MimeAnalyzer */ private $mimeAnalyzer; @@ -39,14 +39,14 @@ class SpecialRequestSSL extends FormSpecialPage { private $userFactory; /** - * @param ILBFactory $dbLoadBalancerFactory + * @param IConnectionProvider $connectionProvider * @param MimeAnalyzer $mimeAnalyzer * @param RemoteWikiFactory $remoteWikiFactory * @param RepoGroup $repoGroup * @param UserFactory $userFactory */ public function __construct( - ILBFactory $dbLoadBalancerFactory, + IConnectionProvider $connectionProvider, MimeAnalyzer $mimeAnalyzer, RemoteWikiFactory $remoteWikiFactory, RepoGroup $repoGroup, @@ -54,7 +54,7 @@ public function __construct( ) { parent::__construct( 'RequestSSL', 'request-ssl' ); - $this->dbLoadBalancerFactory = $dbLoadBalancerFactory; + $this->connectionProvider = $connectionProvider; $this->mimeAnalyzer = $mimeAnalyzer; $this->remoteWikiFactory = $remoteWikiFactory; $this->repoGroup = $repoGroup; @@ -68,10 +68,8 @@ public function execute( $par ) { $this->setParameter( $par ); $this->setHeaders(); - if ( - $this->getConfig()->get( 'RequestSSLCentralWiki' ) && - !WikiMap::isCurrentWikiId( $this->getConfig()->get( 'RequestSSLCentralWiki' ) ) - ) { + $dbr = $this->connectionProvider->getReplicaDatabase( 'virtual-requestssl' ); + if ( !WikiMap::isCurrentWikiDbDomain( $dbr->getDomainID() ) ) { throw new ErrorPageError( 'requestssl-notcentral', 'requestssl-notcentral-text' ); } @@ -143,14 +141,7 @@ public function onSubmit( array $data ) { return Status::newFatal( 'sessionfailure' ); } - $centralWiki = $this->getConfig()->get( 'RequestSSLCentralWiki' ); - if ( $centralWiki ) { - $dbw = $this->dbLoadBalancerFactory->getMainLB( - $centralWiki - )->getConnection( DB_PRIMARY, [], $centralWiki ); - } else { - $dbw = $this->dbLoadBalancerFactory->getMainLB()->getConnection( DB_PRIMARY ); - } + $dbw = $this->connectionProvider->getPrimaryDatabase( 'virtual-requestssl' ); $duplicate = $dbw->newSelectQueryBuilder() ->table( 'requestssl_requests' ) @@ -363,12 +354,6 @@ public function checkPermissions() { ) { throw new UserBlockedError( $block ); } - - // @phan-suppress-next-line PhanDeprecatedFunction Only for MW 1.39 or lower. - $globalBlock = $user->getGlobalBlock(); - if ( $globalBlock ) { - throw new UserBlockedError( $globalBlock ); - } } /** diff --git a/includes/Specials/SpecialRequestSSLQueue.php b/includes/Specials/SpecialRequestSSLQueue.php index c8b2bfc..a68ea72 100644 --- a/includes/Specials/SpecialRequestSSLQueue.php +++ b/includes/Specials/SpecialRequestSSLQueue.php @@ -2,6 +2,7 @@ namespace Miraheze\RequestSSL\Specials; +use ErrorPageError; use HTMLForm; use MediaWiki\Permissions\PermissionManager; use MediaWiki\User\UserFactory; @@ -9,12 +10,13 @@ use Miraheze\RequestSSL\RequestSSLQueuePager; use Miraheze\RequestSSL\RequestSSLViewer; use SpecialPage; -use Wikimedia\Rdbms\ILBFactory; +use WikiMap; +use Wikimedia\Rdbms\IConnectionProvider; class SpecialRequestSSLQueue extends SpecialPage { - /** @var ILBFactory */ - private $dbLoadBalancerFactory; + /** @var IConnectionProvider */ + private $connectionProvider; /** @var RequestSSLManager */ private $requestSslRequestManager; @@ -26,20 +28,20 @@ class SpecialRequestSSLQueue extends SpecialPage { private $userFactory; /** - * @param ILBFactory $dbLoadBalancerFactory + * @param IConnectionProvider $connectionProvider * @param RequestSSLManager $requestSslRequestManager * @param PermissionManager $permissionManager * @param UserFactory $userFactory */ public function __construct( - ILBFactory $dbLoadBalancerFactory, + IConnectionProvider $connectionProvider, RequestSSLManager $requestSslRequestManager, PermissionManager $permissionManager, UserFactory $userFactory ) { parent::__construct( 'RequestSSLQueue' ); - $this->dbLoadBalancerFactory = $dbLoadBalancerFactory; + $this->connectionProvider = $connectionProvider; $this->requestSslRequestManager = $requestSslRequestManager; $this->permissionManager = $permissionManager; $this->userFactory = $userFactory; @@ -51,6 +53,11 @@ public function __construct( public function execute( $par ) { $this->setHeaders(); + $dbr = $this->connectionProvider->getReplicaDatabase( 'virtual-requestssl' ); + if ( !WikiMap::isCurrentWikiDbDomain( $dbr->getDomainID() ) ) { + throw new ErrorPageError( 'requestssl-notcentral', 'requestssl-notcentral-text' ); + } + if ( $par ) { $this->lookupRequest( $par ); return; @@ -108,7 +115,7 @@ private function doPagerStuff() { $pager = new RequestSSLQueuePager( $this->getConfig(), $this->getContext(), - $this->dbLoadBalancerFactory, + $this->connectionProvider, $this->getLinkRenderer(), $this->userFactory, $requester, diff --git a/tests/phpunit/RequestSSLManagerTest.php b/tests/phpunit/RequestSSLManagerTest.php index ea408ab..1aad4ce 100644 --- a/tests/phpunit/RequestSSLManagerTest.php +++ b/tests/phpunit/RequestSSLManagerTest.php @@ -2,9 +2,10 @@ namespace Miraheze\RequestSSL\Tests; +use MediaWiki\MainConfigNames; use MediaWikiIntegrationTestCase; use Miraheze\RequestSSL\RequestSSLManager; -use ReflectionClass; +use Wikimedia\TestingAccessWrapper; use Wikimedia\Timestamp\ConvertibleTimestamp; /** @@ -14,28 +15,30 @@ * @coversDefaultClass \Miraheze\RequestSSL\RequestSSLManager */ class RequestSSLManagerTest extends MediaWikiIntegrationTestCase { - protected function setUp(): void { - parent::setUp(); - $this->tablesUsed[] = 'requestssl_requests'; - } + public function addDBDataOnce(): void { + $this->setMwGlobals( MainConfigNames::VirtualDomainsMapping, [ + 'virtual-requestssl' => [ 'db' => 'wikidb' ], + ] ); - public function addDBData() { ConvertibleTimestamp::setFakeTime( ConvertibleTimestamp::now() ); - $this->db->insert( - 'requestssl_requests', - [ + $connectionProvider = $this->getServiceContainer()->getConnectionProvider(); + $dbw = $connectionProvider->getPrimaryDatabase( 'virtual-requestssl' ); + + $dbw->newInsertQueryBuilder() + ->insertInto( 'requestssl_requests' ) + ->ignore() + ->row( [ 'request_customdomain' => 'https://requestssltest.com', 'request_target' => 'requestssltest', 'request_reason' => 'test', 'request_status' => 'pending', 'request_actor' => $this->getTestUser()->getUser()->getActorId(), 'request_timestamp' => $this->db->timestamp(), - ], - __METHOD__, - [ 'IGNORE' ] - ); + ] ) + ->caller( __METHOD__ ) + ->execute(); } private function getRequestSSLManager(): RequestSSLManager { @@ -52,15 +55,11 @@ private function getRequestSSLManager(): RequestSSLManager { * @covers ::fromID */ public function testFromID() { - $manager = $this->getRequestSSLManager(); - - $reflectedClass = new ReflectionClass( $manager ); - $reflection = $reflectedClass->getProperty( 'ID' ); - $reflection->setAccessible( true ); - - $ID = $reflection->getValue( $manager ); + $manager = TestingAccessWrapper::newFromObject( + $this->getRequestSSLManager() + ); - $this->assertSame( 1, $ID ); + $this->assertSame( 1, $manager->ID ); } /**