MediaWiki master
convertExtensionsMessagesToTranslationAlias.php
Go to the documentation of this file.
1<?php
2
5
6// @codeCoverageIgnoreStart
7require_once __DIR__ . '/Maintenance.php';
8// @codeCoverageIgnoreEnd
9
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
Convert existing ExtensionMessagesFiles to JSON files in different language codes that can be used as...
JSON formatter wrapper class.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
getArgs( $offset=0)
Get arguments.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addDescription( $text)
Set the description text.