11use MediaWiki\MediaWikiServices;
20 private const CACHE_KEY =
'filebased';
21 private const CACHE_VERSION = 2;
32 $this->cache = $cache;
35 'key' => self::CACHE_KEY,
36 'version' => self::CACHE_VERSION,
37 'regenerator' => [ $this,
'getCacheData' ],
38 'touchedCallback' => [ $this,
'isExpired' ]
50 if ( $this->groups ===
null ) {
52 $wrapper = $this->cache->getValue();
53 $cacheData = $wrapper->getValue();
79 public static function getInstance( WANObjectCache $cache =
null ) {
82 $cache ?? MediaWikiServices::getInstance()->getMainWANObjectCache()
93 global $wgTranslateGroupFiles;
95 $autoload = $groups = $deps = $value = [];
96 $deps[] =
new GlobalDependency(
'wgTranslateGroupFiles' );
100 foreach ( $wgTranslateGroupFiles as $configFile ) {
101 $deps[] =
new FileDependency( realpath( $configFile ) );
103 $yaml = file_get_contents( $configFile );
104 $fgroups = $parser->getHopefullyValidConfigurations(
106 static function ( $index, $config, $errmsg ) use ( $configFile ) {
107 trigger_error(
"Document $index in $configFile is invalid: $errmsg", E_USER_WARNING );
111 foreach ( $fgroups as $id => $conf ) {
112 if ( !empty( $conf[
'AUTOLOAD'] ) && is_array( $conf[
'AUTOLOAD'] ) ) {
113 $dir = dirname( $configFile );
114 $additions = array_map(
static function ( $file ) use ( $dir ) {
116 }, $conf[
'AUTOLOAD'] );
117 self::appendAutoloader( $additions, $autoload );
120 $groups[$id] = $conf;
123 $value[
'groups'] = $groups;
124 $value[
'autoload'] = $autoload;
126 $wrapper =
new DependencyWrapper( $value, $deps );
127 $wrapper->initialiseDeps();
136 $this->cache->touchKey();
139 $wrapper = $this->cache->getValue(
'recache' );
140 $cacheData = $wrapper->getValue();
149 $this->cache->delete();
156 $this->groups =
null;
165 global $wgAutoloadClasses;
168 $autoload = $cacheData[
'autoload'];
169 $groupConfs = $cacheData[
'groups'];
171 foreach ( $groupConfs as $id => $conf ) {
172 $this->groups[$id] = MessageGroupBase::factory( $conf );
175 self::appendAutoloader( $autoload, $wgAutoloadClasses );
184 private static function appendAutoloader( array $additions, array &$to ) {
185 foreach ( $additions as $class => $file ) {
186 if ( isset( $to[$class] ) && $to[$class] !== $file ) {
187 $msg =
"Autoload conflict for $class: {$to[$class]} !== $file";
188 trigger_error( $msg, E_USER_WARNING );
Loads FileBasedMessageGroup, and handles the related cache.
initFromCacheValue(array $cacheData)
Initialize groups and autoload classes from the cache value.
static getInstance(WANObjectCache $cache=null)
Return an instance of this class using the parameters, if passed, else initialize the necessary depen...
static registerLoader(array &$groupLoader, array $deps)
Hook: TranslateInitGroupLoaders.
getGroups()
Fetches FileBasedMessageGroups.
recache()
Clear and refill the cache with the latest values.
clearProcessCache()
Clears the process cache, mainly the cached groups property.
getCacheData()
Generates data to be stored in the cache.
clearCache()
Clear values from the cache.
Utility class to parse and validate 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.