Merge "Make Status extend StatusValue and start FileBackend update"
[lhc/web/wiklou.git] / includes / db / loadbalancer / LBFactoryMW.php
1 <?php
2 /**
3 * Generator of database load balancing objects.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 use MediaWiki\Logger\LoggerFactory;
25
26 /**
27 * Legacy MediaWiki-specific class for generating database load balancers
28 * @ingroup Database
29 */
30 abstract class LBFactoryMW extends LBFactory {
31 /** @noinspection PhpMissingParentConstructorInspection */
32 /**
33 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
34 * @param array $conf
35 * @TODO: inject objects via dependency framework
36 */
37 public function __construct( array $conf ) {
38 parent::__construct( self::applyDefaultConfig( $conf ) );
39 }
40
41 /**
42 * @param array $conf
43 * @return array
44 * @TODO: inject objects via dependency framework
45 */
46 public static function applyDefaultConfig( array $conf ) {
47 global $wgCommandLineMode, $wgSQLMode, $wgDBmysql5, $wgDBname, $wgDBprefix;
48
49 $defaults = [
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,
60 'agent' => ''
61 ];
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;
66 }
67 $cCache = ObjectCache::getLocalClusterInstance();
68 if ( $cCache->getQoS( $cCache::ATTR_EMULATION ) > $cCache::QOS_EMULATION_SQL ) {
69 $defaults['memCache'] = $cCache;
70 }
71 $wCache = ObjectCache::getMainWANInstance();
72 if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
73 $defaults['wanCache'] = $wCache;
74 }
75
76 if ( isset( $conf['serverTemplate'] ) ) { // LBFactoryMulti
77 $conf['serverTemplate']['sqlMode'] = $wgSQLMode;
78 $conf['serverTemplate']['utf8Mode'] = $wgDBmysql5;
79 }
80
81 return $conf + $defaults;
82 }
83
84 /**
85 * Returns the LBFactory class to use and the load balancer configuration.
86 *
87 * @todo instead of this, use a ServiceContainer for managing the different implementations.
88 *
89 * @param array $config (e.g. $wgLBFactoryConf)
90 * @return string Class name
91 */
92 public static function getLBFactoryClass( array $config ) {
93 // For configuration backward compatibility after removing
94 // underscores from class names in MediaWiki 1.23.
95 $bcClasses = [
96 'LBFactory_Simple' => 'LBFactorySimple',
97 'LBFactory_Single' => 'LBFactorySingle',
98 'LBFactory_Multi' => 'LBFactoryMulti'
99 ];
100
101 $class = $config['class'];
102
103 if ( isset( $bcClasses[$class] ) ) {
104 $class = $bcClasses[$class];
105 wfDeprecated(
106 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
107 '1.23'
108 );
109 }
110
111 return $class;
112 }
113 }