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,
70 MainConfigNames::VirtualDomainsMapping,
79 private $readOnlyMode;
83 private $chronologyProtector;
99 private $statsdDataFactory;
100 private array $virtualDomains = [];
117 CriticalSectionProvider $csProvider,
118 StatsdDataFactoryInterface $statsdDataFactory,
119 array $virtualDomains
121 $this->options = $options;
122 $this->readOnlyMode = $readOnlyMode;
123 $this->chronologyProtector = $chronologyProtector;
124 $this->srvCache = $srvCache;
125 $this->wanCache = $wanCache;
126 $this->csProvider = $csProvider;
127 $this->statsdDataFactory = $statsdDataFactory;
128 $this->virtualDomains = $virtualDomains;
137 $this->options->assertRequiredOptions( self::APPLY_DEFAULT_CONFIG_OPTIONS );
139 $typesWithSchema = self::getDbTypesWithSchemas();
141 $profilerCallback =
null;
143 $profilerCallback =
static function ( $section ) {
150 $this->options->get( MainConfigNames::DBname ),
151 $this->options->get( MainConfigNames::DBmwschema ),
152 $this->options->get( MainConfigNames::DBprefix )
154 'profiler' => $profilerCallback,
156 'logger' => LoggerFactory::getInstance(
'rdbms' ),
157 'errorLogger' => [ MWExceptionHandler::class,
'logException' ],
158 'deprecationLogger' => [ static::class,
'logDeprecation' ],
159 'statsdDataFactory' => $this->statsdDataFactory,
160 'cliMode' => $this->options->get(
'CommandLineMode' ),
161 'readOnlyReason' => $this->readOnlyMode->getReason(),
162 'defaultGroup' => $this->options->get( MainConfigNames::DBDefaultGroup ),
163 'criticalSectionProvider' => $this->csProvider
170 if ( $lbConf[
'class'] ===
Wikimedia\Rdbms\LBFactorySimple::class ) {
171 if ( isset( $lbConf[
'servers'] ) ) {
173 } elseif ( is_array( $this->options->get( MainConfigNames::DBservers ) ) ) {
174 $lbConf[
'servers'] = [];
175 foreach ( $this->options->get( MainConfigNames::DBservers ) as $i => $server ) {
176 $lbConf[
'servers'][$i] = self::initServerInfo( $server, $this->options );
179 $server = self::initServerInfo(
181 'host' => $this->options->get( MainConfigNames::DBserver ),
182 'user' => $this->options->get( MainConfigNames::DBuser ),
183 'password' => $this->options->get( MainConfigNames::DBpassword ),
184 'dbname' => $this->options->get( MainConfigNames::DBname ),
185 'type' => $this->options->get( MainConfigNames::DBtype ),
191 if ( $this->options->get( MainConfigNames::DBssl ) ) {
192 $server[
'ssl'] =
true;
194 $server[
'flags'] |= $this->options->get( MainConfigNames::DBcompress ) ?
DBO_COMPRESS : 0;
196 $lbConf[
'servers'] = [ $server ];
198 if ( !isset( $lbConf[
'externalClusters'] ) ) {
199 $lbConf[
'externalClusters'] = $this->options->get( MainConfigNames::ExternalServers );
202 $serversCheck = $lbConf[
'servers'];
203 } elseif ( $lbConf[
'class'] ===
Wikimedia\Rdbms\LBFactoryMulti::class ) {
204 if ( isset( $lbConf[
'serverTemplate'] ) ) {
205 if ( in_array( $lbConf[
'serverTemplate'][
'type'], $typesWithSchema,
true ) ) {
206 $lbConf[
'serverTemplate'][
'schema'] = $this->options->get( MainConfigNames::DBmwschema );
208 $lbConf[
'serverTemplate'][
'sqlMode'] = $this->options->get( MainConfigNames::SQLMode );
209 $serversCheck = [ $lbConf[
'serverTemplate'] ];
213 self::assertValidServerConfigs(
215 $this->options->get( MainConfigNames::DBname ),
216 $this->options->get( MainConfigNames::DBprefix )
219 $lbConf[
'chronologyProtector'] = $this->chronologyProtector;
220 $lbConf[
'srvCache'] = $this->srvCache;
221 $lbConf[
'wanCache'] = $this->wanCache;
222 $lbConf[
'virtualDomains'] = $this->virtualDomains;
223 $lbConf[
'virtualDomainsMapping'] = $this->options->get( MainConfigNames::VirtualDomainsMapping );
231 private function getDbTypesWithSchemas() {
232 return [
'postgres' ];
240 private function initServerInfo( array $server,
ServiceOptions $options ) {
241 if ( $server[
'type'] ===
'sqlite' ) {
242 $httpMethod = $_SERVER[
'REQUEST_METHOD'] ??
null;
246 $isHttpRead = in_array( $httpMethod, [
'GET',
'HEAD',
'OPTIONS',
'TRACE' ] );
250 $request = \MediaWiki\Rest\EntryPoint::getMainRequest();
251 if ( $request->hasHeader(
'Promise-Non-Write-API-Action' ) ) {
256 'dbDirectory' => $options->
get( MainConfigNames::SQLiteDataDir ),
257 'trxMode' => $isHttpRead ?
'DEFERRED' :
'IMMEDIATE'
259 } elseif ( $server[
'type'] ===
'postgres' ) {
260 $server += [
'port' => $options->
get( MainConfigNames::DBport ) ];
263 if ( in_array( $server[
'type'], self::getDbTypesWithSchemas(),
true ) ) {
264 $server += [
'schema' => $options->
get( MainConfigNames::DBmwschema ) ];
268 if ( $options->
get( MainConfigNames::DebugDumpSql )
269 || $options->
get( MainConfigNames::DebugLogFile )
270 || $options->
get( MainConfigNames::DebugToolbar )
274 $server[
'flags'] = $flags;
277 'tablePrefix' => $options->
get( MainConfigNames::DBprefix ),
278 'sqlMode' => $options->
get( MainConfigNames::SQLMode ),
289 private function assertValidServerConfigs( array $servers, $ldDB, $ldTP ) {
290 foreach ( $servers as $server ) {
291 $type = $server[
'type'] ??
null;
292 $srvDB = $server[
'dbname'] ??
null;
293 $srvTP = $server[
'tablePrefix'] ??
'';
295 if ( $type ===
'mysql' ) {
298 if ( $srvDB !==
null && $srvDB !== $ldDB ) {
299 self::reportMismatchedDBs( $srvDB, $ldDB );
301 } elseif ( $type ===
'postgres' ) {
302 if ( $srvTP !==
'' ) {
303 self::reportIfPrefixSet( $srvTP, $type );
307 if ( $srvTP !==
'' && $srvTP !== $ldTP ) {
308 self::reportMismatchedPrefixes( $srvTP, $ldTP );
318 private function reportIfPrefixSet( $prefix, $dbType ) {
319 $e =
new UnexpectedValueException(
320 "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
321 "MediaWiki does not support using a table prefix with this RDBMS type."
332 private function reportMismatchedDBs( $srvDB, $ldDB ) {
333 $e =
new UnexpectedValueException(
334 "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
335 "Set \$wgDBname to the database used by this wiki project. " .
336 "There is rarely a need to set 'dbname' in \$wgDBservers. " .
337 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
338 "use of Database::getDomainId(), and other features are not reliable when " .
339 "\$wgDBservers does not match the local wiki database/prefix."
350 private function reportMismatchedPrefixes( $srvTP, $ldTP ) {
351 $e =
new UnexpectedValueException(
352 "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
353 "Set \$wgDBprefix to the table prefix used by this wiki project. " .
354 "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
355 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
356 "use of Database::getDomainId(), and other features are not reliable when " .
357 "\$wgDBservers does not match the local wiki database/prefix."
373 'LBFactory_Single' => Wikimedia\Rdbms\LBFactorySingle::class,
374 'LBFactory_Simple' => Wikimedia\Rdbms\LBFactorySimple::class,
375 'LBFactory_Multi' => Wikimedia\Rdbms\LBFactoryMulti::class,
377 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
378 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
379 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
382 $class = $config[
'class'];
383 return $compat[$class] ?? $class;
393 $rawLocalDomain = strlen( $domain->getTablePrefix() )
394 ?
"{$domain->getDatabase()}-{$domain->getTablePrefix()}"
395 : (string)$domain->getDatabase();
428 if ( $config->get(
'CommandLineMode' ) ) {
429 $lbFactory->
getMainLB()->setTransactionListener(
431 static function ( $trigger ) use ( $stats, $config ) {
436 if ( $trigger === IDatabase::TRIGGER_COMMIT ) {
437 DeferredUpdates::tryOpportunisticExecute();
440 MediaWiki::emitBufferedStatsdData( $stats, $config );
445 static function () use ( $stats, $config ) {
447 MediaWiki::emitBufferedStatsdData( $stats, $config );
460 if ( isset( self::$loggedDeprecations[$msg] ) ) {
463 self::$loggedDeprecations[$msg] =
true;
464 MWDebug::sendRawDeprecated( $msg,
true,
wfGetCaller() );
__construct(ServiceOptions $options, ConfiguredReadOnlyMode $readOnlyMode, ChronologyProtector $chronologyProtector, BagOStuff $srvCache, WANObjectCache $wanCache, CriticalSectionProvider $csProvider, StatsdDataFactoryInterface $statsdDataFactory, array $virtualDomains)