From d0a33fd6713d2251b423608953d0d2bd8b823651 Mon Sep 17 00:00:00 2001 From: domenico Date: Thu, 27 Feb 2025 15:07:14 +0100 Subject: [PATCH] Removed Lara update/importTmx integration --- lib/Utils/Engines/Lara.php | 121 +++---------------------------------- 1 file changed, 8 insertions(+), 113 deletions(-) diff --git a/lib/Utils/Engines/Lara.php b/lib/Utils/Engines/Lara.php index 4e062d3c6d..88299f6714 100644 --- a/lib/Utils/Engines/Lara.php +++ b/lib/Utils/Engines/Lara.php @@ -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; @@ -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 */ @@ -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; } @@ -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; } /** @@ -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 ) { @@ -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; } /**