10 private $excludedMsgs;
12 private $coreDataCache;
15 parent::__construct();
16 $this->
addArg(
'extension',
'The extension name' );
18 'The output directory, default $IP/languages/i18n',
false,
true );
24 $this->outDir = $this->
getOption(
'outdir', MW_INSTALL_PATH .
'/languages/i18n' );
25 if ( !file_exists( $this->outDir ) ) {
26 mkdir( $this->outDir, 0777,
true );
29 $this->extName = $this->
getArg();
30 $extJsonPath = $this->extensionDir .
"/{$this->extName}/extension.json";
31 $extJson = file_get_contents( $extJsonPath );
32 if ( $extJson ===
false ) {
33 $this->
fatalError(
"Unable to open \"$extJsonPath\"" );
35 $extData = json_decode( $extJson, JSON_THROW_ON_ERROR );
37 $this->excludedMsgs = [];
38 foreach ( [
'namemsg',
'descriptionmsg' ] as $key ) {
39 if ( isset( $extData[$key] ) ) {
40 $this->excludedMsgs[] = $extData[$key];
44 foreach ( $this->getMessagesDirs( $extData ) as $dir ) {
45 $this->processDir( $dir );
49 private function init() {
51 $config = $services->getMainConfig();
52 $this->extensionDir = $config->get( MainConfigNames::ExtensionDirectory );
55 private function getMessagesDirs( $extData ) {
56 if ( isset( $extData[
'MessagesDirs'] ) ) {
58 foreach ( $extData[
'MessagesDirs'] as $dirs ) {
59 if ( is_array( $dirs ) ) {
60 foreach ( $dirs as $dir ) {
61 $messagesDirs[] = $dir;
64 $messagesDirs[] = $dirs;
68 $messagesDirs = [
'i18n' ];
73 private function processDir( $dir ) {
74 $path = $this->extensionDir .
"/{$this->extName}/$dir";
76 foreach (
new DirectoryIterator(
$path ) as $file ) {
77 if ( !$file->isDot() && str_ends_with( $file->getFilename(),
'.json' ) ) {
79 substr( $file->getFilename(), 0, -5 ),
86 private function processFile( $lang, $extI18nPath ) {
87 $extJson = file_get_contents( $extI18nPath );
88 if ( $extJson ===
false ) {
89 $this->
error(
"Unable to read i18n file \"$extI18nPath\"" );
92 $extData = json_decode( $extJson, JSON_THROW_ON_ERROR );
93 $coreData = $this->getCoreData( $lang );
95 if ( isset( $extData[
'@metadata'][
'authors'] ) ) {
96 $authors = array_unique( array_merge(
97 $coreData[
'@metadata'][
'authors'] ?? [],
98 $extData[
'@metadata'][
'authors']
101 foreach ( $authors as &$author ) {
102 $author = (string)$author;
105 $coreData[
'@metadata'][
'authors'] = $authors;
108 foreach ( $extData as $name => $value ) {
109 if ( str_starts_with( $name,
'@' ) ) {
112 if ( in_array( $name, $this->excludedMsgs ) ) {
115 $coreData[$name] = $value;
118 $this->setCoreData( $lang, $coreData );
121 private function getCoreData( $lang ) {
122 if ( !isset( $this->coreDataCache[$lang] ) ) {
123 $corePath = MW_INSTALL_PATH .
"/languages/i18n/$lang.json";
125 $coreJson = @file_get_contents( $corePath );
126 if ( $coreJson ===
false ) {
127 $this->
error(
"Warning: discarding extension localisation " .
128 "for language \"$lang\" not present in core" );
132 $this->coreDataCache[$lang] = json_decode( $coreJson, JSON_THROW_ON_ERROR );
134 return $this->coreDataCache[$lang];
137 private function setCoreData( $lang, $data ) {
138 if ( !isset( $this->coreDataCache[$lang] ) ) {
143 $this->coreDataCache[$lang] = $data;
144 $outPath =
"{$this->outDir}/$lang.json";
145 if ( !file_put_contents(
147 FormatJson::encode( $data,
"\t", FormatJson::ALL_OK ) .
"\n"
149 $this->
error(
"Unable to write core i18n file \"$outPath\"" );
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getServiceContainer()
Returns the main service container.
getArg( $argId=0, $default=null)
Get an argument.
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.