MediaWiki  master
MWLBFactory.php
Go to the documentation of this file.
1 <?php
28 
33 abstract 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  }
151  $serversCheck = [ $lbConf['serverTemplate'] ] ?? [];
152  }
153 
155  $serversCheck,
156  $options->get( 'DBname' ),
157  $options->get( 'DBprefix' )
158  );
159 
160  $lbConf = self::injectObjectCaches( $lbConf, $srvCache, $mainStash, $wanCache );
161 
162  return $lbConf;
163  }
164 
168  private static function getDbTypesWithSchemas() {
169  return [ 'postgres' ];
170  }
171 
177  private static function initServerInfo( array $server, ServiceOptions $options ) {
178  if ( $server['type'] === 'sqlite' ) {
179  $httpMethod = $_SERVER['REQUEST_METHOD'] ?? null;
180  // T93097: hint for how file-based databases (e.g. sqlite) should go about locking.
181  // See https://www.sqlite.org/lang_transaction.html
182  // See https://www.sqlite.org/lockingv3.html#shared_lock
183  $isHttpRead = in_array( $httpMethod, [ 'GET', 'HEAD', 'OPTIONS', 'TRACE' ] );
184  $server += [
185  'dbDirectory' => $options->get( 'SQLiteDataDir' ),
186  'trxMode' => $isHttpRead ? 'DEFERRED' : 'IMMEDIATE'
187  ];
188  } elseif ( $server['type'] === 'postgres' ) {
189  $server += [
190  'port' => $options->get( 'DBport' ),
191  // Work around the reserved word usage in MediaWiki schema
192  'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
193  ];
194  }
195 
196  if ( in_array( $server['type'], self::getDbTypesWithSchemas(), true ) ) {
197  $server += [ 'schema' => $options->get( 'DBmwschema' ) ];
198  }
199 
200  $flags = $server['flags'] ?? DBO_DEFAULT;
201  if ( $options->get( 'DebugDumpSql' )
202  || $options->get( 'DebugLogFile' )
203  || $options->get( 'DebugToolbar' )
204  ) {
205  $flags |= DBO_DEBUG;
206  }
207  $server['flags'] = $flags;
208 
209  $server += [
210  'tablePrefix' => $options->get( 'DBprefix' ),
211  'sqlMode' => $options->get( 'SQLMode' ),
212  ];
213 
214  return $server;
215  }
216 
224  private static function injectObjectCaches(
225  array $lbConf, BagOStuff $sCache, BagOStuff $mStash, WANObjectCache $wCache
226  ) {
227  // Fallback if APC style caching is not an option
228  if ( $sCache instanceof EmptyBagOStuff ) {
229  $sCache = new HashBagOStuff( [ 'maxKeys' => 100 ] );
230  }
231 
232  // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
233  if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
234  $lbConf['srvCache'] = $sCache;
235  }
236  if ( $mStash->getQoS( $mStash::ATTR_EMULATION ) > $mStash::QOS_EMULATION_SQL ) {
237  $lbConf['memStash'] = $mStash;
238  }
239  if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
240  $lbConf['wanCache'] = $wCache;
241  }
242 
243  return $lbConf;
244  }
245 
251  private static function assertValidServerConfigs( array $servers, $ldDB, $ldTP ) {
252  foreach ( $servers as $server ) {
253  $type = $server['type'] ?? null;
254  $srvDB = $server['dbname'] ?? null; // server DB
255  $srvTP = $server['tablePrefix'] ?? ''; // server table prefix
256 
257  if ( $type === 'mysql' ) {
258  // A DB name is not needed to connect to mysql; 'dbname' is useless.
259  // This field only defines the DB to use for unspecified DB domains.
260  if ( $srvDB !== null && $srvDB !== $ldDB ) {
261  self::reportMismatchedDBs( $srvDB, $ldDB );
262  }
263  } elseif ( $type === 'postgres' ) {
264  if ( $srvTP !== '' ) {
265  self::reportIfPrefixSet( $srvTP, $type );
266  }
267  }
268 
269  if ( $srvTP !== '' && $srvTP !== $ldTP ) {
270  self::reportMismatchedPrefixes( $srvTP, $ldTP );
271  }
272  }
273  }
274 
279  private static function reportIfPrefixSet( $prefix, $dbType ) {
280  $e = new UnexpectedValueException(
281  "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
282  "MediaWiki does not support using a table prefix with this RDBMS type."
283  );
285  exit;
286  }
287 
292  private static function reportMismatchedDBs( $srvDB, $ldDB ) {
293  $e = new UnexpectedValueException(
294  "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
295  "Set \$wgDBname to the database used by this wiki project. " .
296  "There is rarely a need to set 'dbname' in \$wgDBservers. " .
297  "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
298  "use of Database::getDomainId(), and other features are not reliable when " .
299  "\$wgDBservers does not match the local wiki database/prefix."
300  );
302  exit;
303  }
304 
309  private static function reportMismatchedPrefixes( $srvTP, $ldTP ) {
310  $e = new UnexpectedValueException(
311  "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
312  "Set \$wgDBprefix to the table prefix used by this wiki project. " .
313  "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
314  "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
315  "use of Database::getDomainId(), and other features are not reliable when " .
316  "\$wgDBservers does not match the local wiki database/prefix."
317  );
319  exit;
320  }
321 
331  public static function getLBFactoryClass( array $config ) {
332  // For configuration backward compatibility after removing
333  // underscores from class names in MediaWiki 1.23.
334  $bcClasses = [
335  'LBFactory_Simple' => 'LBFactorySimple',
336  'LBFactory_Single' => 'LBFactorySingle',
337  'LBFactory_Multi' => 'LBFactoryMulti'
338  ];
339 
340  $class = $config['class'];
341 
342  if ( isset( $bcClasses[$class] ) ) {
343  $class = $bcClasses[$class];
344  wfDeprecated(
345  '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
346  '1.23'
347  );
348  }
349 
350  // For configuration backward compatibility after moving classes to namespaces (1.29)
351  $compat = [
352  'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
353  'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
354  'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
355  ];
356 
357  if ( isset( $compat[$class] ) ) {
358  $class = $compat[$class];
359  }
360 
361  return $class;
362  }
363 
367  public static function setDomainAliases( ILBFactory $lbFactory ) {
368  $domain = DatabaseDomain::newFromId( $lbFactory->getLocalDomainID() );
369  // For compatibility with hyphenated $wgDBname values on older wikis, handle callers
370  // that assume corresponding database domain IDs and wiki IDs have identical values
371  $rawLocalDomain = strlen( $domain->getTablePrefix() )
372  ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}"
373  : (string)$domain->getDatabase();
374 
375  $lbFactory->setDomainAliases( [ $rawLocalDomain => $domain ] );
376  }
377 
383  public static function logDeprecation( $msg ) {
384  global $wgDevelopmentWarnings;
385 
386  if ( isset( self::$loggedDeprecations[$msg] ) ) {
387  return;
388  }
389  self::$loggedDeprecations[$msg] = true;
390 
391  if ( $wgDevelopmentWarnings ) {
392  trigger_error( $msg, E_USER_DEPRECATED );
393  }
394  wfDebugLog( 'deprecated', $msg, 'private' );
395  }
396 }
MWLBFactory
MediaWiki-specific class for generating database load balancers.
Definition: MWLBFactory.php:33
Wikimedia\Rdbms\ILBFactory\getLocalDomainID
getLocalDomainID()
Get the local (and default) database domain ID of connection handles.
WANObjectCache\getQoS
getQoS( $flag)
Definition: WANObjectCache.php:2306
MWLBFactory\reportMismatchedDBs
static reportMismatchedDBs( $srvDB, $ldDB)
Definition: MWLBFactory.php:292
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
BagOStuff\getQoS
getQoS( $flag)
Definition: BagOStuff.php:482
EmptyBagOStuff
A BagOStuff object with no objects in it.
Definition: EmptyBagOStuff.php:29
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:63
MWLBFactory\initServerInfo
static initServerInfo(array $server, ServiceOptions $options)
Definition: MWLBFactory.php:177
MWLBFactory\assertValidServerConfigs
static assertValidServerConfigs(array $servers, $ldDB, $ldTP)
Definition: MWLBFactory.php:251
DBO_DEBUG
const DBO_DEBUG
Definition: defines.php:9
ConfiguredReadOnlyMode
A read-only mode service which does not depend on LoadBalancer.
Definition: ConfiguredReadOnlyMode.php:9
wfHostname
wfHostname()
Fetch server name for use in error reporting etc.
Definition: GlobalFunctions.php:1260
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:65
DBO_SSL
const DBO_SSL
Definition: defines.php:17
MWLBFactory\logDeprecation
static logDeprecation( $msg)
Log a database deprecation warning.
Definition: MWLBFactory.php:383
MWLBFactory\setDomainAliases
static setDomainAliases(ILBFactory $lbFactory)
Definition: MWLBFactory.php:367
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:992
ConfiguredReadOnlyMode\getReason
getReason()
Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
Definition: ConfiguredReadOnlyMode.php:48
MWLBFactory\reportMismatchedPrefixes
static reportMismatchedPrefixes( $srvTP, $ldTP)
Definition: MWLBFactory.php:309
MWLBFactory\$loggedDeprecations
static array $loggedDeprecations
Cache of already-logged deprecation messages.
Definition: MWLBFactory.php:36
MediaWiki\Config\ServiceOptions
A class for passing options to services.
Definition: ServiceOptions.php:25
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1030
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
$wgCommandLineMode
global $wgCommandLineMode
Definition: DevelopmentSettings.php:28
MWLBFactory\applyDefaultConfig
static applyDefaultConfig(array $lbConf, ServiceOptions $options, ConfiguredReadOnlyMode $readOnlyMode, BagOStuff $srvCache, BagOStuff $mainStash, WANObjectCache $wanCache)
Definition: MWLBFactory.php:73
MWExceptionRenderer\output
static output(Throwable $e, $mode, Throwable $eNew=null)
Definition: MWExceptionRenderer.php:40
MWLBFactory\getLBFactoryClass
static getLBFactoryClass(array $config)
Returns the LBFactory class to use and the load balancer configuration.
Definition: MWLBFactory.php:331
MWExceptionRenderer\AS_PRETTY
const AS_PRETTY
Definition: MWExceptionRenderer.php:33
DBO_COMPRESS
const DBO_COMPRESS
Definition: defines.php:18
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:119
$wgDevelopmentWarnings
$wgDevelopmentWarnings
If set to true MediaWiki will throw notices for some possible error conditions and for deprecated fun...
Definition: DefaultSettings.php:6671
Wikimedia\Rdbms\ILBFactory\setDomainAliases
setDomainAliases(array $aliases)
Convert certain database domains to alternative ones.
MWLBFactory\injectObjectCaches
static injectObjectCaches(array $lbConf, BagOStuff $sCache, BagOStuff $mStash, WANObjectCache $wCache)
Definition: MWLBFactory.php:224
Wikimedia
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
MediaWiki\Config\ServiceOptions\get
get( $key)
Definition: ServiceOptions.php:84
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/schema/prefix specifications for IDatabase.
Definition: DatabaseDomain.php:40
DBO_DEFAULT
const DBO_DEFAULT
Definition: defines.php:13
MWLBFactory\getDbTypesWithSchemas
static getDbTypesWithSchemas()
Definition: MWLBFactory.php:168
MWLBFactory\reportIfPrefixSet
static reportIfPrefixSet( $prefix, $dbType)
Definition: MWLBFactory.php:279
MediaWiki\Config\ServiceOptions\assertRequiredOptions
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
Definition: ServiceOptions.php:62
Wikimedia\Rdbms\ILBFactory
An interface for generating database load balancers.
Definition: ILBFactory.php:33
$type
$type
Definition: testCompression.php:52