Debian Codegraph @

extensions/AbuseFilter/includes/CentralDBManager.php

raw symbols SCIP
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 /** @var LBFactory */
13 private $loadBalancerFactory;
14 /** @var string|false */
15 private $dbName;
16 /** @var bool */
17 private $filterIsCentral;
18
19 /**
20 * @param LBFactory $loadBalancerFactory
21 * @param string|false|null $dbName
22 * @param bool $filterIsCentral
23 */
24 public function __construct( LBFactory $loadBalancerFactory, $dbName, bool $filterIsCentral ) {
25 $this->loadBalancerFactory = $loadBalancerFactory;
26 // Use false to agree with LoadBalancer
27 $this->dbName = $dbName ?: false;
28 $this->filterIsCentral = $filterIsCentral;
29 }
30
31 /**
32 * @param int $index DB_PRIMARY/DB_REPLICA
33 * @return IDatabase
34 * @throws DBError
35 * @throws CentralDBNotAvailableException
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 * @return string
49 * @throws CentralDBNotAvailableException
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 * Whether this database is the central one.
60 * @todo Deprecate the config in favour of just checking whether the current DB is the same
61 * as $wgAbuseFilterCentralDB.
62 * @return bool
63 */
64 public function filterIsCentral(): bool {
65 return $this->filterIsCentral;
66 }
67 }