Merge "Handle missing namespace prefix in XML dumps more gracefully"
[lhc/web/wiklou.git] / includes / libs / rdbms / loadbalancer / LoadBalancerSingle.php
1 <?php
2 /**
3 * Simple generator of database connections that always returns the same object.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 namespace Wikimedia\Rdbms;
25
26 use IDatabase;
27 use InvalidArgumentException;
28
29 /**
30 * Trivial LoadBalancer that always returns an injected connection handle
31 */
32 class LoadBalancerSingle extends LoadBalancer {
33 /** @var IDatabase */
34 private $db;
35
36 /**
37 * @param array $params An associative array with one member:
38 * - connection: An IDatabase connection object
39 */
40 public function __construct( array $params ) {
41 if ( !isset( $params['connection'] ) ) {
42 throw new InvalidArgumentException( "Missing 'connection' argument." );
43 }
44
45 $this->db = $params['connection'];
46
47 parent::__construct( [
48 'servers' => [
49 [
50 'type' => $this->db->getType(),
51 'host' => $this->db->getServer(),
52 'dbname' => $this->db->getDBname(),
53 'load' => 1,
54 ]
55 ],
56 'trxProfiler' => isset( $params['trxProfiler'] ) ? $params['trxProfiler'] : null,
57 'srvCache' => isset( $params['srvCache'] ) ? $params['srvCache'] : null,
58 'wanCache' => isset( $params['wanCache'] ) ? $params['wanCache'] : null
59 ] );
60
61 if ( isset( $params['readOnlyReason'] ) ) {
62 $this->db->setLBInfo( 'readOnlyReason', $params['readOnlyReason'] );
63 }
64 }
65
66 /**
67 * @param IDatabase $db Live connection handle
68 * @param array $params Parameter map to LoadBalancerSingle::__constructs()
69 * @return LoadBalancerSingle
70 * @since 1.28
71 */
72 public static function newFromConnection( IDatabase $db, array $params = [] ) {
73 return new static( [ 'connection' => $db ] + $params );
74 }
75
76 protected function reallyOpenConnection( array $server, $dbNameOverride = false ) {
77 return $this->db;
78 }
79 }
80
81 class_alias( 'Wikimedia\Rdbms\LoadBalancerSingle', 'LoadBalancerSingle' );