summaryrefslogtreecommitdiff
blob: a663864f9fb92b730afad579499fd8f383f49258 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
 * This file contains a class to load file based message groups and handle
 * the related cache.
 *
 * @file
 * @author Abijeet Patro
 * @license GPL-2.0-or-later
 */

use MediaWiki\MediaWikiServices;

/**
 * Loads FileBasedMessageGroup, and handles the related cache.
 * @since 2019.05
 */
class FileBasedMessageGroupLoader extends MessageGroupLoader
	implements CachedMessageGroupLoader {

	private const CACHE_KEY = 'filebased';
	private const CACHE_VERSION = 2;

	/**
	 * List of groups
	 * @var array|null
	 */
	protected $groups;
	/** @var MessageGroupWANCache */
	protected $cache;

	public function __construct( MessageGroupWANCache $cache ) {
		$this->cache = $cache;
		$this->cache->configure(
			[
				'key' => self::CACHE_KEY,
				'version' => self::CACHE_VERSION,
				'regenerator' => [ $this, 'getCacheData' ],
				'touchedCallback' => [ $this, 'isExpired' ]
			]
		);
	}

	/**
	 * Fetches FileBasedMessageGroups
	 *
	 * @return MessageGroup[] Key is the MessageGroup Id and value is the corresponding
	 * FileBasedMessageGroup
	 */
	public function getGroups() {
		if ( $this->groups === null ) {
			/** @var DependencyWrapper $wrapper */
			$wrapper = $this->cache->getValue();
			$cacheData = $wrapper->getValue();
			$this->initFromCacheValue( $cacheData );
		}

		return $this->groups;
	}

	/**
	 * Hook: TranslateInitGroupLoaders
	 *
	 * @param array &$groupLoader
	 * @param array $deps Dependencies
	 */
	public static function registerLoader( array &$groupLoader, array $deps ) {
		$groupLoader[] = self::getInstance(
			$deps['cache']
		);
	}

	/**
	 * Return an instance of this class using the parameters, if passed,
	 * else initialize the necessary dependencies and return an instance.
	 *
	 * @param WANObjectCache|null $cache
	 * @return self
	 */
	public static function getInstance( WANObjectCache $cache = null ) {
		return new self(
			new MessageGroupWANCache(
				$cache ?? MediaWikiServices::getInstance()->getMainWANObjectCache()
			)
		);
	}

	/**
	 * Generates data to be stored in the cache.
	 *
	 * @return DependencyWrapper
	 */
	public function getCacheData() {
		global $wgTranslateGroupFiles;

		$autoload = $groups = $deps = $value = [];
		$deps[] = new GlobalDependency( 'wgTranslateGroupFiles' );

		// TODO: See if DI can be used here
		$parser = new MessageGroupConfigurationParser();
		foreach ( $wgTranslateGroupFiles as $configFile ) {
			$deps[] = new FileDependency( realpath( $configFile ) );

			$yaml = file_get_contents( $configFile );
			$fgroups = $parser->getHopefullyValidConfigurations(
				$yaml,
				static function ( $index, $config, $errmsg ) use ( $configFile ) {
					trigger_error( "Document $index in $configFile is invalid: $errmsg", E_USER_WARNING );
				}
			);

			foreach ( $fgroups as $id => $conf ) {
				if ( !empty( $conf['AUTOLOAD'] ) && is_array( $conf['AUTOLOAD'] ) ) {
					$dir = dirname( $configFile );
					$additions = array_map( static function ( $file ) use ( $dir ) {
						return "$dir/$file";
					}, $conf['AUTOLOAD'] );
					self::appendAutoloader( $additions, $autoload );
				}

				$groups[$id] = $conf;
			}
		}
		$value['groups'] = $groups;
		$value['autoload'] = $autoload;

		$wrapper = new DependencyWrapper( $value, $deps );
		$wrapper->initialiseDeps();
		return $wrapper;
	}

	/**
	 * Clear and refill the cache with the latest values
	 */
	public function recache() {
		$this->clearProcessCache();
		$this->cache->touchKey();

		/** @var DependencyWrapper $wrapper */
		$wrapper = $this->cache->getValue( 'recache' );
		$cacheData = $wrapper->getValue();
		$this->initFromCacheValue( $cacheData );
	}

	/**
	 * Clear values from the cache
	 */
	public function clearCache() {
		$this->clearProcessCache();
		$this->cache->delete();
	}

	/**
	 * Clears the process cache, mainly the cached groups property.
	 */
	protected function clearProcessCache() {
		$this->groups = null;
	}

	/**
	 * Initialize groups and autoload classes from the cache value.
	 *
	 * @param array $cacheData
	 */
	protected function initFromCacheValue( array $cacheData ) {
		global $wgAutoloadClasses;
		$this->groups = [];

		$autoload = $cacheData['autoload'];
		$groupConfs = $cacheData['groups'];

		foreach ( $groupConfs as $id => $conf ) {
			$this->groups[$id] = MessageGroupBase::factory( $conf );
		}

		self::appendAutoloader( $autoload, $wgAutoloadClasses );
	}
}