Avoid MWDebug usage in DatabaseBase
[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 /**
32 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
33 * @param array $conf
34 * @TODO: inject objects via dependency framework
35 */
36 public function __construct( array $conf ) {
37 parent::__construct( self::applyDefaultConfig( $conf ) );
38 }
39
40 /**
41 * @param array $conf
42 * @return array
43 * @TODO: inject objects via dependency framework
44 */
45 public static function applyDefaultConfig( array $conf ) {
46 global $wgDBtype, $wgSQLMode, $wgDBmysql5, $wgDBname, $wgDBprefix, $wgDBmwschema;
47 global $wgCommandLineMode;
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( 'DBQuery' ),
56 'connLogger' => LoggerFactory::getInstance( 'DBConnection' ),
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 // 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;
81
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;
89 }
90 }
91
92 return $conf + $defaults;
93 }
94
95 /**
96 * Returns the LBFactory class to use and the load balancer configuration.
97 *
98 * @todo instead of this, use a ServiceContainer for managing the different implementations.
99 *
100 * @param array $config (e.g. $wgLBFactoryConf)
101 * @return string Class name
102 */
103 public static function getLBFactoryClass( array $config ) {
104 // For configuration backward compatibility after removing
105 // underscores from class names in MediaWiki 1.23.
106 $bcClasses = [
107 'LBFactory_Simple' => 'LBFactorySimple',
108 'LBFactory_Single' => 'LBFactorySingle',
109 'LBFactory_Multi' => 'LBFactoryMulti'
110 ];
111
112 $class = $config['class'];
113
114 if ( isset( $bcClasses[$class] ) ) {
115 $class = $bcClasses[$class];
116 wfDeprecated(
117 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
118 '1.23'
119 );
120 }
121
122 return $class;
123 }
124 }