Merge "don't die producing xml files if rev text export conversion fails"
[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\LBFactory;
27 use Wikimedia\Rdbms\DatabaseDomain;
28
29 /**
30 * MediaWiki-specific class for generating database load balancers
31 * @ingroup Database
32 */
33 abstract class MWLBFactory {
34
35 /** @var array Cache of already-logged deprecation messages */
36 private static $loggedDeprecations = [];
37
38 /**
39 * @param array $lbConf Config for LBFactory::__construct()
40 * @param Config $mainConfig Main config object from MediaWikiServices
41 * @param ConfiguredReadOnlyMode $readOnlyMode
42 * @return array
43 */
44 public static function applyDefaultConfig( array $lbConf, Config $mainConfig,
45 ConfiguredReadOnlyMode $readOnlyMode
46 ) {
47 global $wgCommandLineMode;
48
49 static $typesWithSchema = [ 'postgres', 'msssql' ];
50
51 $lbConf += [
52 'localDomain' => new DatabaseDomain(
53 $mainConfig->get( 'DBname' ),
54 $mainConfig->get( 'DBmwschema' ),
55 $mainConfig->get( 'DBprefix' )
56 ),
57 'profiler' => function ( $section ) {
58 return Profiler::instance()->scopedProfileIn( $section );
59 },
60 'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
61 'replLogger' => LoggerFactory::getInstance( 'DBReplication' ),
62 'queryLogger' => LoggerFactory::getInstance( 'DBQuery' ),
63 'connLogger' => LoggerFactory::getInstance( 'DBConnection' ),
64 'perfLogger' => LoggerFactory::getInstance( 'DBPerformance' ),
65 'errorLogger' => [ MWExceptionHandler::class, 'logException' ],
66 'deprecationLogger' => [ static::class, 'logDeprecation' ],
67 'cliMode' => $wgCommandLineMode,
68 'hostname' => wfHostname(),
69 'readOnlyReason' => $readOnlyMode->getReason(),
70 'defaultGroup' => $mainConfig->get( 'DBDefaultGroup' ),
71 ];
72
73 // When making changes here, remember to also specify MediaWiki-specific options
74 // for Database classes in the relevant Installer subclass.
75 // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
76 if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple::class ) {
77 if ( isset( $lbConf['servers'] ) ) {
78 // Server array is already explicitly configured; leave alone
79 } elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
80 foreach ( $mainConfig->get( 'DBservers' ) as $i => $server ) {
81 if ( $server['type'] === 'sqlite' ) {
82 $server += [ 'dbDirectory' => $mainConfig->get( 'SQLiteDataDir' ) ];
83 } elseif ( $server['type'] === 'postgres' ) {
84 $server += [
85 'port' => $mainConfig->get( 'DBport' ),
86 // Work around the reserved word usage in MediaWiki schema
87 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
88 ];
89 } elseif ( $server['type'] === 'mssql' ) {
90 $server += [
91 'port' => $mainConfig->get( 'DBport' ),
92 'useWindowsAuth' => $mainConfig->get( 'DBWindowsAuthentication' )
93 ];
94 }
95
96 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
97 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
98 }
99
100 $server += [
101 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
102 'flags' => DBO_DEFAULT,
103 'sqlMode' => $mainConfig->get( 'SQLMode' ),
104 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
105 ];
106
107 $lbConf['servers'][$i] = $server;
108 }
109 } else {
110 $flags = DBO_DEFAULT;
111 $flags |= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG : 0;
112 $flags |= $mainConfig->get( 'DBssl' ) ? DBO_SSL : 0;
113 $flags |= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
114 $server = [
115 'host' => $mainConfig->get( 'DBserver' ),
116 'user' => $mainConfig->get( 'DBuser' ),
117 'password' => $mainConfig->get( 'DBpassword' ),
118 'dbname' => $mainConfig->get( 'DBname' ),
119 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
120 'type' => $mainConfig->get( 'DBtype' ),
121 'load' => 1,
122 'flags' => $flags,
123 'sqlMode' => $mainConfig->get( 'SQLMode' ),
124 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
125 ];
126 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
127 $server += [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
128 }
129 if ( $server['type'] === 'sqlite' ) {
130 $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
131 } elseif ( $server['type'] === 'postgres' ) {
132 $server['port'] = $mainConfig->get( 'DBport' );
133 // Work around the reserved word usage in MediaWiki schema
134 $server['keywordTableMap'] = [ 'user' => 'mwuser', 'text' => 'pagecontent' ];
135 } elseif ( $server['type'] === 'mssql' ) {
136 $server['port'] = $mainConfig->get( 'DBport' );
137 $server['useWindowsAuth'] = $mainConfig->get( 'DBWindowsAuthentication' );
138 }
139 $lbConf['servers'] = [ $server ];
140 }
141 if ( !isset( $lbConf['externalClusters'] ) ) {
142 $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
143 }
144 } elseif ( $lbConf['class'] === Wikimedia\Rdbms\LBFactoryMulti::class ) {
145 if ( isset( $lbConf['serverTemplate'] ) ) {
146 if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
147 $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
148 }
149 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
150 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
151 }
152 }
153
154 $services = MediaWikiServices::getInstance();
155
156 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
157 $sCache = $services->getLocalServerObjectCache();
158 if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
159 $lbConf['srvCache'] = $sCache;
160 }
161 $mStash = $services->getMainObjectStash();
162 if ( $mStash->getQoS( $mStash::ATTR_EMULATION ) > $mStash::QOS_EMULATION_SQL ) {
163 $lbConf['memStash'] = $mStash;
164 }
165 $wCache = $services->getMainWANObjectCache();
166 if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
167 $lbConf['wanCache'] = $wCache;
168 }
169
170 return $lbConf;
171 }
172
173 /**
174 * Returns the LBFactory class to use and the load balancer configuration.
175 *
176 * @todo instead of this, use a ServiceContainer for managing the different implementations.
177 *
178 * @param array $config (e.g. $wgLBFactoryConf)
179 * @return string Class name
180 */
181 public static function getLBFactoryClass( array $config ) {
182 // For configuration backward compatibility after removing
183 // underscores from class names in MediaWiki 1.23.
184 $bcClasses = [
185 'LBFactory_Simple' => 'LBFactorySimple',
186 'LBFactory_Single' => 'LBFactorySingle',
187 'LBFactory_Multi' => 'LBFactoryMulti'
188 ];
189
190 $class = $config['class'];
191
192 if ( isset( $bcClasses[$class] ) ) {
193 $class = $bcClasses[$class];
194 wfDeprecated(
195 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
196 '1.23'
197 );
198 }
199
200 // For configuration backward compatibility after moving classes to namespaces (1.29)
201 $compat = [
202 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
203 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
204 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
205 ];
206
207 if ( isset( $compat[$class] ) ) {
208 $class = $compat[$class];
209 }
210
211 return $class;
212 }
213
214 public static function setSchemaAliases( LBFactory $lbFactory, Config $config ) {
215 if ( $config->get( 'DBtype' ) === 'mysql' ) {
216 /**
217 * When SQLite indexes were introduced in r45764, it was noted that
218 * SQLite requires index names to be unique within the whole database,
219 * not just within a schema. As discussed in CR r45819, to avoid the
220 * need for a schema change on existing installations, the indexes
221 * were implicitly mapped from the new names to the old names.
222 *
223 * This mapping can be removed if DB patches are introduced to alter
224 * the relevant tables in existing installations. Note that because
225 * this index mapping applies to table creation, even new installations
226 * of MySQL have the old names (except for installations created during
227 * a period where this mapping was inappropriately removed, see
228 * T154872).
229 */
230 $lbFactory->setIndexAliases( [
231 'ar_usertext_timestamp' => 'usertext_timestamp',
232 'un_user_id' => 'user_id',
233 'un_user_ip' => 'user_ip',
234 ] );
235 }
236 }
237
238 /**
239 * Log a database deprecation warning
240 * @param string $msg Deprecation message
241 */
242 public static function logDeprecation( $msg ) {
243 global $wgDevelopmentWarnings;
244
245 if ( isset( self::$loggedDeprecations[$msg] ) ) {
246 return;
247 }
248 self::$loggedDeprecations[$msg] = true;
249
250 if ( $wgDevelopmentWarnings ) {
251 trigger_error( $msg, E_USER_DEPRECATED );
252 }
253 wfDebugLog( 'deprecated', $msg, 'private' );
254 }
255 }