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