Merge "Get StatsdDataFactory from MediaWikiServices"
[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
27 /**
28 * MediaWiki-specific class for generating database load balancers
29 * @ingroup Database
30 */
31 abstract class MWLBFactory {
32 /**
33 * @param array $lbConf Config for LBFactory::__construct()
34 * @param Config $mainConfig Main config object from MediaWikiServices
35 * @return array
36 */
37 public static function applyDefaultConfig( array $lbConf, Config $mainConfig ) {
38 global $wgCommandLineMode;
39
40 $lbConf += [
41 'localDomain' => new DatabaseDomain(
42 $mainConfig->get( 'DBname' ),
43 null,
44 $mainConfig->get( 'DBprefix' )
45 ),
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(),
57 ];
58
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 += [
68 'port' => $mainConfig->get( 'DBport' ),
69 // Work around the reserved word usage in MediaWiki schema
70 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
71 ];
72 }
73 $lbConf['servers'][$i] = $server + [
74 'schema' => $mainConfig->get( 'DBmwschema' ),
75 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
76 'flags' => DBO_DEFAULT,
77 'sqlMode' => $mainConfig->get( 'SQLMode' ),
78 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
79 ];
80 }
81 } else {
82 $flags = DBO_DEFAULT;
83 $flags |= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG : 0;
84 $flags |= $mainConfig->get( 'DBssl' ) ? DBO_SSL : 0;
85 $flags |= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
86 $server = [
87 'host' => $mainConfig->get( 'DBserver' ),
88 'user' => $mainConfig->get( 'DBuser' ),
89 'password' => $mainConfig->get( 'DBpassword' ),
90 'dbname' => $mainConfig->get( 'DBname' ),
91 'schema' => $mainConfig->get( 'DBmwschema' ),
92 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
93 'type' => $mainConfig->get( 'DBtype' ),
94 'load' => 1,
95 'flags' => $flags,
96 'sqlMode' => $mainConfig->get( 'SQLMode' ),
97 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
98 ];
99 if ( $server['type'] === 'sqlite' ) {
100 $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
101 } elseif ( $server['type'] === 'postgres' ) {
102 $server['port'] = $mainConfig->get( 'DBport' );
103 // Work around the reserved word usage in MediaWiki schema
104 $server['keywordTableMap'] = [ 'user' => 'mwuser', 'text' => 'pagecontent' ];
105 }
106 $lbConf['servers'] = [ $server ];
107 }
108 if ( !isset( $lbConf['externalClusters'] ) ) {
109 $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
110 }
111 } elseif ( $lbConf['class'] === 'LBFactoryMulti' ) {
112 if ( isset( $lbConf['serverTemplate'] ) ) {
113 $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
114 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
115 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
116 }
117 }
118
119 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
120 $sCache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
121 if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
122 $lbConf['srvCache'] = $sCache;
123 }
124 $cCache = ObjectCache::getLocalClusterInstance();
125 if ( $cCache->getQoS( $cCache::ATTR_EMULATION ) > $cCache::QOS_EMULATION_SQL ) {
126 $lbConf['memCache'] = $cCache;
127 }
128 $wCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
129 if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
130 $lbConf['wanCache'] = $wCache;
131 }
132
133 return $lbConf;
134 }
135
136 /**
137 * Returns the LBFactory class to use and the load balancer configuration.
138 *
139 * @todo instead of this, use a ServiceContainer for managing the different implementations.
140 *
141 * @param array $config (e.g. $wgLBFactoryConf)
142 * @return string Class name
143 */
144 public static function getLBFactoryClass( array $config ) {
145 // For configuration backward compatibility after removing
146 // underscores from class names in MediaWiki 1.23.
147 $bcClasses = [
148 'LBFactory_Simple' => 'LBFactorySimple',
149 'LBFactory_Single' => 'LBFactorySingle',
150 'LBFactory_Multi' => 'LBFactoryMulti'
151 ];
152
153 $class = $config['class'];
154
155 if ( isset( $bcClasses[$class] ) ) {
156 $class = $bcClasses[$class];
157 wfDeprecated(
158 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
159 '1.23'
160 );
161 }
162
163 return $class;
164 }
165 }