| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Json\FormatJson; |
| 4 |
|
| 5 |
|
| 6 |
require_once __DIR__ . '/Maintenance.php'; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class ConvertExtensionsMessagesToTranslationAlias extends Maintenance { |
| 18 |
public function __construct() { |
| 19 |
parent::__construct(); |
| 20 |
$this->addDescription( 'Convert ExtensionMessagesFiles to JSON files in different language codes.' ); |
| 21 |
|
| 22 |
$this->addArg( 'destination', 'Destination folder where the JSON files should be created' ); |
| 23 |
$this->addArg( |
| 24 |
'files', 'ExtensionMessageFiles to be converted', true, true |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
public function execute() { |
| 29 |
$errors = []; |
| 30 |
$destinationFolder = $this->getArg( 0 ); |
| 31 |
if ( !is_dir( $destinationFolder ) || !is_writable( $destinationFolder ) ) { |
| 32 |
$errors[] = "The path: $destinationFolder does not exist, is not a folder or is not writable."; |
| 33 |
} |
| 34 |
|
| 35 |
$messageFiles = $this->getArgs( 1 ); |
| 36 |
foreach ( $messageFiles as $file ) { |
| 37 |
if ( !file_exists( $file ) ) { |
| 38 |
$errors[] = "The message file: $file does not exist"; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
if ( $errors ) { |
| 43 |
$this->fatalError( implode( "\n* ", $errors ) ); |
| 44 |
} |
| 45 |
|
| 46 |
$data = []; |
| 47 |
|
| 48 |
foreach ( $messageFiles as $file ) { |
| 49 |
include $file; |
| 50 |
|
| 51 |
foreach ( LocalisationCache::ALL_ALIAS_KEYS as $key ) { |
| 52 |
|
| 53 |
|
| 54 |
if ( isset( $$key ) ) { |
| 55 |
|
| 56 |
|
| 57 |
$data[$key] = $$key; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
$json = []; |
| 63 |
foreach ( $data as $key => $item ) { |
| 64 |
$normalizedKey = ucfirst( $key ); |
| 65 |
|
| 66 |
foreach ( $item as $languageCode => $itemData ) { |
| 67 |
$json[$languageCode][$normalizedKey] = $itemData; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
foreach ( $json as $languageCode => $data ) { |
| 72 |
$filePath = $destinationFolder . '/' . $languageCode . ".json"; |
| 73 |
file_put_contents( |
| 74 |
$filePath, |
| 75 |
FormatJson::encode( $data, "\t", FormatJson::UTF8_OK ) . "\n" |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
$this->output( "Done!\n" ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
$maintClass = ConvertExtensionsMessagesToTranslationAlias::class; |
| 85 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 86 |
|