Divec has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/109847

Change subject: MediaWiki API for fetching machine translations
......................................................................

MediaWiki API for fetching machine translations

Change-Id: I8f920cddf6f454f69b2e3e6247fb4c18219d3e19
---
A api/ApiContentTranslationGetMt.php
1 file changed, 61 insertions(+), 0 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation 
refs/changes/47/109847/1

diff --git a/api/ApiContentTranslationGetMt.php 
b/api/ApiContentTranslationGetMt.php
new file mode 100644
index 0000000..64620bd
--- /dev/null
+++ b/api/ApiContentTranslationGetMt.php
@@ -0,0 +1,61 @@
+<?php
+/**
+ * Fetching machine translations for ContentTranslation.
+ * TODO: Need to change when storage is implemented, e.g. to pass page and 
text ID
+ *
+ * @file
+ * @copyright See AUTHORS.txt
+ * @license GPL-2.0+
+ */
+
+class ApiContentTranslationGetMt extends ApiBase {
+       public function execute() {
+               $params = $this->extractRequestParams();
+               $sourceLang = $params['sourceLang'];
+               $targetLang = $params['targetLang'];
+               $sourceText = $params['sourceText'];
+
+               $targetText = $this->getMt( $sourceLang, $targetLang, 
$sourceText );
+               $result = array(
+                       'result' => 'success',
+                       'targetText' => $targetText,
+               );
+               $this->getResult()->addValue( null, $this->getModuleName(), 
$result );
+       }
+
+       protected function getMt( $sourceLang, $targetLang, $text ) {
+               global $wgContentTranslationServerURL, 
$wgContentTranslationServerTimeout;
+               $req = MWHttpRequest::factory( wfAppendQuery(
+                               $wgContentTranslationServerURL,
+                               array(
+                                       'sourceLang' => $sourceLang,
+                                       'targetLang' => $targetLang,
+                                       'text' => $text,
+                               )
+                       ),
+                       array(
+                               method => 'GET',
+                               timeout => $wgContentTranslationServerTimeout,
+                       )
+               );
+               $status = $req->execute();
+               if ( $status->isOK() ) {
+                       $content = $req->getContent();
+               } elseif ( $status->isGood() ) {
+                       $this->dieUsage(
+                               $req->getContent(),
+                               'contenttranslationserver-http-' .  
$req->getStatus()
+                       );
+               } elseif ( $errors = $status->getErrorsByType( 'error' ) ) {
+                       $error = $errors[0];
+                       $code = $error['message'];
+                       if ( count( $error['params'] ) ) {
+                               $message = $error['params'][0];
+                       } else {
+                               $message = 'MWHttpRequest error';
+                       }
+                       $this->dieUsage( $message, 'contenttranslationserver-' 
. $code );
+               }
+               return $content;
+       }
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8f920cddf6f454f69b2e3e6247fb4c18219d3e19
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ContentTranslation
Gerrit-Branch: master
Gerrit-Owner: Divec <[email protected]>

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

Reply via email to