MediaWiki  1.31.0
MWLBFactory.php
Go to the documentation of this file.
1 <?php
28 
33 abstract class MWLBFactory {
34 
36  private static $loggedDeprecations = [];
37 
44  public static function applyDefaultConfig( array $lbConf, Config $mainConfig,
45  ConfiguredReadOnlyMode $readOnlyMode
46  ) {
48 
49  static $typesWithSchema = [ 'postgres', 'msssql' ];
50 
51  $lbConf += [
52  'localDomain' => new DatabaseDomain(
53  $mainConfig->get( 'DBname' ),
54  $mainConfig->get( 'DBmwschema' ),
55  $mainConfig->get( 'DBprefix' )
56  ),
57  'profiler' => Profiler::instance(),
58  'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
59  'replLogger' => LoggerFactory::getInstance( 'DBReplication' ),
60  'queryLogger' => LoggerFactory::getInstance( 'DBQuery' ),
61  'connLogger' => LoggerFactory::getInstance( 'DBConnection' ),
62  'perfLogger' => LoggerFactory::getInstance( 'DBPerformance' ),
63  'errorLogger' => [ MWExceptionHandler::class, 'logException' ],
64  'deprecationLogger' => [ static::class, 'logDeprecation' ],
65  'cliMode' => $wgCommandLineMode,
66  'hostname' => wfHostname(),
67  'readOnlyReason' => $readOnlyMode->getReason(),
68  ];
69 
70  // When making changes here, remember to also specify MediaWiki-specific options
71  // for Database classes in the relevant Installer subclass.
72  // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
73  if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple::class ) {
74  if ( isset( $lbConf['servers'] ) ) {
75  // Server array is already explicitly configured; leave alone
76  } elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
77  foreach ( $mainConfig->get( 'DBservers' ) as $i => $server ) {
78  if ( $server['type'] === 'sqlite' ) {
79  $server += [ 'dbDirectory' => $mainConfig->get( 'SQLiteDataDir' ) ];
80  } elseif ( $server['type'] === 'postgres' ) {
81  $server += [
82  'port' => $mainConfig->get( 'DBport' ),
83  // Work around the reserved word usage in MediaWiki schema
84  'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
85  ];
86  } elseif ( $server['type'] === 'mssql' ) {
87  $server += [
88  'port' => $mainConfig->get( 'DBport' ),
89  'useWindowsAuth' => $mainConfig->get( 'DBWindowsAuthentication' )
90  ];
91  }
92 
93  if ( in_array( $server['type'], $typesWithSchema, true ) ) {
94  $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
95  }
96 
97  $server += [
98  'tablePrefix' => $mainConfig->get( 'DBprefix' ),
99  'flags' => DBO_DEFAULT,
100  'sqlMode' => $mainConfig->get( 'SQLMode' ),
101  'utf8Mode' => $mainConfig->get( 'DBmysql5' )
102  ];
103 
104  $lbConf['servers'][$i] = $server;
105  }
106  } else {
107  $flags = DBO_DEFAULT;
108  $flags |= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG : 0;
109  $flags |= $mainConfig->get( 'DBssl' ) ? DBO_SSL : 0;
110  $flags |= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
111  $server = [
112  'host' => $mainConfig->get( 'DBserver' ),
113  'user' => $mainConfig->get( 'DBuser' ),
114  'password' => $mainConfig->get( 'DBpassword' ),
115  'dbname' => $mainConfig->get( 'DBname' ),
116  'tablePrefix' => $mainConfig->get( 'DBprefix' ),
117  'type' => $mainConfig->get( 'DBtype' ),
118  'load' => 1,
119  'flags' => $flags,
120  'sqlMode' => $mainConfig->get( 'SQLMode' ),
121  'utf8Mode' => $mainConfig->get( 'DBmysql5' )
122  ];
123  if ( in_array( $server['type'], $typesWithSchema, true ) ) {
124  $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
125  }
126  if ( $server['type'] === 'sqlite' ) {
127  $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
128  } elseif ( $server['type'] === 'postgres' ) {
129  $server['port'] = $mainConfig->get( 'DBport' );
130  // Work around the reserved word usage in MediaWiki schema
131  $server['keywordTableMap'] = [ 'user' => 'mwuser', 'text' => 'pagecontent' ];
132  } elseif ( $server['type'] === 'mssql' ) {
133  $server['port'] = $mainConfig->get( 'DBport' );
134  $server['useWindowsAuth'] = $mainConfig->get( 'DBWindowsAuthentication' );
135  }
136  $lbConf['servers'] = [ $server ];
137  }
138  if ( !isset( $lbConf['externalClusters'] ) ) {
139  $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
140  }
141  } elseif ( $lbConf['class'] === Wikimedia\Rdbms\LBFactoryMulti::class ) {
142  if ( isset( $lbConf['serverTemplate'] ) ) {
143  if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
144  $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
145  }
146  $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
147  $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
148  }
149  }
150 
151  $services = MediaWikiServices::getInstance();
152 
153  // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
154  $sCache = $services->getLocalServerObjectCache();
155  if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
156  $lbConf['srvCache'] = $sCache;
157  }
158  $mStash = $services->getMainObjectStash();
159  if ( $mStash->getQoS( $mStash::ATTR_EMULATION ) > $mStash::QOS_EMULATION_SQL ) {
160  $lbConf['memStash'] = $mStash;
161  }
162  $wCache = $services->getMainWANObjectCache();
163  if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
164  $lbConf['wanCache'] = $wCache;
165  }
166 
167  return $lbConf;
168  }
169 
178  public static function getLBFactoryClass( array $config ) {
179  // For configuration backward compatibility after removing
180  // underscores from class names in MediaWiki 1.23.
181  $bcClasses = [
182  'LBFactory_Simple' => 'LBFactorySimple',
183  'LBFactory_Single' => 'LBFactorySingle',
184  'LBFactory_Multi' => 'LBFactoryMulti'
185  ];
186 
187  $class = $config['class'];
188 
189  if ( isset( $bcClasses[$class] ) ) {
190  $class = $bcClasses[$class];
191  wfDeprecated(
192  '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
193  '1.23'
194  );
195  }
196 
197  // For configuration backward compatibility after moving classes to namespaces (1.29)
198  $compat = [
199  'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
200  'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
201  'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
202  ];
203 
204  if ( isset( $compat[$class] ) ) {
205  $class = $compat[$class];
206  }
207 
208  return $class;
209  }
210 
211  public static function setSchemaAliases( LBFactory $lbFactory, Config $config ) {
212  if ( $config->get( 'DBtype' ) === 'mysql' ) {
227  $lbFactory->setIndexAliases( [
228  'ar_usertext_timestamp' => 'usertext_timestamp',
229  'un_user_id' => 'user_id',
230  'un_user_ip' => 'user_ip',
231  ] );
232  }
233  }
234 
239  public static function logDeprecation( $msg ) {
241 
242  if ( isset( self::$loggedDeprecations[$msg] ) ) {
243  return;
244  }
245  self::$loggedDeprecations[$msg] = true;
246 
247  if ( $wgDevelopmentWarnings ) {
248  trigger_error( $msg, E_USER_DEPRECATED );
249  }
250  wfDebugLog( 'deprecated', $msg, 'private' );
251  }
252 }
MWLBFactory
MediaWiki-specific class for generating database load balancers.
Definition: MWLBFactory.php:33
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:1408
DBO_SSL
const DBO_SSL
Definition: defines.php:17
MWLBFactory\logDeprecation
static logDeprecation( $msg)
Log a database deprecation warning.
Definition: MWLBFactory.php:239
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:1075
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
MWLBFactory\$loggedDeprecations
static array $loggedDeprecations
Cache of already-logged deprecation messages.
Definition: MWLBFactory.php:36
Config
Interface for configuration instances.
Definition: Config.php:28
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1111
Config\get
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
$wgCommandLineMode
global $wgCommandLineMode
Definition: DevelopmentSettings.php:27
MWLBFactory\getLBFactoryClass
static getLBFactoryClass(array $config)
Returns the LBFactory class to use and the load balancer configuration.
Definition: MWLBFactory.php:178
MWLBFactory\applyDefaultConfig
static applyDefaultConfig(array $lbConf, Config $mainConfig, ConfiguredReadOnlyMode $readOnlyMode)
Definition: MWLBFactory.php:44
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
$services
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition: hooks.txt:2220
DBO_COMPRESS
const DBO_COMPRESS
Definition: defines.php:18
MWLBFactory\setSchemaAliases
static setSchemaAliases(LBFactory $lbFactory, Config $config)
Definition: MWLBFactory.php:211
$wgDevelopmentWarnings
$wgDevelopmentWarnings
If set to true MediaWiki will throw notices for some possible error conditions and for deprecated fun...
Definition: DefaultSettings.php:6287
Wikimedia\Rdbms\LBFactory
An interface for generating database load balancers.
Definition: LBFactory.php:39
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
Wikimedia
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
Wikimedia\Rdbms\LBFactory\setIndexAliases
setIndexAliases(array $aliases)
Convert certain index names to alternative names before querying the DB.
Definition: LBFactory.php:555
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
array
the array() calling protocol came about after MediaWiki 1.4rc1.