MediaWiki
master
Toggle main menu visibility
generateJsonI18n.php
Go to the documentation of this file.
1
<?php
2
13
14
use
MediaWiki\Json\FormatJson
;
15
use
MediaWiki\Maintenance\Maintenance
;
16
17
// @codeCoverageIgnoreStart
18
require_once __DIR__ .
'/Maintenance.php'
;
19
// @codeCoverageIgnoreEnd
20
26
class
GenerateJsonI18n
extends
Maintenance
{
27
public
function
__construct
() {
28
parent::__construct();
29
$this->addDescription(
'Build JSON messages files from a PHP messages file'
);
30
31
$this->addArg(
'phpfile'
,
'PHP file defining a $messages array'
,
false
);
32
$this->addArg(
'jsondir'
,
'Directory to write JSON files to'
,
false
);
33
$this->addOption(
'extension'
,
'Perform default conversion on an extension'
,
34
false
,
true
);
35
$this->addOption(
'supplementary'
,
'Find supplementary i18n files in subdirs and convert those'
,
36
false
,
false
);
37
}
38
39
public
function
execute
() {
40
global
$wgExtensionDirectory
;
41
42
$phpfile = $this->getArg( 0 );
43
$jsondir = $this->getArg( 1 );
44
$extension = $this->getOption(
'extension'
);
45
$convertSupplementaryI18nFiles = $this->hasOption(
'supplementary'
);
46
47
if
( $extension ) {
48
if
( $phpfile ) {
49
$this->fatalError(
"The phpfile is already specified, conflicts with --extension."
);
50
}
51
$phpfile =
"$wgExtensionDirectory/$extension/$extension.i18n.php"
;
52
}
53
54
if
( !$phpfile ) {
55
$this->error(
"I'm here for an argument!"
);
56
$this->maybeHelp(
true
);
57
// dies.
58
}
59
60
if
( $convertSupplementaryI18nFiles ) {
61
if
( is_readable( $phpfile ) ) {
62
$this->
transformI18nFile
( $phpfile, $jsondir );
63
}
else
{
64
// This is non-fatal because we might want to continue searching for
65
// i18n files in subdirs even if the extension does not include a
66
// primary i18n.php.
67
$this->error(
"Warning: no primary i18n file was found."
);
68
}
69
$this->output(
"Searching for supplementary i18n files...\n"
);
70
$dir_iterator =
new
RecursiveDirectoryIterator( dirname( $phpfile ) );
71
$iterator =
new
RecursiveIteratorIterator(
72
$dir_iterator, RecursiveIteratorIterator::LEAVES_ONLY );
74
foreach
( $iterator as
$path
=> $fileObject ) {
75
if
( fnmatch(
"*.i18n.php"
, $fileObject->getFilename() ) ) {
76
$this->output(
"Converting $path.\n"
);
77
$this->
transformI18nFile
(
$path
);
78
}
79
}
80
}
else
{
81
// Just convert the primary i18n file.
82
$this->
transformI18nFile
( $phpfile, $jsondir );
83
}
84
}
85
86
public
function
transformI18nFile
(
string
$phpfile, ?
string
$jsondir =
null
) {
87
if
( !$jsondir ) {
88
// Assume the json directory should be in the same directory as the
89
// .i18n.php file.
90
$jsondir = dirname( $phpfile ) .
"/i18n"
;
91
}
92
if
( !is_dir( $jsondir ) ) {
93
$this->output(
"Creating directory $jsondir.\n"
);
94
$success
= mkdir( $jsondir );
95
if
( !
$success
) {
96
$this->fatalError(
"Could not create directory $jsondir"
);
97
}
98
}
99
100
if
( !is_readable( $phpfile ) ) {
101
$this->fatalError(
"Error reading $phpfile"
);
102
}
103
$messages =
null
;
104
include $phpfile;
105
$phpfileContents = file_get_contents( $phpfile );
106
107
// @phan-suppress-next-line PhanImpossibleCondition,MediaWikiNoIssetIfDefined Set by include of php file
108
if
( !isset( $messages ) ) {
109
$this->fatalError(
"PHP file $phpfile does not define \$messages array"
);
110
}
111
112
if
( !$messages ) {
113
$this->fatalError(
"PHP file $phpfile contains an empty \$messages array. "
.
114
"Maybe it was already converted?"
);
115
}
116
117
if
( !isset( $messages[
'en'
] ) || !is_array( $messages[
'en'
] ) ) {
118
$this->fatalError(
"PHP file $phpfile does not set language codes"
);
119
}
120
121
foreach
( $messages as $langcode => $langmsgs ) {
122
$authors = $this->
getAuthorsFromComment
( $this->
findCommentBefore
(
123
"\$messages['$langcode'] ="
,
124
$phpfileContents
125
) );
126
// Make sure the @metadata key is the first key in the output
127
$langmsgs = array_merge(
128
[
'@metadata'
=> [
'authors'
=> $authors ] ],
129
$langmsgs
130
);
131
132
$jsonfile =
"$jsondir/$langcode.json"
;
133
$success
= file_put_contents(
134
$jsonfile,
135
FormatJson::encode
( $langmsgs,
"\t"
,
FormatJson::ALL_OK
) .
"\n"
136
);
137
if
(
$success
===
false
) {
138
$this->fatalError(
"FAILED to write $jsonfile"
);
139
}
140
$this->output(
"$jsonfile\n"
);
141
}
142
143
$this->output(
144
"All done. To complete the conversion, please do the following:\n"
.
145
"* Add \$wgMessagesDirs['YourExtension'] = __DIR__ . '/i18n';\n"
.
146
"* Remove \$wgExtensionMessagesFiles['YourExtension']\n"
.
147
"* Delete the old PHP message file\n"
.
148
"This script no longer generates backward compatibility shims! If you need\n"
.
149
"compatibility with MediaWiki 1.22 and older, use the MediaWiki 1.23 version\n"
.
150
"of this script instead, or create a shim manually.\n"
151
);
152
}
153
160
protected
function
findCommentBefore
( $needle, $haystack ) {
161
$needlePos = strpos( $haystack, $needle );
162
if
( $needlePos ===
false
) {
163
return
''
;
164
}
165
// Need to pass a negative offset to strrpos() so it'll search backwards from the
166
// offset
167
$startPos = strrpos( $haystack,
'
174
180
protected function getAuthorsFromComment( $comment ) {
181
return preg_match_all( '/@author (.*?)$/m
', $comment, $matches ) ? $matches[1] : [];
182
}
183
}
184
185
// @codeCoverageIgnoreStart
186
$maintClass = GenerateJsonI18n::class;
187
require_once RUN_MAINTENANCE_IF_MAIN;
188
// @codeCoverageIgnoreEnd
$path
$path
Definition
NoLocalSettings.php:14
$success
$success
Definition
NoLocalSettings.php:24
GenerateJsonI18n
Maintenance script to generate JSON i18n files from a PHP i18n file.
Definition
generateJsonI18n.php:26
GenerateJsonI18n\__construct
__construct()
Definition
generateJsonI18n.php:27
GenerateJsonI18n\getAuthorsFromComment
getAuthorsFromComment( $comment)
Get an array of author names from a documentation comment containing.
Definition
generateJsonI18n.php:180
GenerateJsonI18n\transformI18nFile
transformI18nFile(string $phpfile, ?string $jsondir=null)
Definition
generateJsonI18n.php:86
GenerateJsonI18n\findCommentBefore
findCommentBefore( $needle, $haystack)
Find the documentation comment immediately before a given search string.
Definition
generateJsonI18n.php:160
GenerateJsonI18n\execute
execute()
Definition
generateJsonI18n.php:39
MediaWiki\Json\FormatJson
JSON formatter wrapper class.
Definition
FormatJson.php:17
MediaWiki\Json\FormatJson\ALL_OK
const ALL_OK
Skip escaping as many characters as reasonably possible.
Definition
FormatJson.php:46
MediaWiki\Json\FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition
FormatJson.php:83
$wgExtensionDirectory
$wgExtensionDirectory
Config variable stub for the ExtensionDirectory setting, for use by phpdoc and IDEs.
Definition
config-vars.php:109
MediaWiki\Maintenance
Definition
applyChangeTag.php:11
maintenance
generateJsonI18n.php
Generated on
for MediaWiki by
1.18.0