Merge "Less false positives for MEDIATYPE_VIDEO"
[lhc/web/wiklou.git] / maintenance / generateJsonI18n.php
1 <?php
2
3 /**
4 * Convert a PHP messages file to a set of JSON messages files.
5 *
6 * Usage:
7 * php generateJsonI18n.php ExtensionName.i18n.php i18n/
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 * @ingroup Maintenance
26 */
27
28 require_once __DIR__ . '/Maintenance.php';
29
30 /**
31 * Maintenance script to generate JSON i18n files from a PHP i18n file.
32 *
33 * @ingroup Maintenance
34 */
35 class GenerateJsonI18n extends Maintenance {
36 public function __construct() {
37 parent::__construct();
38 $this->mDescription = "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( '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 );
49 }
50
51 public function execute() {
52 global $IP;
53
54 $phpfile = $this->getArg( 0 );
55 $jsondir = $this->getArg( 1 );
56 $extension = $this->getOption( 'extension' );
57 $convertSupplementaryI18nFiles = $this->hasOption( 'supplementary' );
58
59 if ( $extension ) {
60 if ( $phpfile ) {
61 $this->error( "The phpfile is already specified, conflicts with --extension.\n", 1 );
62 }
63 $phpfile = "$IP/extensions/$extension/$extension.i18n.php";
64 }
65
66 if ( !$phpfile ) {
67 $this->error( "I'm here for an argument!\n" );
68 $this->maybeHelp( true );
69 // dies.
70 }
71
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." );
80 }
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 );
88 }
89 }
90 } else {
91 // Just convert the primary i18n file.
92 $this->transformI18nFile( $phpfile, $jsondir );
93 }
94 }
95
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";
101 }
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 );
107 }
108 }
109
110 if ( $this->hasOption( 'shim-only' ) ) {
111 $this->shimOnly( $phpfile, $jsondir );
112
113 return;
114 }
115
116 if ( $jsondir === null ) {
117 $this->error( 'Argument [jsondir] is required unless --shim-only is specified.' );
118 $this->maybeHelp( true );
119 }
120
121 if ( !is_readable( $phpfile ) ) {
122 $this->error( "Error reading $phpfile\n", 1 );
123 }
124 include $phpfile;
125 $phpfileContents = file_get_contents( $phpfile );
126
127 if ( !isset( $messages ) ) {
128 $this->error( "PHP file $phpfile does not define \$messages array\n", 1 );
129 }
130
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 );
136 }
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" );
142 }
143
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
153 );
154
155 $jsonfile = "$jsondir/$langcode.json";
156 $success = file_put_contents(
157 $jsonfile,
158 FormatJson::encode( $langmsgs, "\t", FormatJson::ALL_OK ) . "\n"
159 );
160 if ( $success === false ) {
161 $this->error( "FAILED to write $jsonfile", 1 );
162 }
163 $this->output( "$jsonfile\n" );
164 }
165
166 if ( !$this->hasOption( 'langcode' ) ) {
167 $shim = $this->doShim( $jsondir );
168 file_put_contents( $phpfile, $shim );
169 }
170
171 $this->output( "All done.\n" );
172 $this->output( "Also add \$wgMessagesDirs['YourExtension'] = __DIR__ . '/i18n';\n" );
173 }
174
175 protected function shimOnly( $phpfile, $jsondir ) {
176 if ( file_exists( $phpfile ) ) {
177 if ( !is_readable( $phpfile ) ) {
178 $this->error( "Error reading $phpfile\n", 1 );
179 }
180
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 );
185 }
186
187 if ( $jsondir === null ) {
188 $jsondir = $m[1];
189 }
190
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" );
197 }
198
199 $shim = $this->doShim( $jsondir );
200 file_put_contents( $phpfile, $shim );
201 $this->output( "All done.\n" );
202 }
203
204 protected function doShim( $jsondir ) {
205 $shim = <<<'PHP'
206 <?php
207 /**
208 * This is a backwards-compatibility shim, generated by:
209 * https://git.wikimedia.org/blob/mediawiki%2Fcore.git/HEAD/maintenance%2FgenerateJsonI18n.php
210 *
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
215 *
216 * This shim maintains compatibility back to MediaWiki 1.17.
217 */
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] );
229 }
230 }
231 $cachedData['messages'] = array_merge( $data, $cachedData['messages'] );
232 }
233
234 $cachedData['deps'][] = new FileDependency( $fileName );
235 }
236 return true;
237 }
238
239 $GLOBALS['wgHooks']['LocalisationCacheRecache'][] = '{{FUNC}}';
240 }
241
242 PHP;
243
244 $jsondir = str_replace( '\\', '/', $jsondir );
245 $shim = str_replace( '{{OUT}}', $jsondir, $shim );
246 $shim = str_replace( '{{FUNC}}', 'wfJsonI18nShim' . wfRandomString( 16 ), $shim );
247
248 return $shim;
249 }
250
251 /**
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
256 */
257 protected function findCommentBefore( $needle, $haystack ) {
258 $needlePos = strpos( $haystack, $needle );
259 if ( $needlePos === false ) {
260 return '';
261 }
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 '';
267 }
268
269 return substr( $haystack, $startPos, $needlePos - $startPos );
270 }
271
272 /**
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)
276 */
277 protected function getAuthorsFromComment( $comment ) {
278 $matches = null;
279 preg_match_all( '/@author (.*?)$/m', $comment, $matches );
280
281 return $matches && $matches[1] ? $matches[1] : array();
282 }
283 }
284
285 $maintClass = "GenerateJsonI18n";
286 require_once RUN_MAINTENANCE_IF_MAIN;