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  '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  'DebugToolbar',
57  'ExternalServers',
58  'SQLiteDataDir',
59  'SQLMode',
60  ];
61 
72  public static function applyDefaultConfig(
73  array $lbConf,
74  ServiceOptions $options,
75  ConfiguredReadOnlyMode $readOnlyMode,
76  BagOStuff $srvCache,
77  BagOStuff $mainStash,
78  WANObjectCache $wanCache
79  ) {
80  $options->assertRequiredOptions( self::APPLY_DEFAULT_CONFIG_OPTIONS );
81 
82  global $wgCommandLineMode;
83 
84  $typesWithSchema = self::getDbTypesWithSchemas();
85 
86  $lbConf += [
87  'localDomain' => new DatabaseDomain(
88  $options->get( 'DBname' ),
89  $options->get( 'DBmwschema' ),
90  $options->get( 'DBprefix' )
91  ),
92  'profiler' => function ( $section ) {
93  return Profiler::instance()->scopedProfileIn( $section );
94  },
95  'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
96  'replLogger' => LoggerFactory::getInstance( 'DBReplication' ),
97  'queryLogger' => LoggerFactory::getInstance( 'DBQuery' ),
98  'connLogger' => LoggerFactory::getInstance( 'DBConnection' ),
99  'perfLogger' => LoggerFactory::getInstance( 'DBPerformance' ),
100  'errorLogger' => [ MWExceptionHandler::class, 'logException' ],
101  'deprecationLogger' => [ static::class, 'logDeprecation' ],
102  'cliMode' => $wgCommandLineMode,
103  'hostname' => wfHostname(),
104  'readOnlyReason' => $readOnlyMode->getReason(),
105  'defaultGroup' => $options->get( 'DBDefaultGroup' ),
106  ];
107 
108  $serversCheck = [];
109  // When making changes here, remember to also specify MediaWiki-specific options
110  // for Database classes in the relevant Installer subclass.
111  // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
112  if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple::class ) {
113  if ( isset( $lbConf['servers'] ) ) {
114  // Server array is already explicitly configured
115  } elseif ( is_array( $options->get( 'DBservers' ) ) ) {
116  $lbConf['servers'] = [];
117  foreach ( $options->get( 'DBservers' ) as $i => $server ) {
118  $lbConf['servers'][$i] = self::initServerInfo( $server, $options );
119  }
120  } else {
121  $server = self::initServerInfo(
122  [
123  'host' => $options->get( 'DBserver' ),
124  'user' => $options->get( 'DBuser' ),
125  'password' => $options->get( 'DBpassword' ),
126  'dbname' => $options->get( 'DBname' ),
127  'type' => $options->get( 'DBtype' ),
128  'load' => 1
129  ],
130  $options
131  );
132 
133  $server['flags'] |= $options->get( 'DBssl' ) ? DBO_SSL : 0;
134  $server['flags'] |= $options->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
135 
136  $lbConf['servers'] = [ $server ];
137  }
138  if ( !isset( $lbConf['externalClusters'] ) ) {
139  $lbConf['externalClusters'] = $options->get( 'ExternalServers' );
140  }
141 
142  $serversCheck = $lbConf['servers'];
143  } elseif ( $lbConf['class'] === Wikimedia\Rdbms\LBFactoryMulti::class ) {
144  if ( isset( $lbConf['serverTemplate'] ) ) {
145  if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
146  $lbConf['serverTemplate']['schema'] = $options->get( 'DBmwschema' );
147  }
148  $lbConf['serverTemplate']['sqlMode'] = $options->get( 'SQLMode' );
149  $serversCheck = [ $lbConf['serverTemplate'] ];
150  }
151  }
152 
154  $serversCheck,
155  $options->get( 'DBname' ),
156  $options->get( 'DBprefix' )
157  );
158 
159  $lbConf['srvCache'] = $srvCache;
160  $lbConf['memStash'] = $mainStash;
161  $lbConf['wanCache'] = $wanCache;
162 
163  return $lbConf;
164  }
165 
169  private static function getDbTypesWithSchemas() {
170  return [ 'postgres' ];
171  }
172 
178  private static function initServerInfo( array $server, ServiceOptions $options ) {
179  if ( $server['type'] === 'sqlite' ) {
180  $httpMethod = $_SERVER['REQUEST_METHOD'] ?? null;
181  // T93097: hint for how file-based databases (e.g. sqlite) should go about locking.
182  // See https://www.sqlite.org/lang_transaction.html
183  // See https://www.sqlite.org/lockingv3.html#shared_lock
184  $isHttpRead = in_array( $httpMethod, [ 'GET', 'HEAD', 'OPTIONS', 'TRACE' ] );
185  $server += [
186  'dbDirectory' => $options->get( 'SQLiteDataDir' ),
187  'trxMode' => $isHttpRead ? 'DEFERRED' : 'IMMEDIATE'
188  ];
189  } elseif ( $server['type'] === 'postgres' ) {
190  $server += [
191  'port' => $options->get( 'DBport' ),
192  // Work around the reserved word usage in MediaWiki schema
193  'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
194  ];
195  }
196 
197  if ( in_array( $server['type'], self::getDbTypesWithSchemas(), true ) ) {
198  $server += [ 'schema' => $options->get( 'DBmwschema' ) ];
199  }
200 
201  $flags = $server['flags'] ?? DBO_DEFAULT;
202  if ( $options->get( 'DebugDumpSql' )
203  || $options->get( 'DebugLogFile' )
204  || $options->get( 'DebugToolbar' )
205  ) {
206  $flags |= DBO_DEBUG;
207  }
208  $server['flags'] = $flags;
209 
210  $server += [
211  'tablePrefix' => $options->get( 'DBprefix' ),
212  'sqlMode' => $options->get( 'SQLMode' ),
213  ];
214 
215  return $server;
216  }
217 
223  private static function assertValidServerConfigs( array $servers, $ldDB, $ldTP ) {
224  foreach ( $servers as $server ) {
225  $type = $server['type'] ?? null;
226  $srvDB = $server['dbname'] ?? null; // server DB
227  $srvTP = $server['tablePrefix'] ?? ''; // server table prefix
228 
229  if ( $type === 'mysql' ) {
230  // A DB name is not needed to connect to mysql; 'dbname' is useless.
231  // This field only defines the DB to use for unspecified DB domains.
232  if ( $srvDB !== null && $srvDB !== $ldDB ) {
233  self::reportMismatchedDBs( $srvDB, $ldDB );
234  }
235  } elseif ( $type === 'postgres' ) {
236  if ( $srvTP !== '' ) {
237  self::reportIfPrefixSet( $srvTP, $type );
238  }
239  }
240 
241  if ( $srvTP !== '' && $srvTP !== $ldTP ) {
242  self::reportMismatchedPrefixes( $srvTP, $ldTP );
243  }
244  }
245  }
246 
251  private static function reportIfPrefixSet( $prefix, $dbType ) {
252  $e = new UnexpectedValueException(
253  "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
254  "MediaWiki does not support using a table prefix with this RDBMS type."
255  );
257  exit;
258  }
259 
264  private static function reportMismatchedDBs( $srvDB, $ldDB ) {
265  $e = new UnexpectedValueException(
266  "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
267  "Set \$wgDBname to the database used by this wiki project. " .
268  "There is rarely a need to set 'dbname' in \$wgDBservers. " .
269  "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
270  "use of Database::getDomainId(), and other features are not reliable when " .
271  "\$wgDBservers does not match the local wiki database/prefix."
272  );
274  exit;
275  }
276 
281  private static function reportMismatchedPrefixes( $srvTP, $ldTP ) {
282  $e = new UnexpectedValueException(
283  "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
284  "Set \$wgDBprefix to the table prefix used by this wiki project. " .
285  "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
286  "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
287  "use of Database::getDomainId(), and other features are not reliable when " .
288  "\$wgDBservers does not match the local wiki database/prefix."
289  );
291  exit;
292  }
293 
301  public static function getLBFactoryClass( array $config ) {
302  $compat = [
303  // For LocalSettings.php compat after removing underscores (since 1.23).
304  'LBFactory_Single' => Wikimedia\Rdbms\LBFactorySingle::class,
305  'LBFactory_Simple' => Wikimedia\Rdbms\LBFactorySimple::class,
306  'LBFactory_Multi' => Wikimedia\Rdbms\LBFactoryMulti::class,
307  // For LocalSettings.php compat after moving classes to namespaces (since 1.29).
308  'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
309  'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
310  'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
311  ];
312 
313  $class = $config['class'];
314  return $compat[$class] ?? $class;
315  }
316 
320  public static function setDomainAliases( ILBFactory $lbFactory ) {
321  $domain = DatabaseDomain::newFromId( $lbFactory->getLocalDomainID() );
322  // For compatibility with hyphenated $wgDBname values on older wikis, handle callers
323  // that assume corresponding database domain IDs and wiki IDs have identical values
324  $rawLocalDomain = strlen( $domain->getTablePrefix() )
325  ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}"
326  : (string)$domain->getDatabase();
327 
328  $lbFactory->setDomainAliases( [ $rawLocalDomain => $domain ] );
329  }
330 
336  public static function logDeprecation( $msg ) {
337  if ( isset( self::$loggedDeprecations[$msg] ) ) {
338  return;
339  }
340  self::$loggedDeprecations[$msg] = true;
341  MWDebug::sendRawDeprecated( $msg, true, wfGetCaller() );
342  }
343 }
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.
MWLBFactory\reportMismatchedDBs
static reportMismatchedDBs( $srvDB, $ldDB)
Definition: MWLBFactory.php:264
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:69
MWLBFactory\initServerInfo
static initServerInfo(array $server, ServiceOptions $options)
Definition: MWLBFactory.php:178
MWLBFactory\assertValidServerConfigs
static assertValidServerConfigs(array $servers, $ldDB, $ldTP)
Definition: MWLBFactory.php:223
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()
Get host name of the current machine, for use in error reporting.
Definition: GlobalFunctions.php:1283
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:71
DBO_SSL
const DBO_SSL
Definition: defines.php:17
MWLBFactory\logDeprecation
static logDeprecation( $msg)
Log a database deprecation warning.
Definition: MWLBFactory.php:336
MWLBFactory\setDomainAliases
static setDomainAliases(ILBFactory $lbFactory)
Definition: MWLBFactory.php:320
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:281
MWLBFactory\$loggedDeprecations
static array $loggedDeprecations
Cache of already-logged deprecation messages.
Definition: MWLBFactory.php:36
MWLBFactory\APPLY_DEFAULT_CONFIG_OPTIONS
const APPLY_DEFAULT_CONFIG_OPTIONS
Definition: MWLBFactory.php:41
MediaWiki\Config\ServiceOptions
A class for passing options to services.
Definition: ServiceOptions.php:25
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
$wgCommandLineMode
global $wgCommandLineMode
Definition: DevelopmentSettings.php:29
MWLBFactory\applyDefaultConfig
static applyDefaultConfig(array $lbConf, ServiceOptions $options, ConfiguredReadOnlyMode $readOnlyMode, BagOStuff $srvCache, BagOStuff $mainStash, WANObjectCache $wanCache)
Definition: MWLBFactory.php:72
MWExceptionRenderer\output
static output(Throwable $e, $mode, Throwable $eNew=null)
Definition: MWExceptionRenderer.php:40
MWLBFactory\getLBFactoryClass
static getLBFactoryClass(array $config)
Decide which LBFactory class to use.
Definition: MWLBFactory.php:301
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:125
Wikimedia\Rdbms\ILBFactory\setDomainAliases
setDomainAliases(array $aliases)
Convert certain database domains to alternative ones.
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:169
wfGetCaller
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
Definition: GlobalFunctions.php:1397
MWDebug\sendRawDeprecated
static sendRawDeprecated( $msg, $sendToLog=true, $callerFunc='')
Send a raw deprecation message to the log and the debug toolbar, without filtering of duplicate messa...
Definition: MWDebug.php:321
MWLBFactory\reportIfPrefixSet
static reportIfPrefixSet( $prefix, $dbType)
Definition: MWLBFactory.php:251
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