31 private LanguageNameUtils $languageNameUtils;
32 private ILoadBalancer $loadBalancer;
35 public function __construct(
39 LanguageNameUtils $languageNameUtils,
40 ILoadBalancer $loadBalancer,
43 parent::__construct( $query, $moduleName,
'mc' );
44 $this->configHelper = $configHelper;
45 $this->languageNameUtils = $languageNameUtils;
46 $this->loadBalancer = $loadBalancer;
47 $this->groupReviewStore = $groupReviewStore;
50 public function execute():
void {
61 $this->run( $resultPageSet );
64 private function validateLanguageCode(
string $code ): void {
65 if ( !
Utilities::isSupportedLanguageCode( $code ) ) {
66 $this->dieWithError( [
'apierror-translate-invalidlanguage', $code ] );
70 private function run( ?ApiPageSet $resultPageSet =
null ): void {
71 $params = $this->extractRequestParams();
73 $group = MessageGroups::getGroup( $params[
'group'] );
75 $this->dieWithError( [
'apierror-badparameter',
'mcgroup' ] );
78 $languageCode = $params[
'language' ];
79 $this->validateLanguageCode( $languageCode );
80 $sourceLanguageCode = $group->getSourceLanguage();
84 if ( $sourceLanguageCode === $languageCode ) {
85 $name = $this->getLanguageName( $languageCode );
86 $this->addWarning( [
'apiwarn-translate-language-disabled-source', wfEscapeWikiText( $name ) ] );
89 $languages = $group->getTranslatableLanguages();
90 if ( $languages ===
null ) {
93 strtok( $group->getId(),
'-' ),
97 $disabledLanguages = $this->configHelper->getDisabledTargetLanguages();
98 foreach ( $checks as $check ) {
99 if ( isset( $disabledLanguages[ $check ][ $languageCode ] ) ) {
100 $name = $this->getLanguageName( $languageCode );
101 $reason = $disabledLanguages[ $check ][ $languageCode ];
102 $this->dieWithError( [
'apierror-translate-language-disabled-reason', $name, $reason ] );
105 } elseif ( !isset( $languages[ $languageCode ] ) ) {
107 $name = $this->getLanguageName( $languageCode );
108 $this->dieWithError( [
'apierror-translate-language-disabled', $name ] );
113 if ( strtok( $sourceLanguageCode,
'-' ) === strtok( $languageCode,
'-' ) ) {
114 $sourceLanguageName = $this->getLanguageName( $sourceLanguageCode );
115 $targetLanguageName = $this->getLanguageName( $languageCode );
117 'apiwarn-translate-language-targetlang-variant-of-source',
118 wfEscapeWikiText( $targetLanguageName ),
119 wfEscapeWikiText( $sourceLanguageName ) ]
123 if ( MessageGroups::isDynamic( $group ) ) {
126 $group->setLanguage( $params[
'language'] );
129 $messages = $group->initCollection( $params[
'language'] );
131 foreach ( $params[
'filter'] as $filter ) {
132 if ( $filter ===
'' || $filter ===
null ) {
137 if ( str_contains( $filter,
':' ) ) {
138 [ $filter, $value ] = explode(
':', $filter, 2 );
144 $value = $value ===
null ? $value : (int)$value;
145 if ( str_starts_with( $filter,
'!' ) ) {
146 $messages->filter( substr( $filter, 1 ), MessageCollection::EXCLUDE_MATCHING, $value );
148 $messages->filter( $filter, MessageCollection::INCLUDE_MATCHING, $value );
150 }
catch ( InvalidFilterException $e ) {
152 [
'apierror-translate-invalidfilter', wfEscapeWikiText( $e->getMessage() ) ],
158 $resultSize = count( $messages );
159 $offsets = $messages->slice( $params[
'offset'], $params[
'limit'] );
160 $batchSize = count( $messages );
161 [ , $forwardsOffset, $startOffset ] = $offsets;
163 $result = $this->getResult();
165 [
'query',
'metadata' ],
167 $this->groupReviewStore->getWorkflowState( $group->getId(), $params[
'language'] )
170 $result->addValue( [
'query',
'metadata' ],
'resultsize', $resultSize );
172 [
'query',
'metadata' ],
174 $resultSize - $startOffset - $batchSize
177 $messages->loadTranslations();
181 if ( $forwardsOffset !==
false ) {
182 $this->setContinueEnumParameter(
'offset', $forwardsOffset );
185 $props = array_flip( $params[
'prop'] );
188 foreach ( $messages->keys() as $mkey => $titleValue ) {
189 $title = Title::newFromLinkTarget( $titleValue );
191 if ( $resultPageSet ===
null ) {
192 $data = $this->extractMessageData( $result, $props, $messages[$mkey] );
193 $data[
'title'] = $title->getPrefixedText();
194 $data[
'targetLanguage'] = $messages->getLanguage();
196 $handle =
new MessageHandle( $title );
198 if ( $handle->isValid() ) {
199 $data[
'primaryGroup'] = $handle->getGroup()->getId();
202 $result->addValue( [
'query', $this->getModuleName() ],
null, $data );
208 if ( $resultPageSet ===
null ) {
209 $result->addIndexedTagName(
210 [
'query', $this->getModuleName() ],
214 $resultPageSet->populateFromTitles( $pages );
218 private function getLanguageName(
string $languageCode ): string {
221 ->getLanguageName( $languageCode, $this->getLanguage()->getCode() );
224 private function extractMessageData(
229 $data = [
'key' => $message->key() ];
231 if ( isset( $props[
'definition'] ) ) {
232 $data[
'definition'] = $message->definition();
234 if ( isset( $props[
'translation'] ) ) {
236 $translation = $message->translation();
237 if ( $translation !==
null ) {
238 $translation = str_replace( TRANSLATE_FUZZY,
'', $translation );
240 $data[
'translation'] = $translation;
242 if ( isset( $props[
'tags'] ) ) {
243 $data[
'tags'] = $message->getTags();
244 $result->setIndexedTagName( $data[
'tags'],
'tag' );
247 if ( isset( $props[
'revision'] ) ) {
248 $data[
'revision'] = $message->getProperty(
'revision' );
250 if ( isset( $props[
'properties'] ) ) {
251 foreach ( $message->getPropertyNames() as $prop ) {
252 $data[
'properties'][$prop] = $message->getProperty( $prop );
253 ApiResult::setIndexedTagNameRecursive( $data[
'properties'],
'val' );
264 ParamValidator::PARAM_TYPE =>
'string',
265 ParamValidator::PARAM_REQUIRED => true,
268 ParamValidator::PARAM_TYPE =>
'string',
269 ParamValidator::PARAM_DEFAULT =>
'en',
272 ParamValidator::PARAM_DEFAULT => 500,
273 ParamValidator::PARAM_TYPE =>
'limit',
274 IntegerDef::PARAM_MIN => 1,
275 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG2,
276 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
279 ParamValidator::PARAM_DEFAULT =>
'',
280 ParamValidator::PARAM_TYPE =>
'string',
281 ApiBase::PARAM_HELP_MSG =>
'api-help-param-continue',
284 ParamValidator::PARAM_TYPE =>
'string',
285 ParamValidator::PARAM_DEFAULT =>
'!optional|!ignored',
286 ParamValidator::PARAM_ISMULTI => true,
289 ParamValidator::PARAM_TYPE => [
296 ParamValidator::PARAM_DEFAULT =>
'definition|translation',
297 ParamValidator::PARAM_ISMULTI => true,
298 ApiBase::PARAM_HELP_MSG_PER_VALUE => [
299 'translation' => [
'apihelp-query+messagecollection-paramvalue-prop-translation', TRANSLATE_FUZZY ],
301 EnumDef::PARAM_DEPRECATED_VALUES => [
311 'action=query&meta=siteinfo&siprop=languages'
312 =>
'apihelp-query+messagecollection-example-1',
313 'action=query&list=messagecollection&mcgroup=page-Example'
314 =>
'apihelp-query+messagecollection-example-2',
315 'action=query&list=messagecollection&mcgroup=page-Example&mclanguage=fi&' .
316 'mcprop=definition|translation|tags&mcfilter=optional'
317 =>
'apihelp-query+messagecollection-example-3',
318 'action=query&generator=messagecollection&gmcgroup=page-Example&gmclanguage=nl&prop=revisions'
319 =>
'apihelp-query+messagecollection-example-4',
return[ 'Translate:AggregateGroupManager'=> static function(MediaWikiServices $services):AggregateGroupManager { return new AggregateGroupManager($services->getTitleFactory(), $services->get( 'Translate:MessageGroupMetadata'));}, 'Translate:AggregateGroupMessageGroupFactory'=> static function(MediaWikiServices $services):AggregateGroupMessageGroupFactory { return new AggregateGroupMessageGroupFactory($services->get( 'Translate:MessageGroupMetadata'));}, '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:ExternalMessageSourceStateComparator'=> static function(MediaWikiServices $services):ExternalMessageSourceStateComparator { return new ExternalMessageSourceStateComparator(new SimpleStringComparator(), $services->getRevisionLookup(), $services->getPageStore());}, 'Translate:ExternalMessageSourceStateImporter'=> static function(MediaWikiServices $services):ExternalMessageSourceStateImporter { return new ExternalMessageSourceStateImporter($services->get( 'Translate:GroupSynchronizationCache'), $services->getJobQueueGroup(), LoggerFactory::getInstance(LogNames::GROUP_SYNCHRONIZATION), $services->get( 'Translate:MessageIndex'), $services->getTitleFactory(), $services->get( 'Translate:MessageGroupSubscription'), new ServiceOptions(ExternalMessageSourceStateImporter::CONSTRUCTOR_OPTIONS, $services->getMainConfig()));}, 'Translate:FileBasedMessageGroupFactory'=> static function(MediaWikiServices $services):FileBasedMessageGroupFactory { return new FileBasedMessageGroupFactory(new MessageGroupConfigurationParser(), new ServiceOptions(FileBasedMessageGroupFactory::SERVICE_OPTIONS, $services->getMainConfig()),);}, '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:HookDefinedMessageGroupFactory'=> static function(MediaWikiServices $services):HookDefinedMessageGroupFactory { return new HookDefinedMessageGroupFactory( $services->get( 'Translate:HookRunner'));}, 'Translate:HookRunner'=> static function(MediaWikiServices $services):HookRunner { return new HookRunner( $services->getHookContainer());}, 'Translate:MessageBundleDependencyPurger'=> static function(MediaWikiServices $services):MessageBundleDependencyPurger { return new MessageBundleDependencyPurger( $services->get( 'Translate:TranslatableBundleFactory'));}, 'Translate:MessageBundleMessageGroupFactory'=> static function(MediaWikiServices $services):MessageBundleMessageGroupFactory { return new MessageBundleMessageGroupFactory($services->get( 'Translate:MessageGroupMetadata'), new ServiceOptions(MessageBundleMessageGroupFactory::SERVICE_OPTIONS, $services->getMainConfig()),);}, 'Translate:MessageBundleStore'=> static function(MediaWikiServices $services):MessageBundleStore { return new MessageBundleStore($services->get( 'Translate:RevTagStore'), $services->getJobQueueGroup(), $services->getLanguageNameUtils(), $services->get( 'Translate:MessageIndex'), $services->get( 'Translate:MessageGroupMetadata'));}, 'Translate:MessageBundleTranslationLoader'=> static function(MediaWikiServices $services):MessageBundleTranslationLoader { return new MessageBundleTranslationLoader( $services->getLanguageFallback());}, 'Translate:MessageGroupMetadata'=> static function(MediaWikiServices $services):MessageGroupMetadata { return new MessageGroupMetadata( $services->getConnectionProvider());}, 'Translate:MessageGroupReviewStore'=> static function(MediaWikiServices $services):MessageGroupReviewStore { return new MessageGroupReviewStore($services->getConnectionProvider(), $services->get( 'Translate:HookRunner'));}, 'Translate:MessageGroupStatsTableFactory'=> static function(MediaWikiServices $services):MessageGroupStatsTableFactory { return new MessageGroupStatsTableFactory($services->get( 'Translate:ProgressStatsTableFactory'), $services->getLinkRenderer(), $services->get( 'Translate:MessageGroupReviewStore'), $services->get( 'Translate:MessageGroupMetadata'), $services->getMainConfig() ->get( 'TranslateWorkflowStates') !==false);}, 'Translate:MessageGroupSubscription'=> static function(MediaWikiServices $services):MessageGroupSubscription { return new MessageGroupSubscription($services->get( 'Translate:MessageGroupSubscriptionStore'), $services->getJobQueueGroup(), $services->getUserIdentityLookup(), LoggerFactory::getInstance(LogNames::GROUP_SUBSCRIPTION), new ServiceOptions(MessageGroupSubscription::CONSTRUCTOR_OPTIONS, $services->getMainConfig()));}, 'Translate:MessageGroupSubscriptionHookHandler'=> static function(MediaWikiServices $services):MessageGroupSubscriptionHookHandler { return new MessageGroupSubscriptionHookHandler($services->get( 'Translate:MessageGroupSubscription'), $services->getUserFactory());}, 'Translate:MessageGroupSubscriptionStore'=> static function(MediaWikiServices $services):MessageGroupSubscriptionStore { return new MessageGroupSubscriptionStore( $services->getConnectionProvider());}, 'Translate:MessageIndex'=> static function(MediaWikiServices $services):MessageIndex { $params=(array) $services->getMainConfig() ->get( 'TranslateMessageIndex');$class=array_shift( $params);$implementationMap=['HashMessageIndex'=> HashMessageIndex::class, 'CDBMessageIndex'=> CDBMessageIndex::class, 'DatabaseMessageIndex'=> DatabaseMessageIndex::class, 'hash'=> HashMessageIndex::class, 'cdb'=> CDBMessageIndex::class, 'database'=> DatabaseMessageIndex::class,];$messageIndexStoreClass=$implementationMap[$class] ?? $implementationMap['database'];return new MessageIndex(new $messageIndexStoreClass, $services->getMainWANObjectCache(), $services->getJobQueueGroup(), $services->get( 'Translate:HookRunner'), LoggerFactory::getInstance(LogNames::MAIN), $services->getMainObjectStash(), $services->getConnectionProvider(), new ServiceOptions(MessageIndex::SERVICE_OPTIONS, $services->getMainConfig()),);}, '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->getConnectionProvider(), $services->getJsonCodec());}, 'Translate:ProgressStatsTableFactory'=> static function(MediaWikiServices $services):ProgressStatsTableFactory { return new ProgressStatsTableFactory($services->getLinkRenderer(), $services->get( 'Translate:ConfigHelper'), $services->get( 'Translate:MessageGroupMetadata'));}, 'Translate:RevTagStore'=> static function(MediaWikiServices $services):RevTagStore { return new RevTagStore( $services->getConnectionProvider());}, 'Translate:SubpageListBuilder'=> static function(MediaWikiServices $services):SubpageListBuilder { return new SubpageListBuilder($services->get( 'Translate:TranslatableBundleFactory'), $services->getLinkBatchFactory());}, 'Translate:TranslatableBundleDeleter'=> static function(MediaWikiServices $services):TranslatableBundleDeleter { return new TranslatableBundleDeleter($services->getMainObjectStash(), $services->getJobQueueGroup(), $services->get( 'Translate:SubpageListBuilder'), $services->get( 'Translate:TranslatableBundleFactory'));}, 'Translate:TranslatableBundleExporter'=> static function(MediaWikiServices $services):TranslatableBundleExporter { return new TranslatableBundleExporter($services->get( 'Translate:SubpageListBuilder'), $services->getWikiExporterFactory(), $services->getConnectionProvider());}, '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(), $services->getNamespaceInfo(), $services->getTitleFactory(), $services->getFormatterFactory());}, '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->getConnectionProvider(), $services->getObjectCacheFactory(), $services->getMainConfig() ->get( 'TranslatePageMoveLimit'));}, 'Translate:TranslatableBundleStatusStore'=> static function(MediaWikiServices $services):TranslatableBundleStatusStore { return new TranslatableBundleStatusStore($services->getConnectionProvider() ->getPrimaryDatabase(), $services->getCollationFactory() ->makeCollation( 'uca-default-u-kn'), $services->getDBLoadBalancer() ->getMaintenanceConnectionRef(DB_PRIMARY));}, 'Translate:TranslatablePageMarker'=> static function(MediaWikiServices $services):TranslatablePageMarker { return new TranslatablePageMarker($services->getConnectionProvider(), $services->getJobQueueGroup(), $services->getLinkRenderer(), MessageGroups::singleton(), $services->get( 'Translate:MessageIndex'), $services->getTitleFormatter(), $services->getTitleParser(), $services->get( 'Translate:TranslatablePageParser'), $services->get( 'Translate:TranslatablePageStore'), $services->get( 'Translate:TranslatablePageStateStore'), $services->get( 'Translate:TranslationUnitStoreFactory'), $services->get( 'Translate:MessageGroupMetadata'), $services->getWikiPageFactory(), $services->get( 'Translate:TranslatablePageView'), $services->get( 'Translate:MessageGroupSubscription'), $services->getFormatterFactory());}, 'Translate:TranslatablePageMessageGroupFactory'=> static function(MediaWikiServices $services):TranslatablePageMessageGroupFactory { return new TranslatablePageMessageGroupFactory(new ServiceOptions(TranslatablePageMessageGroupFactory::SERVICE_OPTIONS, $services->getMainConfig()),);}, 'Translate:TranslatablePageParser'=> static function(MediaWikiServices $services):TranslatablePageParser { return new TranslatablePageParser($services->get( 'Translate:ParsingPlaceholderFactory'));}, 'Translate:TranslatablePageStateStore'=> static function(MediaWikiServices $services):TranslatablePageStateStore { return new TranslatablePageStateStore($services->get( 'Translate:PersistentCache'), $services->getPageStore());}, 'Translate:TranslatablePageStore'=> static function(MediaWikiServices $services):TranslatablePageStore { return new TranslatablePageStore($services->get( 'Translate:MessageIndex'), $services->getJobQueueGroup(), $services->get( 'Translate:RevTagStore'), $services->getConnectionProvider(), $services->get( 'Translate:TranslatableBundleStatusStore'), $services->get( 'Translate:TranslatablePageParser'), $services->get( 'Translate:MessageGroupMetadata'));}, 'Translate:TranslatablePageView'=> static function(MediaWikiServices $services):TranslatablePageView { return new TranslatablePageView($services->getConnectionProvider(), $services->get( 'Translate:TranslatablePageStateStore'), new ServiceOptions(TranslatablePageView::SERVICE_OPTIONS, $services->getMainConfig()));}, 'Translate:TranslateSandbox'=> static function(MediaWikiServices $services):TranslateSandbox { return new TranslateSandbox($services->getUserFactory(), $services->getConnectionProvider(), $services->getPermissionManager(), $services->getAuthManager(), $services->getUserGroupManager(), $services->getActorStore(), $services->getUserOptionsManager(), $services->getJobQueueGroup(), $services->get( 'Translate:HookRunner'), new ServiceOptions(TranslateSandbox::CONSTRUCTOR_OPTIONS, $services->getMainConfig()));}, 'Translate:TranslationStashReader'=> static function(MediaWikiServices $services):TranslationStashReader { return new TranslationStashStorage( $services->getConnectionProvider() ->getPrimaryDatabase());}, 'Translate:TranslationStatsDataProvider'=> static function(MediaWikiServices $services):TranslationStatsDataProvider { return new TranslationStatsDataProvider(new ServiceOptions(TranslationStatsDataProvider::CONSTRUCTOR_OPTIONS, $services->getMainConfig()), $services->getObjectFactory(), $services->getConnectionProvider());}, '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