MediaWiki  master
generateJsonI18n.php
Go to the documentation of this file.
1 <?php
2 
28 require_once __DIR__ . '/Maintenance.php';
29 
36  public function __construct() {
37  parent::__construct();
38  $this->addDescription( 'Build JSON messages files from a PHP messages file' );
39 
40  $this->addArg( 'phpfile', 'PHP file defining a $messages array', false );
41  $this->addArg( 'jsondir', 'Directory to write JSON files to', false );
42  $this->addOption( 'extension', 'Perform default conversion on an extension',
43  false, true );
44  $this->addOption( 'supplementary', 'Find supplementary i18n files in subdirs and convert those',
45  false, false );
46  }
47 
48  public function execute() {
49  global $IP;
50 
51  $phpfile = $this->getArg( 0 );
52  $jsondir = $this->getArg( 1 );
53  $extension = $this->getOption( 'extension' );
54  $convertSupplementaryI18nFiles = $this->hasOption( 'supplementary' );
55 
56  if ( $extension ) {
57  if ( $phpfile ) {
58  $this->fatalError( "The phpfile is already specified, conflicts with --extension." );
59  }
60  $phpfile = "$IP/extensions/$extension/$extension.i18n.php";
61  }
62 
63  if ( !$phpfile ) {
64  $this->error( "I'm here for an argument!" );
65  $this->maybeHelp( true );
66  // dies.
67  }
68 
69  if ( $convertSupplementaryI18nFiles ) {
70  if ( is_readable( $phpfile ) ) {
71  $this->transformI18nFile( $phpfile, $jsondir );
72  } else {
73  // This is non-fatal because we might want to continue searching for
74  // i18n files in subdirs even if the extension does not include a
75  // primary i18n.php.
76  $this->error( "Warning: no primary i18n file was found." );
77  }
78  $this->output( "Searching for supplementary i18n files...\n" );
79  $dir_iterator = new RecursiveDirectoryIterator( dirname( $phpfile ) );
80  $iterator = new RecursiveIteratorIterator(
81  $dir_iterator, RecursiveIteratorIterator::LEAVES_ONLY );
83  foreach ( $iterator as $path => $fileObject ) {
84  if ( fnmatch( "*.i18n.php", $fileObject->getFilename() ) ) {
85  $this->output( "Converting $path.\n" );
86  $this->transformI18nFile( $path );
87  }
88  }
89  } else {
90  // Just convert the primary i18n file.
91  $this->transformI18nFile( $phpfile, $jsondir );
92  }
93  }
94 
95  public function transformI18nFile( $phpfile, $jsondir = null ) {
96  if ( !$jsondir ) {
97  // Assume the json directory should be in the same directory as the
98  // .i18n.php file.
99  $jsondir = dirname( $phpfile ) . "/i18n";
100  }
101  if ( !is_dir( $jsondir ) ) {
102  $this->output( "Creating directory $jsondir.\n" );
103  $success = mkdir( $jsondir );
104  if ( !$success ) {
105  $this->fatalError( "Could not create directory $jsondir" );
106  }
107  }
108 
109  if ( !is_readable( $phpfile ) ) {
110  $this->fatalError( "Error reading $phpfile" );
111  }
112  $messages = null;
113  include $phpfile;
114  $phpfileContents = file_get_contents( $phpfile );
115 
116  // @phan-suppress-next-line PhanImpossibleCondition Set by include of php file
117  if ( !isset( $messages ) ) {
118  $this->fatalError( "PHP file $phpfile does not define \$messages array" );
119  }
120 
121  if ( !$messages ) {
122  $this->fatalError( "PHP file $phpfile contains an empty \$messages array. " .
123  "Maybe it was already converted?" );
124  }
125 
126  if ( !isset( $messages['en'] ) || !is_array( $messages['en'] ) ) {
127  $this->fatalError( "PHP file $phpfile does not set language codes" );
128  }
129 
130  foreach ( $messages as $langcode => $langmsgs ) {
131  $authors = $this->getAuthorsFromComment( $this->findCommentBefore(
132  "\$messages['$langcode'] =",
133  $phpfileContents
134  ) );
135  // Make sure the @metadata key is the first key in the output
136  $langmsgs = array_merge(
137  [ '@metadata' => [ 'authors' => $authors ] ],
138  $langmsgs
139  );
140 
141  $jsonfile = "$jsondir/$langcode.json";
142  $success = file_put_contents(
143  $jsonfile,
144  FormatJson::encode( $langmsgs, "\t", FormatJson::ALL_OK ) . "\n"
145  );
146  if ( $success === false ) {
147  $this->fatalError( "FAILED to write $jsonfile" );
148  }
149  $this->output( "$jsonfile\n" );
150  }
151 
152  $this->output(
153  "All done. To complete the conversion, please do the following:\n" .
154  "* Add \$wgMessagesDirs['YourExtension'] = __DIR__ . '/i18n';\n" .
155  "* Remove \$wgExtensionMessagesFiles['YourExtension']\n" .
156  "* Delete the old PHP message file\n" .
157  "This script no longer generates backward compatibility shims! If you need\n" .
158  "compatibility with MediaWiki 1.22 and older, use the MediaWiki 1.23 version\n" .
159  "of this script instead, or create a shim manually.\n"
160  );
161  }
162 
169  protected function findCommentBefore( $needle, $haystack ) {
170  $needlePos = strpos( $haystack, $needle );
171  if ( $needlePos === false ) {
172  return '';
173  }
174  // Need to pass a negative offset to strrpos() so it'll search backwards from the
175  // offset
176  $startPos = strrpos( $haystack, '
189  protected function getAuthorsFromComment( $comment ) {
190  $matches = null;
191  preg_match_all( '/@author (.*?)$/m', $comment, $matches );
192 
193  return $matches && $matches[1] ? $matches[1] : [];
194  }
195 }
196 
197 $maintClass = GenerateJsonI18n::class;
198 require_once RUN_MAINTENANCE_IF_MAIN;
GenerateJsonI18n\__construct
__construct()
Default constructor.
Definition: generateJsonI18n.php:36
Maintenance\maybeHelp
maybeHelp( $force=false)
Maybe show the help.
Definition: Maintenance.php:1046
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:487
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
$success
$success
Definition: NoLocalSettings.php:42
FormatJson\ALL_OK
const ALL_OK
Skip escaping as many characters as reasonably possible.
Definition: FormatJson.php:55
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
GenerateJsonI18n\execute
execute()
Do the actual work.
Definition: generateJsonI18n.php:48
GenerateJsonI18n\getAuthorsFromComment
getAuthorsFromComment( $comment)
Get an array of author names from a documentation comment containing.
Definition: generateJsonI18n.php:189
GenerateJsonI18n
Maintenance script to generate JSON i18n files from a PHP i18n file.
Definition: generateJsonI18n.php:35
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
GenerateJsonI18n\findCommentBefore
findCommentBefore( $needle, $haystack)
Find the documentation comment immediately before a given search string.
Definition: generateJsonI18n.php:169
GenerateJsonI18n\transformI18nFile
transformI18nFile( $phpfile, $jsondir=null)
Definition: generateJsonI18n.php:95
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:298
$path
$path
Definition: NoLocalSettings.php:25
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:463
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:351
$IP
$IP
Definition: WebStart.php:49