48 private static $loggedDeprecations = [];
56 MainConfigNames::DBcompress,
57 MainConfigNames::DBDefaultGroup,
58 MainConfigNames::DBmwschema,
59 MainConfigNames::DBname,
60 MainConfigNames::DBpassword,
61 MainConfigNames::DBport,
62 MainConfigNames::DBprefix,
63 MainConfigNames::DBserver,
64 MainConfigNames::DBservers,
65 MainConfigNames::DBssl,
66 MainConfigNames::DBStrictWarnings,
67 MainConfigNames::DBtype,
68 MainConfigNames::DBuser,
69 MainConfigNames::DebugDumpSql,
70 MainConfigNames::DebugLogFile,
71 MainConfigNames::DebugToolbar,
72 MainConfigNames::ExternalServers,
73 MainConfigNames::SQLiteDataDir,
74 MainConfigNames::SQLMode,
75 MainConfigNames::VirtualDomainsMapping,
84 private $readOnlyMode;
88 private $chronologyProtector;
104 private $statsdDataFactory;
106 private array $virtualDomains;
124 CriticalSectionProvider $csProvider,
125 StatsdDataFactoryInterface $statsdDataFactory,
126 array $virtualDomains
128 $this->options = $options;
129 $this->readOnlyMode = $readOnlyMode;
130 $this->chronologyProtector = $chronologyProtector;
131 $this->srvCache = $srvCache;
132 $this->wanCache = $wanCache;
133 $this->csProvider = $csProvider;
134 $this->statsdDataFactory = $statsdDataFactory;
135 $this->virtualDomains = $virtualDomains;
144 $this->options->assertRequiredOptions( self::APPLY_DEFAULT_CONFIG_OPTIONS );
146 $typesWithSchema = self::getDbTypesWithSchemas();
148 $profilerCallback =
null;
150 $profilerCallback =
static function ( $section ) {
157 $this->options->get( MainConfigNames::DBname ),
158 $this->options->get( MainConfigNames::DBmwschema ),
159 $this->options->get( MainConfigNames::DBprefix )
161 'profiler' => $profilerCallback,
163 'logger' => LoggerFactory::getInstance(
'rdbms' ),
164 'errorLogger' => [ MWExceptionHandler::class,
'logException' ],
165 'deprecationLogger' => [ static::class,
'logDeprecation' ],
166 'statsdDataFactory' => $this->statsdDataFactory,
168 'readOnlyReason' => $this->readOnlyMode->getReason(),
169 'defaultGroup' => $this->options->get( MainConfigNames::DBDefaultGroup ),
170 'criticalSectionProvider' => $this->csProvider
177 if ( $lbConf[
'class'] ===
Wikimedia\Rdbms\LBFactorySimple::class ) {
178 if ( isset( $lbConf[
'servers'] ) ) {
180 } elseif ( is_array( $this->options->get( MainConfigNames::DBservers ) ) ) {
181 $lbConf[
'servers'] = [];
182 foreach ( $this->options->get( MainConfigNames::DBservers ) as $i => $server ) {
183 $lbConf[
'servers'][$i] = self::initServerInfo( $server, $this->options );
186 $server = self::initServerInfo(
188 'host' => $this->options->get( MainConfigNames::DBserver ),
189 'user' => $this->options->get( MainConfigNames::DBuser ),
190 'password' => $this->options->get( MainConfigNames::DBpassword ),
191 'dbname' => $this->options->get( MainConfigNames::DBname ),
192 'type' => $this->options->get( MainConfigNames::DBtype ),
198 if ( $this->options->get( MainConfigNames::DBssl ) ) {
199 $server[
'ssl'] =
true;
201 $server[
'flags'] |= $this->options->get( MainConfigNames::DBcompress ) ?
DBO_COMPRESS : 0;
202 if ( $this->options->get( MainConfigNames::DBStrictWarnings ) ) {
203 $server[
'strictWarnings'] =
true;
206 $lbConf[
'servers'] = [ $server ];
208 if ( !isset( $lbConf[
'externalClusters'] ) ) {
209 $lbConf[
'externalClusters'] = $this->options->get( MainConfigNames::ExternalServers );
212 $serversCheck = $lbConf[
'servers'];
213 } elseif ( $lbConf[
'class'] ===
Wikimedia\Rdbms\LBFactoryMulti::class ) {
214 if ( isset( $lbConf[
'serverTemplate'] ) ) {
215 if ( in_array( $lbConf[
'serverTemplate'][
'type'], $typesWithSchema,
true ) ) {
216 $lbConf[
'serverTemplate'][
'schema'] = $this->options->get( MainConfigNames::DBmwschema );
218 $lbConf[
'serverTemplate'][
'sqlMode'] = $this->options->get( MainConfigNames::SQLMode );
219 $serversCheck = [ $lbConf[
'serverTemplate'] ];
223 self::assertValidServerConfigs(
225 $this->options->get( MainConfigNames::DBname ),
226 $this->options->get( MainConfigNames::DBprefix )
229 $lbConf[
'chronologyProtector'] = $this->chronologyProtector;
230 $lbConf[
'srvCache'] = $this->srvCache;
231 $lbConf[
'wanCache'] = $this->wanCache;
232 $lbConf[
'virtualDomains'] = array_merge( $this->virtualDomains, self::CORE_VIRTUAL_DOMAINS );
233 $lbConf[
'virtualDomainsMapping'] = $this->options->get( MainConfigNames::VirtualDomainsMapping );
241 private function getDbTypesWithSchemas() {
242 return [
'postgres' ];
250 private function initServerInfo( array $server,
ServiceOptions $options ) {
251 if ( $server[
'type'] ===
'sqlite' ) {
252 $httpMethod = $_SERVER[
'REQUEST_METHOD'] ??
null;
256 $isHttpRead = in_array( $httpMethod, [
'GET',
'HEAD',
'OPTIONS',
'TRACE' ] );
260 $request = \MediaWiki\Rest\EntryPoint::getMainRequest();
261 if ( $request->hasHeader(
'Promise-Non-Write-API-Action' ) ) {
266 'dbDirectory' => $options->
get( MainConfigNames::SQLiteDataDir ),
267 'trxMode' => $isHttpRead ?
'DEFERRED' :
'IMMEDIATE'
269 } elseif ( $server[
'type'] ===
'postgres' ) {
270 $server += [
'port' => $options->
get( MainConfigNames::DBport ) ];
273 if ( in_array( $server[
'type'], self::getDbTypesWithSchemas(),
true ) ) {
274 $server += [
'schema' => $options->
get( MainConfigNames::DBmwschema ) ];
278 if ( $options->
get( MainConfigNames::DebugDumpSql )
279 || $options->
get( MainConfigNames::DebugLogFile )
280 || $options->
get( MainConfigNames::DebugToolbar )
284 $server[
'flags'] = $flags;
287 'tablePrefix' => $options->
get( MainConfigNames::DBprefix ),
288 'sqlMode' => $options->
get( MainConfigNames::SQLMode ),
299 private function assertValidServerConfigs( array $servers, $ldDB, $ldTP ) {
300 foreach ( $servers as $server ) {
301 $type = $server[
'type'] ??
null;
302 $srvDB = $server[
'dbname'] ??
null;
303 $srvTP = $server[
'tablePrefix'] ??
'';
305 if ( $type ===
'mysql' ) {
308 if ( $srvDB !==
null && $srvDB !== $ldDB ) {
309 self::reportMismatchedDBs( $srvDB, $ldDB );
311 } elseif ( $type ===
'postgres' ) {
312 if ( $srvTP !==
'' ) {
313 self::reportIfPrefixSet( $srvTP, $type );
317 if ( $srvTP !==
'' && $srvTP !== $ldTP ) {
318 self::reportMismatchedPrefixes( $srvTP, $ldTP );
328 private function reportIfPrefixSet( $prefix, $dbType ) {
329 $e =
new UnexpectedValueException(
330 "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
331 "MediaWiki does not support using a table prefix with this RDBMS type."
342 private function reportMismatchedDBs( $srvDB, $ldDB ) {
343 $e =
new UnexpectedValueException(
344 "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
345 "Set \$wgDBname to the database used by this wiki project. " .
346 "There is rarely a need to set 'dbname' in \$wgDBservers. " .
347 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
348 "use of Database::getDomainId(), and other features are not reliable when " .
349 "\$wgDBservers does not match the local wiki database/prefix."
360 private function reportMismatchedPrefixes( $srvTP, $ldTP ) {
361 $e =
new UnexpectedValueException(
362 "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
363 "Set \$wgDBprefix to the table prefix used by this wiki project. " .
364 "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
365 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
366 "use of Database::getDomainId(), and other features are not reliable when " .
367 "\$wgDBservers does not match the local wiki database/prefix."
383 'LBFactory_Single' => Wikimedia\Rdbms\LBFactorySingle::class,
384 'LBFactory_Simple' => Wikimedia\Rdbms\LBFactorySimple::class,
385 'LBFactory_Multi' => Wikimedia\Rdbms\LBFactoryMulti::class,
387 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
388 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
389 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
392 $class = $config[
'class'];
393 return $compat[$class] ?? $class;
403 $rawLocalDomain = strlen( $domain->getTablePrefix() )
404 ?
"{$domain->getDatabase()}-{$domain->getTablePrefix()}"
405 : (string)$domain->getDatabase();
439 $lbFactory->
getMainLB()->setTransactionListener(
441 static function ( $trigger ) use ( $stats, $config ) {
446 if ( $trigger === IDatabase::TRIGGER_COMMIT ) {
447 DeferredUpdates::tryOpportunisticExecute();
450 MediaWiki::emitBufferedStatsdData( $stats, $config );
455 static function () use ( $stats, $config ) {
457 MediaWiki::emitBufferedStatsdData( $stats, $config );
470 if ( isset( self::$loggedDeprecations[$msg] ) ) {
473 self::$loggedDeprecations[$msg] =
true;
474 MWDebug::sendRawDeprecated( $msg,
true,
wfGetCaller() );
__construct(ServiceOptions $options, ConfiguredReadOnlyMode $readOnlyMode, ChronologyProtector $chronologyProtector, BagOStuff $srvCache, WANObjectCache $wanCache, CriticalSectionProvider $csProvider, StatsdDataFactoryInterface $statsdDataFactory, array $virtualDomains)