Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 8 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
LoadBalancer | |
0.00% |
0 / 8 |
|
0.00% |
0 / 2 |
12 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getConnection | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace ContentTranslation; |
4 | |
5 | use MediaWiki\Config\ServiceOptions; |
6 | use Wikimedia\Rdbms\IDatabase; |
7 | use Wikimedia\Rdbms\LBFactory; |
8 | |
9 | /** |
10 | * ContentTranslation Database Connection abstraction |
11 | */ |
12 | class LoadBalancer { |
13 | |
14 | /** @var LBFactory */ |
15 | private $lbFactory; |
16 | |
17 | /** |
18 | * The Database domain ID of the relevant wiki or false for the local wiki |
19 | * @var string|false |
20 | */ |
21 | private $contentTranslationDatabase; |
22 | |
23 | /** |
24 | * The external Database cluster name where the database lives or false if not exists |
25 | * @var string|false |
26 | */ |
27 | private $contentTranslationCluster; |
28 | |
29 | /** |
30 | * @internal For use by ServiceWiring |
31 | */ |
32 | public const CONSTRUCTOR_OPTIONS = [ |
33 | 'ContentTranslationDatabase', |
34 | 'ContentTranslationCluster', |
35 | ]; |
36 | |
37 | public function __construct( LBFactory $lbFactory, ServiceOptions $options ) { |
38 | $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
39 | |
40 | $this->lbFactory = $lbFactory; |
41 | $this->contentTranslationDatabase = $options->get( 'ContentTranslationDatabase' ); |
42 | $this->contentTranslationCluster = $options->get( 'ContentTranslationCluster' ); |
43 | } |
44 | |
45 | /** |
46 | * Gets a database connection to the ContentTranslation database |
47 | * @param int $type Either DB_REPLICA or DB_PRIMARY |
48 | * @return IDatabase |
49 | */ |
50 | public function getConnection( int $type ): IDatabase { |
51 | $lb = $this->contentTranslationCluster |
52 | ? $this->lbFactory->getExternalLB( $this->contentTranslationCluster ) |
53 | : $this->lbFactory->getMainLB( $this->contentTranslationDatabase ); |
54 | |
55 | return $lb->getConnection( $type, [], $this->contentTranslationDatabase ); |
56 | } |
57 | |
58 | } |