/**
  * @param Metadata $metadata
  */
 public static function onMetadataChanged(Metadata $metadata, $previousVersion)
 {
     if (!ContentDistributionPlugin::isAllowedPartner($metadata->getPartnerId())) {
         return true;
     }
     if ($metadata->getObjectType() != MetadataObjectType::ENTRY) {
         return true;
     }
     KalturaLog::log("Metadata [" . $metadata->getId() . "] for entry [" . $metadata->getObjectId() . "] changed");
     $syncKey = $metadata->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA);
     $xmlPath = kFileSyncUtils::getLocalFilePathForKey($syncKey);
     if (!$xmlPath) {
         KalturaLog::log("Entry metadata xml not found");
         return true;
     }
     $xml = new KDOMDocument();
     $xml->load($xmlPath);
     $previousXml = null;
     if ($previousVersion) {
         $syncKey = $metadata->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA, $previousVersion);
         $xmlPath = kFileSyncUtils::getLocalFilePathForKey($syncKey);
         if ($xmlPath) {
             $previousXml = new KDOMDocument();
             $previousXml->load($xmlPath);
         } else {
             KalturaLog::log("Entry metadata previous version xml not found");
         }
     }
     $entryDistributions = EntryDistributionPeer::retrieveByEntryId($metadata->getObjectId());
     foreach ($entryDistributions as $entryDistribution) {
         if ($entryDistribution->getStatus() != EntryDistributionStatus::QUEUED && $entryDistribution->getStatus() != EntryDistributionStatus::PENDING && $entryDistribution->getStatus() != EntryDistributionStatus::READY) {
             continue;
         }
         $distributionProfileId = $entryDistribution->getDistributionProfileId();
         $distributionProfile = DistributionProfilePeer::retrieveByPK($distributionProfileId);
         if (!$distributionProfile) {
             KalturaLog::err("Entry distribution [" . $entryDistribution->getId() . "] profile [{$distributionProfileId}] not found");
             continue;
         }
         $distributionProvider = $distributionProfile->getProvider();
         if (!$distributionProvider) {
             KalturaLog::err("Entry distribution [" . $entryDistribution->getId() . "] provider [" . $distributionProfile->getProviderType() . "] not found");
             continue;
         }
         if ($entryDistribution->getStatus() == EntryDistributionStatus::PENDING || $entryDistribution->getStatus() == EntryDistributionStatus::QUEUED) {
             $validationErrors = $distributionProfile->validateForSubmission($entryDistribution, DistributionAction::SUBMIT);
             $entryDistribution->setValidationErrorsArray($validationErrors);
             $entryDistribution->save();
             if ($entryDistribution->getStatus() == EntryDistributionStatus::QUEUED) {
                 if ($entryDistribution->getDirtyStatus() != EntryDistributionDirtyStatus::SUBMIT_REQUIRED) {
                     self::submitAddEntryDistribution($entryDistribution, $distributionProfile);
                 }
             }
             continue;
         }
         if ($entryDistribution->getStatus() == EntryDistributionStatus::READY) {
             if ($entryDistribution->getDirtyStatus() == EntryDistributionDirtyStatus::UPDATE_REQUIRED) {
                 KalturaLog::log("Entry distribution [" . $entryDistribution->getId() . "] already flaged for updating");
                 //					continue;
             }
             if (!$distributionProvider->isUpdateEnabled()) {
                 KalturaLog::log("Entry distribution [" . $entryDistribution->getId() . "] provider [" . $distributionProvider->getName() . "] does not support update");
                 continue;
             }
             $updateRequiredMetadataXPaths = $distributionProvider->getUpdateRequiredMetadataXPaths($distributionProfileId);
             $updateRequired = false;
             foreach ($updateRequiredMetadataXPaths as $updateRequiredMetadataXPath) {
                 $xPath = new DOMXpath($xml);
                 $newElements = $xPath->query($updateRequiredMetadataXPath);
                 $oldElements = null;
                 if ($previousXml) {
                     $xPath = new DOMXpath($previousXml);
                     $oldElements = $xPath->query($updateRequiredMetadataXPath);
                 }
                 if (is_null($newElements) && is_null($oldElements)) {
                     continue;
                 }
                 if (is_null($newElements) xor is_null($oldElements)) {
                     $updateRequired = true;
                 } elseif ($newElements->length == $oldElements->length) {
                     for ($index = 0; $index < $newElements->length; $index++) {
                         $newValue = $newElements->item($index)->textContent;
                         $oldValue = $oldElements->item($index)->textContent;
                         if ($newValue != $oldValue) {
                             $updateRequired = true;
                             break;
                         }
                     }
                 }
                 if ($updateRequired) {
                     break;
                 }
             }
             $validationErrors = $distributionProfile->validateForSubmission($entryDistribution, DistributionAction::UPDATE);
             $entryDistribution->setValidationErrorsArray($validationErrors);
             $entryDistribution->save();
             if (!$updateRequired) {
                 KalturaLog::log("Entry distribution [" . $entryDistribution->getId() . "] update not required");
                 continue;
             }
             if (!count($validationErrors) && $distributionProfile->getUpdateEnabled() == DistributionProfileActionStatus::AUTOMATIC) {
                 self::submitUpdateEntryDistribution($entryDistribution, $distributionProfile);
             } else {
                 KalturaLog::log("Entry distribution [" . $entryDistribution->getId() . "] should not be updated automatically");
                 $entryDistribution->setDirtyStatus(EntryDistributionDirtyStatus::UPDATE_REQUIRED);
                 $entryDistribution->save();
                 continue;
             }
         }
     }
     return true;
 }
