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
24 use MediaWiki\Logger\LoggerFactory
;
25 use MediaWiki\MediaWikiServices
;
26 use Wikimedia\Rdbms\LBFactory
;
27 use Wikimedia\Rdbms\DatabaseDomain
;
30 * MediaWiki-specific class for generating database load balancers
33 abstract class MWLBFactory
{
35 /** @var array Cache of already-logged deprecation messages */
36 private static $loggedDeprecations = [];
39 * @param array $lbConf Config for LBFactory::__construct()
40 * @param Config $mainConfig Main config object from MediaWikiServices
41 * @param ConfiguredReadOnlyMode $readOnlyMode
44 public static function applyDefaultConfig( array $lbConf, Config
$mainConfig,
45 ConfiguredReadOnlyMode
$readOnlyMode
47 global $wgCommandLineMode;
49 static $typesWithSchema = [ 'postgres', 'msssql' ];
52 'localDomain' => new DatabaseDomain(
53 $mainConfig->get( 'DBname' ),
54 $mainConfig->get( 'DBmwschema' ),
55 $mainConfig->get( 'DBprefix' )
57 'profiler' => Profiler
::instance(),
58 'trxProfiler' => Profiler
::instance()->getTransactionProfiler(),
59 'replLogger' => LoggerFactory
::getInstance( 'DBReplication' ),
60 'queryLogger' => LoggerFactory
::getInstance( 'DBQuery' ),
61 'connLogger' => LoggerFactory
::getInstance( 'DBConnection' ),
62 'perfLogger' => LoggerFactory
::getInstance( 'DBPerformance' ),
63 'errorLogger' => [ MWExceptionHandler
::class, 'logException' ],
64 'deprecationLogger' => [ static::class, 'logDeprecation' ],
65 'cliMode' => $wgCommandLineMode,
66 'hostname' => wfHostname(),
67 'readOnlyReason' => $readOnlyMode->getReason(),
70 // When making changes here, remember to also specify MediaWiki-specific options
71 // for Database classes in the relevant Installer subclass.
72 // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
73 if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple
::class ) {
74 if ( isset( $lbConf['servers'] ) ) {
75 // Server array is already explicitly configured; leave alone
76 } elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
77 foreach ( $mainConfig->get( 'DBservers' ) as $i => $server ) {
78 if ( $server['type'] === 'sqlite' ) {
79 $server +
= [ 'dbDirectory' => $mainConfig->get( 'SQLiteDataDir' ) ];
80 } elseif ( $server['type'] === 'postgres' ) {
82 'port' => $mainConfig->get( 'DBport' ),
83 // Work around the reserved word usage in MediaWiki schema
84 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
86 } elseif ( $server['type'] === 'mssql' ) {
88 'port' => $mainConfig->get( 'DBport' ),
89 'useWindowsAuth' => $mainConfig->get( 'DBWindowsAuthentication' )
93 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
94 $server +
= [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
98 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
99 'flags' => DBO_DEFAULT
,
100 'sqlMode' => $mainConfig->get( 'SQLMode' ),
101 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
104 $lbConf['servers'][$i] = $server;
107 $flags = DBO_DEFAULT
;
108 $flags |
= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG
: 0;
109 $flags |
= $mainConfig->get( 'DBssl' ) ? DBO_SSL
: 0;
110 $flags |
= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS
: 0;
112 'host' => $mainConfig->get( 'DBserver' ),
113 'user' => $mainConfig->get( 'DBuser' ),
114 'password' => $mainConfig->get( 'DBpassword' ),
115 'dbname' => $mainConfig->get( 'DBname' ),
116 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
117 'type' => $mainConfig->get( 'DBtype' ),
120 'sqlMode' => $mainConfig->get( 'SQLMode' ),
121 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
123 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
124 $server +
= [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
126 if ( $server['type'] === 'sqlite' ) {
127 $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
128 } elseif ( $server['type'] === 'postgres' ) {
129 $server['port'] = $mainConfig->get( 'DBport' );
130 // Work around the reserved word usage in MediaWiki schema
131 $server['keywordTableMap'] = [ 'user' => 'mwuser', 'text' => 'pagecontent' ];
132 } elseif ( $server['type'] === 'mssql' ) {
133 $server['port'] = $mainConfig->get( 'DBport' );
134 $server['useWindowsAuth'] = $mainConfig->get( 'DBWindowsAuthentication' );
136 $lbConf['servers'] = [ $server ];
138 if ( !isset( $lbConf['externalClusters'] ) ) {
139 $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
141 } elseif ( $lbConf['class'] === Wikimedia\Rdbms\LBFactoryMulti
::class ) {
142 if ( isset( $lbConf['serverTemplate'] ) ) {
143 if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
144 $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
146 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
147 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
151 $services = MediaWikiServices
::getInstance();
153 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
154 $sCache = $services->getLocalServerObjectCache();
155 if ( $sCache->getQoS( $sCache::ATTR_EMULATION
) > $sCache::QOS_EMULATION_SQL
) {
156 $lbConf['srvCache'] = $sCache;
158 $mStash = $services->getMainObjectStash();
159 if ( $mStash->getQoS( $mStash::ATTR_EMULATION
) > $mStash::QOS_EMULATION_SQL
) {
160 $lbConf['memStash'] = $mStash;
162 $wCache = $services->getMainWANObjectCache();
163 if ( $wCache->getQoS( $wCache::ATTR_EMULATION
) > $wCache::QOS_EMULATION_SQL
) {
164 $lbConf['wanCache'] = $wCache;
171 * Returns the LBFactory class to use and the load balancer configuration.
173 * @todo instead of this, use a ServiceContainer for managing the different implementations.
175 * @param array $config (e.g. $wgLBFactoryConf)
176 * @return string Class name
178 public static function getLBFactoryClass( array $config ) {
179 // For configuration backward compatibility after removing
180 // underscores from class names in MediaWiki 1.23.
182 'LBFactory_Simple' => 'LBFactorySimple',
183 'LBFactory_Single' => 'LBFactorySingle',
184 'LBFactory_Multi' => 'LBFactoryMulti'
187 $class = $config['class'];
189 if ( isset( $bcClasses[$class] ) ) {
190 $class = $bcClasses[$class];
192 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
197 // For configuration backward compatibility after moving classes to namespaces (1.29)
199 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle
::class,
200 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple
::class,
201 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti
::class
204 if ( isset( $compat[$class] ) ) {
205 $class = $compat[$class];
211 public static function setSchemaAliases( LBFactory
$lbFactory, Config
$config ) {
212 if ( $config->get( 'DBtype' ) === 'mysql' ) {
214 * When SQLite indexes were introduced in r45764, it was noted that
215 * SQLite requires index names to be unique within the whole database,
216 * not just within a schema. As discussed in CR r45819, to avoid the
217 * need for a schema change on existing installations, the indexes
218 * were implicitly mapped from the new names to the old names.
220 * This mapping can be removed if DB patches are introduced to alter
221 * the relevant tables in existing installations. Note that because
222 * this index mapping applies to table creation, even new installations
223 * of MySQL have the old names (except for installations created during
224 * a period where this mapping was inappropriately removed, see
227 $lbFactory->setIndexAliases( [
228 'ar_usertext_timestamp' => 'usertext_timestamp',
229 'un_user_id' => 'user_id',
230 'un_user_ip' => 'user_ip',
236 * Log a database deprecation warning
237 * @param string $msg Deprecation message
239 public static function logDeprecation( $msg ) {
240 global $wgDevelopmentWarnings;
242 if ( isset( self
::$loggedDeprecations[$msg] ) ) {
245 self
::$loggedDeprecations[$msg] = true;
247 if ( $wgDevelopmentWarnings ) {
248 trigger_error( $msg, E_USER_DEPRECATED
);
250 wfDebugLog( 'deprecated', $msg, 'private' );