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 Hooks as MediaWikiHooks;
8use IContextSource;
9use LogicException;
14use MediaWiki\MediaWikiServices;
15use MediaWiki\Revision\SlotRecord;
16use MessageGroup;
18use Title;
19use const NS_TRANSLATIONS;
20
28 private $name;
30 private $groupId;
32 private $pageId;
34 private $revisionId;
36 private $data;
38 private $description;
39
40 public function __construct(
41 string $groupId,
42 string $name,
43 int $pageId,
44 int $revisionId,
45 ?string $description
46 ) {
47 $this->groupId = $groupId;
48 $this->name = $name;
49 $this->pageId = $pageId;
50 $this->revisionId = $revisionId;
51 $this->description = $description;
52 }
53
55 public static function getGroupId( string $name ): string {
56 return "messagebundle-$name";
57 }
58
59 public function getBundlePageId(): int {
60 return $this->pageId;
61 }
62
63 private function getData(): array {
64 if ( !$this->data ) {
65 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
66 $revision = $revisionStore->getRevisionById( $this->revisionId );
67
68 if ( $revision === null ) {
69 throw new LogicException( "Could not find revision id $this->revisionId" );
70 }
71
72 $content = $revision->getContent( SlotRecord::MAIN );
73 if ( !$content instanceof MessageBundleContent ) {
74 throw new LogicException(
75 "Content with revision id $this->revisionId has wrong content format"
76 );
77 }
78
79 $data = json_decode( $content->getText(), true );
80 if ( !$data ) {
81 throw new LogicException(
82 "Content with revision id $this->revisionId is not valid JSON"
83 );
84 }
85
86 $this->data = $data;
87 }
88
89 return $this->data;
90 }
91
92 private function makeGroupKeys( array $keys ): array {
93 $result = [];
94 foreach ( $keys as $key ) {
95 $result[] = str_replace( ' ', '_', "$this->name/$key" );
96 }
97 return $result;
98 }
99
101 public function getId(): string {
102 return $this->groupId;
103 }
104
106 public function getLabel( IContextSource $context = null ): string {
107 return $this->name;
108 }
109
111 public function getDescription( IContextSource $context = null ): string {
112 $titleText = Title::newFromID( $this->pageId )->getPrefixedText();
113 $linkTargetText = ":$titleText";
114 if ( $context ) {
115 $message = $context->msg( 'translate-messagebundle-group-description' );
116 } else {
117 $message = wfMessage( 'translate-messagebundle-group-description' )
118 ->inContentLanguage();
119 }
120
121 $plainMessage = $message->params( $titleText, $linkTargetText )->plain();
122
123 if ( $this->description === null ) {
124 return $plainMessage;
125 }
126
127 return $plainMessage . ' ' . $this->description;
128 }
129
131 public function getIcon(): ?string {
132 return null;
133 }
134
136 public function getNamespace(): int {
137 return NS_TRANSLATIONS;
138 }
139
141 public function isMeta(): bool {
142 return false;
143 }
144
146 public function exists(): bool {
147 return true;
148 }
149
151 public function getValidator(): ?ValidationRunner {
152 return null;
153 }
154
156 public function getMangler(): ?StringMatcher {
157 return null;
158 }
159
161 public function initCollection( $code ): MessageCollection {
162 $defs = new MessageDefinitions( $this->getDefinitions(), $this->getNamespace() );
163 $collection = MessageCollection::newFromDefinitions( $defs, $code );
164
165 foreach ( $this->getTags() as $type => $tags ) {
166 $collection->setTags( $type, $tags );
167 }
168
169 return $collection;
170 }
171
173 public function load( $code ): array {
174 return [];
175 }
176
178 public function getDefinitions(): array {
179 $data = $this->getData();
180 unset( $data['@metadata'] );
181
182 return array_combine(
183 $this->makeGroupKeys( array_keys( $data ) ),
184 array_values( $data )
185 );
186 }
187
189 public function getKeys(): array {
190 return array_keys( $this->getDefinitions() );
191 }
192
194 public function getTags( $type = null ): array {
195 return [];
196 }
197
199 public function getMessage( $key, $code ): ?string {
200 if ( $code === $this->getSourceLanguage() ) {
201 return $this->getDefinitions()[$key] ?? null;
202 }
203
204 return null;
205 }
206
208 public function getSourceLanguage(): string {
209 return Title::newFromText( $this->name )->getPageLanguage()->getCode();
210 }
211
214 global $wgTranslateWorkflowStates;
215 $conf = $wgTranslateWorkflowStates ?: [];
216
217 MediaWikiHooks::run( 'Translate: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:GroupSynchronizationCache'=> static function(MediaWikiServices $services):GroupSynchronizationCache { return new GroupSynchronizationCache( $services->get( 'Translate:PersistentCache'));}, 'Translate:MessageBundleStore'=> static function(MediaWikiServices $services):MessageBundleStore { return new MessageBundleStore(new RevTagStore(), $services->getJobQueueGroup(), $services->getLanguageNameUtils(), $services->get( 'Translate:MessageIndex'));}, 'Translate:MessageGroupReview'=> static function(MediaWikiServices $services):MessageGroupReview { return new MessageGroupReview($services->getDBLoadBalancer(), $services->getHookContainer());}, 'Translate:MessageGroupStatsTableFactory'=> static function(MediaWikiServices $services):MessageGroupStatsTableFactory { return new MessageGroupStatsTableFactory($services->get( 'Translate:ProgressStatsTableFactory'), $services->getDBLoadBalancer(), $services->getLinkRenderer(), $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:SubpageListBuilder'=> static function(MediaWikiServices $services):SubpageListBuilder { return new SubpageListBuilder($services->get( 'Translate:TranslatableBundleFactory'), $services->getLinkBatchFactory());}, 'Translate:TranslatableBundleFactory'=> static function(MediaWikiServices $services):TranslatableBundleFactory { return new TranslatableBundleFactory($services->get( 'Translate:TranslatablePageStore'), $services->get( 'Translate:MessageBundleStore'));}, '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(), new RevTagStore(), $services->getDBLoadBalancer(), $services->get( 'Translate:TranslatableBundleStatusStore'));}, 'Translate:TranslationStashReader'=> static function(MediaWikiServices $services):TranslationStashReader { $db=$services->getDBLoadBalancer() ->getConnectionRef(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());}, '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.
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.