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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
<?php
/**
* This file a contains a message group implementation.
*
* @file
* @author Niklas Laxström
* @copyright Copyright © 2010-2013, Niklas Laxström
* @license GPL-2.0-or-later
*/
/**
* This class implements default behavior for file based message groups.
*
* File based message groups are primary type of groups at translatewiki.net,
* while other projects may use mainly page translation message groups, or
* custom type of message groups.
* @ingroup MessageGroup
*/
class FileBasedMessageGroup extends MessageGroupBase implements MetaYamlSchemaExtender {
public const NO_FFS_CLASS = 1;
public const INVALID_FFS_CLASS = 2;
protected $reverseCodeMap;
/**
* Constructs a FileBasedMessageGroup from any normal message group.
* Useful for doing special Gettext exports from any group.
* @param MessageGroup $group
* @param string $targetPattern Value for FILES.targetPattern
* @return self
*/
public static function newFromMessageGroup(
MessageGroup $group,
string $targetPattern = ''
) {
$conf = [
'BASIC' => [
'class' => self::class,
'id' => $group->getId(),
'label' => $group->getLabel(),
'namespace' => $group->getNamespace(),
],
'FILES' => [
'sourcePattern' => '',
'targetPattern' => $targetPattern,
],
];
$group = MessageGroupBase::factory( $conf );
if ( !$group instanceof self ) {
$actual = get_class( $group );
throw new DomainException( "Expected FileBasedMessageGroup, got $actual" );
}
return $group;
}
public function getFFS(): SimpleFFS {
$class = $this->getFromConf( 'FILES', 'class' );
if ( $class === null ) {
throw new RuntimeException( 'FFS class is not set.', self::NO_FFS_CLASS );
}
if ( !class_exists( $class ) ) {
throw new RuntimeException( "FFS class $class does not exist.", self::INVALID_FFS_CLASS );
}
return new $class( $this );
}
public function exists() {
$ffs = $this->getFFS();
return $ffs->exists();
}
public function load( $code ) {
$ffs = $this->getFFS();
$data = $ffs->read( $code );
return $data ? $data['MESSAGES'] : [];
}
/**
* @param string $code Language tag.
* @return array Array with keys MESSAGES, AUTHORS and EXTRA, containing only primitive values.
* @since 2020.04
*/
public function parseExternal( string $code ): array {
$supportedKeys = [ 'MESSAGES', 'AUTHORS', 'EXTRA' ];
$parsedData = $this->getFFS()->read( $code );
// Ensure we return correct keys
$data = [];
foreach ( $supportedKeys as $key ) {
$data[$key] = $parsedData[$key] ?? [];
}
return $data;
}
/**
* @param string $code Language code.
* @return string
* @throws MWException
*/
public function getSourceFilePath( $code ) {
if ( $this->isSourceLanguage( $code ) ) {
$pattern = $this->getFromConf( 'FILES', 'definitionFile' );
if ( $pattern !== null ) {
return $this->replaceVariables( $pattern, $code );
}
}
$pattern = $this->getFromConf( 'FILES', 'sourcePattern' );
if ( $pattern === null ) {
throw new MWException( 'No source file pattern defined.' );
}
return $this->replaceVariables( $pattern, $code );
}
public function getTargetFilename( $code ) {
// Check if targetPattern explicitly defined
$pattern = $this->getFromConf( 'FILES', 'targetPattern' );
if ( $pattern !== null ) {
return $this->replaceVariables( $pattern, $code );
}
// Check if definitionFile is explicitly defined
if ( $this->isSourceLanguage( $code ) ) {
$pattern = $this->getFromConf( 'FILES', 'definitionFile' );
}
// Fallback to sourcePattern which must be defined
if ( $pattern === null ) {
$pattern = $this->getFromConf( 'FILES', 'sourcePattern' );
}
if ( $pattern === null ) {
throw new MWException( 'No source file pattern defined.' );
}
// For exports, the scripts take output directory. We want to
// return a path where the prefix is current directory instead
// of full path of the source location.
$pattern = str_replace( '%GROUPROOT%', '.', $pattern );
return $this->replaceVariables( $pattern, $code );
}
/**
* @param string $pattern
* @param string $code Language code.
* @return string
* @since 2014.02 Made public
*/
public function replaceVariables( $pattern, $code ) {
global $IP, $wgTranslateGroupRoot;
$variables = [
'%CODE%' => $this->mapCode( $code ),
'%MWROOT%' => $IP,
'%GROUPROOT%' => $wgTranslateGroupRoot,
'%GROUPID%' => $this->getId(),
];
return str_replace( array_keys( $variables ), array_values( $variables ), $pattern );
}
/**
* @param string $code Language code.
* @return string
*/
public function mapCode( $code ) {
if ( !isset( $this->conf['FILES']['codeMap'] ) ) {
return $code;
}
if ( isset( $this->conf['FILES']['codeMap'][$code] ) ) {
return $this->conf['FILES']['codeMap'][$code];
} else {
if ( !isset( $this->reverseCodeMap ) ) {
$this->reverseCodeMap = array_flip( $this->conf['FILES']['codeMap'] );
}
if ( isset( $this->reverseCodeMap[$code] ) ) {
return 'x-invalidLanguageCode';
}
return $code;
}
}
public static function getExtraSchema() {
$schema = [
'root' => [
'_type' => 'array',
'_children' => [
'FILES' => [
'_type' => 'array',
'_children' => [
'class' => [
'_type' => 'text',
'_not_empty' => true,
],
'codeMap' => [
'_type' => 'array',
'_ignore_extra_keys' => true,
'_children' => [],
],
'definitionFile' => [
'_type' => 'text',
],
'sourcePattern' => [
'_type' => 'text',
'_not_empty' => true,
],
'targetPattern' => [
'_type' => 'text',
],
]
]
]
]
];
return $schema;
}
/** @inheritDoc */
public function getKeys() {
$cache = $this->getMessageGroupCache( $this->getSourceLanguage() );
if ( !$cache->exists() ) {
return array_keys( $this->getDefinitions() );
} else {
return $cache->getKeys();
}
}
/** @inheritDoc */
public function initCollection( $code ) {
$namespace = $this->getNamespace();
$messages = [];
$cache = $this->getMessageGroupCache( $this->getSourceLanguage() );
if ( !$cache->exists() ) {
wfWarn( "By-passing message group cache for {$this->getId()}" );
$messages = $this->getDefinitions();
} else {
foreach ( $cache->getKeys() as $key ) {
$messages[$key] = $cache->get( $key );
}
}
$definitions = new MessageDefinitions( $messages, $namespace );
$collection = MessageCollection::newFromDefinitions( $definitions, $code );
$this->setTags( $collection );
return $collection;
}
/** @inheritDoc */
public function getMessage( $key, $code ) {
$cache = $this->getMessageGroupCache( $code );
if ( $cache->exists() ) {
$msg = $cache->get( $key );
if ( $msg !== false ) {
return $msg;
}
// Try harder
$nkey = str_replace( ' ', '_', strtolower( $key ) );
$keys = $cache->getKeys();
foreach ( $keys as $k ) {
if ( $nkey === str_replace( ' ', '_', strtolower( $k ) ) ) {
return $cache->get( $k );
}
}
return null;
} else {
return null;
}
}
public function getMessageGroupCache( string $code ): MessageGroupCache {
$cacheFilePath = TranslateUtils::cacheFile(
"translate_groupcache-{$this->getId()}/{$code}.cdb"
);
return new MessageGroupCache( $this, $code, $cacheFilePath );
}
}
|