Revert "Use display name in category page subheadings if provided"
[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 LBFactory {
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;
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['slave'] = true;
58 }
59 $server += [ 'flags' => DBO_DEFAULT ];
60 }
61 } else {
62 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
63 global $wgDBssl, $wgDBcompress;
64
65 $flags = DBO_DEFAULT;
66 if ( $wgDebugDumpSql ) {
67 $flags |= DBO_DEBUG;
68 }
69 if ( $wgDBssl ) {
70 $flags |= DBO_SSL;
71 }
72 if ( $wgDBcompress ) {
73 $flags |= DBO_COMPRESS;
74 }
75
76 $servers = [ [
77 'host' => $wgDBserver,
78 'user' => $wgDBuser,
79 'password' => $wgDBpassword,
80 'dbname' => $wgDBname,
81 'type' => $wgDBtype,
82 'load' => 1,
83 'flags' => $flags,
84 'master' => true
85 ] ];
86 }
87
88 return $this->newLoadBalancer( $servers );
89 }
90
91 /**
92 * @param bool|string $wiki
93 * @return LoadBalancer
94 */
95 public function getMainLB( $wiki = false ) {
96 if ( !isset( $this->mainLB ) ) {
97 $this->mainLB = $this->newMainLB( $wiki );
98 $this->mainLB->parentInfo( [ 'id' => 'main' ] );
99 $this->chronProt->initLB( $this->mainLB );
100 }
101
102 return $this->mainLB;
103 }
104
105 /**
106 * @throws MWException
107 * @param string $cluster
108 * @param bool|string $wiki
109 * @return LoadBalancer
110 */
111 protected function newExternalLB( $cluster, $wiki = false ) {
112 global $wgExternalServers;
113 if ( !isset( $wgExternalServers[$cluster] ) ) {
114 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
115 }
116
117 return $this->newLoadBalancer( $wgExternalServers[$cluster] );
118 }
119
120 /**
121 * @param string $cluster
122 * @param bool|string $wiki
123 * @return array
124 */
125 public function &getExternalLB( $cluster, $wiki = false ) {
126 if ( !isset( $this->extLBs[$cluster] ) ) {
127 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
128 $this->extLBs[$cluster]->parentInfo( [ 'id' => "ext-$cluster" ] );
129 $this->chronProt->initLB( $this->extLBs[$cluster] );
130 }
131
132 return $this->extLBs[$cluster];
133 }
134
135 private function newLoadBalancer( array $servers ) {
136 return new LoadBalancer( [
137 'servers' => $servers,
138 'loadMonitor' => $this->loadMonitorClass,
139 'readOnlyReason' => $this->readOnlyReason,
140 'trxProfiler' => $this->trxProfiler,
141 'srvCache' => $this->srvCache,
142 'wanCache' => $this->wanCache
143 ] );
144 }
145
146 /**
147 * Execute a function for each tracked load balancer
148 * The callback is called with the load balancer as the first parameter,
149 * and $params passed as the subsequent parameters.
150 *
151 * @param callable $callback
152 * @param array $params
153 */
154 public function forEachLB( $callback, array $params = [] ) {
155 if ( isset( $this->mainLB ) ) {
156 call_user_func_array( $callback, array_merge( [ $this->mainLB ], $params ) );
157 }
158 foreach ( $this->extLBs as $lb ) {
159 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
160 }
161 }
162
163 public function shutdown( $flags = 0 ) {
164 if ( !( $flags & self::SHUTDOWN_NO_CHRONPROT ) ) {
165 $this->shutdownChronologyProtector( $this->chronProt );
166 }
167 $this->commitMasterChanges( __METHOD__ ); // sanity
168 }
169 }