Merge "Remove not used private member variable mParserWarnings from OutputPage"
[mediawiki.git] / includes / db / loadbalancer / LBFactorySimple.php
blob3349e1f7373cad907ebda62bea74f9d3d23748a2
1 <?php
2 /**
3 * Generator of database load balancing objects.
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.
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.
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
20 * @file
21 * @ingroup Database
24 /**
25 * A simple single-master LBFactory that gets its configuration from the b/c globals
27 class LBFactorySimple extends LBFactory {
28 /** @var LoadBalancer */
29 private $mainLB;
30 /** @var LoadBalancer[] */
31 private $extLBs = array();
33 /** @var string */
34 private $loadMonitorClass;
36 public function __construct( array $conf ) {
37 parent::__construct( $conf );
39 $this->loadMonitorClass = isset( $conf['loadMonitorClass'] )
40 ? $conf['loadMonitorClass']
41 : null;
44 /**
45 * @param bool|string $wiki
46 * @return LoadBalancer
48 public function newMainLB( $wiki = false ) {
49 global $wgDBservers;
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;
60 } else {
61 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
62 global $wgDBssl, $wgDBcompress;
64 $flags = DBO_DEFAULT;
65 if ( $wgDebugDumpSql ) {
66 $flags |= DBO_DEBUG;
68 if ( $wgDBssl ) {
69 $flags |= DBO_SSL;
71 if ( $wgDBcompress ) {
72 $flags |= DBO_COMPRESS;
75 $servers = array( array(
76 'host' => $wgDBserver,
77 'user' => $wgDBuser,
78 'password' => $wgDBpassword,
79 'dbname' => $wgDBname,
80 'type' => $wgDBtype,
81 'load' => 1,
82 'flags' => $flags,
83 'master' => true
84 ) );
87 return new LoadBalancer( array(
88 'servers' => $servers,
89 'loadMonitor' => $this->loadMonitorClass,
90 'readOnlyReason' => $this->readOnlyReason
91 ) );
94 /**
95 * @param bool|string $wiki
96 * @return LoadBalancer
98 public function getMainLB( $wiki = false ) {
99 if ( !isset( $this->mainLB ) ) {
100 $this->mainLB = $this->newMainLB( $wiki );
101 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
102 $this->chronProt->initLB( $this->mainLB );
105 return $this->mainLB;
109 * @throws MWException
110 * @param string $cluster
111 * @param bool|string $wiki
112 * @return LoadBalancer
114 protected function newExternalLB( $cluster, $wiki = false ) {
115 global $wgExternalServers;
116 if ( !isset( $wgExternalServers[$cluster] ) ) {
117 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
120 return new LoadBalancer( array(
121 'servers' => $wgExternalServers[$cluster],
122 'loadMonitor' => $this->loadMonitorClass,
123 'readOnlyReason' => $this->readOnlyReason
124 ) );
128 * @param string $cluster
129 * @param bool|string $wiki
130 * @return array
132 public function &getExternalLB( $cluster, $wiki = false ) {
133 if ( !isset( $this->extLBs[$cluster] ) ) {
134 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
135 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
136 $this->chronProt->initLB( $this->extLBs[$cluster] );
139 return $this->extLBs[$cluster];
143 * Execute a function for each tracked load balancer
144 * The callback is called with the load balancer as the first parameter,
145 * and $params passed as the subsequent parameters.
147 * @param callable $callback
148 * @param array $params
150 public function forEachLB( $callback, array $params = array() ) {
151 if ( isset( $this->mainLB ) ) {
152 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
154 foreach ( $this->extLBs as $lb ) {
155 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
159 public function shutdown( $flags = 0 ) {
160 if ( !( $flags & self::SHUTDOWN_NO_CHRONPROT ) ) {
161 $this->shutdownChronologyProtector( $this->chronProt );
163 $this->commitMasterChanges( __METHOD__ ); // sanity