function updateImpl($id, $xmlData = null, $version = null) { $dbMetadata = MetadataPeer::retrieveByPK($id); if (!$dbMetadata) { throw new KalturaAPIException(MetadataErrors::METADATA_NOT_FOUND, $id); } if ($version && $dbMetadata->getVersion() != $version) { throw new KalturaAPIException(MetadataErrors::INVALID_METADATA_VERSION, $dbMetadata->getVersion()); } $dbMetadataProfile = MetadataProfilePeer::retrieveByPK($dbMetadata->getMetadataProfileId()); if (!$dbMetadataProfile) { throw new KalturaAPIException(MetadataErrors::INVALID_METADATA_PROFILE, $dbMetadata->getMetadataProfileId()); } $previousVersion = null; if ($dbMetadata->getStatus() == Metadata::STATUS_VALID) { $previousVersion = $dbMetadata->getVersion(); } if ($xmlData) { // if a metadata xslt is defined on the metadata profile - transform the given metadata $xmlDataTransformed = $this->transformMetadata($dbMetadata->getMetadataProfileId(), $xmlData); if ($xmlDataTransformed) { $xmlData = $xmlDataTransformed; } $errorMessage = ''; if (!kMetadataManager::validateMetadata($dbMetadata->getMetadataProfileId(), $xmlData, $errorMessage)) { // if metadata profile is transforming, and metadata profile version is not the latest, try to validate againts previous version if ($dbMetadataProfile->getStatus() != MetadataProfile::STATUS_TRANSFORMING || $dbMetadata->getMetadataProfileVersion() >= $dbMetadataProfile->getVersion()) { throw new KalturaAPIException(MetadataErrors::INVALID_METADATA_DATA, $errorMessage); } // validates against previous version $errorMessagePrevVersion = ''; if (!kMetadataManager::validateMetadata($dbMetadata->getMetadataProfileId(), $xmlData, $errorMessagePrevVersion, true)) { KalturaLog::err("Failed to validate metadata object [{$id}] against metadata profile previous version [" . $dbMetadata->getMetadataProfileVersion() . "] error: {$errorMessagePrevVersion}"); // throw the error with the original error message throw new KalturaAPIException(MetadataErrors::INVALID_METADATA_DATA, $errorMessage); } } else { $dbMetadata->setMetadataProfileVersion($dbMetadataProfile->getVersion()); } $key = $dbMetadata->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA); if (!kFileSyncUtils::compareContent($key, $xmlData)) { $dbMetadata->incrementVersion(); $key = $dbMetadata->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA); kFileSyncUtils::file_put_contents($key, $xmlData); $dbMetadata->save(); kEventsManager::raiseEvent(new kObjectDataChangedEvent($dbMetadata, $previousVersion)); } else { KalturaLog::info("XML data MD5 matches current filesync content MD5. Update is not necessary."); //adding this save() in order to save the metadata profile version field in case there are no diffrences $dbMetadata->save(); } } $metadata = new KalturaMetadata(); $metadata->fromObject($dbMetadata, $this->getResponseProfile()); return $metadata; }