2020-10-16 22:29:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter;
|
|
|
|
|
|
|
|
use Wikimedia\Rdbms\DBError;
|
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
use Wikimedia\Rdbms\LBFactory;
|
|
|
|
|
|
|
|
class CentralDBManager {
|
|
|
|
public const SERVICE_NAME = 'AbuseFilterCentralDBManager';
|
|
|
|
|
|
|
|
/** @var LBFactory */
|
|
|
|
private $loadBalancerFactory;
|
2023-09-04 10:52:35 +00:00
|
|
|
/** @var string|false */
|
2020-10-16 22:29:41 +00:00
|
|
|
private $dbName;
|
2020-09-20 11:30:59 +00:00
|
|
|
/** @var bool */
|
|
|
|
private $filterIsCentral;
|
2020-10-16 22:29:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param LBFactory $loadBalancerFactory
|
2020-09-20 11:30:59 +00:00
|
|
|
* @param string|false|null $dbName
|
|
|
|
* @param bool $filterIsCentral
|
2020-10-16 22:29:41 +00:00
|
|
|
*/
|
2020-09-20 11:30:59 +00:00
|
|
|
public function __construct( LBFactory $loadBalancerFactory, $dbName, bool $filterIsCentral ) {
|
2020-10-16 22:29:41 +00:00
|
|
|
$this->loadBalancerFactory = $loadBalancerFactory;
|
2020-09-20 11:30:59 +00:00
|
|
|
// Use false to agree with LoadBalancer
|
|
|
|
$this->dbName = $dbName ?: false;
|
|
|
|
$this->filterIsCentral = $filterIsCentral;
|
2020-10-16 22:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-12 23:43:37 +00:00
|
|
|
* @param int $index DB_PRIMARY/DB_REPLICA
|
2020-10-16 22:29:41 +00:00
|
|
|
* @return IDatabase
|
|
|
|
* @throws DBError
|
|
|
|
* @throws CentralDBNotAvailableException
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function getConnection( int $index ): IDatabase {
|
2020-10-16 22:29:41 +00:00
|
|
|
if ( !is_string( $this->dbName ) ) {
|
|
|
|
throw new CentralDBNotAvailableException( '$wgAbuseFilterCentralDB is not configured' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->loadBalancerFactory
|
|
|
|
->getMainLB( $this->dbName )
|
2023-08-01 20:57:49 +00:00
|
|
|
->getConnection( $index, [], $this->dbName );
|
2020-10-16 22:29:41 +00:00
|
|
|
}
|
2020-09-20 11:30:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
* @throws CentralDBNotAvailableException
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function getCentralDBName(): string {
|
2020-09-20 11:30:59 +00:00
|
|
|
if ( !is_string( $this->dbName ) ) {
|
|
|
|
throw new CentralDBNotAvailableException( '$wgAbuseFilterCentralDB is not configured' );
|
|
|
|
}
|
|
|
|
return $this->dbName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this database is the central one.
|
|
|
|
* @todo Deprecate the config in favour of just checking whether the current DB is the same
|
|
|
|
* as $wgAbuseFilterCentralDB.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function filterIsCentral(): bool {
|
2020-09-20 11:30:59 +00:00
|
|
|
return $this->filterIsCentral;
|
|
|
|
}
|
2020-10-16 22:29:41 +00:00
|
|
|
}
|