Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
AggregateMessageGroupLoader.php
Go to the documentation of this file.
1<?php
11use MediaWiki\MediaWikiServices;
12use Wikimedia\Rdbms\IDatabase;
13
19 implements CachedMessageGroupLoader {
20
21 private const CACHE_KEY = 'aggregate';
22 private const CACHE_VERSION = 2;
23
25 protected $cache;
27 protected $db;
29 protected $groups;
30
31 public function __construct( IDatabase $db, MessageGroupWANCache $cache ) {
32 $this->db = $db;
33 $this->cache = $cache;
34 $this->cache->configure(
35 [
36 'key' => self::CACHE_KEY,
37 'version' => self::CACHE_VERSION,
38 'regenerator' => [ $this, 'getCacheData' ],
39 ]
40 );
41 }
42
49 public function getGroups() {
50 if ( $this->groups === null ) {
51 $this->groups = $this->initGroupsFromConf( $this->cache->getValue() );
52 }
53
54 return $this->groups;
55 }
56
63 public static function registerLoader( array &$groupLoader, array $deps ) {
64 $groupLoader[] = self::getInstance(
65 $deps['database'],
66 $deps['cache']
67 );
68 }
69
78 public static function getInstance( IDatabase $db = null, WANObjectCache $cache = null ) {
79 return new self(
80 $db ?? TranslateUtils::getSafeReadDB(),
82 $cache ?? MediaWikiServices::getInstance()->getMainWANObjectCache()
83 )
84 );
85 }
86
92 public function getCacheData(): array {
93 $tables = [ 'translate_metadata' ];
94 $field = 'tmd_group';
95 $conds = [ 'tmd_key' => 'subgroups' ];
96 $groupIds = $this->db->selectFieldValues( $tables, $field, $conds, __METHOD__ );
97 TranslateMetadata::preloadGroups( $groupIds, __METHOD__ );
98
99 $groups = [];
100 foreach ( $groupIds as $id ) {
101 $conf = [];
102 $conf['BASIC'] = [
103 'id' => $id,
104 'label' => TranslateMetadata::get( $id, 'name' ),
105 'description' => TranslateMetadata::get( $id, 'description' ),
106 'meta' => 1,
107 'class' => AggregateMessageGroup::class,
108 'namespace' => NS_TRANSLATIONS,
109 ];
110 $conf['GROUPS'] = TranslateMetadata::getSubgroups( $id );
111 $groups[$id] = $conf;
112 }
113
114 return $groups;
115 }
116
122 public function loadAggregateGroups(): array {
123 // get the data from the database everytime
124 // @phan-suppress-next-line PhanTypeMismatchReturn type is guaranteed via getCacheData above
125 return $this->initGroupsFromConf( $this->getCacheData() );
126 }
127
132 protected function initGroupsFromConf( array $groups ): array {
133 return array_map( [ MessageGroupBase::class, 'factory' ], $groups );
134 }
135
139 public function recache() {
140 $this->clearProcessCache();
141 $this->cache->touchKey();
142
143 $cacheData = $this->cache->getValue( 'recache' );
144 $this->groups = $this->initGroupsFromConf( $cacheData );
145 }
146
150 public function clearCache() {
151 $this->clearProcessCache();
152 $this->cache->delete();
153 }
154
158 protected function clearProcessCache() {
159 $this->groups = null;
160 }
161}
return[ 'Translate:ConfigHelper'=> static function():ConfigHelper { return new ConfigHelper();}, 'Translate:CsvTranslationImporter'=> static function(MediaWikiServices $services):CsvTranslationImporter { return new CsvTranslationImporter( $services->getWikiPageFactory());}, 'Translate:EntitySearch'=> static function(MediaWikiServices $services):EntitySearch { return new EntitySearch($services->getMainWANObjectCache(), $services->getCollationFactory() ->makeCollation( 'uca-default-u-kn'), MessageGroups::singleton(), $services->getNamespaceInfo(), $services->get( 'Translate:MessageIndex'), $services->getTitleParser(), $services->getTitleFormatter());}, 'Translate:ExternalMessageSourceStateImporter'=> static function(MediaWikiServices $services):ExternalMessageSourceStateImporter { return new ExternalMessageSourceStateImporter($services->getMainConfig(), $services->get( 'Translate:GroupSynchronizationCache'), $services->getJobQueueGroup(), LoggerFactory::getInstance( 'Translate.GroupSynchronization'), MessageIndex::singleton());}, 'Translate:GroupSynchronizationCache'=> static function(MediaWikiServices $services):GroupSynchronizationCache { return new GroupSynchronizationCache( $services->get( 'Translate:PersistentCache'));}, 'Translate:MessageBundleStore'=> static function(MediaWikiServices $services):MessageBundleStore { return new MessageBundleStore(new RevTagStore(), $services->getJobQueueGroup(), $services->getLanguageNameUtils(), $services->get( 'Translate:MessageIndex'));}, 'Translate:MessageGroupReview'=> static function(MediaWikiServices $services):MessageGroupReview { return new MessageGroupReview($services->getDBLoadBalancer(), $services->getHookContainer());}, 'Translate:MessageIndex'=> static function(MediaWikiServices $services):MessageIndex { $params=$services->getMainConfig() ->get( 'TranslateMessageIndex');if(is_string( $params)) { $params=(array) $params;} $class=array_shift( $params);return new $class( $params);}, 'Translate:ParsingPlaceholderFactory'=> static function():ParsingPlaceholderFactory { return new ParsingPlaceholderFactory();}, 'Translate:PersistentCache'=> static function(MediaWikiServices $services):PersistentCache { return new PersistentDatabaseCache($services->getDBLoadBalancer(), $services->getJsonCodec());}, 'Translate:ProgressStatsTableFactory'=> static function(MediaWikiServices $services):ProgressStatsTableFactory { return new ProgressStatsTableFactory($services->getLinkRenderer(), $services->get( 'Translate:ConfigHelper'));}, 'Translate:SubpageListBuilder'=> static function(MediaWikiServices $services):SubpageListBuilder { return new SubpageListBuilder($services->get( 'Translate:TranslatableBundleFactory'), $services->getLinkBatchFactory());}, 'Translate:TranslatableBundleFactory'=> static function(MediaWikiServices $services):TranslatableBundleFactory { return new TranslatableBundleFactory($services->get( 'Translate:TranslatablePageStore'), $services->get( 'Translate:MessageBundleStore'));}, 'Translate:TranslatableBundleMover'=> static function(MediaWikiServices $services):TranslatableBundleMover { return new TranslatableBundleMover($services->getMovePageFactory(), $services->getJobQueueGroup(), $services->getLinkBatchFactory(), $services->get( 'Translate:TranslatableBundleFactory'), $services->get( 'Translate:SubpageListBuilder'), $services->getMainConfig() ->get( 'TranslatePageMoveLimit'));}, 'Translate:TranslatablePageParser'=> static function(MediaWikiServices $services):TranslatablePageParser { return new TranslatablePageParser($services->get( 'Translate:ParsingPlaceholderFactory'));}, 'Translate:TranslatablePageStore'=> static function(MediaWikiServices $services):TranslatablePageStore { return new TranslatablePageStore($services->get( 'Translate:MessageIndex'), $services->getJobQueueGroup(), new RevTagStore(), $services->getDBLoadBalancer());}, 'Translate:TranslationStashReader'=> static function(MediaWikiServices $services):TranslationStashReader { $db=$services->getDBLoadBalancer() ->getConnectionRef(DB_REPLICA);return new TranslationStashStorage( $db);}, 'Translate:TranslationStatsDataProvider'=> static function(MediaWikiServices $services):TranslationStatsDataProvider { return new TranslationStatsDataProvider(new ServiceOptions(TranslationStatsDataProvider::CONSTRUCTOR_OPTIONS, $services->getMainConfig()), $services->getObjectFactory());}, 'Translate:TranslationUnitStoreFactory'=> static function(MediaWikiServices $services):TranslationUnitStoreFactory { return new TranslationUnitStoreFactory( $services->getDBLoadBalancer());}, 'Translate:TranslatorActivity'=> static function(MediaWikiServices $services):TranslatorActivity { $query=new TranslatorActivityQuery($services->getMainConfig(), $services->getDBLoadBalancer());return new TranslatorActivity($services->getMainObjectStash(), $query, $services->getJobQueueGroup());}, 'Translate:TtmServerFactory'=> static function(MediaWikiServices $services):TtmServerFactory { $config=$services->getMainConfig();$default=$config->get( 'TranslateTranslationDefaultService');if( $default===false) { $default=null;} return new TtmServerFactory( $config->get( 'TranslateTranslationServices'), $default);}]
@phpcs-require-sorted-array
Loads AggregateMessageGroup, and handles the related cache.
clearProcessCache()
Clears the process cache, mainly the cached groups property.
static registerLoader(array &$groupLoader, array $deps)
Hook: TranslateInitGroupLoaders.
static getInstance(IDatabase $db=null, WANObjectCache $cache=null)
Return an instance of this class using the parameters, if passed, else initialize the necessary depen...
getCacheData()
Get all the aggregate messages groups defined in translate_metadata table and return their configurat...
recache()
Clear and refill the cache with the latest values.
getGroups()
Fetches the AggregateMessageGroups.
clearCache()
Clear values from the cache.
loadAggregateGroups()
Loads AggregateMessageGroup from the database.
This class implements some basic functions that wrap around the YAML message group configurations.
An abstract class to be implemented by group loaders / stores.
Wrapper around WANObjectCache providing a simpler interface for MessageGroups to use the cache.
configure(array $config)
Configure the message group.
To be implemented by MessageGroupLoaders that use the MessageGroupWANCache.