Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / maintenance / convertExtensionsMessagesToTranslationAlias.php
blob83d057e90ff22f7adf644e64067e7701653a7648
1 <?php
3 use MediaWiki\Json\FormatJson;
4 use MediaWiki\Maintenance\Maintenance;
6 // @codeCoverageIgnoreStart
7 require_once __DIR__ . '/Maintenance.php';
8 // @codeCoverageIgnoreEnd
10 /**
11 * Convert existing ExtensionMessagesFiles to JSON files in different language codes that can be used as
12 * input for TranslationAliasesDirs configuration.
14 * @since 1.42
15 * @ingroup Maintenance
18 class ConvertExtensionsMessagesToTranslationAlias extends Maintenance {
19 public function __construct() {
20 parent::__construct();
21 $this->addDescription( 'Convert ExtensionMessagesFiles to JSON files in different language codes.' );
23 $this->addArg( 'destination', 'Destination folder where the JSON files should be created' );
24 $this->addArg(
25 'files', 'ExtensionMessageFiles to be converted', true, true
29 public function execute() {
30 $errors = [];
31 $destinationFolder = $this->getArg( 0 );
32 if ( !is_dir( $destinationFolder ) || !is_writable( $destinationFolder ) ) {
33 $errors[] = "The path: $destinationFolder does not exist, is not a folder or is not writable.";
36 $messageFiles = $this->getArgs( 1 );
37 foreach ( $messageFiles as $file ) {
38 if ( !file_exists( $file ) ) {
39 $errors[] = "The message file: $file does not exist";
43 if ( $errors ) {
44 $this->fatalError( implode( "\n* ", $errors ) );
47 $data = [];
49 foreach ( $messageFiles as $file ) {
50 include $file;
52 foreach ( LocalisationCache::ALL_ALIAS_KEYS as $key ) {
53 // Can be removed once LocalisationCache::ALL_ALIAS_KEYS has multiple entries
54 // @phan-suppress-next-line PhanImpossibleConditionInLoop False positive
55 if ( isset( $$key ) ) {
56 // Can be removed once LocalisationCache::ALL_ALIAS_KEYS has multiple entries
57 // @phan-suppress-next-line PhanUndeclaredVariable Possibly declared in $file
58 $data[$key] = $$key;
63 $json = [];
64 foreach ( $data as $key => $item ) {
65 $normalizedKey = ucfirst( $key );
66 // @phan-suppress-next-line PhanTypeMismatchForeach False positive
67 foreach ( $item as $languageCode => $itemData ) {
68 $json[$languageCode][$normalizedKey] = $itemData;
72 foreach ( $json as $languageCode => $data ) {
73 $filePath = $destinationFolder . '/' . $languageCode . ".json";
74 file_put_contents(
75 $filePath,
76 FormatJson::encode( $data, "\t", FormatJson::UTF8_OK ) . "\n"
80 $this->output( "Done!\n" );
84 // @codeCoverageIgnoreStart
85 $maintClass = ConvertExtensionsMessagesToTranslationAlias::class;
86 require_once RUN_MAINTENANCE_IF_MAIN;
87 // @codeCoverageIgnoreEnd