47 private static $loggedDeprecations = [];
55 MainConfigNames::DBcompress,
56 MainConfigNames::DBDefaultGroup,
57 MainConfigNames::DBmwschema,
58 MainConfigNames::DBname,
59 MainConfigNames::DBpassword,
60 MainConfigNames::DBport,
61 MainConfigNames::DBprefix,
62 MainConfigNames::DBserver,
63 MainConfigNames::DBservers,
64 MainConfigNames::DBssl,
65 MainConfigNames::DBStrictWarnings,
66 MainConfigNames::DBtype,
67 MainConfigNames::DBuser,
68 MainConfigNames::DebugDumpSql,
69 MainConfigNames::DebugLogFile,
70 MainConfigNames::DebugToolbar,
71 MainConfigNames::ExternalServers,
72 MainConfigNames::SQLiteDataDir,
73 MainConfigNames::SQLMode,
74 MainConfigNames::VirtualDomainsMapping,
83 private $readOnlyMode;
87 private $chronologyProtector;
103 private $statsdDataFactory;
105 private array $virtualDomains;
123 CriticalSectionProvider $csProvider,
124 StatsdDataFactoryInterface $statsdDataFactory,
125 array $virtualDomains
127 $this->options = $options;
128 $this->readOnlyMode = $readOnlyMode;
129 $this->chronologyProtector = $chronologyProtector;
130 $this->srvCache = $srvCache;
131 $this->wanCache = $wanCache;
132 $this->csProvider = $csProvider;
133 $this->statsdDataFactory = $statsdDataFactory;
134 $this->virtualDomains = $virtualDomains;
143 $this->options->assertRequiredOptions( self::APPLY_DEFAULT_CONFIG_OPTIONS );
145 $typesWithSchema = self::getDbTypesWithSchemas();
147 $profilerCallback =
null;
149 $profilerCallback =
static function ( $section ) {
156 $this->options->get( MainConfigNames::DBname ),
157 $this->options->get( MainConfigNames::DBmwschema ),
158 $this->options->get( MainConfigNames::DBprefix )
160 'profiler' => $profilerCallback,
162 'logger' => LoggerFactory::getInstance(
'rdbms' ),
163 'errorLogger' => [ MWExceptionHandler::class,
'logException' ],
164 'deprecationLogger' => [ static::class,
'logDeprecation' ],
165 'statsdDataFactory' => $this->statsdDataFactory,
167 'readOnlyReason' => $this->readOnlyMode->getReason(),
168 'defaultGroup' => $this->options->get( MainConfigNames::DBDefaultGroup ),
169 'criticalSectionProvider' => $this->csProvider
176 if ( $lbConf[
'class'] ===
Wikimedia\Rdbms\LBFactorySimple::class ) {
177 if ( isset( $lbConf[
'servers'] ) ) {
179 } elseif ( is_array( $this->options->get( MainConfigNames::DBservers ) ) ) {
180 $lbConf[
'servers'] = [];
181 foreach ( $this->options->get( MainConfigNames::DBservers ) as $i => $server ) {
182 $lbConf[
'servers'][$i] = self::initServerInfo( $server, $this->options );
185 $server = self::initServerInfo(
187 'host' => $this->options->get( MainConfigNames::DBserver ),
188 'user' => $this->options->get( MainConfigNames::DBuser ),
189 'password' => $this->options->get( MainConfigNames::DBpassword ),
190 'dbname' => $this->options->get( MainConfigNames::DBname ),
191 'type' => $this->options->get( MainConfigNames::DBtype ),
197 if ( $this->options->get( MainConfigNames::DBssl ) ) {
198 $server[
'ssl'] =
true;
200 $server[
'flags'] |= $this->options->get( MainConfigNames::DBcompress ) ?
DBO_COMPRESS : 0;
201 if ( $this->options->get( MainConfigNames::DBStrictWarnings ) ) {
202 $server[
'strictWarnings'] =
true;
205 $lbConf[
'servers'] = [ $server ];
207 if ( !isset( $lbConf[
'externalClusters'] ) ) {
208 $lbConf[
'externalClusters'] = $this->options->get( MainConfigNames::ExternalServers );
211 $serversCheck = $lbConf[
'servers'];
212 } elseif ( $lbConf[
'class'] ===
Wikimedia\Rdbms\LBFactoryMulti::class ) {
213 if ( isset( $lbConf[
'serverTemplate'] ) ) {
214 if ( in_array( $lbConf[
'serverTemplate'][
'type'], $typesWithSchema,
true ) ) {
215 $lbConf[
'serverTemplate'][
'schema'] = $this->options->get( MainConfigNames::DBmwschema );
217 $lbConf[
'serverTemplate'][
'sqlMode'] = $this->options->get( MainConfigNames::SQLMode );
218 $serversCheck = [ $lbConf[
'serverTemplate'] ];
222 self::assertValidServerConfigs(
224 $this->options->get( MainConfigNames::DBname ),
225 $this->options->get( MainConfigNames::DBprefix )
228 $lbConf[
'chronologyProtector'] = $this->chronologyProtector;
229 $lbConf[
'srvCache'] = $this->srvCache;
230 $lbConf[
'wanCache'] = $this->wanCache;
231 $lbConf[
'virtualDomains'] = array_merge( $this->virtualDomains, self::CORE_VIRTUAL_DOMAINS );
232 $lbConf[
'virtualDomainsMapping'] = $this->options->get( MainConfigNames::VirtualDomainsMapping );
240 private function getDbTypesWithSchemas() {
241 return [
'postgres' ];
249 private function initServerInfo( array $server,
ServiceOptions $options ) {
250 if ( $server[
'type'] ===
'sqlite' ) {
251 $httpMethod = $_SERVER[
'REQUEST_METHOD'] ??
null;
255 $isHttpRead = in_array( $httpMethod, [
'GET',
'HEAD',
'OPTIONS',
'TRACE' ] );
259 $request = \MediaWiki\Rest\EntryPoint::getMainRequest();
260 if ( $request->hasHeader(
'Promise-Non-Write-API-Action' ) ) {
265 'dbDirectory' => $options->
get( MainConfigNames::SQLiteDataDir ),
266 'trxMode' => $isHttpRead ?
'DEFERRED' :
'IMMEDIATE'
268 } elseif ( $server[
'type'] ===
'postgres' ) {
269 $server += [
'port' => $options->
get( MainConfigNames::DBport ) ];
272 if ( in_array( $server[
'type'], self::getDbTypesWithSchemas(),
true ) ) {
273 $server += [
'schema' => $options->
get( MainConfigNames::DBmwschema ) ];
277 if ( $options->
get( MainConfigNames::DebugDumpSql )
278 || $options->
get( MainConfigNames::DebugLogFile )
279 || $options->
get( MainConfigNames::DebugToolbar )
283 $server[
'flags'] = $flags;
286 'tablePrefix' => $options->
get( MainConfigNames::DBprefix ),
287 'sqlMode' => $options->
get( MainConfigNames::SQLMode ),
298 private function assertValidServerConfigs( array $servers, $ldDB, $ldTP ) {
299 foreach ( $servers as $server ) {
300 $type = $server[
'type'] ??
null;
301 $srvDB = $server[
'dbname'] ??
null;
302 $srvTP = $server[
'tablePrefix'] ??
'';
304 if ( $type ===
'mysql' ) {
307 if ( $srvDB !==
null && $srvDB !== $ldDB ) {
308 self::reportMismatchedDBs( $srvDB, $ldDB );
310 } elseif ( $type ===
'postgres' ) {
311 if ( $srvTP !==
'' ) {
312 self::reportIfPrefixSet( $srvTP, $type );
316 if ( $srvTP !==
'' && $srvTP !== $ldTP ) {
317 self::reportMismatchedPrefixes( $srvTP, $ldTP );
327 private function reportIfPrefixSet( $prefix, $dbType ) {
328 $e =
new UnexpectedValueException(
329 "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
330 "MediaWiki does not support using a table prefix with this RDBMS type."
341 private function reportMismatchedDBs( $srvDB, $ldDB ) {
342 $e =
new UnexpectedValueException(
343 "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
344 "Set \$wgDBname to the database used by this wiki project. " .
345 "There is rarely a need to set 'dbname' in \$wgDBservers. " .
346 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
347 "use of Database::getDomainId(), and other features are not reliable when " .
348 "\$wgDBservers does not match the local wiki database/prefix."
359 private function reportMismatchedPrefixes( $srvTP, $ldTP ) {
360 $e =
new UnexpectedValueException(
361 "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
362 "Set \$wgDBprefix to the table prefix used by this wiki project. " .
363 "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
364 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
365 "use of Database::getDomainId(), and other features are not reliable when " .
366 "\$wgDBservers does not match the local wiki database/prefix."
382 'LBFactory_Single' => Wikimedia\Rdbms\LBFactorySingle::class,
383 'LBFactory_Simple' => Wikimedia\Rdbms\LBFactorySimple::class,
384 'LBFactory_Multi' => Wikimedia\Rdbms\LBFactoryMulti::class,
386 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
387 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
388 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
391 $class = $config[
'class'];
392 return $compat[$class] ?? $class;
402 $rawLocalDomain = strlen( $domain->getTablePrefix() )
403 ?
"{$domain->getDatabase()}-{$domain->getTablePrefix()}"
404 : (string)$domain->getDatabase();
438 $lbFactory->
getMainLB()->setTransactionListener(
440 static function ( $trigger ) use ( $stats, $config ) {
445 if ( $trigger === IDatabase::TRIGGER_COMMIT ) {
446 DeferredUpdates::tryOpportunisticExecute();
449 MediaWiki::emitBufferedStatsdData( $stats, $config );
454 static function () use ( $stats, $config ) {
456 MediaWiki::emitBufferedStatsdData( $stats, $config );
469 if ( isset( self::$loggedDeprecations[$msg] ) ) {
472 self::$loggedDeprecations[$msg] =
true;
473 MWDebug::sendRawDeprecated( $msg,
true,
wfGetCaller() );
__construct(ServiceOptions $options, ConfiguredReadOnlyMode $readOnlyMode, ChronologyProtector $chronologyProtector, BagOStuff $srvCache, WANObjectCache $wanCache, CriticalSectionProvider $csProvider, StatsdDataFactoryInterface $statsdDataFactory, array $virtualDomains)