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
{
31 /** @noinspection PhpMissingParentConstructorInspection */
33 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
35 * @TODO: inject objects via dependency framework
37 public function __construct( array $conf ) {
38 parent
::__construct( self
::applyDefaultConfig( $conf ) );
44 * @TODO: inject objects via dependency framework
46 public static function applyDefaultConfig( array $conf ) {
47 global $wgCommandLineMode, $wgSQLMode, $wgDBmysql5, $wgDBname, $wgDBprefix;
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 if ( isset( $conf['serverTemplate'] ) ) { // LBFactoryMulti
77 $conf['serverTemplate']['sqlMode'] = $wgSQLMode;
78 $conf['serverTemplate']['utf8Mode'] = $wgDBmysql5;
81 return $conf +
$defaults;
85 * Returns the LBFactory class to use and the load balancer configuration.
87 * @todo instead of this, use a ServiceContainer for managing the different implementations.
89 * @param array $config (e.g. $wgLBFactoryConf)
90 * @return string Class name
92 public static function getLBFactoryClass( array $config ) {
93 // For configuration backward compatibility after removing
94 // underscores from class names in MediaWiki 1.23.
96 'LBFactory_Simple' => 'LBFactorySimple',
97 'LBFactory_Single' => 'LBFactorySingle',
98 'LBFactory_Multi' => 'LBFactoryMulti'
101 $class = $config['class'];
103 if ( isset( $bcClasses[$class] ) ) {
104 $class = $bcClasses[$class];
106 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',