Merge "Allow additional interwiki prefixes on local interwiki links"
[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 $this->addArg( 'phpfile', 'PHP file defining a $messages array', true );
40 $this->addArg( 'jsondir', 'Directory to write JSON files to', true );
41 $this->addOption( 'langcode', 'Language code; only needed for converting core i18n files',
42 false, true );
43 }
44
45 public function execute() {
46 $phpfile = $this->getArg( 0 );
47 $jsondir = $this->getArg( 1 );
48
49 if ( !is_readable( $phpfile ) ) {
50 $this->error( "Error reading $phpfile\n", 1 );
51 }
52 include $phpfile;
53 $phpfileContents = file_get_contents( $phpfile );
54
55 if ( !isset( $messages ) ) {
56 $this->error( "PHP file $phpfile does not define \$messages array\n", 1 );
57 }
58
59 $extensionStyle = true;
60 if ( !isset( $messages['en'] ) || !is_array( $messages['en'] ) ) {
61 if ( !$this->hasOption( 'langcode' ) ) {
62 $this->error( "PHP file $phpfile does not set language codes, --langcode " .
63 "is required.\n", 1 );
64 }
65 $extensionStyle = false;
66 $langcode = $this->getOption( 'langcode' );
67 $messages = array( $langcode => $messages );
68 } elseif ( $this->hasOption( 'langcode' ) ) {
69 $this->output( "Warning: --langcode option set but will not be used.\n" );
70 }
71
72 foreach ( $messages as $langcode => $langmsgs ) {
73 $authors = $this->getAuthorsFromComment( $this->findCommentBefore(
74 $extensionStyle ? "\$messages['$langcode'] =" : '$messages =',
75 $phpfileContents
76 ) );
77 // Make sure the @metadata key is the first key in the output
78 $langmsgs = array_merge(
79 array( '@metadata' => array( 'authors' => $authors ) ),
80 $langmsgs
81 );
82
83 $jsonfile = "$jsondir/$langcode.json";
84 $success = file_put_contents(
85 $jsonfile,
86 FormatJson::encode( $langmsgs, true, FormatJson::ALL_OK ) . "\n"
87 );
88 if ( $success === false ) {
89 $this->error( "FAILED to write $jsonfile", 1 );
90 }
91 $this->output( "$jsonfile\n" );
92 }
93
94 if ( !$this->hasOption( 'langcode' ) ) {
95 $shim = $this->doShim( $jsondir );
96 file_put_contents( $phpfile, $shim );
97 }
98
99 $this->output( "All done.\n" );
100 $this->output( "Also add \$wgMessagesDirs['YourExtension'] = __DIR__ . '/i18n';\n" );
101 }
102
103 protected function doShim( $jsondir ) {
104 $shim = <<<'PHP'
105 <?php
106 /**
107 * This is a backwards-compatibility shim, generated by:
108 * https://git.wikimedia.org/blob/mediawiki%2Fcore.git/HEAD/maintenance%2FgenerateJsonI18n.php
109 *
110 * Beginning with MediaWiki 1.23, translation strings are stored in json files,
111 * and the EXTENSION.i18n.php file only exists to provide compatibility with
112 * older releases of MediaWiki. For more information about this migration, see:
113 * https://www.mediawiki.org/wiki/Requests_for_comment/Localisation_format
114 *
115 * This shim maintains compatibility back to MediaWiki 1.17.
116 */
117 $messages = array();
118 $GLOBALS['wgHooks']['LocalisationCacheRecache'][] = function ( $cache, $code, &$cachedData ) {
119 $codeSequence = array_merge( array( $code ), $cachedData['fallbackSequence'] );
120 foreach ( $codeSequence as $csCode ) {
121 $fileName = __DIR__ . "/{{OUT}}/$csCode.json";
122 if ( is_readable( $fileName ) ) {
123 $data = FormatJson::decode( file_get_contents( $fileName ), true );
124 foreach ( array_keys( $data ) as $key ) {
125 if ( $key === '' || $key[0] === '@' ) {
126 unset( $data[$key] );
127 }
128 }
129 $cachedData['messages'] = array_merge( $data, $cachedData['messages'] );
130 }
131
132 $cachedData['deps'][] = new FileDependency( $fileName );
133 }
134 return true;
135 };
136
137 PHP;
138
139 $jsondir = str_replace( '\\', '/', $jsondir );
140 $shim = str_replace( '{{OUT}}', $jsondir, $shim );
141 return $shim;
142 }
143
144 /**
145 * Find the documentation comment immediately before a given search string
146 * @param string $needle String to search for
147 * @param string $haystack String to search in
148 * @return string Substring of $haystack starting at '/**' ending right before $needle, or empty
149 */
150 protected function findCommentBefore( $needle, $haystack ) {
151 $needlePos = strpos( $haystack, $needle );
152 if ( $needlePos === false ) {
153 return '';
154 }
155 // Need to pass a negative offset to strrpos() so it'll search backwards from the
156 // offset
157 $startPos = strrpos( $haystack, '/**', $needlePos - strlen( $haystack ) );
158 if ( $startPos === false ) {
159 return '';
160 }
161
162 return substr( $haystack, $startPos, $needlePos - $startPos );
163 }
164
165 /**
166 * Get an array of author names from a documentation comment containing @author declarations.
167 * @param string $comment Documentation comment
168 * @return array Array of author names (strings)
169 */
170 protected function getAuthorsFromComment( $comment ) {
171 $matches = null;
172 preg_match_all( '/@author (.*?)$/m', $comment, $matches );
173 return $matches && $matches[1] ? $matches[1] : array();
174 }
175 }
176
177 $maintClass = "GenerateJsonI18n";
178 require_once RUN_MAINTENANCE_IF_MAIN;