public function __construct() {
parent::__construct();
$this->mDescription = "Build JSON messages files from a PHP messages file";
- $this->addArg( 'phpfile', 'PHP file defining a $messages array', true );
- $this->addArg( 'jsondir', 'Directory to write JSON files to', true );
+
+ $this->addArg( 'phpfile', 'PHP file defining a $messages array', false );
+ $this->addArg( 'jsondir', 'Directory to write JSON files to', false );
$this->addOption( 'langcode', 'Language code; only needed for converting core i18n files',
false, true );
+ $this->addOption( 'extension', 'Perform default conversion on an extension',
+ false, true );
+ $this->addOption( 'shim-only', 'Only create or update the backward-compatibility shim' );
}
public function execute() {
+ global $IP;
+
$phpfile = $this->getArg( 0 );
$jsondir = $this->getArg( 1 );
+ $extension = $this->getOption( 'extension' );
+
+ if ( $extension ) {
+ if ( $phpfile ) {
+ $this->error( "The phpfile is already specified, conflicts with --extension.\n", 1 );
+ }
+ $phpfile = "$IP/extensions/$extension/$extension.i18n.php";
+ }
+
+ if ( !$phpfile ) {
+ $this->error( "I'm here for an argument!\n" );
+ $this->maybeHelp( true );
+ // dies.
+ }
+
+ $this->transformI18nFile( $phpfile, $jsondir );
+ }
+
+ public function transformI18nFile( $phpfile, $jsondir = null ) {
+ if ( !$jsondir ) {
+ // Assume the json directory should be in the same directory as the
+ // .i18n.php file.
+ $jsondir = dirname( $phpfile ) . "/i18n";
+ }
+ if ( !is_dir( $jsondir ) ) {
+ $this->output( "Creating directory $jsondir.\n" );
+ $success = mkdir( $jsondir );
+ if ( !$success ) {
+ $this->error( "Could not create directory $jsondir\n", 1 );
+ }
+ }
+
+ if ( $this->hasOption( 'shim-only' ) ) {
+ $this->shimOnly( $phpfile, $jsondir );
+
+ return;
+ }
+
+ if ( $jsondir === null ) {
+ $this->error( 'Argument [jsondir] is required unless --shim-only is specified.' );
+ $this->maybeHelp( true );
+ }
if ( !is_readable( $phpfile ) ) {
$this->error( "Error reading $phpfile\n", 1 );
$jsonfile = "$jsondir/$langcode.json";
$success = file_put_contents(
$jsonfile,
- FormatJson::encode( $langmsgs, true, FormatJson::ALL_OK )
+ FormatJson::encode( $langmsgs, "\t", FormatJson::ALL_OK ) . "\n"
);
if ( $success === false ) {
$this->error( "FAILED to write $jsonfile", 1 );
$this->output( "Also add \$wgMessagesDirs['YourExtension'] = __DIR__ . '/i18n';\n" );
}
+ protected function shimOnly( $phpfile, $jsondir ) {
+ if ( file_exists( $phpfile ) ) {
+ if ( !is_readable( $phpfile ) ) {
+ $this->error( "Error reading $phpfile\n", 1 );
+ }
+
+ $phpfileContents = file_get_contents( $phpfile );
+ $m = array();
+ if ( !preg_match( '!"/([^"$]+)/\$csCode.json";!', $phpfileContents, $m ) ) {
+ $this->error( "Cannot recognize $phpfile as a shim.\n", 1 );
+ }
+
+ if ( $jsondir === null ) {
+ $jsondir = $m[1];
+ }
+
+ $this->output( "Updating existing shim $phpfile\n" );
+ } elseif ( $jsondir === null ) {
+ $this->error( "$phpfile does not exist.\n" .
+ "Argument [jsondir] is required in order to create a new shim.\n", 1 );
+ } else {
+ $this->output( "Creating new shim $phpfile\n" );
+ }
+
+ $shim = $this->doShim( $jsondir );
+ file_put_contents( $phpfile, $shim );
+ $this->output( "All done.\n" );
+ }
+
protected function doShim( $jsondir ) {
$shim = <<<'PHP'
<?php
+/**
+ * This is a backwards-compatibility shim, generated by:
+ * https://git.wikimedia.org/blob/mediawiki%2Fcore.git/HEAD/maintenance%2FgenerateJsonI18n.php
+ *
+ * Beginning with MediaWiki 1.23, translation strings are stored in json files,
+ * and the EXTENSION.i18n.php file only exists to provide compatibility with
+ * older releases of MediaWiki. For more information about this migration, see:
+ * https://www.mediawiki.org/wiki/Requests_for_comment/Localisation_format
+ *
+ * This shim maintains compatibility back to MediaWiki 1.17.
+ */
$messages = array();
-$GLOBALS['wgHooks']['LocalisationCacheRecache'][] = function ( $cache, $code, &$cachedData ) {
- $codeSequence = array_merge( array( $code ), $cachedData['fallbackSequence'] );
- foreach ( $codeSequence as $csCode ) {
- $fileName = __DIR__ . "/{{OUT}}/$csCode.json";
- if ( is_readable( $fileName ) ) {
- $data = FormatJson::decode( file_get_contents( $fileName ), true );
- foreach ( array_keys( $data ) as $key ) {
- if ( $key === '' || $key[0] === '@' ) {
- unset( $data[$key] );
+if ( !function_exists( '{{FUNC}}' ) ) {
+ function {{FUNC}}( $cache, $code, &$cachedData ) {
+ $codeSequence = array_merge( array( $code ), $cachedData['fallbackSequence'] );
+ foreach ( $codeSequence as $csCode ) {
+ $fileName = dirname( __FILE__ ) . "/{{OUT}}/$csCode.json";
+ if ( is_readable( $fileName ) ) {
+ $data = FormatJson::decode( file_get_contents( $fileName ), true );
+ foreach ( array_keys( $data ) as $key ) {
+ if ( $key === '' || $key[0] === '@' ) {
+ unset( $data[$key] );
+ }
}
+ $cachedData['messages'] = array_merge( $data, $cachedData['messages'] );
}
- $cachedData['messages'] = array_merge( $data, $cachedData['messages'] );
- }
- $cachedData['deps'][] = new FileDependency( $fileName );
+ $cachedData['deps'][] = new FileDependency( $fileName );
+ }
+ return true;
}
- return true;
-};
+
+ $GLOBALS['wgHooks']['LocalisationCacheRecache'][] = '{{FUNC}}';
+}
PHP;
$jsondir = str_replace( '\\', '/', $jsondir );
$shim = str_replace( '{{OUT}}', $jsondir, $shim );
+ $shim = str_replace( '{{FUNC}}', 'wfJsonI18nShim' . wfRandomString( 16 ), $shim );
+
return $shim;
}
/**
* Get an array of author names from a documentation comment containing @author declarations.
* @param string $comment Documentation comment
- * @return Array of author names (strings)
+ * @return array Array of author names (strings)
*/
protected function getAuthorsFromComment( $comment ) {
$matches = null;
preg_match_all( '/@author (.*?)$/m', $comment, $matches );
+
return $matches && $matches[1] ? $matches[1] : array();
}
}