Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
CaighdeanWebService.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\WebService;
5
6use FormatJson;
7
18 public function getType(): string {
19 return 'mt';
20 }
21
23 public function mapCode( string $code ): string {
24 return $code;
25 }
26
28 protected function doPairs(): array {
29 $pairs = [
30 'gd' => [ 'ga' => true ],
31 'gv' => [ 'ga' => true ],
32 ];
33
34 return $pairs;
35 }
36
38 protected function getQuery( string $text, string $sourceLanguage, string $targetLanguage ): TranslationQuery {
39 if ( !isset( $this->config['url'] ) ) {
40 throw new TranslationWebServiceConfigurationException( '`url` not set in configuration' );
41 }
42
43 $text = trim( $text );
44 if ( $text === '' ) {
45 throw new TranslationWebServiceInvalidInputException( 'Input is empty' );
46 }
47
48 $data = wfArrayToCgi( [
49 'foinse' => $sourceLanguage,
50 'teacs' => $text,
51 ] );
52
53 // Maximum payload is 16 KiB. Based ont testing 16000 bytes is safe by leaving 224
54 // bytes for other things.
55 if ( strlen( $data ) > 16000 ) {
56 throw new TranslationWebServiceInvalidInputException( 'Input is over 16000 bytes long' );
57 }
58
59 return TranslationQuery::factory( $this->config['url'] )
60 ->timeout( intval( $this->config['timeout'] ) )
61 ->postWithData( $data )
62 ->attachProcessingInstructions( $text );
63 }
64
66 protected function parseResponse( TranslationQueryResponse $reply ): string {
67 $body = $reply->getBody();
68 $response = FormatJson::decode( $body );
69 if ( !is_array( $response ) ) {
70 throw new TranslationWebServiceException( 'Invalid json: ' . serialize( $body ) );
71 }
72
73 $text = '';
74 $originalText = $reply->getQuery()->getProcessingInstructions();
75 foreach ( $response as list( $sourceToken, $targetToken ) ) {
76 $separator = ' ';
77 $pos = strpos( $originalText, $sourceToken );
78 // Try to keep the effects local. If we fail to match at token, we could accidentally
79 // scan very far ahead in the text, find a false match and not find matches for all
80 // of the tokens in the between.
81 if ( $pos !== false && $pos < 50 ) {
82 // Remove the portion of text we have processed. $pos should be zero, unless
83 // we failed to match something earlier.
84 $originalText = substr( $originalText, $pos + strlen( $sourceToken ) );
85 if ( preg_match( '/^\s+/', $originalText, $match ) ) {
86 $separator = $match[ 0 ];
87 $originalText = substr( $originalText, strlen( $separator ) );
88 } else {
89 $separator = '';
90 }
91 }
92
93 $text .= $targetToken . $separator;
94 }
95
96 return $text;
97 }
98}
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
parseResponse(TranslationQueryResponse $reply)
@inheritDoc
getQuery(string $text, string $sourceLanguage, string $targetLanguage)
@inheritDoc
Value object that represents a HTTP(S) query response.
getQuery()
Get the TranslationQuery that was made for this request.
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.