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