MediaWiki REL1_35
MWLBFactory.php
Go to the documentation of this file.
1<?php
28
33abstract class MWLBFactory {
34
36 private static $loggedDeprecations = [];
37
42 public const APPLY_DEFAULT_CONFIG_OPTIONS = [
43 'DBcompress',
44 'DBDefaultGroup',
45 'DBmwschema',
46 'DBname',
47 'DBpassword',
48 'DBport',
49 'DBprefix',
50 'DBserver',
51 'DBservers',
52 'DBssl',
53 'DBtype',
54 'DBuser',
55 'DebugDumpSql',
56 'DebugLogFile',
57 'DebugToolbar',
58 'ExternalServers',
59 'SQLiteDataDir',
60 'SQLMode',
61 ];
62
73 public static function applyDefaultConfig(
74 array $lbConf,
75 ServiceOptions $options,
76 ConfiguredReadOnlyMode $readOnlyMode,
77 BagOStuff $srvCache,
78 BagOStuff $mainStash,
79 WANObjectCache $wanCache
80 ) {
81 $options->assertRequiredOptions( self::APPLY_DEFAULT_CONFIG_OPTIONS );
82
83 global $wgCommandLineMode;
84
85 $typesWithSchema = self::getDbTypesWithSchemas();
86
87 $lbConf += [
88 'localDomain' => new DatabaseDomain(
89 $options->get( 'DBname' ),
90 $options->get( 'DBmwschema' ),
91 $options->get( 'DBprefix' )
92 ),
93 'profiler' => function ( $section ) {
94 return Profiler::instance()->scopedProfileIn( $section );
95 },
96 'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
97 'replLogger' => LoggerFactory::getInstance( 'DBReplication' ),
98 'queryLogger' => LoggerFactory::getInstance( 'DBQuery' ),
99 'connLogger' => LoggerFactory::getInstance( 'DBConnection' ),
100 'perfLogger' => LoggerFactory::getInstance( 'DBPerformance' ),
101 'errorLogger' => [ MWExceptionHandler::class, 'logException' ],
102 'deprecationLogger' => [ static::class, 'logDeprecation' ],
103 'cliMode' => $wgCommandLineMode,
104 'hostname' => wfHostname(),
105 'readOnlyReason' => $readOnlyMode->getReason(),
106 'defaultGroup' => $options->get( 'DBDefaultGroup' ),
107 ];
108
109 $serversCheck = [];
110 // When making changes here, remember to also specify MediaWiki-specific options
111 // for Database classes in the relevant Installer subclass.
112 // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
113 if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple::class ) {
114 if ( isset( $lbConf['servers'] ) ) {
115 // Server array is already explicitly configured
116 } elseif ( is_array( $options->get( 'DBservers' ) ) ) {
117 $lbConf['servers'] = [];
118 foreach ( $options->get( 'DBservers' ) as $i => $server ) {
119 $lbConf['servers'][$i] = self::initServerInfo( $server, $options );
120 }
121 } else {
122 $server = self::initServerInfo(
123 [
124 'host' => $options->get( 'DBserver' ),
125 'user' => $options->get( 'DBuser' ),
126 'password' => $options->get( 'DBpassword' ),
127 'dbname' => $options->get( 'DBname' ),
128 'type' => $options->get( 'DBtype' ),
129 'load' => 1
130 ],
131 $options
132 );
133
134 $server['flags'] |= $options->get( 'DBssl' ) ? DBO_SSL : 0;
135 $server['flags'] |= $options->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
136
137 $lbConf['servers'] = [ $server ];
138 }
139 if ( !isset( $lbConf['externalClusters'] ) ) {
140 $lbConf['externalClusters'] = $options->get( 'ExternalServers' );
141 }
142
143 $serversCheck = $lbConf['servers'];
144 } elseif ( $lbConf['class'] === Wikimedia\Rdbms\LBFactoryMulti::class ) {
145 if ( isset( $lbConf['serverTemplate'] ) ) {
146 if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
147 $lbConf['serverTemplate']['schema'] = $options->get( 'DBmwschema' );
148 }
149 $lbConf['serverTemplate']['sqlMode'] = $options->get( 'SQLMode' );
150 $serversCheck = [ $lbConf['serverTemplate'] ];
151 }
152 }
153
155 $serversCheck,
156 $options->get( 'DBname' ),
157 $options->get( 'DBprefix' )
158 );
159
160 $lbConf['srvCache'] = $srvCache;
161 $lbConf['memStash'] = $mainStash;
162 $lbConf['wanCache'] = $wanCache;
163
164 return $lbConf;
165 }
166
170 private static function getDbTypesWithSchemas() {
171 return [ 'postgres' ];
172 }
173
179 private static function initServerInfo( array $server, ServiceOptions $options ) {
180 if ( $server['type'] === 'sqlite' ) {
181 $httpMethod = $_SERVER['REQUEST_METHOD'] ?? null;
182 // T93097: hint for how file-based databases (e.g. sqlite) should go about locking.
183 // See https://www.sqlite.org/lang_transaction.html
184 // See https://www.sqlite.org/lockingv3.html#shared_lock
185 $isHttpRead = in_array( $httpMethod, [ 'GET', 'HEAD', 'OPTIONS', 'TRACE' ] );
186 if ( MW_ENTRY_POINT === 'rest' && !$isHttpRead ) {
187 // Hack to support some re-entrant invocations using sqlite
188 // See: T259685, T91820
189 $request = \MediaWiki\Rest\EntryPoint::getMainRequest();
190 if ( $request->hasHeader( 'Promise-Non-Write-API-Action' ) ) {
191 $isHttpRead = true;
192 }
193 }
194 $server += [
195 'dbDirectory' => $options->get( 'SQLiteDataDir' ),
196 'trxMode' => $isHttpRead ? 'DEFERRED' : 'IMMEDIATE'
197 ];
198 } elseif ( $server['type'] === 'postgres' ) {
199 $server += [
200 'port' => $options->get( 'DBport' ),
201 // Work around the reserved word usage in MediaWiki schema
202 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
203 ];
204 }
205
206 if ( in_array( $server['type'], self::getDbTypesWithSchemas(), true ) ) {
207 $server += [ 'schema' => $options->get( 'DBmwschema' ) ];
208 }
209
210 $flags = $server['flags'] ?? DBO_DEFAULT;
211 if ( $options->get( 'DebugDumpSql' )
212 || $options->get( 'DebugLogFile' )
213 || $options->get( 'DebugToolbar' )
214 ) {
215 $flags |= DBO_DEBUG;
216 }
217 $server['flags'] = $flags;
218
219 $server += [
220 'tablePrefix' => $options->get( 'DBprefix' ),
221 'sqlMode' => $options->get( 'SQLMode' ),
222 ];
223
224 return $server;
225 }
226
232 private static function assertValidServerConfigs( array $servers, $ldDB, $ldTP ) {
233 foreach ( $servers as $server ) {
234 $type = $server['type'] ?? null;
235 $srvDB = $server['dbname'] ?? null; // server DB
236 $srvTP = $server['tablePrefix'] ?? ''; // server table prefix
237
238 if ( $type === 'mysql' ) {
239 // A DB name is not needed to connect to mysql; 'dbname' is useless.
240 // This field only defines the DB to use for unspecified DB domains.
241 if ( $srvDB !== null && $srvDB !== $ldDB ) {
242 self::reportMismatchedDBs( $srvDB, $ldDB );
243 }
244 } elseif ( $type === 'postgres' ) {
245 if ( $srvTP !== '' ) {
247 }
248 }
249
250 if ( $srvTP !== '' && $srvTP !== $ldTP ) {
251 self::reportMismatchedPrefixes( $srvTP, $ldTP );
252 }
253 }
254 }
255
260 private static function reportIfPrefixSet( $prefix, $dbType ) {
261 $e = new UnexpectedValueException(
262 "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
263 "MediaWiki does not support using a table prefix with this RDBMS type."
264 );
266 exit;
267 }
268
273 private static function reportMismatchedDBs( $srvDB, $ldDB ) {
274 $e = new UnexpectedValueException(
275 "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
276 "Set \$wgDBname to the database used by this wiki project. " .
277 "There is rarely a need to set 'dbname' in \$wgDBservers. " .
278 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
279 "use of Database::getDomainId(), and other features are not reliable when " .
280 "\$wgDBservers does not match the local wiki database/prefix."
281 );
283 exit;
284 }
285
290 private static function reportMismatchedPrefixes( $srvTP, $ldTP ) {
291 $e = new UnexpectedValueException(
292 "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
293 "Set \$wgDBprefix to the table prefix used by this wiki project. " .
294 "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
295 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
296 "use of Database::getDomainId(), and other features are not reliable when " .
297 "\$wgDBservers does not match the local wiki database/prefix."
298 );
300 exit;
301 }
302
310 public static function getLBFactoryClass( array $config ) {
311 $compat = [
312 // For LocalSettings.php compat after removing underscores (since 1.23).
313 'LBFactory_Single' => Wikimedia\Rdbms\LBFactorySingle::class,
314 'LBFactory_Simple' => Wikimedia\Rdbms\LBFactorySimple::class,
315 'LBFactory_Multi' => Wikimedia\Rdbms\LBFactoryMulti::class,
316 // For LocalSettings.php compat after moving classes to namespaces (since 1.29).
317 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
318 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
319 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
320 ];
321
322 $class = $config['class'];
323 return $compat[$class] ?? $class;
324 }
325
329 public static function setDomainAliases( ILBFactory $lbFactory ) {
330 $domain = DatabaseDomain::newFromId( $lbFactory->getLocalDomainID() );
331 // For compatibility with hyphenated $wgDBname values on older wikis, handle callers
332 // that assume corresponding database domain IDs and wiki IDs have identical values
333 $rawLocalDomain = strlen( $domain->getTablePrefix() )
334 ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}"
335 : (string)$domain->getDatabase();
336
337 $lbFactory->setDomainAliases( [ $rawLocalDomain => $domain ] );
338 }
339
345 public static function logDeprecation( $msg ) {
346 if ( isset( self::$loggedDeprecations[$msg] ) ) {
347 return;
348 }
349 self::$loggedDeprecations[$msg] = true;
350 MWDebug::sendRawDeprecated( $msg, true, wfGetCaller() );
351 }
352}
global $wgCommandLineMode
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
wfHostname()
Get host name of the current machine, for use in error reporting.
const MW_ENTRY_POINT
Definition api.php:41
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:71
A read-only mode service which does not depend on LoadBalancer.
getReason()
Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
static output(Throwable $e, $mode, Throwable $eNew=null)
MediaWiki-specific class for generating database load balancers.
static setDomainAliases(ILBFactory $lbFactory)
static logDeprecation( $msg)
Log a database deprecation warning.
static assertValidServerConfigs(array $servers, $ldDB, $ldTP)
static getDbTypesWithSchemas()
static initServerInfo(array $server, ServiceOptions $options)
static array $loggedDeprecations
Cache of already-logged deprecation messages.
static reportMismatchedPrefixes( $srvTP, $ldTP)
static getLBFactoryClass(array $config)
Decide which LBFactory class to use.
static reportMismatchedDBs( $srvDB, $ldDB)
static reportIfPrefixSet( $prefix, $dbType)
static applyDefaultConfig(array $lbConf, ServiceOptions $options, ConfiguredReadOnlyMode $readOnlyMode, BagOStuff $srvCache, BagOStuff $mainStash, WANObjectCache $wanCache)
A class for passing options to services.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
PSR-3 logger instance factory.
Multi-datacenter aware caching interface.
Class to handle database/schema/prefix specifications for IDatabase.
An interface for generating database load balancers.
getLocalDomainID()
Get the local (and default) database domain ID of connection handles.
setDomainAliases(array $aliases)
Convert certain database domains to alternative ones.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
const DBO_COMPRESS
Definition defines.php:18
const DBO_DEFAULT
Definition defines.php:13
const DBO_SSL
Definition defines.php:17
const DBO_DEBUG
Definition defines.php:9