Merge "Drop index oi_name_archive_name on table oldimage"
[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 // When making changes here, remember to also specify MediaWiki-specific options
63 // for Database classes in the relevant Installer subclass.
64 // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
65 if ( $lbConf['class'] === 'LBFactorySimple' ) {
66 if ( isset( $lbConf['servers'] ) ) {
67 // Server array is already explicitly configured; leave alone
68 } elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
69 foreach ( $mainConfig->get( 'DBservers' ) as $i => $server ) {
70 if ( $server['type'] === 'sqlite' ) {
71 $server += [ 'dbDirectory' => $mainConfig->get( 'SQLiteDataDir' ) ];
72 } elseif ( $server['type'] === 'postgres' ) {
73 $server += [
74 'port' => $mainConfig->get( 'DBport' ),
75 // Work around the reserved word usage in MediaWiki schema
76 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
77 ];
78 } elseif ( $server['type'] === 'mssql' ) {
79 $server += [
80 'port' => $mainConfig->get( 'DBport' ),
81 'useWindowsAuth' => $mainConfig->get( 'DBWindowsAuthentication' )
82 ];
83 }
84
85 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
86 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
87 }
88
89 $server += [
90 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
91 'flags' => DBO_DEFAULT,
92 'sqlMode' => $mainConfig->get( 'SQLMode' ),
93 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
94 ];
95
96 $lbConf['servers'][$i] = $server;
97 }
98 } else {
99 $flags = DBO_DEFAULT;
100 $flags |= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG : 0;
101 $flags |= $mainConfig->get( 'DBssl' ) ? DBO_SSL : 0;
102 $flags |= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
103 $server = [
104 'host' => $mainConfig->get( 'DBserver' ),
105 'user' => $mainConfig->get( 'DBuser' ),
106 'password' => $mainConfig->get( 'DBpassword' ),
107 'dbname' => $mainConfig->get( 'DBname' ),
108 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
109 'type' => $mainConfig->get( 'DBtype' ),
110 'load' => 1,
111 'flags' => $flags,
112 'sqlMode' => $mainConfig->get( 'SQLMode' ),
113 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
114 ];
115 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
116 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
117 }
118 if ( $server['type'] === 'sqlite' ) {
119 $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
120 } elseif ( $server['type'] === 'postgres' ) {
121 $server['port'] = $mainConfig->get( 'DBport' );
122 // Work around the reserved word usage in MediaWiki schema
123 $server['keywordTableMap'] = [ 'user' => 'mwuser', 'text' => 'pagecontent' ];
124 } elseif ( $server['type'] === 'mssql' ) {
125 $server['port'] = $mainConfig->get( 'DBport' );
126 $server['useWindowsAuth'] = $mainConfig->get( 'DBWindowsAuthentication' );
127 }
128 $lbConf['servers'] = [ $server ];
129 }
130 if ( !isset( $lbConf['externalClusters'] ) ) {
131 $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
132 }
133 } elseif ( $lbConf['class'] === 'LBFactoryMulti' ) {
134 if ( isset( $lbConf['serverTemplate'] ) ) {
135 if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
136 $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
137 }
138 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
139 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
140 }
141 }
142
143 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
144 $sCache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
145 if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
146 $lbConf['srvCache'] = $sCache;
147 }
148 $cCache = ObjectCache::getLocalClusterInstance();
149 if ( $cCache->getQoS( $cCache::ATTR_EMULATION ) > $cCache::QOS_EMULATION_SQL ) {
150 $lbConf['memCache'] = $cCache;
151 }
152 $wCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
153 if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
154 $lbConf['wanCache'] = $wCache;
155 }
156
157 return $lbConf;
158 }
159
160 /**
161 * Returns the LBFactory class to use and the load balancer configuration.
162 *
163 * @todo instead of this, use a ServiceContainer for managing the different implementations.
164 *
165 * @param array $config (e.g. $wgLBFactoryConf)
166 * @return string Class name
167 */
168 public static function getLBFactoryClass( array $config ) {
169 // For configuration backward compatibility after removing
170 // underscores from class names in MediaWiki 1.23.
171 $bcClasses = [
172 'LBFactory_Simple' => 'LBFactorySimple',
173 'LBFactory_Single' => 'LBFactorySingle',
174 'LBFactory_Multi' => 'LBFactoryMulti'
175 ];
176
177 $class = $config['class'];
178
179 if ( isset( $bcClasses[$class] ) ) {
180 $class = $bcClasses[$class];
181 wfDeprecated(
182 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
183 '1.23'
184 );
185 }
186
187 // For configuration backward compatibility after moving classes to namespaces (1.29)
188 $compat = [
189 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
190 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
191 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
192 ];
193
194 if ( isset( $compat[$class] ) ) {
195 $class = $compat[$class];
196 }
197
198 return $class;
199 }
200 }