Exemplo n.º 1
0
 public function multiConvertToOriginal($xliffVector_array, $chosen_by_user_machine = false)
 {
     $multiCurlObj = new MultiCurlHandler();
     $conversionObjects = array();
     $temporary_files = array();
     //iterate files.
     //For each file prepare a curl resource
     foreach ($xliffVector_array as $id_file => $xliffVector) {
         $xliffContent = $xliffVector['documentContent'];
         //assign converter
         if (!$chosen_by_user_machine) {
             $this->ip = $this->pickRandConverter();
             $storage = $this->getValidStorage();
             //add replace/regexp pattern because we have more than 1 replacement
             //http://stackoverflow.com/questions/2222643/php-preg-replace
             $xliffContent = self::replacedAddress($storage, $xliffContent);
         } else {
             $this->ip = $chosen_by_user_machine;
         }
         $url = "{$this->ip}:{$this->port}/{$this->fromXliffFunction}";
         $uid_ext = $this->extractUidandExt($xliffContent);
         $data['uid'] = $uid_ext[0];
         //get random name for temporary location
         $tmp_name = tempnam("/tmp", "MAT_BW");
         $temporary_files[] = $tmp_name;
         //write encoded file to temporary location
         $fileSize = file_put_contents($tmp_name, $xliffContent);
         $data['xliffContent'] = "@{$tmp_name}";
         $xliffName = $xliffVector['out_xliff_name'];
         //prepare conversion object
         $this->conversionObject->ip_machine = $this->ip;
         $this->conversionObject->ip_client = Utils::getRealIpAddr();
         $this->conversionObject->path_name = $xliffVector['out_xliff_name'];
         $this->conversionObject->file_name = pathinfo($xliffName, PATHINFO_BASENAME);
         $this->conversionObject->direction = 'bw';
         $this->conversionObject->src_lang = $this->lang_handler->getLangRegionCode($xliffVector['source']);
         $this->conversionObject->trg_lang = $this->lang_handler->getLangRegionCode($xliffVector['target']);
         $this->conversionObject->file_size = $fileSize;
         $conversionObjects[$id_file] = clone $this->conversionObject;
         $options = array(CURLOPT_URL => $url, CURLOPT_HEADER => 0, CURLOPT_USERAGENT => INIT::MATECAT_USER_AGENT . INIT::$BUILD_NUMBER, CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $data, CURLOPT_HTTPHEADER => $this->opt['httpheader']);
         $multiCurlObj->createResource($url, $options, $id_file);
     }
     //Perform curl
     Log::doLog("multicurl_start");
     $multiCurlObj->multiExec();
     Log::doLog("multicurl_end");
     $multiInfo = $multiCurlObj->getAllInfo();
     $multiResponses = $multiCurlObj->getAllContents();
     //decode response and return the result
     foreach ($multiResponses as $hash => $json) {
         $multiResponses[$hash] = json_decode($json, true);
         $conversionObjects[$hash]->conversion_time = $multiInfo[$hash]['curlinfo_total_time'];
         $multiResponses[$hash] = $this->__parseOutput($multiResponses[$hash], $conversionObjects[$hash]);
     }
     //remove temporary files
     foreach ($temporary_files as $temp_name) {
         unlink($tmp_name);
     }
     return $multiResponses;
 }