MediaWiki master
convertExtensionsMessagesToTranslationAlias.php
Go to the documentation of this file.
1<?php
2
4
5// @codeCoverageIgnoreStart
6require_once __DIR__ . '/Maintenance.php';
7// @codeCoverageIgnoreEnd
8
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 // Can be removed once LocalisationCache::ALL_ALIAS_KEYS has multiple entries
53 // @phan-suppress-next-line PhanImpossibleConditionInLoop False positive
54 if ( isset( $$key ) ) {
55 // Can be removed once LocalisationCache::ALL_ALIAS_KEYS has multiple entries
56 // @phan-suppress-next-line PhanUndeclaredVariable Possibly declared in $file
57 $data[$key] = $$key;
58 }
59 }
60 }
61
62 $json = [];
63 foreach ( $data as $key => $item ) {
64 $normalizedKey = ucfirst( $key );
65 // @phan-suppress-next-line PhanTypeMismatchForeach False positive
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// @codeCoverageIgnoreStart
84$maintClass = ConvertExtensionsMessagesToTranslationAlias::class;
85require_once RUN_MAINTENANCE_IF_MAIN;
86// @codeCoverageIgnoreEnd
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.