Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
FileBasedMessageGroupLoader.php
Go to the documentation of this file.
1<?php
12use MediaWiki\MediaWikiServices;
13
19 implements CachedMessageGroupLoader {
20
21 private const CACHE_KEY = 'filebased';
22 private const CACHE_VERSION = 2;
23
28 protected $groups;
30 protected $cache;
31
32 public function __construct( MessageGroupWANCache $cache ) {
33 $this->cache = $cache;
34 $this->cache->configure(
35 [
36 'key' => self::CACHE_KEY,
37 'version' => self::CACHE_VERSION,
38 'regenerator' => [ $this, 'getCacheData' ],
39 'touchedCallback' => [ $this, 'isExpired' ]
40 ]
41 );
42 }
43
50 public function getGroups() {
51 if ( $this->groups === null ) {
53 $wrapper = $this->cache->getValue();
54 $cacheData = $wrapper->getValue();
55 $this->initFromCacheValue( $cacheData );
56 }
57
58 return $this->groups;
59 }
60
67 public static function registerLoader( array &$groupLoader, array $deps ) {
68 $groupLoader[] = self::getInstance(
69 $deps['cache']
70 );
71 }
72
80 public static function getInstance( WANObjectCache $cache = null ) {
81 return new self(
83 $cache ?? MediaWikiServices::getInstance()->getMainWANObjectCache()
84 )
85 );
86 }
87
93 public function getCacheData() {
94 global $wgTranslateGroupFiles;
95
96 $autoload = $groups = $deps = $value = [];
97 $deps[] = new GlobalDependency( 'wgTranslateGroupFiles' );
98
99 // TODO: See if DI can be used here
100 $parser = new MessageGroupConfigurationParser();
101 foreach ( $wgTranslateGroupFiles as $configFile ) {
102 $deps[] = new FileDependency( realpath( $configFile ) );
103
104 $yaml = file_get_contents( $configFile );
105 $fgroups = $parser->getHopefullyValidConfigurations(
106 $yaml,
107 static function ( $index, $config, $errmsg ) use ( $configFile ) {
108 trigger_error( "Document $index in $configFile is invalid: $errmsg", E_USER_WARNING );
109 }
110 );
111
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 ) {
116 return "$dir/$file";
117 }, $conf['AUTOLOAD'] );
118 self::appendAutoloader( $additions, $autoload );
119 }
120
121 $groups[$id] = $conf;
122 }
123 }
124 $value['groups'] = $groups;
125 $value['autoload'] = $autoload;
126
127 $wrapper = new DependencyWrapper( $value, $deps );
128 $wrapper->initialiseDeps();
129 return $wrapper;
130 }
131
135 public function recache() {
136 $this->clearProcessCache();
137 $this->cache->touchKey();
138
140 $wrapper = $this->cache->getValue( 'recache' );
141 $cacheData = $wrapper->getValue();
142 $this->initFromCacheValue( $cacheData );
143 }
144
148 public function clearCache() {
149 $this->clearProcessCache();
150 $this->cache->delete();
151 }
152
156 protected function clearProcessCache() {
157 $this->groups = null;
158 }
159
165 protected function initFromCacheValue( array $cacheData ) {
166 global $wgAutoloadClasses;
167 $this->groups = [];
168
169 $autoload = $cacheData['autoload'];
170 $groupConfs = $cacheData['groups'];
171
172 foreach ( $groupConfs as $id => $conf ) {
173 $this->groups[$id] = MessageGroupBase::factory( $conf );
174 }
175
176 self::appendAutoloader( $autoload, $wgAutoloadClasses );
177 }
178
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 );
190 continue;
191 }
192
193 $to[$class] = $file;
194 }
195 }
196}
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.