Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / libs / rdbms / connectionmanager / ConnectionManager.php
blob4f72f77afd1d4871a5ee4a94e06800e078c2105c
1 <?php
3 namespace Wikimedia\Rdbms;
5 use Database;
6 use DBConnRef;
7 use IDatabase;
8 use InvalidArgumentException;
9 use LoadBalancer;
11 /**
12 * Database connection manager.
14 * This manages access to master and replica databases.
16 * @since 1.29
18 * @license GPL-2.0+
19 * @author Addshore
21 class ConnectionManager {
23 /**
24 * @var LoadBalancer
26 private $loadBalancer;
28 /**
29 * The symbolic name of the target database, or false for the local wiki's database.
31 * @var string|false
33 private $domain;
35 /**
36 * @var string[]
38 private $groups = [];
40 /**
41 * @param LoadBalancer $loadBalancer
42 * @param string|bool $domain Optional logical DB name, defaults to current wiki.
43 * This follows the convention for database names used by $loadBalancer.
44 * @param string[] $groups see LoadBalancer::getConnection
46 * @throws InvalidArgumentException
48 public function __construct( LoadBalancer $loadBalancer, $domain = false, array $groups = [] ) {
49 if ( !is_string( $domain ) && $domain !== false ) {
50 throw new InvalidArgumentException( '$dbName must be a string, or false.' );
53 $this->loadBalancer = $loadBalancer;
54 $this->domain = $domain;
55 $this->groups = $groups;
58 /**
59 * @param int $i
60 * @param string[]|null $groups
62 * @return Database
64 private function getConnection( $i, array $groups = null ) {
65 $groups = $groups === null ? $this->groups : $groups;
66 return $this->loadBalancer->getConnection( $i, $groups, $this->domain );
69 /**
70 * @param int $i
71 * @param string[]|null $groups
73 * @return DBConnRef
75 private function getConnectionRef( $i, array $groups = null ) {
76 $groups = $groups === null ? $this->groups : $groups;
77 return $this->loadBalancer->getConnectionRef( $i, $groups, $this->domain );
80 /**
81 * Returns a connection to the master DB, for updating. The connection should later be released
82 * by calling releaseConnection().
84 * @since 1.29
86 * @return Database
88 public function getWriteConnection() {
89 return $this->getConnection( DB_MASTER );
92 /**
93 * Returns a database connection for reading. The connection should later be released by
94 * calling releaseConnection().
96 * @since 1.29
98 * @param string[]|null $groups
100 * @return Database
102 public function getReadConnection( array $groups = null ) {
103 $groups = $groups === null ? $this->groups : $groups;
104 return $this->getConnection( DB_REPLICA, $groups );
108 * @since 1.29
110 * @param IDatabase $db
112 public function releaseConnection( IDatabase $db ) {
113 $this->loadBalancer->reuseConnection( $db );
117 * Returns a connection ref to the master DB, for updating.
119 * @since 1.29
121 * @return DBConnRef
123 public function getWriteConnectionRef() {
124 return $this->getConnectionRef( DB_MASTER );
128 * Returns a database connection ref for reading.
130 * @since 1.29
132 * @param string[]|null $groups
134 * @return DBConnRef
136 public function getReadConnectionRef( array $groups = null ) {
137 $groups = $groups === null ? $this->groups : $groups;
138 return $this->getConnectionRef( DB_REPLICA, $groups );