/** * @param KalturaMetadataObjectType $objectType * @param BaseObject $fromObject * @param BaseObject $toObject */ protected function copyMetadata($objectType, BaseObject $fromObject, BaseObject $toObject) { KalturaLog::debug("Copy metadata type [{$objectType}] from " . get_class($fromObject) . '[' . $fromObject->getId() . "] to[" . $toObject->getId() . "]"); $c = new Criteria(); $c->add(MetadataPeer::OBJECT_TYPE, $objectType); $c->add(MetadataPeer::OBJECT_ID, $fromObject->getId()); $metadatas = MetadataPeer::doSelect($c); foreach ($metadatas as $metadata) { $newMetadata = $metadata->copy(); $newMetadata->setObjectId($toObject->getId()); if ($toObject instanceof Partner) { $newMetadata->setPartnerId($toObject->getId()); } else { $newMetadata->setPartnerId($toObject->getPartnerId()); } $metadataProfileId = kObjectCopyHandler::getMappedId('MetadataProfile', $metadata->getMetadataProfileId()); if ($metadataProfileId) { $metadataProfile = MetadataProfilePeer::retrieveByPK($metadataProfileId); if ($metadataProfile) { $newMetadata->setMetadataProfileId($metadataProfileId); $newMetadata->setMetadataProfileVersion($metadataProfile->getVersion()); } } $newMetadata->save(); kFileSyncUtils::createSyncFileLinkForKey($newMetadata->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA), $metadata->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA)); } }
/** * Adds a metadata object associated with Kaltura object * * @param int $metadataProfileId * @param KalturaMetadataObjectType $objectType * @param string $objectId * @return Metadata * @throws MetadataErrors::METADATA_ALREADY_EXISTS * @throws MetadataErrors::INVALID_METADATA_PROFILE * @throws MetadataErrors::INVALID_METADATA_OBJECT */ protected function addMetadata($metadataProfileId, $objectType, $objectId) { $check = MetadataPeer::retrieveByObject($metadataProfileId, $objectType, $objectId); if ($check) { throw new KalturaAPIException(MetadataErrors::METADATA_ALREADY_EXISTS, $check->getId()); } $dbMetadataProfile = MetadataProfilePeer::retrieveByPK($metadataProfileId); if (!$dbMetadataProfile) { throw new KalturaAPIException(MetadataErrors::INVALID_METADATA_PROFILE, $metadataProfileId); } $dbMetadata = new Metadata(); $dbMetadata->setPartnerId($this->getPartnerId()); $dbMetadata->setMetadataProfileId($metadataProfileId); $dbMetadata->setMetadataProfileVersion($dbMetadataProfile->getVersion()); $dbMetadata->setObjectType($objectType); $dbMetadata->setObjectId($objectId); $dbMetadata->setStatus(KalturaMetadataStatus::INVALID); // validate object exists $object = kMetadataManager::getObjectFromPeer($dbMetadata); if (!$object) { throw new KalturaAPIException(MetadataErrors::INVALID_METADATA_OBJECT, $objectId); } $dbMetadata->save(); $this->deleteOldVersions($dbMetadata); return $dbMetadata; }
/** * @param string $baseClass * @param string $enumValue * @param array $constructorArgs * @return object */ public static function loadObject($baseClass, $enumValue, array $constructorArgs = null) { if ($baseClass == 'ISyncableFile' && isset($constructorArgs['objectId'])) { $objectId = $constructorArgs['objectId']; switch ($enumValue) { case FileSyncObjectType::METADATA: MetadataPeer::setUseCriteriaFilter(false); $object = MetadataPeer::retrieveByPK($objectId); MetadataPeer::setUseCriteriaFilter(true); return $object; case FileSyncObjectType::METADATA_PROFILE: MetadataProfilePeer::setUseCriteriaFilter(false); $object = MetadataProfilePeer::retrieveByPK($objectId); MetadataProfilePeer::setUseCriteriaFilter(true); return $object; } } if ($baseClass == 'kJobData') { switch ($enumValue) { case KalturaBatchJobType::METADATA_IMPORT: return new kImportJobData(); case KalturaBatchJobType::METADATA_TRANSFORM: return new kTransformMetadataJobData(); } } if ($baseClass == 'KalturaJobData') { switch ($enumValue) { case KalturaBatchJobType::METADATA_IMPORT: return new KalturaImportJobData(); case KalturaBatchJobType::METADATA_TRANSFORM: return new KalturaTransformMetadataJobData(); } } return null; }
public function validateForUsage($sourceObject, $propertiesToSkip = array()) { parent::validateForUsage($sourceObject, $propertiesToSkip); $this->validatePropertyNotNull('metadataProfileId'); $this->validatePropertyNotNull('metadataObjectType'); $this->validatePropertyNotNull('xslt'); myPartnerUtils::addPartnerToCriteria('MetadataProfile', kCurrentContext::getCurrentPartnerId(), true); $metadataProfile = MetadataProfilePeer::retrieveByPK($this->metadataProfileId); if (is_null($metadataProfile)) { throw new KalturaAPIException(MetadataErrors::METADATA_PROFILE_NOT_FOUND, $this->metadataProfileId); } }
/** * Retrieve all metadta object by profile. * * @param int $metadataProfileId * @param int $metadataProfileVersion * @param PropelPDO $con the connection to use * @return array<Metadata> */ public static function retrieveByProfile($metadataProfileId, $metadataProfileVersion = null, PropelPDO $con = null) { if (is_null($metadataProfileVersion)) { $metadataProfile = MetadataProfilePeer::retrieveByPK($metadataProfileId, $con); if (!$metadataProfile) { return null; } $metadataProfileVersion = $metadataProfile->getVersion(); } $criteria = new Criteria(); $criteria->add(MetadataPeer::METADATA_PROFILE_ID, $metadataProfileId); $criteria->add(MetadataPeer::METADATA_PROFILE_VERSION, $metadataProfileVersion); return MetadataPeer::doSelect($criteria, $con); }
protected function updatedTransformMetadataFailed(BatchJob $dbBatchJob, kTransformMetadataJobData $data) { if (!$data->getMetadataProfileId()) { return $dbBatchJob; } $metadataProfile = MetadataProfilePeer::retrieveByPK($data->getMetadataProfileId()); if (!$metadataProfile) { return $dbBatchJob; } if ($data->getSrcXslPath()) { $metadataProfile->setStatus(MetadataProfile::STATUS_DEPRECATED); $metadataProfile->save(); } return $dbBatchJob; }
public static function validateMetadataObjects($profileField, $objectIds, &$errorMessage) { /** @var MetadataProfileField $profileField */ $subMetadataProfileId = $profileField->getRelatedMetadataProfileId(); $subMetadataProfile = MetadataProfilePeer::retrieveByPK($subMetadataProfileId); if (!$subMetadataProfile) { $errorMessage = 'Sub metadata profile ' . $subMetadataProfileId . ' was not found'; return false; } $subMetadataObjects = MetadataPeer::retrieveByObjects($subMetadataProfileId, $subMetadataProfile->getObjectType(), $objectIds); if (count($subMetadataObjects) != count($objectIds)) { $errorMessage = 'One of the following objects: ' . implode(', ', $objectIds) . ' was not found for profile ' . $subMetadataProfileId; return false; } return true; }
/** * batch getExclusiveTransformMetadataJob action allows to get a BatchJob of type METADATA_TRANSFORM * * @action getExclusiveTransformMetadataJobs * @param KalturaExclusiveLockKey $lockKey The unique lock key from the batch-process. Is used for the locking mechanism * @param int $maxExecutionTime The maximum time in seconds the job reguarly take. Is used for the locking mechanism when determining an unexpected termination of a batch-process. * @param int $numberOfJobs The maximum number of jobs to return. * @param KalturaBatchJobFilter $filter Set of rules to fetch only rartial list of jobs * @return KalturaBatchJobArray */ function getExclusiveTransformMetadataJobsAction(KalturaExclusiveLockKey $lockKey, $maxExecutionTime, $numberOfJobs, KalturaBatchJobFilter $filter = null) { $jobs = $this->getExclusiveJobs($lockKey, $maxExecutionTime, $numberOfJobs, $filter, BatchJobType::METADATA_TRANSFORM); if ($jobs) { foreach ($jobs as &$job) { $data = $job->getData(); $metadataProfileId = $data->getMetadataProfileId(); $metadataProfile = MetadataProfilePeer::retrieveByPK($metadataProfileId); if (!$metadataProfile) { continue; } $key = $metadataProfile->getSyncKey(MetadataProfile::FILE_SYNC_METADATA_DEFINITION); $xsdPath = kFileSyncUtils::getLocalFilePathForKey($key); $data->setDestXsdPath($xsdPath); $job->setData($data); } } return KalturaBatchJobArray::fromBatchJobArray($jobs); }
private function transformMetadata($metadataProfileId, $xmlData) { $result = null; $metadataProfile = MetadataProfilePeer::retrieveByPK($metadataProfileId); if (!$metadataProfile) { KalturaLog::err('Cannot find metadata profile id [' . $metadataProfileId . ']'); return null; } $metadataXsltKey = $metadataProfile->getSyncKey(MetadataProfile::FILE_SYNC_METADATA_XSLT); if (!kFileSyncUtils::file_exists($metadataXsltKey, true)) { return null; } $xsltString = kFileSyncUtils::file_get_contents($metadataXsltKey, true, false); if (!$xsltString) { return null; } $xsltParams = array(XsltParameterName::KALTURA_CURRENT_TIMESTAMP => time()); $xsltErrors = array(); $xmlDataTransformed = kXml::transformXmlUsingXslt($xmlData, $xsltString, $xsltParams, $xsltErrors); if (!empty($xsltErrors)) { throw new KalturaAPIException(MetadataErrors::XSLT_VALIDATION_ERROR, implode(',', $xsltErrors)); } if ($xmlDataTransformed) { return $xmlDataTransformed; } KalturaLog::err('Failed XML [$xmlData] transformation for metadata with XSL [$xsltString]'); return null; }
public static function parseXml($objectType, SimpleXMLElement $scene, $partnerId, CuePoint $cuePoint) { $metadataElements = $scene->xpath('scene-customData'); if (!count($metadataElements)) { return $cuePoint; } foreach ($metadataElements as $metadataElement) { $metadata = null; $metadataProfile = null; if (isset($metadataElement['metadataId'])) { $metadata = MetadataPeer::retrieveByPK($metadataElement['metadataId']); } if ($metadata) { $metadataProfile = $metadata->getMetadataProfile(); } else { if (isset($metadataElement['metadataProfileId'])) { $metadataProfile = MetadataProfilePeer::retrieveByPK($metadataElement['metadataProfileId']); } elseif (isset($metadataElement['metadataProfile'])) { $metadataProfile = MetadataProfilePeer::retrieveBySystemName($metadataElement['metadataProfile']); } if ($metadataProfile) { $metadata = MetadataPeer::retrieveByObject($metadataProfile->getId(), $objectType, $cuePoint->getId()); } } if (!$metadataProfile) { continue; } if (!$metadata) { $metadata = new Metadata(); $metadata->setPartnerId($partnerId); $metadata->setMetadataProfileId($metadataProfile->getId()); $metadata->setMetadataProfileVersion($metadataProfile->getVersion()); $metadata->setObjectType($objectType); $metadata->setObjectId($cuePoint->getId()); $metadata->setStatus(KalturaMetadataStatus::VALID); foreach ($metadataElement->children() as $metadataContent) { $xmlData = $metadataContent->asXML(); $errorMessage = ''; if (kMetadataManager::validateMetadata($metadataProfile->getId(), $xmlData, $errorMessage)) { $metadata->save(); $key = $metadata->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA); kFileSyncUtils::file_put_contents($key, $xmlData); kEventsManager::raiseEvent(new kObjectDataChangedEvent($metadata)); } break; } } } return $cuePoint; }
public static function validateProfileFields($partnerId, $xsd) { $xPaths = array_merge(kXsd::findXpathsByAppInfo($xsd, self::APP_INFO_SEARCH, 'true', false), kXsd::findXpathsByAppInfo($xsd, self::APP_INFO_SEARCH, 'false', false)); foreach ($xPaths as $xPath => $xPathData) { if ($xPathData['type'] === MetadataSearchFilter::KMC_FIELD_TYPE_METADATA_OBJECT && isset($xPathData['metadataProfileId'])) { $relatedMetadataProfileId = $xPathData['metadataProfileId']; $relatedMetadataProfile = MetadataProfilePeer::retrieveByPK($relatedMetadataProfileId); if (!$relatedMetadataProfile) { throw new kCoreException('Metadata profile id [' . $relatedMetadataProfileId . ' was not found', kCoreException::ID_NOT_FOUND, $relatedMetadataProfileId); } } } }
/** * Read multiple metadata schemas * @param BaseObject $object * @param array $data */ protected static function addMetadataWithProfilesSystemNames(BaseObject $object, array $data) { $newFieldValuesMap = array(); $xmlDataArray = array(); //Construct mapping of all metadata profile system names, their fields and the field values. foreach ($data as $key => $value) { if (strpos($key, self::BULK_UPLOAD_METADATA_FIELD_PREFIX) === 0) { $prefix = null; $metadataProfileSystemName = null; $metadataProfileFieldName = null; list($prefix, $metadataProfileSystemName, $metadataProfileFieldName) = explode(self::BULK_UPLOAD_METADATA_SYSTEMNAME_SEPARATOR, $key); if (!$prefix || !$metadataProfileSystemName || !$metadataProfileFieldName) { $errorMessage = "Unexpected key structure. Expected metadata::ProfileSystemName::FieldSystemName."; KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($object, $object->getBulkUploadId(), $errorMessage); continue; } if (!isset($newFieldValuesMap[$metadataProfileSystemName])) { $newFieldValuesMap[$metadataProfileSystemName] = array(); } $newFieldValuesMap[$metadataProfileSystemName][$metadataProfileFieldName] = $value; } } foreach ($newFieldValuesMap as $metadataProfileSystemName => $fieldsArray) { /* @var array $fieldsArray */ if (!$fieldsArray || !count($fieldsArray)) { continue; } $metadataProfile = MetadataProfilePeer::retrieveBySystemName($metadataProfileSystemName, $object->getPartnerId()); if (!$metadataProfile) { $errorMessage = "Metadata profile with system name [{$metadataProfileSystemName}] could not be found."; KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($object, $object->getBulkUploadId(), $errorMessage); continue; } if ($metadataProfile->getObjectType() != kMetadataManager::getTypeNameFromObject($object)) { $errorMessage = "Metadata profile [{$metadataProfileSystemName}] object type [" . $metadataProfile->getObjectType() . "] is not compatible with object type [" . kMetadataManager::getTypeNameFromObject($object) . "]"; KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($object, $object->getBulkUploadId(), $errorMessage); continue; } $metadataProfileId = $metadataProfile->getId(); $xml = new DOMDocument(); $metadataProfileFields = array(); MetadataProfileFieldPeer::setUseCriteriaFilter(false); $tmpMetadataProfileFields = MetadataProfileFieldPeer::retrieveByMetadataProfileId($metadataProfileId); MetadataProfileFieldPeer::setUseCriteriaFilter(true); foreach ($tmpMetadataProfileFields as $metadataProfileField) { /* @var $metadataProfileField MetadataProfileField */ $metadataProfileFields[$metadataProfileField->getKey()] = $metadataProfileField; } foreach ($fieldsArray as $fieldSysName => $fieldValue) { if (!isset($metadataProfileFields[$fieldSysName])) { $errorMessage = "Metadata profile field with system name [{$fieldSysName}] missing from metadata profile with id [{$metadataProfileId}]"; KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($object, $object->getBulkUploadId(), $errorMessage); continue; } $metadataProfileField = $metadataProfileFields[$fieldSysName]; KalturaLog::debug("Found field [" . $metadataProfileField->getXpath() . "] for value [{$fieldValue}]"); $fieldValues = explode(self::BULK_UPLOAD_MULTI_VALUES_DELIMITER, $fieldValue); foreach ($fieldValues as $fieldSingleValue) { if ($fieldSingleValue) { if ($metadataProfileField->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_DATE && !is_numeric($fieldSingleValue)) { $valueAsDate = self::parseFormatedDate($fieldSingleValue); if (!$valueAsDate || !strlen($valueAsDate)) { $errorMessage = "Could not parse date format [{$fieldValue}] for field [{$key}]"; KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($object, $object->getBulkUploadId(), $errorMessage); continue; } $fieldSingleValue = $valueAsDate; } if ($metadataProfileField->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_INT && !is_numeric($fieldSingleValue)) { $errorMessage = "Could not parse int format [{$fieldSingleValue}] for field [{$key}]"; KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($object, $object->getBulkUploadId(), $errorMessage); continue; } self::addXpath($xml, $metadataProfileField->getXpath(), $fieldSingleValue); } } $dataFound = true; if ($dataFound && $xml->hasChildNodes()) { $xmlDataArray[$metadataProfileId] = $xml->saveXML($xml->firstChild); $xmlDataArray[$metadataProfileId] = trim($xmlDataArray[$metadataProfileId], " \n\r\t"); } } } foreach ($xmlDataArray as $metadataProfileId => $xmlData) { $errorMessage = ''; if (!kMetadataManager::validateMetadata($metadataProfileId, $xmlData, $errorMessage)) { self::addBulkUploadResultDescription($object, $object->getBulkUploadId(), $errorMessage); continue; } $metadataProfile = MetadataProfilePeer::retrieveByPK($metadataProfileId); $dbMetadata = self::createOrFindMetadataObject($object, $metadataProfile); KalturaLog::debug("Metadata [" . $dbMetadata->getId() . "] saved [{$xmlData}]"); $key = $dbMetadata->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA); kFileSyncUtils::file_put_contents($key, $xmlData); kEventsManager::raiseEvent(new kObjectDataChangedEvent($dbMetadata)); } }
/** * Serves metadata profile view file * * @action serveView * @param int $id * @return file * * @throws MetadataErrors::METADATA_PROFILE_NOT_FOUND * @throws KalturaErrors::FILE_DOESNT_EXIST */ public function serveViewAction($id) { $dbMetadataProfile = MetadataProfilePeer::retrieveByPK($id); if (!$dbMetadataProfile) { throw new KalturaAPIException(MetadataErrors::METADATA_PROFILE_NOT_FOUND, $id); } $fileName = $dbMetadataProfile->getSystemName() . '.xml'; $fileSubType = MetadataProfile::FILE_SYNC_METADATA_VIEWS; return $this->serveFile($dbMetadataProfile, $fileSubType, $fileName); }
/** * Update an existing metadata object views file * * @action updateViewsFromFile * @param int $id * @param file $viewsFile UI views file * @return KalturaMetadataProfile * @throws KalturaErrors::INVALID_OBJECT_ID * @throws MetadataErrors::METADATA_FILE_NOT_FOUND */ function updateViewsFromFileAction($id, $viewsFile) { $dbMetadataProfile = MetadataProfilePeer::retrieveByPK($id); if (!$dbMetadataProfile) { throw new KalturaAPIException(KalturaErrors::INVALID_OBJECT_ID, $id); } $filePath = null; if ($viewsFile) { $filePath = $viewsFile['tmp_name']; if (!file_exists($filePath)) { throw new KalturaAPIException(MetadataErrors::METADATA_FILE_NOT_FOUND, $viewsFile['name']); } } $dbMetadataProfile->incrementViewsVersion(); $dbMetadataProfile->save(); if (trim(file_get_contents($filePath)) != '') { $key = $dbMetadataProfile->getSyncKey(MetadataProfile::FILE_SYNC_METADATA_VIEWS); kFileSyncUtils::moveFromFile($filePath, $key); } $metadataProfile = new KalturaMetadataProfile(); $metadataProfile->fromObject($dbMetadataProfile); return $metadataProfile; }
/** * @return MetadataProfile */ public function getMetadataProfile() { if ($this->aMetadataProfile === null && $this->metadata_profile_id) { $this->aMetadataProfile = MetadataProfilePeer::retrieveByPK($this->metadata_profile_id); } return $this->aMetadataProfile; }
/** * Validate the XML against the profile XSD and set the metadata status * * @param int $metadataProfileId * @param string $metadata * @param string $errorMessage * @param int $metadataProfileVersion leave it null to use the latest * * returns bool */ public static function validateMetadata($metadataProfileId, $metadata, &$errorMessage, $metadataProfileVersion = null) { KalturaLog::debug("Validating metadata [{$metadata}]"); $metadataProfile = MetadataProfilePeer::retrieveByPK($metadataProfileId); if (!$metadataProfile) { $errorMessage = "Metadata profile [{$metadataProfileId}] not found"; KalturaLog::err($errorMessage); return false; } $metadataProfileKey = $metadataProfile->getSyncKey(MetadataProfile::FILE_SYNC_METADATA_DEFINITION, $metadataProfileVersion); $xsdData = kFileSyncUtils::file_get_contents($metadataProfileKey, true, false); if (!$xsdData) { $errorMessage = "Metadata profile xsd not found"; KalturaLog::err($errorMessage); return false; } libxml_use_internal_errors(true); libxml_clear_errors(); $xml = new KDOMDocument(); $xml->loadXML($metadata); if ($xml->schemaValidateSource($xsdData)) { KalturaLog::debug("Metadata is valid"); return true; } $errorMessage = kXml::getLibXmlErrorDescription($metadata); KalturaLog::err("Metadata is invalid:\n{$errorMessage}"); return false; }