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' => function ( $section ) {
58 return Profiler
::instance()->scopedProfileIn( $section );
60 'trxProfiler' => Profiler
::instance()->getTransactionProfiler(),
61 'replLogger' => LoggerFactory
::getInstance( 'DBReplication' ),
62 'queryLogger' => LoggerFactory
::getInstance( 'DBQuery' ),
63 'connLogger' => LoggerFactory
::getInstance( 'DBConnection' ),
64 'perfLogger' => LoggerFactory
::getInstance( 'DBPerformance' ),
65 'errorLogger' => [ MWExceptionHandler
::class, 'logException' ],
66 'deprecationLogger' => [ static::class, 'logDeprecation' ],
67 'cliMode' => $wgCommandLineMode,
68 'hostname' => wfHostname(),
69 'readOnlyReason' => $readOnlyMode->getReason(),
70 'defaultGroup' => $mainConfig->get( 'DBDefaultGroup' ),
73 // When making changes here, remember to also specify MediaWiki-specific options
74 // for Database classes in the relevant Installer subclass.
75 // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
76 if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple
::class ) {
77 if ( isset( $lbConf['servers'] ) ) {
78 // Server array is already explicitly configured; leave alone
79 } elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
80 foreach ( $mainConfig->get( 'DBservers' ) as $i => $server ) {
81 if ( $server['type'] === 'sqlite' ) {
82 $server +
= [ 'dbDirectory' => $mainConfig->get( 'SQLiteDataDir' ) ];
83 } elseif ( $server['type'] === 'postgres' ) {
85 'port' => $mainConfig->get( 'DBport' ),
86 // Work around the reserved word usage in MediaWiki schema
87 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
89 } elseif ( $server['type'] === 'mssql' ) {
91 'port' => $mainConfig->get( 'DBport' ),
92 'useWindowsAuth' => $mainConfig->get( 'DBWindowsAuthentication' )
96 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
97 $server +
= [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
101 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
102 'flags' => DBO_DEFAULT
,
103 'sqlMode' => $mainConfig->get( 'SQLMode' ),
104 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
107 $lbConf['servers'][$i] = $server;
110 $flags = DBO_DEFAULT
;
111 $flags |
= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG
: 0;
112 $flags |
= $mainConfig->get( 'DBssl' ) ? DBO_SSL
: 0;
113 $flags |
= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS
: 0;
115 'host' => $mainConfig->get( 'DBserver' ),
116 'user' => $mainConfig->get( 'DBuser' ),
117 'password' => $mainConfig->get( 'DBpassword' ),
118 'dbname' => $mainConfig->get( 'DBname' ),
119 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
120 'type' => $mainConfig->get( 'DBtype' ),
123 'sqlMode' => $mainConfig->get( 'SQLMode' ),
124 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
126 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
127 $server +
= [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
129 if ( $server['type'] === 'sqlite' ) {
130 $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
131 } elseif ( $server['type'] === 'postgres' ) {
132 $server['port'] = $mainConfig->get( 'DBport' );
133 // Work around the reserved word usage in MediaWiki schema
134 $server['keywordTableMap'] = [ 'user' => 'mwuser', 'text' => 'pagecontent' ];
135 } elseif ( $server['type'] === 'mssql' ) {
136 $server['port'] = $mainConfig->get( 'DBport' );
137 $server['useWindowsAuth'] = $mainConfig->get( 'DBWindowsAuthentication' );
139 $lbConf['servers'] = [ $server ];
141 if ( !isset( $lbConf['externalClusters'] ) ) {
142 $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
144 } elseif ( $lbConf['class'] === Wikimedia\Rdbms\LBFactoryMulti
::class ) {
145 if ( isset( $lbConf['serverTemplate'] ) ) {
146 if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
147 $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
149 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
150 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
154 $services = MediaWikiServices
::getInstance();
156 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
157 $sCache = $services->getLocalServerObjectCache();
158 if ( $sCache->getQoS( $sCache::ATTR_EMULATION
) > $sCache::QOS_EMULATION_SQL
) {
159 $lbConf['srvCache'] = $sCache;
161 $mStash = $services->getMainObjectStash();
162 if ( $mStash->getQoS( $mStash::ATTR_EMULATION
) > $mStash::QOS_EMULATION_SQL
) {
163 $lbConf['memStash'] = $mStash;
165 $wCache = $services->getMainWANObjectCache();
166 if ( $wCache->getQoS( $wCache::ATTR_EMULATION
) > $wCache::QOS_EMULATION_SQL
) {
167 $lbConf['wanCache'] = $wCache;
174 * Returns the LBFactory class to use and the load balancer configuration.
176 * @todo instead of this, use a ServiceContainer for managing the different implementations.
178 * @param array $config (e.g. $wgLBFactoryConf)
179 * @return string Class name
181 public static function getLBFactoryClass( array $config ) {
182 // For configuration backward compatibility after removing
183 // underscores from class names in MediaWiki 1.23.
185 'LBFactory_Simple' => 'LBFactorySimple',
186 'LBFactory_Single' => 'LBFactorySingle',
187 'LBFactory_Multi' => 'LBFactoryMulti'
190 $class = $config['class'];
192 if ( isset( $bcClasses[$class] ) ) {
193 $class = $bcClasses[$class];
195 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
200 // For configuration backward compatibility after moving classes to namespaces (1.29)
202 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle
::class,
203 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple
::class,
204 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti
::class
207 if ( isset( $compat[$class] ) ) {
208 $class = $compat[$class];
214 public static function setSchemaAliases( LBFactory
$lbFactory, Config
$config ) {
215 if ( $config->get( 'DBtype' ) === 'mysql' ) {
217 * When SQLite indexes were introduced in r45764, it was noted that
218 * SQLite requires index names to be unique within the whole database,
219 * not just within a schema. As discussed in CR r45819, to avoid the
220 * need for a schema change on existing installations, the indexes
221 * were implicitly mapped from the new names to the old names.
223 * This mapping can be removed if DB patches are introduced to alter
224 * the relevant tables in existing installations. Note that because
225 * this index mapping applies to table creation, even new installations
226 * of MySQL have the old names (except for installations created during
227 * a period where this mapping was inappropriately removed, see
230 $lbFactory->setIndexAliases( [
231 'ar_usertext_timestamp' => 'usertext_timestamp',
232 'un_user_id' => 'user_id',
233 'un_user_ip' => 'user_ip',
239 * Log a database deprecation warning
240 * @param string $msg Deprecation message
242 public static function logDeprecation( $msg ) {
243 global $wgDevelopmentWarnings;
245 if ( isset( self
::$loggedDeprecations[$msg] ) ) {
248 self
::$loggedDeprecations[$msg] = true;
250 if ( $wgDevelopmentWarnings ) {
251 trigger_error( $msg, E_USER_DEPRECATED
);
253 wfDebugLog( 'deprecated', $msg, 'private' );