Merge "mw.Feedback: If the message is posted remotely, link the title correctly"
[lhc/web/wiklou.git] / includes / libs / rdbms / connectionmanager / ConnectionManager.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Database
20 */
21
22 namespace Wikimedia\Rdbms;
23
24 use InvalidArgumentException;
25
26 /**
27 * Database connection manager.
28 *
29 * This manages access to master and replica databases.
30 *
31 * @since 1.29
32 *
33 * @author Addshore
34 */
35 class ConnectionManager {
36
37 /**
38 * @var LoadBalancer
39 */
40 private $loadBalancer;
41
42 /**
43 * The symbolic name of the target database, or false for the local wiki's database.
44 *
45 * @var string|false
46 */
47 private $domain;
48
49 /**
50 * @var string[]
51 */
52 private $groups = [];
53
54 /**
55 * @param LoadBalancer $loadBalancer
56 * @param string|bool $domain Optional logical DB name, defaults to current wiki.
57 * This follows the convention for database names used by $loadBalancer.
58 * @param string[] $groups see LoadBalancer::getConnection
59 *
60 * @throws InvalidArgumentException
61 */
62 public function __construct( LoadBalancer $loadBalancer, $domain = false, array $groups = [] ) {
63 if ( !is_string( $domain ) && $domain !== false ) {
64 throw new InvalidArgumentException( '$dbName must be a string, or false.' );
65 }
66
67 $this->loadBalancer = $loadBalancer;
68 $this->domain = $domain;
69 $this->groups = $groups;
70 }
71
72 /**
73 * @param int $i
74 * @param string[]|null $groups
75 *
76 * @return Database
77 */
78 private function getConnection( $i, array $groups = null ) {
79 $groups = $groups === null ? $this->groups : $groups;
80 return $this->loadBalancer->getConnection( $i, $groups, $this->domain );
81 }
82
83 /**
84 * @param int $i
85 * @param string[]|null $groups
86 *
87 * @return DBConnRef
88 */
89 private function getConnectionRef( $i, array $groups = null ) {
90 $groups = $groups === null ? $this->groups : $groups;
91 return $this->loadBalancer->getConnectionRef( $i, $groups, $this->domain );
92 }
93
94 /**
95 * Returns a connection to the master DB, for updating. The connection should later be released
96 * by calling releaseConnection().
97 *
98 * @since 1.29
99 *
100 * @return Database
101 */
102 public function getWriteConnection() {
103 return $this->getConnection( DB_MASTER );
104 }
105
106 /**
107 * Returns a database connection for reading. The connection should later be released by
108 * calling releaseConnection().
109 *
110 * @since 1.29
111 *
112 * @param string[]|null $groups
113 *
114 * @return Database
115 */
116 public function getReadConnection( array $groups = null ) {
117 $groups = $groups === null ? $this->groups : $groups;
118 return $this->getConnection( DB_REPLICA, $groups );
119 }
120
121 /**
122 * @since 1.29
123 *
124 * @param IDatabase $db
125 */
126 public function releaseConnection( IDatabase $db ) {
127 $this->loadBalancer->reuseConnection( $db );
128 }
129
130 /**
131 * Returns a connection ref to the master DB, for updating.
132 *
133 * @since 1.29
134 *
135 * @return DBConnRef
136 */
137 public function getWriteConnectionRef() {
138 return $this->getConnectionRef( DB_MASTER );
139 }
140
141 /**
142 * Returns a database connection ref for reading.
143 *
144 * @since 1.29
145 *
146 * @param string[]|null $groups
147 *
148 * @return DBConnRef
149 */
150 public function getReadConnectionRef( array $groups = null ) {
151 $groups = $groups === null ? $this->groups : $groups;
152 return $this->getConnectionRef( DB_REPLICA, $groups );
153 }
154
155 }