Example #1
0
 /**
  * This method adds an engine in a user's keyring
  */
 private function add()
 {
     $newEngine = null;
     $validEngine = true;
     switch (strtolower($this->provider)) {
         case strtolower(Constants_Engines::MICROSOFT_HUB):
             /**
              * Create a record of type MicrosoftHub
              */
             $newEngine = EnginesModel_MicrosoftHubStruct::getStruct();
             $newEngine->name = $this->name;
             $newEngine->uid = $this->uid;
             $newEngine->type = Constants_Engines::MT;
             $newEngine->extra_parameters['client_id'] = $this->engineData['client_id'];
             $newEngine->extra_parameters['client_secret'] = $this->engineData['secret'];
             break;
         case strtolower(Constants_Engines::MOSES):
             /**
              * Create a record of type Moses
              */
             $newEngine = EnginesModel_MosesStruct::getStruct();
             $newEngine->name = $this->name;
             $newEngine->uid = $this->uid;
             $newEngine->type = Constants_Engines::MT;
             $newEngine->base_url = $this->engineData['url'];
             $newEngine->extra_parameters['client_secret'] = $this->engineData['secret'];
             break;
         case strtolower(Constants_Engines::IP_TRANSLATOR):
             /**
              * Create a record of type IPTranslator
              */
             $newEngine = EnginesModel_IPTranslatorStruct::getStruct();
             $newEngine->name = $this->name;
             $newEngine->uid = $this->uid;
             $newEngine->type = Constants_Engines::MT;
             $newEngine->extra_parameters['client_secret'] = $this->engineData['secret'];
             break;
         case strtolower(Constants_Engines::DEEPLINGO):
             /**
              * Create a record of type IPTranslator
              */
             $newEngine = EnginesModel_DeepLingoStruct::getStruct();
             $newEngine->name = $this->name;
             $newEngine->uid = $this->uid;
             $newEngine->type = Constants_Engines::MT;
             $newEngine->base_url = $this->engineData['url'];
             $newEngine->extra_parameters['client_secret'] = $this->engineData['secret'];
             break;
         default:
             $validEngine = false;
     }
     if (!$validEngine) {
         $this->result['errors'][] = array('code' => -4, 'message' => "Engine not allowed");
         return;
     }
     $engineDAO = new EnginesModel_EngineDAO(Database::obtain());
     $result = $engineDAO->create($newEngine);
     if (!$result instanceof EnginesModel_EngineStruct) {
         $this->result['errors'][] = array('code' => -9, 'message' => "Creation failed. Generic error");
         return;
     }
     if ($newEngine instanceof EnginesModel_MicrosoftHubStruct) {
         $engine_test = Engine::getInstance($result->id);
         $config = $engine_test->getConfigStruct();
         $config['segment'] = "Hello World";
         $config['source'] = "en-US";
         $config['target'] = "fr-FR";
         $mt_result = $engine_test->get($config);
         if (isset($mt_result['error']['code'])) {
             $this->result['errors'][] = $mt_result['error'];
             $engineDAO->delete($result);
             return;
         }
     } elseif ($newEngine instanceof EnginesModel_IPTranslatorStruct) {
         $engine_test = Engine::getInstance($result->id);
         /**
          * @var $engine_test Engines_IPTranslator
          */
         $config = $engine_test->getConfigStruct();
         $config['source'] = "en-US";
         $config['target'] = "fr-FR";
         $mt_result = $engine_test->ping($config);
         if (isset($mt_result['error']['code'])) {
             $this->result['errors'][] = $mt_result['error'];
             $engineDAO->delete($result);
             return;
         }
     }
     $this->result['data']['id'] = $result->id;
 }
