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
24 use MediaWiki\Logger\LoggerFactory
;
27 * Legacy MediaWiki-specific class for generating database load balancers
30 abstract class LBFactoryMW
{
32 * @param array $lbConf Config for LBFactory::__construct()
33 * @param Config $mainConfig Main config object from MediaWikiServices
36 public static function applyDefaultConfig( array $lbConf, Config
$mainConfig ) {
37 global $wgCommandLineMode;
40 'localDomain' => new DatabaseDomain(
41 $mainConfig->get( 'DBname' ),
43 $mainConfig->get( 'DBprefix' )
45 'profiler' => Profiler
::instance(),
46 'trxProfiler' => Profiler
::instance()->getTransactionProfiler(),
47 'replLogger' => LoggerFactory
::getInstance( 'DBReplication' ),
48 'queryLogger' => LoggerFactory
::getInstance( 'DBQuery' ),
49 'connLogger' => LoggerFactory
::getInstance( 'DBConnection' ),
50 'perfLogger' => LoggerFactory
::getInstance( 'DBPerformance' ),
51 'errorLogger' => [ MWExceptionHandler
::class, 'logException' ],
52 'cliMode' => $wgCommandLineMode,
53 'hostname' => wfHostname(),
54 // TODO: replace the global wfConfiguredReadOnlyReason() with a service.
55 'readOnlyReason' => wfConfiguredReadOnlyReason(),
58 if ( $lbConf['class'] === 'LBFactorySimple' ) {
59 if ( isset( $lbConf['servers'] ) ) {
60 // Server array is already explicitly configured; leave alone
61 } elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
62 foreach ( $mainConfig->get( 'DBservers' ) as $i => $server ) {
63 if ( $server['type'] === 'sqlite' ) {
64 $server +
= [ 'dbDirectory' => $mainConfig->get( 'SQLiteDataDir' ) ];
66 $lbConf['servers'][$i] = $server +
[
67 'schema' => $mainConfig->get( 'DBmwschema' ),
68 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
69 'flags' => DBO_DEFAULT
,
70 'sqlMode' => $mainConfig->get( 'SQLMode' ),
71 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
76 $flags |
= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG
: 0;
77 $flags |
= $mainConfig->get( 'DBssl' ) ? DBO_SSL
: 0;
78 $flags |
= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS
: 0;
80 'host' => $mainConfig->get( 'DBserver' ),
81 'user' => $mainConfig->get( 'DBuser' ),
82 'password' => $mainConfig->get( 'DBpassword' ),
83 'dbname' => $mainConfig->get( 'DBname' ),
84 'schema' => $mainConfig->get( 'DBmwschema' ),
85 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
86 'type' => $mainConfig->get( 'DBtype' ),
89 'sqlMode' => $mainConfig->get( 'SQLMode' ),
90 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
92 if ( $server['type'] === 'sqlite' ) {
93 $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
95 $lbConf['servers'] = [ $server ];
97 if ( !isset( $lbConf['externalServers'] ) ) {
98 $lbConf['externalServers'] = $mainConfig->get( 'ExternalServers' );
100 } elseif ( $lbConf['class'] === 'LBFactoryMulti' ) {
101 if ( isset( $lbConf['serverTemplate'] ) ) {
102 $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
103 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
104 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
108 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
109 $sCache = ObjectCache
::getLocalServerInstance();
110 if ( $sCache->getQoS( $sCache::ATTR_EMULATION
) > $sCache::QOS_EMULATION_SQL
) {
111 $lbConf['srvCache'] = $sCache;
113 $cCache = ObjectCache
::getLocalClusterInstance();
114 if ( $cCache->getQoS( $cCache::ATTR_EMULATION
) > $cCache::QOS_EMULATION_SQL
) {
115 $lbConf['memCache'] = $cCache;
117 $wCache = ObjectCache
::getMainWANInstance();
118 if ( $wCache->getQoS( $wCache::ATTR_EMULATION
) > $wCache::QOS_EMULATION_SQL
) {
119 $lbConf['wanCache'] = $wCache;
126 * Returns the LBFactory class to use and the load balancer configuration.
128 * @todo instead of this, use a ServiceContainer for managing the different implementations.
130 * @param array $config (e.g. $wgLBFactoryConf)
131 * @return string Class name
133 public static function getLBFactoryClass( array $config ) {
134 // For configuration backward compatibility after removing
135 // underscores from class names in MediaWiki 1.23.
137 'LBFactory_Simple' => 'LBFactorySimple',
138 'LBFactory_Single' => 'LBFactorySingle',
139 'LBFactory_Multi' => 'LBFactoryMulti'
142 $class = $config['class'];
144 if ( isset( $bcClasses[$class] ) ) {
145 $class = $bcClasses[$class];
147 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',