Move mssql class to /libs
[mediawiki.git] / includes / db / MWLBFactory.php
blob0186222f0689779a11d32166c2369db8992f33c6
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 use MediaWiki\Logger\LoggerFactory;
25 use MediaWiki\MediaWikiServices;
26 use Wikimedia\Rdbms\DatabaseDomain;
28 /**
29 * MediaWiki-specific class for generating database load balancers
30 * @ingroup Database
32 abstract class MWLBFactory {
33 /**
34 * @param array $lbConf Config for LBFactory::__construct()
35 * @param Config $mainConfig Main config object from MediaWikiServices
36 * @return array
38 public static function applyDefaultConfig( array $lbConf, Config $mainConfig ) {
39 global $wgCommandLineMode;
41 static $typesWithSchema = [ 'postgres', 'msssql' ];
43 $lbConf += [
44 'localDomain' => new DatabaseDomain(
45 $mainConfig->get( 'DBname' ),
46 null,
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' ) {
70 $server += [
71 'port' => $mainConfig->get( 'DBport' ),
72 // Work around the reserved word usage in MediaWiki schema
73 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
75 } elseif ( $server['type'] === 'mssql' ) {
76 $server += [
77 'port' => $mainConfig->get( 'DBport' ),
78 'useWindowsAuth' => $mainConfig->get( 'DBWindowsAuthentication' )
82 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
83 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
86 $server += [
87 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
88 'flags' => DBO_DEFAULT,
89 'sqlMode' => $mainConfig->get( 'SQLMode' ),
90 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
93 $lbConf['servers'][$i] = $server;
95 } else {
96 $flags = DBO_DEFAULT;
97 $flags |= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG : 0;
98 $flags |= $mainConfig->get( 'DBssl' ) ? DBO_SSL : 0;
99 $flags |= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
100 $server = [
101 'host' => $mainConfig->get( 'DBserver' ),
102 'user' => $mainConfig->get( 'DBuser' ),
103 'password' => $mainConfig->get( 'DBpassword' ),
104 'dbname' => $mainConfig->get( 'DBname' ),
105 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
106 'type' => $mainConfig->get( 'DBtype' ),
107 'load' => 1,
108 'flags' => $flags,
109 'sqlMode' => $mainConfig->get( 'SQLMode' ),
110 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
112 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
113 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
115 if ( $server['type'] === 'sqlite' ) {
116 $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
117 } elseif ( $server['type'] === 'postgres' ) {
118 $server['port'] = $mainConfig->get( 'DBport' );
119 // Work around the reserved word usage in MediaWiki schema
120 $server['keywordTableMap'] = [ 'user' => 'mwuser', 'text' => 'pagecontent' ];
121 } elseif ( $server['type'] === 'mssql' ) {
122 $server['port'] = $mainConfig->get( 'DBport' );
123 $server['useWindowsAuth'] = $mainConfig->get( 'DBWindowsAuthentication' );
125 $lbConf['servers'] = [ $server ];
127 if ( !isset( $lbConf['externalClusters'] ) ) {
128 $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
130 } elseif ( $lbConf['class'] === 'LBFactoryMulti' ) {
131 if ( isset( $lbConf['serverTemplate'] ) ) {
132 if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
133 $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
135 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
136 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
140 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
141 $sCache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
142 if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
143 $lbConf['srvCache'] = $sCache;
145 $cCache = ObjectCache::getLocalClusterInstance();
146 if ( $cCache->getQoS( $cCache::ATTR_EMULATION ) > $cCache::QOS_EMULATION_SQL ) {
147 $lbConf['memCache'] = $cCache;
149 $wCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
150 if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
151 $lbConf['wanCache'] = $wCache;
154 return $lbConf;
158 * Returns the LBFactory class to use and the load balancer configuration.
160 * @todo instead of this, use a ServiceContainer for managing the different implementations.
162 * @param array $config (e.g. $wgLBFactoryConf)
163 * @return string Class name
165 public static function getLBFactoryClass( array $config ) {
166 // For configuration backward compatibility after removing
167 // underscores from class names in MediaWiki 1.23.
168 $bcClasses = [
169 'LBFactory_Simple' => 'LBFactorySimple',
170 'LBFactory_Single' => 'LBFactorySingle',
171 'LBFactory_Multi' => 'LBFactoryMulti'
174 $class = $config['class'];
176 if ( isset( $bcClasses[$class] ) ) {
177 $class = $bcClasses[$class];
178 wfDeprecated(
179 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
180 '1.23'
184 // For configuration backward compatibility after moving classes to namespaces (1.29)
185 $compat = [
186 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
187 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
188 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
191 if ( isset( $compat[$class] ) ) {
192 $class = $compat[$class];
195 return $class;