Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
FileBasedMessageGroupFactory.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupConfiguration;
5
6use FileDependency;
7use MainConfigDependency;
8use MediaWiki\Config\ServiceOptions;
11use Wikimedia\Rdbms\IReadableDatabase;
12
20 public const SERVICE_OPTIONS = [ 'TranslateGroupFiles' ];
21
22 private MessageGroupConfigurationParser $messageGroupConfigurationParser;
24 private array $groupFiles;
25
26 public function __construct(
27 MessageGroupConfigurationParser $messageGroupConfigurationParser,
28 ServiceOptions $serviceOptions
29 ) {
30 $this->messageGroupConfigurationParser = $messageGroupConfigurationParser;
31 $serviceOptions->assertRequiredOptions( self::SERVICE_OPTIONS );
32 $this->groupFiles = $serviceOptions->get( 'TranslateGroupFiles' );
33 }
34
35 public function getCacheKey(): string {
36 return 'file-based-groups';
37 }
38
39 public function getCacheVersion(): int {
40 return 1;
41 }
42
43 public function getDependencies(): array {
44 $deps = [ new MainConfigDependency( 'TranslateGroupFiles' ) ];
45
46 foreach ( $this->groupFiles as $configFile ) {
47 $deps[] = new FileDependency( realpath( $configFile ) );
48 }
49
50 return $deps;
51 }
52
53 public function getData( IReadableDatabase $db ): array {
54 $autoload = $groups = $value = [];
55 $parser = $this->messageGroupConfigurationParser;
56 foreach ( $this->groupFiles as $configFile ) {
57 $yaml = file_get_contents( $configFile );
58 $parsedData = $parser->getHopefullyValidConfigurations(
59 $yaml,
60 static function ( $index, $config, $error ) use ( $configFile ) {
61 trigger_error( "Document $index in $configFile is invalid: $error", E_USER_WARNING );
62 }
63 );
64
65 foreach ( $parsedData as $id => $conf ) {
66 if ( !empty( $conf['AUTOLOAD'] ) && is_array( $conf['AUTOLOAD'] ) ) {
67 $dir = dirname( $configFile );
68 $additions = array_map( static function ( $file ) use ( $dir ) {
69 return "$dir/$file";
70 }, $conf['AUTOLOAD'] );
71 self::appendAutoloader( $additions, $autoload );
72 }
73
74 $groups[$id] = $conf;
75 }
76 }
77 $value['groups'] = $groups;
78 $value['autoload'] = $autoload;
79
80 return $value;
81 }
82
83 public function createGroups( $data ): array {
84 global $wgAutoloadClasses;
85 self::appendAutoloader( $data['autoload'], $wgAutoloadClasses );
86
87 $groups = [];
88 foreach ( $data['groups'] as $id => $conf ) {
89 $groups[$id] = MessageGroupBase::factory( $conf );
90 }
91
92 return $groups;
93 }
94
101 private static function appendAutoloader( array $additions, array &$to ) {
102 foreach ( $additions as $class => $file ) {
103 if ( isset( $to[$class] ) && $to[$class] !== $file ) {
104 $msg = "Autoload conflict for $class: $to[$class] !== $file";
105 trigger_error( $msg, E_USER_WARNING );
106 continue;
107 }
108
109 $to[$class] = $file;
110 }
111 }
112}
This class implements some basic functions that wrap around the YAML message group configurations.