Merge "Html: Update list of $voidElements"
[lhc/web/wiklou.git] / includes / db / loadbalancer / LBFactoryMW.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
26 /**
27 * Legacy MediaWiki-specific class for generating database load balancers
28 * @ingroup Database
29 */
30 abstract class LBFactoryMW {
31 /**
32 * @param array $lbConf Config for LBFactory::__construct()
33 * @param Config $mainConfig Main config object from MediaWikiServices
34 * @return array
35 */
36 public static function applyDefaultConfig( array $lbConf, Config $mainConfig ) {
37 global $wgCommandLineMode;
38
39 $lbConf += [
40 'localDomain' => new DatabaseDomain(
41 $mainConfig->get( 'DBname' ),
42 null,
43 $mainConfig->get( 'DBprefix' )
44 ),
45 'profiler' => Profiler::instance(),
46 'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
47 'replLogger' => LoggerFactory::getInstance( 'DBReplication' ),
48 'queryLogger' => LoggerFactory::getInstance( 'DBQuery' ),
49 'connLogger' => LoggerFactory::getInstance( 'DBConnection' ),
50 'perfLogger' => LoggerFactory::getInstance( 'DBPerformance' ),
51 'errorLogger' => [ MWExceptionHandler::class, 'logException' ],
52 'cliMode' => $wgCommandLineMode,
53 'hostname' => wfHostname(),
54 // TODO: replace the global wfConfiguredReadOnlyReason() with a service.
55 'readOnlyReason' => wfConfiguredReadOnlyReason(),
56 ];
57
58 if ( $lbConf['class'] === 'LBFactorySimple' ) {
59 if ( isset( $lbConf['servers'] ) ) {
60 // Server array is already explicitly configured; leave alone
61 } elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
62 foreach ( $mainConfig->get( 'DBservers' ) as $i => $server ) {
63 if ( $server['type'] === 'sqlite' ) {
64 $server += [ 'dbDirectory' => $mainConfig->get( 'SQLiteDataDir' ) ];
65 }
66 $lbConf['servers'][$i] = $server + [
67 'schema' => $mainConfig->get( 'DBmwschema' ),
68 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
69 'flags' => DBO_DEFAULT,
70 'sqlMode' => $mainConfig->get( 'SQLMode' ),
71 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
72 ];
73 }
74 } else {
75 $flags = DBO_DEFAULT;
76 $flags |= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG : 0;
77 $flags |= $mainConfig->get( 'DBssl' ) ? DBO_SSL : 0;
78 $flags |= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
79 $server = [
80 'host' => $mainConfig->get( 'DBserver' ),
81 'user' => $mainConfig->get( 'DBuser' ),
82 'password' => $mainConfig->get( 'DBpassword' ),
83 'dbname' => $mainConfig->get( 'DBname' ),
84 'schema' => $mainConfig->get( 'DBmwschema' ),
85 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
86 'type' => $mainConfig->get( 'DBtype' ),
87 'load' => 1,
88 'flags' => $flags,
89 'sqlMode' => $mainConfig->get( 'SQLMode' ),
90 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
91 ];
92 if ( $server['type'] === 'sqlite' ) {
93 $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
94 }
95 $lbConf['servers'] = [ $server ];
96 }
97 if ( !isset( $lbConf['externalServers'] ) ) {
98 $lbConf['externalServers'] = $mainConfig->get( 'ExternalServers' );
99 }
100 } elseif ( $lbConf['class'] === 'LBFactoryMulti' ) {
101 if ( isset( $lbConf['serverTemplate'] ) ) {
102 $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
103 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
104 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
105 }
106 }
107
108 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
109 $sCache = ObjectCache::getLocalServerInstance();
110 if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
111 $lbConf['srvCache'] = $sCache;
112 }
113 $cCache = ObjectCache::getLocalClusterInstance();
114 if ( $cCache->getQoS( $cCache::ATTR_EMULATION ) > $cCache::QOS_EMULATION_SQL ) {
115 $lbConf['memCache'] = $cCache;
116 }
117 $wCache = ObjectCache::getMainWANInstance();
118 if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
119 $lbConf['wanCache'] = $wCache;
120 }
121
122 return $lbConf;
123 }
124
125 /**
126 * Returns the LBFactory class to use and the load balancer configuration.
127 *
128 * @todo instead of this, use a ServiceContainer for managing the different implementations.
129 *
130 * @param array $config (e.g. $wgLBFactoryConf)
131 * @return string Class name
132 */
133 public static function getLBFactoryClass( array $config ) {
134 // For configuration backward compatibility after removing
135 // underscores from class names in MediaWiki 1.23.
136 $bcClasses = [
137 'LBFactory_Simple' => 'LBFactorySimple',
138 'LBFactory_Single' => 'LBFactorySingle',
139 'LBFactory_Multi' => 'LBFactoryMulti'
140 ];
141
142 $class = $config['class'];
143
144 if ( isset( $bcClasses[$class] ) ) {
145 $class = $bcClasses[$class];
146 wfDeprecated(
147 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
148 '1.23'
149 );
150 }
151
152 return $class;
153 }
154 }