4 * Convert a PHP messages file to a set of JSON messages files.
7 * php generateJsonI18n.php ExtensionName.i18n.php i18n/
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.
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.
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
25 * @ingroup Maintenance
28 require_once __DIR__
. '/Maintenance.php';
31 * Maintenance script to generate JSON i18n files from a PHP i18n file.
33 * @ingroup Maintenance
35 class GenerateJsonI18n
extends Maintenance
{
36 public function __construct() {
37 parent
::__construct();
38 $this->addDescription( 'Build JSON messages files from a PHP messages file' );
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',
44 $this->addOption( 'supplementary', 'Find supplementary i18n files in subdirs and convert those',
48 public function execute() {
51 $phpfile = $this->getArg( 0 );
52 $jsondir = $this->getArg( 1 );
53 $extension = $this->getOption( 'extension' );
54 $convertSupplementaryI18nFiles = $this->hasOption( 'supplementary' );
58 $this->error( "The phpfile is already specified, conflicts with --extension.", 1 );
60 $phpfile = "$IP/extensions/$extension/$extension.i18n.php";
64 $this->error( "I'm here for an argument!" );
65 $this->maybeHelp( true );
69 if ( $convertSupplementaryI18nFiles ) {
70 if ( is_readable( $phpfile ) ) {
71 $this->transformI18nFile( $phpfile, $jsondir );
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
76 $this->error( "Warning: no primary i18n file was found." );
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 foreach ( $iterator as $path => $fileObject ) {
83 if ( fnmatch( "*.i18n.php", $fileObject->getFilename() ) ) {
84 $this->output( "Converting $path.\n" );
85 $this->transformI18nFile( $path );
89 // Just convert the primary i18n file.
90 $this->transformI18nFile( $phpfile, $jsondir );
94 public function transformI18nFile( $phpfile, $jsondir = null ) {
96 // Assume the json directory should be in the same directory as the
98 $jsondir = dirname( $phpfile ) . "/i18n";
100 if ( !is_dir( $jsondir ) ) {
101 $this->output( "Creating directory $jsondir.\n" );
102 $success = mkdir( $jsondir );
104 $this->error( "Could not create directory $jsondir", 1 );
108 if ( !is_readable( $phpfile ) ) {
109 $this->error( "Error reading $phpfile", 1 );
113 $phpfileContents = file_get_contents( $phpfile );
115 if ( !isset( $messages ) ) {
116 $this->error( "PHP file $phpfile does not define \$messages array", 1 );
120 $this->error( "PHP file $phpfile contains an empty \$messages array. " .
121 "Maybe it was already converted?", 1 );
124 if ( !isset( $messages['en'] ) ||
!is_array( $messages['en'] ) ) {
125 $this->error( "PHP file $phpfile does not set language codes", 1 );
128 foreach ( $messages as $langcode => $langmsgs ) {
129 $authors = $this->getAuthorsFromComment( $this->findCommentBefore(
130 "\$messages['$langcode'] =",
133 // Make sure the @metadata key is the first key in the output
134 $langmsgs = array_merge(
135 [ '@metadata' => [ 'authors' => $authors ] ],
139 $jsonfile = "$jsondir/$langcode.json";
140 $success = file_put_contents(
142 FormatJson
::encode( $langmsgs, "\t", FormatJson
::ALL_OK
) . "\n"
144 if ( $success === false ) {
145 $this->error( "FAILED to write $jsonfile", 1 );
147 $this->output( "$jsonfile\n" );
151 "All done. To complete the conversion, please do the following:\n" .
152 "* Add \$wgMessagesDirs['YourExtension'] = __DIR__ . '/i18n';\n" .
153 "* Remove \$wgExtensionMessagesFiles['YourExtension']\n" .
154 "* Delete the old PHP message file\n" .
155 "This script no longer generates backward compatibility shims! If you need\n" .
156 "compatibility with MediaWiki 1.22 and older, use the MediaWiki 1.23 version\n" .
157 "of this script instead, or create a shim manually.\n"
162 * Find the documentation comment immediately before a given search string
163 * @param string $needle String to search for
164 * @param string $haystack String to search in
165 * @return string Substring of $haystack starting at '/**' ending right before $needle, or empty
167 protected function findCommentBefore( $needle, $haystack ) {
168 $needlePos = strpos( $haystack, $needle );
169 if ( $needlePos === false ) {
172 // Need to pass a negative offset to strrpos() so it'll search backwards from the
174 $startPos = strrpos( $haystack, '/**', $needlePos - strlen( $haystack ) );
175 if ( $startPos === false ) {
179 return substr( $haystack, $startPos, $needlePos - $startPos );
183 * Get an array of author names from a documentation comment containing @author declarations.
184 * @param string $comment Documentation comment
185 * @return array Array of author names (strings)
187 protected function getAuthorsFromComment( $comment ) {
189 preg_match_all( '/@author (.*?)$/m', $comment, $matches );
191 return $matches && $matches[1] ?
$matches[1] : [];
195 $maintClass = "GenerateJsonI18n";
196 require_once RUN_MAINTENANCE_IF_MAIN
;