MediaWiki  master
MWLBFactory.php
Go to the documentation of this file.
1 <?php
27 
32 abstract class MWLBFactory {
33 
35  private static $loggedDeprecations = [];
36 
41  public const APPLY_DEFAULT_CONFIG_OPTIONS = [
42  'DBcompress',
43  'DBDefaultGroup',
44  'DBmwschema',
45  'DBname',
46  'DBpassword',
47  'DBport',
48  'DBprefix',
49  'DBserver',
50  'DBservers',
51  'DBssl',
52  'DBtype',
53  'DBuser',
54  'DebugDumpSql',
55  'DebugLogFile',
56  'ExternalServers',
57  'SQLiteDataDir',
58  'SQLMode',
59  ];
60 
71  public static function applyDefaultConfig(
72  array $lbConf,
73  ServiceOptions $options,
74  ConfiguredReadOnlyMode $readOnlyMode,
75  BagOStuff $srvCache,
76  BagOStuff $mainStash,
77  WANObjectCache $wanCache
78  ) {
79  $options->assertRequiredOptions( self::APPLY_DEFAULT_CONFIG_OPTIONS );
80 
81  global $wgCommandLineMode;
82 
83  $typesWithSchema = self::getDbTypesWithSchemas();
84 
85  $lbConf += [
86  'localDomain' => new DatabaseDomain(
87  $options->get( 'DBname' ),
88  $options->get( 'DBmwschema' ),
89  $options->get( 'DBprefix' )
90  ),
91  'profiler' => function ( $section ) {
92  return Profiler::instance()->scopedProfileIn( $section );
93  },
94  'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
95  'replLogger' => LoggerFactory::getInstance( 'DBReplication' ),
96  'queryLogger' => LoggerFactory::getInstance( 'DBQuery' ),
97  'connLogger' => LoggerFactory::getInstance( 'DBConnection' ),
98  'perfLogger' => LoggerFactory::getInstance( 'DBPerformance' ),
99  'errorLogger' => [ MWExceptionHandler::class, 'logException' ],
100  'deprecationLogger' => [ static::class, 'logDeprecation' ],
101  'cliMode' => $wgCommandLineMode,
102  'hostname' => wfHostname(),
103  'readOnlyReason' => $readOnlyMode->getReason(),
104  'defaultGroup' => $options->get( 'DBDefaultGroup' ),
105  ];
106 
107  $serversCheck = [];
108  // When making changes here, remember to also specify MediaWiki-specific options
109  // for Database classes in the relevant Installer subclass.
110  // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
111  if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple::class ) {
112  if ( isset( $lbConf['servers'] ) ) {
113  // Server array is already explicitly configured
114  } elseif ( is_array( $options->get( 'DBservers' ) ) ) {
115  $lbConf['servers'] = [];
116  foreach ( $options->get( 'DBservers' ) as $i => $server ) {
117  $lbConf['servers'][$i] = self::initServerInfo( $server, $options );
118  }
119  } else {
120  $server = self::initServerInfo(
121  [
122  'host' => $options->get( 'DBserver' ),
123  'user' => $options->get( 'DBuser' ),
124  'password' => $options->get( 'DBpassword' ),
125  'dbname' => $options->get( 'DBname' ),
126  'type' => $options->get( 'DBtype' ),
127  'load' => 1
128  ],
129  $options
130  );
131 
132  $server['flags'] |= $options->get( 'DBssl' ) ? DBO_SSL : 0;
133  $server['flags'] |= $options->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
134 
135  $lbConf['servers'] = [ $server ];
136  }
137  if ( !isset( $lbConf['externalClusters'] ) ) {
138  $lbConf['externalClusters'] = $options->get( 'ExternalServers' );
139  }
140 
141  $serversCheck = $lbConf['servers'];
142  } elseif ( $lbConf['class'] === Wikimedia\Rdbms\LBFactoryMulti::class ) {
143  if ( isset( $lbConf['serverTemplate'] ) ) {
144  if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
145  $lbConf['serverTemplate']['schema'] = $options->get( 'DBmwschema' );
146  }
147  $lbConf['serverTemplate']['sqlMode'] = $options->get( 'SQLMode' );
148  }
149  $serversCheck = [ $lbConf['serverTemplate'] ] ?? [];
150  }
151 
152  self::assertValidServerConfigs(
153  $serversCheck,
154  $options->get( 'DBname' ),
155  $options->get( 'DBprefix' )
156  );
157 
158  $lbConf = self::injectObjectCaches( $lbConf, $srvCache, $mainStash, $wanCache );
159 
160  return $lbConf;
161  }
162 
166  private static function getDbTypesWithSchemas() {
167  return [ 'postgres' ];
168  }
169 
175  private static function initServerInfo( array $server, ServiceOptions $options ) {
176  if ( $server['type'] === 'sqlite' ) {
177  $httpMethod = $_SERVER['REQUEST_METHOD'] ?? null;
178  // T93097: hint for how file-based databases (e.g. sqlite) should go about locking.
179  // See https://www.sqlite.org/lang_transaction.html
180  // See https://www.sqlite.org/lockingv3.html#shared_lock
181  $isHttpRead = in_array( $httpMethod, [ 'GET', 'HEAD', 'OPTIONS', 'TRACE' ] );
182  $server += [
183  'dbDirectory' => $options->get( 'SQLiteDataDir' ),
184  'trxMode' => $isHttpRead ? 'DEFERRED' : 'IMMEDIATE'
185  ];
186  } elseif ( $server['type'] === 'postgres' ) {
187  $server += [
188  'port' => $options->get( 'DBport' ),
189  // Work around the reserved word usage in MediaWiki schema
190  'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
191  ];
192  }
193 
194  if ( in_array( $server['type'], self::getDbTypesWithSchemas(), true ) ) {
195  $server += [ 'schema' => $options->get( 'DBmwschema' ) ];
196  }
197 
198  $flags = $server['flags'] ?? DBO_DEFAULT;
199  if ( $options->get( 'DebugDumpSql' ) || $options->get( 'DebugLogFile' ) ) {
200  $flags |= DBO_DEBUG;
201  }
202  $server['flags'] = $flags;
203 
204  $server += [
205  'tablePrefix' => $options->get( 'DBprefix' ),
206  'sqlMode' => $options->get( 'SQLMode' ),
207  ];
208 
209  return $server;
210  }
211 
219  private static function injectObjectCaches(
220  array $lbConf, BagOStuff $sCache, BagOStuff $mStash, WANObjectCache $wCache
221  ) {
222  // Fallback if APC style caching is not an option
223  if ( $sCache instanceof EmptyBagOStuff ) {
224  $sCache = new HashBagOStuff( [ 'maxKeys' => 100 ] );
225  }
226 
227  // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
228  if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
229  $lbConf['srvCache'] = $sCache;
230  }
231  if ( $mStash->getQoS( $mStash::ATTR_EMULATION ) > $mStash::QOS_EMULATION_SQL ) {
232  $lbConf['memStash'] = $mStash;
233  }
234  if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
235  $lbConf['wanCache'] = $wCache;
236  }
237 
238  return $lbConf;
239  }
240 
246  private static function assertValidServerConfigs( array $servers, $ldDB, $ldTP ) {
247  foreach ( $servers as $server ) {
248  $type = $server['type'] ?? null;
249  $srvDB = $server['dbname'] ?? null; // server DB
250  $srvTP = $server['tablePrefix'] ?? ''; // server table prefix
251 
252  if ( $type === 'mysql' ) {
253  // A DB name is not needed to connect to mysql; 'dbname' is useless.
254  // This field only defines the DB to use for unspecified DB domains.
255  if ( $srvDB !== null && $srvDB !== $ldDB ) {
256  self::reportMismatchedDBs( $srvDB, $ldDB );
257  }
258  } elseif ( $type === 'postgres' ) {
259  if ( $srvTP !== '' ) {
260  self::reportIfPrefixSet( $srvTP, $type );
261  }
262  }
263 
264  if ( $srvTP !== '' && $srvTP !== $ldTP ) {
265  self::reportMismatchedPrefixes( $srvTP, $ldTP );
266  }
267  }
268  }
269 
274  private static function reportIfPrefixSet( $prefix, $dbType ) {
275  $e = new UnexpectedValueException(
276  "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
277  "MediaWiki does not support using a table prefix with this RDBMS type."
278  );
280  exit;
281  }
282 
287  private static function reportMismatchedDBs( $srvDB, $ldDB ) {
288  $e = new UnexpectedValueException(
289  "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
290  "Set \$wgDBname to the database used by this wiki project. " .
291  "There is rarely a need to set 'dbname' in \$wgDBservers. " .
292  "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
293  "use of Database::getDomainId(), and other features are not reliable when " .
294  "\$wgDBservers does not match the local wiki database/prefix."
295  );
297  exit;
298  }
299 
304  private static function reportMismatchedPrefixes( $srvTP, $ldTP ) {
305  $e = new UnexpectedValueException(
306  "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
307  "Set \$wgDBprefix to the table prefix used by this wiki project. " .
308  "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
309  "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
310  "use of Database::getDomainId(), and other features are not reliable when " .
311  "\$wgDBservers does not match the local wiki database/prefix."
312  );
314  exit;
315  }
316 
326  public static function getLBFactoryClass( array $config ) {
327  // For configuration backward compatibility after removing
328  // underscores from class names in MediaWiki 1.23.
329  $bcClasses = [
330  'LBFactory_Simple' => 'LBFactorySimple',
331  'LBFactory_Single' => 'LBFactorySingle',
332  'LBFactory_Multi' => 'LBFactoryMulti'
333  ];
334 
335  $class = $config['class'];
336 
337  if ( isset( $bcClasses[$class] ) ) {
338  $class = $bcClasses[$class];
339  wfDeprecated(
340  '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
341  '1.23'
342  );
343  }
344 
345  // For configuration backward compatibility after moving classes to namespaces (1.29)
346  $compat = [
347  'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
348  'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
349  'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
350  ];
351 
352  if ( isset( $compat[$class] ) ) {
353  $class = $compat[$class];
354  }
355 
356  return $class;
357  }
358 
364  public static function logDeprecation( $msg ) {
365  global $wgDevelopmentWarnings;
366 
367  if ( isset( self::$loggedDeprecations[$msg] ) ) {
368  return;
369  }
370  self::$loggedDeprecations[$msg] = true;
371 
372  if ( $wgDevelopmentWarnings ) {
373  trigger_error( $msg, E_USER_DEPRECATED );
374  }
375  wfDebugLog( 'deprecated', $msg, 'private' );
376  }
377 }
static instance()
Singleton.
Definition: Profiler.php:63
wfHostname()
Fetch server name for use in error reporting etc.
static getDbTypesWithSchemas()
A class for passing options to services.
static initServerInfo(array $server, ServiceOptions $options)
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
static assertValidServerConfigs(array $servers, $ldDB, $ldTP)
const DBO_DEBUG
Definition: defines.php:9
static logDeprecation( $msg)
Log a database deprecation warning.
static reportMismatchedPrefixes( $srvTP, $ldTP)
static array $loggedDeprecations
Cache of already-logged deprecation messages.
Definition: MWLBFactory.php:35
static injectObjectCaches(array $lbConf, BagOStuff $sCache, BagOStuff $mStash, WANObjectCache $wCache)
static applyDefaultConfig(array $lbConf, ServiceOptions $options, ConfiguredReadOnlyMode $readOnlyMode, BagOStuff $srvCache, BagOStuff $mainStash, WANObjectCache $wanCache)
Definition: MWLBFactory.php:71
getQoS( $flag)
Definition: BagOStuff.php:467
static output( $e, $mode, $eNew=null)
getReason()
Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys, without regard for order.
const DBO_DEFAULT
Definition: defines.php:13
Class to handle database/schema/prefix specifications for IDatabase.
static reportIfPrefixSet( $prefix, $dbType)
static getLBFactoryClass(array $config)
Returns the LBFactory class to use and the load balancer configuration.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not...
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
global $wgCommandLineMode
MediaWiki-specific class for generating database load balancers.
Definition: MWLBFactory.php:32
const DBO_SSL
Definition: defines.php:17
static reportMismatchedDBs( $srvDB, $ldDB)
$wgDevelopmentWarnings
If set to true MediaWiki will throw notices for some possible error conditions and for deprecated fun...
const DBO_COMPRESS
Definition: defines.php:18