0186222f0689779a11d32166c2369db8992f33c6
[lhc/web/wiklou.git] / includes / db / MWLBFactory.php
1 <?php
2 /**
3 * Generator of database load balancing objects.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 use MediaWiki\Logger\LoggerFactory;
25 use MediaWiki\MediaWikiServices;
26 use Wikimedia\Rdbms\DatabaseDomain;
27
28 /**
29 * MediaWiki-specific class for generating database load balancers
30 * @ingroup Database
31 */
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
37 */
38 public static function applyDefaultConfig( array $lbConf, Config $mainConfig ) {
39 global $wgCommandLineMode;
40
41 static $typesWithSchema = [ 'postgres', 'msssql' ];
42
43 $lbConf += [
44 'localDomain' => new DatabaseDomain(
45 $mainConfig->get( 'DBname' ),
46 null,
47 $mainConfig->get( 'DBprefix' )
48 ),
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(),
60 ];
61
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' ]
74 ];
75 } elseif ( $server['type'] === 'mssql' ) {
76 $server += [
77 'port' => $mainConfig->get( 'DBport' ),
78 'useWindowsAuth' => $mainConfig->get( 'DBWindowsAuthentication' )
79 ];
80 }
81
82 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
83 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
84 }
85
86 $server += [
87 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
88 'flags' => DBO_DEFAULT,
89 'sqlMode' => $mainConfig->get( 'SQLMode' ),
90 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
91 ];
92
93 $lbConf['servers'][$i] = $server;
94 }
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' )
111 ];
112 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
113 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
114 }
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' );
124 }
125 $lbConf['servers'] = [ $server ];
126 }
127 if ( !isset( $lbConf['externalClusters'] ) ) {
128 $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
129 }
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' );
134 }
135 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
136 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
137 }
138 }
139
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;
144 }
145 $cCache = ObjectCache::getLocalClusterInstance();
146 if ( $cCache->getQoS( $cCache::ATTR_EMULATION ) > $cCache::QOS_EMULATION_SQL ) {
147 $lbConf['memCache'] = $cCache;
148 }
149 $wCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
150 if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
151 $lbConf['wanCache'] = $wCache;
152 }
153
154 return $lbConf;
155 }
156
157 /**
158 * Returns the LBFactory class to use and the load balancer configuration.
159 *
160 * @todo instead of this, use a ServiceContainer for managing the different implementations.
161 *
162 * @param array $config (e.g. $wgLBFactoryConf)
163 * @return string Class name
164 */
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'
172 ];
173
174 $class = $config['class'];
175
176 if ( isset( $bcClasses[$class] ) ) {
177 $class = $bcClasses[$class];
178 wfDeprecated(
179 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
180 '1.23'
181 );
182 }
183
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
189 ];
190
191 if ( isset( $compat[$class] ) ) {
192 $class = $compat[$class];
193 }
194
195 return $class;
196 }
197 }