Skip to content

Commit

Permalink
GetContribution completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Ostico committed Dec 6, 2024
1 parent 5a7b326 commit 330a87c
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 72 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"stevenmaguire/oauth2-microsoft": "^2.2",
"league/oauth2-github": "^3.1",
"league/oauth2-facebook": "^2.2",
"translated/lara-sdk":"^0",
"translated/lara-sdk":"^1.0.1",
"psr/log": "~1.0",
"ext-curl": "*",
"ext-pcntl": "*",
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions lib/Controller/catController.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function doAction() {
];
}

$this->template->active_engine = Utils::escapeJsonEncode( $active_mt_engine_array );
$this->template->active_engine = $active_mt_engine_array;

/*
* array_unique cast EnginesModel_EngineStruct to string
Expand Down Expand Up @@ -380,7 +380,6 @@ public function setTemplateVars() {

$this->template->mt_engines = $this->translation_engines;
$this->template->translation_engines_intento_providers = Intento::getProviderList();
$this->template->translation_engines_intento_prov_json = Utils::escapeJsonEncode( Intento::getProviderList() );

$this->template->not_empty_default_tm_key = !empty( INIT::$DEFAULT_TM_KEY );

Expand Down
77 changes: 56 additions & 21 deletions lib/Controller/engineController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

use EnginesModel\DeepLStruct;
use EnginesModel\LaraStruct;
use Lara\LaraException;
use Utils\Engines\Lara;
use Validator\DeepLValidator;

/**
Expand All @@ -21,7 +24,7 @@ class engineController extends ajaxController {
'add', 'delete', 'execute'
];
private static $allowed_execute_functions = [
// 'letsmt' => [ 'getTermList' ] // letsmt no longer requires this function. it's left as an example
// 'letsmt' => [ 'getTermList' ] // letsmt no longer requires this function. it's left as an example
];

public function __construct() {
Expand Down Expand Up @@ -126,7 +129,7 @@ private function add() {
$newEngineStruct->extra_parameters[ 'DeepL-Auth-Key' ] = $this->engineData[ 'client_id' ];

try {
DeepLValidator::validate($newEngineStruct);
DeepLValidator::validate( $newEngineStruct );
} catch ( Exception $e ) {
$this->result[ 'errors' ][] = [ 'code' => $e->getCode(), 'message' => $e->getMessage() ];

Expand All @@ -143,11 +146,11 @@ private function add() {
*/
$newEngineStruct = EnginesModel_MicrosoftHubStruct::getStruct();

$newEngineStruct->name = $this->name;
$newEngineStruct->uid = $this->user->uid;
$newEngineStruct->type = Constants_Engines::MT;
$newEngineStruct->extra_parameters[ 'client_id' ] = $this->engineData[ 'client_id' ];
$newEngineStruct->extra_parameters[ 'category' ] = $this->engineData[ 'category' ];
$newEngineStruct->name = $this->name;
$newEngineStruct->uid = $this->user->uid;
$newEngineStruct->type = Constants_Engines::MT;
$newEngineStruct->extra_parameters[ 'client_id' ] = $this->engineData[ 'client_id' ];
$newEngineStruct->extra_parameters[ 'category' ] = $this->engineData[ 'category' ];
break;