Exemplo n.º 2
0
 /**
  * Parse the XML and update the list of search values
  *
  * @param Metadata $metadata
  * @param array $searchValues
  *
  * @return array
  */
 public static function getDataSearchValues(Metadata $metadata, $searchValues = array())
 {
     KalturaLog::debug("Parsing metadata [" . $metadata->getId() . "] search values");
     $searchTexts = array();
     if (isset($searchValues[MetadataPlugin::getSphinxFieldName(MetadataPlugin::SPHINX_EXPANDER_FIELD_DATA)])) {
         foreach ($searchValues[MetadataPlugin::getSphinxFieldName(MetadataPlugin::SPHINX_EXPANDER_FIELD_DATA)] as $DataSerachValue) {
             $searchTexts[] = $DataSerachValue;
         }
     }
     $key = $metadata->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA);
     $xmlPath = kFileSyncUtils::getLocalFilePathForKey($key);
     try {
         $xml = new KDOMDocument();
         $xml->load($xmlPath);
         $xPath = new DOMXPath($xml);
     } catch (Exception $ex) {
         KalturaLog::err('Could not load metadata xml [' . $xmlPath . '] - ' . $ex->getMessage());
         return '';
     }
     $profileFields = MetadataProfileFieldPeer::retrieveActiveByMetadataProfileId($metadata->getMetadataProfileId());
     $searchItems = array();
     $textItems = array();
     foreach ($profileFields as $profileField) {
         /* @var  $profileField MetadataProfileField */
         $nodes = $xPath->query($profileField->getXpath());
         if (!$nodes->length) {
             continue;
         }
         if ($profileField->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_DATE || $profileField->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_INT) {
             $node = $nodes->item(0);
             if (!isset($searchValues[MetadataPlugin::SPHINX_DYNAMIC_ATTRIBUTES])) {
                 $searchValues[MetadataPlugin::SPHINX_DYNAMIC_ATTRIBUTES] = array();
             }
             $fieldName = MetadataPlugin::getSphinxFieldName($profileField->getId());
             $searchValues[MetadataPlugin::SPHINX_DYNAMIC_ATTRIBUTES][$fieldName] = intval($node->nodeValue);
             continue;
         }
         $searchItemValues = array();
         foreach ($nodes as $node) {
             $searchItemValues[] = $node->nodeValue;
         }
         if (!count($searchItemValues)) {
             continue;
         }
         if ($profileField->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_TEXT || $profileField->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_METADATA_OBJECT) {
             $textItems[] = implode(' ', $searchItemValues);
             $searchItems[$profileField->getId()] = array();
             foreach ($searchItemValues as $searchItemValue) {
                 if (iconv_strlen($searchItemValue, 'UTF-8') >= 128) {
                     continue;
                 }
                 $searchItems[$profileField->getId()][] = $searchItemValue;
             }
             if ($profileField->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_METADATA_OBJECT && $profileField->getRelatedMetadataProfileId()) {
                 $subMetadataProfileId = $profileField->getRelatedMetadataProfileId();
                 $subMetadataProfile = MetadataProfilePeer::retrieveByPK($subMetadataProfileId);
                 if (!$subMetadataProfile) {
                     KalturaLog::err('Sub metadata profile ' . $subMetadataProfileId . ' was not found');
                     continue;
                 }
                 $subMetadataObjects = MetadataPeer::retrieveByObjects($subMetadataProfileId, $subMetadataProfile->getObjectType(), $searchItemValues);
                 foreach ($subMetadataObjects as $subMetadataObject) {
                     /** @var Metadata $subMetadataObject */
                     KalturaLog::debug("Found metadata object for profile {$subMetadataProfileId} and id {$subMetadataObject->getObjectId()}, extracting search data");
                     $subSearchTextsResult = self::getDataSearchValues($subMetadataObject);
                     $subSearchTexts = $subSearchTextsResult[MetadataPlugin::getSphinxFieldName(MetadataPlugin::SPHINX_EXPANDER_FIELD_DATA)];
                     foreach ($subSearchTexts as $subSearchText) {
                         $searchTexts[] = $subSearchText;
                     }
                 }
             }
         } else {
             $searchItems[$profileField->getId()] = $searchItemValues;
         }
     }
     foreach ($searchItems as $key => $searchItem) {
         foreach ($searchItem as $searchPhrase) {
             $searchTexts[] = MetadataPlugin::PLUGIN_NAME . '_' . "{$key} {$searchPhrase} " . kMetadataManager::SEARCH_TEXT_SUFFIX . '_' . $key;
         }
     }
     if (count($textItems)) {
         $searchTexts['text'] = MetadataSearchFilter::createSphinxSearchCondition($metadata->getPartnerId(), implode(' ', $textItems), true);
     }
     $ret = array();
     foreach ($searchTexts as $index => $value) {
         if (is_int($index)) {
             $ret[$index] = $value;
         }
     }
     if (isset($searchTexts['text'])) {
         $ret['text'] = $searchTexts['text'];
     }
     $searchValues[MetadataPlugin::getSphinxFieldName(MetadataPlugin::SPHINX_EXPANDER_FIELD_DATA)] = $ret;
     return $searchValues;
 }