Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 8 additions & 113 deletions lib/Utils/Engines/Lara.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@
use Projects_ProjectDao;
use RedisHandler;
use ReflectionException;
use RuntimeException;
use SplFileObject;
use Stomp\Transport\Message;
use Throwable;
use TmKeyManagement_MemoryKeyStruct;
use TmKeyManagement_TmKeyManagement;
use TmKeyManagement_TmKeyStruct;
use Users_UserDao;
use Users_UserStruct;

Expand All @@ -57,11 +54,6 @@ class Lara extends Engines_AbstractEngine {
*/
private Engines_EngineInterface $mmt_GET_Fallback;

/**
* @var ?Engines_MMT
*/
private ?Engines_EngineInterface $mmt_SET_PrivateLicense = null;

/**
* @throws Exception
*/
Expand Down Expand Up @@ -99,19 +91,7 @@ protected function _getClient(): Translator {
'MMT-preimport' => false,
];
$this->mmt_GET_Fallback = Engine::createTempInstance( $mmtStruct );

if ( !empty( $extraParams[ 'MMT-License' ] ) ) {
$mmtStruct = EnginesModel_MMTStruct::getStruct();
$mmtStruct->type = Constants_Engines::MT;
$mmtStruct->extra_parameters = [
'MMT-License' => $extraParams[ 'MMT-License' ],
'MMT-pretranslate' => true,
'MMT-preimport' => false,
];
$this->mmt_SET_PrivateLicense = Engine::createTempInstance( $mmtStruct );
}

$this->clientLoaded = new Translator( $credentials );
$this->clientLoaded = new Translator( $credentials );

return $this->clientLoaded;
}
Expand Down Expand Up @@ -297,53 +277,7 @@ public function set( $_config ) {
* @throws Exception
*/
public function update( $_config ) {

$client = $this->_getClient();
$_keys = $this->_reMapKeyList( $_config[ 'keys' ] ?? [] );

if( empty( $_keys ) ) {
Log::doJsonLog( [ "LARA: update skipped. No keys provided." ] );
return true;
}

try {

$time_start = microtime( true );
// call lara
$client->memories->addTranslation(
$_keys,
$_config[ 'source' ],
$_config[ 'target' ],
$_config[ 'segment' ],
$_config[ 'translation' ],
$_config[ 'tuid' ],
$_config[ 'context_before' ],
$_config[ 'context_after' ],
);
$time_end = microtime( true );
$time = $time_end - $time_start;

Log::doJsonLog( [
'LARA REQUEST' => 'PUT https://api.laratranslate.com/memories/content',
'timing' => [ 'Total Time' => $time, 'Request Start Time' => $time_start, 'Request End Time' => $time_end ],
'keys' => $_keys,
'source' => $_config[ 'source' ],
'target' => $_config[ 'target' ],
'sentence' => $_config[ 'segment' ],
'translation' => $_config[ 'translation' ],
'tuid' => $_config[ 'tuid' ],
'sentence_before' => $_config[ 'context_before' ],
'sentence_after' => $_config[ 'context_after' ],
] );

} catch ( Exception $e ) {
// for any exception (HTTP connection or timeout) requeue
return false;
}

// let MMT to have the last word on requeue
return !empty( $this->mmt_SET_PrivateLicense ) ? $this->mmt_SET_PrivateLicense->update( $_config ) : true;

return true;
}

/**
Expand All @@ -370,17 +304,6 @@ public function memoryExists( TmKeyManagement_MemoryKeyStruct $memoryKey ): ?arr
public function deleteMemory( array $memoryKey ): array {
$clientMemories = $this->_getClient()->memories;
try {

if ( !empty( $this->mmt_SET_PrivateLicense ) ) {
$memoryKeyToUpdate = new TmKeyManagement_MemoryKeyStruct();
$memoryKeyToUpdate->tm_key = new TmKeyManagement_TmKeyStruct( [ 'key' => str_replace( 'ext_my_', '', $memoryKey[ 'externalId' ] ) ] );

$memoryMMT = $this->mmt_SET_PrivateLicense->getMemoryIfMine( $memoryKeyToUpdate );
if ( !empty( $memoryMMT ) ) {
$this->mmt_SET_PrivateLicense->deleteMemory( $memoryMMT );
}
}

return $clientMemories->delete( trim( $memoryKey[ 'id' ] ) )->jsonSerialize();
} catch ( LaraApiException $e ) {
if ( $e->getCode() == 404 ) {
Expand All @@ -401,42 +324,14 @@ public function getMemoryIfMine( TmKeyManagement_MemoryKeyStruct $memoryKey ): ?


/**
* @throws LaraException
* @throws Exception
* @param string $filePath
* @param string $memoryKey
* @param Users_UserStruct $user
*
* @return null
*/
public function importMemory( string $filePath, string $memoryKey, Users_UserStruct $user ) {

$clientMemories = $this->_getClient()->memories;

if ( !$clientMemories->get( 'ext_my_' . trim( $memoryKey ) ) ) {
return null;
}

$fp_out = gzopen( "$filePath.gz", 'wb9' );

if ( !$fp_out ) {
$fp_out = null;
throw new RuntimeException( 'IOException. Unable to create temporary file.' );
}

$tmpFileObject = new SplFileObject( $filePath, 'r' );

while ( !$tmpFileObject->eof() ) {
gzwrite( $fp_out, $tmpFileObject->fgets() );
}

$tmpFileObject = null;
gzclose( $fp_out );

$res = $clientMemories->importTmx( 'ext_my_' . $memoryKey, "$filePath.gz", true );
Log::doJsonLog( $res );

$fp_out = null;

if ( !empty( $this->mmt_SET_PrivateLicense ) ) {
$this->mmt_SET_PrivateLicense->importMemory( $filePath, $memoryKey, $user );
}

return null;
}

/**
Expand Down