Merge "Implement WikiPage::getOldestRevision() in terms of Title::getFirstRevision()"
[lhc/web/wiklou.git] / includes / libs / rdbms / connectionmanager / ConnectionManager.php
1 <?php
2
3 namespace Wikimedia\Rdbms;
4
5 use Database;
6 use InvalidArgumentException;
7
8 /**
9 * Database connection manager.
10 *
11 * This manages access to master and replica databases.
12 *
13 * @since 1.29
14 *
15 * @license GPL-2.0+
16 * @author Addshore
17 */
18 class ConnectionManager {
19
20 /**
21 * @var LoadBalancer
22 */
23 private $loadBalancer;
24
25 /**
26 * The symbolic name of the target database, or false for the local wiki's database.
27 *
28 * @var string|false
29 */
30 private $domain;
31
32 /**
33 * @var string[]
34 */
35 private $groups = [];
36
37 /**
38 * @param LoadBalancer $loadBalancer
39 * @param string|bool $domain Optional logical DB name, defaults to current wiki.
40 * This follows the convention for database names used by $loadBalancer.
41 * @param string[] $groups see LoadBalancer::getConnection
42 *
43 * @throws InvalidArgumentException
44 */
45 public function __construct( LoadBalancer $loadBalancer, $domain = false, array $groups = [] ) {
46 if ( !is_string( $domain ) && $domain !== false ) {
47 throw new InvalidArgumentException( '$dbName must be a string, or false.' );
48 }
49
50 $this->loadBalancer = $loadBalancer;
51 $this->domain = $domain;
52 $this->groups = $groups;
53 }
54
55 /**
56 * @param int $i
57 * @param string[]|null $groups
58 *
59 * @return Database
60 */
61 private function getConnection( $i, array $groups = null ) {
62 $groups = $groups === null ? $this->groups : $groups;
63 return $this->loadBalancer->getConnection( $i, $groups, $this->domain );
64 }
65
66 /**
67 * @param int $i
68 * @param string[]|null $groups
69 *
70 * @return DBConnRef
71 */
72 private function getConnectionRef( $i, array $groups = null ) {
73 $groups = $groups === null ? $this->groups : $groups;
74 return $this->loadBalancer->getConnectionRef( $i, $groups, $this->domain );
75 }
76
77 /**
78 * Returns a connection to the master DB, for updating. The connection should later be released
79 * by calling releaseConnection().
80 *
81 * @since 1.29
82 *
83 * @return Database
84 */
85 public function getWriteConnection() {
86 return $this->getConnection( DB_MASTER );
87 }
88
89 /**
90 * Returns a database connection for reading. The connection should later be released by
91 * calling releaseConnection().
92 *
93 * @since 1.29
94 *
95 * @param string[]|null $groups
96 *
97 * @return Database
98 */
99 public function getReadConnection( array $groups = null ) {
100 $groups = $groups === null ? $this->groups : $groups;
101 return $this->getConnection( DB_REPLICA, $groups );
102 }
103
104 /**
105 * @since 1.29
106 *
107 * @param IDatabase $db
108 */
109 public function releaseConnection( IDatabase $db ) {
110 $this->loadBalancer->reuseConnection( $db );
111 }
112
113 /**
114 * Returns a connection ref to the master DB, for updating.
115 *
116 * @since 1.29
117 *
118 * @return DBConnRef
119 */
120 public function getWriteConnectionRef() {
121 return $this->getConnectionRef( DB_MASTER );
122 }
123
124 /**
125 * Returns a database connection ref for reading.
126 *
127 * @since 1.29
128 *
129 * @param string[]|null $groups
130 *
131 * @return DBConnRef
132 */
133 public function getReadConnectionRef( array $groups = null ) {
134 $groups = $groups === null ? $this->groups : $groups;
135 return $this->getConnectionRef( DB_REPLICA, $groups );
136 }
137
138 }