MediaWiki master
MWLBConfig.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\DB;
11
16use UnexpectedValueException;
20
28 private const array DB_TYPES_WITH_SCHEMAS = [ 'postgres' ];
29
54 ];
55 private ServiceOptions $options;
56
57 private array $lbConf;
58
63 public function __construct(
64 ServiceOptions $options,
65 array $lbConf
66 ) {
67 $this->options = $options;
68 $this->lbConf = $this->applyConfig( $lbConf );
69 }
70
76 private function applyConfig( array $lbConf ): array {
77 $this->options->assertRequiredOptions( self::APPLY_DEFAULT_CONFIG_OPTIONS );
78
79 $lbConf += [
80 'localDomain' => new DatabaseDomain(
81 $this->options->get( MainConfigNames::DBname ),
82 $this->options->get( MainConfigNames::DBmwschema ),
83 $this->options->get( MainConfigNames::DBprefix )
84 ),
85 ];
86
87 $serversCheck = [];
88 // When making changes here, remember to also specify MediaWiki-specific options
89 // for Database classes in the relevant Installer subclass.
90 // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
91 if ( $lbConf['class'] === LBFactorySimple::class ) {
92 if ( isset( $lbConf['servers'] ) ) {
93 // Server array is already explicitly configured
94 } elseif ( is_array( $this->options->get( MainConfigNames::DBservers ) ) ) {
95 $lbConf['servers'] = [];
96 foreach ( $this->options->get( MainConfigNames::DBservers ) as $i => $server ) {
97 $lbConf['servers'][$i] = $this->initServerInfo( $server, $this->options );
98 }
99 } else {
100 $server = $this->initServerInfo(
101 [
102 'host' => $this->options->get( MainConfigNames::DBserver ),
103 'user' => $this->options->get( MainConfigNames::DBuser ),
104 'password' => $this->options->get( MainConfigNames::DBpassword ),
105 'dbname' => $this->options->get( MainConfigNames::DBname ),
106 'type' => $this->options->get( MainConfigNames::DBtype ),
107 'load' => 1
108 ],
109 $this->options
110 );
111
112 if ( $this->options->get( MainConfigNames::DBssl ) ) {
113 $server['ssl'] = true;
114 }
115 $server['flags'] |= $this->options->get( MainConfigNames::DBcompress ) ? DBO_COMPRESS : 0;
116 if ( $this->options->get( MainConfigNames::DBStrictWarnings ) ) {
117 $server['strictWarnings'] = true;
118 }
119
120 $lbConf['servers'] = [ $server ];
121 }
122 if ( !isset( $lbConf['externalClusters'] ) ) {
123 $lbConf['externalClusters'] = $this->options->get( MainConfigNames::ExternalServers );
124 }
125
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 ) ) {
130 $lbConf['serverTemplate']['schema'] = $this->options->get( MainConfigNames::DBmwschema );
131 }
132 $lbConf['serverTemplate']['sqlMode'] = $this->options->get( MainConfigNames::SQLMode );
133 $serversCheck = [ $lbConf['serverTemplate'] ];
134 }
135 }
136
137 $this->assertValidServerConfigs(
138 $serversCheck,
139 $this->options->get( MainConfigNames::DBname ),
140 $this->options->get( MainConfigNames::DBprefix )
141 );
142
143 $lbConf['virtualDomainsMapping'] = $this->options->get( MainConfigNames::VirtualDomainsMapping );
144 $lbConf['remoteVirtualDomainsMapping'] = $this->options->get( MainConfigNames::RemoteVirtualDomainsMapping );
145
146 return $lbConf;
147 }
148
152 public function getConfig() {
153 return $this->lbConf;
154 }
155
161 private function initServerInfo( array $server, ServiceOptions $options ): array {
162 if ( $server['type'] === 'sqlite' ) {
163 $httpMethod = $_SERVER['REQUEST_METHOD'] ?? null;
164 // T93097: hint for how file-based databases (e.g. sqlite) should go about locking.
165 // See https://www.sqlite.org/lang_transaction.html
166 // See https://www.sqlite.org/lockingv3.html#shared_lock
167 $isHttpRead = in_array( $httpMethod, [ 'GET', 'HEAD', 'OPTIONS', 'TRACE' ] );
168 if ( MW_ENTRY_POINT === 'rest' && !$isHttpRead ) {
169 // Hack to support some re-entrant invocations using sqlite
170 // See: T259685, T91820
172 if ( $request->hasHeader( 'Promise-Non-Write-API-Action' ) ) {
173 $isHttpRead = true;
174 }
175 }
176 $server += [
177 'dbDirectory' => $options->get( MainConfigNames::SQLiteDataDir ),
178 'trxMode' => $isHttpRead ? 'DEFERRED' : 'IMMEDIATE'
179 ];
180 } elseif ( $server['type'] === 'postgres' ) {
181 $server += [ 'port' => $options->get( MainConfigNames::DBport ) ];
182 }
183
184 if ( in_array( $server['type'], self::DB_TYPES_WITH_SCHEMAS, true ) ) {
185 $server += [ 'schema' => $options->get( MainConfigNames::DBmwschema ) ];
186 }
187
188 $flags = $server['flags'] ?? DBO_DEFAULT;
189 if ( $options->get( MainConfigNames::DebugDumpSql )
190 || $options->get( MainConfigNames::DebugLogFile )
191 || $options->get( MainConfigNames::DebugToolbar )
192 ) {
193 $flags |= DBO_DEBUG;
194 }
195 $server['flags'] = $flags;
196
197 $server += [
198 'tablePrefix' => $options->get( MainConfigNames::DBprefix ),
199 'sqlMode' => $options->get( MainConfigNames::SQLMode ),
200 ];
201
202 return $server;
203 }
204
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; // server DB
214 $srvTP = $server['tablePrefix'] ?? ''; // server table prefix
215
216 if ( $type === 'mysql' ) {
217 // A DB name is not needed to connect to mysql; 'dbname' is useless.
218 // This field only defines the DB to use for unspecified DB domains.
219 if ( $srvDB !== null && $srvDB !== $ldDB ) {
220 $this->reportMismatchedDBs( $srvDB, $ldDB );
221 }
222 } elseif ( $type === 'postgres' ) {
223 if ( $srvTP !== '' ) {
224 $this->reportIfPrefixSet( $srvTP, $type );
225 }
226 }
227
228 if ( $srvTP !== '' && $srvTP !== $ldTP ) {
229 $this->reportMismatchedPrefixes( $srvTP, $ldTP );
230 }
231 }
232 }
233
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."
243 );
244 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_RAW );
245 exit;
246 }
247
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."
261 );
262 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_RAW );
263 exit;
264 }
265
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."
279 );
280 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_RAW );
281 exit;
282 }
283}
const DBO_COMPRESS
Definition defines.php:19
const DBO_DEFAULT
Definition defines.php:13
const DBO_DEBUG
Definition defines.php:9
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:71
const MW_ENTRY_POINT
Definition api.php:21
A class for passing options to services.
MediaWiki-specific class for generating configuration of load balancers.
__construct(ServiceOptions $options, array $lbConf)
Class to expose exceptions to the client (API bots, users, admins using CLI scripts)
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.
LoadBalancer manager for sites with several "main" database clusters.
LoadBalancer manager for sites with one "main" cluster and any number of "external" clusters.