protected function copyLiveMetadata(baseEntry $object, $liveEntryId) { $recordedEntryId = $object->getId(); $partnerId = $object->getPartnerId(); $metadataProfiles = MetadataProfilePeer::retrieveAllActiveByPartnerId($partnerId, MetadataObjectType::ENTRY); foreach ($metadataProfiles as $metadataProfile) { $originMetadataObj = MetadataPeer::retrieveByObject($metadataProfile->getId(), MetadataObjectType::ENTRY, $liveEntryId); if ($originMetadataObj) { $metadataProfileId = $metadataProfile->getId(); $metadataProfileVersion = $metadataProfile->getVersion(); $destMetadataObj = new Metadata(); $destMetadataObj->setPartnerId($partnerId); $destMetadataObj->setMetadataProfileId($metadataProfileId); $destMetadataObj->setMetadataProfileVersion($metadataProfileVersion); $destMetadataObj->setObjectType(MetadataObjectType::ENTRY); $destMetadataObj->setObjectId($recordedEntryId); $destMetadataObj->setStatus(KalturaMetadataStatus::VALID); $originMetadataKey = $originMetadataObj->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA); $originXml = kFileSyncUtils::file_get_contents($originMetadataKey, true, false); // validate object exists $object = kMetadataManager::getObjectFromPeer($destMetadataObj); if ($object) { $destMetadataObj->save(); } else { KalturaLog::err('invalid object type'); continue; } $destMetadataKey = $destMetadataObj->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA); kFileSyncUtils::file_put_contents($destMetadataKey, $originXml); } } }
/** * 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; }
/** * 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_PROFILE_TYPE * @throws MetadataErrors::INVALID_METADATA_OBJECT */ protected function addMetadata($metadataProfileId, $objectType, $objectId) { $objectType = kPluginableEnumsManager::apiToCore('MetadataObjectType', $objectType); $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); } if ($dbMetadataProfile->getObjectType() != $objectType) { throw new KalturaAPIException(MetadataErrors::INVALID_METADATA_PROFILE_TYPE, $dbMetadataProfile->getObjectType()); } $dbMetadata = new Metadata(); $dbMetadata->setPartnerId($this->getPartnerId()); $dbMetadata->setMetadataProfileId($metadataProfileId); $dbMetadata->setMetadataProfileVersion($dbMetadataProfile->getVersion()); $dbMetadata->setObjectType($objectType); $dbMetadata->setObjectId($objectId); $dbMetadata->setStatus(KalturaMetadataStatus::VALID); $dbMetadata->setLikeNew(true); // dynamic objects are metadata only, skip validating object id if ($objectType != KalturaMetadataObjectType::DYNAMIC_OBJECT) { // validate object exists $object = kMetadataManager::getObjectFromPeer($dbMetadata); if (!$object) { throw new KalturaAPIException(MetadataErrors::INVALID_METADATA_OBJECT, $objectId); } } $dbMetadata->save(); $this->deleteOldVersions($dbMetadata); return $dbMetadata; }
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; }
/** * @param string $entryId the new created entry * @param array $data key => value pairs */ public static function handleBulkUploadData($entryId, array $data) { KalturaLog::debug("Handle metadata bulk upload data:\n" . print_r($data, true)); if (!isset($data[self::BULK_UPLOAD_COLUMN_PROFILE_ID])) { return; } $metadataProfileId = $data[self::BULK_UPLOAD_COLUMN_PROFILE_ID]; $xmlData = null; $entry = entryPeer::retrieveByPK($entryId); if (!$entry) { return; } // $criteriaFilter = FileSyncPeer::getCriteriaFilter(); // $criteria = $criteriaFilter->getFilter(); // $criteria->add(FileSyncPeer::PARTNER_ID, $entry->getPartnerId()); $metadataProfile = MetadataProfilePeer::retrieveById($metadataProfileId); if (!$metadataProfile) { $errorMessage = "Metadata profile [{$metadataProfileId}] not found"; KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($entryId, $entry->getBulkUploadId(), $errorMessage); return; } if (isset($data[self::BULK_UPLOAD_COLUMN_URL])) { try { $xmlData = file_get_contents($data[self::BULK_UPLOAD_COLUMN_URL]); KalturaLog::debug("Metadata downloaded [" . $data[self::BULK_UPLOAD_COLUMN_URL] . "]"); } catch (Exception $e) { $errorMessage = "Download metadata[" . $data[self::BULK_UPLOAD_COLUMN_URL] . "] error: " . $e->getMessage(); KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($entryId, $entry->getBulkUploadId(), $errorMessage); $xmlData = null; } } elseif (isset($data[self::BULK_UPLOAD_COLUMN_XML])) { $xmlData = $data[self::BULK_UPLOAD_COLUMN_XML]; } else { $metadataProfileFields = array(); MetadataProfileFieldPeer::setUseCriteriaFilter(false); $tmpMetadataProfileFields = MetadataProfileFieldPeer::retrieveByMetadataProfileId($metadataProfileId); MetadataProfileFieldPeer::setUseCriteriaFilter(true); foreach ($tmpMetadataProfileFields as $metadataProfileField) { $metadataProfileFields[$metadataProfileField->getKey()] = $metadataProfileField; } KalturaLog::debug("Found fields [" . count($metadataProfileFields) . "] for metadata profile [{$metadataProfileId}]"); $xml = new DOMDocument(); $dataFound = false; foreach ($data as $key => $value) { if (!$value || !strlen($value)) { continue; } if (!preg_match('/^' . self::BULK_UPLOAD_COLUMN_FIELD_PREFIX . '(.+)$/', $key, $matches)) { continue; } $key = $matches[1]; if (!isset($metadataProfileFields[$key])) { $errorMessage = "Field [{$key}] does not exist"; KalturaLog::debug($errorMessage); self::addBulkUploadResultDescription($entryId, $entry->getBulkUploadId(), $errorMessage); continue; } $metadataProfileField = $metadataProfileFields[$key]; KalturaLog::debug("Found field [" . $metadataProfileField->getXpath() . "] for value [{$value}]"); $fieldValues = explode(self::BULK_UPLOAD_MULTI_VALUES_DELIMITER, $value); foreach ($fieldValues as $fieldValue) { if ($metadataProfileField->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_DATE && !is_numeric($fieldValue)) { $value = self::parseFormatedDate($fieldValue); if (!$value || !strlen($value)) { $errorMessage = "Could not parse date format [{$fieldValue}] for field [{$key}]"; KalturaLog::debug($errorMessage); self::addBulkUploadResultDescription($entryId, $entry->getBulkUploadId(), $errorMessage); continue; } $fieldValue = $value; } self::addXpath($xml, $metadataProfileField->getXpath(), $fieldValue); } $dataFound = true; } if ($dataFound) { $xmlData = $xml->saveXML($xml->firstChild); $xmlData = trim($xmlData, " \n\r\t"); } } if (!$xmlData) { return; } $dbMetadata = new Metadata(); $dbMetadata->setPartnerId($entry->getPartnerId()); $dbMetadata->setMetadataProfileId($metadataProfileId); $dbMetadata->setMetadataProfileVersion($metadataProfile->getVersion()); $dbMetadata->setObjectType(Metadata::TYPE_ENTRY); $dbMetadata->setObjectId($entryId); $dbMetadata->setStatus(Metadata::STATUS_INVALID); $dbMetadata->save(); KalturaLog::debug("Metadata [" . $dbMetadata->getId() . "] saved [{$xmlData}]"); $key = $dbMetadata->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA); kFileSyncUtils::file_put_contents($key, $xmlData); $errorMessage = ''; $status = kMetadataManager::validateMetadata($dbMetadata, $errorMessage); if ($status == Metadata::STATUS_VALID) { kEventsManager::raiseEvent(new kObjectDataChangedEvent($dbMetadata)); } else { self::addBulkUploadResultDescription($entryId, $entry->getBulkUploadId(), $errorMessage); } }
/** * Function returns metadata object which needs to be set with the new metadata XML * @param BaseObject $object * @param MetadataProfile $metadataProfileId */ protected static function createOrFindMetadataObject(BaseObject $object, MetadataProfile $metadataProfile) { $c = new Criteria(); $c->addAnd(MetadataPeer::PARTNER_ID, $object->getPartnerId(), Criteria::EQUAL); $c->addAnd(MetadataPeer::OBJECT_ID, $object->getId(), Criteria::EQUAL); $c->addAnd(MetadataPeer::METADATA_PROFILE_ID, $metadataProfile->getId(), Criteria::EQUAL); $c->addAnd(MetadataPeer::METADATA_PROFILE_VERSION, $metadataProfile->getVersion(), Criteria::EQUAL); $c->addAnd(MetadataPeer::OBJECT_TYPE, kMetadataManager::getTypeNameFromObject($object), Criteria::EQUAL); $c->addAnd(MetadataPeer::STATUS, Metadata::STATUS_VALID); $dbMetadata = MetadataPeer::doSelectOne($c); if (!$dbMetadata) { $dbMetadata = new Metadata(); $dbMetadata->setPartnerId($object->getPartnerId()); $dbMetadata->setMetadataProfileId($metadataProfile->getId()); $dbMetadata->setMetadataProfileVersion($metadataProfile->getVersion()); $dbMetadata->setObjectType(kMetadataManager::getTypeNameFromObject($object)); $dbMetadata->setObjectId($object->getId()); $dbMetadata->setStatus(Metadata::STATUS_VALID); $dbMetadata->save(); } else { $dbMetadata->incrementVersion(); $dbMetadata->save(); } return $dbMetadata; }