Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
DbFactory | |
0.00% |
0 / 17 |
|
0.00% |
0 / 6 |
110 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
forcePrimary | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDB | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
getLB | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getWikiDB | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
waitForReplicas | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace Flow; |
4 | |
5 | use MediaWiki\MediaWikiServices; |
6 | use Wikimedia\Rdbms\DBReplicationWaitError; |
7 | use Wikimedia\Rdbms\IDatabase; |
8 | use Wikimedia\Rdbms\IReadableDatabase; |
9 | |
10 | /** |
11 | * All classes within Flow that need to access the Flow db will go through |
12 | * this class. Having it separated into an object greatly simplifies testing |
13 | * anything that needs to talk to the database. |
14 | * |
15 | * The factory receives, in its constructor, the wiki name and cluster name |
16 | * that Flow-specific data is stored on. Multiple wiki's can and should be |
17 | * using the same wiki name and cluster to share Flow-specific data. These values |
18 | * are used. |
19 | * |
20 | * There are also get getWikiLB and getWikiDB for the main wiki database. |
21 | */ |
22 | class DbFactory { |
23 | /** |
24 | * @var string|false Wiki ID, or false for the current wiki |
25 | */ |
26 | protected $wiki; |
27 | |
28 | /** |
29 | * @var string|false External storage cluster, or false for core |
30 | */ |
31 | protected $cluster; |
32 | |
33 | /** |
34 | * @var bool When true only DB_PRIMARY will be returned |
35 | */ |
36 | protected $forcePrimary = false; |
37 | |
38 | /** |
39 | * @param string|false $wiki Wiki ID, or false for the current wiki |
40 | * @param string|false $cluster External storage cluster, or false for core |
41 | */ |
42 | public function __construct( $wiki, $cluster ) { |
43 | $this->wiki = $wiki; |
44 | $this->cluster = $cluster; |
45 | } |
46 | |
47 | public function forcePrimary() { |
48 | $this->forcePrimary = true; |
49 | } |
50 | |
51 | /** |
52 | * Gets a database connection for the Flow-specific database. |
53 | * |
54 | * @param int $db index of the connection to get. DB_PRIMARY|DB_REPLICA. |
55 | * @return IDatabase|IReadableDatabase |
56 | */ |
57 | public function getDB( $db ) { |
58 | return $this->getLB()->getConnection( $this->forcePrimary ? DB_PRIMARY : $db, [], $this->wiki ); |
59 | } |
60 | |
61 | /** |
62 | * Gets a load balancer for the Flow-specific database. |
63 | * |
64 | * @return \Wikimedia\Rdbms\ILoadBalancer |
65 | */ |
66 | private function getLB() { |
67 | $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
68 | if ( $this->cluster !== false ) { |
69 | return $lbFactory->getExternalLB( $this->cluster ); |
70 | } else { |
71 | return $lbFactory->getMainLB( $this->wiki ); |
72 | } |
73 | } |
74 | |
75 | /** |
76 | * Gets a database connection for the main wiki database. Mockable version of wfGetDB. |
77 | * |
78 | * @param int $db index of the connection to get. DB_PRIMARY|DB_REPLICA. |
79 | * @param string|bool $wiki The wiki ID, or false for the current wiki |
80 | * @return IDatabase|IReadableDatabase |
81 | */ |
82 | public function getWikiDB( $db, $wiki = false ) { |
83 | $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
84 | return $lbFactory->getMainLB( $wiki )->getConnection( $this->forcePrimary ? DB_PRIMARY : $db, [], $wiki ); |
85 | } |
86 | |
87 | /** |
88 | * Wait for the replicas of the Flow database |
89 | */ |
90 | public function waitForReplicas() { |
91 | $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
92 | try { |
93 | $lbFactory->waitForReplication( [ |
94 | 'wiki' => $this->wiki, |
95 | 'cluster' => $this->cluster, |
96 | 'ifWritesSince' => false |
97 | ] ); |
98 | } catch ( DBReplicationWaitError $e ) { |
99 | } |
100 | } |
101 | |
102 | } |