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