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