18require_once __DIR__ .
'/Maintenance.php';
28 parent::__construct();
29 $this->
addDescription(
'Build JSON messages files from a PHP messages file' );
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',
35 $this->
addOption(
'supplementary',
'Find supplementary i18n files in subdirs and convert those',
42 $phpfile = $this->
getArg( 0 );
43 $jsondir = $this->
getArg( 1 );
44 $extension = $this->
getOption(
'extension' );
45 $convertSupplementaryI18nFiles = $this->
hasOption(
'supplementary' );
49 $this->
fatalError(
"The phpfile is already specified, conflicts with --extension." );
51 $phpfile =
"$wgExtensionDirectory/$extension/$extension.i18n.php";
55 $this->
error(
"I'm here for an argument!" );
60 if ( $convertSupplementaryI18nFiles ) {
61 if ( is_readable( $phpfile ) ) {
67 $this->
error(
"Warning: no primary i18n file was found." );
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" );
90 $jsondir = dirname( $phpfile ) .
"/i18n";
92 if ( !is_dir( $jsondir ) ) {
93 $this->
output(
"Creating directory $jsondir.\n" );
96 $this->
fatalError(
"Could not create directory $jsondir" );
100 if ( !is_readable( $phpfile ) ) {
101 $this->
fatalError(
"Error reading $phpfile" );
105 $phpfileContents = file_get_contents( $phpfile );
108 if ( !isset( $messages ) ) {
109 $this->
fatalError(
"PHP file $phpfile does not define \$messages array" );
113 $this->
fatalError(
"PHP file $phpfile contains an empty \$messages array. " .
114 "Maybe it was already converted?" );
117 if ( !isset( $messages[
'en'] ) || !is_array( $messages[
'en'] ) ) {
118 $this->
fatalError(
"PHP file $phpfile does not set language codes" );
121 foreach ( $messages as $langcode => $langmsgs ) {
123 "\$messages['$langcode'] =",
127 $langmsgs = array_merge(
128 [
'@metadata' => [
'authors' => $authors ] ],
132 $jsonfile =
"$jsondir/$langcode.json";
135 FormatJson::encode( $langmsgs,
"\t", FormatJson::ALL_OK ) .
"\n"
138 $this->
fatalError(
"FAILED to write $jsonfile" );
140 $this->
output(
"$jsonfile\n" );
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"
161 $needlePos = strpos( $haystack, $needle );
162 if ( $needlePos ===
false ) {
167 $startPos = strrpos( $haystack,
'
180 protected function getAuthorsFromComment( $comment ) {
181 return preg_match_all( '/@author (.*?)$/m
', $comment, $matches ) ? $matches[1] : [];
185// @codeCoverageIgnoreStart
186$maintClass = GenerateJsonI18n::class;
187require_once RUN_MAINTENANCE_IF_MAIN;
188// @codeCoverageIgnoreEnd
Maintenance script to generate JSON i18n files from a PHP i18n file.
__construct()
Default constructor.
getAuthorsFromComment( $comment)
Get an array of author names from a documentation comment containing.
transformI18nFile(string $phpfile, ?string $jsondir=null)
findCommentBefore( $needle, $haystack)
Find the documentation comment immediately before a given search string.
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
maybeHelp( $force=false)
Maybe show the help.
addDescription( $text)
Set the description text.
$wgExtensionDirectory
Config variable stub for the ExtensionDirectory setting, for use by phpdoc and IDEs.