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
;
26 use Wikimedia\Rdbms\DatabaseDomain
;
29 * MediaWiki-specific class for generating database load balancers
32 abstract class MWLBFactory
{
34 * @param array $lbConf Config for LBFactory::__construct()
35 * @param Config $mainConfig Main config object from MediaWikiServices
38 public static function applyDefaultConfig( array $lbConf, Config
$mainConfig ) {
39 global $wgCommandLineMode;
41 static $typesWithSchema = [ 'postgres', 'msssql' ];
44 'localDomain' => new DatabaseDomain(
45 $mainConfig->get( 'DBname' ),
47 $mainConfig->get( 'DBprefix' )
49 'profiler' => Profiler
::instance(),
50 'trxProfiler' => Profiler
::instance()->getTransactionProfiler(),
51 'replLogger' => LoggerFactory
::getInstance( 'DBReplication' ),
52 'queryLogger' => LoggerFactory
::getInstance( 'DBQuery' ),
53 'connLogger' => LoggerFactory
::getInstance( 'DBConnection' ),
54 'perfLogger' => LoggerFactory
::getInstance( 'DBPerformance' ),
55 'errorLogger' => [ MWExceptionHandler
::class, 'logException' ],
56 'cliMode' => $wgCommandLineMode,
57 'hostname' => wfHostname(),
58 // TODO: replace the global wfConfiguredReadOnlyReason() with a service.
59 'readOnlyReason' => wfConfiguredReadOnlyReason(),
62 if ( $lbConf['class'] === 'LBFactorySimple' ) {
63 if ( isset( $lbConf['servers'] ) ) {
64 // Server array is already explicitly configured; leave alone
65 } elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
66 foreach ( $mainConfig->get( 'DBservers' ) as $i => $server ) {
67 if ( $server['type'] === 'sqlite' ) {
68 $server +
= [ 'dbDirectory' => $mainConfig->get( 'SQLiteDataDir' ) ];
69 } elseif ( $server['type'] === 'postgres' ) {
71 'port' => $mainConfig->get( 'DBport' ),
72 // Work around the reserved word usage in MediaWiki schema
73 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
76 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
77 $server +
= [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
81 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
82 'flags' => DBO_DEFAULT
,
83 'sqlMode' => $mainConfig->get( 'SQLMode' ),
84 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
87 $lbConf['servers'][$i] = $server;
91 $flags |
= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG
: 0;
92 $flags |
= $mainConfig->get( 'DBssl' ) ? DBO_SSL
: 0;
93 $flags |
= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS
: 0;
95 'host' => $mainConfig->get( 'DBserver' ),
96 'user' => $mainConfig->get( 'DBuser' ),
97 'password' => $mainConfig->get( 'DBpassword' ),
98 'dbname' => $mainConfig->get( 'DBname' ),
99 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
100 'type' => $mainConfig->get( 'DBtype' ),
103 'sqlMode' => $mainConfig->get( 'SQLMode' ),
104 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
106 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
107 $server +
= [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
109 if ( $server['type'] === 'sqlite' ) {
110 $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
111 } elseif ( $server['type'] === 'postgres' ) {
112 $server['port'] = $mainConfig->get( 'DBport' );
113 // Work around the reserved word usage in MediaWiki schema
114 $server['keywordTableMap'] = [ 'user' => 'mwuser', 'text' => 'pagecontent' ];
116 $lbConf['servers'] = [ $server ];
118 if ( !isset( $lbConf['externalClusters'] ) ) {
119 $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
121 } elseif ( $lbConf['class'] === 'LBFactoryMulti' ) {
122 if ( isset( $lbConf['serverTemplate'] ) ) {
123 if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
124 $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
126 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
127 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
131 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
132 $sCache = MediaWikiServices
::getInstance()->getLocalServerObjectCache();
133 if ( $sCache->getQoS( $sCache::ATTR_EMULATION
) > $sCache::QOS_EMULATION_SQL
) {
134 $lbConf['srvCache'] = $sCache;
136 $cCache = ObjectCache
::getLocalClusterInstance();
137 if ( $cCache->getQoS( $cCache::ATTR_EMULATION
) > $cCache::QOS_EMULATION_SQL
) {
138 $lbConf['memCache'] = $cCache;
140 $wCache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
141 if ( $wCache->getQoS( $wCache::ATTR_EMULATION
) > $wCache::QOS_EMULATION_SQL
) {
142 $lbConf['wanCache'] = $wCache;
149 * Returns the LBFactory class to use and the load balancer configuration.
151 * @todo instead of this, use a ServiceContainer for managing the different implementations.
153 * @param array $config (e.g. $wgLBFactoryConf)
154 * @return string Class name
156 public static function getLBFactoryClass( array $config ) {
157 // For configuration backward compatibility after removing
158 // underscores from class names in MediaWiki 1.23.
160 'LBFactory_Simple' => 'LBFactorySimple',
161 'LBFactory_Single' => 'LBFactorySingle',
162 'LBFactory_Multi' => 'LBFactoryMulti'
165 $class = $config['class'];
167 if ( isset( $bcClasses[$class] ) ) {
168 $class = $bcClasses[$class];
170 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
175 // For configuration backward compatibility after moving classes to namespaces (1.29)
177 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle
::class,
178 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple
::class,
179 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti
::class
182 if ( isset( $compat[$class] ) ) {
183 $class = $compat[$class];