Merge "Add dev dependency on HTML matchers"
[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 }
76 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
77 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
78 }
79
80 $server += [
81 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
82 'flags' => DBO_DEFAULT,
83 'sqlMode' => $mainConfig->get( 'SQLMode' ),
84 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
85 ];
86
87 $lbConf['servers'][$i] = $server;
88 }
89 } else {
90 $flags = DBO_DEFAULT;
91 $flags |= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG : 0;
92 $flags |= $mainConfig->get( 'DBssl' ) ? DBO_SSL : 0;
93 $flags |= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
94 $server = [
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' ),
101 'load' => 1,
102 'flags' => $flags,
103 'sqlMode' => $mainConfig->get( 'SQLMode' ),
104 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
105 ];
106 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
107 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
108 }
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' ];
115 }
116 $lbConf['servers'] = [ $server ];
117 }
118 if ( !isset( $lbConf['externalClusters'] ) ) {
119 $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
120 }
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' );
125 }
126 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
127 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
128 }
129 }
130
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;
135 }
136 $cCache = ObjectCache::getLocalClusterInstance();
137 if ( $cCache->getQoS( $cCache::ATTR_EMULATION ) > $cCache::QOS_EMULATION_SQL ) {
138 $lbConf['memCache'] = $cCache;
139 }
140 $wCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
141 if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
142 $lbConf['wanCache'] = $wCache;
143 }
144
145 return $lbConf;
146 }
147
148 /**
149 * Returns the LBFactory class to use and the load balancer configuration.
150 *
151 * @todo instead of this, use a ServiceContainer for managing the different implementations.
152 *
153 * @param array $config (e.g. $wgLBFactoryConf)
154 * @return string Class name
155 */
156 public static function getLBFactoryClass( array $config ) {
157 // For configuration backward compatibility after removing
158 // underscores from class names in MediaWiki 1.23.
159 $bcClasses = [
160 'LBFactory_Simple' => 'LBFactorySimple',
161 'LBFactory_Single' => 'LBFactorySingle',
162 'LBFactory_Multi' => 'LBFactoryMulti'
163 ];
164
165 $class = $config['class'];
166
167 if ( isset( $bcClasses[$class] ) ) {
168 $class = $bcClasses[$class];
169 wfDeprecated(
170 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
171 '1.23'
172 );
173 }
174
175 // For configuration backward compatibility after moving classes to namespaces (1.29)
176 $compat = [
177 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
178 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
179 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
180 ];
181
182 if ( isset( $compat[$class] ) ) {
183 $class = $compat[$class];
184 }
185
186 return $class;
187 }
188 }