Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
RESTBaseWebService.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\WebService;
5
6use FormatJson;
7use MediaWiki\Http\HttpRequestFactory;
8
18 private $httpRequestFactory;
19
20 public function __construct(
21 HttpRequestFactory $httpRequestFactory,
22 string $serviceName,
23 array $config
24 ) {
25 parent::__construct( $serviceName, $config );
26 $this->httpRequestFactory = $httpRequestFactory;
27 }
28
30 public function getType(): string {
31 return 'mt';
32 }
33
35 protected function mapCode( string $code ): string {
36 return $code;
37 }
38
40 protected function doPairs(): array {
41 if ( !isset( $this->config['host'] ) ) {
42 throw new TranslationWebServiceConfigurationException( 'RESTBase host not set' );
43 }
44
45 $pairs = [];
46
47 $url = $this->config['host'] . '/rest_v1/transform/list/tool/mt/';
48 $json = $this->httpRequestFactory->get( $url, [ $this->config['timeout'] ], __METHOD__ );
49 $response = FormatJson::decode( $json, true );
50
51 if ( !is_array( $response ) ) {
52 $exception = 'Malformed reply from remote server: ' . $url . ' ' . (string)$json;
53 throw new TranslationWebServiceException( $exception );
54 }
55
56 foreach ( $response['Apertium'] as $source => $targets ) {
57 foreach ( $targets as $target ) {
58 $pairs[$source][$target] = true;
59 }
60 }
61
62 return $pairs;
63 }
64
66 protected function getQuery( string $text, string $sourceLanguage, string $targetLanguage ): TranslationQuery {
67 if ( !isset( $this->config['host'] ) ) {
68 throw new TranslationWebServiceConfigurationException( 'RESTBase host not set' );
69 }
70
71 $text = trim( $text );
72 $text = $this->wrapUntranslatable( $text );
73 $url = $this->config['host'] . "/rest_v1/transform/html/from/$sourceLanguage/to/$targetLanguage/Apertium";
74
75 return TranslationQuery::factory( $url )
76 ->timeout( intval( $this->config['timeout'] ) )
77 ->postWithData( wfArrayToCgi( [ 'html' => $text ] ) );
78 }
79
81 protected function parseResponse( TranslationQueryResponse $reply ): string {
82 $body = $reply->getBody();
83
84 $response = FormatJson::decode( $body );
85 if ( !is_object( $response ) ) {
86 throw new TranslationWebServiceException( 'Invalid json: ' . serialize( $body ) );
87 }
88
89 $text = $response->contents;
90 $text = $this->unwrapUntranslatable( $text );
91
92 return trim( $text );
93 }
94}
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
Implements support for cxserver proxied through RESTBase.
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.