Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
DbFactory | |
0.00% |
0 / 21 |
|
0.00% |
0 / 8 |
156 | |
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 | |||
getWikiLB | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
waitForReplicas | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
rollbackPrimaryChanges | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 |
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\ILoadBalancer; |
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|bool Wiki ID, or false for the current wiki |
25 | */ |
26 | protected $wiki; |
27 | |
28 | /** |
29 | * @var string|bool 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|bool $wiki Wiki ID, or false for the current wiki |
40 | * @param string|bool $cluster External storage cluster, or false for core |
41 | */ |
42 | public function __construct( $wiki = false, $cluster = false ) { |
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 |
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 | public 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 |
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 | * Gets a load balancer for the main wiki database. |
89 | * |
90 | * @param string|bool $wiki wiki ID, or false for the current wiki |
91 | * @return ILoadBalancer |
92 | */ |
93 | public function getWikiLB( $wiki = false ) { |
94 | $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
95 | return $lbFactory->getMainLB( $wiki ); |
96 | } |
97 | |
98 | /** |
99 | * Wait for the replicas of the Flow database |
100 | */ |
101 | public function waitForReplicas() { |
102 | $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
103 | try { |
104 | $lbFactory->waitForReplication( [ |
105 | 'wiki' => $this->wiki, |
106 | 'cluster' => $this->cluster, |
107 | 'ifWritesSince' => false |
108 | ] ); |
109 | } catch ( DBReplicationWaitError $e ) { |
110 | } |
111 | } |
112 | |
113 | /** |
114 | * Roll back changes on all databases. |
115 | * @see LBFactory::rollbackPrimaryChanges |
116 | * @param string $fname |
117 | */ |
118 | public function rollbackPrimaryChanges( $fname = __METHOD__ ) { |
119 | $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
120 | $lbFactory->rollbackPrimaryChanges( $fname ); |
121 | } |
122 | } |