rdbms: inject the mysql index name aliases into Database
[lhc/web/wiklou.git] / includes / db / MWLBFactory.php
index 5196ac2..f0a17f7 100644 (file)
@@ -23,6 +23,7 @@
 
 use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\MediaWikiServices;
+use Wikimedia\Rdbms\LBFactory;
 use Wikimedia\Rdbms\DatabaseDomain;
 
 /**
@@ -46,7 +47,7 @@ abstract class MWLBFactory {
                $lbConf += [
                        'localDomain' => new DatabaseDomain(
                                $mainConfig->get( 'DBname' ),
-                               null,
+                               $mainConfig->get( 'DBmwschema' ),
                                $mainConfig->get( 'DBprefix' )
                        ),
                        'profiler' => Profiler::instance(),
@@ -64,7 +65,7 @@ abstract class MWLBFactory {
                // When making changes here, remember to also specify MediaWiki-specific options
                // for Database classes in the relevant Installer subclass.
                // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
-               if ( $lbConf['class'] === 'LBFactorySimple' ) {
+               if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple::class ) {
                        if ( isset( $lbConf['servers'] ) ) {
                                // Server array is already explicitly configured; leave alone
                        } elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
@@ -132,7 +133,7 @@ abstract class MWLBFactory {
                        if ( !isset( $lbConf['externalClusters'] ) ) {
                                $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
                        }
-               } elseif ( $lbConf['class'] === 'LBFactoryMulti' ) {
+               } elseif ( $lbConf['class'] === Wikimedia\Rdbms\LBFactoryMulti::class ) {
                        if ( isset( $lbConf['serverTemplate'] ) ) {
                                if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
                                        $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
@@ -142,16 +143,18 @@ abstract class MWLBFactory {
                        }
                }
 
+               $services = MediaWikiServices::getInstance();
+
                // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
-               $sCache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
+               $sCache = $services->getLocalServerObjectCache();
                if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
                        $lbConf['srvCache'] = $sCache;
                }
-               $cCache = ObjectCache::getLocalClusterInstance();
-               if ( $cCache->getQoS( $cCache::ATTR_EMULATION ) > $cCache::QOS_EMULATION_SQL ) {
-                       $lbConf['memStash'] = $cCache;
+               $mStash = $services->getMainObjectStash();
+               if ( $mStash->getQoS( $mStash::ATTR_EMULATION ) > $mStash::QOS_EMULATION_SQL ) {
+                       $lbConf['memStash'] = $mStash;
                }
-               $wCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
+               $wCache = $services->getMainWANObjectCache();
                if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
                        $lbConf['wanCache'] = $wCache;
                }
@@ -199,4 +202,30 @@ abstract class MWLBFactory {
 
                return $class;
        }
+
+       public static function setSchemaAliases( LBFactory $lbFactory ) {
+               $mainLB = $lbFactory->getMainLB();
+               $masterType = $mainLB->getServerType( $mainLB->getWriterIndex() );
+               if ( $masterType === 'mysql' ) {
+                       /**
+                        * When SQLite indexes were introduced in r45764, it was noted that
+                        * SQLite requires index names to be unique within the whole database,
+                        * not just within a schema. As discussed in CR r45819, to avoid the
+                        * need for a schema change on existing installations, the indexes
+                        * were implicitly mapped from the new names to the old names.
+                        *
+                        * This mapping can be removed if DB patches are introduced to alter
+                        * the relevant tables in existing installations. Note that because
+                        * this index mapping applies to table creation, even new installations
+                        * of MySQL have the old names (except for installations created during
+                        * a period where this mapping was inappropriately removed, see
+                        * T154872).
+                        */
+                       $lbFactory->setIndexAliases( [
+                               'ar_usertext_timestamp' => 'usertext_timestamp',
+                               'un_user_id' => 'user_id',
+                               'un_user_ip' => 'user_ip',
+                       ] );
+               }
+       }
 }