46 private static $loggedDeprecations = [];
54 MainConfigNames::DBcompress,
55 MainConfigNames::DBDefaultGroup,
56 MainConfigNames::DBmwschema,
57 MainConfigNames::DBname,
58 MainConfigNames::DBpassword,
59 MainConfigNames::DBport,
60 MainConfigNames::DBprefix,
61 MainConfigNames::DBserver,
62 MainConfigNames::DBservers,
63 MainConfigNames::DBssl,
64 MainConfigNames::DBStrictWarnings,
65 MainConfigNames::DBtype,
66 MainConfigNames::DBuser,
67 MainConfigNames::DebugDumpSql,
68 MainConfigNames::DebugLogFile,
69 MainConfigNames::DebugToolbar,
70 MainConfigNames::ExternalServers,
71 MainConfigNames::SQLiteDataDir,
72 MainConfigNames::SQLMode,
73 MainConfigNames::VirtualDomainsMapping,
82 private $readOnlyMode;
86 private $chronologyProtector;
102 private $statsdDataFactory;
104 private array $virtualDomains;
122 CriticalSectionProvider $csProvider,
123 StatsdDataFactoryInterface $statsdDataFactory,
124 array $virtualDomains
126 $this->options = $options;
127 $this->readOnlyMode = $readOnlyMode;
128 $this->chronologyProtector = $chronologyProtector;
129 $this->srvCache = $srvCache;
130 $this->wanCache = $wanCache;
131 $this->csProvider = $csProvider;
132 $this->statsdDataFactory = $statsdDataFactory;
133 $this->virtualDomains = $virtualDomains;
142 $this->options->assertRequiredOptions( self::APPLY_DEFAULT_CONFIG_OPTIONS );
144 $typesWithSchema = self::getDbTypesWithSchemas();
146 $profilerCallback =
null;
148 $profilerCallback =
static function ( $section ) {
155 $this->options->get( MainConfigNames::DBname ),
156 $this->options->get( MainConfigNames::DBmwschema ),
157 $this->options->get( MainConfigNames::DBprefix )
159 'profiler' => $profilerCallback,
161 'logger' => LoggerFactory::getInstance(
'rdbms' ),
162 'errorLogger' => [ MWExceptionHandler::class,
'logException' ],
163 'deprecationLogger' => [ static::class,
'logDeprecation' ],
164 'statsdDataFactory' => $this->statsdDataFactory,
166 'readOnlyReason' => $this->readOnlyMode->getReason(),
167 'defaultGroup' => $this->options->get( MainConfigNames::DBDefaultGroup ),
168 'criticalSectionProvider' => $this->csProvider
175 if ( $lbConf[
'class'] ===
Wikimedia\Rdbms\LBFactorySimple::class ) {
176 if ( isset( $lbConf[
'servers'] ) ) {
178 } elseif ( is_array( $this->options->get( MainConfigNames::DBservers ) ) ) {
179 $lbConf[
'servers'] = [];
180 foreach ( $this->options->get( MainConfigNames::DBservers ) as $i => $server ) {
181 $lbConf[
'servers'][$i] = self::initServerInfo( $server, $this->options );
184 $server = self::initServerInfo(
186 'host' => $this->options->get( MainConfigNames::DBserver ),
187 'user' => $this->options->get( MainConfigNames::DBuser ),
188 'password' => $this->options->get( MainConfigNames::DBpassword ),
189 'dbname' => $this->options->get( MainConfigNames::DBname ),
190 'type' => $this->options->get( MainConfigNames::DBtype ),
196 if ( $this->options->get( MainConfigNames::DBssl ) ) {
197 $server[
'ssl'] =
true;
199 $server[
'flags'] |= $this->options->get( MainConfigNames::DBcompress ) ?
DBO_COMPRESS : 0;
200 if ( $this->options->get( MainConfigNames::DBStrictWarnings ) ) {
201 $server[
'strictWarnings'] =
true;
204 $lbConf[
'servers'] = [ $server ];
206 if ( !isset( $lbConf[
'externalClusters'] ) ) {
207 $lbConf[
'externalClusters'] = $this->options->get( MainConfigNames::ExternalServers );
210 $serversCheck = $lbConf[
'servers'];
211 } elseif ( $lbConf[
'class'] ===
Wikimedia\Rdbms\LBFactoryMulti::class ) {
212 if ( isset( $lbConf[
'serverTemplate'] ) ) {
213 if ( in_array( $lbConf[
'serverTemplate'][
'type'], $typesWithSchema,
true ) ) {
214 $lbConf[
'serverTemplate'][
'schema'] = $this->options->get( MainConfigNames::DBmwschema );
216 $lbConf[
'serverTemplate'][
'sqlMode'] = $this->options->get( MainConfigNames::SQLMode );
217 $serversCheck = [ $lbConf[
'serverTemplate'] ];
221 self::assertValidServerConfigs(
223 $this->options->get( MainConfigNames::DBname ),
224 $this->options->get( MainConfigNames::DBprefix )
227 $lbConf[
'chronologyProtector'] = $this->chronologyProtector;
228 $lbConf[
'srvCache'] = $this->srvCache;
229 $lbConf[
'wanCache'] = $this->wanCache;
230 $lbConf[
'virtualDomains'] = array_merge( $this->virtualDomains, self::CORE_VIRTUAL_DOMAINS );
231 $lbConf[
'virtualDomainsMapping'] = $this->options->get( MainConfigNames::VirtualDomainsMapping );
239 private function getDbTypesWithSchemas() {
240 return [
'postgres' ];
248 private function initServerInfo( array $server,
ServiceOptions $options ) {
249 if ( $server[
'type'] ===
'sqlite' ) {
250 $httpMethod = $_SERVER[
'REQUEST_METHOD'] ??
null;
254 $isHttpRead = in_array( $httpMethod, [
'GET',
'HEAD',
'OPTIONS',
'TRACE' ] );
258 $request = \MediaWiki\Rest\EntryPoint::getMainRequest();
259 if ( $request->hasHeader(
'Promise-Non-Write-API-Action' ) ) {
264 'dbDirectory' => $options->
get( MainConfigNames::SQLiteDataDir ),
265 'trxMode' => $isHttpRead ?
'DEFERRED' :
'IMMEDIATE'
267 } elseif ( $server[
'type'] ===
'postgres' ) {
268 $server += [
'port' => $options->
get( MainConfigNames::DBport ) ];
271 if ( in_array( $server[
'type'], self::getDbTypesWithSchemas(),
true ) ) {
272 $server += [
'schema' => $options->
get( MainConfigNames::DBmwschema ) ];
276 if ( $options->
get( MainConfigNames::DebugDumpSql )
277 || $options->
get( MainConfigNames::DebugLogFile )
278 || $options->
get( MainConfigNames::DebugToolbar )
282 $server[
'flags'] = $flags;
285 'tablePrefix' => $options->
get( MainConfigNames::DBprefix ),
286 'sqlMode' => $options->
get( MainConfigNames::SQLMode ),
297 private function assertValidServerConfigs( array $servers, $ldDB, $ldTP ) {
298 foreach ( $servers as $server ) {
299 $type = $server[
'type'] ??
null;
300 $srvDB = $server[
'dbname'] ??
null;
301 $srvTP = $server[
'tablePrefix'] ??
'';
303 if ( $type ===
'mysql' ) {
306 if ( $srvDB !==
null && $srvDB !== $ldDB ) {
307 self::reportMismatchedDBs( $srvDB, $ldDB );
309 } elseif ( $type ===
'postgres' ) {
310 if ( $srvTP !==
'' ) {
311 self::reportIfPrefixSet( $srvTP, $type );
315 if ( $srvTP !==
'' && $srvTP !== $ldTP ) {
316 self::reportMismatchedPrefixes( $srvTP, $ldTP );
326 private function reportIfPrefixSet( $prefix, $dbType ) {
327 $e =
new UnexpectedValueException(
328 "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
329 "MediaWiki does not support using a table prefix with this RDBMS type."
340 private function reportMismatchedDBs( $srvDB, $ldDB ) {
341 $e =
new UnexpectedValueException(
342 "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
343 "Set \$wgDBname to the database used by this wiki project. " .
344 "There is rarely a need to set 'dbname' in \$wgDBservers. " .
345 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
346 "use of Database::getDomainId(), and other features are not reliable when " .
347 "\$wgDBservers does not match the local wiki database/prefix."
358 private function reportMismatchedPrefixes( $srvTP, $ldTP ) {
359 $e =
new UnexpectedValueException(
360 "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
361 "Set \$wgDBprefix to the table prefix used by this wiki project. " .
362 "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
363 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
364 "use of Database::getDomainId(), and other features are not reliable when " .
365 "\$wgDBservers does not match the local wiki database/prefix."
381 'LBFactory_Single' => Wikimedia\Rdbms\LBFactorySingle::class,
382 'LBFactory_Simple' => Wikimedia\Rdbms\LBFactorySimple::class,
383 'LBFactory_Multi' => Wikimedia\Rdbms\LBFactoryMulti::class,
385 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
386 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
387 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
390 $class = $config[
'class'];
391 return $compat[$class] ?? $class;
401 $rawLocalDomain = strlen( $domain->getTablePrefix() )
402 ?
"{$domain->getDatabase()}-{$domain->getTablePrefix()}"
403 : (string)$domain->getDatabase();
437 $lbFactory->
getMainLB()->setTransactionListener(
439 static function ( $trigger ) use ( $stats, $config ) {
444 if ( $trigger === IDatabase::TRIGGER_COMMIT ) {
445 DeferredUpdates::tryOpportunisticExecute();
448 MediaWiki::emitBufferedStatsdData( $stats, $config );
453 static function () use ( $stats, $config ) {
455 MediaWiki::emitBufferedStatsdData( $stats, $config );
468 if ( isset( self::$loggedDeprecations[$msg] ) ) {
471 self::$loggedDeprecations[$msg] =
true;
472 MWDebug::sendRawDeprecated( $msg,
true,
wfGetCaller() );
__construct(ServiceOptions $options, ConfiguredReadOnlyMode $readOnlyMode, ChronologyProtector $chronologyProtector, BagOStuff $srvCache, WANObjectCache $wanCache, CriticalSectionProvider $csProvider, StatsdDataFactoryInterface $statsdDataFactory, array $virtualDomains)