MediaWiki  1.30.0
MWLBFactory.php
Go to the documentation of this file.
1 <?php
27 
32 abstract class MWLBFactory {
39  public static function applyDefaultConfig( array $lbConf, Config $mainConfig,
40  ConfiguredReadOnlyMode $readOnlyMode
41  ) {
43 
44  static $typesWithSchema = [ 'postgres', 'msssql' ];
45 
46  $lbConf += [
47  'localDomain' => new DatabaseDomain(
48  $mainConfig->get( 'DBname' ),
49  null,
50  $mainConfig->get( 'DBprefix' )
51  ),
52  'profiler' => Profiler::instance(),
53  'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
54  'replLogger' => LoggerFactory::getInstance( 'DBReplication' ),
55  'queryLogger' => LoggerFactory::getInstance( 'DBQuery' ),
56  'connLogger' => LoggerFactory::getInstance( 'DBConnection' ),
57  'perfLogger' => LoggerFactory::getInstance( 'DBPerformance' ),
58  'errorLogger' => [ MWExceptionHandler::class, 'logException' ],
59  'cliMode' => $wgCommandLineMode,
60  'hostname' => wfHostname(),
61  'readOnlyReason' => $readOnlyMode->getReason(),
62  ];
63 
64  // When making changes here, remember to also specify MediaWiki-specific options
65  // for Database classes in the relevant Installer subclass.
66  // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
67  if ( $lbConf['class'] === 'LBFactorySimple' ) {
68  if ( isset( $lbConf['servers'] ) ) {
69  // Server array is already explicitly configured; leave alone
70  } elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
71  foreach ( $mainConfig->get( 'DBservers' ) as $i => $server ) {
72  if ( $server['type'] === 'sqlite' ) {
73  $server += [ 'dbDirectory' => $mainConfig->get( 'SQLiteDataDir' ) ];
74  } elseif ( $server['type'] === 'postgres' ) {
75  $server += [
76  'port' => $mainConfig->get( 'DBport' ),
77  // Work around the reserved word usage in MediaWiki schema
78  'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
79  ];
80  } elseif ( $server['type'] === 'mssql' ) {
81  $server += [
82  'port' => $mainConfig->get( 'DBport' ),
83  'useWindowsAuth' => $mainConfig->get( 'DBWindowsAuthentication' )
84  ];
85  }
86 
87  if ( in_array( $server['type'], $typesWithSchema, true ) ) {
88  $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
89  }
90 
91  $server += [
92  'tablePrefix' => $mainConfig->get( 'DBprefix' ),
93  'flags' => DBO_DEFAULT,
94  'sqlMode' => $mainConfig->get( 'SQLMode' ),
95  'utf8Mode' => $mainConfig->get( 'DBmysql5' )
96  ];
97 
98  $lbConf['servers'][$i] = $server;
99  }
100  } else {
102  $flags |= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG : 0;
103  $flags |= $mainConfig->get( 'DBssl' ) ? DBO_SSL : 0;
104  $flags |= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
105  $server = [
106  'host' => $mainConfig->get( 'DBserver' ),
107  'user' => $mainConfig->get( 'DBuser' ),
108  'password' => $mainConfig->get( 'DBpassword' ),
109  'dbname' => $mainConfig->get( 'DBname' ),
110  'tablePrefix' => $mainConfig->get( 'DBprefix' ),
111  'type' => $mainConfig->get( 'DBtype' ),
112  'load' => 1,
113  'flags' => $flags,
114  'sqlMode' => $mainConfig->get( 'SQLMode' ),
115  'utf8Mode' => $mainConfig->get( 'DBmysql5' )
116  ];
117  if ( in_array( $server['type'], $typesWithSchema, true ) ) {
118  $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
119  }
120  if ( $server['type'] === 'sqlite' ) {
121  $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
122  } elseif ( $server['type'] === 'postgres' ) {
123  $server['port'] = $mainConfig->get( 'DBport' );
124  // Work around the reserved word usage in MediaWiki schema
125  $server['keywordTableMap'] = [ 'user' => 'mwuser', 'text' => 'pagecontent' ];
126  } elseif ( $server['type'] === 'mssql' ) {
127  $server['port'] = $mainConfig->get( 'DBport' );
128  $server['useWindowsAuth'] = $mainConfig->get( 'DBWindowsAuthentication' );
129  }
130  $lbConf['servers'] = [ $server ];
131  }
132  if ( !isset( $lbConf['externalClusters'] ) ) {
133  $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
134  }
135  } elseif ( $lbConf['class'] === 'LBFactoryMulti' ) {
136  if ( isset( $lbConf['serverTemplate'] ) ) {
137  if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
138  $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
139  }
140  $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
141  $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
142  }
143  }
144 
145  // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
146  $sCache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
147  if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
148  $lbConf['srvCache'] = $sCache;
149  }
151  if ( $cCache->getQoS( $cCache::ATTR_EMULATION ) > $cCache::QOS_EMULATION_SQL ) {
152  $lbConf['memStash'] = $cCache;
153  }
154  $wCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
155  if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
156  $lbConf['wanCache'] = $wCache;
157  }
158 
159  return $lbConf;
160  }
161 
170  public static function getLBFactoryClass( array $config ) {
171  // For configuration backward compatibility after removing
172  // underscores from class names in MediaWiki 1.23.
173  $bcClasses = [
174  'LBFactory_Simple' => 'LBFactorySimple',
175  'LBFactory_Single' => 'LBFactorySingle',
176  'LBFactory_Multi' => 'LBFactoryMulti'
177  ];
178 
179  $class = $config['class'];
180 
181  if ( isset( $bcClasses[$class] ) ) {
182  $class = $bcClasses[$class];
183  wfDeprecated(
184  '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
185  '1.23'
186  );
187  }
188 
189  // For configuration backward compatibility after moving classes to namespaces (1.29)
190  $compat = [
191  'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
192  'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
193  'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
194  ];
195 
196  if ( isset( $compat[$class] ) ) {
197  $class = $compat[$class];
198  }
199 
200  return $class;
201  }
202 }
MWLBFactory
MediaWiki-specific class for generating database load balancers.
Definition: MWLBFactory.php:32
ObjectCache\getLocalClusterInstance
static getLocalClusterInstance()
Get the main cluster-local cache object.
Definition: ObjectCache.php:357
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:62
DBO_DEBUG
const DBO_DEBUG
Definition: defines.php:9
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
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:1482
DBO_SSL
const DBO_SSL
Definition: defines.php:17
ConfiguredReadOnlyMode\getReason
getReason()
Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
Definition: ConfiguredReadOnlyMode.php:37
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1176
$wgCommandLineMode
global $wgCommandLineMode
Definition: Setup.php:526
MWLBFactory\getLBFactoryClass
static getLBFactoryClass(array $config)
Returns the LBFactory class to use and the load balancer configuration.
Definition: MWLBFactory.php:170
MWLBFactory\applyDefaultConfig
static applyDefaultConfig(array $lbConf, Config $mainConfig, ConfiguredReadOnlyMode $readOnlyMode)
Definition: MWLBFactory.php:39
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DBO_COMPRESS
const DBO_COMPRESS
Definition: defines.php:18
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
LoggerFactory
MediaWiki Logger LoggerFactory implements a PSR[0] compatible message logging system Named Psr Log LoggerInterface instances can be obtained from the MediaWiki Logger LoggerFactory::getInstance() static method. MediaWiki\Logger\LoggerFactory expects a class implementing the MediaWiki\Logger\Spi interface to act as a factory for new Psr\Log\LoggerInterface instances. The "Spi" in MediaWiki\Logger\Spi stands for "service provider interface". An SPI is an API intended to be implemented or extended by a third party. This software design pattern is intended to enable framework extension and replaceable components. It is specifically used in the MediaWiki\Logger\LoggerFactory service to allow alternate PSR-3 logging implementations to be easily integrated with MediaWiki. The service provider interface allows the backend logging library to be implemented in multiple ways. The $wgMWLoggerDefaultSpi global provides the classname of the default MediaWiki\Logger\Spi implementation to be loaded at runtime. This can either be the name of a class implementing the MediaWiki\Logger\Spi with a zero argument const ructor or a callable that will return an MediaWiki\Logger\Spi instance. Alternately the MediaWiki\Logger\LoggerFactory MediaWiki Logger LoggerFactory
Definition: logger.txt:5
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/prefix specification for IDatabase domains.
Definition: DatabaseDomain.php:28
DBO_DEFAULT
const DBO_DEFAULT
Definition: defines.php:13
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2801
array
the array() calling protocol came about after MediaWiki 1.4rc1.