Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
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
3require_once __DIR__ . '/Maintenance.php';
4
5/**
6 * Convert existing ExtensionMessagesFiles to JSON files in different language codes that can be used as
7 * input for TranslationAliasesDirs configuration.
8 *
9 * @since 1.42
10 * @ingroup Maintenance
11 */
12
13class ConvertExtensionsMessagesToTranslationAlias extends Maintenance {
14    public function __construct() {
15        parent::__construct();
16        $this->addDescription( 'Convert ExtensionMessagesFiles to JSON files in different language codes.' );
17
18        $this->addArg( 'destination', 'Destination folder where the JSON files should be created' );
19        $this->addArg(
20            'files', 'ExtensionMessageFiles to be converted', true, true
21        );
22    }
23
24    public function execute() {
25        $errors = [];
26        $destinationFolder = $this->getArg( 0 );
27        if ( !is_dir( $destinationFolder ) || !is_writable( $destinationFolder ) ) {
28            $errors[] = "The path: $destinationFolder does not exist, is not a folder or is not writable.";
29        }
30
31        $messageFiles = $this->getArgs( 1 );
32        foreach ( $messageFiles as $file ) {
33            if ( !file_exists( $file ) ) {
34                $errors[] = "The message file: $file does not exist";
35            }
36        }
37
38        if ( $errors ) {
39            $this->fatalError( implode( "\n* ", $errors ) );
40        }
41
42        $data = [];
43
44        foreach ( $messageFiles as $file ) {
45            include $file;
46
47            foreach ( LocalisationCache::ALL_ALIAS_KEYS as $key ) {
48                // Can be removed once LocalisationCache::ALL_ALIAS_KEYS has multiple entries
49                // @phan-suppress-next-line PhanImpossibleConditionInLoop False positive
50                if ( isset( $$key ) ) {
51                    // Can be removed once LocalisationCache::ALL_ALIAS_KEYS has multiple entries
52                    // @phan-suppress-next-line PhanUndeclaredVariable Possibly declared in $file
53                    $data[$key] = $$key;
54                }
55            }
56        }
57
58        $json = [];
59        foreach ( $data as $key => $item ) {
60            $normalizedKey = ucfirst( $key );
61            // @phan-suppress-next-line PhanTypeMismatchForeach False positive
62            foreach ( $item as $languageCode => $itemData ) {
63                $json[$languageCode][$normalizedKey] = $itemData;
64            }
65        }
66
67        foreach ( $json as $languageCode => $data ) {
68            $filePath = $destinationFolder . '/' . $languageCode . ".json";
69            file_put_contents(
70                $filePath,
71                FormatJson::encode( $data, "\t", FormatJson::UTF8_OK ) . "\n"
72            );
73        }
74
75        $this->output( "Done!\n" );
76    }
77}
78
79$maintClass = ConvertExtensionsMessagesToTranslationAlias::class;
80require_once RUN_MAINTENANCE_IF_MAIN;