Nikerabbit has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/372140 )

Change subject: Add RESTBaseWebService
......................................................................

Add RESTBaseWebService

This allows to use Apertium using CXServer MT API through RESTBase.

Example configuration:
$GLOBALS['wgTranslateTranslationServices']['Restbase (WMF)'] = array(
        'type' => 'restbase',
        'host' => 'https://wikimedia.org/api',
);

Change-Id: I3e8130df2e302be629d9cc9c3c591eb194eb7d77
---
M Autoload.php
A webservices/RESTBaseWebService.php
M webservices/TranslationWebService.php
3 files changed, 83 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Translate 
refs/changes/40/372140/1

diff --git a/Autoload.php b/Autoload.php
index b40ba8d..7ed70a1 100644
--- a/Autoload.php
+++ b/Autoload.php
@@ -305,6 +305,7 @@
 $al['CxserverWebService'] = "$dir/webservices/CxserverWebService.php";
 $al['MicrosoftWebService'] = "$dir/webservices/MicrosoftWebService.php";
 $al['RemoteTTMServerWebService'] = 
"$dir/webservices/RemoteTTMServerWebService.php";
+$al['RESTBaseWebService'] = "$dir/webservices/RESTBaseWebService.php";
 $al['TranslationQuery'] = "$dir/webservices/TranslationQuery.php";
 $al['TranslationQueryResponse'] = 
"$dir/webservices/TranslationQueryResponse.php";
 $al['TranslationWebService'] = "$dir/webservices/TranslationWebService.php";
diff --git a/webservices/RESTBaseWebService.php 
b/webservices/RESTBaseWebService.php
new file mode 100644
index 0000000..98ea7ad
--- /dev/null
+++ b/webservices/RESTBaseWebService.php
@@ -0,0 +1,81 @@
+<?php
+/**
+ * Contains a class for querying external translation service.
+ *
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+/**
+ * Implements support for cxserver proxied through RESTBase
+ * @ingroup TranslationWebService
+ * @since 2017.10
+ */
+class RESTBaseWebService extends TranslationWebService {
+       public function getType() {
+               return 'mt';
+       }
+
+       protected function mapCode( $code ) {
+               return $code;
+       }
+
+       protected function doPairs() {
+               if ( !isset( $this->config['host'] ) ) {
+                       throw new TranslationWebServiceConfigurationException( 
'RESTBase host not set' );
+               }
+
+               $pairs = [];
+
+               $url = $this->config['host'] . 
'/rest_v1/transform/list/tool/mt/';
+               $json = Http::get(
+                       $url,
+                       [ $this->config['timeout'] ],
+                       __METHOD__
+               );
+               $response = FormatJson::decode( $json, true );
+
+               if ( !is_array( $response ) ) {
+                       $exception = 'Malformed reply from remote server: ' . 
$url . ' ' . (string)$json;
+                       throw new TranslationWebServiceException( $exception );
+               }
+
+               foreach ( $response['Apertium'] as $source => $targets ) {
+                       foreach ( $targets as $target ) {
+                               $pairs[$source][$target] = true;
+                       }
+               }
+
+               return $pairs;
+       }
+
+       protected function getQuery( $text, $from, $to ) {
+               if ( !isset( $this->config['host'] ) ) {
+                       throw new TranslationWebServiceConfigurationException( 
'RESTBase host not set' );
+               }
+
+               $text = trim( $text );
+               $text = $this->wrapUntranslatable( $text );
+               $url = $this->config['host'] . 
"/rest_v1/transform/html/from/$from/to/$to/Apertium";
+
+               return TranslationQuery::factory( $url )
+                       ->timeout( $this->config['timeout'] )
+                       ->postWithData( wfArrayToCgi( [ 'html' => $text ] ) );
+       }
+
+       protected function parseResponse( TranslationQueryResponse $reply ) {
+               $body = $reply->getBody();
+
+               throw new TranslationWebServiceException( $body );
+               $response = FormatJson::decode( $body );
+               if ( !is_object( $response ) ) {
+                       throw new TranslationWebServiceException( 'Invalid 
json: ' . serialize( $body ) );
+               }
+
+               $text = preg_replace( '~^<div>(.*)</div>$~', '\1', 
$response->contents );
+               $text = $this->unwrapUntranslatable( $text );
+
+               return trim( $text );
+       }
+}
diff --git a/webservices/TranslationWebService.php 
b/webservices/TranslationWebService.php
index 4989fd7..b6515ca 100644
--- a/webservices/TranslationWebService.php
+++ b/webservices/TranslationWebService.php
@@ -34,6 +34,7 @@
                        'yandex' => 'YandexWebService',
                        'remote-ttmserver' => 'RemoteTTMServerWebService',
                        'cxserver' => 'CxserverWebService',
+                       'restbase' => 'RESTBaseWebService',
                        'caighdean' => 'CaighdeanWebService',
                ];
 

-- 
To view, visit https://gerrit.wikimedia.org/r/372140
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3e8130df2e302be629d9cc9c3c591eb194eb7d77
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: Nikerabbit <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to