Merge "Make update.php file executable"
[mediawiki.git] / maintenance / generateJsonI18n.php
blob83e731ab9f3aec6f839bcb6f653a04b78a59f1ac
1 <?php
3 /**
4 * Convert a PHP messages file to a set of JSON messages files.
6 * Usage:
7 * php generateJsonI18n.php ExtensionName.i18n.php i18n/
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
25 * @ingroup Maintenance
28 require_once __DIR__ . '/Maintenance.php';
30 /**
31 * Maintenance script to generate JSON i18n files from a PHP i18n file.
33 * @ingroup Maintenance
35 class GenerateJsonI18n extends Maintenance {
36 public function __construct() {
37 parent::__construct();
38 $this->mDescription = "Build JSON messages files from a PHP messages file";
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( 'langcode', 'Language code; only needed for converting core i18n files',
43 false, true );
44 $this->addOption( 'extension', 'Perform default conversion on an extension',
45 false, true );
46 $this->addOption( 'shim-only', 'Only create or update the backward-compatibility shim' );
47 $this->addOption( 'supplementary', 'Find supplementary i18n files in subdirs and convert those',
48 false, false );
51 public function execute() {
52 global $IP;
54 $phpfile = $this->getArg( 0 );
55 $jsondir = $this->getArg( 1 );
56 $extension = $this->getOption( 'extension' );
57 $convertSupplementaryI18nFiles = $this->hasOption( 'supplementary' );
59 if ( $extension ) {
60 if ( $phpfile ) {
61 $this->error( "The phpfile is already specified, conflicts with --extension.\n", 1 );
63 $phpfile = "$IP/extensions/$extension/$extension.i18n.php";
66 if ( !$phpfile ) {
67 $this->error( "I'm here for an argument!\n" );
68 $this->maybeHelp( true );
69 // dies.
72 if ( $convertSupplementaryI18nFiles ) {
73 if ( is_readable( $phpfile ) ) {
74 $this->transformI18nFile( $phpfile, $jsondir );
75 } else {
76 // This is non-fatal because we might want to continue searching for
77 // i18n files in subdirs even if the extension does not include a
78 // primary i18n.php.
79 $this->error( "Warning: no primary i18n file was found." );
81 $this->output( "Searching for supplementary i18n files...\n" );
82 $dir_iterator = new RecursiveDirectoryIterator( dirname( $phpfile ) );
83 $iterator = new RecursiveIteratorIterator( $dir_iterator, RecursiveIteratorIterator::LEAVES_ONLY );
84 foreach ( $iterator as $path => $fileObject ) {
85 if ( fnmatch( "*.i18n.php", $fileObject->getFilename() ) ) {
86 $this->output( "Converting $path.\n" );
87 $this->transformI18nFile( $path );
90 } else {
91 // Just convert the primary i18n file.
92 $this->transformI18nFile( $phpfile, $jsondir );
96 public function transformI18nFile( $phpfile, $jsondir = null ) {
97 if ( !$jsondir ) {
98 // Assume the json directory should be in the same directory as the
99 // .i18n.php file.
100 $jsondir = dirname( $phpfile ) . "/i18n";
102 if ( !is_dir( $jsondir ) ) {
103 $this->output( "Creating directory $jsondir.\n" );
104 $success = mkdir( $jsondir );
105 if ( !$success ) {
106 $this->error( "Could not create directory $jsondir\n", 1 );
110 if ( $this->hasOption( 'shim-only' ) ) {
111 $this->shimOnly( $phpfile, $jsondir );
113 return;
116 if ( $jsondir === null ) {
117 $this->error( 'Argument [jsondir] is required unless --shim-only is specified.' );
118 $this->maybeHelp( true );
121 if ( !is_readable( $phpfile ) ) {
122 $this->error( "Error reading $phpfile\n", 1 );
124 include $phpfile;
125 $phpfileContents = file_get_contents( $phpfile );
127 if ( !isset( $messages ) ) {
128 $this->error( "PHP file $phpfile does not define \$messages array\n", 1 );
131 $extensionStyle = true;
132 if ( !isset( $messages['en'] ) || !is_array( $messages['en'] ) ) {
133 if ( !$this->hasOption( 'langcode' ) ) {
134 $this->error( "PHP file $phpfile does not set language codes, --langcode " .
135 "is required.\n", 1 );
137 $extensionStyle = false;
138 $langcode = $this->getOption( 'langcode' );
139 $messages = array( $langcode => $messages );
140 } elseif ( $this->hasOption( 'langcode' ) ) {
141 $this->output( "Warning: --langcode option set but will not be used.\n" );
144 foreach ( $messages as $langcode => $langmsgs ) {
145 $authors = $this->getAuthorsFromComment( $this->findCommentBefore(
146 $extensionStyle ? "\$messages['$langcode'] =" : '$messages =',
147 $phpfileContents
148 ) );
149 // Make sure the @metadata key is the first key in the output
150 $langmsgs = array_merge(
151 array( '@metadata' => array( 'authors' => $authors ) ),
152 $langmsgs
155 $jsonfile = "$jsondir/$langcode.json";
156 $success = file_put_contents(
157 $jsonfile,
158 FormatJson::encode( $langmsgs, "\t", FormatJson::ALL_OK ) . "\n"
160 if ( $success === false ) {
161 $this->error( "FAILED to write $jsonfile", 1 );
163 $this->output( "$jsonfile\n" );
166 if ( !$this->hasOption( 'langcode' ) ) {
167 $shim = $this->doShim( $jsondir );
168 file_put_contents( $phpfile, $shim );
171 $this->output( "All done.\n" );
172 $this->output( "Also add \$wgMessagesDirs['YourExtension'] = __DIR__ . '/i18n';\n" );
175 protected function shimOnly( $phpfile, $jsondir ) {
176 if ( file_exists( $phpfile ) ) {
177 if ( !is_readable( $phpfile ) ) {
178 $this->error( "Error reading $phpfile\n", 1 );
181 $phpfileContents = file_get_contents( $phpfile );
182 $m = array();
183 if ( !preg_match( '!"/([^"$]+)/\$csCode.json";!', $phpfileContents, $m ) ) {
184 $this->error( "Cannot recognize $phpfile as a shim.\n", 1 );
187 if ( $jsondir === null ) {
188 $jsondir = $m[1];
191 $this->output( "Updating existing shim $phpfile\n" );
192 } elseif ( $jsondir === null ) {
193 $this->error( "$phpfile does not exist.\n" .
194 "Argument [jsondir] is required in order to create a new shim.\n", 1 );
195 } else {
196 $this->output( "Creating new shim $phpfile\n" );
199 $shim = $this->doShim( $jsondir );
200 file_put_contents( $phpfile, $shim );
201 $this->output( "All done.\n" );
204 protected function doShim( $jsondir ) {
205 $shim = <<<'PHP'
206 <?php
208 * This is a backwards-compatibility shim, generated by:
209 * https://git.wikimedia.org/blob/mediawiki%2Fcore.git/HEAD/maintenance%2FgenerateJsonI18n.php
211 * Beginning with MediaWiki 1.23, translation strings are stored in json files,
212 * and the EXTENSION.i18n.php file only exists to provide compatibility with
213 * older releases of MediaWiki. For more information about this migration, see:
214 * https://www.mediawiki.org/wiki/Requests_for_comment/Localisation_format
216 * This shim maintains compatibility back to MediaWiki 1.17.
218 $messages = array();
219 if ( !function_exists( '{{FUNC}}' ) ) {
220 function {{FUNC}}( $cache, $code, &$cachedData ) {
221 $codeSequence = array_merge( array( $code ), $cachedData['fallbackSequence'] );
222 foreach ( $codeSequence as $csCode ) {
223 $fileName = dirname( __FILE__ ) . "/{{OUT}}/$csCode.json";
224 if ( is_readable( $fileName ) ) {
225 $data = FormatJson::decode( file_get_contents( $fileName ), true );
226 foreach ( array_keys( $data ) as $key ) {
227 if ( $key === '' || $key[0] === '@' ) {
228 unset( $data[$key] );
231 $cachedData['messages'] = array_merge( $data, $cachedData['messages'] );
234 $cachedData['deps'][] = new FileDependency( $fileName );
236 return true;
239 $GLOBALS['wgHooks']['LocalisationCacheRecache'][] = '{{FUNC}}';
242 PHP;
244 $jsondir = str_replace( '\\', '/', $jsondir );
245 $shim = str_replace( '{{OUT}}', $jsondir, $shim );
246 $shim = str_replace( '{{FUNC}}', 'wfJsonI18nShim' . wfRandomString( 16 ), $shim );
248 return $shim;
252 * Find the documentation comment immediately before a given search string
253 * @param string $needle String to search for
254 * @param string $haystack String to search in
255 * @return string Substring of $haystack starting at '/**' ending right before $needle, or empty
257 protected function findCommentBefore( $needle, $haystack ) {
258 $needlePos = strpos( $haystack, $needle );
259 if ( $needlePos === false ) {
260 return '';
262 // Need to pass a negative offset to strrpos() so it'll search backwards from the
263 // offset
264 $startPos = strrpos( $haystack, '/**', $needlePos - strlen( $haystack ) );
265 if ( $startPos === false ) {
266 return '';
269 return substr( $haystack, $startPos, $needlePos - $startPos );
273 * Get an array of author names from a documentation comment containing @author declarations.
274 * @param string $comment Documentation comment
275 * @return array Array of author names (strings)
277 protected function getAuthorsFromComment( $comment ) {
278 $matches = null;
279 preg_match_all( '/@author (.*?)$/m', $comment, $matches );
281 return $matches && $matches[1] ? $matches[1] : array();
285 $maintClass = "GenerateJsonI18n";
286 require_once RUN_MAINTENANCE_IF_MAIN;