PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/package/app/app/plugins/bulk_upload/xml/batch/BulkUploadEngineXml.php

https://bitbucket.org/pandaos/kaltura
PHP | 1983 lines | 1434 code | 239 blank | 310 comment | 173 complexity | 8a18648e719801c99db23361e8d60fb2 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, BSD-3-Clause, LGPL-2.1, GPL-2.0, LGPL-3.0, JSON, MPL-2.0-no-copyleft-exception, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Class for the handling Bulk upload using XML in the system
  4. *
  5. * @package Scheduler
  6. * @subpackage Provision
  7. */
  8. class BulkUploadEngineXml extends KBulkUploadEngine
  9. {
  10. /**
  11. * The defalut thumbnail tag
  12. * @var string
  13. */
  14. const DEFAULT_THUMB_TAG = 'default_thumb';
  15. /**
  16. * The default ingestion profile id
  17. * @var int
  18. */
  19. private $defaultConversionProfileId = null;
  20. /**
  21. * Holds the number of the current proccessed item
  22. * @var int
  23. */
  24. private $currentItem = 0;
  25. /**
  26. * The engine xsd file path
  27. * @var string
  28. */
  29. protected $xsdFilePath = null;
  30. /**
  31. * Allows the usage of server content resource
  32. * @var bool
  33. */
  34. protected $allowServerResource = false;
  35. /**
  36. * Maps the flavor params name to id
  37. * @var array()
  38. */
  39. private $assetParamsNameToIdPerConversionProfile = null;
  40. /**
  41. * Maps the asset id to flavor params id
  42. * @var array()
  43. */
  44. private $assetIdToAssetParamsId = null;
  45. /**
  46. * Maps the access control name to id
  47. * @var array()
  48. */
  49. private $accessControlNameToId = null;
  50. /**
  51. * Maps the converstion profile name to id
  52. * @var array()
  53. */
  54. private $conversionProfileNameToId = array();
  55. /**
  56. * Maps the storage profile name to id
  57. * @var array()
  58. */
  59. private $storageProfileNameToId = null;
  60. /**
  61. * Conversion profile xsl file
  62. * @var string
  63. */
  64. protected $conversionProfileXsl = null;
  65. /**
  66. * @param KSchedularTaskConfig $taskConfig
  67. * @param KalturaClient $kClient
  68. * @param KalturaBatchJob $job
  69. */
  70. public function __construct( KSchedularTaskConfig $taskConfig, KalturaClient $kClient, KalturaBatchJob $job)
  71. {
  72. parent::__construct($taskConfig, $kClient, $job);
  73. $this->xsdFilePath = 'http://' . kConf::get('www_host') . '/api_v3/index.php/service/schema/action/serve/type/' . KalturaSchemaType::BULK_UPLOAD_XML;
  74. if($taskConfig->params->xsdFilePath)
  75. $this->xsdFilePath = $taskConfig->params->xsdFilePath;
  76. if($taskConfig->params->allowServerResource)
  77. $this->allowServerResource = (bool) $taskConfig->params->allowServerResource;
  78. }
  79. /* (non-PHPdoc)
  80. * @see KBulkUploadEngine::HandleBulkUpload()
  81. */
  82. public function handleBulkUpload()
  83. {
  84. $this->validate();
  85. $this->parse();
  86. }
  87. /**
  88. * Validates that the xml is valid using the XSD
  89. *@return bool - if the validation is ok
  90. */
  91. protected function validate()
  92. {
  93. if(!file_exists($this->data->filePath))
  94. {
  95. throw new KalturaBatchException("File doesn't exist [{$this->data->filePath}]", KalturaBatchJobAppErrors::BULK_FILE_NOT_FOUND);
  96. }
  97. libxml_use_internal_errors(true);
  98. $this->loadXslt();
  99. $xdoc = new DomDocument();
  100. $xmlContent = $this->xslTransform($this->data->filePath);
  101. libxml_clear_errors();
  102. if(!$xdoc->loadXML($xmlContent)){
  103. $errorMessage = kXml::getLibXmlErrorDescription($xmlContent);
  104. KalturaLog::debug("Could not load xml");
  105. throw new KalturaBatchException("Could not load xml [{$this->job->id}], $errorMessage", KalturaBatchJobAppErrors::BULK_VALIDATION_FAILED);
  106. }
  107. //Validate the XML file against the schema
  108. libxml_clear_errors();
  109. if(!$xdoc->schemaValidate($this->xsdFilePath))
  110. {
  111. $errorMessage = kXml::getLibXmlErrorDescription($xmlContent);
  112. KalturaLog::debug("XML is invalid:\n$errorMessage");
  113. throw new KalturaBatchException("Validate files failed on job [{$this->job->id}], $errorMessage", KalturaBatchJobAppErrors::BULK_VALIDATION_FAILED);
  114. }
  115. return true;
  116. }
  117. /**
  118. * Load xsl transform
  119. */
  120. protected function loadXslt()
  121. {
  122. $data = self::getData();
  123. $conversionProfileId = $data->conversionProfileId;
  124. if($data->conversionProfileId == -1){
  125. $conversionProfileId = PartnerPeer::retrieveByPK($this->currentPartnerId)->getDefaultConversionProfileId();
  126. }
  127. $this->impersonate();
  128. $conversionProfile = $this->kClient->conversionProfile->get($conversionProfileId);
  129. $this->unimpersonate();
  130. if(!$conversionProfile || !$conversionProfile->xslTransformation)
  131. return false;
  132. $this->conversionProfileXsl = $conversionProfile->xslTransformation;
  133. return true;
  134. }
  135. /**
  136. * Transform Xml file with conversion profile xsl
  137. * If xsl is not found, original Xml returned
  138. * @param string $filePath the original xml that was taken from partner file path
  139. * @return string - transformed Xml
  140. */
  141. protected function xslTransform($filePath)
  142. {
  143. $xdoc = file_get_contents($filePath);
  144. if(is_null($xdoc) || is_null($this->conversionProfileXsl))
  145. return $xdoc;
  146. libxml_clear_errors();
  147. $xml = new DOMDocument();
  148. if(!$xml->loadXML($xdoc)){
  149. KalturaLog::debug("Could not load xml");
  150. $errorMessage = kXml::getLibXmlErrorDescription($xdoc);
  151. throw new KalturaBatchException("Could not load xml [{$this->job->id}], $errorMessage", KalturaBatchJobAppErrors::BULK_VALIDATION_FAILED);
  152. }
  153. libxml_clear_errors();
  154. $proc = new XSLTProcessor;
  155. $xsl = new DOMDocument();
  156. if(!$xsl->loadXML($this->conversionProfileXsl)){
  157. KalturaLog::debug("Could not load xsl".$this->conversionProfileXsl);
  158. $errorMessage = kXml::getLibXmlErrorDescription($this->conversionProfileXsl);
  159. throw new KalturaBatchException("Could not load xsl [{$this->job->id}], $errorMessage", KalturaBatchJobAppErrors::BULK_VALIDATION_FAILED);
  160. }
  161. $proc->importStyleSheet($xsl);
  162. libxml_clear_errors();
  163. $transformedXml = $proc->transformToXML($xml);
  164. if(!$transformedXml){
  165. KalturaLog::debug("Could not transform xml".$this->conversionProfileXsl);
  166. $errorMessage = kXml::getLibXmlErrorDescription($this->conversionProfileXsl);
  167. throw new KalturaBatchException("Could not transform xml [{$this->job->id}], $errorMessage", KalturaBatchJobAppErrors::BULK_VALIDATION_FAILED);
  168. }
  169. return $transformedXml;
  170. }
  171. /**
  172. * Parses the Xml file lines and creates the right actions in the system
  173. */
  174. protected function parse()
  175. {
  176. $this->currentItem = 0;
  177. $xdoc = new SimpleXMLElement($this->xslTransform($this->data->filePath));
  178. foreach( $xdoc->channel as $channel)
  179. {
  180. $this->handleChannel($channel);
  181. if($this->exceededMaxRecordsEachRun) // exit if we have proccessed max num of items
  182. return;
  183. }
  184. }
  185. /**
  186. * @param string $referenceId
  187. * @return string entry id
  188. */
  189. protected function getEntryIdFromReference($referenceId)
  190. {
  191. $existingEntry = $this->getEntryFromReference($referenceId);
  192. if($existingEntry)
  193. return $existingEntry->id;
  194. return null;
  195. }
  196. /**
  197. * @param string $referenceId
  198. * @return KalturaBaseEntry
  199. */
  200. protected function getEntryFromReference($referenceId)
  201. {
  202. $filter = new KalturaBaseEntryFilter();
  203. $filter->referenceIdEqual = $referenceId;
  204. $filter->statusIn = implode(',', array(
  205. KalturaEntryStatus::ERROR_IMPORTING,
  206. KalturaEntryStatus::ERROR_CONVERTING,
  207. KalturaEntryStatus::IMPORT,
  208. KalturaEntryStatus::PRECONVERT,
  209. KalturaEntryStatus::READY,
  210. KalturaEntryStatus::PENDING,
  211. KalturaEntryStatus::NO_CONTENT,
  212. ));
  213. $pager = new KalturaFilterPager();
  214. $pager->pageSize = 1;
  215. $this->impersonate();
  216. $entries = null;
  217. try
  218. {
  219. $entries = $this->kClient->baseEntry->listAction($filter, $pager);
  220. }
  221. catch (KalturaException $e)
  222. {
  223. KalturaLog::err($e->getMessage());
  224. }
  225. $this->unimpersonate();
  226. /* @var $entries KalturaBaseEntryListResponse */
  227. if(!$entries || !$entries->totalCount)
  228. return null;
  229. return reset($entries->objects);
  230. }
  231. /**
  232. * @param string $entryId
  233. * @return KalturaBaseEntry
  234. */
  235. protected function getEntry($entryId)
  236. {
  237. $entry = null;
  238. $this->impersonate();
  239. try
  240. {
  241. $entry = $this->kClient->baseEntry->get($entryId);
  242. }
  243. catch (KalturaException $e)
  244. {
  245. KalturaLog::err($e->getMessage());
  246. }
  247. $this->unimpersonate();
  248. return $entry;
  249. }
  250. /**
  251. * Gets and handles a channel from the mrss
  252. * @param SimpleXMLElement $channel
  253. */
  254. protected function handleChannel(SimpleXMLElement $channel)
  255. {
  256. $startIndex = $this->getStartIndex();
  257. KalturaLog::debug("startIndex [$startIndex]");
  258. //Gets all items from the channel
  259. foreach( $channel->item as $item)
  260. {
  261. if($this->currentItem < $startIndex)
  262. {
  263. $this->currentItem++;
  264. continue;
  265. }
  266. if($this->exceededMaxRecordsEachRun) // exit if we have proccessed max num of items
  267. return;
  268. $this->currentItem++; //move to the next item (first item is 1)
  269. try
  270. {
  271. $this->checkAborted();
  272. $actionToPerform = self::$actionsMap[KalturaBulkUploadAction::ADD];
  273. $action = KalturaBulkUploadAction::ADD;
  274. if(isset($item->action))
  275. {
  276. $actionToPerform = strtolower($item->action);
  277. }
  278. elseif(isset($item->entryId))
  279. {
  280. $actionToPerform = self::$actionsMap[KalturaBulkUploadAction::UPDATE];
  281. }
  282. elseif(isset($item->referenceId))
  283. {
  284. if($this->getEntryIdFromReference("{$item->referenceId}"))
  285. $actionToPerform = self::$actionsMap[KalturaBulkUploadAction::UPDATE];
  286. }
  287. switch($actionToPerform)
  288. {
  289. case self::$actionsMap[KalturaBulkUploadAction::ADD]:
  290. $action = KalturaBulkUploadAction::ADD;
  291. $this->validateItem($item);
  292. $this->handleItemAdd($item);
  293. break;
  294. case self::$actionsMap[KalturaBulkUploadAction::UPDATE]:
  295. $action = KalturaBulkUploadAction::UPDATE;
  296. $this->handleItemUpdate($item);
  297. break;
  298. case self::$actionsMap[KalturaBulkUploadAction::DELETE]:
  299. $action = KalturaBulkUploadAction::DELETE;
  300. $this->handleItemDelete($item);
  301. break;
  302. default :
  303. throw new KalturaBatchException("Action: {$actionToPerform} is not supported", KalturaBatchJobAppErrors::BULK_ACTION_NOT_SUPPORTED);
  304. }
  305. }
  306. catch (Exception $e)
  307. {
  308. KalturaLog::err('Item failed (' . get_class($e) . '): ' . $e->getMessage());
  309. $bulkUploadResult = $this->createUploadResult($item, $action);
  310. $bulkUploadResult->errorDescription = $e->getMessage();
  311. $bulkUploadResult->entryStatus = KalturaEntryStatus::ERROR_IMPORTING;
  312. $this->addBulkUploadResult($bulkUploadResult);
  313. }
  314. }
  315. }
  316. /**
  317. * Validates the given item so it's valid (some validation can't be enforced in the schema)
  318. * @param SimpleXMLElement $item
  319. */
  320. protected function validateItem(SimpleXMLElement $item)
  321. {
  322. //Validates that the item type has a matching type element
  323. $this->validateTypeToTypedElement($item);
  324. }
  325. /**
  326. * Gets the flavor params from the given flavor asset
  327. * @param string $assetId
  328. */
  329. protected function getAssetParamsIdFromAssetId($assetId, $entryId)
  330. {
  331. if(is_null($this->assetIdToAssetParamsId[$entryId]))
  332. {
  333. $this->initAssetIdToAssetParamsId($entryId);
  334. }
  335. if(isset($this->assetIdToAssetParamsId[$entryId][$assetId]))
  336. {
  337. return $this->assetIdToAssetParamsId[$entryId][$assetId];
  338. }
  339. else //The asset wasn't found on the entry
  340. {
  341. throw new KalturaBatchException("Asset Id [$assetId] not found on entry [$entryId]", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  342. }
  343. }
  344. /**
  345. * Removes all non updatble fields from the entry
  346. * @param KalturaBaseEntry $entry
  347. */
  348. protected function removeNonUpdatbleFields(KalturaBaseEntry $entry)
  349. {
  350. $retEntry = clone $entry;
  351. $retEntry->conversionProfileId = null;
  352. return $retEntry;
  353. }
  354. /**
  355. * Handles xml bulk upload update
  356. * @param SimpleXMLElement $item
  357. * @throws KalturaException
  358. */
  359. protected function handleItemUpdate(SimpleXMLElement $item)
  360. {
  361. KalturaLog::debug("xml [" . $item->asXML() . "]");
  362. $entryId = null;
  363. $conversionProfileId = null;
  364. if(isset($item->entryId))
  365. {
  366. $entryId = "{$item->entryId}";
  367. $existingEntry = $this->getEntry($entryId);
  368. if(!$existingEntry)
  369. throw new KalturaBatchException("Entry id [$entryId] not found", KalturaBatchJobAppErrors::BULK_ITEM_NOT_FOUND);
  370. $conversionProfileId = $existingEntry->conversionProfileId;
  371. }
  372. elseif(isset($item->referenceId))
  373. {
  374. $existingEntry = $this->getEntryFromReference("{$item->referenceId}");
  375. if(!$existingEntry)
  376. throw new KalturaBatchException("Reference id [{$item->referenceId}] not found", KalturaBatchJobAppErrors::BULK_ITEM_NOT_FOUND);
  377. $entryId = $existingEntry->id;
  378. $conversionProfileId = $existingEntry->conversionProfileId;
  379. }
  380. else
  381. {
  382. throw new KalturaBatchException("Missing entry id element", KalturaBatchJobAppErrors::BULK_MISSING_MANDATORY_PARAMETER);
  383. }
  384. //Throw exception in case of max proccessed items and handle all exceptions there
  385. $updatedEntryBulkUploadResult = $this->createUploadResult($item, KalturaBulkUploadAction::UPDATE);
  386. if($this->exceededMaxRecordsEachRun) // exit if we have proccessed max num of items
  387. return;
  388. $entry = $this->createEntryFromItem($item, $existingEntry->type); //Creates the entry from the item element
  389. $this->handleTypedElement($entry, $item); //Sets the typed element values (Mix, Media, ...)
  390. KalturaLog::debug("current entry is: " . print_r($entry, true));
  391. $thumbAssets = array();
  392. $thumbAssetsResources = array();
  393. $flavorAssets = array();
  394. $flavorAssetsResources = array();
  395. $noParamsThumbAssets = array(); //Holds the no flavor params thumb assests
  396. $noParamsThumbResources = array(); //Holds the no flavor params resources assests
  397. $noParamsFlavorAssets = array(); //Holds the no flavor params flavor assests
  398. $noParamsFlavorResources = array(); //Holds the no flavor params flavor resources
  399. $flavorAssetsForUpdate = array();
  400. $flavorResources = array();
  401. $thumbAssetsForUpdate = array();
  402. $thumbResources = array();
  403. $resource = new KalturaAssetsParamsResourceContainers(); // holds all teh needed resources for the conversion
  404. $resource->resources = array();
  405. //action to perfom for assets - default = replace
  406. $contentAssetsAction = self::$actionsMap[KalturaBulkUploadAction::REPLACE];
  407. if(isset($item->contentAssets->action))
  408. $contentAssetsAction = strtolower($item->contentAssets->action);
  409. //action to perfom for assets - default = replace
  410. $thumbnailsAction = self::$actionsMap[KalturaBulkUploadAction::REPLACE];
  411. if(isset($item->thumbnails->action))
  412. $thumbnailsAction = strtolower($item->thumbnails->action);
  413. if (isset($item->contentAssets->action) && isset($item->thumbnails->action) && ($contentAssetsAction != $thumbnailsAction))
  414. throw new KalturaBatchException("ContentAsset->action: {$contentAssetsAction} must be the same as thumbnails->action: {$thumbnailsAction}", KalturaBatchJobAppErrors::BULK_ACTION_NOT_SUPPORTED);
  415. //For each content in the item element we add a new flavor asset
  416. if(isset($item->contentAssets))
  417. {
  418. foreach ($item->contentAssets->content as $contentElement)
  419. {
  420. KalturaLog::debug("contentElement [" . print_r($contentElement->asXml(), true). "]");
  421. if(empty($contentElement)) // if the content is empty skip
  422. {
  423. continue;
  424. }
  425. $flavorAsset = $this->getFlavorAsset($contentElement, $conversionProfileId);
  426. $flavorAssetResource = $this->getResource($contentElement, $conversionProfileId);
  427. if(!$flavorAssetResource)
  428. continue;
  429. $assetParamsId = $flavorAsset->flavorParamsId;
  430. $assetId = kXml::getXmlAttributeAsString($contentElement, "assetId");
  431. if($assetId) // if we have an asset id then we need to update the asset
  432. {
  433. KalturaLog::debug("Asset id [ $assetId]");
  434. $assetParamsId = $this->getAssetParamsIdFromAssetId($assetId, $entryId);
  435. }
  436. KalturaLog::debug("assetParamsId [$assetParamsId]");
  437. if(is_null($assetParamsId)) // no params resource
  438. {
  439. $noParamsFlavorAssets[] = $flavorAsset;
  440. $noParamsFlavorResources[] = $flavorAssetResource;
  441. continue;
  442. }
  443. else
  444. {
  445. $flavorAssetsResources[$flavorAsset->flavorParamsId] = $flavorAssetResource;
  446. }
  447. $flavorAssets[$flavorAsset->flavorParamsId] = $flavorAsset;
  448. $assetResource = new KalturaAssetParamsResourceContainer();
  449. $assetResource->resource = $flavorAssetResource;
  450. $assetResource->assetParamsId = $assetParamsId;
  451. $resource->resources[] = $assetResource;
  452. }
  453. }
  454. //For each thumbnail in the item element we create a new thumb asset
  455. if(isset($item->thumbnails))
  456. {
  457. foreach ($item->thumbnails->thumbnail as $thumbElement)
  458. {
  459. if(empty($thumbElement)) // if the content is empty
  460. {
  461. continue;
  462. }
  463. KalturaLog::debug("thumbElement [" . print_r($thumbElement->asXml(), true). "]");
  464. $thumbAsset = $this->getThumbAsset($thumbElement, $conversionProfileId);
  465. $thumbAssetResource = $this->getResource($thumbElement, $conversionProfileId);
  466. if(!$thumbAssetResource)
  467. continue;
  468. $assetParamsId = $thumbAsset->thumbParamsId;
  469. $assetId = kXml::getXmlAttributeAsString($thumbElement, "assetId");
  470. if($assetId) // if we have an asset id then we need to update the asset
  471. {
  472. KalturaLog::debug("Asset id [ $assetId]");
  473. $assetParamsId = $this->getAssetParamsIdFromAssetId($assetId, $entryId);
  474. }
  475. KalturaLog::debug("assetParamsId [$assetParamsId]");
  476. if(is_null($assetParamsId))
  477. {
  478. $noParamsThumbAssets[] = $thumbAsset;
  479. $noParamsThumbResources[] = $thumbAssetResource;
  480. continue;
  481. }
  482. else
  483. {
  484. $thumbAssetsResources[$thumbAsset->thumbParamsId] = $thumbAssetResource;
  485. }
  486. $thumbAssets[$thumbAsset->thumbParamsId] = $thumbAsset;
  487. $assetResource = new KalturaAssetParamsResourceContainer();
  488. $assetResource->resource = $thumbAssetResource;
  489. $assetResource->assetParamsId = $assetParamsId;
  490. $resource->resources[] = $assetResource;
  491. }
  492. }
  493. if(!count($resource->resources))
  494. {
  495. if (count($noParamsFlavorResources) == 1)
  496. {
  497. $resource = reset($noParamsFlavorResources);
  498. $noParamsFlavorResources = array();
  499. $noParamsFlavorAssets = array();
  500. }
  501. else
  502. {
  503. $resource = null;
  504. }
  505. }
  506. switch($contentAssetsAction)
  507. {
  508. case self::$actionsMap[KalturaBulkUploadAction::UPDATE]:
  509. $entry = $this->sendItemUpdateData($entryId, $entry, $flavorAssets, $flavorAssetsResources, $thumbAssets, $thumbAssetsResources);
  510. break;
  511. case self::$actionsMap[KalturaBulkUploadAction::REPLACE]:
  512. $entry = $this->sendItemReplaceData($entryId, $entry, $resource,
  513. $noParamsFlavorAssets, $noParamsFlavorResources,
  514. $noParamsThumbAssets, $noParamsThumbResources);
  515. $entryId = $entry->id;
  516. break;
  517. default :
  518. throw new KalturaBatchException("Action: {$contentAssetsAction} is not supported", KalturaBatchJobAppErrors::BULK_ACTION_NOT_SUPPORTED);
  519. }
  520. //Adds the additional data for the flavors and thumbs
  521. $this->handleFlavorAndThumbsAdditionalData($entryId, $flavorAssets, $thumbAssets);
  522. //Handles the plugin added data
  523. $pluginsErrorResults = array();
  524. $pluginsInstances = KalturaPluginManager::getPluginInstances('IKalturaBulkUploadXmlHandler');
  525. foreach($pluginsInstances as $pluginsInstance)
  526. {
  527. /* @var $pluginsInstance IKalturaBulkUploadXmlHandler */
  528. try {
  529. $pluginsInstance->configureBulkUploadXmlHandler($this);
  530. $pluginsInstance->handleItemUpdated($entry, $item);
  531. }catch (Exception $e)
  532. {
  533. KalturaLog::err($pluginsInstance->getContainerName() . ' failed: ' . $e->getMessage());
  534. $pluginsErrorResults[] = $pluginsInstance->getContainerName() . ' failed: ' . $e->getMessage();
  535. }
  536. }
  537. if(count($pluginsErrorResults))
  538. throw new Exception(implode(', ', $pluginsErrorResults));
  539. //Updates the bulk upload result for the given entry (with the status and other data)
  540. $this->updateEntriesResults(array($entry), array($updatedEntryBulkUploadResult));
  541. }
  542. /**
  543. * (non-PHPdoc)
  544. * @see KBulkUploadEngine::addBulkUploadResult()
  545. */
  546. protected function addBulkUploadResult(KalturaBulkUploadResult $bulkUploadResult)
  547. {
  548. parent::addBulkUploadResult($bulkUploadResult);
  549. $this->handledRecordsThisRun++; //adds one to the count of handled records
  550. }
  551. /**
  552. * Sends the data using a multi requsest according to the given data
  553. * @param int $entryID
  554. * @param KalturaBaseEntry $entry
  555. * @param KalturaResource $resource - the main resource collection for the entry
  556. * @param array $noParamsFlavorAssets - Holds the no flavor params flavor assets
  557. * @param array $noParamsFlavorResources - Holds the no flavor params flavor resources
  558. * @param array $noParamsThumbAssets - Holds the no flavor params thumb assets
  559. * @param array $noParamsThumbResources - Holds the no flavor params thumb resources
  560. * @return $requestResults - the multi request result
  561. */
  562. protected function sendItemReplaceData($entryId, KalturaBaseEntry $entry ,KalturaResource $resource = null,
  563. array $noParamsFlavorAssets, array $noParamsFlavorResources,
  564. array $noParamsThumbAssets, array $noParamsThumbResources)
  565. {
  566. KalturaLog::debug("Resource is: " . print_r($resource, true));
  567. $this->impersonate();
  568. $updateEntry = $this->removeNonUpdatbleFields($entry);
  569. $updatedEntry = $this->kClient->baseEntry->get($entryId);
  570. $this->kClient->startMultiRequest();
  571. $updatedEntryId = $updatedEntry->id;
  572. if(!is_null($updatedEntry->replacingEntryId))
  573. $updatedEntryId = $updatedEntry->replacingEntryId;
  574. if($resource)
  575. $this->kClient->baseEntry->updateContent($updatedEntryId ,$resource); //to create a temporery entry.
  576. foreach($noParamsFlavorAssets as $index => $flavorAsset) // Adds all the entry flavors
  577. {
  578. $flavorResource = $noParamsFlavorResources[$index];
  579. $flavor = $this->kClient->flavorAsset->add($updatedEntryId, $flavorAsset);
  580. $this->kClient->flavorAsset->setContent($this->kClient->getMultiRequestResult()->id, $flavorResource); // TODO: use flavor instead of getMultiRequestResult
  581. }
  582. foreach($noParamsThumbAssets as $index => $thumbAsset) //Adds the entry thumb assests
  583. {
  584. $thumbResource = $noParamsThumbResources[$index];
  585. $thumb = $this->kClient->thumbAsset->add($updatedEntryId, $thumbAsset);
  586. $this->kClient->thumbAsset->setContent($this->kClient->getMultiRequestResult()->id, $thumbResource); // TODO: use thumb instead of getMultiRequestResult
  587. }
  588. $requestResults = $this->kClient->doMultiRequest();
  589. //update is after add content since in case entry replacement we want the duration to be set on the new entry.
  590. $updatedEntry = $this->kClient->baseEntry->update($entryId, $updateEntry);
  591. $this->unimpersonate();
  592. if(is_array($requestResults))
  593. foreach($requestResults as $requestResult)
  594. if($requestResult instanceof Exception)
  595. throw $requestResult;
  596. return $updatedEntry;
  597. }
  598. /**
  599. * Handles xml bulk upload delete
  600. * @param SimpleXMLElement $item
  601. * @throws KalturaException
  602. */
  603. protected function handleItemDelete(SimpleXMLElement $item)
  604. {
  605. $entryId = null;
  606. if(isset($item->entryId))
  607. {
  608. $entryId = "{$item->entryId}";
  609. }
  610. elseif(isset($item->referenceId))
  611. {
  612. $entryId = $this->getEntryIdFromReference("{$item->referenceId}");
  613. if(!$entryId)
  614. throw new KalturaBatchException("Reference id [{$item->referenceId}] not found", KalturaBatchJobAppErrors::BULK_ITEM_NOT_FOUND);
  615. }
  616. else
  617. {
  618. throw new KalturaBatchException("Missing entry id element", KalturaBatchJobAppErrors::BULK_MISSING_MANDATORY_PARAMETER);
  619. }
  620. $this->impersonate();
  621. $result = $this->kClient->baseEntry->delete($entryId);
  622. $this->unimpersonate();
  623. $bulkUploadResult = $this->createUploadResult($item, KalturaBulkUploadAction::DELETE);
  624. if($this->exceededMaxRecordsEachRun) // exit if we have proccessed max num of items
  625. return;
  626. $bulkUploadResult->entryId = $entryId;
  627. $this->addBulkUploadResult($bulkUploadResult);
  628. }
  629. /**
  630. * Gets an item and insert it into the system
  631. * @param SimpleXMLElement $item
  632. */
  633. protected function handleItemAdd(SimpleXMLElement $item)
  634. {
  635. KalturaLog::debug("xml [" . $item->asXML() . "]");
  636. //Throw exception in case of max proccessed items and handle all exceptions there
  637. $createdEntryBulkUploadResult = $this->createUploadResult($item, KalturaBulkUploadAction::ADD);
  638. if($this->exceededMaxRecordsEachRun) // exit if we have proccessed max num of items
  639. return;
  640. $entry = $this->createEntryFromItem($item); //Creates the entry from the item element
  641. $this->handleTypedElement($entry, $item); //Sets the typed element values (Mix, Media, ...)
  642. KalturaLog::debug("current entry is: " . print_r($entry, true));
  643. $thumbAssets = array();
  644. $flavorAssets = array();
  645. $noParamsThumbAssets = array(); //Holds the no flavor params thumb assests
  646. $noParamsThumbResources = array(); //Holds the no flavor params resources assests
  647. $noParamsFlavorAssets = array(); //Holds the no flavor params flavor assests
  648. $noParamsFlavorResources = array(); //Holds the no flavor params flavor resources
  649. $resource = new KalturaAssetsParamsResourceContainers(); // holds all teh needed resources for the conversion
  650. $resource->resources = array();
  651. //For each content in the item element we add a new flavor asset
  652. if(isset($item->contentAssets))
  653. {
  654. foreach ($item->contentAssets->content as $contentElement)
  655. {
  656. $assetResource = $this->getResource($contentElement, $entry->conversionProfileId);
  657. if(!$assetResource)
  658. continue;
  659. $assetResourceContainer = new KalturaAssetParamsResourceContainer();
  660. $flavorAsset = $this->getFlavorAsset($contentElement, $entry->conversionProfileId);
  661. if(is_null($flavorAsset->flavorParamsId))
  662. {
  663. KalturaLog::debug("flavorAsset [". print_r($flavorAsset, true) . "]");
  664. $noParamsFlavorAssets[] = $flavorAsset;
  665. $noParamsFlavorResources[] = $assetResource;
  666. }
  667. else
  668. {
  669. KalturaLog::debug("flavorAsset->flavorParamsId [$flavorAsset->flavorParamsId]");
  670. $flavorAssets[$flavorAsset->flavorParamsId] = $flavorAsset;
  671. $assetResourceContainer->assetParamsId = $flavorAsset->flavorParamsId;
  672. $assetResourceContainer->resource = $assetResource;
  673. $resource->resources[] = $assetResourceContainer;
  674. }
  675. }
  676. }
  677. //For each thumbnail in the item element we create a new thumb asset
  678. if(isset($item->thumbnails))
  679. {
  680. foreach ($item->thumbnails->thumbnail as $thumbElement)
  681. {
  682. $assetResource = $this->getResource($thumbElement, $entry->conversionProfileId);
  683. if(!$assetResource)
  684. continue;
  685. $assetResourceContainer = new KalturaAssetParamsResourceContainer();
  686. $thumbAsset = $this->getThumbAsset($thumbElement, $entry->conversionProfileId);
  687. if(is_null($thumbAsset->thumbParamsId))
  688. {
  689. KalturaLog::debug("thumbAsset [". print_r($thumbAsset, true) . "]");
  690. $noParamsThumbAssets[] = $thumbAsset;
  691. $noParamsThumbResources[] = $assetResource;
  692. }
  693. else //we have a thumbParamsId so we add to the resources
  694. {
  695. KalturaLog::debug("thumbAsset->thumbParamsId [$thumbAsset->thumbParamsId]");
  696. $thumbAssets[$thumbAsset->thumbParamsId] = $thumbAsset;
  697. $assetResourceContainer->assetParamsId = $thumbAsset->thumbParamsId;
  698. $assetResourceContainer->resource = $assetResource;
  699. $resource->resources[] = $assetResourceContainer;
  700. }
  701. }
  702. }
  703. if(!count($resource->resources))
  704. {
  705. if (count($noParamsFlavorResources) == 1)
  706. {
  707. $resource = reset($noParamsFlavorResources);
  708. $noParamsFlavorResources = array();
  709. $noParamsFlavorAssets = array();
  710. }
  711. else
  712. {
  713. $resource = null;
  714. }
  715. }
  716. $createdEntry = $this->sendItemAddData($entry, $resource, $noParamsFlavorAssets, $noParamsFlavorResources, $noParamsThumbAssets, $noParamsThumbResources);
  717. //Updates the bulk upload result for the given entry (with the status and other data)
  718. $this->updateEntriesResults(array($createdEntry), array($createdEntryBulkUploadResult));
  719. //Adds the additional data for the flavors and thumbs
  720. $this->handleFlavorAndThumbsAdditionalData($createdEntry->id, $flavorAssets, $thumbAssets);
  721. //Handles the plugin added data
  722. $pluginsErrorResults = array();
  723. $pluginsInstances = KalturaPluginManager::getPluginInstances('IKalturaBulkUploadXmlHandler');
  724. foreach($pluginsInstances as $pluginsInstance)
  725. {
  726. /* @var $pluginsInstance IKalturaBulkUploadXmlHandler */
  727. try {
  728. $pluginsInstance->configureBulkUploadXmlHandler($this);
  729. $pluginsInstance->handleItemAdded($createdEntry, $item);
  730. }catch (Exception $e)
  731. {
  732. KalturaLog::err($pluginsInstance->getContainerName() . ' failed: ' . $e->getMessage());
  733. $pluginsErrorResults[] = $pluginsInstance->getContainerName() . ' failed: ' . $e->getMessage();
  734. }
  735. }
  736. if(count($pluginsErrorResults))
  737. throw new Exception(implode(', ', $pluginsErrorResults));
  738. }
  739. /**
  740. * Sends the data using a multi requsest according to the given data
  741. * @param KalturaBaseEntry $entry
  742. * @param KalturaResource $resource
  743. * @param array $noParamsFlavorAssets
  744. * @param array $noParamsFlavorResources
  745. * @param array $noParamsThumbAssets
  746. * @param array $noParamsThumbResources
  747. * @return $requestResults - the multi request result
  748. */
  749. protected function sendItemAddData(KalturaBaseEntry $entry ,KalturaResource $resource = null, array $noParamsFlavorAssets, array $noParamsFlavorResources, array $noParamsThumbAssets, array $noParamsThumbResources)
  750. {
  751. $this->impersonate();
  752. $this->kClient->startMultiRequest();
  753. KalturaLog::debug("Resource is: " . print_r($resource, true));
  754. $this->kClient->baseEntry->add($entry); //Adds the entry
  755. $newEntryId = $this->kClient->getMultiRequestResult()->id; // TODO: use the return value of add instead of getMultiRequestResult
  756. if($resource)
  757. $this->kClient->baseEntry->addContent($newEntryId, $resource); // adds the entry resources
  758. foreach($noParamsFlavorAssets as $index => $flavorAsset) // Adds all the entry flavors
  759. {
  760. $flavorResource = $noParamsFlavorResources[$index];
  761. $flavor = $this->kClient->flavorAsset->add($newEntryId, $flavorAsset);
  762. $this->kClient->flavorAsset->setContent($this->kClient->getMultiRequestResult()->id, $flavorResource); // TODO: use flavor instead of getMultiRequestResult
  763. }
  764. foreach($noParamsThumbAssets as $index => $thumbAsset) //Adds the entry thumb assests
  765. {
  766. $thumbResource = $noParamsThumbResources[$index];
  767. $thumb = $this->kClient->thumbAsset->add($newEntryId, $thumbAsset, $thumbResource);
  768. $this->kClient->thumbAsset->setContent($this->kClient->getMultiRequestResult()->id, $thumbResource); // TODO: use thumb instead of getMultiRequestResult
  769. }
  770. $requestResults = $this->kClient->doMultiRequest();;
  771. $this->unimpersonate();
  772. foreach($requestResults as $requestResult)
  773. if($requestResult instanceof Exception)
  774. throw $requestResult;
  775. $createdEntry = reset($requestResults);
  776. if(is_null($createdEntry)) //checks that the entry was created
  777. throw new KalturaBulkUploadXmlException("The entry wasn't created", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  778. if(!($createdEntry instanceof KalturaObjectBase)) // if the entry is not kaltura object (in case of errors)
  779. throw new KalturaBulkUploadXmlException("Returned type is [" . get_class($createdEntry) . "], KalturaObjectBase was expected", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  780. return $createdEntry;
  781. }
  782. /**
  783. * Handles the adding of additional data to the preciously created flavors and thumbs
  784. * @param int $createdEntryId
  785. * @param array $flavorAssets
  786. * @param array $thumbAssets
  787. */
  788. protected function handleFlavorAndThumbsAdditionalData($createdEntryId, $flavorAssets, $thumbAssets)
  789. {
  790. $this->impersonate();
  791. $this->kClient->startMultiRequest();
  792. $this->kClient->flavorAsset->getByEntryId($createdEntryId);
  793. $this->kClient->thumbAsset->getByEntryId($createdEntryId);
  794. $result = $this->kClient->doMultiRequest();
  795. foreach($result as $requestResult)
  796. if($requestResult instanceof Exception)
  797. throw $requestResult;
  798. $createdFlavorAssets = $result[0];
  799. $createdThumbAssets = $result[1];
  800. $this->kClient->startMultiRequest();
  801. ///For each flavor asset that we just added without his data then we need to update his additional data
  802. foreach($createdFlavorAssets as $createdFlavorAsset)
  803. {
  804. if(is_null($createdFlavorAsset->flavorParamsId)) //no flavor params to the flavor asset
  805. continue;
  806. if(!isset($flavorAssets[$createdFlavorAsset->flavorParamsId])) // We don't have the flavor in our dictionary
  807. continue;
  808. $flavorAsset = $flavorAssets[$createdFlavorAsset->flavorParamsId];
  809. $this->kClient->flavorAsset->update($createdFlavorAsset->id, $flavorAsset);
  810. }
  811. foreach($createdThumbAssets as $createdThumbAsset)
  812. {
  813. if(is_null($createdThumbAsset->thumbParamsId))
  814. continue;
  815. if(!isset($thumbAssets[$createdThumbAsset->thumbParamsId]))
  816. continue;
  817. $thumbAsset = $thumbAssets[$createdThumbAsset->thumbParamsId];
  818. $this->kClient->thumbAsset->update($createdThumbAsset->id, $thumbAsset);
  819. }
  820. $requestResults = $this->kClient->doMultiRequest();
  821. $this->unimpersonate();
  822. if(is_array($requestResults))
  823. foreach($requestResults as $requestResult)
  824. if($requestResult instanceof Exception)
  825. throw $requestResult;
  826. return $requestResults;
  827. }
  828. /**
  829. * Handles the adding of additional data to the preciously created flavors and thumbs
  830. * @param int $entryId
  831. * @param KalturaBaseEntry $entry
  832. * @param array $flavorAssets
  833. * @param array $flavorAssetsResources
  834. * @param array $thumbAssets
  835. * @param array $thumbAssetsResources
  836. */
  837. protected function sendItemUpdateData($entryId, $entry, array $flavorAssets, array $flavorAssetsResources, array $thumbAssets, array $thumbAssetsResources)
  838. {
  839. $this->impersonate();
  840. $updateEntry = $this->removeNonUpdatbleFields($entry);
  841. $updatedEntry = $this->kClient->baseEntry->update($entryId, $updateEntry);
  842. $this->kClient->startMultiRequest();
  843. $this->kClient->flavorAsset->getByEntryId($entryId);
  844. $this->kClient->thumbAsset->getByEntryId($entryId);
  845. $result = $this->kClient->doMultiRequest();
  846. foreach($result as $requestResult)
  847. if($requestResult instanceof Exception)
  848. throw $requestResult;
  849. $createdFlavorAssets = $result[0];
  850. $createdThumbAssets = $result[1];
  851. $existingflavorAssets = array();
  852. $existingthumbAssets = array();
  853. foreach($createdFlavorAssets as $createdFlavorAsset)
  854. {
  855. if(is_null($createdFlavorAsset->flavorParamsId)) //no flavor params to the flavor asset
  856. continue;
  857. $existingflavorAssets[$createdFlavorAsset->flavorParamsId] = $createdFlavorAsset->id;
  858. }
  859. $this->kClient->startMultiRequest();
  860. foreach ($flavorAssetsResources as $flavorParamsId => $flavorAssetsResource)
  861. {
  862. if(!isset($existingflavorAssets[$flavorParamsId]))
  863. {
  864. $this->kClient->flavorAsset->add($entryId, $flavorAssets[$flavorParamsId]);
  865. $this->kClient->flavorAsset->setContent($this->kClient->getMultiRequestResult()->id, $flavorAssetsResource);
  866. }else{
  867. $this->kClient->flavorAsset->update($existingflavorAssets[$flavorParamsId], $flavorAssets[$flavorParamsId]);
  868. $this->kClient->flavorAsset->setContent($existingflavorAssets[$flavorParamsId], $flavorAssetsResource);
  869. }
  870. }
  871. foreach($createdThumbAssets as $createdThumbAsset)
  872. {
  873. if(is_null($createdThumbAsset->thumbParamsId))
  874. continue;
  875. $existingthumbAssets[$createdThumbAsset->thumbParamsId] = $createdThumbAsset->id;
  876. }
  877. foreach($thumbAssetsResources as $thumbParamsId => $thumbAssetsResource)
  878. {
  879. if(!isset($existingthumbAssets[$thumbParamsId]))
  880. {
  881. $this->kClient->thumbAsset->add($entryId, $thumbAssets[$thumbParamsId]);
  882. $this->kClient->thumbAsset->setContent($this->kClient->getMultiRequestResult()->id, $thumbAssetsResource);
  883. }else{
  884. $this->kClient->thumbAsset->update($existingthumbAssets[$thumbParamsId], $thumbAssets[$thumbParamsId]);
  885. $this->kClient->thumbAsset->setContent($existingthumbAssets[$thumbParamsId], $thumbAssetsResource);
  886. }
  887. }
  888. $requestResults = $this->kClient->doMultiRequest();
  889. $this->unimpersonate();
  890. return $updatedEntry;
  891. }
  892. /**
  893. * returns a flavor asset form the current content element
  894. * @param SimpleXMLElement $contentElement
  895. * @return KalturaFlavorAsset
  896. */
  897. protected function getFlavorAsset(SimpleXMLElement $contentElement, $conversionProfileId)
  898. {
  899. $flavorAsset = new KalturaFlavorAsset(); //we create a new asset (for add)
  900. $flavorAsset->flavorParamsId = $this->getFlavorParamsId($contentElement, $conversionProfileId, true);
  901. $flavorAsset->tags = $this->implodeChildElements($contentElement->tags);
  902. return $flavorAsset;
  903. }
  904. /**
  905. * returns a thumbnail asset form the current thumbnail element
  906. * @param SimpleXMLElement $thumbElement
  907. * @param int $conversionProfileId - The converrsion profile id
  908. * @return KalturaThumbAsset
  909. */
  910. protected function getThumbAsset(SimpleXMLElement $thumbElement, $conversionProfileId)
  911. {
  912. $thumbAsset = new KalturaThumbAsset();
  913. $thumbAsset->thumbParamsId = $this->getThumbParamsId($thumbElement, $conversionProfileId);
  914. if(isset($thumbElement["isDefault"]) && $thumbElement["isDefault"] == 'true') // if the attribute is set to true we add the is default tag to the thumb
  915. $thumbAsset->tags = self::DEFAULT_THUMB_TAG;
  916. $thumbAsset->tags = $this->implodeChildElements($thumbElement->tags, $thumbAsset->tags);
  917. return $thumbAsset;
  918. }
  919. /**
  920. * Validates if the resource is valid
  921. * @param KalturaResource $resource
  922. * @param SimpleXMLElement $elementToSearchIn
  923. */
  924. protected function validateResource(KalturaResource $resource = null, SimpleXMLElement $elementToSearchIn)
  925. {
  926. if(!$resource)
  927. return;
  928. //We only check for filesize and check sum in local files
  929. if($resource instanceof KalturaServerFileResource)
  930. {
  931. $filePath = $resource->localFilePath;
  932. if(is_null($filePath))
  933. {
  934. throw new KalturaBulkUploadXmlException("Can't validate file as file path is null", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  935. }
  936. if(isset($elementToSearchIn->serverFileContentResource->fileChecksum)) //Check checksum if exists
  937. {
  938. KalturaLog::debug("Validating checksum");
  939. if($elementToSearchIn->serverFileContentResource->fileChecksum['type'] == 'sha1')
  940. {
  941. $checksum = sha1_file($filePath);
  942. }
  943. else
  944. {
  945. $checksum = md5_file($filePath);
  946. }
  947. $xmlChecksum = (string)$elementToSearchIn->serverFileContentResource->fileChecksum;
  948. if($xmlChecksum != $checksum)
  949. {
  950. throw new KalturaBulkUploadXmlException("File checksum is invalid for file [$filePath], Xml checksum [$xmlChecksum], actual checksum [$checksum]", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  951. }
  952. }
  953. if(isset($elementToSearchIn->serverFileContentResource->fileSize)) //Check checksum if exists
  954. {
  955. KalturaLog::debug("Validating file size");
  956. $fileSize = filesize($filePath);
  957. $xmlFileSize = (int)$elementToSearchIn->serverFileContentResource->fileSize;
  958. if($xmlFileSize != $fileSize)
  959. {
  960. throw new KalturaBulkUploadXmlException("File size is invalid for file [$filePath], Xml size [$xmlFileSize], actual size [$fileSize]", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  961. }
  962. }
  963. }
  964. }
  965. /**
  966. * Gets an item and returns the resource
  967. * @param SimpleXMLElement $elementToSearchIn
  968. * @param int $conversionProfileId
  969. * @return KalturaResource - the resource located in the given element
  970. */
  971. public function getResource(SimpleXMLElement $elementToSearchIn, $conversionProfileId)
  972. {
  973. $resource = $this->getResourceInstance($elementToSearchIn, $conversionProfileId);
  974. if($resource)
  975. $this->validateResource($resource, $elementToSearchIn);
  976. return $resource;
  977. }
  978. /**
  979. * Returns the right resource instance for the source content of the item
  980. * @param SimpleXMLElement $elementToSearchIn
  981. * @param int $conversionProfileId
  982. * @return KalturaResource - the resource located in the given element
  983. */
  984. protected function getResourceInstance(SimpleXMLElement $elementToSearchIn, $conversionProfileId)
  985. {
  986. $resource = null;
  987. if(isset($elementToSearchIn->serverFileContentResource))
  988. {
  989. if($this->allowServerResource)
  990. {
  991. KalturaLog::debug("Resource is : serverFileContentResource");
  992. $resource = new KalturaServerFileResource();
  993. $localContentResource = $elementToSearchIn->serverFileContentResource;
  994. $resource->localFilePath = kXml::getXmlAttributeAsString($localContentResource, "filePath");
  995. }
  996. else
  997. {
  998. KalturaLog::err("serverFileContentResource is not allowed");
  999. }
  1000. }
  1001. elseif(isset($elementToSearchIn->urlContentResource))
  1002. {
  1003. KalturaLog::debug("Resource is : urlContentResource");
  1004. $resource = new KalturaUrlResource();
  1005. $urlContentResource = $elementToSearchIn->urlContentResource;
  1006. $resource->url = kXml::getXmlAttributeAsString($urlContentResource, "url");
  1007. }
  1008. elseif(isset($elementToSearchIn->remoteStorageContentResource))
  1009. {
  1010. KalturaLog::debug("Resource is : remoteStorageContentResource");
  1011. $resource = new KalturaRemoteStorageResource();
  1012. $remoteContentResource = $elementToSearchIn->remoteStorageContentResource;
  1013. $resource->url = kXml::getXmlAttributeAsString($remoteContentResource, "url");
  1014. $resource->storageProfileId = $this->getStorageProfileId($remoteContentResource);
  1015. }
  1016. elseif(isset($elementToSearchIn->remoteStorageContentResources))
  1017. {
  1018. KalturaLog::debug("Resource is : remoteStorageContentResources");
  1019. $resource = new KalturaRemoteStorageResources();
  1020. $resource->resources = array();
  1021. $remoteContentResources = $elementToSearchIn->remoteStorageContentResources;
  1022. foreach($remoteContentResources->remoteStorageContentResource as $remoteContentResource)
  1023. {
  1024. /* @var $remoteContentResource SimpleXMLElement */
  1025. KalturaLog::debug("Resources name [" . $remoteContentResource->getName() . "] url [" . $remoteContentResource['url'] . "] storage [$remoteContentResource->storageProfile]");
  1026. $childResource = new KalturaRemoteStorageResource();
  1027. $childResource->url = kXml::getXmlAttributeAsString($remoteContentResource, "url");
  1028. $childResource->storageProfileId = $this->getStorageProfileId($remoteContentResource);
  1029. $resource->resources[] = $childResource;
  1030. }
  1031. }
  1032. elseif(isset($elementToSearchIn->entryContentResource))
  1033. {
  1034. KalturaLog::debug("Resource is : entryContentResource");
  1035. $resource = new KalturaEntryResource();
  1036. $entryContentResource = $elementToSearchIn->entryContentResource;
  1037. $resource->entryId = kXml::getXmlAttributeAsString($entryContentResource, "entryId");
  1038. $resource->flavorParamsId = $this->getFlavorParamsId($entryContentResource, $conversionProfileId, false);
  1039. }
  1040. elseif(isset($elementToSearchIn->assetContentResource))
  1041. {
  1042. KalturaLog::debug("Resource is : assetContentResource");
  1043. $resource = new KalturaAssetResource();
  1044. $assetContentResource = $elementToSearchIn->assetContentResource;
  1045. $resource->assetId = kXml::getXmlAttributeAsString($assetContentResource, "assetId");
  1046. }
  1047. return $resource;
  1048. }
  1049. /**
  1050. * Gets the flavor params id from the given element
  1051. * @param $elementToSearchIn - The element to search in
  1052. * @param $conversionProfileId - The conversion profile on the item
  1053. * @return int - The id of the flavor params
  1054. */
  1055. protected function getFlavorParamsId(SimpleXMLElement $elementToSearchIn, $conversionProfileId, $isAttribute = true)
  1056. {
  1057. return $this->getAssetParamsId($elementToSearchIn, $conversionProfileId, $isAttribute, 'flavor');
  1058. }
  1059. /**
  1060. * Validates a given asset params id for the current partner
  1061. * @param int $assetParamsId - The asset id
  1062. * @param string $assetType - The asset type (flavor or thumb)
  1063. * @param $conversionProfileId - The conversion profile this asset relates to
  1064. * @throws KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED
  1065. */
  1066. protected function validateAssetParamsId($assetParamsId, $assetType, $conversionProfileId)
  1067. {
  1068. if(count($this->assetParamsNameToIdPerConversionProfile[$conversionProfileId]) == 0) //the name to id profiles weren't initialized
  1069. {
  1070. $this->initAssetParamsNameToId($conversionProfileId);
  1071. }
  1072. if(!in_array($assetParamsId, $this->assetParamsNameToIdPerConversionProfile[$conversionProfileId]))
  1073. {
  1074. throw new KalturaBatchException("Asset Params Id [$assetParamsId] not found for conversion profile [$conversionProfileId] ", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  1075. }
  1076. }
  1077. /**
  1078. * Gets the flavor params id from the given element
  1079. * @param SimpleXMLElement $elementToSearchIn - The element to search in
  1080. * @param int $conversionProfileId - The conversion profile on the item
  1081. * @param bool $isAttribute
  1082. * @param string $assetType flavor / thumb
  1083. * @return int - The id of the flavor params
  1084. */
  1085. public function getAssetParamsId(SimpleXMLElement $elementToSearchIn, $conversionProfileId, $isAttribute, $assetType)
  1086. {
  1087. $assetParams = "{$assetType}Params";
  1088. $assetParamsId = "{$assetParams}Id";
  1089. $assetParamsName = null;
  1090. if($isAttribute)
  1091. {
  1092. if(isset($elementToSearchIn[$assetParamsId]))
  1093. {
  1094. $this->validateAssetParamsId($elementToSearchIn[$assetParamsId], $assetType, $conversionProfileId);
  1095. return (int)$elementToSearchIn[$assetParamsId];
  1096. }
  1097. if(isset($elementToSearchIn[$assetParams]))
  1098. $assetParamsName = $elementToSearchIn[$assetParams];
  1099. }
  1100. else
  1101. {
  1102. if(isset($elementToSearchIn->$assetParamsId))
  1103. {
  1104. $this->validateAssetParamsId($elementToSearchIn->$assetParamsId, $assetType, $conversionProfileId);
  1105. return (int)$elementToSearchIn->$assetParamsId;
  1106. }
  1107. if(isset($elementToSearchIn->$assetParams))
  1108. $assetParamsName = $elementToSearchIn->$assetParams;
  1109. }
  1110. if(!$assetParamsName)
  1111. return null;
  1112. if(!isset($this->assetParamsNameToIdPerConversionProfile[$conversionProfileId]))
  1113. $this->initAssetParamsNameToId($conversionProfileId);
  1114. if(isset($this->assetParamsNameToIdPerConversionProfile["$conversionProfileId"]["$assetParamsName"]))
  1115. return $this->assetParamsNameToIdPerConversionProfile["$conversionProfileId"]["$assetParamsName"];
  1116. throw new KalturaBatchException("{$assetParams} system name [$assetParamsName] not found for conversion profile [$conversionProfileId] ", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  1117. }
  1118. /**
  1119. * Gets the ingestion profile id in this order:
  1120. * 1.from the element 2.from the data of the bulk 3.use default)
  1121. * @param SimpleXMLElement $elementToSearchIn
  1122. */
  1123. protected function getConversionProfileId(SimpleXMLElement $elementToSearchIn)
  1124. {
  1125. $conversionProfileId = $this->getConversionProfileIdFromElement($elementToSearchIn);
  1126. KalturaLog::debug("conversionProfileid from element [ $conversionProfileId ]");
  1127. if(is_null($conversionProfileId)) // if we didn't set it in the item element
  1128. {
  1129. $conversionProfileId = $this->data->conversionProfileId;
  1130. KalturaLog::debug("conversionProfileid from data [ $conversionProfileId ]");
  1131. }
  1132. if(is_null($conversionProfileId)) // if we didn't set it in the item element
  1133. {
  1134. //Gets the user default conversion
  1135. if(!isset($this->defaultConversionProfileId))
  1136. {
  1137. $this->impersonate();
  1138. $conversionProfile = $this->kClient->conversionProfile->getDefault();
  1139. $this->unimpersonate();
  1140. $this->defaultConversionProfileId = $conversionProfile->id;
  1141. }
  1142. $conversionProfileId = $this->defaultConversionProfileId;
  1143. KalturaLog::debug("conversionProfileid from default [ $conversionProfileId ]");
  1144. }
  1145. return $conversionProfileId;
  1146. }
  1147. /**
  1148. * Validates a given conversion profile id for the current partner
  1149. * @param int $converionProfileId
  1150. * @throws KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED
  1151. */
  1152. protected function validateConversionProfileId($converionProfileId)
  1153. {
  1154. if(count($this->conversionProfileNameToId) == 0) //the name to id profiles weren't initialized
  1155. {
  1156. $this->initConversionProfileNameToId();
  1157. }
  1158. if(!in_array($converionProfileId, $this->conversionProfileNameToId))
  1159. {
  1160. throw new KalturaBatchException("conversion profile Id [$converionProfileId] not found", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  1161. }
  1162. }
  1163. /**
  1164. * Validates a given storage profile id for the current partner
  1165. * @param int $storageProfileId
  1166. * @throws KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED
  1167. */
  1168. protected function validateStorageProfileId($storageProfileId)
  1169. {
  1170. if(count($this->storageProfileNameToId) == 0) //the name to id profiles weren't initialized
  1171. {
  1172. $this->initStorageProfileNameToId();
  1173. }
  1174. if(!in_array($storageProfileId, $this->storageProfileNameToId))
  1175. {
  1176. throw new KalturaBatchException("Storage profile id [$storageProfileId] not found", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  1177. }
  1178. }
  1179. /**
  1180. * Gets the coversion profile id from the given element
  1181. * @param $elementToSearchIn - The element to search in
  1182. * @return int - The id of the ingestion profile params
  1183. */
  1184. protecte

Large files files are truncated, but you can click here to view the full file