'SiteStore' => function( MediaWikiServices $services ) {
$rawSiteStore = new DBSiteStore( $services->getDBLoadBalancer() );
+ $rawSiteStore->setLanguageCodeMapping(
+ $services->getMainConfig()->get( 'DummyLanguageCodes' ) ?: []
+ );
// TODO: replace wfGetCache with a CacheFactory service.
// TODO: replace wfIsHHVM with a capabilities service.
*/
private $dbLoadBalancer;
+ /**
+ * @var string[]
+ */
+ private $languageCodeMapping = [];
+
/**
* @since 1.27
*
);
foreach ( $res as $row ) {
+ $languageCode = $row->site_language === '' ? null : $row->site_language;
+ if ( isset( $this->languageCodeMapping[ $languageCode ] ) ) {
+ $languageCode = $this->languageCodeMapping[ $languageCode ];
+ }
+
$site = Site::newForType( $row->site_type );
$site->setGlobalId( $row->site_global_key );
$site->setInternalId( (int)$row->site_id );
$site->setForward( (bool)$row->site_forward );
$site->setGroup( $row->site_group );
- $site->setLanguageCode( $row->site_language === ''
- ? null
- : $row->site_language
- );
+ $site->setLanguageCode( $languageCode );
$site->setSource( $row->site_source );
$site->setExtraData( unserialize( $row->site_data ) );
$site->setExtraConfig( unserialize( $row->site_config ) );
return $ok;
}
+ /**
+ * Provide an array that maps language codes
+ *
+ * @param string[] $newMapping
+ */
+ public function setLanguageCodeMapping( array $newMapping ) {
+ $this->languageCodeMapping = $newMapping;
+ }
+
}
*/
private $cacheFile;
+ /**
+ * @var string[]
+ */
+ private $languageCodeMapping = [];
+
/**
* @param string $cacheFile
*/
* @return Site
*/
private function newSiteFromArray( array $data ) {
+ $languageCode = $data['language'];
+ if ( isset( $this->languageCodeMapping[ $languageCode ] ) ) {
+ $languageCode = $this->languageCodeMapping[ $languageCode ];
+ }
+
$siteType = array_key_exists( 'type', $data ) ? $data['type'] : Site::TYPE_UNKNOWN;
$site = Site::newForType( $siteType );
$site->setGlobalId( $data['globalid'] );
$site->setForward( $data['forward'] );
$site->setGroup( $data['group'] );
- $site->setLanguageCode( $data['language'] );
+ $site->setLanguageCode( $languageCode );
$site->setSource( $data['source'] );
$site->setExtraData( $data['data'] );
$site->setExtraConfig( $data['config'] );
return $site;
}
+ /**
+ * Provide an array that maps language codes
+ *
+ * @param string[] $newMapping
+ */
+ public function setLanguageCodeMapping( array $newMapping ) {
+ $this->languageCodeMapping = $newMapping;
+ }
+
}
}
}
+ /**
+ * @covers DBSiteStore::getSites
+ * @covers DBSiteStore::setLanguageCodeMapping
+ */
+ public function testLanguageCodeMapping() {
+ TestSites::insertIntoDb();
+
+ $store = $this->newDBSiteStore();
+ $store->setLanguageCodeMapping( [ 'no' => 'nb' ] );
+
+ $site = $store->getSite( 'nowiki' );
+ $this->assertEquals( $site->getLanguageCode(), 'nb' );
+ }
+
/**
* @covers DBSiteStore::saveSites
*/
return tempnam( sys_get_temp_dir(), 'mw-test-sitelist' );
}
+ public function testLanguageCodeMapping() {
+ $sites = $this->getSites();
+ $cacheBuilder = $this->newSitesCacheFileBuilder( $sites );
+ $cacheBuilder->build();
+
+ $cache = new FileBasedSiteLookup( $this->cacheFile );
+ $cache->setLanguageCodeMapping( [ 'en' => 'fa' ] );
+
+ $this->assertEquals( $cache->getSite( 'enwiktionary' )->getLanguageCode(), 'fa' );
+ }
+
}