Fix ParserOutput::getText 'unwrap' flag for end-of-doc comment
[lhc/web/wiklou.git] / includes / db / MWLBFactory.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 use MediaWiki\MediaWikiServices;
26 use Wikimedia\Rdbms\DatabaseDomain;
27
28 /**
29 * MediaWiki-specific class for generating database load balancers
30 * @ingroup Database
31 */
32 abstract class MWLBFactory {
33 /**
34 * @param array $lbConf Config for LBFactory::__construct()
35 * @param Config $mainConfig Main config object from MediaWikiServices
36 * @param ConfiguredReadOnlyMode $readOnlyMode
37 * @return array
38 */
39 public static function applyDefaultConfig( array $lbConf, Config $mainConfig,
40 ConfiguredReadOnlyMode $readOnlyMode
41 ) {
42 global $wgCommandLineMode;
43
44 static $typesWithSchema = [ 'postgres', 'msssql' ];
45
46 $lbConf += [
47 'localDomain' => new DatabaseDomain(
48 $mainConfig->get( 'DBname' ),
49 null,
50 $mainConfig->get( 'DBprefix' )
51 ),
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 'hostname' => wfHostname(),
61 'readOnlyReason' => $readOnlyMode->getReason(),
62 ];
63
64 // When making changes here, remember to also specify MediaWiki-specific options
65 // for Database classes in the relevant Installer subclass.
66 // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
67 if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple::class ) {
68 if ( isset( $lbConf['servers'] ) ) {
69 // Server array is already explicitly configured; leave alone
70 } elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
71 foreach ( $mainConfig->get( 'DBservers' ) as $i => $server ) {
72 if ( $server['type'] === 'sqlite' ) {
73 $server += [ 'dbDirectory' => $mainConfig->get( 'SQLiteDataDir' ) ];
74 } elseif ( $server['type'] === 'postgres' ) {
75 $server += [
76 'port' => $mainConfig->get( 'DBport' ),
77 // Work around the reserved word usage in MediaWiki schema
78 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
79 ];
80 } elseif ( $server['type'] === 'mssql' ) {
81 $server += [
82 'port' => $mainConfig->get( 'DBport' ),
83 'useWindowsAuth' => $mainConfig->get( 'DBWindowsAuthentication' )
84 ];
85 }
86
87 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
88 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
89 }
90
91 $server += [
92 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
93 'flags' => DBO_DEFAULT,
94 'sqlMode' => $mainConfig->get( 'SQLMode' ),
95 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
96 ];
97
98 $lbConf['servers'][$i] = $server;
99 }
100 } else {
101 $flags = DBO_DEFAULT;
102 $flags |= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG : 0;
103 $flags |= $mainConfig->get( 'DBssl' ) ? DBO_SSL : 0;
104 $flags |= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
105 $server = [
106 'host' => $mainConfig->get( 'DBserver' ),
107 'user' => $mainConfig->get( 'DBuser' ),
108 'password' => $mainConfig->get( 'DBpassword' ),
109 'dbname' => $mainConfig->get( 'DBname' ),
110 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
111 'type' => $mainConfig->get( 'DBtype' ),
112 'load' => 1,
113 'flags' => $flags,
114 'sqlMode' => $mainConfig->get( 'SQLMode' ),
115 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
116 ];
117 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
118 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
119 }
120 if ( $server['type'] === 'sqlite' ) {
121 $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
122 } elseif ( $server['type'] === 'postgres' ) {
123 $server['port'] = $mainConfig->get( 'DBport' );
124 // Work around the reserved word usage in MediaWiki schema
125 $server['keywordTableMap'] = [ 'user' => 'mwuser', 'text' => 'pagecontent' ];
126 } elseif ( $server['type'] === 'mssql' ) {
127 $server['port'] = $mainConfig->get( 'DBport' );
128 $server['useWindowsAuth'] = $mainConfig->get( 'DBWindowsAuthentication' );
129 }
130 $lbConf['servers'] = [ $server ];
131 }
132 if ( !isset( $lbConf['externalClusters'] ) ) {
133 $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
134 }
135 } elseif ( $lbConf['class'] === Wikimedia\Rdbms\LBFactoryMulti::class ) {
136 if ( isset( $lbConf['serverTemplate'] ) ) {
137 if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
138 $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
139 }
140 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
141 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
142 }
143 }
144
145 $services = MediaWikiServices::getInstance();
146
147 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
148 $sCache = $services->getLocalServerObjectCache();
149 if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
150 $lbConf['srvCache'] = $sCache;
151 }
152 $mStash = $services->getMainObjectStash();
153 if ( $mStash->getQoS( $mStash::ATTR_EMULATION ) > $mStash::QOS_EMULATION_SQL ) {
154 $lbConf['memStash'] = $mStash;
155 }
156 $wCache = $services->getMainWANObjectCache();
157 if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
158 $lbConf['wanCache'] = $wCache;
159 }
160
161 return $lbConf;
162 }
163
164 /**
165 * Returns the LBFactory class to use and the load balancer configuration.
166 *
167 * @todo instead of this, use a ServiceContainer for managing the different implementations.
168 *
169 * @param array $config (e.g. $wgLBFactoryConf)
170 * @return string Class name
171 */
172 public static function getLBFactoryClass( array $config ) {
173 // For configuration backward compatibility after removing
174 // underscores from class names in MediaWiki 1.23.
175 $bcClasses = [
176 'LBFactory_Simple' => 'LBFactorySimple',
177 'LBFactory_Single' => 'LBFactorySingle',
178 'LBFactory_Multi' => 'LBFactoryMulti'
179 ];
180
181 $class = $config['class'];
182
183 if ( isset( $bcClasses[$class] ) ) {
184 $class = $bcClasses[$class];
185 wfDeprecated(
186 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
187 '1.23'
188 );
189 }
190
191 // For configuration backward compatibility after moving classes to namespaces (1.29)
192 $compat = [
193 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
194 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
195 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
196 ];
197
198 if ( isset( $compat[$class] ) ) {
199 $class = $compat[$class];
200 }
201
202 return $class;
203 }
204 }