31 private $configHelper;
33 private $languageNameUtils;
35 private $loadBalancer;
37 public function __construct(
41 LanguageNameUtils $languageNameUtils,
42 ILoadBalancer $loadBalancer
44 parent::__construct( $query, $moduleName,
'mc' );
45 $this->configHelper = $configHelper;
46 $this->languageNameUtils = $languageNameUtils;
47 $this->loadBalancer = $loadBalancer;
50 public function execute():
void {
61 $this->run( $resultPageSet );
64 private function validateLanguageCode(
string $code ): void {
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 );
83 if ( $group->getSourceLanguage() === $languageCode ) {
84 $name = $this->getLanguageName( $languageCode );
85 $this->addWarning( [
'apiwarn-translate-language-disabled-source', wfEscapeWikiText( $name ) ] );
87 $languages = $group->getTranslatableLanguages();
88 if ( $languages ===
null ) {
91 strtok( $group->getId(),
'-' ),
95 $disabledLanguages = $this->configHelper->getDisabledTargetLanguages();
96 foreach ( $checks as $check ) {
97 if ( isset( $disabledLanguages[ $check ][ $languageCode ] ) ) {
98 $name = $this->getLanguageName( $languageCode );
99 $reason = $disabledLanguages[ $check ][ $languageCode ];
100 $this->dieWithError( [
'apierror-translate-language-disabled-reason', $name, $reason ] );
103 } elseif ( !isset( $languages[ $languageCode ] ) ) {
105 $name = $this->getLanguageName( $languageCode );
106 $this->dieWithError( [
'apierror-translate-language-disabled', $name ] );
110 if ( MessageGroups::isDynamic( $group ) ) {
113 $group->setLanguage( $params[
'language'] );
116 $messages = $group->initCollection( $params[
'language'] );
118 foreach ( $params[
'filter'] as $filter ) {
120 if ( strpos( $filter,
':' ) !==
false ) {
121 list( $filter, $value ) = explode(
':', $filter, 2 );
127 if ( $filter[0] ===
'!' ) {
128 $messages->filter( substr( $filter, 1 ),
true, $value );
130 $messages->filter( $filter,
false, $value );
132 }
catch ( MWException $e ) {
134 [
'apierror-translate-invalidfilter', wfEscapeWikiText( $e->getMessage() ) ],
140 $resultSize = count( $messages );
141 $offsets = $messages->slice( $params[
'offset'], $params[
'limit'] );
142 $batchSize = count( $messages );
143 list( , $forwardsOffset, $startOffset ) = $offsets;
145 $result = $this->getResult();
147 [
'query',
'metadata' ],
149 self::getWorkflowState( $group->getId(), $params[
'language'] )
152 $result->addValue( [
'query',
'metadata' ],
'resultsize', $resultSize );
154 [
'query',
'metadata' ],
156 $resultSize - $startOffset - $batchSize
159 $messages->loadTranslations();
163 if ( $forwardsOffset !==
false ) {
164 $this->setContinueEnumParameter(
'offset', $forwardsOffset );
167 $props = array_flip( $params[
'prop'] );
170 foreach ( $messages->keys() as $mkey => $titleValue ) {
171 $title = Title::newFromLinkTarget( $titleValue );
173 if ( $resultPageSet ===
null ) {
174 $data = $this->extractMessageData( $result, $props, $messages[$mkey] );
175 $data[
'title'] = $title->getPrefixedText();
176 $data[
'targetLanguage'] = $messages->getLanguage();
180 if ( $handle->isValid() ) {
181 $data[
'primaryGroup'] = $handle->getGroup()->getId();
184 $result->addValue( [
'query', $this->getModuleName() ],
null, $data );
190 if ( $resultPageSet ===
null ) {
191 $result->addIndexedTagName(
192 [
'query', $this->getModuleName() ],
196 $resultPageSet->populateFromTitles( $pages );
200 private function getLanguageName(
string $languageCode ): string {
203 ->getLanguageName( $languageCode, $this->getLanguage()->getCode() );
206 private function extractMessageData(
211 $data = [
'key' => $message->key() ];
213 if ( isset( $props[
'definition'] ) ) {
216 if ( isset( $props[
'translation'] ) ) {
219 if ( $translation !==
null ) {
220 $translation = str_replace( TRANSLATE_FUZZY,
'', $translation );
222 $data[
'translation'] = $translation;
224 if ( isset( $props[
'tags'] ) ) {
225 $data[
'tags'] = $message->
getTags();
226 $result->setIndexedTagName( $data[
'tags'],
'tag' );
229 if ( isset( $props[
'revision'] ) ) {
230 $data[
'revision'] = $message->getProperty(
'revision' );
232 if ( isset( $props[
'properties'] ) ) {
234 $data[
'properties'][$prop] = $message->getProperty( $prop );
235 ApiResult::setIndexedTagNameRecursive( $data[
'properties'],
'val' );
246 private function getWorkflowState(
string $groupId,
string $language ) {
247 $dbr = $this->loadBalancer->getConnection( DB_REPLICA );
248 return $dbr->newSelectQueryBuilder()
249 ->select(
'tgr_state' )
250 ->from(
'translate_groupreviews' )
252 'tgr_group' => $groupId,
253 'tgr_lang' => $language
255 ->caller( __METHOD__ )
263 ParamValidator::PARAM_TYPE =>
'string',
264 ParamValidator::PARAM_REQUIRED => true,
267 ParamValidator::PARAM_TYPE =>
'string',
268 ParamValidator::PARAM_DEFAULT =>
'en',
271 ParamValidator::PARAM_DEFAULT => 500,
272 ParamValidator::PARAM_TYPE =>
'limit',
273 IntegerDef::PARAM_MIN => 1,
274 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG2,
275 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
278 ParamValidator::PARAM_DEFAULT =>
'',
279 ParamValidator::PARAM_TYPE =>
'string',
280 ApiBase::PARAM_HELP_MSG =>
'api-help-param-continue',
283 ParamValidator::PARAM_TYPE =>
'string',
284 ParamValidator::PARAM_DEFAULT =>
'!optional|!ignored',
285 ParamValidator::PARAM_ISMULTI => true,
288 ParamValidator::PARAM_TYPE => [
295 ParamValidator::PARAM_DEFAULT =>
'definition|translation',
296 ParamValidator::PARAM_ISMULTI => true,
297 ApiBase::PARAM_HELP_MSG =>
298 [
'apihelp-query+messagecollection-param-prop', TRANSLATE_FUZZY ],
306 'action=query&meta=siteinfo&siprop=languages'
307 =>
'apihelp-query+messagecollection-example-1',
308 'action=query&list=messagecollection&mcgroup=page-Example'
309 =>
'apihelp-query+messagecollection-example-2',
310 'action=query&list=messagecollection&mcgroup=page-Example&mclanguage=fi&' .
311 'mcprop=definition|translation|tags&mcfilter=optional'
312 =>
'apihelp-query+messagecollection-example-3',
313 'action=query&generator=messagecollection&gmcgroup=page-Example&gmclanguage=nl&prop=revisions'
314 =>
'apihelp-query+messagecollection-example-4',
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