3 * Generator of database load balancing objects.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use MediaWiki\Logger\LoggerFactory
;
27 * Legacy MediaWiki-specific class for generating database load balancers
30 abstract class LBFactoryMW
extends LBFactory
{
32 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
34 * @TODO: inject objects via dependency framework
36 public function __construct( array $conf ) {
37 parent
::__construct( self
::applyDefaultConfig( $conf ) );
43 * @TODO: inject objects via dependency framework
45 public static function applyDefaultConfig( array $conf ) {
46 global $wgDBtype, $wgSQLMode, $wgDBmysql5, $wgDBname, $wgDBprefix, $wgDBmwschema;
47 global $wgCommandLineMode;
50 'localDomain' => new DatabaseDomain( $wgDBname, null, $wgDBprefix ),
51 'hostname' => wfHostname(),
52 'profiler' => Profiler
::instance(),
53 'trxProfiler' => Profiler
::instance()->getTransactionProfiler(),
54 'replLogger' => LoggerFactory
::getInstance( 'DBReplication' ),
55 'queryLogger' => LoggerFactory
::getInstance( 'wfLogDBError' ),
56 'connLogger' => LoggerFactory
::getInstance( 'wfLogDBError' ),
57 'perfLogger' => LoggerFactory
::getInstance( 'DBPerformance' ),
58 'errorLogger' => [ MWExceptionHandler
::class, 'logException' ],
59 'cliMode' => $wgCommandLineMode,
62 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
63 $sCache = ObjectCache
::getLocalServerInstance();
64 if ( $sCache->getQoS( $sCache::ATTR_EMULATION
) > $sCache::QOS_EMULATION_SQL
) {
65 $defaults['srvCache'] = $sCache;
67 $cCache = ObjectCache
::getLocalClusterInstance();
68 if ( $cCache->getQoS( $cCache::ATTR_EMULATION
) > $cCache::QOS_EMULATION_SQL
) {
69 $defaults['memCache'] = $cCache;
71 $wCache = ObjectCache
::getMainWANInstance();
72 if ( $wCache->getQoS( $wCache::ATTR_EMULATION
) > $wCache::QOS_EMULATION_SQL
) {
73 $defaults['wanCache'] = $wCache;
76 // Determine schema defaults. Currently Microsoft SQL Server uses $wgDBmwschema,
77 // and everything else doesn't use a schema (e.g. null)
78 // Although postgres and oracle support schemas, we don't use them (yet)
79 // to maintain backwards compatibility
80 $schema = ( $wgDBtype === 'mssql' ) ?
$wgDBmwschema : null;
82 if ( isset( $conf['serverTemplate'] ) ) { // LBFactoryMulti
83 $conf['serverTemplate']['schema'] = $schema;
84 $conf['serverTemplate']['sqlMode'] = $wgSQLMode;
85 $conf['serverTemplate']['utf8Mode'] = $wgDBmysql5;
86 } elseif ( isset( $conf['servers'] ) ) { // LBFactorySimple
87 foreach ( $conf['servers'] as $i => $server ) {
88 $conf['servers'][$i]['schema'] = $schema;
92 return $conf +
$defaults;
96 * Returns the LBFactory class to use and the load balancer configuration.
98 * @todo instead of this, use a ServiceContainer for managing the different implementations.
100 * @param array $config (e.g. $wgLBFactoryConf)
101 * @return string Class name
103 public static function getLBFactoryClass( array $config ) {
104 // For configuration backward compatibility after removing
105 // underscores from class names in MediaWiki 1.23.
107 'LBFactory_Simple' => 'LBFactorySimple',
108 'LBFactory_Single' => 'LBFactorySingle',
109 'LBFactory_Multi' => 'LBFactoryMulti'
112 $class = $config['class'];
114 if ( isset( $bcClasses[$class] ) ) {
115 $class = $bcClasses[$class];
117 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',