Merge "Make database classes handle hyphens in $wgDBname"
[lhc/web/wiklou.git] / includes / db / loadbalancer / LBFactorySimple.php
1 <?php
2 /**
3 * Generator of database load balancing objects.
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 * A simple single-master LBFactory that gets its configuration from the b/c globals
26 */
27 class LBFactorySimple extends LBFactoryMW {
28 /** @var LoadBalancer */
29 private $mainLB;
30 /** @var LoadBalancer[] */
31 private $extLBs = [];
32
33 /** @var string */
34 private $loadMonitorClass;
35
36 public function __construct( array $conf ) {
37 parent::__construct( $conf );
38
39 $this->loadMonitorClass = isset( $conf['loadMonitorClass'] )
40 ? $conf['loadMonitorClass']
41 : null;
42 }
43
44 /**
45 * @param bool|string $wiki
46 * @return LoadBalancer
47 */
48 public function newMainLB( $wiki = false ) {
49 global $wgDBservers, $wgDBprefix, $wgDBmwschema, $wgSQLMode, $wgDBmysql5;
50
51 if ( is_array( $wgDBservers ) ) {
52 $servers = $wgDBservers;
53 foreach ( $servers as $i => &$server ) {
54 if ( $i == 0 ) {
55 $server['master'] = true;
56 } else {
57 $server['replica'] = true;
58 }
59 $server += [
60 'schema' => $wgDBmwschema,
61 'tablePrefix' => $wgDBprefix,
62 'flags' => DBO_DEFAULT,
63 'sqlMode' => $wgSQLMode,
64 'utf8Mode' => $wgDBmysql5
65 ];
66 }
67 } else {
68 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
69 global $wgDBssl, $wgDBcompress;
70
71 $flags = DBO_DEFAULT;
72 if ( $wgDebugDumpSql ) {
73 $flags |= DBO_DEBUG;
74 }
75 if ( $wgDBssl ) {
76 $flags |= DBO_SSL;
77 }
78 if ( $wgDBcompress ) {
79 $flags |= DBO_COMPRESS;
80 }
81
82 $servers = [ [
83 'host' => $wgDBserver,
84 'user' => $wgDBuser,
85 'password' => $wgDBpassword,
86 'dbname' => $wgDBname,
87 'schema' => $wgDBmwschema,
88 'tablePrefix' => $wgDBprefix,
89 'type' => $wgDBtype,
90 'load' => 1,
91 'flags' => $flags,
92 'master' => true,
93 'sqlMode' => $wgSQLMode,
94 'utf8Mode' => $wgDBmysql5
95 ] ];
96 }
97
98 return $this->newLoadBalancer( $servers );
99 }
100
101 /**
102 * @param bool|string $wiki
103 * @return LoadBalancer
104 */
105 public function getMainLB( $wiki = false ) {
106 if ( !isset( $this->mainLB ) ) {
107 $this->mainLB = $this->newMainLB( $wiki );
108 $this->chronProt->initLB( $this->mainLB );
109 }
110
111 return $this->mainLB;
112 }
113
114 /**
115 * @param string $cluster
116 * @param bool|string $wiki
117 * @return LoadBalancer
118 * @throws InvalidArgumentException
119 */
120 protected function newExternalLB( $cluster, $wiki = false ) {
121 global $wgExternalServers;
122 if ( !isset( $wgExternalServers[$cluster] ) ) {
123 throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
124 }
125
126 return $this->newLoadBalancer( $wgExternalServers[$cluster] );
127 }
128
129 /**
130 * @param string $cluster
131 * @param bool|string $wiki
132 * @return array
133 */
134 public function getExternalLB( $cluster, $wiki = false ) {
135 if ( !isset( $this->extLBs[$cluster] ) ) {
136 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
137 $this->chronProt->initLB( $this->extLBs[$cluster] );
138 }
139
140 return $this->extLBs[$cluster];
141 }
142
143 private function newLoadBalancer( array $servers ) {
144 $lb = new LoadBalancer( array_merge(
145 $this->baseLoadBalancerParams(),
146 [
147 'servers' => $servers,
148 'loadMonitor' => $this->loadMonitorClass,
149 ]
150 ) );
151 $this->initLoadBalancer( $lb );
152
153 return $lb;
154 }
155
156 /**
157 * Execute a function for each tracked load balancer
158 * The callback is called with the load balancer as the first parameter,
159 * and $params passed as the subsequent parameters.
160 *
161 * @param callable $callback
162 * @param array $params
163 */
164 public function forEachLB( $callback, array $params = [] ) {
165 if ( isset( $this->mainLB ) ) {
166 call_user_func_array( $callback, array_merge( [ $this->mainLB ], $params ) );
167 }
168 foreach ( $this->extLBs as $lb ) {
169 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
170 }
171 }
172 }