PageRenderTime 61ms CodeModel.GetById 15ms RepoModel.GetById 0ms 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
  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. protected function getConversionProfileIdFromElement(SimpleXMLElement $elementToSearchIn)
  1185. {
  1186. if(isset($elementToSearchIn->conversionProfileId))
  1187. {
  1188. $this->validateConversionProfileId((int)$elementToSearchIn->conversionProfileId);
  1189. return (int)$elementToSearchIn->conversionProfileId;
  1190. }
  1191. if(!isset($elementToSearchIn->conversionProfile))
  1192. return null;
  1193. if(!isset($this->conversionProfileNameToId["$elementToSearchIn->conversionProfile"]))
  1194. {
  1195. $this->initConversionProfileNameToId();
  1196. }
  1197. if(isset($this->conversionProfileNameToId["$elementToSearchIn->conversionProfile"]))
  1198. return $this->conversionProfileNameToId["$elementToSearchIn->conversionProfile"];
  1199. throw new KalturaBatchException("conversion profile system name [{$elementToSearchIn->conversionProfile}] not valid", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  1200. }
  1201. /**
  1202. * Gets the thumb params id from the given element
  1203. * @param $elementToSearchIn - The element to search in
  1204. * @param $conversionProfileId - The conversion profile id
  1205. * @param $isAttribute - bool
  1206. * @return int - The id of the thumb params
  1207. */
  1208. protected function getThumbParamsId(SimpleXMLElement $elementToSearchIn, $conversionProfileId, $isAttribute = true)
  1209. {
  1210. return $this->getAssetParamsId($elementToSearchIn, $conversionProfileId, $isAttribute, 'thumb');
  1211. }
  1212. /**
  1213. * Validates a given access control id for the current partner
  1214. * @param int $accessControlId
  1215. * @throws KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED
  1216. */
  1217. protected function validateAccessControlId($accessControlId)
  1218. {
  1219. if(count($this->accessControlNameToId) == 0) //the name to id profiles weren't initialized
  1220. {
  1221. $this->initAccessControlNameToId();
  1222. }
  1223. if(!in_array($accessControlId, $this->accessControlNameToId))
  1224. {
  1225. throw new KalturaBatchException("access control Id [$accessControlId] not valid", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  1226. }
  1227. }
  1228. /**
  1229. * Gets the flavor params id from the source content element
  1230. * @param $elementToSearchIn - The element to search in
  1231. * @return int - The id of the flavor params
  1232. */
  1233. protected function getAccessControlId(SimpleXMLElement $elementToSearchIn)
  1234. {
  1235. if(isset($elementToSearchIn->accessControlId))
  1236. {
  1237. $this->validateAccessControlId($elementToSearchIn->accessControlId);
  1238. return (int)$elementToSearchIn->accessControlId;
  1239. }
  1240. if(!isset($elementToSearchIn->accessControl))
  1241. return null;
  1242. if(is_null($this->accessControlNameToId))
  1243. {
  1244. $this->initAccessControlNameToId();
  1245. }
  1246. if(isset($this->accessControlNameToId["$elementToSearchIn->accessControl"]))
  1247. return trim($this->accessControlNameToId["$elementToSearchIn->accessControl"]);
  1248. throw new KalturaBatchException("access control system name [{$elementToSearchIn->accessControl}] not found", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  1249. }
  1250. /**
  1251. * Gets the storage profile id from the source content element
  1252. * @param $elementToSearchIn - The element to search in
  1253. * @return int - The id of the storage profile
  1254. */
  1255. protected function getStorageProfileId(SimpleXMLElement $elementToSearchIn)
  1256. {
  1257. if(isset($elementToSearchIn->storageProfileId))
  1258. {
  1259. $this->validateStorageProfileId($elementToSearchIn->storageProfileId);
  1260. return (int)$elementToSearchIn->storageProfileId;
  1261. }
  1262. if(!isset($elementToSearchIn->storageProfile))
  1263. return null;
  1264. if(is_null($this->storageProfileNameToId))
  1265. {
  1266. $this->initStorageProfileNameToId();
  1267. }
  1268. if(isset($this->storageProfileNameToId["$elementToSearchIn->storageProfile"]))
  1269. return trim($this->storageProfileNameToId["$elementToSearchIn->storageProfile"]);
  1270. throw new KalturaBatchException("storage profile system name [{$elementToSearchIn->storageProfileId}] not found", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  1271. }
  1272. /**
  1273. * Inits the array of flavor params name to Id (with all given flavor params)
  1274. * @param $coversionProfileId - The conversion profile for which we ini the arrays for
  1275. */
  1276. protected function initAssetParamsNameToId($conversionProfileId)
  1277. {
  1278. $conversionProfileFilter = new KalturaConversionProfileAssetParamsFilter();
  1279. $conversionProfileFilter->conversionProfileIdEqual = $conversionProfileId;
  1280. $pager = new KalturaFilterPager();
  1281. $pager->pageSize = 500;
  1282. $this->impersonate();
  1283. $allFlavorParams = $this->kClient->conversionProfileAssetParams->listAction($conversionProfileFilter, $pager);
  1284. $this->unimpersonate();
  1285. $allFlavorParams = $allFlavorParams->objects;
  1286. // KalturaLog::debug("allFlavorParams [" . print_r($allFlavorParams, true). "]");
  1287. foreach ($allFlavorParams as $flavorParams)
  1288. {
  1289. if($flavorParams->systemName)
  1290. $this->assetParamsNameToIdPerConversionProfile[$conversionProfileId][$flavorParams->systemName] = $flavorParams->assetParamsId;
  1291. else //NO system name so we add them to a default name
  1292. $this->assetParamsNameToIdPerConversionProfile[$conversionProfileId]["NO SYSTEM NAME $flavorParams->assetParamsId"] = $flavorParams->assetParamsId;
  1293. }
  1294. // KalturaLog::debug("new assetParamsNameToIdPerConversionProfile [" . print_r($this->assetParamsNameToIdPerConversionProfile, true). "]");
  1295. }
  1296. /**
  1297. * Inits the array of access control name to Id (with all given flavor params)
  1298. */
  1299. protected function initAccessControlNameToId()
  1300. {
  1301. $pager = new KalturaFilterPager();
  1302. $pager->pageSize = 500;
  1303. $this->impersonate();
  1304. $allAccessControl = $this->kClient->accessControl->listAction(null, $pager);
  1305. $this->unimpersonate();
  1306. $allAccessControl = $allAccessControl->objects;
  1307. // KalturaLog::debug("allAccessControl [" . print_r($allAccessControl, true). "]");
  1308. foreach ($allAccessControl as $accessControl)
  1309. {
  1310. if($accessControl->systemName)
  1311. $this->accessControlNameToId[$accessControl->systemName] = $accessControl->id;
  1312. else //NO system name so we add them to a default name
  1313. $this->accessControlNameToId["No system name " ."$accessControl->id"] = $accessControl->id;
  1314. }
  1315. // KalturaLog::debug("new accessControlNameToId [" . print_r($this->accessControlNameToId, true). "]");
  1316. }
  1317. /**
  1318. * Inits the array of access control name to Id (with all given flavor params)
  1319. * @param $entryId - the entry id to take the flavor assets from
  1320. */
  1321. protected function initAssetIdToAssetParamsId($entryId)
  1322. {
  1323. $this->impersonate();
  1324. $allFlavorAssets = $this->kClient->flavorAsset->getByEntryId($entryId);
  1325. $allThumbAssets = $this->kClient->thumbAsset->getByEntryId($entryId);
  1326. $this->unimpersonate();
  1327. // KalturaLog::debug("allFlavorAssets [" . print_r($allFlavorAssets, true). "]");
  1328. // KalturaLog::debug("allThumbAssets [" . print_r($allThumbAssets, true). "]");
  1329. foreach ($allFlavorAssets as $flavorAsset)
  1330. {
  1331. if(!is_null($flavorAsset->id)) //Should always have an id
  1332. $this->assetIdToAssetParamsId[$entryId][$flavorAsset->id] = $flavorAsset->flavorParamsId;
  1333. }
  1334. foreach ($allThumbAssets as $thumbAsset)
  1335. {
  1336. if(!is_null($thumbAsset->id)) //Should always have an id
  1337. $this->assetIdToAssetParamsId[$entryId][$thumbAsset->id] = $thumbAsset->thumbParamsId;
  1338. }
  1339. // KalturaLog::debug("new assetIdToAssetParamsId [" . print_r($this->assetIdToAssetParamsId, true). "]");
  1340. }
  1341. /**
  1342. * Inits the array of conversion profile name to Id (with all given flavor params)
  1343. */
  1344. protected function initConversionProfileNameToId()
  1345. {
  1346. $pager = new KalturaFilterPager();
  1347. $pager->pageSize = 500;
  1348. $this->impersonate();
  1349. $allConversionProfile = $this->kClient->conversionProfile->listAction(null, $pager);
  1350. $this->unimpersonate();
  1351. $allConversionProfile = $allConversionProfile->objects;
  1352. // KalturaLog::debug("allConversionProfile [" . print_r($allConversionProfile,true) ." ]");
  1353. foreach ($allConversionProfile as $conversionProfile)
  1354. {
  1355. $systemName = $conversionProfile->systemName;
  1356. if($systemName)
  1357. $this->conversionProfileNameToId[$systemName] = $conversionProfile->id;
  1358. else //NO system name so we add them to a default name
  1359. $this->conversionProfileNameToId["No system name " ."{$conversionProfile->id}"] = $conversionProfile->id;
  1360. }
  1361. // KalturaLog::debug("new conversionProfileNameToId [" . print_r($this->conversionProfileNameToId, true). "]");
  1362. }
  1363. /**
  1364. * Inits the array of storage profile to Id (with all given flavor params)
  1365. */
  1366. protected function initStorageProfileNameToId()
  1367. {
  1368. $pager = new KalturaFilterPager();
  1369. $pager->pageSize = 500;
  1370. $this->impersonate();
  1371. $allStorageProfiles = $this->kClient->storageProfile->listAction(null, $pager);
  1372. $this->unimpersonate();
  1373. $allStorageProfiles = $allStorageProfiles->objects;
  1374. // KalturaLog::debug("allStorageProfiles [" . print_r($allStorageProfiles,true) ." ]");
  1375. foreach ($allStorageProfiles as $storageProfile)
  1376. {
  1377. if($storageProfile->systemName)
  1378. $this->storageProfileNameToId["$storageProfile->systemName"] = $storageProfile->id;
  1379. else //NO system name so we add them to a default name
  1380. $this->storageProfileNameToId["No system name " ."{$storageProfile->id}"] = $storageProfile->id;
  1381. }
  1382. // KalturaLog::debug("new storageProfileNameToId [" . print_r($this->storageProfileNameToId, true). "]");
  1383. }
  1384. /**
  1385. * Creates and returns a new media entry for the given job data and bulk upload result object
  1386. * @param SimpleXMLElement $bulkUploadResult
  1387. * @param int $type
  1388. * @return KalturaBaseEntry
  1389. */
  1390. protected function createEntryFromItem(SimpleXMLElement $item, $type = null)
  1391. {
  1392. //Create the new media entry and set basic values
  1393. if(isset($item->type))
  1394. $entryType = $item->type;
  1395. elseif($type)
  1396. $entryType = $type;
  1397. else
  1398. throw new KalturaBulkUploadXmlException("entry type must be set to a value on item [$item->name] ", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  1399. $entry = $this->getEntryInstanceByType($entryType);
  1400. $entry->type = (int)$item->type;
  1401. if(isset($item->referenceId))
  1402. $entry->referenceId = (string)$item->referenceId;
  1403. if(isset($item->name))
  1404. $entry->name = (string)$item->name;
  1405. if(isset($item->description))
  1406. $entry->description = (string)$item->description;
  1407. if(isset($item->tags))
  1408. $entry->tags = $this->implodeChildElements($item->tags);
  1409. if(isset($item->categories))
  1410. $entry->categories = $this->implodeChildElements($item->categories);
  1411. if(isset($item->userId))
  1412. $entry->userId = (string)$item->userId;;
  1413. if(isset($item->licenseType))
  1414. $entry->licenseType = (string)$item->licenseType;
  1415. if(isset($item->partnerData))
  1416. $entry->partnerData = (string)$item->partnerData;
  1417. if(isset($item->partnerSortData))
  1418. $entry->partnerSortValue = (string)$item->partnerSortData;
  1419. if(isset($item->accessControlId) || isset($item->accessControl))
  1420. $entry->accessControlId = $this->getAccessControlId($item);
  1421. if(isset($item->startDate))
  1422. $entry->startDate = self::parseFormatedDate((string)$item->startDate);
  1423. if(isset($item->endDate))
  1424. $entry->endDate = self::parseFormatedDate((string)$item->endDate);
  1425. if(isset($item->conversionProfileId) || isset($item->conversionProfile))
  1426. $entry->conversionProfileId = $this->getConversionProfileId($item);
  1427. if(($entry instanceof KalturaPlayableEntry) && isset($item->msDuration))
  1428. $entry->msDuration = (int)$item->msDuration;
  1429. return $entry;
  1430. }
  1431. /**
  1432. * Returns the right entry instace by the given item type
  1433. * @param int $item
  1434. * @return KalturaBaseEntry
  1435. */
  1436. protected function getEntryInstanceByType($type)
  1437. {
  1438. switch(trim($type))
  1439. {
  1440. case KalturaEntryType::MEDIA_CLIP :
  1441. return new KalturaMediaEntry();
  1442. case KalturaEntryType::DATA:
  1443. return new KalturaDataEntry();
  1444. case KalturaEntryType::LIVE_STREAM:
  1445. return new KalturaLiveStreamEntry();
  1446. case KalturaEntryType::MIX:
  1447. return new KalturaMixEntry();
  1448. case KalturaEntryType::PLAYLIST:
  1449. return new KalturaPlaylist();
  1450. default:
  1451. return new KalturaBaseEntry();
  1452. }
  1453. }
  1454. /**
  1455. * Handles the type additional data for the given item
  1456. * @param KalturaBaseEntry $media
  1457. * @param SimpleXMLElement $item
  1458. */
  1459. protected function handleTypedElement(KalturaBaseEntry $entry, SimpleXMLElement $item)
  1460. {
  1461. // TODO take type from ingestion profile default entry
  1462. switch ($entry->type)
  1463. {
  1464. case KalturaEntryType::MEDIA_CLIP:
  1465. $this->setMediaElementValues($entry, $item);
  1466. break;
  1467. case KalturaEntryType::MIX:
  1468. $this->setMixElementValues($entry, $item);
  1469. break;
  1470. case KalturaEntryType::DATA:
  1471. $this->setDataElementValues($entry, $item);
  1472. break;
  1473. case KalturaEntryType::LIVE_STREAM:
  1474. $this->setLiveStreamElementValues($entry, $item);
  1475. break;
  1476. case KalturaEntryType::PLAYLIST:
  1477. $this->setPlaylistElementValues($entry, $item);
  1478. break;
  1479. default:
  1480. $entry->type = KalturaEntryType::AUTOMATIC;
  1481. break;
  1482. }
  1483. }
  1484. /**
  1485. * Check if the item type and the type element are matching
  1486. * @param SimpleXMLElement $item
  1487. * @throws KalturaBatchException - KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED ;
  1488. */
  1489. protected function validateTypeToTypedElement(SimpleXMLElement $item)
  1490. {
  1491. $typeNumber = $item->type;
  1492. $typeNumber = trim($typeNumber);
  1493. if(isset($item->media) && $item->type != KalturaEntryType::MEDIA_CLIP)
  1494. throw new KalturaBulkUploadXmlException("Conflicted typed element for type [$typeNumber] on item [$item->name] ", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  1495. if(isset($item->mix) && $item->type != KalturaEntryType::MIX)
  1496. throw new KalturaBulkUploadXmlException("Conflicted typed element for type [$typeNumber] on item [$item->name] ", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  1497. if(isset($item->playlist) && $item->type != KalturaEntryType::PLAYLIST)
  1498. throw new KalturaBulkUploadXmlException("Conflicted typed element for type [$typeNumber] on item [$item->name] ", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  1499. if(isset($item->liveStream) && $item->type != KalturaEntryType::LIVE_STREAM)
  1500. throw new KalturaBulkUploadXmlException("Conflicted typed element for type [$typeNumber] on item [$item->name] ", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  1501. if(isset($item->data) && $item->type != KalturaEntryType::DATA)
  1502. throw new KalturaBulkUploadXmlException("Conflicted typed element for type [$typeNumber] on item [$item->name] ", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
  1503. }
  1504. /**
  1505. * Sets the media values in the media entry according to the given item node
  1506. * @param KalturaMediaEntry $media
  1507. * @param SimpleXMLElement $itemElement
  1508. */
  1509. protected function setMediaElementValues(KalturaMediaEntry $media, SimpleXMLElement $itemElement)
  1510. {
  1511. if(!isset($itemElement->media))
  1512. return;
  1513. $mediaElement = $itemElement->media;
  1514. if(isset($mediaElement->mediaType))
  1515. {
  1516. $media->mediaType = (int)$mediaElement->mediaType;
  1517. $this->validateMediaTypes($media->mediaType);
  1518. }
  1519. }
  1520. /**
  1521. * Sets the playlist values in the live stream entry according to the given item node
  1522. * @param KalturaPlaylist $playlistEntry
  1523. * @param SimpleXMLElement $itemElement
  1524. */
  1525. protected function setPlaylistElementValues(KalturaPlaylist $playlistEntry, SimpleXMLElement $itemElement)
  1526. {
  1527. $playlistElement = $itemElement->playlist;
  1528. $playlistEntry->playlistType = (int)$playlistElement->playlistType;
  1529. $playlistEntry->playlistContent = (string)$playlistElement->playlistContent;
  1530. }
  1531. /**
  1532. * Sets the live stream values in the live stream entry according to the given item node
  1533. * @param KalturaLiveStreamEntry $liveStreamEntry
  1534. * @param SimpleXMLElement $itemElement
  1535. */
  1536. protected function setLiveStreamElementValues(KalturaLiveStreamEntry $liveStreamEntry, SimpleXMLElement $itemElement)
  1537. {
  1538. $liveStreamElement = $itemElement->liveStream;
  1539. $liveStreamEntry->bitrates = (int)$liveStreamElement->bitrates;
  1540. //What to do with those?
  1541. // $liveStreamEntry->encodingIP1 = $dataElement->encodingIP1;
  1542. // $liveStreamEntry->encodingIP2 = $dataElement->encodingIP2;
  1543. // $liveStreamEntry->streamPassword = $dataElement->streamPassword
  1544. }
  1545. /**
  1546. * Sets the data values in the data entry according to the given item node
  1547. * @param KalturaDataEntry $dataEntry
  1548. * @param SimpleXMLElement $itemElement
  1549. */
  1550. protected function setDataElementValues(KalturaDataEntry $dataEntry, SimpleXMLElement $itemElement)
  1551. {
  1552. $dataElement = $itemElement->media;
  1553. $dataEntry->dataContent = (string)$dataElement->dataContent;
  1554. $dataEntry->retrieveDataContentByGet = (bool)$dataElement->retrieveDataContentByGet;
  1555. }
  1556. /**
  1557. * Sets the mix values in the mix entry according to the given item node
  1558. * @param KalturaMixEntry $mix
  1559. * @param SimpleXMLElement $itemElement
  1560. */
  1561. protected function setMixElementValues(KalturaMixEntry $mix, SimpleXMLElement $itemElement)
  1562. {
  1563. //TOOD: add support for the mix elements
  1564. $mixElement = $itemElement->mix;
  1565. $mix->editorType = $mixElement->editorType;
  1566. $mix->dataContent = $mixElement->dataContent;
  1567. }
  1568. /**
  1569. * Checks if the media type and the type are valid
  1570. * @param KalturaMediaType $mediaType
  1571. */
  1572. protected function validateMediaTypes($mediaType)
  1573. {
  1574. $mediaTypes = array(
  1575. KalturaMediaType::LIVE_STREAM_FLASH,
  1576. KalturaMediaType::LIVE_STREAM_QUICKTIME,
  1577. KalturaMediaType::LIVE_STREAM_REAL_MEDIA,
  1578. KalturaMediaType::LIVE_STREAM_WINDOWS_MEDIA
  1579. );
  1580. //TODO: use this function
  1581. if(in_array($mediaType, $mediaTypes))
  1582. return false;
  1583. if($mediaType == KalturaMediaType::IMAGE)
  1584. {
  1585. // TODO - make sure that there are no flavors or thumbnails in the XML
  1586. }
  1587. return true;
  1588. }
  1589. /**
  1590. * Adds the given media entry to the given playlists in the element
  1591. * @param SimpleXMLElement $playlistsElement
  1592. */
  1593. protected function addToPlaylists(SimpleXMLElement $playlistsElement)
  1594. {
  1595. foreach ($playlistsElement->children() as $playlistElement)
  1596. {
  1597. //TODO: Roni - add the media to the play list not supported
  1598. //AddToPlaylist();
  1599. }
  1600. }
  1601. /**
  1602. * Returns a comma seperated string with the values of the child nodes of the given element
  1603. * @param SimpleXMLElement $element
  1604. */
  1605. public function implodeChildElements(SimpleXMLElement $element, $baseValues = null)
  1606. {
  1607. $ret = array();
  1608. if($baseValues)
  1609. $ret = explode(',', $baseValues);
  1610. if(empty($element))
  1611. return $baseValues;
  1612. foreach ($element->children() as $child)
  1613. {
  1614. if(is_null($child))
  1615. continue;
  1616. $value = trim("$child");
  1617. if($value)
  1618. $ret[] = $value;
  1619. }
  1620. $ret = implode(',', $ret);
  1621. KalturaLog::debug("The created string [$ret]");
  1622. return $ret;
  1623. }
  1624. /**
  1625. * Gets the entry status from the given item
  1626. * @param unknown_type $item
  1627. * @return KalturaEntryStatus - the new entry status
  1628. */
  1629. protected function getEntryStatusFromItem(SimpleXMLElement $item)
  1630. {
  1631. $status = KalturaEntryStatus::IMPORT;
  1632. if($item->action == self::$actionsMap[KalturaBulkUploadAction::ADD])
  1633. $status = KalturaEntryStatus::DELETED;
  1634. return $status;
  1635. }
  1636. /**
  1637. * Creates a new upload result object from the given SimpleXMLElement item
  1638. * @param SimpleXMLElement $item
  1639. * @param KalturaBulkUploadAction $action
  1640. * @return KalturaBulkUploadResult
  1641. */
  1642. protected function createUploadResult(SimpleXMLElement $item, $action)
  1643. {
  1644. if($this->handledRecordsThisRun >= $this->maxRecordsEachRun)
  1645. {
  1646. $this->exceededMaxRecordsEachRun = true;
  1647. return;
  1648. }
  1649. //TODO: What should we write in the bulk upload result for update?
  1650. //only the changed parameters or just the one theat was changed
  1651. // KalturaLog::debug("Creating upload result");
  1652. KalturaLog::debug("this->handledRecordsThisRun [$this->handledRecordsThisRun], this->maxRecordsEachRun [$this->maxRecordsEachRun]");
  1653. $bulkUploadResult = new KalturaBulkUploadResult();
  1654. $bulkUploadResult->action = $action;
  1655. $bulkUploadResult->bulkUploadJobId = $this->job->id;
  1656. $bulkUploadResult->lineIndex = $this->currentItem;
  1657. $bulkUploadResult->partnerId = $this->job->partnerId;
  1658. $bulkUploadResult->rowData = $item->asXml();
  1659. $bulkUploadResult->entryStatus = $this->getEntryStatusFromItem($item);
  1660. $bulkUploadResult->conversionProfileId = $this->getConversionProfileId($item);
  1661. $bulkUploadResult->accessControlProfileId = $this->getAccessControlId($item);
  1662. if(!is_numeric($bulkUploadResult->conversionProfileId))
  1663. $bulkUploadResult->conversionProfileId = null;
  1664. if(!is_numeric($bulkUploadResult->accessControlProfileId))
  1665. $bulkUploadResult->accessControlProfileId = null;
  1666. if(isset($item->startDate))
  1667. {
  1668. if((string)$item->startDate && !self::isFormatedDate((string)$item->startDate))
  1669. {
  1670. $bulkUploadResult->entryStatus = KalturaEntryStatus::ERROR_IMPORTING;
  1671. $bulkUploadResult->errorDescription = "Invalid schedule start date {$item->startDate} on item $item->name";
  1672. }
  1673. }
  1674. if(isset($item->endDate))
  1675. {
  1676. if((string)$item->endDate && !self::isFormatedDate((string)$item->endDate))
  1677. {
  1678. $bulkUploadResult->entryStatus = KalturaEntryStatus::ERROR_IMPORTING;
  1679. $bulkUploadResult->errorDescription = "Invalid schedule end date {$item->endDate} on item $item->name";
  1680. }
  1681. }
  1682. if($bulkUploadResult->entryStatus == KalturaEntryStatus::ERROR_IMPORTING)
  1683. {
  1684. $this->addBulkUploadResult($bulkUploadResult);
  1685. return;
  1686. }
  1687. $bulkUploadResult->scheduleStartDate = self::parseFormatedDate((string)$item->startDate);
  1688. $bulkUploadResult->scheduleEndDate = self::parseFormatedDate((string)$item->endDate);
  1689. return $bulkUploadResult;
  1690. }
  1691. }