MediaWiki master
importExtensionMessages.php
Go to the documentation of this file.
1<?php
2
6
7// @codeCoverageIgnoreStart
8require_once __DIR__ . '/../Maintenance.php';
9// @codeCoverageIgnoreEnd
10
13 private $extensionDir;
15 private $extName;
17 private $excludedMsgs;
19 private $outDir;
21 private $coreDataCache;
22
23 public function __construct() {
24 parent::__construct();
25 $this->addArg( 'extension', 'The extension name' );
26 $this->addOption( 'outdir',
27 'The output directory, default $IP/languages/i18n', false, true );
28 }
29
30 public function execute() {
31 $this->init();
32
33 $this->outDir = $this->getOption( 'outdir', MW_INSTALL_PATH . '/languages/i18n' );
34 if ( !file_exists( $this->outDir ) ) {
35 mkdir( $this->outDir, 0777, true );
36 }
37
38 $this->extName = $this->getArg();
39 $extJsonPath = $this->extensionDir . "/{$this->extName}/extension.json";
40 $extJson = file_get_contents( $extJsonPath );
41 if ( $extJson === false ) {
42 $this->fatalError( "Unable to open \"$extJsonPath\"" );
43 }
44 $extData = json_decode( $extJson, JSON_THROW_ON_ERROR );
45
46 $this->excludedMsgs = [];
47 foreach ( [ 'namemsg', 'descriptionmsg' ] as $key ) {
48 if ( isset( $extData[$key] ) ) {
49 $this->excludedMsgs[] = $extData[$key];
50 }
51 }
52
53 foreach ( $this->getMessagesDirs( $extData ) as $dir ) {
54 $this->processDir( $dir );
55 }
56 }
57
58 private function init() {
59 $services = $this->getServiceContainer();
60 $config = $services->getMainConfig();
61 $this->extensionDir = $config->get( MainConfigNames::ExtensionDirectory );
62 }
63
64 private function getMessagesDirs( array $extData ): array {
65 if ( isset( $extData['MessagesDirs'] ) ) {
66 $messagesDirs = [];
67 foreach ( $extData['MessagesDirs'] as $dirs ) {
68 if ( is_array( $dirs ) ) {
69 foreach ( $dirs as $dir ) {
70 $messagesDirs[] = $dir;
71 }
72 } else {
73 $messagesDirs[] = $dirs;
74 }
75 }
76 } else {
77 $messagesDirs = [ 'i18n' ];
78 }
79 return $messagesDirs;
80 }
81
82 private function processDir( string $dir ) {
83 $path = $this->extensionDir . "/{$this->extName}/$dir";
84
85 foreach ( new DirectoryIterator( $path ) as $file ) {
86 if ( !$file->isDot() && str_ends_with( $file->getFilename(), '.json' ) ) {
87 $this->processFile(
88 substr( $file->getFilename(), 0, -5 ),
89 $file->getPathname()
90 );
91 }
92 }
93 }
94
95 private function processFile( string $lang, string $extI18nPath ) {
96 $extJson = file_get_contents( $extI18nPath );
97 if ( $extJson === false ) {
98 $this->error( "Unable to read i18n file \"$extI18nPath\"" );
99 return;
100 }
101 $extData = json_decode( $extJson, JSON_THROW_ON_ERROR );
102 $coreData = $this->getCoreData( $lang );
103
104 if ( isset( $extData['@metadata']['authors'] ) ) {
105 $authors = array_unique( array_merge(
106 $coreData['@metadata']['authors'] ?? [],
107 $extData['@metadata']['authors']
108 ) );
109 // Fix numeric authors
110 foreach ( $authors as &$author ) {
111 $author = (string)$author;
112 }
113 sort( $authors );
114 $coreData['@metadata']['authors'] = $authors;
115 }
116
117 foreach ( $extData as $name => $value ) {
118 if ( str_starts_with( $name, '@' ) ) {
119 continue;
120 }
121 if ( in_array( $name, $this->excludedMsgs ) ) {
122 continue;
123 }
124 $coreData[$name] = $value;
125 }
126
127 $this->setCoreData( $lang, $coreData );
128 }
129
131 private function getCoreData( string $lang ) {
132 if ( !isset( $this->coreDataCache[$lang] ) ) {
133 $corePath = MW_INSTALL_PATH . "/languages/i18n/$lang.json";
134 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
135 $coreJson = @file_get_contents( $corePath );
136 if ( $coreJson === false ) {
137 $this->error( "Warning: discarding extension localisation " .
138 "for language \"$lang\" not present in core" );
139 // Do not write to coreDataCache -- suppress creation of the core file.
140 return [];
141 }
142 $this->coreDataCache[$lang] = json_decode( $coreJson, JSON_THROW_ON_ERROR );
143 }
144 return $this->coreDataCache[$lang];
145 }
146
151 private function setCoreData( string $lang, $data ) {
152 if ( !isset( $this->coreDataCache[$lang] ) ) {
153 // Non-existent file, do not create
154 return;
155 }
156
157 $this->coreDataCache[$lang] = $data;
158 $outPath = "{$this->outDir}/$lang.json";
159 if ( !file_put_contents(
160 $outPath,
161 FormatJson::encode( $data, "\t", FormatJson::ALL_OK ) . "\n"
162 ) ) {
163 $this->error( "Unable to write core i18n file \"$outPath\"" );
164 }
165 }
166}
167
168// @codeCoverageIgnoreStart
169$maintClass = ImportExtensionMessages::class;
170require_once RUN_MAINTENANCE_IF_MAIN;
171// @codeCoverageIgnoreEnd
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:82
__construct()
Default constructor.
JSON formatter wrapper class.
A class containing constants representing the names of configuration variables.
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.