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 'replLogger' => LoggerFactory::getInstance(
'DBReplication' ),
155 'queryLogger' => LoggerFactory::getInstance(
'DBQuery' ),
156 'connLogger' => LoggerFactory::getInstance(
'DBConnection' ),
157 'perfLogger' => LoggerFactory::getInstance(
'DBPerformance' ),
158 'errorLogger' => [ MWExceptionHandler::class,
'logException' ],
159 'deprecationLogger' => [ static::class,
'logDeprecation' ],
160 'statsdDataFactory' => $this->statsdDataFactory,
161 'cliMode' => $this->options->get(
'CommandLineMode' ),
162 'readOnlyReason' => $this->readOnlyMode->getReason(),
163 'defaultGroup' => $this->options->get( MainConfigNames::DBDefaultGroup ),
164 'criticalSectionProvider' => $this->csProvider
171 if ( $lbConf[
'class'] ===
Wikimedia\Rdbms\LBFactorySimple::class ) {
172 if ( isset( $lbConf[
'servers'] ) ) {
174 } elseif ( is_array( $this->options->get( MainConfigNames::DBservers ) ) ) {
175 $lbConf[
'servers'] = [];
176 foreach ( $this->options->get( MainConfigNames::DBservers ) as $i => $server ) {
177 $lbConf[
'servers'][$i] = self::initServerInfo( $server, $this->options );
180 $server = self::initServerInfo(
182 'host' => $this->options->get( MainConfigNames::DBserver ),
183 'user' => $this->options->get( MainConfigNames::DBuser ),
184 'password' => $this->options->get( MainConfigNames::DBpassword ),
185 'dbname' => $this->options->get( MainConfigNames::DBname ),
186 'type' => $this->options->get( MainConfigNames::DBtype ),
192 if ( $this->options->get( MainConfigNames::DBssl ) ) {
193 $server[
'ssl'] =
true;
195 $server[
'flags'] |= $this->options->get( MainConfigNames::DBcompress ) ?
DBO_COMPRESS : 0;
197 $lbConf[
'servers'] = [ $server ];
199 if ( !isset( $lbConf[
'externalClusters'] ) ) {
200 $lbConf[
'externalClusters'] = $this->options->get( MainConfigNames::ExternalServers );
203 $serversCheck = $lbConf[
'servers'];
204 } elseif ( $lbConf[
'class'] ===
Wikimedia\Rdbms\LBFactoryMulti::class ) {
205 if ( isset( $lbConf[
'serverTemplate'] ) ) {
206 if ( in_array( $lbConf[
'serverTemplate'][
'type'], $typesWithSchema,
true ) ) {
207 $lbConf[
'serverTemplate'][
'schema'] = $this->options->get( MainConfigNames::DBmwschema );
209 $lbConf[
'serverTemplate'][
'sqlMode'] = $this->options->get( MainConfigNames::SQLMode );
210 $serversCheck = [ $lbConf[
'serverTemplate'] ];
214 self::assertValidServerConfigs(
216 $this->options->get( MainConfigNames::DBname ),
217 $this->options->get( MainConfigNames::DBprefix )
220 $lbConf[
'cpStash'] = $this->cpStash;
221 $lbConf[
'srvCache'] = $this->srvCache;
222 $lbConf[
'wanCache'] = $this->wanCache;
223 $lbConf[
'databaseFactory'] = $this->databaseFactory;
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();
437 $reqStart = (int)( $_SERVER[
'REQUEST_TIME_FLOAT'] ?? time() );
438 $cpPosInfo = LBFactory::getCPInfoFromCookieValue(
440 $req->getCookie(
'cpPosIndex',
'' ),
442 $reqStart - ChronologyProtector::POSITION_COOKIE_TTL
445 'IPAddress' => $req->getIP(),
446 'UserAgent' => $req->getHeader(
'User-Agent' ),
447 'ChronologyProtection' => $req->getHeader(
'MediaWiki-Chronology-Protection' ),
448 'ChronologyPositionIndex' => $req->getInt(
'cpPosIndex', $cpPosInfo[
'index'] ),
449 'ChronologyClientId' => $cpPosInfo[
'clientId']
450 ?? $req->getHeader(
'MediaWiki-Chronology-Client-Id' )
453 if ( $config->
get(
'CommandLineMode' ) ) {
459 static function () use ( $stats, $config ) {
460 DeferredUpdates::tryOpportunisticExecute();
462 MediaWiki::emitBufferedStatsdData( $stats, $config );
467 $lbFactory->
getMainLB()->setTransactionListener(
469 static function ( $trigger ) use ( $stats, $config ) {
470 if ( $trigger === IDatabase::TRIGGER_COMMIT ) {
471 DeferredUpdates::tryOpportunisticExecute();
487 if ( isset( self::$loggedDeprecations[$msg] ) ) {
490 self::$loggedDeprecations[$msg] =
true;
491 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.