Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / db / loadbalancer / LBFactorySingle.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 /**
25 * An LBFactory class that always returns a single database object.
26 */
27 class LBFactorySingle extends LBFactory {
28 /** @var LoadBalancerSingle */
29 private $lb;
30
31 /**
32 * @param array $conf An associative array with one member:
33 * - connection: The DatabaseBase connection object
34 */
35 public function __construct( array $conf ) {
36 parent::__construct( $conf );
37
38 $this->lb = new LoadBalancerSingle( [
39 'readOnlyReason' => $this->readOnlyReason,
40 'trxProfiler' => $this->trxProfiler
41 ] + $conf );
42 }
43
44 /**
45 * @param bool|string $wiki
46 * @return LoadBalancerSingle
47 */
48 public function newMainLB( $wiki = false ) {
49 return $this->lb;
50 }
51
52 /**
53 * @param bool|string $wiki
54 * @return LoadBalancerSingle
55 */
56 public function getMainLB( $wiki = false ) {
57 return $this->lb;
58 }
59
60 /**
61 * @param string $cluster External storage cluster, or false for core
62 * @param bool|string $wiki Wiki ID, or false for the current wiki
63 * @return LoadBalancerSingle
64 */
65 protected function newExternalLB( $cluster, $wiki = false ) {
66 return $this->lb;
67 }
68
69 /**
70 * @param string $cluster External storage cluster, or false for core
71 * @param bool|string $wiki Wiki ID, or false for the current wiki
72 * @return LoadBalancerSingle
73 */
74 public function &getExternalLB( $cluster, $wiki = false ) {
75 return $this->lb;
76 }
77
78 /**
79 * @param string|callable $callback
80 * @param array $params
81 */
82 public function forEachLB( $callback, array $params = [] ) {
83 call_user_func_array( $callback, array_merge( [ $this->lb ], $params ) );
84 }
85 }
86
87 /**
88 * Helper class for LBFactorySingle.
89 */
90 class LoadBalancerSingle extends LoadBalancer {
91 /** @var DatabaseBase */
92 private $db;
93
94 /**
95 * @param array $params
96 */
97 public function __construct( array $params ) {
98 $this->db = $params['connection'];
99
100 parent::__construct( [
101 'servers' => [
102 [
103 'type' => $this->db->getType(),
104 'host' => $this->db->getServer(),
105 'dbname' => $this->db->getDBname(),
106 'load' => 1,
107 ]
108 ],
109 'trxProfiler' => $this->trxProfiler
110 ] );
111
112 if ( isset( $params['readOnlyReason'] ) ) {
113 $this->db->setLBInfo( 'readOnlyReason', $params['readOnlyReason'] );
114 }
115 }
116
117 /**
118 *
119 * @param string $server
120 * @param bool $dbNameOverride
121 *
122 * @return DatabaseBase
123 */
124 protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
125 return $this->db;
126 }
127 }