Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageBundleMessageGroup.php
1<?php
2
3declare( strict_types = 1 );
4
5namespace MediaWiki\Extension\Translate\MessageBundleTranslation;
6
7use IContextSource;
8use LogicException;
14use MediaWiki\MediaWikiServices;
15use MediaWiki\Revision\SlotRecord;
16use MessageGroup;
18use Title;
19use const NS_TRANSLATIONS;
20
28 private string $name;
29 private string $groupId;
30 private int $pageId;
31 private int $revisionId;
32 private ?array $data = null;
33 private ?string $description;
34 private ?string $label;
35
36 public function __construct(
37 string $groupId,
38 string $name,
39 int $pageId,
40 int $revisionId,
41 ?string $description,
42 ?string $label
43 ) {
44 $this->groupId = $groupId;
45 $this->name = $name;
46 $this->pageId = $pageId;
47 $this->revisionId = $revisionId;
48 $this->description = $description;
49 $this->label = $label;
50 }
51
53 public static function getGroupId( string $name ): string {
54 return "messagebundle-$name";
55 }
56
57 public function getBundlePageId(): int {
58 return $this->pageId;
59 }
60
61 private function getData(): array {
62 if ( isset( $this->data ) ) {
63 return $this->data;
64 }
65
66 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
67 $revision = $revisionStore->getRevisionById( $this->revisionId );
68
69 if ( $revision === null ) {
70 throw new LogicException( "Could not find revision id $this->revisionId" );
71 }
72
73 $content = $revision->getContent( SlotRecord::MAIN );
74 if ( !$content instanceof MessageBundleContent ) {
75 throw new LogicException(
76 "Content with revision id $this->revisionId has wrong content format"
77 );
78 }
79
80 $data = json_decode( $content->getText(), true );
81 if ( !$data ) {
82 throw new LogicException(
83 "Content with revision id $this->revisionId is not valid JSON"
84 );
85 }
86
87 $this->data = $data;
88 return $this->data;
89 }
90
91 private function makeGroupKeys( array $keys ): array {
92 $result = [];
93 foreach ( $keys as $key ) {
94 $result[] = str_replace( ' ', '_', "$this->name/$key" );
95 }
96 return $result;
97 }
98
100 public function getId(): string {
101 return $this->groupId;
102 }
103
105 public function getLabel( IContextSource $context = null ): string {
106 return $this->label ?? $this->name;
107 }
108
110 public function getDescription( IContextSource $context = null ): string {
111 $titleText = Title::newFromID( $this->pageId )->getPrefixedText();
112 $linkTargetText = ":$titleText";
113 if ( $context ) {
114 $message = $context->msg( 'translate-messagebundle-group-description' );
115 } else {
116 $message = wfMessage( 'translate-messagebundle-group-description' )
117 ->inContentLanguage();
118 }
119
120 $plainMessage = $message->params( $titleText, $linkTargetText )->plain();
121
122 if ( $this->description === null ) {
123 return $plainMessage;
124 }
125
126 return $plainMessage . ' ' . $this->description;
127 }
128
130 public function getIcon(): ?string {
131 return null;
132 }
133
135 public function getNamespace(): int {
136 return NS_TRANSLATIONS;
137 }
138
140 public function isMeta(): bool {
141 return false;
142 }
143
145 public function exists(): bool {
146 return true;
147 }
148
150 public function getValidator(): ?ValidationRunner {
151 return null;
152 }
153
155 public function getMangler(): ?StringMatcher {
156 return null;
157 }
158
160 public function initCollection( $code ): MessageCollection {
161 $defs = new MessageDefinitions( $this->getDefinitions(), $this->getNamespace() );
162 $collection = MessageCollection::newFromDefinitions( $defs, $code );
163
164 foreach ( $this->getTags() as $type => $tags ) {
165 $collection->setTags( $type, $tags );
166 }
167
168 return $collection;
169 }
170
172 public function load( $code ): array {
173 return [];
174 }
175
177 public function getDefinitions(): array {
178 $data = $this->getData();
179 unset( $data['@metadata'] );
180
181 return array_combine(
182 $this->makeGroupKeys( array_keys( $data ) ),
183 array_values( $data )
184 );
185 }
186
188 public function getKeys(): array {
189 return array_keys( $this->getDefinitions() );
190 }
191
193 public function getTags( $type = null ): array {
194 return [];
195 }
196
198 public function getMessage( $key, $code ): ?string {
199 if ( $code === $this->getSourceLanguage() ) {
200 return $this->getDefinitions()[$key] ?? null;
201 }
202
203 return null;
204 }
205
207 public function getSourceLanguage(): string {
208 return Title::newFromText( $this->name )->getPageLanguage()->getCode();
209 }
210
213 global $wgTranslateWorkflowStates;
214 $conf = $wgTranslateWorkflowStates ?: [];
215
216 Services::getInstance()->getHookRunner()
217 ->onTranslate_modifyMessageGroupStates( $this->getId(), $conf );
218
219 return new MessageGroupStates( $conf );
220 }
221
223 public function getTranslatableLanguages(): ?array {
224 return null;
225 }
226
228 public function getSupportConfig(): ?array {
229 return null;
230 }
231}
return[ 'Translate:ConfigHelper'=> static function():ConfigHelper { return new ConfigHelper();}, 'Translate:CsvTranslationImporter'=> static function(MediaWikiServices $services):CsvTranslationImporter { return new CsvTranslationImporter( $services->getWikiPageFactory());}, 'Translate:EntitySearch'=> static function(MediaWikiServices $services):EntitySearch { return new EntitySearch($services->getMainWANObjectCache(), $services->getCollationFactory() ->makeCollation( 'uca-default-u-kn'), MessageGroups::singleton(), $services->getNamespaceInfo(), $services->get( 'Translate:MessageIndex'), $services->getTitleParser(), $services->getTitleFormatter());}, 'Translate:ExternalMessageSourceStateImporter'=> static function(MediaWikiServices $services):ExternalMessageSourceStateImporter { return new ExternalMessageSourceStateImporter($services->getMainConfig(), $services->get( 'Translate:GroupSynchronizationCache'), $services->getJobQueueGroup(), LoggerFactory::getInstance( 'Translate.GroupSynchronization'), $services->get( 'Translate:MessageIndex'));}, 'Translate:FileFormatFactory'=> static function(MediaWikiServices $services):FileFormatFactory { return new FileFormatFactory( $services->getObjectFactory());}, 'Translate:GroupSynchronizationCache'=> static function(MediaWikiServices $services):GroupSynchronizationCache { return new GroupSynchronizationCache( $services->get( 'Translate:PersistentCache'));}, 'Translate:HookRunner'=> static function(MediaWikiServices $services):HookRunner { return new HookRunner( $services->getHookContainer());}, 'Translate:MessageBundleStore'=> static function(MediaWikiServices $services):MessageBundleStore { return new MessageBundleStore($services->get( 'Translate:RevTagStore'), $services->getJobQueueGroup(), $services->getLanguageNameUtils(), $services->get( 'Translate:MessageIndex'));}, 'Translate:MessageGroupReviewStore'=> static function(MediaWikiServices $services):MessageGroupReviewStore { return new MessageGroupReviewStore($services->getDBLoadBalancer(), $services->get( 'Translate:HookRunner'));}, 'Translate:MessageGroupStatsTableFactory'=> static function(MediaWikiServices $services):MessageGroupStatsTableFactory { return new MessageGroupStatsTableFactory($services->get( 'Translate:ProgressStatsTableFactory'), $services->getDBLoadBalancer(), $services->getLinkRenderer(), $services->get( 'Translate:MessageGroupReviewStore'), $services->getMainConfig() ->get( 'TranslateWorkflowStates') !==false);}, 'Translate:MessageIndex'=> static function(MediaWikiServices $services):MessageIndex { $params=$services->getMainConfig() ->get( 'TranslateMessageIndex');if(is_string( $params)) { $params=(array) $params;} $class=array_shift( $params);return new $class( $params);}, 'Translate:MessagePrefixStats'=> static function(MediaWikiServices $services):MessagePrefixStats { return new MessagePrefixStats( $services->getTitleParser());}, 'Translate:ParsingPlaceholderFactory'=> static function():ParsingPlaceholderFactory { return new ParsingPlaceholderFactory();}, 'Translate:PersistentCache'=> static function(MediaWikiServices $services):PersistentCache { return new PersistentDatabaseCache($services->getDBLoadBalancer(), $services->getJsonCodec());}, 'Translate:ProgressStatsTableFactory'=> static function(MediaWikiServices $services):ProgressStatsTableFactory { return new ProgressStatsTableFactory($services->getLinkRenderer(), $services->get( 'Translate:ConfigHelper'));}, 'Translate:RevTagStore'=> static function(MediaWikiServices $services):RevTagStore { return new RevTagStore($services->getDBLoadBalancerFactory());}, 'Translate:SubpageListBuilder'=> static function(MediaWikiServices $services):SubpageListBuilder { return new SubpageListBuilder($services->get( 'Translate:TranslatableBundleFactory'), $services->getLinkBatchFactory());}, 'Translate:TranslatableBundleExporter'=> static function(MediaWikiServices $services):TranslatableBundleExporter { return new TranslatableBundleExporter($services->get( 'Translate:SubpageListBuilder'), $services->getWikiExporterFactory(), $services->getDBLoadBalancer());}, 'Translate:TranslatableBundleFactory'=> static function(MediaWikiServices $services):TranslatableBundleFactory { return new TranslatableBundleFactory($services->get( 'Translate:TranslatablePageStore'), $services->get( 'Translate:MessageBundleStore'));}, 'Translate:TranslatableBundleImporter'=> static function(MediaWikiServices $services):TranslatableBundleImporter { return new TranslatableBundleImporter($services->getWikiImporterFactory(), $services->get( 'Translate:TranslatablePageParser'), $services->getRevisionLookup());}, 'Translate:TranslatableBundleMover'=> static function(MediaWikiServices $services):TranslatableBundleMover { return new TranslatableBundleMover($services->getMovePageFactory(), $services->getJobQueueGroup(), $services->getLinkBatchFactory(), $services->get( 'Translate:TranslatableBundleFactory'), $services->get( 'Translate:SubpageListBuilder'), $services->getMainConfig() ->get( 'TranslatePageMoveLimit'));}, 'Translate:TranslatableBundleStatusStore'=> static function(MediaWikiServices $services):TranslatableBundleStatusStore { return new TranslatableBundleStatusStore($services->getDBLoadBalancer() ->getConnection(DB_PRIMARY), $services->getCollationFactory() ->makeCollation( 'uca-default-u-kn'), $services->getDBLoadBalancer() ->getMaintenanceConnectionRef(DB_PRIMARY));}, 'Translate:TranslatablePageParser'=> static function(MediaWikiServices $services):TranslatablePageParser { return new TranslatablePageParser($services->get( 'Translate:ParsingPlaceholderFactory'));}, 'Translate:TranslatablePageStore'=> static function(MediaWikiServices $services):TranslatablePageStore { return new TranslatablePageStore($services->get( 'Translate:MessageIndex'), $services->getJobQueueGroup(), $services->get( 'Translate:RevTagStore'), $services->getDBLoadBalancer(), $services->get( 'Translate:TranslatableBundleStatusStore'), $services->get( 'Translate:TranslatablePageParser'),);}, 'Translate:TranslationStashReader'=> static function(MediaWikiServices $services):TranslationStashReader { $db=$services->getDBLoadBalancer() ->getConnection(DB_REPLICA);return new TranslationStashStorage( $db);}, 'Translate:TranslationStatsDataProvider'=> static function(MediaWikiServices $services):TranslationStatsDataProvider { return new TranslationStatsDataProvider(new ServiceOptions(TranslationStatsDataProvider::CONSTRUCTOR_OPTIONS, $services->getMainConfig()), $services->getObjectFactory(), $services->getDBLoadBalancer());}, 'Translate:TranslationUnitStoreFactory'=> static function(MediaWikiServices $services):TranslationUnitStoreFactory { return new TranslationUnitStoreFactory( $services->getDBLoadBalancer());}, 'Translate:TranslatorActivity'=> static function(MediaWikiServices $services):TranslatorActivity { $query=new TranslatorActivityQuery($services->getMainConfig(), $services->getDBLoadBalancer());return new TranslatorActivity($services->getMainObjectStash(), $query, $services->getJobQueueGroup());}, 'Translate:TtmServerFactory'=> static function(MediaWikiServices $services):TtmServerFactory { $config=$services->getMainConfig();$default=$config->get( 'TranslateTranslationDefaultService');if( $default===false) { $default=null;} return new TtmServerFactory( $config->get( 'TranslateTranslationServices'), $default);}]
@phpcs-require-sorted-array
This file contains the class for core message collections implementation.
Wrapper for message definitions, just to beauty the code.
The versatile default implementation of StringMangler interface.
Minimal service container.
Definition Services.php:44
Message validator is used to run validators to find common mistakes so that translators can fix them ...
Class for making the use of message group state easier.
Interface for message groups.