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