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;
12use MediaWiki\MediaWikiServices;
13use 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 private function getData(): array {
60 if ( !$this->data ) {
61 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
62 $revision = $revisionStore->getRevisionById( $this->revisionId );
63
64 if ( $revision === null ) {
65 throw new LogicException( "Could not find revision id $this->revisionId" );
66 }
67
68 $content = $revision->getContent( SlotRecord::MAIN );
69 if ( !$content instanceof MessageBundleContent ) {
70 throw new LogicException(
71 "Content with revision id $this->revisionId has wrong content format"
72 );
73 }
74
75 $data = json_decode( $content->getText(), true );
76 if ( !$data ) {
77 throw new LogicException(
78 "Content with revision id $this->revisionId is not valid JSON"
79 );
80 }
81
82 $this->data = $data;
83 }
84
85 return $this->data;
86 }
87
88 private function prefixKeys( array $keys ): array {
89 $result = [];
90 foreach ( $keys as $key ) {
91 $result[] = "$this->name/$key";
92 }
93 return $result;
94 }
95
97 public function getId(): string {
98 return $this->groupId;
99 }
100
102 public function getLabel( IContextSource $context = null ): string {
103 return $this->name;
104 }
105
107 public function getDescription( IContextSource $context = null ): string {
108 $titleText = Title::newFromID( $this->pageId )->getPrefixedText();
109 $linkTargetText = ":$titleText";
110 if ( $context ) {
111 $message = $context->msg( 'translate-messagebundle-group-description' );
112 } else {
113 $message = wfMessage( 'translate-messagebundle-group-description' )
114 ->inContentLanguage();
115 }
116
117 $plainMessage = $message->params( $titleText, $linkTargetText )->plain();
118
119 if ( $this->description === null ) {
120 return $plainMessage;
121 }
122
123 return $plainMessage . ' ' . $this->description;
124 }
125
127 public function getIcon(): ?string {
128 return null;
129 }
130
132 public function getNamespace(): int {
133 return NS_TRANSLATIONS;
134 }
135
137 public function isMeta(): bool {
138 return false;
139 }
140
142 public function exists(): bool {
143 return true;
144 }
145
147 public function getValidator(): ?ValidationRunner {
148 return null;
149 }
150
152 public function getMangler(): ?StringMatcher {
153 return null;
154 }
155
157 public function initCollection( $code ): MessageCollection {
158 $defs = new MessageDefinitions( $this->getDefinitions(), $this->getNamespace() );
159 $collection = MessageCollection::newFromDefinitions( $defs, $code );
160
161 foreach ( $this->getTags() as $type => $tags ) {
162 $collection->setTags( $type, $tags );
163 }
164
165 return $collection;
166 }
167
169 public function load( $code ): array {
170 return [];
171 }
172
174 public function getDefinitions(): array {
175 $data = $this->getData();
176 unset( $data['@metadata'] );
177
178 return array_combine(
179 $this->prefixKeys( array_keys( $data ) ),
180 array_values( $data )
181 );
182 }
183
185 public function getKeys(): array {
186 return array_keys( $this->getDefinitions() );
187 }
188
190 public function getTags( $type = null ): array {
191 return [];
192 }
193
195 public function getMessage( $key, $code ): ?string {
196 if ( $code === $this->getSourceLanguage() ) {
197 return $this->getData()[$key] ?? null;
198 }
199
200 return null;
201 }
202
204 public function getSourceLanguage(): string {
205 return Title::newFromText( $this->name )->getPageLanguage()->getCode();
206 }
207
210 global $wgTranslateWorkflowStates;
211 $conf = $wgTranslateWorkflowStates ?: [];
212
213 MediaWikiHooks::run( 'Translate:modifyMessageGroupStates', [ $this->getId(), &$conf ] );
214
215 return new MessageGroupStates( $conf );
216 }
217
219 public function getTranslatableLanguages(): ?array {
220 return null;
221 }
222
224 public function getSupportConfig(): ?array {
225 return null;
226 }
227}
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'), MessageIndex::singleton());}, '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: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: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: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());}, '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
The versatile default implementation of StringMangler interface.
Message validator is used to run validators to find common mistakes so that translators can fix them ...
Core message collection class.
Wrapper for message definitions, just to beauty the code.
Class for making the use of message group state easier.
Interface for message groups.