Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
ApertiumWebService.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\WebService;
5
6use FormatJson;
7use LanguageCode;
8use MediaWiki\Http\HttpRequestFactory;
9use Sanitizer;
11
22 private $httpRequestFactory;
23
24 public function __construct(
25 HttpRequestFactory $httpRequestFactory,
26 string $serviceName,
27 array $config
28 ) {
29 parent::__construct( $serviceName, $config );
30 $this->httpRequestFactory = $httpRequestFactory;
31 }
32
34 public function getType(): string {
35 return 'mt';
36 }
37
39 protected function mapCode( string $code ): string {
40 return str_replace( '-', '_', LanguageCode::bcp47( $code ) );
41 }
42
44 protected function doPairs(): array {
45 $pairs = [];
46 $json = $this->httpRequestFactory->get(
47 $this->config['pairs'],
48 [ 'timeout' => $this->config['timeout'] ],
49 __METHOD__
50 );
51 $response = FormatJson::decode( $json );
52
53 if ( !is_object( $response ) ) {
54 $error = 'Malformed reply from remote server: ' . (string)$json;
55 throw new TranslationWebServiceException( $error );
56 }
57
58 foreach ( $response->responseData as $pair ) {
59 $source = $pair->sourceLanguage;
60 $target = $pair->targetLanguage;
61 $pairs[$source][$target] = true;
62 }
63
64 return $pairs;
65 }
66
68 protected function getQuery( string $text, string $sourceLanguage, string $targetLanguage ): TranslationQuery {
69 if ( !isset( $this->config['key'] ) ) {
70 throw new TranslationWebServiceConfigurationException( 'API key is not set' );
71 }
72
73 $text = trim( $text );
74 $text = $this->wrapUntranslatable( $text );
75
76 $params = [
77 'q' => $text,
78 'langpair' => "$sourceLanguage|$targetLanguage",
79 'x-application' => 'MediaWiki Translate extension ' . TranslateUtils::getVersion(),
80 ];
81
82 return TranslationQuery::factory( $this->config['url'] )
83 ->timeout( intval( $this->config['timeout'] ) )
84 ->queryParameters( $params );
85 }
86
88 protected function parseResponse( TranslationQueryResponse $reply ): string {
89 $body = $reply->getBody();
90 $response = FormatJson::decode( $body );
91 if ( !is_object( $response ) ) {
92 throw new TranslationWebServiceException( 'Invalid json: ' . serialize( $body ) );
93 } elseif ( $response->responseStatus !== 200 ) {
94 throw new TranslationWebServiceException( $response->responseDetails );
95 }
96
97 $text = Sanitizer::decodeCharReferences( $response->responseData->translatedText );
98 $text = $this->unwrapUntranslatable( $text );
99
100 return trim( $text );
101 }
102}
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
getQuery(string $text, string $sourceLanguage, string $targetLanguage)
@inheritDoc
parseResponse(TranslationQueryResponse $reply)
@inheritDoc
Value object that represents a HTTP(S) query response.
Mutable objects that represents a HTTP(S) query.
Essentially random collection of helper functions, similar to GlobalFunctions.php.