| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter; |
| 4 |
|
| 5 |
use Wikimedia\Rdbms\DBError; |
| 6 |
use Wikimedia\Rdbms\IDatabase; |
| 7 |
use Wikimedia\Rdbms\LBFactory; |
| 8 |
|
| 9 |
class CentralDBManager { |
| 10 |
public const SERVICE_NAME = 'AbuseFilterCentralDBManager'; |
| 11 |
|
| 12 |
|
| 13 |
private $loadBalancerFactory; |
| 14 |
|
| 15 |
private $dbName; |
| 16 |
|
| 17 |
private $filterIsCentral; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function __construct( LBFactory $loadBalancerFactory, $dbName, bool $filterIsCentral ) { |
| 25 |
$this->loadBalancerFactory = $loadBalancerFactory; |
| 26 |
|
| 27 |
$this->dbName = $dbName ?: false; |
| 28 |
$this->filterIsCentral = $filterIsCentral; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public function getConnection( int $index ): IDatabase { |
| 38 |
if ( !is_string( $this->dbName ) ) { |
| 39 |
throw new CentralDBNotAvailableException( '$wgAbuseFilterCentralDB is not configured' ); |
| 40 |
} |
| 41 |
|
| 42 |
return $this->loadBalancerFactory |
| 43 |
->getMainLB( $this->dbName ) |
| 44 |
->getConnection( $index, [], $this->dbName ); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function getCentralDBName(): string { |
| 52 |
if ( !is_string( $this->dbName ) ) { |
| 53 |
throw new CentralDBNotAvailableException( '$wgAbuseFilterCentralDB is not configured' ); |
| 54 |
} |
| 55 |
return $this->dbName; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function filterIsCentral(): bool { |
| 65 |
return $this->filterIsCentral; |
| 66 |
} |
| 67 |
} |