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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
<?php
/**
* Script to export translations of one message group to file(s).
*
* @author Niklas Laxström
* @author Siebrand Mazeland
* @copyright Copyright © 2008-2013, Niklas Laxström, Siebrand Mazeland
* @license GPL-2.0-or-later
* @file
*/
// Standard boilerplate to define $IP
if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
$IP = getenv( 'MW_INSTALL_PATH' );
} else {
$dir = __DIR__;
$IP = "$dir/../../..";
}
require_once "$IP/maintenance/Maintenance.php";
class CommandlineExport extends Maintenance {
private const EXPORT_LOG_FILE = 'translation-exports';
public function __construct() {
parent::__construct();
$this->addDescription( 'Message exporter.' );
$this->addOption(
'group',
'Comma separated list of group IDs (can use * as wildcard)',
true, /*required*/
true /*has arg*/
);
$this->addOption(
'lang',
'Comma separated list of language codes or *',
true, /*required*/
true /*has arg*/
);
$this->addOption(
'target',
'Target directory for exported files',
true, /*required*/
true /*has arg*/
);
$this->addOption(
'skip',
'(optional) Languages to skip, comma separated list',
false, /*required*/
true /*has arg*/
);
$this->addOption(
'skipgroup',
'(optional) Comma separated list of group IDs that should not be exported',
false, /*required*/
true /*has arg*/
);
$this->addOption(
'threshold',
'(optional) Do not export under this percentage translated',
false, /*required*/
true /*has arg*/
);
$this->addOption(
'hours',
'(optional) Only export languages with changes in the last given number of hours',
false, /*required*/
true /*has arg*/
);
$this->addOption(
'ppgettext',
'(optional) Group root path for checkout of product. "msgmerge" will post ' .
'process on the export result based on the current source file ' .
'in that location (from sourcePattern or definitionFile)',
false, /*required*/
true /*has arg*/
);
$this->addOption(
'no-location',
'(optional) Only used combined with "ppgettext". This option will rebuild ' .
'the gettext file without location information',
false, /*required*/
false /*has arg*/
);
$this->addOption(
'no-fuzzy',
'(optional) Do not include any messages marked as fuzzy/outdated',
false, /*required*/
false /*has arg*/
);
$this->addOption(
'codemaponly',
'(optional) Only export languages that have a codeMap entry',
false, /*required*/
false /*has arg*/
);
$this->addOption(
'offline-gettext-format',
'(optional) Export languages in offline Gettext format. Give a file pattern with '
. '%GROUPID% and %CODE%. Empty pattern defaults to %GROUPID%/%CODE%.po.',
false, /*required*/
true /*has arg*/
);
$this->requireExtension( 'Translate' );
}
public function execute() {
wfDebugLog( self::EXPORT_LOG_FILE, 'Starting exports for groups - '
. $this->getOption( 'group' ) . '... ' );
$exportStartTime = microtime( true );
$target = $this->getOption( 'target' );
if ( !is_writable( $target ) ) {
$this->fatalError( "Target directory is not writable ($target)." );
}
$threshold = $this->getOption( 'threshold' );
$noFuzzy = $this->hasOption( 'no-fuzzy' );
$noLocation = '';
if ( $this->hasOption( 'no-location' ) ) {
$noLocation = '--no-location ';
}
$skip = [];
if ( $this->hasOption( 'skip' ) ) {
$skip = array_map( 'trim', explode( ',', $this->getOption( 'skip' ) ) );
}
$reqLangs = TranslateUtils::parseLanguageCodes( $this->getOption( 'lang' ) );
$reqLangs = array_flip( $reqLangs );
foreach ( $skip as $skipLang ) {
unset( $reqLangs[$skipLang] );
}
$reqLangs = array_flip( $reqLangs );
$codemapOnly = $this->hasOption( 'codemaponly' );
$forOffline = $this->hasOption( 'offline-gettext-format' );
$offlineTargetPattern = $this->getOption( 'offline-gettext-format' ) ?: "%GROUPID%/%CODE%.po";
$groupIds = explode( ',', trim( $this->getOption( 'group' ) ) );
$groupIds = MessageGroups::expandWildcards( $groupIds );
$groups = MessageGroups::getGroupsById( $groupIds );
'@phan-var FileBasedMessageGroup[] $groups';
/** @var FileBasedMessageGroup $group */
foreach ( $groups as $groupId => $group ) {
if ( $group->isMeta() ) {
$this->output( "Skipping meta message group $groupId.\n" );
unset( $groups[$groupId] );
continue;
}
if ( !$forOffline && !$group instanceof FileBasedMessageGroup ) {
$this->output( "EE2: Unexportable message group $groupId.\n" );
unset( $groups[$groupId] );
continue;
}
}
if ( !count( $groups ) ) {
$this->fatalError( 'EE1: No valid message groups identified.' );
}
$changeFilter = false;
$hours = $this->getOption( 'hours' );
if ( $hours ) {
$namespaces = [];
/** @var FileBasedMessageGroup $group */
foreach ( $groups as $group ) {
$namespaces[$group->getNamespace()] = true;
}
$namespaces = array_keys( $namespaces );
$bots = true;
$changeFilter = [];
$rows = TranslateUtils::translationChanges( $hours, $bots, $namespaces );
foreach ( $rows as $row ) {
$title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
$handle = new MessageHandle( $title );
$code = $handle->getCode();
if ( !$code ) {
continue;
}
$groupIds = $handle->getGroupIds();
foreach ( $groupIds as $groupId ) {
$changeFilter[$groupId][$code] = true;
}
}
}
$skipGroups = [];
if ( $this->hasOption( 'skipgroup' ) ) {
$skipGroups = array_map( 'trim', explode( ',', $this->getOption( 'skipgroup' ) ) );
}
foreach ( $groups as $groupId => $group ) {
if ( in_array( $groupId, $skipGroups ) ) {
$this->output( "Group $groupId is in skipgroup.\n" );
continue;
}
// No changes to this group at all
if ( is_array( $changeFilter ) && !isset( $changeFilter[$groupId] ) ) {
$this->output( "No recent changes to $groupId.\n" );
continue;
}
$langs = $reqLangs;
if ( $codemapOnly ) {
foreach ( $langs as $index => $code ) {
if ( $group->mapCode( $code ) === $code ) {
unset( $langs[$index] );
}
}
}
if ( $threshold ) {
wfDebugLog( self::EXPORT_LOG_FILE, "Calculating stats for group $groupId" );
$tStartTime = microtime( true );
$stats = MessageGroupStats::forGroup( $groupId );
$emptyLangs = [];
foreach ( $langs as $index => $code ) {
if ( !isset( $stats[$code] ) ) {
unset( $langs[$index] );
continue;
}
$total = $stats[$code][MessageGroupStats::TOTAL];
$translated = $stats[$code][MessageGroupStats::TRANSLATED];
if ( $total === 0 ) {
$emptyLangs[] = $code;
unset( $langs[$index] );
continue;
}
if ( $translated / $total * 100 < $threshold ) {
unset( $langs[$index] );
}
}
if ( $emptyLangs !== [] ) {
$this->output(
"Message group $groupId doesn't contain messages in language(s): " .
implode( ', ', $emptyLangs ) . "."
);
}
$tEndTime = microtime( true );
wfDebugLog( self::EXPORT_LOG_FILE,
"Finished calculating stats for group $groupId. Time: "
. ( $tEndTime - $tStartTime ) . ' secs.' );
}
// Filter out unchanged languages from requested languages
if ( is_array( $changeFilter ) ) {
$langs = array_intersect( $langs, array_keys( $changeFilter[$groupId] ) );
}
if ( !count( $langs ) ) {
continue;
}
$this->output( 'Exporting ' . count( $langs ) . " languages for group $groupId" );
if ( $forOffline ) {
$fileBasedGroup = FileBasedMessageGroup::newFromMessageGroup( $group, $offlineTargetPattern );
$ffs = new GettextFFS( $fileBasedGroup );
$ffs->setOfflineMode( true );
} else {
$ffs = $group->getFFS();
}
$ffs->setWritePath( $target );
$sourceLanguage = $group->getSourceLanguage();
$collection = $group->initCollection( $sourceLanguage );
$definitionFile = false;
if ( $this->hasOption( 'ppgettext' ) && $ffs instanceof GettextFFS ) {
global $wgMaxShellMemory, $wgTranslateGroupRoot;
// Need more shell memory for msgmerge.
$wgMaxShellMemory = 402400;
$path = $group->getSourceFilePath( $sourceLanguage );
$definitionFile = str_replace(
$wgTranslateGroupRoot,
$this->getOption( 'ppgettext' ),
$path
);
}
$whitelist = $group->getTranslatableLanguages();
wfDebugLog(
self::EXPORT_LOG_FILE, 'Exporting languages ('
. count( $langs ) . ") for group - $groupId."
);
$langExportTimes = [
'collection' => 0,
'ffs' => 0,
'definitionFile' => 0
];
$langStartTime = microtime( true );
foreach ( $langs as $lang ) {
// Do not export languages that are blacklisted (or not whitelisted).
// Also check that whitelist is not null, which means that all
// languages are allowed for translation and export.
if ( is_array( $whitelist ) && !isset( $whitelist[$lang] ) ) {
continue;
}
$startTime = microtime( true );
$collection->resetForNewLanguage( $lang );
$collection->loadTranslations();
// Don't export ignored, unless it is the source language
// or message documentation
global $wgTranslateDocumentationLanguageCode;
if ( $lang !== $wgTranslateDocumentationLanguageCode
&& $lang !== $sourceLanguage
) {
$collection->filter( 'ignored' );
}
if ( $noFuzzy ) {
$collection->filter( 'fuzzy' );
}
$endTime = microtime( true );
$langExportTimes['collection'] += ( $endTime - $startTime );
$startTime = microtime( true );
$ffs->write( $collection );
$endTime = microtime( true );
$langExportTimes['ffs'] += ( $endTime - $startTime );
// Do post processing if requested.
if ( $definitionFile ) {
$startTime = microtime( true );
if ( is_file( $definitionFile ) ) {
$targetFileName = $ffs->getWritePath() .
'/' . $group->getTargetFilename( $collection->code );
$cmd = 'msgmerge --quiet ' . $noLocation . '--output-file=' .
$targetFileName . ' ' . $targetFileName . ' ' . $definitionFile;
wfShellExec( $cmd, $ret );
// Report on errors.
if ( $ret ) {
$this->error( "ERROR: $ret" );
}
} else {
$this->fatalError( "$definitionFile does not exist for group $groupId." );
}
$endTime = microtime( true );
$langExportTimes['definitionFile'] += ( $endTime - $startTime );
}
}
$langEndTime = microtime( true );
wfDebugLog(
self::EXPORT_LOG_FILE,
"Done exporting languages for group - $groupId. " .
'Time taken - ' . ( $langEndTime - $langStartTime ) . ' secs.'
);
foreach ( $langExportTimes as $type => $time ) {
wfDebugLog(
self::EXPORT_LOG_FILE,
"Time taken by '$type' for group $groupId - $time secs."
);
}
}
$exportEndTime = microtime( true );
wfDebugLog(
self::EXPORT_LOG_FILE, 'Finished export process for groups - ' .
$this->getOption( 'group' ) .
'. Time: ' . ( $exportEndTime - $exportStartTime ) . ' secs.'
);
}
}
$maintClass = CommandlineExport::class;
require_once RUN_MAINTENANCE_IF_MAIN;
|