12use MediaWiki\MediaWikiServices;
21 private const CACHE_KEY =
'filebased';
22 private const CACHE_VERSION = 2;
33 $this->cache = $cache;
36 'key' => self::CACHE_KEY,
37 'version' => self::CACHE_VERSION,
38 'regenerator' => [ $this,
'getCacheData' ],
39 'touchedCallback' => [ $this,
'isExpired' ]
51 if ( $this->groups ===
null ) {
53 $wrapper = $this->cache->getValue();
54 $cacheData = $wrapper->getValue();
80 public static function getInstance( WANObjectCache $cache =
null ) {
83 $cache ?? MediaWikiServices::getInstance()->getMainWANObjectCache()
94 global $wgTranslateGroupFiles;
96 $autoload = $groups = $deps = $value = [];
97 $deps[] =
new GlobalDependency(
'wgTranslateGroupFiles' );
101 foreach ( $wgTranslateGroupFiles as $configFile ) {
102 $deps[] =
new FileDependency( realpath( $configFile ) );
104 $yaml = file_get_contents( $configFile );
105 $fgroups = $parser->getHopefullyValidConfigurations(
107 static function ( $index, $config, $errmsg ) use ( $configFile ) {
108 trigger_error(
"Document $index in $configFile is invalid: $errmsg", E_USER_WARNING );
112 foreach ( $fgroups as $id => $conf ) {
113 if ( !empty( $conf[
'AUTOLOAD'] ) && is_array( $conf[
'AUTOLOAD'] ) ) {
114 $dir = dirname( $configFile );
115 $additions = array_map(
static function ( $file ) use ( $dir ) {
117 }, $conf[
'AUTOLOAD'] );
118 self::appendAutoloader( $additions, $autoload );
121 $groups[$id] = $conf;
124 $value[
'groups'] = $groups;
125 $value[
'autoload'] = $autoload;
127 $wrapper =
new DependencyWrapper( $value, $deps );
128 $wrapper->initialiseDeps();
137 $this->cache->touchKey();
140 $wrapper = $this->cache->getValue(
'recache' );
141 $cacheData = $wrapper->getValue();
150 $this->cache->delete();
157 $this->groups =
null;
166 global $wgAutoloadClasses;
169 $autoload = $cacheData[
'autoload'];
170 $groupConfs = $cacheData[
'groups'];
172 foreach ( $groupConfs as $id => $conf ) {
173 $this->groups[$id] = MessageGroupBase::factory( $conf );
176 self::appendAutoloader( $autoload, $wgAutoloadClasses );
185 private static function appendAutoloader( array $additions, array &$to ) {
186 foreach ( $additions as $class => $file ) {
187 if ( isset( $to[$class] ) && $to[$class] !== $file ) {
188 $msg =
"Autoload conflict for $class: {$to[$class]} !== $file";
189 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.
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.