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
;
25 use MediaWiki\MediaWikiServices
;
28 * MediaWiki-specific class for generating database load balancers
31 abstract class MWLBFactory
{
33 * @param array $lbConf Config for LBFactory::__construct()
34 * @param Config $mainConfig Main config object from MediaWikiServices
37 public static function applyDefaultConfig( array $lbConf, Config
$mainConfig ) {
38 global $wgCommandLineMode;
41 'localDomain' => new DatabaseDomain(
42 $mainConfig->get( 'DBname' ),
44 $mainConfig->get( 'DBprefix' )
46 'profiler' => Profiler
::instance(),
47 'trxProfiler' => Profiler
::instance()->getTransactionProfiler(),
48 'replLogger' => LoggerFactory
::getInstance( 'DBReplication' ),
49 'queryLogger' => LoggerFactory
::getInstance( 'DBQuery' ),
50 'connLogger' => LoggerFactory
::getInstance( 'DBConnection' ),
51 'perfLogger' => LoggerFactory
::getInstance( 'DBPerformance' ),
52 'errorLogger' => [ MWExceptionHandler
::class, 'logException' ],
53 'cliMode' => $wgCommandLineMode,
54 'hostname' => wfHostname(),
55 // TODO: replace the global wfConfiguredReadOnlyReason() with a service.
56 'readOnlyReason' => wfConfiguredReadOnlyReason(),
59 if ( $lbConf['class'] === 'LBFactorySimple' ) {
60 if ( isset( $lbConf['servers'] ) ) {
61 // Server array is already explicitly configured; leave alone
62 } elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
63 foreach ( $mainConfig->get( 'DBservers' ) as $i => $server ) {
64 if ( $server['type'] === 'sqlite' ) {
65 $server +
= [ 'dbDirectory' => $mainConfig->get( 'SQLiteDataDir' ) ];
66 } elseif ( $server['type'] === 'postgres' ) {
67 $server +
= [ 'port' => $mainConfig->get( 'DBport' ) ];
69 $lbConf['servers'][$i] = $server +
[
70 'schema' => $mainConfig->get( 'DBmwschema' ),
71 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
72 'flags' => DBO_DEFAULT
,
73 'sqlMode' => $mainConfig->get( 'SQLMode' ),
74 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
79 $flags |
= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG
: 0;
80 $flags |
= $mainConfig->get( 'DBssl' ) ? DBO_SSL
: 0;
81 $flags |
= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS
: 0;
83 'host' => $mainConfig->get( 'DBserver' ),
84 'user' => $mainConfig->get( 'DBuser' ),
85 'password' => $mainConfig->get( 'DBpassword' ),
86 'dbname' => $mainConfig->get( 'DBname' ),
87 'schema' => $mainConfig->get( 'DBmwschema' ),
88 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
89 'type' => $mainConfig->get( 'DBtype' ),
92 'sqlMode' => $mainConfig->get( 'SQLMode' ),
93 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
95 if ( $server['type'] === 'sqlite' ) {
96 $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
97 } elseif ( $server['type'] === 'postgres' ) {
98 $server['port'] = $mainConfig->get( 'DBport' );
100 $lbConf['servers'] = [ $server ];
102 if ( !isset( $lbConf['externalClusters'] ) ) {
103 $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
105 } elseif ( $lbConf['class'] === 'LBFactoryMulti' ) {
106 if ( isset( $lbConf['serverTemplate'] ) ) {
107 $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
108 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
109 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
113 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
114 $sCache = MediaWikiServices
::getInstance()->getLocalServerObjectCache();
115 if ( $sCache->getQoS( $sCache::ATTR_EMULATION
) > $sCache::QOS_EMULATION_SQL
) {
116 $lbConf['srvCache'] = $sCache;
118 $cCache = ObjectCache
::getLocalClusterInstance();
119 if ( $cCache->getQoS( $cCache::ATTR_EMULATION
) > $cCache::QOS_EMULATION_SQL
) {
120 $lbConf['memCache'] = $cCache;
122 $wCache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
123 if ( $wCache->getQoS( $wCache::ATTR_EMULATION
) > $wCache::QOS_EMULATION_SQL
) {
124 $lbConf['wanCache'] = $wCache;
131 * Returns the LBFactory class to use and the load balancer configuration.
133 * @todo instead of this, use a ServiceContainer for managing the different implementations.
135 * @param array $config (e.g. $wgLBFactoryConf)
136 * @return string Class name
138 public static function getLBFactoryClass( array $config ) {
139 // For configuration backward compatibility after removing
140 // underscores from class names in MediaWiki 1.23.
142 'LBFactory_Simple' => 'LBFactorySimple',
143 'LBFactory_Single' => 'LBFactorySingle',
144 'LBFactory_Multi' => 'LBFactoryMulti'
147 $class = $config['class'];
149 if ( isset( $bcClasses[$class] ) ) {
150 $class = $bcClasses[$class];
152 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',