MediaWiki master
convertExtensionsMessagesToTranslationAlias.php
Go to the documentation of this file.
1<?php
2
3require_once __DIR__ . '/Maintenance.php';
4
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;
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.