Example #2
0
 /**
  * This method adds an engine in a user's keyring
  */
 private function add()
 {
     $newEngine = null;
     $validEngine = true;
     switch (strtolower($this->provider)) {
         case strtolower(Constants_Engines::MICROSOFT_HUB):
             /**
              * Create a record of type MicrosoftHub
              */
             $newEngine = EnginesModel_MicrosoftHubStruct::getStruct();
             $newEngine->name = $this->name;
             $newEngine->uid = $this->uid;
             $newEngine->type = Constants_Engines::MT;
             $newEngine->extra_parameters['client_id'] = $this->engineData['client_id'];
             $newEngine->extra_parameters['client_secret'] = $this->engineData['secret'];
             break;
         case strtolower(Constants_Engines::MOSES):
             /**
              * Create a record of type Moses
              */
             $newEngine = EnginesModel_MosesStruct::getStruct();
             $newEngine->name = $this->name;
             $newEngine->uid = $this->uid;
             $newEngine->type = Constants_Engines::MT;
             $newEngine->base_url = $this->engineData['url'];
             $newEngine->extra_parameters['client_secret'] = $this->engineData['secret'];
             break;
         case strtolower(Constants_Engines::IP_TRANSLATOR):
             /**
              * Create a record of type IPTranslator
              */
             $newEngine = EnginesModel_IPTranslatorStruct::getStruct();
             $newEngine->name = $this->name;
             $newEngine->uid = $this->uid;
             $newEngine->type = Constants_Engines::MT;
             $newEngine->extra_parameters['client_secret'] = $this->engineData['secret'];
             break;
         case strtolower(Constants_Engines::DEEPLINGO):
             /**
              * Create a record of type IPTranslator
              */
             $newEngine = EnginesModel_DeepLingoStruct::getStruct();
             $newEngine->name = $this->name;
             $newEngine->uid = $this->uid;
             $newEngine->type = Constants_Engines::MT;
             $newEngine->base_url = $this->engineData['url'];
             $newEngine->extra_parameters['client_secret'] = $this->engineData['secret'];
             break;
         case strtolower(Constants_Engines::LETSMT):
             /**
              * Create a record of type LetsMT
              */
             $newEngine = EnginesModel_LetsMTStruct::getStruct();
             $newEngine->name = $this->name;
             $newEngine->uid = $this->uid;
             $newEngine->type = Constants_Engines::MT;
             $newEngine->extra_parameters['client_id'] = $this->engineData['client_id'];
             $newEngine->extra_parameters['system_id'] = $this->engineData['system_id'];
             // whether this has been set or not indicates whether we should
             // return the newly added system's id or the list of available systems
             // for the user to choose from. the check happens later on
             $newEngine->extra_parameters['terms_id'] = $this->engineData['terms_id'];
             $newEngine->extra_parameters['use_qe'] = $this->engineData['use_qe'];
             $newEngine->extra_parameters['source_lang'] = $this->engineData['source_lang'];
             $newEngine->extra_parameters['target_lang'] = $this->engineData['target_lang'];
             if ($newEngine->extra_parameters['use_qe']) {
                 $minQEString = $this->engineData['minimum_qe'];
                 if (!is_numeric($minQEString)) {
                     $this->result['errors'][] = array('code' => -13, 'message' => "Minimum QE score should be a number between 0 and 1.");
                     return;
                 }
                 $minimumQEScore = floatval($minQEString);
                 if ($minimumQEScore < 0 || $minimumQEScore > 1) {
                     $this->result['errors'][] = array('code' => -13, 'message' => "Minimum QE score should be a number between 0 and 1.");
                     return;
                 }
                 $newEngine->extra_parameters['minimum_qe'] = $minimumQEScore;
             }
             break;
         default:
             $validEngine = false;
     }
     if (!$validEngine) {
         $this->result['errors'][] = array('code' => -4, 'message' => "Engine not allowed");
         return;
     }
     $engineDAO = new EnginesModel_EngineDAO(Database::obtain());
     $result = $engineDAO->create($newEngine);
     if (!$result instanceof EnginesModel_EngineStruct) {
         $this->result['errors'][] = array('code' => -9, 'message' => "Creation failed. Generic error");
         return;
     }
     if ($newEngine instanceof EnginesModel_MicrosoftHubStruct) {
         $engine_test = Engine::getInstance($result->id);
         $config = $engine_test->getConfigStruct();
         $config['segment'] = "Hello World";
         $config['source'] = "en-US";
         $config['target'] = "fr-FR";
         $mt_result = $engine_test->get($config);
         if (isset($mt_result['error']['code'])) {
             $this->result['errors'][] = $mt_result['error'];
             $engineDAO->delete($result);
             return;
         }
     } elseif ($newEngine instanceof EnginesModel_IPTranslatorStruct) {
         $engine_test = Engine::getInstance($result->id);
         /**
          * @var $engine_test Engines_IPTranslator
          */
         $config = $engine_test->getConfigStruct();
         $config['source'] = "en-US";
         $config['target'] = "fr-FR";
         $mt_result = $engine_test->ping($config);
         if (isset($mt_result['error']['code'])) {
             $this->result['errors'][] = $mt_result['error'];
             $engineDAO->delete($result);
             return;
         }
     } elseif ($newEngine instanceof EnginesModel_LetsMTStruct && empty($this->engineData['system_id'])) {
         // the user has not selected a translation system. only the User ID and the engine's name has been entered
         // get the list of available systems and return it to the user
         $temp_engine = Engine::getInstance($result->id);
         $config = $temp_engine->getConfigStruct();
         $systemList = $temp_engine->getSystemList($config);
         $engineDAO->delete($result);
         // delete the newly added engine. this is the first time in engineController::add()
         // and the user has not yet selected a translation system
         if (isset($systemList['error']['code'])) {
             $this->result['errors'][] = $systemList['error'];
             return;
         }
         $uiConfig = array('client_id' => array('value' => $this->engineData['client_id']), 'system_id' => array(), 'terms_id' => array());
         foreach ($systemList as $systemID => $systemInfo) {
             $uiConfig['system_id'][$systemID] = array('value' => $systemInfo['name'], 'data' => $systemInfo['metadata']);
         }
         $this->result['name'] = $this->name;
         $this->result['data']['config'] = $uiConfig;
     } elseif ($newEngine instanceof EnginesModel_LetsMTStruct) {
         // The user has added and configured the Tilde MT engine (the System ID has been set)
         // Do a simple translation request so that the system wakes up by the time the user needs it for translating
         $engine_test = Engine::getInstance($result->id);
         $engine_test->wakeUp();
     }
     $this->result['data']['id'] = $result->id;
 }
