MediaWiki REL1_32
MWLBFactory.php
Go to the documentation of this file.
1<?php
28
33abstract class MWLBFactory {
34
36 private static $loggedDeprecations = [];
37
44 public static function applyDefaultConfig( array $lbConf, Config $mainConfig,
45 ConfiguredReadOnlyMode $readOnlyMode
46 ) {
47 global $wgCommandLineMode;
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 'defaultGroup' => $mainConfig->get( 'DBDefaultGroup' ),
69 ];
70
71 // When making changes here, remember to also specify MediaWiki-specific options
72 // for Database classes in the relevant Installer subclass.
73 // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
74 if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple::class ) {
75 if ( isset( $lbConf['servers'] ) ) {
76 // Server array is already explicitly configured; leave alone
77 } elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
78 foreach ( $mainConfig->get( 'DBservers' ) as $i => $server ) {
79 if ( $server['type'] === 'sqlite' ) {
80 $server += [ 'dbDirectory' => $mainConfig->get( 'SQLiteDataDir' ) ];
81 } elseif ( $server['type'] === 'postgres' ) {
82 $server += [
83 'port' => $mainConfig->get( 'DBport' ),
84 // Work around the reserved word usage in MediaWiki schema
85 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
86 ];
87 } elseif ( $server['type'] === 'mssql' ) {
88 $server += [
89 'port' => $mainConfig->get( 'DBport' ),
90 'useWindowsAuth' => $mainConfig->get( 'DBWindowsAuthentication' )
91 ];
92 }
93
94 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
95 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
96 }
97
98 $server += [
99 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
100 'flags' => DBO_DEFAULT,
101 'sqlMode' => $mainConfig->get( 'SQLMode' ),
102 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
103 ];
104
105 $lbConf['servers'][$i] = $server;
106 }
107 } else {
108 $flags = DBO_DEFAULT;
109 $flags |= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG : 0;
110 $flags |= $mainConfig->get( 'DBssl' ) ? DBO_SSL : 0;
111 $flags |= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
112 $server = [
113 'host' => $mainConfig->get( 'DBserver' ),
114 'user' => $mainConfig->get( 'DBuser' ),
115 'password' => $mainConfig->get( 'DBpassword' ),
116 'dbname' => $mainConfig->get( 'DBname' ),
117 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
118 'type' => $mainConfig->get( 'DBtype' ),
119 'load' => 1,
120 'flags' => $flags,
121 'sqlMode' => $mainConfig->get( 'SQLMode' ),
122 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
123 ];
124 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
125 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
126 }
127 if ( $server['type'] === 'sqlite' ) {
128 $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
129 } elseif ( $server['type'] === 'postgres' ) {
130 $server['port'] = $mainConfig->get( 'DBport' );
131 // Work around the reserved word usage in MediaWiki schema
132 $server['keywordTableMap'] = [ 'user' => 'mwuser', 'text' => 'pagecontent' ];
133 } elseif ( $server['type'] === 'mssql' ) {
134 $server['port'] = $mainConfig->get( 'DBport' );
135 $server['useWindowsAuth'] = $mainConfig->get( 'DBWindowsAuthentication' );
136 }
137 $lbConf['servers'] = [ $server ];
138 }
139 if ( !isset( $lbConf['externalClusters'] ) ) {
140 $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
141 }
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'] = $mainConfig->get( 'DBmwschema' );
146 }
147 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
148 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
149 }
150 }
151
152 $services = MediaWikiServices::getInstance();
153
154 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
155 $sCache = $services->getLocalServerObjectCache();
156 if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
157 $lbConf['srvCache'] = $sCache;
158 }
159 $mStash = $services->getMainObjectStash();
160 if ( $mStash->getQoS( $mStash::ATTR_EMULATION ) > $mStash::QOS_EMULATION_SQL ) {
161 $lbConf['memStash'] = $mStash;
162 }
163 $wCache = $services->getMainWANObjectCache();
164 if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
165 $lbConf['wanCache'] = $wCache;
166 }
167
168 return $lbConf;
169 }
170
179 public static function getLBFactoryClass( array $config ) {
180 // For configuration backward compatibility after removing
181 // underscores from class names in MediaWiki 1.23.
182 $bcClasses = [
183 'LBFactory_Simple' => 'LBFactorySimple',
184 'LBFactory_Single' => 'LBFactorySingle',
185 'LBFactory_Multi' => 'LBFactoryMulti'
186 ];
187
188 $class = $config['class'];
189
190 if ( isset( $bcClasses[$class] ) ) {
191 $class = $bcClasses[$class];
193 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
194 '1.23'
195 );
196 }
197
198 // For configuration backward compatibility after moving classes to namespaces (1.29)
199 $compat = [
200 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
201 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
202 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
203 ];
204
205 if ( isset( $compat[$class] ) ) {
206 $class = $compat[$class];
207 }
208
209 return $class;
210 }
211
212 public static function setSchemaAliases( LBFactory $lbFactory, Config $config ) {
213 if ( $config->get( 'DBtype' ) === 'mysql' ) {
228 $lbFactory->setIndexAliases( [
229 'ar_usertext_timestamp' => 'usertext_timestamp',
230 'un_user_id' => 'user_id',
231 'un_user_ip' => 'user_ip',
232 ] );
233 }
234 }
235
240 public static function logDeprecation( $msg ) {
242
243 if ( isset( self::$loggedDeprecations[$msg] ) ) {
244 return;
245 }
246 self::$loggedDeprecations[$msg] = true;
247
249 trigger_error( $msg, E_USER_DEPRECATED );
250 }
251 wfDebugLog( 'deprecated', $msg, 'private' );
252 }
253}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$wgDevelopmentWarnings
If set to true MediaWiki will throw notices for some possible error conditions and for deprecated fun...
global $wgCommandLineMode
wfHostname()
Fetch server name for use in error reporting etc.
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.
A read-only mode service which does not depend on LoadBalancer.
getReason()
Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
MediaWiki-specific class for generating database load balancers.
static applyDefaultConfig(array $lbConf, Config $mainConfig, ConfiguredReadOnlyMode $readOnlyMode)
static logDeprecation( $msg)
Log a database deprecation warning.
static array $loggedDeprecations
Cache of already-logged deprecation messages.
static getLBFactoryClass(array $config)
Returns the LBFactory class to use and the load balancer configuration.
static setSchemaAliases(LBFactory $lbFactory, Config $config)
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Class to handle database/prefix specification for IDatabase domains.
An interface for generating database load balancers.
Definition LBFactory.php:39
setIndexAliases(array $aliases)
Convert certain index names to alternative names before querying the DB.
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
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response 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:2335
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:37
Interface for configuration instances.
Definition Config.php:28
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
const DBO_COMPRESS
Definition defines.php:18
const DBO_DEFAULT
Definition defines.php:13
const DBO_SSL
Definition defines.php:17
const DBO_DEBUG
Definition defines.php:9