24 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
34 use Wikimedia\RequestTimeout\CriticalSectionProvider;
45 private static $loggedDeprecations = [];
52 MainConfigNames::DBcompress,
53 MainConfigNames::DBDefaultGroup,
54 MainConfigNames::DBmwschema,
55 MainConfigNames::DBname,
56 MainConfigNames::DBpassword,
57 MainConfigNames::DBport,
58 MainConfigNames::DBprefix,
59 MainConfigNames::DBserver,
60 MainConfigNames::DBservers,
61 MainConfigNames::DBssl,
62 MainConfigNames::DBtype,
63 MainConfigNames::DBuser,
64 MainConfigNames::DebugDumpSql,
65 MainConfigNames::DebugLogFile,
66 MainConfigNames::DebugToolbar,
67 MainConfigNames::ExternalServers,
68 MainConfigNames::SQLiteDataDir,
69 MainConfigNames::SQLMode,
78 private $readOnlyMode;
82 private $chronologyProtector;
98 private $statsdDataFactory;
115 CriticalSectionProvider $csProvider,
116 StatsdDataFactoryInterface $statsdDataFactory
118 $this->options = $options;
119 $this->readOnlyMode = $readOnlyMode;
120 $this->chronologyProtector = $chronologyProtector;
121 $this->srvCache = $srvCache;
122 $this->wanCache = $wanCache;
123 $this->csProvider = $csProvider;
124 $this->statsdDataFactory = $statsdDataFactory;
133 $this->options->assertRequiredOptions( self::APPLY_DEFAULT_CONFIG_OPTIONS );
135 $typesWithSchema = self::getDbTypesWithSchemas();
137 $profilerCallback =
null;
139 $profilerCallback =
static function ( $section ) {
146 $this->options->get( MainConfigNames::DBname ),
147 $this->options->get( MainConfigNames::DBmwschema ),
148 $this->options->get( MainConfigNames::DBprefix )
150 'profiler' => $profilerCallback,
152 'logger' => LoggerFactory::getInstance(
'rdbms' ),
153 'errorLogger' => [ MWExceptionHandler::class,
'logException' ],
154 'deprecationLogger' => [ static::class,
'logDeprecation' ],
155 'statsdDataFactory' => $this->statsdDataFactory,
156 'cliMode' => $this->options->get(
'CommandLineMode' ),
157 'readOnlyReason' => $this->readOnlyMode->getReason(),
158 'defaultGroup' => $this->options->get( MainConfigNames::DBDefaultGroup ),
159 'criticalSectionProvider' => $this->csProvider
166 if ( $lbConf[
'class'] === Wikimedia\Rdbms\LBFactorySimple::class ) {
167 if ( isset( $lbConf[
'servers'] ) ) {
169 } elseif ( is_array( $this->options->get( MainConfigNames::DBservers ) ) ) {
170 $lbConf[
'servers'] = [];
171 foreach ( $this->options->get( MainConfigNames::DBservers ) as $i => $server ) {
172 $lbConf[
'servers'][$i] = self::initServerInfo( $server, $this->options );
175 $server = self::initServerInfo(
177 'host' => $this->options->get( MainConfigNames::DBserver ),
178 'user' => $this->options->get( MainConfigNames::DBuser ),
179 'password' => $this->options->get( MainConfigNames::DBpassword ),
180 'dbname' => $this->options->get( MainConfigNames::DBname ),
181 'type' => $this->options->get( MainConfigNames::DBtype ),
187 if ( $this->options->get( MainConfigNames::DBssl ) ) {
188 $server[
'ssl'] =
true;
190 $server[
'flags'] |= $this->options->get( MainConfigNames::DBcompress ) ?
DBO_COMPRESS : 0;
192 $lbConf[
'servers'] = [ $server ];
194 if ( !isset( $lbConf[
'externalClusters'] ) ) {
195 $lbConf[
'externalClusters'] = $this->options->get( MainConfigNames::ExternalServers );
198 $serversCheck = $lbConf[
'servers'];
199 } elseif ( $lbConf[
'class'] === Wikimedia\Rdbms\LBFactoryMulti::class ) {
200 if ( isset( $lbConf[
'serverTemplate'] ) ) {
201 if ( in_array( $lbConf[
'serverTemplate'][
'type'], $typesWithSchema,
true ) ) {
202 $lbConf[
'serverTemplate'][
'schema'] = $this->options->get( MainConfigNames::DBmwschema );
204 $lbConf[
'serverTemplate'][
'sqlMode'] = $this->options->get( MainConfigNames::SQLMode );
205 $serversCheck = [ $lbConf[
'serverTemplate'] ];
209 self::assertValidServerConfigs(
211 $this->options->get( MainConfigNames::DBname ),
212 $this->options->get( MainConfigNames::DBprefix )
215 $lbConf[
'chronologyProtector'] = $this->chronologyProtector;
216 $lbConf[
'srvCache'] = $this->srvCache;
217 $lbConf[
'wanCache'] = $this->wanCache;
225 private function getDbTypesWithSchemas() {
226 return [
'postgres' ];
234 private function initServerInfo( array $server,
ServiceOptions $options ) {
235 if ( $server[
'type'] ===
'sqlite' ) {
236 $httpMethod = $_SERVER[
'REQUEST_METHOD'] ??
null;
240 $isHttpRead = in_array( $httpMethod, [
'GET',
'HEAD',
'OPTIONS',
'TRACE' ] );
245 if ( $request->hasHeader(
'Promise-Non-Write-API-Action' ) ) {
250 'dbDirectory' => $options->
get( MainConfigNames::SQLiteDataDir ),
251 'trxMode' => $isHttpRead ?
'DEFERRED' :
'IMMEDIATE'
253 } elseif ( $server[
'type'] ===
'postgres' ) {
254 $server += [
'port' => $options->
get( MainConfigNames::DBport ) ];
257 if ( in_array( $server[
'type'], self::getDbTypesWithSchemas(),
true ) ) {
258 $server += [
'schema' => $options->
get( MainConfigNames::DBmwschema ) ];
262 if ( $options->
get( MainConfigNames::DebugDumpSql )
263 || $options->
get( MainConfigNames::DebugLogFile )
264 || $options->
get( MainConfigNames::DebugToolbar )
268 $server[
'flags'] = $flags;
271 'tablePrefix' => $options->
get( MainConfigNames::DBprefix ),
272 'sqlMode' => $options->
get( MainConfigNames::SQLMode ),
283 private function assertValidServerConfigs( array $servers, $ldDB, $ldTP ) {
284 foreach ( $servers as $server ) {
285 $type = $server[
'type'] ??
null;
286 $srvDB = $server[
'dbname'] ??
null;
287 $srvTP = $server[
'tablePrefix'] ??
'';
289 if ( $type ===
'mysql' ) {
292 if ( $srvDB !==
null && $srvDB !== $ldDB ) {
293 self::reportMismatchedDBs( $srvDB, $ldDB );
295 } elseif ( $type ===
'postgres' ) {
296 if ( $srvTP !==
'' ) {
297 self::reportIfPrefixSet( $srvTP, $type );
301 if ( $srvTP !==
'' && $srvTP !== $ldTP ) {
302 self::reportMismatchedPrefixes( $srvTP, $ldTP );
312 private function reportIfPrefixSet( $prefix, $dbType ) {
313 $e =
new UnexpectedValueException(
314 "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
315 "MediaWiki does not support using a table prefix with this RDBMS type."
326 private function reportMismatchedDBs( $srvDB, $ldDB ) {
327 $e =
new UnexpectedValueException(
328 "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
329 "Set \$wgDBname to the database used by this wiki project. " .
330 "There is rarely a need to set 'dbname' in \$wgDBservers. " .
331 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
332 "use of Database::getDomainId(), and other features are not reliable when " .
333 "\$wgDBservers does not match the local wiki database/prefix."
344 private function reportMismatchedPrefixes( $srvTP, $ldTP ) {
345 $e =
new UnexpectedValueException(
346 "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
347 "Set \$wgDBprefix to the table prefix used by this wiki project. " .
348 "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
349 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
350 "use of Database::getDomainId(), and other features are not reliable when " .
351 "\$wgDBservers does not match the local wiki database/prefix."
367 'LBFactory_Single' => Wikimedia\Rdbms\LBFactorySingle::class,
368 'LBFactory_Simple' => Wikimedia\Rdbms\LBFactorySimple::class,
369 'LBFactory_Multi' => Wikimedia\Rdbms\LBFactoryMulti::class,
371 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
372 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
373 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
376 $class = $config[
'class'];
377 return $compat[$class] ?? $class;
387 $rawLocalDomain = strlen( $domain->getTablePrefix() )
388 ?
"{$domain->getDatabase()}-{$domain->getTablePrefix()}"
389 : (string)$domain->getDatabase();
422 if ( $config->get(
'CommandLineMode' ) ) {
423 $lbFactory->
getMainLB()->setTransactionListener(
425 static function ( $trigger ) use ( $stats, $config ) {
430 if ( $trigger === IDatabase::TRIGGER_COMMIT ) {
439 static function () use ( $stats, $config ) {
454 if ( isset( self::$loggedDeprecations[$msg] ) ) {
457 self::$loggedDeprecations[$msg] =
true;
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
if(!defined('MW_SETUP_CALLBACK'))
Class representing a cache/ephemeral data store.
static tryOpportunisticExecute()
Consume and execute pending updates now if possible, instead of waiting.
static sendRawDeprecated( $msg, $sendToLog=true, $callerFunc='')
Send a raw deprecation message to the log and the debug toolbar, without filtering of duplicate messa...
static output(Throwable $e, $mode, Throwable $eNew=null)
MediaWiki-specific class for generating database load balancers.
static logDeprecation( $msg)
Log a database deprecation warning.
const APPLY_DEFAULT_CONFIG_OPTIONS
setDomainAliases(ILBFactory $lbFactory)
applyDefaultConfig(array $lbConf)
applyGlobalState(ILBFactory $lbFactory, Config $config, IBufferingStatsdDataFactory $stats)
Apply global state from the current web request or other PHP process.
__construct(ServiceOptions $options, ConfiguredReadOnlyMode $readOnlyMode, ChronologyProtector $chronologyProtector, BagOStuff $srvCache, WANObjectCache $wanCache, CriticalSectionProvider $csProvider, StatsdDataFactoryInterface $statsdDataFactory)
getLBFactoryClass(array $config)
Decide which LBFactory class to use.
A class containing constants representing the names of configuration variables.
Stub profiler that does nothing.
static instance()
Singleton.
Multi-datacenter aware caching interface.
Class to handle database/schema/prefix specifications for IDatabase.
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.