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;
98 private $statsdDataFactory;
102 private $databaseFactory;
120 CriticalSectionProvider $csProvider,
121 StatsdDataFactoryInterface $statsdDataFactory,
124 $this->options = $options;
125 $this->readOnlyMode = $readOnlyMode;
126 $this->cpStash = $cpStash;
127 $this->srvCache = $srvCache;
128 $this->wanCache = $wanCache;
129 $this->csProvider = $csProvider;
130 $this->statsdDataFactory = $statsdDataFactory;
131 $this->databaseFactory = $databaseFactory;
140 $this->options->assertRequiredOptions( self::APPLY_DEFAULT_CONFIG_OPTIONS );
142 $typesWithSchema = self::getDbTypesWithSchemas();
146 $this->options->get( MainConfigNames::DBname ),
147 $this->options->get( MainConfigNames::DBmwschema ),
148 $this->options->get( MainConfigNames::DBprefix )
150 'profiler' =>
static function ( $section ) {
151 return Profiler::instance()->scopedProfileIn( $section );
153 'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
154 'logger' => LoggerFactory::getInstance(
'rdbms' ),
155 'errorLogger' => [ MWExceptionHandler::class,
'logException' ],
156 'deprecationLogger' => [ static::class,
'logDeprecation' ],
157 'statsdDataFactory' => $this->statsdDataFactory,
158 'cliMode' => $this->options->get(
'CommandLineMode' ),
159 'readOnlyReason' => $this->readOnlyMode->getReason(),
160 'defaultGroup' => $this->options->get( MainConfigNames::DBDefaultGroup ),
161 'criticalSectionProvider' => $this->csProvider
168 if ( $lbConf[
'class'] ===
Wikimedia\Rdbms\LBFactorySimple::class ) {
169 if ( isset( $lbConf[
'servers'] ) ) {
171 } elseif ( is_array( $this->options->get( MainConfigNames::DBservers ) ) ) {
172 $lbConf[
'servers'] = [];
173 foreach ( $this->options->get( MainConfigNames::DBservers ) as $i => $server ) {
174 $lbConf[
'servers'][$i] = self::initServerInfo( $server, $this->options );
177 $server = self::initServerInfo(
179 'host' => $this->options->get( MainConfigNames::DBserver ),
180 'user' => $this->options->get( MainConfigNames::DBuser ),
181 'password' => $this->options->get( MainConfigNames::DBpassword ),
182 'dbname' => $this->options->get( MainConfigNames::DBname ),
183 'type' => $this->options->get( MainConfigNames::DBtype ),
189 if ( $this->options->get( MainConfigNames::DBssl ) ) {
190 $server[
'ssl'] =
true;
192 $server[
'flags'] |= $this->options->get( MainConfigNames::DBcompress ) ?
DBO_COMPRESS : 0;
194 $lbConf[
'servers'] = [ $server ];
196 if ( !isset( $lbConf[
'externalClusters'] ) ) {
197 $lbConf[
'externalClusters'] = $this->options->get( MainConfigNames::ExternalServers );
200 $serversCheck = $lbConf[
'servers'];
201 } elseif ( $lbConf[
'class'] ===
Wikimedia\Rdbms\LBFactoryMulti::class ) {
202 if ( isset( $lbConf[
'serverTemplate'] ) ) {
203 if ( in_array( $lbConf[
'serverTemplate'][
'type'], $typesWithSchema,
true ) ) {
204 $lbConf[
'serverTemplate'][
'schema'] = $this->options->get( MainConfigNames::DBmwschema );
206 $lbConf[
'serverTemplate'][
'sqlMode'] = $this->options->get( MainConfigNames::SQLMode );
207 $serversCheck = [ $lbConf[
'serverTemplate'] ];
211 self::assertValidServerConfigs(
213 $this->options->get( MainConfigNames::DBname ),
214 $this->options->get( MainConfigNames::DBprefix )
217 $lbConf[
'cpStash'] = $this->cpStash;
218 $lbConf[
'srvCache'] = $this->srvCache;
219 $lbConf[
'wanCache'] = $this->wanCache;
220 $lbConf[
'databaseFactory'] = $this->databaseFactory;
228 private function getDbTypesWithSchemas() {
229 return [
'postgres' ];
237 private function initServerInfo( array $server,
ServiceOptions $options ) {
238 if ( $server[
'type'] ===
'sqlite' ) {
239 $httpMethod = $_SERVER[
'REQUEST_METHOD'] ??
null;
243 $isHttpRead = in_array( $httpMethod, [
'GET',
'HEAD',
'OPTIONS',
'TRACE' ] );
247 $request = \MediaWiki\Rest\EntryPoint::getMainRequest();
248 if ( $request->hasHeader(
'Promise-Non-Write-API-Action' ) ) {
253 'dbDirectory' => $options->
get( MainConfigNames::SQLiteDataDir ),
254 'trxMode' => $isHttpRead ?
'DEFERRED' :
'IMMEDIATE'
256 } elseif ( $server[
'type'] ===
'postgres' ) {
257 $server += [
'port' => $options->
get( MainConfigNames::DBport ) ];
260 if ( in_array( $server[
'type'], self::getDbTypesWithSchemas(),
true ) ) {
261 $server += [
'schema' => $options->
get( MainConfigNames::DBmwschema ) ];
265 if ( $options->
get( MainConfigNames::DebugDumpSql )
266 || $options->
get( MainConfigNames::DebugLogFile )
267 || $options->
get( MainConfigNames::DebugToolbar )
271 $server[
'flags'] = $flags;
274 'tablePrefix' => $options->
get( MainConfigNames::DBprefix ),
275 'sqlMode' => $options->
get( MainConfigNames::SQLMode ),
286 private function assertValidServerConfigs( array $servers, $ldDB, $ldTP ) {
287 foreach ( $servers as $server ) {
288 $type = $server[
'type'] ??
null;
289 $srvDB = $server[
'dbname'] ??
null;
290 $srvTP = $server[
'tablePrefix'] ??
'';
292 if (
$type ===
'mysql' ) {
295 if ( $srvDB !==
null && $srvDB !== $ldDB ) {
296 self::reportMismatchedDBs( $srvDB, $ldDB );
298 } elseif (
$type ===
'postgres' ) {
299 if ( $srvTP !==
'' ) {
300 self::reportIfPrefixSet( $srvTP,
$type );
304 if ( $srvTP !==
'' && $srvTP !== $ldTP ) {
305 self::reportMismatchedPrefixes( $srvTP, $ldTP );
315 private function reportIfPrefixSet( $prefix, $dbType ) {
316 $e =
new UnexpectedValueException(
317 "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
318 "MediaWiki does not support using a table prefix with this RDBMS type."
329 private function reportMismatchedDBs( $srvDB, $ldDB ) {
330 $e =
new UnexpectedValueException(
331 "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
332 "Set \$wgDBname to the database used by this wiki project. " .
333 "There is rarely a need to set 'dbname' in \$wgDBservers. " .
334 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
335 "use of Database::getDomainId(), and other features are not reliable when " .
336 "\$wgDBservers does not match the local wiki database/prefix."
347 private function reportMismatchedPrefixes( $srvTP, $ldTP ) {
348 $e =
new UnexpectedValueException(
349 "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
350 "Set \$wgDBprefix to the table prefix used by this wiki project. " .
351 "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
352 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
353 "use of Database::getDomainId(), and other features are not reliable when " .
354 "\$wgDBservers does not match the local wiki database/prefix."
370 'LBFactory_Single' => Wikimedia\Rdbms\LBFactorySingle::class,
371 'LBFactory_Simple' => Wikimedia\Rdbms\LBFactorySimple::class,
372 'LBFactory_Multi' => Wikimedia\Rdbms\LBFactoryMulti::class,
374 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
375 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
376 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
379 $class = $config[
'class'];
380 return $compat[$class] ?? $class;
390 $rawLocalDomain = strlen( $domain->getTablePrefix() )
391 ?
"{$domain->getDatabase()}-{$domain->getTablePrefix()}"
392 : (string)$domain->getDatabase();
434 $reqStart = (int)( $_SERVER[
'REQUEST_TIME_FLOAT'] ?? time() );
435 $cpPosInfo = LBFactory::getCPInfoFromCookieValue(
437 $req->getCookie(
'cpPosIndex',
'' ),
439 $reqStart - ChronologyProtector::POSITION_COOKIE_TTL
442 'IPAddress' => $req->getIP(),
443 'UserAgent' => $req->getHeader(
'User-Agent' ),
444 'ChronologyProtection' => $req->getHeader(
'MediaWiki-Chronology-Protection' ),
445 'ChronologyPositionIndex' => $req->getInt(
'cpPosIndex', $cpPosInfo[
'index'] ),
446 'ChronologyClientId' => $cpPosInfo[
'clientId']
447 ?? $req->getHeader(
'MediaWiki-Chronology-Client-Id' )
450 if ( $config->
get(
'CommandLineMode' ) ) {
456 static function () use ( $stats, $config ) {
457 DeferredUpdates::tryOpportunisticExecute();
459 MediaWiki::emitBufferedStatsdData( $stats, $config );
464 $lbFactory->
getMainLB()->setTransactionListener(
466 static function ( $trigger ) use ( $stats, $config ) {
467 if ( $trigger === IDatabase::TRIGGER_COMMIT ) {
468 DeferredUpdates::tryOpportunisticExecute();
484 if ( isset( self::$loggedDeprecations[$msg] ) ) {
487 self::$loggedDeprecations[$msg] =
true;
488 MWDebug::sendRawDeprecated( $msg,
true,
wfGetCaller() );
__construct(ServiceOptions $options, ConfiguredReadOnlyMode $readOnlyMode, BagOStuff $cpStash, BagOStuff $srvCache, WANObjectCache $wanCache, CriticalSectionProvider $csProvider, StatsdDataFactoryInterface $statsdDataFactory, DatabaseFactory $databaseFactory)
Group all the pieces relevant to the context of a request into one instance.