Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConvertExtensionsMessagesToTranslationAlias
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 2
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
156
1<?php
2
3use MediaWiki\Json\FormatJson;
4use MediaWiki\Maintenance\Maintenance;
5
6// @codeCoverageIgnoreStart
7require_once __DIR__ . '/Maintenance.php';
8// @codeCoverageIgnoreEnd
9
10/**
11 * Convert existing ExtensionMessagesFiles to JSON files in different language codes that can be used as
12 * input for TranslationAliasesDirs configuration.
13 *
14 * @since 1.42
15 * @ingroup Maintenance
16 */
17
18class ConvertExtensionsMessagesToTranslationAlias extends Maintenance {
19    public function __construct() {
20        parent::__construct();
21        $this->addDescription( 'Convert ExtensionMessagesFiles to JSON files in different language codes.' );
22
23        $this->addArg( 'destination', 'Destination folder where the JSON files should be created' );
24        $this->addArg(
25            'files', 'ExtensionMessageFiles to be converted', true, true
26        );
27    }
28
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.";
34        }
35
36        $messageFiles = $this->getArgs( 1 );
37        foreach ( $messageFiles as $file ) {
38            if ( !file_exists( $file ) ) {
39                $errors[] = "The message file: $file does not exist";
40            }
41        }
42
43        if ( $errors ) {
44            $this->fatalError( implode( "\n* ", $errors ) );
45        }
46
47        $data = [];
48
49        foreach ( $messageFiles as $file ) {
50            include $file;
51
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;
59                }
60            }
61        }
62
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;
69            }
70        }
71
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"
77            );
78        }
79
80        $this->output( "Done!\n" );
81    }
82}
83
84// @codeCoverageIgnoreStart
85$maintClass = ConvertExtensionsMessagesToTranslationAlias::class;
86require_once RUN_MAINTENANCE_IF_MAIN;
87// @codeCoverageIgnoreEnd