Adds an --extension option to generateJsonI18n
[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 }
48
49 public function execute() {
50 global $IP;
51
52 $phpfile = $this->getArg( 0 );
53 $jsondir = $this->getArg( 1 );
54 $extension = $this->getOption( 'extension' );
55
56 if ( $extension ) {
57 if ( $phpfile ) {
58 $this->error( "The phpfile is already specified, conflicts with --extension.\n", 1 );
59 }
60 $phpfile = "$IP/extensions/$extension/$extension.i18n.php";
61 }
62
63 if ( !$phpfile ) {
64 $this->error( "I'm here for an argument!\n" );
65 $this->maybeHelp( true );
66 // dies.
67 }
68
69 $this->transformI18nFile( $phpfile, $jsondir );
70 }
71
72 public function transformI18nFile( $phpfile, $jsondir = null ) {
73 if ( !$jsondir ) {
74 // Assume the json directory should be in the same directory as the
75 // .i18n.php file.
76 $jsondir = dirname( $phpfile ) . "/i18n";
77 }
78 if ( !is_dir( $jsondir ) ) {
79 $this->output( "Creating directory $jsondir.\n" );
80 $success = mkdir( $jsondir );
81 if ( !$success ) {
82 $this->error( "Could not create directory $jsondir\n", 1 );
83 }
84 }
85
86 if ( $this->hasOption( 'shim-only' ) ) {
87 $this->shimOnly( $phpfile, $jsondir );
88
89 return;
90 }
91
92 if ( $jsondir === null ) {
93 $this->error( 'Argument [jsondir] is required unless --shim-only is specified.' );
94 $this->maybeHelp( true );
95 }
96
97 if ( !is_readable( $phpfile ) ) {
98 $this->error( "Error reading $phpfile\n", 1 );
99 }
100 include $phpfile;
101 $phpfileContents = file_get_contents( $phpfile );
102
103 if ( !isset( $messages ) ) {
104 $this->error( "PHP file $phpfile does not define \$messages array\n", 1 );
105 }
106
107 $extensionStyle = true;
108 if ( !isset( $messages['en'] ) || !is_array( $messages['en'] ) ) {
109 if ( !$this->hasOption( 'langcode' ) ) {
110 $this->error( "PHP file $phpfile does not set language codes, --langcode " .
111 "is required.\n", 1 );
112 }
113 $extensionStyle = false;
114 $langcode = $this->getOption( 'langcode' );
115 $messages = array( $langcode => $messages );
116 } elseif ( $this->hasOption( 'langcode' ) ) {
117 $this->output( "Warning: --langcode option set but will not be used.\n" );
118 }
119
120 foreach ( $messages as $langcode => $langmsgs ) {
121 $authors = $this->getAuthorsFromComment( $this->findCommentBefore(
122 $extensionStyle ? "\$messages['$langcode'] =" : '$messages =',
123 $phpfileContents
124 ) );
125 // Make sure the @metadata key is the first key in the output
126 $langmsgs = array_merge(
127 array( '@metadata' => array( 'authors' => $authors ) ),
128 $langmsgs
129 );
130
131 $jsonfile = "$jsondir/$langcode.json";
132 $success = file_put_contents(
133 $jsonfile,
134 FormatJson::encode( $langmsgs, "\t", FormatJson::ALL_OK ) . "\n"
135 );
136 if ( $success === false ) {
137 $this->error( "FAILED to write $jsonfile", 1 );
138 }
139 $this->output( "$jsonfile\n" );
140 }
141
142 if ( !$this->hasOption( 'langcode' ) ) {
143 $shim = $this->doShim( $jsondir );
144 file_put_contents( $phpfile, $shim );
145 }
146
147 $this->output( "All done.\n" );
148 $this->output( "Also add \$wgMessagesDirs['YourExtension'] = __DIR__ . '/i18n';\n" );
149 }
150
151 protected function shimOnly( $phpfile, $jsondir ) {
152 if ( file_exists( $phpfile ) ) {
153 if ( !is_readable( $phpfile ) ) {
154 $this->error( "Error reading $phpfile\n", 1 );
155 }
156
157 $phpfileContents = file_get_contents( $phpfile );
158 $m = array();
159 if ( !preg_match( '!"/([^"$]+)/\$csCode.json";!', $phpfileContents, $m ) ) {
160 $this->error( "Cannot recognize $phpfile as a shim.\n", 1 );
161 }
162
163 if ( $jsondir === null ) {
164 $jsondir = $m[1];
165 }
166
167 $this->output( "Updating existing shim $phpfile\n" );
168 } elseif ( $jsondir === null ) {
169 $this->error( "$phpfile does not exist.\n" .
170 "Argument [jsondir] is required in order to create a new shim.\n", 1 );
171 } else {
172 $this->output( "Creating new shim $phpfile\n" );
173 }
174
175 $shim = $this->doShim( $jsondir );
176 file_put_contents( $phpfile, $shim );
177 $this->output( "All done.\n" );
178 }
179
180 protected function doShim( $jsondir ) {
181 $shim = <<<'PHP'
182 <?php
183 /**
184 * This is a backwards-compatibility shim, generated by:
185 * https://git.wikimedia.org/blob/mediawiki%2Fcore.git/HEAD/maintenance%2FgenerateJsonI18n.php
186 *
187 * Beginning with MediaWiki 1.23, translation strings are stored in json files,
188 * and the EXTENSION.i18n.php file only exists to provide compatibility with
189 * older releases of MediaWiki. For more information about this migration, see:
190 * https://www.mediawiki.org/wiki/Requests_for_comment/Localisation_format
191 *
192 * This shim maintains compatibility back to MediaWiki 1.17.
193 */
194 $messages = array();
195 if ( !function_exists( '{{FUNC}}' ) ) {
196 function {{FUNC}}( $cache, $code, &$cachedData ) {
197 $codeSequence = array_merge( array( $code ), $cachedData['fallbackSequence'] );
198 foreach ( $codeSequence as $csCode ) {
199 $fileName = dirname( __FILE__ ) . "/{{OUT}}/$csCode.json";
200 if ( is_readable( $fileName ) ) {
201 $data = FormatJson::decode( file_get_contents( $fileName ), true );
202 foreach ( array_keys( $data ) as $key ) {
203 if ( $key === '' || $key[0] === '@' ) {
204 unset( $data[$key] );
205 }
206 }
207 $cachedData['messages'] = array_merge( $data, $cachedData['messages'] );
208 }
209
210 $cachedData['deps'][] = new FileDependency( $fileName );
211 }
212 return true;
213 }
214
215 $GLOBALS['wgHooks']['LocalisationCacheRecache'][] = '{{FUNC}}';
216 }
217
218 PHP;
219
220 $jsondir = str_replace( '\\', '/', $jsondir );
221 $shim = str_replace( '{{OUT}}', $jsondir, $shim );
222 $shim = str_replace( '{{FUNC}}', 'wfJsonI18nShim' . wfRandomString( 16 ), $shim );
223
224 return $shim;
225 }
226
227 /**
228 * Find the documentation comment immediately before a given search string
229 * @param string $needle String to search for
230 * @param string $haystack String to search in
231 * @return string Substring of $haystack starting at '/**' ending right before $needle, or empty
232 */
233 protected function findCommentBefore( $needle, $haystack ) {
234 $needlePos = strpos( $haystack, $needle );
235 if ( $needlePos === false ) {
236 return '';
237 }
238 // Need to pass a negative offset to strrpos() so it'll search backwards from the
239 // offset
240 $startPos = strrpos( $haystack, '/**', $needlePos - strlen( $haystack ) );
241 if ( $startPos === false ) {
242 return '';
243 }
244
245 return substr( $haystack, $startPos, $needlePos - $startPos );
246 }
247
248 /**
249 * Get an array of author names from a documentation comment containing @author declarations.
250 * @param string $comment Documentation comment
251 * @return array Array of author names (strings)
252 */
253 protected function getAuthorsFromComment( $comment ) {
254 $matches = null;
255 preg_match_all( '/@author (.*?)$/m', $comment, $matches );
256
257 return $matches && $matches[1] ? $matches[1] : array();
258 }
259 }
260
261 $maintClass = "GenerateJsonI18n";
262 require_once RUN_MAINTENANCE_IF_MAIN;