MessageCache invalidation improvements
[mediawiki.git] / includes / db / MWLBFactory.php
blob40418fd47929fc6ba8245e5adf76f855389daac0
1 <?php
2 /**
3 * Generator of database load balancing objects.
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.
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.
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
20 * @file
21 * @ingroup Database
24 use MediaWiki\Logger\LoggerFactory;
25 use MediaWiki\MediaWikiServices;
27 /**
28 * MediaWiki-specific class for generating database load balancers
29 * @ingroup Database
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
37 public static function applyDefaultConfig( array $lbConf, Config $mainConfig ) {
38 global $wgCommandLineMode;
40 static $typesWithSchema = [ 'postgres', 'msssql' ];
42 $lbConf += [
43 'localDomain' => new DatabaseDomain(
44 $mainConfig->get( 'DBname' ),
45 null,
46 $mainConfig->get( 'DBprefix' )
48 'profiler' => Profiler::instance(),
49 'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
50 'replLogger' => LoggerFactory::getInstance( 'DBReplication' ),
51 'queryLogger' => LoggerFactory::getInstance( 'DBQuery' ),
52 'connLogger' => LoggerFactory::getInstance( 'DBConnection' ),
53 'perfLogger' => LoggerFactory::getInstance( 'DBPerformance' ),
54 'errorLogger' => [ MWExceptionHandler::class, 'logException' ],
55 'cliMode' => $wgCommandLineMode,
56 'hostname' => wfHostname(),
57 // TODO: replace the global wfConfiguredReadOnlyReason() with a service.
58 'readOnlyReason' => wfConfiguredReadOnlyReason(),
61 if ( $lbConf['class'] === 'LBFactorySimple' ) {
62 if ( isset( $lbConf['servers'] ) ) {
63 // Server array is already explicitly configured; leave alone
64 } elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
65 foreach ( $mainConfig->get( 'DBservers' ) as $i => $server ) {
66 if ( $server['type'] === 'sqlite' ) {
67 $server += [ 'dbDirectory' => $mainConfig->get( 'SQLiteDataDir' ) ];
68 } elseif ( $server['type'] === 'postgres' ) {
69 $server += [
70 'port' => $mainConfig->get( 'DBport' ),
71 // Work around the reserved word usage in MediaWiki schema
72 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
75 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
76 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
79 $server += [
80 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
81 'flags' => DBO_DEFAULT,
82 'sqlMode' => $mainConfig->get( 'SQLMode' ),
83 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
86 $lbConf['servers'][$i] = $server;
88 } else {
89 $flags = DBO_DEFAULT;
90 $flags |= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG : 0;
91 $flags |= $mainConfig->get( 'DBssl' ) ? DBO_SSL : 0;
92 $flags |= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
93 $server = [
94 'host' => $mainConfig->get( 'DBserver' ),
95 'user' => $mainConfig->get( 'DBuser' ),
96 'password' => $mainConfig->get( 'DBpassword' ),
97 'dbname' => $mainConfig->get( 'DBname' ),
98 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
99 'type' => $mainConfig->get( 'DBtype' ),
100 'load' => 1,
101 'flags' => $flags,
102 'sqlMode' => $mainConfig->get( 'SQLMode' ),
103 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
105 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
106 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
108 if ( $server['type'] === 'sqlite' ) {
109 $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
110 } elseif ( $server['type'] === 'postgres' ) {
111 $server['port'] = $mainConfig->get( 'DBport' );
112 // Work around the reserved word usage in MediaWiki schema
113 $server['keywordTableMap'] = [ 'user' => 'mwuser', 'text' => 'pagecontent' ];
115 $lbConf['servers'] = [ $server ];
117 if ( !isset( $lbConf['externalClusters'] ) ) {
118 $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
120 } elseif ( $lbConf['class'] === 'LBFactoryMulti' ) {
121 if ( isset( $lbConf['serverTemplate'] ) ) {
122 if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
123 $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
125 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
126 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
130 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
131 $sCache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
132 if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
133 $lbConf['srvCache'] = $sCache;
135 $cCache = ObjectCache::getLocalClusterInstance();
136 if ( $cCache->getQoS( $cCache::ATTR_EMULATION ) > $cCache::QOS_EMULATION_SQL ) {
137 $lbConf['memCache'] = $cCache;
139 $wCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
140 if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
141 $lbConf['wanCache'] = $wCache;
144 return $lbConf;
148 * Returns the LBFactory class to use and the load balancer configuration.
150 * @todo instead of this, use a ServiceContainer for managing the different implementations.
152 * @param array $config (e.g. $wgLBFactoryConf)
153 * @return string Class name
155 public static function getLBFactoryClass( array $config ) {
156 // For configuration backward compatibility after removing
157 // underscores from class names in MediaWiki 1.23.
158 $bcClasses = [
159 'LBFactory_Simple' => 'LBFactorySimple',
160 'LBFactory_Single' => 'LBFactorySingle',
161 'LBFactory_Multi' => 'LBFactoryMulti'
164 $class = $config['class'];
166 if ( isset( $bcClasses[$class] ) ) {
167 $class = $bcClasses[$class];
168 wfDeprecated(
169 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
170 '1.23'
174 return $class;