/**
  * @param int $partnerId
  * @param string $entryId
  * @param string $assetIds comma seperated
  * @return array<KalturaCaptionAsset>
  */
 protected function getCaptionAssets($partnerId, $entryId, $assetIds)
 {
     KBatchBase::impersonate($partnerId);
     $filter = new KalturaCaptionAssetFilter();
     $filter->entryIdEqual = $entryId;
     $filter->idIn = $assetIds;
     $captionPlugin = KalturaCaptionClientPlugin::get(KBatchBase::$kClient);
     $assetsList = $captionPlugin->captionAsset->listAction($filter);
     KBatchBase::unimpersonate();
     return $assetsList->objects;
 }
 private function getCurrentCaptionAssets($entryId)
 {
     $filter = new KalturaCaptionAssetItemFilter();
     $filter->entryIdEqual = $entryId;
     $pager = new KalturaFilterPager();
     $pager->pageSize = 500;
     $captionAssetPlugin = KalturaCaptionClientPlugin::get($this->xmlBulkUploadEngine->getClient());
     $captions = $captionAssetPlugin->captionAsset->listAction($filter, $pager);
     $this->currentCaptionAssets = array();
     if (!isset($captions->objects)) {
         return;
     }
     foreach ($captions->objects as $caption) {
         if ($caption->captionParamsId != 0) {
             //there could be multiple captions with captionParamsId=0
             $this->currentCaptionAssets[$caption->captionParamsId] = $caption->id;
         }
     }
 }
 /**
  * Sync target objects
  * @param KalturaDistributionJobData $jobData
  * @param CrossKalturaEntryObjectsContainer $targetObjects
  */
 protected function sync(KalturaDistributionJobData $jobData, CrossKalturaEntryObjectsContainer $targetObjects)
 {
     $syncedObjects = new CrossKalturaEntryObjectsContainer();
     $targetEntryId = $jobData->remoteId;
     // add/update entry
     if ($targetEntryId) {
         // update entry
         KalturaLog::info('Updating target entry id [' . $targetEntryId . ']');
         $syncedObjects->entry = $this->targetClient->baseEntry->update($targetEntryId, $targetObjects->entry);
     } else {
         // add entry
         $syncedObjects->entry = $this->targetClient->baseEntry->add($targetObjects->entry);
         $targetEntryId = $syncedObjects->entry->id;
         KalturaLog::info('New target entry added with id [' . $targetEntryId . ']');
     }
     $this->targetEntryId = $targetEntryId;
     // sync metadata objects
     foreach ($targetObjects->metadataObjects as $metadataObj) {
         /* @var $metadataObj KalturaMetadata */
         $metadataObj->objectId = $targetEntryId;
     }
     $targetMetadataClient = KalturaMetadataClientPlugin::get($this->targetClient);
     $syncedObjects->metadataObjects = $this->syncTargetEntryObjects($targetMetadataClient->metadata, $targetObjects->metadataObjects, $this->sourceObjects->metadataObjects, $jobData->providerData->distributedMetadata, $targetEntryId, 'getMetadataAddArgs', 'getMetadataUpdateArgs');
     // sync flavor assets
     $syncedObjects->flavorAssets = $this->syncTargetEntryObjects($this->targetClient->flavorAsset, $targetObjects->flavorAssets, $this->sourceObjects->flavorAssets, $jobData->providerData->distributedFlavorAssets, $targetEntryId);
     // sync flavor content
     $this->syncAssetsContent($this->targetClient->flavorAsset, $targetObjects->flavorAssetsContent, $syncedObjects->flavorAssets, $jobData->providerData->distributedFlavorAssets, $this->sourceObjects->flavorAssets);
     // sync thumbnail assets
     $syncedObjects->thumbAssets = $this->syncTargetEntryObjects($this->targetClient->thumbAsset, $targetObjects->thumbAssets, $this->sourceObjects->thumbAssets, $jobData->providerData->distributedThumbAssets, $targetEntryId);
     // sync thumbnail content
     $this->syncAssetsContent($this->targetClient->thumbAsset, $targetObjects->thumbAssetsContent, $syncedObjects->thumbAssets, $jobData->providerData->distributedThumbAssets, $this->sourceObjects->thumbAssets);
     // sync caption assets
     if ($this->distributeCaptions) {
         $targetCaptionClient = KalturaCaptionClientPlugin::get($this->targetClient);
         $syncedObjects->captionAssets = $this->syncTargetEntryObjects($targetCaptionClient->captionAsset, $targetObjects->captionAssets, $this->sourceObjects->captionAssets, $jobData->providerData->distributedCaptionAssets, $targetEntryId);
         // sync caption content
         $this->syncAssetsContent($targetCaptionClient->captionAsset, $targetObjects->captionAssetsContent, $syncedObjects->captionAssets, $jobData->providerData->distributedCaptionAssets, $this->sourceObjects->captionAssets);
     }
     // sync cue points
     if ($this->distributeCuePoints) {
         foreach ($targetObjects->cuePoints as $cuePoint) {
             /* @var $cuePoint KalturaCuePoint */
             $cuePoint->entryId = $targetEntryId;
         }
         $targetCuePointClient = KalturaCuePointClientPlugin::get($this->targetClient);
         $syncedObjects->cuePoints = $this->syncTargetEntryObjects($targetCuePointClient->cuePoint, $targetObjects->cuePoints, $this->sourceObjects->cuePoints, $jobData->providerData->distributedCuePoints, $targetEntryId, 'getCuePointAddArgs');
     }
     return $syncedObjects;
 }
 protected function parseMultiLanguage(KalturaBatchJob $job, KalturaParseMultiLanguageCaptionAssetJobData $data)
 {
     KalturaLog::debug("parse Multi Language job id - ({$job->id})");
     $this->updateJob($job, "Start parsing multi-language caption asset [{$data->multiLanaguageCaptionAssetId}]", KalturaBatchJobStatus::QUEUED);
     $this->captionClientPlugin = KalturaCaptionClientPlugin::get(self::$kClient);
     $parentId = $data->multiLanaguageCaptionAssetId;
     $entryId = $data->entryId;
     $fileLoc = $data->fileLocation;
     $xmlString = file_get_contents($fileLoc);
     if (!$xmlString) {
         $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, 'UNABLE_TO_GET_FILE', "Error: " . 'UNABLE_TO_GET_FILE', KalturaBatchJobStatus::FAILED, $data);
         return $job;
     }
     $xml = simplexml_load_string($xmlString);
     if (!$xml) {
         $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, 'INVALID_XML', "Error: " . 'INVALID_XML', KalturaBatchJobStatus::FAILED, $data);
         return $job;
     }
     $filter = new KalturaAssetFilter();
     $filter->entryIdEqual = $entryId;
     $pager = null;
     $bodyNode = $xml->body;
     if (count($bodyNode->div) > self::NUMBER_OF_LANGUAGES_LIMIT) {
         $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, 'EXCEEDED_NUMBER_OF_LANGUAGES', "Error: " . "exceeded number of languages - " . self::NUMBER_OF_LANGUAGES_LIMIT, KalturaBatchJobStatus::FAILED, $data);
         return $job;
     }
     self::impersonate($job->partnerId);
     $result = $this->captionClientPlugin->captionAsset->listAction($filter, $pager);
     $captionChildernIds = array();
     foreach ($result->objects as $caption) {
         if ($caption->parentId == $parentId) {
             $captionChildernIds[$caption->languageCode] = $caption->id;
         }
     }
     $indexStart = strpos($xmlString, '<div');
     $indexEnd = strrpos($xmlString, '</div>', -1);
     $subXMLStart = substr($xmlString, 0, $indexStart);
     $subXMLEnd = substr($xmlString, $indexEnd + 6);
     foreach ($bodyNode->div as $divNode) {
         $onlyUpdate = false;
         $xmlDivNode = $divNode->asXml();
         $langPos = strpos($xmlDivNode, "xml:lang=");
         $languageShort = substr($xmlDivNode, $langPos + 10, 2);
         if (isset($captionChildernIds[$languageShort])) {
             $id = $captionChildernIds[$languageShort];
             KalturaLog::debug("language {$languageShort} exists as a child of asset {$parentId}");
             $onlyUpdate = true;
             unset($captionChildernIds[$languageShort]);
         }
         $completeXML = $subXMLStart . $xmlDivNode . $subXMLEnd;
         $languageShort = strtoupper($languageShort);
         $languageLong = constant('KalturaLanguage::' . $languageShort);
         $captionAsset = new KalturaCaptionAsset();
         $captionAsset->fileExt = 'xml';
         $captionAsset->language = $languageLong;
         $captionAsset->format = KalturaCaptionType::DFXP;
         $captionAsset->parentId = $parentId;
         $contentResource = new KalturaStringResource();
         $contentResource->content = $completeXML;
         if (!$onlyUpdate) {
             $this->addCaption($entryId, $captionAsset, $contentResource);
         } else {
             $this->setCaptionContent($id, $contentResource);
         }
     }
     //deleting captions of languages that weren't in uploaded file
     self::deleteCaptions($captionChildernIds);
     self::unimpersonate();
     $this->closeJob($job, null, null, "Finished parsing", KalturaBatchJobStatus::FINISHED);
     return $job;
 }
 /**
  * @return KalturaCaptionClientPlugin
  */
 public static function get(KalturaClient $client)
 {
     if (!self::$instance) {
         self::$instance = new KalturaCaptionClientPlugin($client);
     }
     return self::$instance;
 }
 protected function addCaptions(KalturaYouTubeDistributionJobProviderData $providerData, $sftpManager, KalturaDistributionJobData $data)
 {
     if ($providerData->captionAssetIds == "") {
         return;
     }
     $entryId = $data->entryDistribution->entryId;
     $filter = new KalturaAssetFilter();
     $filter->idIn = $providerData->captionAssetIds;
     $filter->entryIdEqual = $entryId;
     KBatchBase::impersonate($data->entryDistribution->partnerId);
     try {
         $captionPlugin = KalturaCaptionClientPlugin::get(KBatchBase::$kClient);
         $result = $captionPlugin->captionAsset->listAction($filter);
     } catch (Exception $e) {
         KBatchBase::unimpersonate();
         throw $e;
     }
     KBatchBase::unimpersonate();
     foreach ($result->objects as $asset) {
         if ($asset instanceof KalturaCaptionAsset) {
             $captionFilePath = $this->getFilePath($asset, $entryId);
             if (file_exists($captionFilePath)) {
                 $captionSFTPPath = $providerData->sftpDirectory . '/' . pathinfo($captionFilePath, PATHINFO_BASENAME);
                 $sftpManager->putFile($captionSFTPPath, $captionFilePath);
             }
         }
     }
 }