MediaWiki master
convertExtensionsMessagesToTranslationAlias.php
Go to the documentation of this file.
1<?php
2
4
5require_once __DIR__ . '/Maintenance.php';
6
16 public function __construct() {
17 parent::__construct();
18 $this->addDescription( 'Convert ExtensionMessagesFiles to JSON files in different language codes.' );
19
20 $this->addArg( 'destination', 'Destination folder where the JSON files should be created' );
21 $this->addArg(
22 'files', 'ExtensionMessageFiles to be converted', true, true
23 );
24 }
25
26 public function execute() {
27 $errors = [];
28 $destinationFolder = $this->getArg( 0 );
29 if ( !is_dir( $destinationFolder ) || !is_writable( $destinationFolder ) ) {
30 $errors[] = "The path: $destinationFolder does not exist, is not a folder or is not writable.";
31 }
32
33 $messageFiles = $this->getArgs( 1 );
34 foreach ( $messageFiles as $file ) {
35 if ( !file_exists( $file ) ) {
36 $errors[] = "The message file: $file does not exist";
37 }
38 }
39
40 if ( $errors ) {
41 $this->fatalError( implode( "\n* ", $errors ) );
42 }
43
44 $data = [];
45
46 foreach ( $messageFiles as $file ) {
47 include $file;
48
49 foreach ( LocalisationCache::ALL_ALIAS_KEYS as $key ) {
50 // Can be removed once LocalisationCache::ALL_ALIAS_KEYS has multiple entries
51 // @phan-suppress-next-line PhanImpossibleConditionInLoop False positive
52 if ( isset( $$key ) ) {
53 // Can be removed once LocalisationCache::ALL_ALIAS_KEYS has multiple entries
54 // @phan-suppress-next-line PhanUndeclaredVariable Possibly declared in $file
55 $data[$key] = $$key;
56 }
57 }
58 }
59
60 $json = [];
61 foreach ( $data as $key => $item ) {
62 $normalizedKey = ucfirst( $key );
63 // @phan-suppress-next-line PhanTypeMismatchForeach False positive
64 foreach ( $item as $languageCode => $itemData ) {
65 $json[$languageCode][$normalizedKey] = $itemData;
66 }
67 }
68
69 foreach ( $json as $languageCode => $data ) {
70 $filePath = $destinationFolder . '/' . $languageCode . ".json";
71 file_put_contents(
72 $filePath,
73 FormatJson::encode( $data, "\t", FormatJson::UTF8_OK ) . "\n"
74 );
75 }
76
77 $this->output( "Done!\n" );
78 }
79}
80
81$maintClass = ConvertExtensionsMessagesToTranslationAlias::class;
82require_once RUN_MAINTENANCE_IF_MAIN;
Convert existing ExtensionMessagesFiles to JSON files in different language codes that can be used as...
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.
getArgs( $offset=0)
Get arguments.
output( $out, $channel=null)
Throw some output to the user.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
JSON formatter wrapper class.