Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / libs / rdbms / loadbalancer / LoadBalancerSingle.php
blob691d56ed6b30081b630556b982691efd163b6197
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.
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.
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
18 * @file
20 namespace Wikimedia\Rdbms;
22 use InvalidArgumentException;
24 /**
25 * Trivial LoadBalancer that always returns an injected connection handle.
27 * @ingroup Database
29 class LoadBalancerSingle extends LoadBalancer {
30 /** @var Database */
31 private $conn;
33 /**
34 * You probably want to use {@link newFromConnection} instead.
36 * @param array $params An associative array with one member:
37 * - connection: An IDatabase connection object
39 public function __construct( array $params ) {
40 /** @var Database $conn */
41 $conn = $params['connection'] ?? null;
42 if ( !$conn ) {
43 throw new InvalidArgumentException( "Missing 'connection' argument." );
46 $this->conn = $conn;
48 parent::__construct( [
49 'servers' => [ [
50 'type' => $conn->getType(),
51 'host' => $conn->getServer(),
52 'dbname' => $conn->getDBname(),
53 'load' => 1,
54 ] ],
55 'trxProfiler' => $params['trxProfiler'] ?? null,
56 'srvCache' => $params['srvCache'] ?? null,
57 'wanCache' => $params['wanCache'] ?? null,
58 'localDomain' => $params['localDomain'] ?? $this->conn->getDomainID(),
59 'readOnlyReason' => $params['readOnlyReason'] ?? false,
60 'clusterName' => $params['clusterName'] ?? null,
61 ] );
63 if ( isset( $params['readOnlyReason'] ) ) {
64 $conn->setLBInfo( $conn::LB_READ_ONLY_REASON, $params['readOnlyReason'] );
68 /**
69 * @param IDatabase $db Live connection handle
70 * @param array $params Parameter map to LoadBalancerSingle::__constructs()
71 * @return LoadBalancerSingle
72 * @since 1.28
74 public static function newFromConnection( IDatabase $db, array $params = [] ) {
75 return new static( array_merge(
76 [ 'localDomain' => $db->getDomainID() ],
77 $params,
78 [ 'connection' => $db ]
79 ) );
82 protected function sanitizeConnectionFlags( $flags, $domain ) {
83 // There is only one underlying connection handle. Also, this class is only meant to
84 // be used during situations like site installation, where there should be no contenting
85 // connections, and integration testing, where everything uses temporary tables.
86 $flags &= ~self::CONN_TRX_AUTOCOMMIT;
88 return $flags;
91 protected function reallyOpenConnection( $i, DatabaseDomain $domain, array $lbInfo ) {
92 foreach ( $lbInfo as $k => $v ) {
93 $this->conn->setLBInfo( $k, $v );
96 return $this->conn;
99 public function __destruct() {
100 // do nothing since the connection was injected