case strtolower( Constants_Engines::APERTIUM ):
Expand Down Expand Up @@ -221,28 +224,43 @@ private function add() {

break;

case strtolower(Constants_Engines::INTENTO):
case strtolower( Constants_Engines::INTENTO ):
/**
* Create a record of type Intento
*/
$newEngineStruct = EnginesModel_IntentoStruct::getStruct();
$newEngineStruct->name = $this->name;
$newEngineStruct->uid = $this->user->uid;
$newEngineStruct->type = Constants_Engines::MT;
$newEngineStruct->extra_parameters['apikey'] = $this->engineData['secret'];
$newEngineStruct->extra_parameters['provider'] = $this->engineData['provider'];
$newEngineStruct->extra_parameters['providerkey'] = $this->engineData['providerkey'];
$newEngineStruct->extra_parameters['providercategory'] = $this->engineData['providercategory'];
$newEngineStruct = EnginesModel_IntentoStruct::getStruct();
$newEngineStruct->name = $this->name;
$newEngineStruct->uid = $this->user->uid;
$newEngineStruct->type = Constants_Engines::MT;
$newEngineStruct->extra_parameters[ 'apikey' ] = $this->engineData[ 'secret' ];
$newEngineStruct->extra_parameters[ 'provider' ] = $this->engineData[ 'provider' ];
$newEngineStruct->extra_parameters[ 'providerkey' ] = $this->engineData[ 'providerkey' ];
$newEngineStruct->extra_parameters[ 'providercategory' ] = $this->engineData[ 'providercategory' ];
break;

case strtolower( Constants_Engines::LARA ):
/**
* Create a record of type Lara
*/
$newEngineStruct = LaraStruct::getStruct();

$newEngineStruct->name = $this->name;
$newEngineStruct->uid = $this->user->uid;
$newEngineStruct->type = Constants_Engines::MT;
$newEngineStruct->extra_parameters[ 'Lara-AccessKeyId' ] = $this->engineData[ 'lara-access-key-id' ];
$newEngineStruct->extra_parameters[ 'Lara-AccessKeySecret' ] = $this->engineData[ 'secret' ];
$newEngineStruct->extra_parameters[ 'MMT-License' ] = $this->engineData[ 'mmt-license' ];

break;

default:

// MMT
$validEngine = $newEngineStruct = $this->featureSet->filter( 'buildNewEngineStruct', false, (object)[
'featureSet' => $this->featureSet,
'providerName' => $this->provider,
'logged_user' => $this->user,
'engineData' => $this->engineData
'featureSet' => $this->featureSet,
'providerName' => $this->provider,
'logged_user' => $this->user,
'engineData' => $this->engineData
] );
break;

Expand All @@ -261,7 +279,7 @@ private function add() {

if ( array_search( $newEngineStruct->class_load, $engineList ) ) {
$newEngineStruct->active = true;
$newCreatedDbRowStruct = $engineDAO->create( $newEngineStruct );
$newCreatedDbRowStruct = $engineDAO->create( $newEngineStruct );
$this->destroyUserEnginesCache();
}

Expand Down Expand Up @@ -311,6 +329,23 @@ private function add() {

return;
}
} elseif ( $newEngineStruct instanceof LaraStruct ) {

/**
* @var $newTestCreatedMT Lara
*/
$newTestCreatedMT = Engine::createTempInstance( $newCreatedDbRowStruct );

try {
$newTestCreatedMT->getAvailableLanguages();
} catch ( LaraException $e ) {
$this->result[ 'errors' ][] = $e->getMessage();
$engineDAO->delete( $newCreatedDbRowStruct );
$this->destroyUserEnginesCache();

return;
}

} else {

try {
Expand Down
4 changes: 2 additions & 2 deletions lib/Model/EnginesModel/EngineStruct.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class EnginesModel_EngineStruct


/**
* @var array
* @var array|string
*/
public $extra_parameters;

Expand Down Expand Up @@ -186,7 +186,7 @@ public function getExtraParamsAsArray() {
return $this->extra_parameters;
}

if ( empty( $this->extra_parameters ) or $this->extra_parameters === null ) {
if ( empty( $this->extra_parameters ) ) {
return [];
}

Expand Down
27 changes: 15 additions & 12 deletions lib/Model/EnginesModel/LaraStruct.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
* Time: 14.54
*/

namespace EnginesModel;

use EnginesModel_EngineStruct;
use Utils\Engines\Lara;

/**
* Class EnginesModel_LaraStruct
* Class LaraStruct
*
* This class contains the default parameters for a Lara Engine CREATION
*
*/
class EnginesModel_LaraStruct extends EnginesModel_EngineStruct {
class LaraStruct extends EnginesModel_EngineStruct {

/**
* @var string
Expand All @@ -23,7 +28,7 @@ class EnginesModel_LaraStruct extends EnginesModel_EngineStruct {
/**
* @var string
*/
public $description = "Lara for subscribers, includes adaptive suggestions for entire documents, integrated glossary support and TM usage optimization.";
public $description = "Lara for subscribers, includes adaptive suggestions for entire documents, with TM usage optimization.";

/**
* @var string
Expand All @@ -50,24 +55,22 @@ class EnginesModel_LaraStruct extends EnginesModel_EngineStruct {
*/
public $others = [
"tmx_import_relative_url" => "memories/content",
"api_key_check_auth_url" => "users/me",
"user_update_activate" => "memories/connect",
"context_get" => "context-vector",
];

/**
* @var string
*/
public $class_load = Constants_Engines::MMT;
public $class_load = Lara::class;


/**
* @var array
*/
public $extra_parameters = [
'MMT-License' => "",
'MMT-pretranslate' => "",
'MMT-preimport' => "",
'Lara-AccessKeyId' => "",
'Lara-AccessKeySecret' => "",
'MMT-License' => ""
];

/**
Expand All @@ -82,9 +85,9 @@ class EnginesModel_LaraStruct extends EnginesModel_EngineStruct {

/**
* An empty struct
* @return EnginesModel_EngineStruct
* @return LaraStruct
*/
public static function getStruct() {
return new EnginesModel_MMTStruct();
public static function getStruct(): LaraStruct {
return new LaraStruct();
}
}
2 changes: 1 addition & 1 deletion lib/Plugins/Features/Mmt.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public static function analysisBeforeMTGetContribution( $config, Engines_Abstrac

$config[ 'secret_key' ] = self::getG2FallbackSecretKey();
$config[ 'priority' ] = 'background';
$config[ 'keys' ] = @$config[ 'id_user' ];
$config[ 'keys' ] = $config[ 'id_user' ] ?? [];

}

Expand Down
17 changes: 9 additions & 8 deletions lib/Utils/AsyncTasks/Workers/GetContributionWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,14 +510,15 @@ protected function _getMatches( ContributionRequestStruct $contributionStruct, $
//if a callback is not set only the first argument is returned, get the config params from the callback
$config = $featureSet->filter( 'beforeGetContribution', $config, $mt_engine, $jobStruct );

$config[ 'segment' ] = $contributionStruct->getContexts()->segment;
$config[ 'source' ] = $jobStruct->source;
$config[ 'target' ] = $jobStruct->target;
$config[ 'email' ] = INIT::$MYMEMORY_API_KEY;
$config[ 'segid' ] = $contributionStruct->segmentId;
$config[ 'job_id' ] = $jobStruct->id;
$config[ 'job_password' ] = $jobStruct->password;
$config[ 'session' ] = $contributionStruct->getSessionId();
$config[ 'segment' ] = $contributionStruct->getContexts()->segment;
$config[ 'source' ] = $jobStruct->source;
$config[ 'target' ] = $jobStruct->target;
$config[ 'email' ] = INIT::$MYMEMORY_API_KEY;
$config[ 'segid' ] = $contributionStruct->segmentId;
$config[ 'job_id' ] = $jobStruct->id;
$config[ 'job_password' ] = $jobStruct->password;
$config[ 'session' ] = $contributionStruct->getSessionId();
$config[ 'all_job_tm_keys' ] = $jobStruct->tm_keys;

if ( $mt_engine instanceof Engines_MMT ) {
$metadataDao = new Projects_MetadataDao();
Expand Down
4 changes: 4 additions & 0 deletions lib/Utils/Constants/Engines.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Utils\Engines\Lara;

/**
* Created by PhpStorm.
* User: roberto
Expand All @@ -19,6 +21,7 @@ class Constants_Engines {
const SMART_MATE = 'SmartMATE';
const YANDEX_TRANSLATE = 'YandexTranslate';
const MMT = 'MMT';
const LARA = 'Lara';
const DEEPL = 'DeepL';
const GOOGLE_TRANSLATE = 'GoogleTranslate';
const INTENTO = 'Intento';
Expand All @@ -34,6 +37,7 @@ class Constants_Engines {
self::INTENTO => self::INTENTO,
self::MMT => self::MMT,
self::DEEPL => self::DEEPL,
Lara::class => Lara::class, // new namespaced engine classes must be loaded by fully qualified class name
];

/**
Expand Down
9 changes: 6 additions & 3 deletions lib/Utils/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ public static function getInstance( $id ) {
}

$className = 'Engines_' . $engineRecord->class_load;
if ( !class_exists( $className, true ) ) {
throw new Exception( "Engine Class $className not Found" );
if ( !class_exists( $className ) ) {
$className = $engineRecord->class_load;
if( !class_exists( $className ) ){
throw new Exception( "Engine Class $className not Found" );
}
}

return new $className( $engineRecord );
Expand All @@ -48,7 +51,7 @@ public static function getInstance( $id ) {
/**
* @param EnginesModel_EngineStruct $engineRecord
*
* @return mixed
* @return Engines_AbstractEngine
*/
public static function createTempInstance( EnginesModel_EngineStruct $engineRecord ) {
$className = 'Engines_' . $engineRecord->class_load;
Expand Down
Loading

0 comments on commit 330a87c

Please sign in to comment.