3 use MediaWiki\Json\FormatJson
;
4 use MediaWiki\Maintenance\Maintenance
;
6 // @codeCoverageIgnoreStart
7 require_once __DIR__
. '/Maintenance.php';
8 // @codeCoverageIgnoreEnd
11 * Convert existing ExtensionMessagesFiles to JSON files in different language codes that can be used as
12 * input for TranslationAliasesDirs configuration.
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' );
25 'files', 'ExtensionMessageFiles to be converted', true, true
29 public function execute() {
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";
44 $this->fatalError( implode( "\n* ", $errors ) );
49 foreach ( $messageFiles as $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
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";
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