Enable configuration to supply options for Special:Search form
[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\Config\ServiceOptions;
25 use MediaWiki\Logger\LoggerFactory;
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 * TODO Make this a const when HHVM support is dropped (T192166)
40 *
41 * @var array
42 * @since 1.34
43 */
44 public static $applyDefaultConfigOptions = [
45 'DBcompress',
46 'DBDefaultGroup',
47 'DBmwschema',
48 'DBname',
49 'DBpassword',
50 'DBport',
51 'DBprefix',
52 'DBserver',
53 'DBservers',
54 'DBssl',
55 'DBtype',
56 'DBuser',
57 'DBWindowsAuthentication',
58 'DebugDumpSql',
59 'ExternalServers',
60 'SQLiteDataDir',
61 'SQLMode',
62 ];
63
64 /**
65 * @param array $lbConf Config for LBFactory::__construct()
66 * @param ServiceOptions $options
67 * @param ConfiguredReadOnlyMode $readOnlyMode
68 * @param BagOStuff $srvCace
69 * @param BagOStuff $mainStash
70 * @param WANObjectCache $wanCache
71 * @return array
72 */
73 public static function applyDefaultConfig(
74 array $lbConf,
75 ServiceOptions $options,
76 ConfiguredReadOnlyMode $readOnlyMode,
77 BagOStuff $srvCace,
78 BagOStuff $mainStash,
79 WANObjectCache $wanCache
80 ) {
81 $options->assertRequiredOptions( self::$applyDefaultConfigOptions );
82
83 global $wgCommandLineMode;
84
85 $typesWithSchema = self::getDbTypesWithSchemas();
86
87 $lbConf += [
88 'localDomain' => new DatabaseDomain(
89 $options->get( 'DBname' ),
90 $options->get( 'DBmwschema' ),
91 $options->get( 'DBprefix' )
92 ),
93 'profiler' => function ( $section ) {
94 return Profiler::instance()->scopedProfileIn( $section );
95 },
96 'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
97 'replLogger' => LoggerFactory::getInstance( 'DBReplication' ),
98 'queryLogger' => LoggerFactory::getInstance( 'DBQuery' ),
99 'connLogger' => LoggerFactory::getInstance( 'DBConnection' ),
100 'perfLogger' => LoggerFactory::getInstance( 'DBPerformance' ),
101 'errorLogger' => [ MWExceptionHandler::class, 'logException' ],
102 'deprecationLogger' => [ static::class, 'logDeprecation' ],
103 'cliMode' => $wgCommandLineMode,
104 'hostname' => wfHostname(),
105 'readOnlyReason' => $readOnlyMode->getReason(),
106 'defaultGroup' => $options->get( 'DBDefaultGroup' ),
107 ];
108
109 $serversCheck = [];
110 // When making changes here, remember to also specify MediaWiki-specific options
111 // for Database classes in the relevant Installer subclass.
112 // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
113 if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple::class ) {
114 if ( isset( $lbConf['servers'] ) ) {
115 // Server array is already explicitly configured
116 } elseif ( is_array( $options->get( 'DBservers' ) ) ) {
117 $lbConf['servers'] = [];
118 foreach ( $options->get( 'DBservers' ) as $i => $server ) {
119 $lbConf['servers'][$i] = self::initServerInfo( $server, $options );
120 }
121 } else {
122 $server = self::initServerInfo(
123 [
124 'host' => $options->get( 'DBserver' ),
125 'user' => $options->get( 'DBuser' ),
126 'password' => $options->get( 'DBpassword' ),
127 'dbname' => $options->get( 'DBname' ),
128 'type' => $options->get( 'DBtype' ),
129 'load' => 1
130 ],
131 $options
132 );
133
134 $server['flags'] |= $options->get( 'DBssl' ) ? DBO_SSL : 0;
135 $server['flags'] |= $options->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
136
137 $lbConf['servers'] = [ $server ];
138 }
139 if ( !isset( $lbConf['externalClusters'] ) ) {
140 $lbConf['externalClusters'] = $options->get( 'ExternalServers' );
141 }
142
143 $serversCheck = $lbConf['servers'];
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'] = $options->get( 'DBmwschema' );
148 }
149 $lbConf['serverTemplate']['sqlMode'] = $options->get( 'SQLMode' );
150 }
151 $serversCheck = [ $lbConf['serverTemplate'] ] ?? [];
152 }
153
154 self::assertValidServerConfigs( $serversCheck, $options->get( 'DBname' ),
155 $options->get( 'DBprefix' ) );
156
157 $lbConf = self::injectObjectCaches( $lbConf, $srvCace, $mainStash, $wanCache );
158
159 return $lbConf;
160 }
161
162 /**
163 * @return array
164 */
165 private static function getDbTypesWithSchemas() {
166 return [ 'postgres', 'msssql' ];
167 }
168
169 /**
170 * @param array $server
171 * @param ServiceOptions $options
172 * @return array
173 */
174 private static function initServerInfo( array $server, ServiceOptions $options ) {
175 if ( $server['type'] === 'sqlite' ) {
176 $httpMethod = $_SERVER['REQUEST_METHOD'] ?? null;
177 // T93097: hint for how file-based databases (e.g. sqlite) should go about locking.
178 // See https://www.sqlite.org/lang_transaction.html
179 // See https://www.sqlite.org/lockingv3.html#shared_lock
180 $isHttpRead = in_array( $httpMethod, [ 'GET', 'HEAD', 'OPTIONS', 'TRACE' ] );
181 $server += [
182 'dbDirectory' => $options->get( 'SQLiteDataDir' ),
183 'trxMode' => $isHttpRead ? 'DEFERRED' : 'IMMEDIATE'
184 ];
185 } elseif ( $server['type'] === 'postgres' ) {
186 $server += [
187 'port' => $options->get( 'DBport' ),
188 // Work around the reserved word usage in MediaWiki schema
189 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
190 ];
191 } elseif ( $server['type'] === 'oracle' ) {
192 $server += [
193 // Work around the reserved word usage in MediaWiki schema
194 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
195 ];
196 } elseif ( $server['type'] === 'mssql' ) {
197 $server += [
198 'port' => $options->get( 'DBport' ),
199 'useWindowsAuth' => $options->get( 'DBWindowsAuthentication' )
200 ];
201 }
202
203 if ( in_array( $server['type'], self::getDbTypesWithSchemas(), true ) ) {
204 $server += [ 'schema' => $options->get( 'DBmwschema' ) ];
205 }
206
207 $flags = DBO_DEFAULT;
208 $flags |= $options->get( 'DebugDumpSql' ) ? DBO_DEBUG : 0;
209 if ( $server['type'] === 'oracle' ) {
210 $flags |= $options->get( 'DBOracleDRCP' ) ? DBO_PERSISTENT : 0;
211 }
212
213 $server += [
214 'tablePrefix' => $options->get( 'DBprefix' ),
215 'flags' => $flags,
216 'sqlMode' => $options->get( 'SQLMode' ),
217 ];
218
219 return $server;
220 }
221
222 /**
223 * @param array $lbConf
224 * @param BagOStuff $sCache
225 * @param BagOStuff $mStash
226 * @param WANObjectCache $wCache
227 * @return array
228 */
229 private static function injectObjectCaches(
230 array $lbConf, BagOStuff $sCache, BagOStuff $mStash, WANObjectCache $wCache
231 ) {
232 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
233 if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
234 $lbConf['srvCache'] = $sCache;
235 }
236 if ( $mStash->getQoS( $mStash::ATTR_EMULATION ) > $mStash::QOS_EMULATION_SQL ) {
237 $lbConf['memStash'] = $mStash;
238 }
239 if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
240 $lbConf['wanCache'] = $wCache;
241 }
242
243 return $lbConf;
244 }
245
246 /**
247 * @param array $servers
248 * @param string $lbDB Local domain database name
249 * @param string $lbTP Local domain prefix
250 */
251 private static function assertValidServerConfigs( array $servers, $ldDB, $ldTP ) {
252 foreach ( $servers as $server ) {
253 $type = $server['type'] ?? null;
254 $srvDB = $server['dbname'] ?? null; // server DB
255 $srvTP = $server['tablePrefix'] ?? ''; // server table prefix
256
257 if ( $type === 'mysql' ) {
258 // A DB name is not needed to connect to mysql; 'dbname' is useless.
259 // This field only defines the DB to use for unspecified DB domains.
260 if ( $srvDB !== null && $srvDB !== $ldDB ) {
261 self::reportMismatchedDBs( $srvDB, $ldDB );
262 }
263 } elseif ( $type === 'postgres' ) {
264 if ( $srvTP !== '' ) {
265 self::reportIfPrefixSet( $srvTP, $type );
266 }
267 }
268
269 if ( $srvTP !== '' && $srvTP !== $ldTP ) {
270 self::reportMismatchedPrefixes( $srvTP, $ldTP );
271 }
272 }
273 }
274
275 /**
276 * @param string $prefix Table prefix
277 * @param string $dbType Database type
278 */
279 private static function reportIfPrefixSet( $prefix, $dbType ) {
280 $e = new UnexpectedValueException(
281 "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
282 "MediaWiki does not support using a table prefix with this RDBMS type."
283 );
284 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_PRETTY );
285 exit;
286 }
287
288 /**
289 * @param string $srvDB Server config database
290 * @param string $ldDB Local DB domain database
291 */
292 private static function reportMismatchedDBs( $srvDB, $ldDB ) {
293 $e = new UnexpectedValueException(
294 "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
295 "Set \$wgDBname to the database used by this wiki project. " .
296 "There is rarely a need to set 'dbname' in \$wgDBservers. " .
297 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
298 "use of Database::getDomainId(), and other features are not reliable when " .
299 "\$wgDBservers does not match the local wiki database/prefix."
300 );
301 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_PRETTY );
302 exit;
303 }
304
305 /**
306 * @param string $srvTP Server config table prefix
307 * @param string $ldTP Local DB domain database
308 */
309 private static function reportMismatchedPrefixes( $srvTP, $ldTP ) {
310 $e = new UnexpectedValueException(
311 "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
312 "Set \$wgDBprefix to the table prefix used by this wiki project. " .
313 "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
314 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
315 "use of Database::getDomainId(), and other features are not reliable when " .
316 "\$wgDBservers does not match the local wiki database/prefix."
317 );
318 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_PRETTY );
319 exit;
320 }
321
322 /**
323 * Returns the LBFactory class to use and the load balancer configuration.
324 *
325 * @todo instead of this, use a ServiceContainer for managing the different implementations.
326 *
327 * @param array $config (e.g. $wgLBFactoryConf)
328 * @return string Class name
329 */
330 public static function getLBFactoryClass( array $config ) {
331 // For configuration backward compatibility after removing
332 // underscores from class names in MediaWiki 1.23.
333 $bcClasses = [
334 'LBFactory_Simple' => 'LBFactorySimple',
335 'LBFactory_Single' => 'LBFactorySingle',
336 'LBFactory_Multi' => 'LBFactoryMulti'
337 ];
338
339 $class = $config['class'];
340
341 if ( isset( $bcClasses[$class] ) ) {
342 $class = $bcClasses[$class];
343 wfDeprecated(
344 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
345 '1.23'
346 );
347 }
348
349 // For configuration backward compatibility after moving classes to namespaces (1.29)
350 $compat = [
351 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
352 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
353 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
354 ];
355
356 if ( isset( $compat[$class] ) ) {
357 $class = $compat[$class];
358 }
359
360 return $class;
361 }
362
363 /**
364 * @param LBFactory $lbFactory
365 * @param string $dbType 'mysql', 'sqlite', etc.
366 */
367 public static function setSchemaAliases( LBFactory $lbFactory, $dbType ) {
368 if ( $dbType instanceof Config ) {
369 // Before 1.34 this took a whole Config just to get $dbType
370 wfDeprecated( __METHOD__ . ' with Config argument', '1.34' );
371 $dbType = $dbType->get( 'DBtype' );
372 }
373 if ( $dbType === 'mysql' ) {
374 /**
375 * When SQLite indexes were introduced in r45764, it was noted that
376 * SQLite requires index names to be unique within the whole database,
377 * not just within a schema. As discussed in CR r45819, to avoid the
378 * need for a schema change on existing installations, the indexes
379 * were implicitly mapped from the new names to the old names.
380 *
381 * This mapping can be removed if DB patches are introduced to alter
382 * the relevant tables in existing installations. Note that because
383 * this index mapping applies to table creation, even new installations
384 * of MySQL have the old names (except for installations created during
385 * a period where this mapping was inappropriately removed, see
386 * T154872).
387 */
388 $lbFactory->setIndexAliases( [
389 'ar_usertext_timestamp' => 'usertext_timestamp',
390 'un_user_id' => 'user_id',
391 'un_user_ip' => 'user_ip',
392 ] );
393 }
394 }
395
396 /**
397 * Log a database deprecation warning
398 * @param string $msg Deprecation message
399 */
400 public static function logDeprecation( $msg ) {
401 global $wgDevelopmentWarnings;
402
403 if ( isset( self::$loggedDeprecations[$msg] ) ) {
404 return;
405 }
406 self::$loggedDeprecations[$msg] = true;
407
408 if ( $wgDevelopmentWarnings ) {
409 trigger_error( $msg, E_USER_DEPRECATED );
410 }
411 wfDebugLog( 'deprecated', $msg, 'private' );
412 }
413 }