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
|
<?php
/**
* Script to export special page aliases and magic words of extensions.
*
* @author Robert Leverington <robert@rhl.me.uk>
*
* @copyright Copyright © 2010 Robert Leverington
* @license GPL-2.0-or-later
* @file
*/
// Standard boilerplate to define $IP
if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
$IP = getenv( 'MW_INSTALL_PATH' );
} else {
$IP = __DIR__ . '/../../../';
}
require_once "$IP/maintenance/Maintenance.php";
/**
* Maintenance class for the fast export of special page aliases and magic words.
*/
class MagicExport extends Maintenance {
protected $type;
protected $target;
protected $handles = [];
protected $messagesOld = [];
protected $extraInformation = [];
public function __construct() {
parent::__construct();
$this->mDescription = 'Export of aliases and magic words for MediaWiki extensions.';
$this->addOption(
'target',
'Target directory for exported files',
true, /*required*/
true /*has arg*/
);
$this->addOption(
'type',
'magic or special',
true, /*required*/
true /*has arg*/
);
}
public function execute() {
$this->target = $this->getOption( 'target' );
$this->type = $this->getOption( 'type' );
switch ( $this->type ) {
case 'special':
case 'magic':
break;
default:
$this->error( 'Invalid type.', 1 );
}
$this->openHandles();
$this->writeHeaders();
$this->writeFiles();
$this->writeFooters();
$this->closeHandles();
}
/**
* Iterate through all groups, loading current data from the existing
* extension and opening message files for message output.
* - If the group does not define a special page alias file or magic
* words file, or that file does not exist, it is ignored silently.
* - If the file does contain a data array (e.g. $aliases) then the
* program exits.
*/
protected function openHandles() {
$this->output( "Opening file handles and loading current data...\n" );
$groups = MessageGroups::singleton()->getGroups();
foreach ( $groups as $group ) {
if ( !$group instanceof MediaWikiExtensionMessageGroup ) {
continue;
}
$conf = $group->getConfiguration();
$inFile = $outFile = null;
if ( $this->type === 'special' && isset( $conf['FILES']['aliasFile'] ) ) {
$inFile = $conf['FILES']['aliasFileSource'];
$outFile = $conf['FILES']['aliasFile'];
}
if ( $this->type === 'magic' && isset( $conf['FILES']['magicFile'] ) ) {
$inFile = $conf['FILES']['magicFileSource'];
$outFile = $conf['FILES']['magicFile'];
}
if ( $inFile === null ) {
continue;
}
$inFile = $group->replaceVariables( $inFile, 'en' );
$outFile = $this->target . '/' . $outFile;
$varName = null;
if ( !is_readable( $inFile ) ) {
$this->error( "File '$inFile' not readable." );
continue;
}
include $inFile;
switch ( $this->type ) {
case 'special':
if ( isset( $aliases ) ) {
$this->messagesOld[$group->getId()] = $aliases;
unset( $aliases );
$varName = '$aliases';
} elseif ( isset( $specialPageAliases ) ) {
$this->messagesOld[$group->getId()] = $specialPageAliases;
unset( $specialPageAliases );
$varName = '$specialPageAliases';
} else {
$this->error( "File '$inFile' does not contain an aliases array." );
continue;
}
break;
case 'magic':
if ( !isset( $magicWords ) ) {
$this->error( "File '$inFile' does not contain a magic words array." );
continue;
}
$this->messagesOld[$group->getId()] = $magicWords;
unset( $magicWords );
$varName = '$magicWords';
break;
}
wfMkdirParents( dirname( $outFile ), null, __METHOD__ );
$this->handles[$group->getId()] = fopen( $outFile, 'w' );
$headerInformation = $this->readHeader( $inFile, $varName );
fwrite( $this->handles[$group->getId()], $headerInformation['fileBegin'] );
$this->extraInformation[$group->getId()] = $headerInformation;
$this->output( "\t{$group->getId()}\n" );
}
}
protected function readHeader( $file, $varName ) {
$data = file_get_contents( $file );
// Seek first '*/'.
$end = strpos( $data, '*/' );
// But not when it is the english comment
$varPos = strpos( $data, $varName );
if ( $varPos && $end && $varPos <= $end ) {
$end = false;
}
if ( $end === false ) {
$fileBegin = "<?php\n";
} else {
// Grab header.
$fileBegin = substr( $data, 0, $end + 2 );
}
// preserve the phpcs codingStandardsIgnoreFile, if exists
$preserveIgnoreTag = strpos( $data, '@codingStandardsIgnoreFile' ) !== false;
// preserve the long array syntax, if varName is written with it
$preserveLongArraySyntax = preg_match(
'/' . preg_quote( $varName, '/' ) . '\s*=\s*array\s*\(\s*\)\s*;/',
$data
);
// avoid difference by the last character
$preserveNewlineAtEnd = substr( $data, -1 ) === "\n";
return [
'fileBegin' => $fileBegin,
'preserveIgnoreTag' => $preserveIgnoreTag,
'preserveLongArraySyntax' => $preserveLongArraySyntax,
'preserveNewlineAtEnd' => $preserveNewlineAtEnd,
];
}
/**
* Write the opening of the files for each output file handle.
*/
protected function writeHeaders() {
foreach ( $this->handles as $group => $handle ) {
$arraySyntax = $this->extraInformation[$group]['preserveLongArraySyntax']
? 'array()'
: '[]';
switch ( $this->type ) {
case 'special':
$ignoreTag = $this->extraInformation[$group]['preserveIgnoreTag']
? "\n// @codingStandardsIgnoreFile"
: '';
fwrite( $handle, <<<PHP
$ignoreTag
\$specialPageAliases = $arraySyntax;
PHP
);
break;
case 'magic':
fwrite( $handle, <<<PHP
\$magicWords = $arraySyntax;
PHP
);
break;
}
}
}
/**
* Itterate through available languages, loading and parsing the data
* message from the MediaWiki namespace and writing the data to its output
* file handle.
*/
protected function writeFiles() {
$langs = TranslateUtils::parseLanguageCodes( '*' );
unset( $langs[array_search( 'en', $langs )] );
$langs = array_merge( [ 'en' ], $langs );
foreach ( $langs as $l ) {
// Load message page.
switch ( $this->type ) {
case 'special':
$title = Title::makeTitleSafe( NS_MEDIAWIKI, 'Sp-translate-data-SpecialPageAliases/' . $l );
break;
case 'magic':
$title = Title::makeTitleSafe( NS_MEDIAWIKI, 'Sp-translate-data-MagicWords/' . $l );
break;
default:
exit( 1 );
}
// Parse message page.
if ( !$title || !$title->exists() ) {
$this->output( "Skiping $l...\n" );
$messagesNew = [];
} else {
$this->output( "Processing $l...\n" );
$page = WikiPage::factory( $title );
$content = $page->getContent();
$data = $content->getNativeData();
// Parse message file.
$segments = explode( "\n", $data );
array_shift( $segments );
array_shift( $segments );
unset( $segments[count( $segments ) - 1] );
unset( $segments[count( $segments ) - 1] );
$messagesNew = [];
foreach ( $segments as $segment ) {
$parts = explode( ' = ', $segment );
$key = array_shift( $parts );
$translations = explode( ', ', implode( $parts ) );
$messagesNew[$key] = $translations;
}
}
// Write data to handles.
$namesEn = LanguageNames::getNames( 'en' );
$namesNative = Language::fetchLanguageNames();
foreach ( $this->handles as $group => $handle ) {
// Find messages to write to this handle.
$messagesOut = [];
if ( !isset( $this->messagesOld[$group] ) ) {
continue;
}
foreach ( $this->messagesOld[$group]['en'] as $key => $message ) {
if ( array_key_exists( $key, $messagesNew ) ) {
$messagesOut[$key] = $messagesNew[$key];
} elseif ( isset( $this->messagesOld[$group][$l][$key] ) ) {
$messagesOut[$key] = $this->messagesOld[$group][$l][$key];
}
}
if ( $this->extraInformation[$group]['preserveLongArraySyntax'] ) {
$arrayStart = 'array(';
$arrayEnd = ')';
} else {
$arrayStart = '[';
$arrayEnd = ']';
}
// If there are messages to write, write them.
if ( $messagesOut !== [] ) {
$out = '';
switch ( $this->type ) {
case 'special':
$out .= "\n\n/** {$namesEn[$l]} ({$namesNative[$l]}) " .
"*/\n\$specialPageAliases['{$l}'] = {$arrayStart}\n";
break;
case 'magic':
$out .= "\n\n/** {$namesEn[$l]} ({$namesNative[$l]}) *" .
"/\n\$magicWords['{$l}'] = {$arrayStart}\n";
break;
}
foreach ( $messagesOut as $key => $translations ) {
if ( !is_array( $translations ) ) {
$this->error( "$l in $group has not an array..." );
continue;
}
foreach ( $translations as $id => $translation ) {
$translations[$id] = addslashes( $translation );
if ( $this->type === 'magic' && $translation === 0 ) {
unset( $translations[$id] );
}
}
$translations = implode( "', '", $translations );
switch ( $this->type ) {
case 'special':
$out .= "\t'$key' => $arrayStart '$translations' $arrayEnd,\n";
break;
case 'magic':
if ( $this->messagesOld[$group]['en'][$key][0] === 0 ) {
$out .= "\t'$key' => $arrayStart 0, '$translations' $arrayEnd,\n";
} else {
$out .= "\t'$key' => $arrayStart '$translations' $arrayEnd,\n";
}
break;
}
}
$out .= "$arrayEnd;";
fwrite( $handle, $out );
}
}
}
}
/**
* Do whatever needs doing after writing the primary content.
*/
protected function writeFooters() {
$this->output( "Writing file footers...\n" );
foreach ( $this->handles as $group => $handle ) {
if ( $this->extraInformation[$group]['preserveNewlineAtEnd'] ) {
// php files should end with a newline
fwrite( $handle, "\n" );
}
}
}
/**
* Close all output file handles.
*/
protected function closeHandles() {
$this->output( "Closing file handles...\n" );
foreach ( $this->handles as $handle ) {
fclose( $handle );
}
}
}
$maintClass = MagicExport::class;
require_once RUN_MAINTENANCE_IF_MAIN;
|