Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
YandexWebService.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\WebService;
5
6use FormatJson;
7use MediaWiki\Http\HttpRequestFactory;
8use Sanitizer;
9
20 private $httpRequestFactory;
21
22 public function __construct(
23 HttpRequestFactory $httpRequestFactory,
24 string $serviceName,
25 array $config
26 ) {
27 parent::__construct( $serviceName, $config );
28 $this->httpRequestFactory = $httpRequestFactory;
29 }
30
32 public function getType(): string {
33 return 'mt';
34 }
35
37 protected function mapCode( string $code ): string {
38 if ( $code === 'be-tarask' ) {
39 $code = 'be';
40 }
41 return $code;
42 }
43
45 protected function doPairs(): array {
46 if ( !isset( $this->config['key'] ) ) {
47 throw new TranslationWebServiceConfigurationException( 'API key is not set' );
48 }
49
50 $pairs = [];
51
52 $params = [
53 'key' => $this->config['key'],
54 ];
55
56 $url = $this->config['pairs'] . '?' . wfArrayToCgi( $params );
57 $json = $this->httpRequestFactory->get( $url, [ 'timeout' => $this->config['timeout'] ], __METHOD__ );
58 $response = FormatJson::decode( $json );
59
60 if ( !is_object( $response ) ) {
61 $exception = 'Malformed reply from remote server: ' . (string)$json;
62 throw new TranslationWebServiceException( $exception );
63 }
64
65 foreach ( $response->dirs as $pair ) {
66 list( $source, $target ) = explode( '-', $pair );
67 $pairs[$source][$target] = true;
68 }
69
70 return $pairs;
71 }
72
74 protected function getQuery( string $text, string $sourceLanguage, string $targetLanguage ): TranslationQuery {
75 if ( !isset( $this->config['key'] ) ) {
76 throw new TranslationWebServiceConfigurationException( 'API key is not set' );
77 }
78
79 # https://tech.yandex.com/translate/doc/dg/reference/translate-docpage/
80 if ( strlen( $text ) > 10000 ) {
81 throw new TranslationWebServiceInvalidInputException( 'Source text too long' );
82 }
83
84 $text = trim( $text );
85 $text = $this->wrapUntranslatable( $text );
86
87 return TranslationQuery::factory( $this->config['url'] )
88 ->timeout( intval( $this->config['timeout'] ) )
89 ->postWithData( wfArrayToCgi(
90 [
91 'key' => $this->config['key'],
92 'text' => $text,
93 'lang' => "$sourceLanguage-$targetLanguage",
94 'format' => 'html',
95 ]
96 ) );
97 }
98
100 protected function parseResponse( TranslationQueryResponse $reply ): string {
101 $body = $reply->getBody();
102 $response = FormatJson::decode( $body );
103 if ( !is_object( $response ) ) {
104 throw new TranslationWebServiceException( 'Invalid json: ' . serialize( $body ) );
105 } elseif ( $response->code !== 200 ) {
106 throw new TranslationWebServiceException( $response->message );
107 }
108
109 $text = Sanitizer::decodeCharReferences( $response->text[0] );
110 $text = $this->unwrapUntranslatable( $text );
111
112 return trim( $text );
113 }
114}
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
Value object that represents a HTTP(S) query response.
Mutable objects that represents a HTTP(S) query.
Used to signal that the requested input is rejected and cannot be used with an external web service.
Implements support for Yandex translation api v1.
getQuery(string $text, string $sourceLanguage, string $targetLanguage)
@inheritDoc
parseResponse(TranslationQueryResponse $reply)
@inheritDoc