Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 86 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
GenerateJsonI18n | |
0.00% |
0 / 86 |
|
0.00% |
0 / 5 |
650 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
72 | |||
transformI18nFile | |
0.00% |
0 / 45 |
|
0.00% |
0 / 1 |
132 | |||
findCommentBefore | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
getAuthorsFromComment | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 |
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 | use MediaWiki\Json\FormatJson; |
29 | use MediaWiki\Maintenance\Maintenance; |
30 | |
31 | // @codeCoverageIgnoreStart |
32 | require_once __DIR__ . '/Maintenance.php'; |
33 | // @codeCoverageIgnoreEnd |
34 | |
35 | /** |
36 | * Maintenance script to generate JSON i18n files from a PHP i18n file. |
37 | * |
38 | * @ingroup Maintenance |
39 | */ |
40 | class GenerateJsonI18n extends Maintenance { |
41 | public function __construct() { |
42 | parent::__construct(); |
43 | $this->addDescription( 'Build JSON messages files from a PHP messages file' ); |
44 | |
45 | $this->addArg( 'phpfile', 'PHP file defining a $messages array', false ); |
46 | $this->addArg( 'jsondir', 'Directory to write JSON files to', false ); |
47 | $this->addOption( 'extension', 'Perform default conversion on an extension', |
48 | false, true ); |
49 | $this->addOption( 'supplementary', 'Find supplementary i18n files in subdirs and convert those', |
50 | false, false ); |
51 | } |
52 | |
53 | public function execute() { |
54 | global $IP; |
55 | |
56 | $phpfile = $this->getArg( 0 ); |
57 | $jsondir = $this->getArg( 1 ); |
58 | $extension = $this->getOption( 'extension' ); |
59 | $convertSupplementaryI18nFiles = $this->hasOption( 'supplementary' ); |
60 | |
61 | if ( $extension ) { |
62 | if ( $phpfile ) { |
63 | $this->fatalError( "The phpfile is already specified, conflicts with --extension." ); |
64 | } |
65 | $phpfile = "$IP/extensions/$extension/$extension.i18n.php"; |
66 | } |
67 | |
68 | if ( !$phpfile ) { |
69 | $this->error( "I'm here for an argument!" ); |
70 | $this->maybeHelp( true ); |
71 | // dies. |
72 | } |
73 | |
74 | if ( $convertSupplementaryI18nFiles ) { |
75 | if ( is_readable( $phpfile ) ) { |
76 | $this->transformI18nFile( $phpfile, $jsondir ); |
77 | } else { |
78 | // This is non-fatal because we might want to continue searching for |
79 | // i18n files in subdirs even if the extension does not include a |
80 | // primary i18n.php. |
81 | $this->error( "Warning: no primary i18n file was found." ); |
82 | } |
83 | $this->output( "Searching for supplementary i18n files...\n" ); |
84 | $dir_iterator = new RecursiveDirectoryIterator( dirname( $phpfile ) ); |
85 | $iterator = new RecursiveIteratorIterator( |
86 | $dir_iterator, RecursiveIteratorIterator::LEAVES_ONLY ); |
87 | /** @var SplFileInfo $fileObject */ |
88 | foreach ( $iterator as $path => $fileObject ) { |
89 | if ( fnmatch( "*.i18n.php", $fileObject->getFilename() ) ) { |
90 | $this->output( "Converting $path.\n" ); |
91 | $this->transformI18nFile( $path ); |
92 | } |
93 | } |
94 | } else { |
95 | // Just convert the primary i18n file. |
96 | $this->transformI18nFile( $phpfile, $jsondir ); |
97 | } |
98 | } |
99 | |
100 | public function transformI18nFile( $phpfile, $jsondir = null ) { |
101 | if ( !$jsondir ) { |
102 | // Assume the json directory should be in the same directory as the |
103 | // .i18n.php file. |
104 | $jsondir = dirname( $phpfile ) . "/i18n"; |
105 | } |
106 | if ( !is_dir( $jsondir ) ) { |
107 | $this->output( "Creating directory $jsondir.\n" ); |
108 | $success = mkdir( $jsondir ); |
109 | if ( !$success ) { |
110 | $this->fatalError( "Could not create directory $jsondir" ); |
111 | } |
112 | } |
113 | |
114 | if ( !is_readable( $phpfile ) ) { |
115 | $this->fatalError( "Error reading $phpfile" ); |
116 | } |
117 | $messages = null; |
118 | include $phpfile; |
119 | $phpfileContents = file_get_contents( $phpfile ); |
120 | |
121 | // @phan-suppress-next-line PhanImpossibleCondition,MediaWikiNoIssetIfDefined Set by include of php file |
122 | if ( !isset( $messages ) ) { |
123 | $this->fatalError( "PHP file $phpfile does not define \$messages array" ); |
124 | } |
125 | |
126 | if ( !$messages ) { |
127 | $this->fatalError( "PHP file $phpfile contains an empty \$messages array. " . |
128 | "Maybe it was already converted?" ); |
129 | } |
130 | |
131 | if ( !isset( $messages['en'] ) || !is_array( $messages['en'] ) ) { |
132 | $this->fatalError( "PHP file $phpfile does not set language codes" ); |
133 | } |
134 | |
135 | foreach ( $messages as $langcode => $langmsgs ) { |
136 | $authors = $this->getAuthorsFromComment( $this->findCommentBefore( |
137 | "\$messages['$langcode'] =", |
138 | $phpfileContents |
139 | ) ); |
140 | // Make sure the @metadata key is the first key in the output |
141 | $langmsgs = array_merge( |
142 | [ '@metadata' => [ 'authors' => $authors ] ], |
143 | $langmsgs |
144 | ); |
145 | |
146 | $jsonfile = "$jsondir/$langcode.json"; |
147 | $success = file_put_contents( |
148 | $jsonfile, |
149 | FormatJson::encode( $langmsgs, "\t", FormatJson::ALL_OK ) . "\n" |
150 | ); |
151 | if ( $success === false ) { |
152 | $this->fatalError( "FAILED to write $jsonfile" ); |
153 | } |
154 | $this->output( "$jsonfile\n" ); |
155 | } |
156 | |
157 | $this->output( |
158 | "All done. To complete the conversion, please do the following:\n" . |
159 | "* Add \$wgMessagesDirs['YourExtension'] = __DIR__ . '/i18n';\n" . |
160 | "* Remove \$wgExtensionMessagesFiles['YourExtension']\n" . |
161 | "* Delete the old PHP message file\n" . |
162 | "This script no longer generates backward compatibility shims! If you need\n" . |
163 | "compatibility with MediaWiki 1.22 and older, use the MediaWiki 1.23 version\n" . |
164 | "of this script instead, or create a shim manually.\n" |
165 | ); |
166 | } |
167 | |
168 | /** |
169 | * Find the documentation comment immediately before a given search string |
170 | * @param string $needle String to search for |
171 | * @param string $haystack String to search in |
172 | * @return string Substring of $haystack starting at '/**' ending right before $needle, or empty |
173 | */ |
174 | protected function findCommentBefore( $needle, $haystack ) { |
175 | $needlePos = strpos( $haystack, $needle ); |
176 | if ( $needlePos === false ) { |
177 | return ''; |
178 | } |
179 | // Need to pass a negative offset to strrpos() so it'll search backwards from the |
180 | // offset |
181 | $startPos = strrpos( $haystack, '/**', $needlePos - strlen( $haystack ) ); |
182 | if ( $startPos === false ) { |
183 | return ''; |
184 | } |
185 | |
186 | return substr( $haystack, $startPos, $needlePos - $startPos ); |
187 | } |
188 | |
189 | /** |
190 | * Get an array of author names from a documentation comment containing @author declarations. |
191 | * @param string $comment Documentation comment |
192 | * @return string[] Array of author names |
193 | */ |
194 | protected function getAuthorsFromComment( $comment ) { |
195 | return preg_match_all( '/@author (.*?)$/m', $comment, $matches ) ? $matches[1] : []; |
196 | } |
197 | } |
198 | |
199 | // @codeCoverageIgnoreStart |
200 | $maintClass = GenerateJsonI18n::class; |
201 | require_once RUN_MAINTENANCE_IF_MAIN; |
202 | // @codeCoverageIgnoreEnd |