Completed
Branch master (e770d9)
by
unknown
33:41
created

MWLBFactory::applyDefaultConfig()   F

Complexity

Conditions 18
Paths 440

Size

Total Lines 92
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 70
nc 440
nop 2
dl 0
loc 92
rs 3.4591
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 * MediaWiki-specific class for generating database load balancers
28
 * @ingroup Database
29
 */
30
abstract class MWLBFactory {
31
	/**
32
	 * @param array $lbConf Config for LBFactory::__construct()
33
	 * @param Config $mainConfig Main config object from MediaWikiServices
34
	 * @return array
35
	 */
36
	public static function applyDefaultConfig( array $lbConf, Config $mainConfig ) {
37
		global $wgCommandLineMode;
38
39
		$lbConf += [
40
			'localDomain' => new DatabaseDomain(
41
				$mainConfig->get( 'DBname' ),
42
				null,
43
				$mainConfig->get( 'DBprefix' )
44
			),
45
			'profiler' => Profiler::instance(),
46
			'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
47
			'replLogger' => LoggerFactory::getInstance( 'DBReplication' ),
48
			'queryLogger' => LoggerFactory::getInstance( 'DBQuery' ),
49
			'connLogger' => LoggerFactory::getInstance( 'DBConnection' ),
50
			'perfLogger' => LoggerFactory::getInstance( 'DBPerformance' ),
51
			'errorLogger' => [ MWExceptionHandler::class, 'logException' ],
52
			'cliMode' => $wgCommandLineMode,
53
			'hostname' => wfHostname(),
54
			// TODO: replace the global wfConfiguredReadOnlyReason() with a service.
55
			'readOnlyReason' => wfConfiguredReadOnlyReason(),
56
		];
57
58
		if ( $lbConf['class'] === 'LBFactorySimple' ) {
59
			if ( isset( $lbConf['servers'] ) ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
60
				// Server array is already explicitly configured; leave alone
61
			} elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
62
				foreach ( $mainConfig->get( 'DBservers' ) as $i => $server ) {
63
					if ( $server['type'] === 'sqlite' ) {
64
						$server += [ 'dbDirectory' => $mainConfig->get( 'SQLiteDataDir' ) ];
65
					} elseif ( $server['type'] === 'postgres' ) {
66
						$server += [ 'port' => $mainConfig->get( 'DBport' ) ];
67
					}
68
					$lbConf['servers'][$i] = $server + [
69
						'schema' => $mainConfig->get( 'DBmwschema' ),
70
						'tablePrefix' => $mainConfig->get( 'DBprefix' ),
71
						'flags' => DBO_DEFAULT,
72
						'sqlMode' => $mainConfig->get( 'SQLMode' ),
73
						'utf8Mode' => $mainConfig->get( 'DBmysql5' )
74
					];
75
				}
76
			} else {
77
				$flags = DBO_DEFAULT;
78
				$flags |= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG : 0;
79
				$flags |= $mainConfig->get( 'DBssl' ) ? DBO_SSL : 0;
80
				$flags |= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
81
				$server = [
82
					'host' => $mainConfig->get( 'DBserver' ),
83
					'user' => $mainConfig->get( 'DBuser' ),
84
					'password' => $mainConfig->get( 'DBpassword' ),
85
					'dbname' => $mainConfig->get( 'DBname' ),
86
					'schema' => $mainConfig->get( 'DBmwschema' ),
87
					'tablePrefix' => $mainConfig->get( 'DBprefix' ),
88
					'type' => $mainConfig->get( 'DBtype' ),
89
					'load' => 1,
90
					'flags' => $flags,
91
					'sqlMode' => $mainConfig->get( 'SQLMode' ),
92
					'utf8Mode' => $mainConfig->get( 'DBmysql5' )
93
				];
94
				if ( $server['type'] === 'sqlite' ) {
95
					$server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
96
				} elseif ( $server['type'] === 'postgres' ) {
97
					$server['port'] = $mainConfig->get( 'DBport' );
98
				}
99
				$lbConf['servers'] = [ $server ];
100
			}
101
			if ( !isset( $lbConf['externalClusters'] ) ) {
102
				$lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
103
			}
104
		} elseif ( $lbConf['class'] === 'LBFactoryMulti' ) {
105
			if ( isset( $lbConf['serverTemplate'] ) ) {
106
				$lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
107
				$lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
108
				$lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
109
			}
110
		}
111
112
		// Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
113
		$sCache = ObjectCache::getLocalServerInstance();
114
		if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
115
			$lbConf['srvCache'] = $sCache;
116
		}
117
		$cCache = ObjectCache::getLocalClusterInstance();
118
		if ( $cCache->getQoS( $cCache::ATTR_EMULATION ) > $cCache::QOS_EMULATION_SQL ) {
119
			$lbConf['memCache'] = $cCache;
120
		}
121
		$wCache = ObjectCache::getMainWANInstance();
122
		if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
123
			$lbConf['wanCache'] = $wCache;
124
		}
125
126
		return $lbConf;
127
	}
128
129
	/**
130
	 * Returns the LBFactory class to use and the load balancer configuration.
131
	 *
132
	 * @todo instead of this, use a ServiceContainer for managing the different implementations.
133
	 *
134
	 * @param array $config (e.g. $wgLBFactoryConf)
135
	 * @return string Class name
136
	 */
137
	public static function getLBFactoryClass( array $config ) {
138
		// For configuration backward compatibility after removing
139
		// underscores from class names in MediaWiki 1.23.
140
		$bcClasses = [
141
			'LBFactory_Simple' => 'LBFactorySimple',
142
			'LBFactory_Single' => 'LBFactorySingle',
143
			'LBFactory_Multi' => 'LBFactoryMulti'
144
		];
145
146
		$class = $config['class'];
147
148
		if ( isset( $bcClasses[$class] ) ) {
149
			$class = $bcClasses[$class];
150
			wfDeprecated(
151
				'$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
152
				'1.23'
153
			);
154
		}
155
156
		return $class;
157
	}
158
}
159