Skip to content

Commit

Permalink
Migrate to use DatabaseVirtualDomains (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
Universal-Omega authored Nov 20, 2024
1 parent ce7e4e0 commit 97e2730
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 108 deletions.
17 changes: 7 additions & 10 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"license-name": "GPL-3.0-or-later",
"type": "specialpage",
"requires": {
"MediaWiki": ">= 1.39.0"
"MediaWiki": ">= 1.42.0"
},
"MessagesDirs": {
"RequestSSL": [
Expand Down Expand Up @@ -79,7 +79,7 @@
"RequestSSL": {
"class": "Miraheze\\RequestSSL\\Specials\\SpecialRequestSSL",
"services": [
"DBLoadBalancerFactory",
"ConnectionProvider",
"MimeAnalyzer",
"RemoteWikiFactory",
"RepoGroup",
Expand All @@ -89,7 +89,7 @@
"RequestSSLQueue": {
"class": "Miraheze\\RequestSSL\\Specials\\SpecialRequestSSLQueue",
"services": [
"DBLoadBalancerFactory",
"ConnectionProvider",
"RequestSSLManager",
"PermissionManager",
"UserFactory"
Expand Down Expand Up @@ -117,21 +117,19 @@
"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",
"oojs-ui-widgets"
]
},
"ext.requestssl.oouiform.styles": {
"targets": [ "desktop", "mobile" ],
"styles": "ext.requestssl.oouiform.ooui.less"
}
},
Expand All @@ -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."
Expand Down Expand Up @@ -183,5 +177,8 @@
"ServiceWiringFiles": [
"includes/ServiceWiring.php"
],
"DatabaseVirtualDomains": [
"virtual-requestssl"
],
"manifest_version": 2
}
8 changes: 7 additions & 1 deletion includes/Hooks/Handlers/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
] );
}
}
25 changes: 10 additions & 15 deletions includes/Hooks/Handlers/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@

namespace Miraheze\RequestSSL\Hooks\Handlers;

use Config;
use ConfigFactory;
use EchoAttributeManager;
use MediaWiki\Block\Hook\GetAllBlockActionsHook;
use MediaWiki\User\Hook\UserGetReservedNamesHook;
use Miraheze\RequestSSL\Notifications\EchoNewRequestPresentationModel;
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;
}

/**
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
27 changes: 9 additions & 18 deletions includes/RequestSSLManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -29,14 +29,13 @@ class RequestSSLManager {
];

public const CONSTRUCTOR_OPTIONS = [
'RequestSSLCentralWiki',
'RequestSSLScriptCommand',
];

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

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

/** @var int */
Expand All @@ -45,8 +44,8 @@ class RequestSSLManager {
/** @var ActorStoreFactory */
private $actorStoreFactory;

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

/** @var MessageLocalizer */
private $messageLocalizer;
Expand Down Expand Up @@ -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
Expand All @@ -87,7 +86,7 @@ class RequestSSLManager {
public function __construct(
Config $config,
ActorStoreFactory $actorStoreFactory,
ILBFactory $dbLoadBalancerFactory,
IConnectionProvider $connectionProvider,
LinkRenderer $linkRenderer,
RemoteWikiFactory $remoteWikiFactory,
RepoGroup $repoGroup,
Expand All @@ -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;
Expand All @@ -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( '*' )
Expand Down
15 changes: 4 additions & 11 deletions includes/RequestSSLQueuePager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use MediaWiki\User\UserFactory;
use SpecialPage;
use TablePager;
use Wikimedia\Rdbms\ILBFactory;
use Wikimedia\Rdbms\IConnectionProvider;

class RequestSSLQueuePager extends TablePager {

Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion includes/ServiceWiring.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
return new RequestSSLManager(
$services->getConfigFactory()->makeConfig( 'RequestSSL' ),
$services->getActorStoreFactory(),
$services->getDBLoadBalancerFactory(),
$services->getConnectionProvider(),
$services->getLinkRenderer(),
$services->get( 'RemoteWikiFactory' ),
$services->getRepoGroup(),
Expand Down
33 changes: 9 additions & 24 deletions includes/Specials/SpecialRequestSSL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,22 +39,22 @@ 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,
UserFactory $userFactory
) {
parent::__construct( 'RequestSSL', 'request-ssl' );

$this->dbLoadBalancerFactory = $dbLoadBalancerFactory;
$this->connectionProvider = $connectionProvider;
$this->mimeAnalyzer = $mimeAnalyzer;
$this->remoteWikiFactory = $remoteWikiFactory;
$this->repoGroup = $repoGroup;
Expand All @@ -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' );
}

Expand Down Expand Up @@ -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' )
Expand Down Expand Up @@ -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 );
}
}

/**
Expand Down
Loading

0 comments on commit 97e2730

Please sign in to comment.