16use UnexpectedValueException;
28 private const array DB_TYPES_WITH_SCHEMAS = [
'postgres' ];
57 private array $lbConf;
67 $this->options = $options;
68 $this->lbConf = $this->applyConfig( $lbConf );
76 private function applyConfig( array $lbConf ): array {
91 if ( $lbConf[
'class'] === LBFactorySimple::class ) {
92 if ( isset( $lbConf[
'servers'] ) ) {
95 $lbConf[
'servers'] = [];
97 $lbConf[
'servers'][$i] = $this->initServerInfo( $server, $this->options );
100 $server = $this->initServerInfo(
113 $server[
'ssl'] =
true;
117 $server[
'strictWarnings'] =
true;
120 $lbConf[
'servers'] = [ $server ];
122 if ( !isset( $lbConf[
'externalClusters'] ) ) {
126 $serversCheck = $lbConf[
'servers'];
127 } elseif ( $lbConf[
'class'] === LBFactoryMulti::class ) {
128 if ( isset( $lbConf[
'serverTemplate'] ) ) {
129 if ( in_array( $lbConf[
'serverTemplate'][
'type'], self::DB_TYPES_WITH_SCHEMAS,
true ) ) {
133 $serversCheck = [ $lbConf[
'serverTemplate'] ];
137 $this->assertValidServerConfigs(
153 return $this->lbConf;
161 private function initServerInfo( array $server,
ServiceOptions $options ): array {
162 if ( $server[
'type'] ===
'sqlite' ) {
163 $httpMethod = $_SERVER[
'REQUEST_METHOD'] ??
null;
167 $isHttpRead = in_array( $httpMethod, [
'GET',
'HEAD',
'OPTIONS',
'TRACE' ] );
172 if (
$request->hasHeader(
'Promise-Non-Write-API-Action' ) ) {
178 'trxMode' => $isHttpRead ?
'DEFERRED' :
'IMMEDIATE'
180 } elseif ( $server[
'type'] ===
'postgres' ) {
184 if ( in_array( $server[
'type'], self::DB_TYPES_WITH_SCHEMAS,
true ) ) {
195 $server[
'flags'] = $flags;
210 private function assertValidServerConfigs( array $servers,
string $ldDB,
string $ldTP ): void {
211 foreach ( $servers as $server ) {
212 $type = $server[
'type'] ??
null;
213 $srvDB = $server[
'dbname'] ??
null;
214 $srvTP = $server[
'tablePrefix'] ??
'';
216 if ( $type ===
'mysql' ) {
219 if ( $srvDB !==
null && $srvDB !== $ldDB ) {
220 $this->reportMismatchedDBs( $srvDB, $ldDB );
222 } elseif ( $type ===
'postgres' ) {
223 if ( $srvTP !==
'' ) {
224 $this->reportIfPrefixSet( $srvTP, $type );
228 if ( $srvTP !==
'' && $srvTP !== $ldTP ) {
229 $this->reportMismatchedPrefixes( $srvTP, $ldTP );
239 private function reportIfPrefixSet(
string $prefix,
string $dbType ): never {
240 $e = new UnexpectedValueException(
241 "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
242 "MediaWiki does not support using a table prefix with this RDBMS type."
244 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_RAW );
253 private function reportMismatchedDBs(
string $srvDB,
string $ldDB ): never {
254 $e = new UnexpectedValueException(
255 "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
256 "Set \$wgDBname to the database used by this wiki project. " .
257 "There is rarely a need to set 'dbname' in \$wgDBservers. " .
258 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
259 "use of Database::getDomainId(), and other features are not reliable when " .
260 "\$wgDBservers does not match the local wiki database/prefix."
262 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_RAW );
271 private function reportMismatchedPrefixes(
string $srvTP,
string $ldTP ): never {
272 $e = new UnexpectedValueException(
273 "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
274 "Set \$wgDBprefix to the table prefix used by this wiki project. " .
275 "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
276 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
277 "use of Database::getDomainId(), and other features are not reliable when " .
278 "\$wgDBservers does not match the local wiki database/prefix."
280 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_RAW );
if(!defined('MW_SETUP_CALLBACK'))
A class containing constants representing the names of configuration variables.
const SQLMode
Name constant for the SQLMode setting, for use with Config::get()
const DBtype
Name constant for the DBtype setting, for use with Config::get()
const DBname
Name constant for the DBname setting, for use with Config::get()
const DBprefix
Name constant for the DBprefix setting, for use with Config::get()
const DBssl
Name constant for the DBssl setting, for use with Config::get()
const DBserver
Name constant for the DBserver setting, for use with Config::get()
const ExternalServers
Name constant for the ExternalServers setting, for use with Config::get()
const DebugToolbar
Name constant for the DebugToolbar setting, for use with Config::get()
const DBmwschema
Name constant for the DBmwschema setting, for use with Config::get()
const DBport
Name constant for the DBport setting, for use with Config::get()
const RemoteVirtualDomainsMapping
Name constant for the RemoteVirtualDomainsMapping setting, for use with Config::get()
const SQLiteDataDir
Name constant for the SQLiteDataDir setting, for use with Config::get()
const DebugDumpSql
Name constant for the DebugDumpSql setting, for use with Config::get()
const DBpassword
Name constant for the DBpassword setting, for use with Config::get()
const DBcompress
Name constant for the DBcompress setting, for use with Config::get()
const DebugLogFile
Name constant for the DebugLogFile setting, for use with Config::get()
const DBStrictWarnings
Name constant for the DBStrictWarnings setting, for use with Config::get()
const VirtualDomainsMapping
Name constant for the VirtualDomainsMapping setting, for use with Config::get()
const DBservers
Name constant for the DBservers setting, for use with Config::get()
const DBuser
Name constant for the DBuser setting, for use with Config::get()
Class to handle database/schema/prefix specifications for IDatabase.