MediaWiki REL1_31
MWLBFactory.php
Go to the documentation of this file.
1<?php
28
33abstract class MWLBFactory {
34
36 private static $loggedDeprecations = [];
37
44 public static function applyDefaultConfig( array $lbConf, Config $mainConfig,
45 ConfiguredReadOnlyMode $readOnlyMode
46 ) {
47 global $wgCommandLineMode;
48
49 static $typesWithSchema = [ 'postgres', 'msssql' ];
50
51 $lbConf += [
52 'localDomain' => new DatabaseDomain(
53 $mainConfig->get( 'DBname' ),
54 $mainConfig->get( 'DBmwschema' ),
55 $mainConfig->get( 'DBprefix' )
56 ),
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(),
68 ];
69
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' ) {
81 $server += [
82 'port' => $mainConfig->get( 'DBport' ),
83 // Work around the reserved word usage in MediaWiki schema
84 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
85 ];
86 } elseif ( $server['type'] === 'mssql' ) {
87 $server += [
88 'port' => $mainConfig->get( 'DBport' ),
89 'useWindowsAuth' => $mainConfig->get( 'DBWindowsAuthentication' )
90 ];
91 }
92
93 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
94 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
95 }
96
97 $server += [
98 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
99 'flags' => DBO_DEFAULT,
100 'sqlMode' => $mainConfig->get( 'SQLMode' ),
101 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
102 ];
103
104 $lbConf['servers'][$i] = $server;
105 }
106 } else {
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;
111 $server = [
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' ),
118 'load' => 1,
119 'flags' => $flags,
120 'sqlMode' => $mainConfig->get( 'SQLMode' ),
121 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
122 ];
123 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
124 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
125 }
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' );
135 }
136 $lbConf['servers'] = [ $server ];
137 }
138 if ( !isset( $lbConf['externalClusters'] ) ) {
139 $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
140 }
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' );
145 }
146 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
147 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
148 }
149 }
150
151 $services = MediaWikiServices::getInstance();
152
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;
157 }
158 $mStash = $services->getMainObjectStash();
159 if ( $mStash->getQoS( $mStash::ATTR_EMULATION ) > $mStash::QOS_EMULATION_SQL ) {
160 $lbConf['memStash'] = $mStash;
161 }
162 $wCache = $services->getMainWANObjectCache();
163 if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
164 $lbConf['wanCache'] = $wCache;
165 }
166
167 return $lbConf;
168 }
169
178 public static function getLBFactoryClass( array $config ) {
179 // For configuration backward compatibility after removing
180 // underscores from class names in MediaWiki 1.23.
181 $bcClasses = [
182 'LBFactory_Simple' => 'LBFactorySimple',
183 'LBFactory_Single' => 'LBFactorySingle',
184 'LBFactory_Multi' => 'LBFactoryMulti'
185 ];
186
187 $class = $config['class'];
188
189 if ( isset( $bcClasses[$class] ) ) {
190 $class = $bcClasses[$class];
192 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
193 '1.23'
194 );
195 }
196
197 // For configuration backward compatibility after moving classes to namespaces (1.29)
198 $compat = [
199 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
200 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
201 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
202 ];
203
204 if ( isset( $compat[$class] ) ) {
205 $class = $compat[$class];
206 }
207
208 return $class;
209 }
210
211 public static function setSchemaAliases( LBFactory $lbFactory, Config $config ) {
212 if ( $config->get( 'DBtype' ) === 'mysql' ) {
227 $lbFactory->setIndexAliases( [
228 'ar_usertext_timestamp' => 'usertext_timestamp',
229 'un_user_id' => 'user_id',
230 'un_user_ip' => 'user_ip',
231 ] );
232 }
233 }
234
239 public static function logDeprecation( $msg ) {
241
242 if ( isset( self::$loggedDeprecations[$msg] ) ) {
243 return;
244 }
245 self::$loggedDeprecations[$msg] = true;
246
248 trigger_error( $msg, E_USER_DEPRECATED );
249 }
250 wfDebugLog( 'deprecated', $msg, 'private' );
251 }
252}
$wgDevelopmentWarnings
If set to true MediaWiki will throw notices for some possible error conditions and for deprecated fun...
global $wgCommandLineMode
wfHostname()
Fetch server name for use in error reporting etc.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
A read-only mode service which does not depend on LoadBalancer.
getReason()
Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
MediaWiki-specific class for generating database load balancers.
static applyDefaultConfig(array $lbConf, Config $mainConfig, ConfiguredReadOnlyMode $readOnlyMode)
static logDeprecation( $msg)
Log a database deprecation warning.
static array $loggedDeprecations
Cache of already-logged deprecation messages.
static getLBFactoryClass(array $config)
Returns the LBFactory class to use and the load balancer configuration.
static setSchemaAliases(LBFactory $lbFactory, Config $config)
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Class to handle database/prefix specification for IDatabase domains.
An interface for generating database load balancers.
Definition LBFactory.php:39
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition hooks.txt:2273
Interface for configuration instances.
Definition Config.php:28
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
const DBO_COMPRESS
Definition defines.php:18
const DBO_DEFAULT
Definition defines.php:13
const DBO_SSL
Definition defines.php:17
const DBO_DEBUG
Definition defines.php:9