Example #3
0
 /**
  * Check for time to live and refresh cache and token info
  *
  * @return mixed
  * @throws Exception
  */
 protected function _authenticate()
 {
     $parameters = array();
     $parameters['client_id'] = $this->client_id;
     $parameters['client_secret'] = $this->client_secret;
     /**
      * Hardcoded params, from documentation
      * @see https://msdn.microsoft.com/en-us/library/hh454950.aspx
      */
     $parameters['grant_type'] = "client_credentials";
     $parameters['scope'] = "http://api.microsofttranslator.com";
     $url = $this->oauth_url;
     $curl_opt = array(CURLOPT_POST => true, CURLOPT_POSTFIELDS => http_build_query($parameters), CURLOPT_TIMEOUT => 120);
     $rawValue = $this->_call($url, $curl_opt);
     $objResponse = json_decode($rawValue, true);
     if (isset($objResponse['error'])) {
         //format as a normal Translate Response and send to decoder to output the data
         $rawValue = sprintf($this->rawXmlErrStruct, $objResponse['error'], 'getToken', $objResponse['error_description']);
         $this->result = $this->_decode($rawValue, $parameters);
         throw new Exception($objResponse['error_description']);
     } else {
         $this->token = $objResponse['access_token'];
         /**
          * Gain a minute to not fallback into a recursion
          *
          * @see Engines_MicrosoftHub::get
          */
         $this->token_endlife = time() + $objResponse['expires_in'] - 60;
     }
     $record = clone $this->engineRecord;
     $engineDAO = new EnginesModel_EngineDAO(Database::obtain());
     /**
      * Use a generic Engine and not Engine_MicrosoftHubStruct
      * because the Engine Factory Class built the query as generic engine
      *
      */
     $engineStruct = EnginesModel_MicrosoftHubStruct::getStruct();
     $engineStruct->id = $record->id;
     //variable assignment only used for debugging purpose
     $debugParam = $engineDAO->destroyCache($engineStruct);
     $engineDAO->update($record);
 }