/** * List metadata profile fields by metadata profile id * * @action listFields * @param int $metadataProfileId * @return KalturaMetadataProfileFieldListResponse */ function listFieldsAction($metadataProfileId) { $dbFields = MetadataProfileFieldPeer::retrieveActiveByMetadataProfileId($metadataProfileId); $response = new KalturaMetadataProfileFieldListResponse(); $response->objects = KalturaMetadataProfileFieldArray::fromMetadataProfileFieldArray($dbFields); $response->totalCount = count($dbFields); return $response; }
public function doGetCondition($xPaths = null) { if ($this->condition) { return $this->condition; } if (is_null($xPaths)) { $xPaths = array(); $profileFields = MetadataProfileFieldPeer::retrieveActiveByMetadataProfileId($this->metadataProfileId); foreach ($profileFields as $profileField) { $xPaths[$profileField->getXpath()] = $profileField->getId(); } } $conditions = array(); $pluginName = MetadataPlugin::PLUGIN_NAME; if (count($this->items)) { foreach ($this->items as $item) { $condition = null; if ($item instanceof AdvancedSearchFilterCondition) { $field = $item->getField(); if (isset($xPaths[$field])) { $value = $item->getValue(); $value = SphinxUtils::escapeString($value); $fieldId = $xPaths[$field]; $condition = "\"{$pluginName}_{$fieldId} {$value} " . kMetadataManager::SEARCH_TEXT_SUFFIX . "\""; } } elseif ($item instanceof MetadataSearchFilter) { $condition = $item->doGetCondition($xPaths); } KalturaLog::debug("Append item [" . get_class($item) . "] condition [{$condition}]"); if ($condition) { $conditions[] = "({$condition})"; } } } if (!count($conditions)) { return null; } $glue = $this->type == MetadataSearchFilter::SEARCH_AND ? ' & ' : ' | '; $this->condition = implode($glue, $conditions); return $this->condition; }
public function getFreeTextConditions($partnerScope, $freeTexts) { $metadataProfileFieldIds = array(); $metadataProfileId = $this->getMetadataProfileId(); if ($metadataProfileId) { $metadataProfileFields = MetadataProfileFieldPeer::retrieveActiveByMetadataProfileId($metadataProfileId); foreach ($metadataProfileFields as $metadataProfileField) { $metadataProfileFieldIds[] = $metadataProfileField->getId(); } } KalturaLog::debug("freeText [{$freeTexts}]"); $additionalConditions = array(); if (preg_match('/^"[^"]+"$/', $freeTexts)) { $freeText = str_replace('"', '', $freeTexts); $freeText = SphinxUtils::escapeString($freeText); $freeText = "^{$freeText}\$"; $additionalConditions[] = $this->createSphinxSearchPhrase($partnerScope, $freeTexts); return $additionalConditions; } if (strpos($freeTexts, baseObjectFilter::IN_SEPARATOR) > 0) { str_replace(baseObjectFilter::AND_SEPARATOR, baseObjectFilter::IN_SEPARATOR, $freeTexts); $freeTextsArr = explode(baseObjectFilter::IN_SEPARATOR, $freeTexts); foreach ($freeTextsArr as $valIndex => $valValue) { if (!is_numeric($valValue) && strlen($valValue) <= 0) { unset($freeTextsArr[$valIndex]); } } foreach ($freeTextsArr as $freeText) { $freeText = SphinxUtils::escapeString($freeText); $additionalConditions[] = $this->createSphinxSearchPhrase($partnerScope, $freeText); foreach ($metadataProfileFieldIds as $metadataProfileFieldId) { $additionalConditions[] = $this->createSphinxSearchPhrase($partnerScope, $freeText, $metadataProfileFieldId); } } return $additionalConditions; } $freeTextsArr = explode(baseObjectFilter::AND_SEPARATOR, $freeTexts); foreach ($freeTextsArr as $valIndex => $valValue) { if (!is_numeric($valValue) && strlen($valValue) <= 0) { unset($freeTextsArr[$valIndex]); } } $freeTextsArr = array_unique($freeTextsArr); $freeTextExpr = implode(baseObjectFilter::AND_SEPARATOR, $freeTextsArr); $freeTextExpr = SphinxUtils::escapeString($freeTextExpr); $additionalConditions[] = $this->createSphinxSearchPhrase($partnerScope, $freeTextExpr); foreach ($metadataProfileFieldIds as $metadataProfileFieldId) { $additionalConditions[] = $this->createSphinxSearchPhrase($partnerScope, $freeTextExpr, $metadataProfileFieldId); } return $additionalConditions; }
protected function loadFields($metadataProfileId) { $xPaths = array(); $profileFields = MetadataProfileFieldPeer::retrieveActiveByMetadataProfileId($metadataProfileId); foreach ($profileFields as $profileField) { /** @var MetadataProfileField $profileField */ $xPaths[$profileField->getXpath()] = $profileField; } return $xPaths; }
/** * 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; }
/** * * @param string $field - xPath (metadataProfileField) */ public function getMetadataSearchField($field = null, $xPaths = array()) { if (!$field) { return MetadataPlugin::getSphinxFieldName(MetadataPlugin::SPHINX_EXPANDER_FIELD_DATA); } if (!count($xPaths)) { $profileFields = MetadataProfileFieldPeer::retrieveActiveByMetadataProfileId($this->metadataProfileId); foreach ($profileFields as $profileField) { $xPaths[$profileField->getXpath()] = $profileField; } } if (!isset($xPaths[$field])) { KalturaLog::ERR("Missing field: " . $field); return null; } if (is_null($xPaths[$field]->getSearchIndex())) { KalturaLog::ERR("Missing field search index: " . $field); return null; } switch ($xPaths[$field]->getType()) { case MetadataSearchFilter::KMC_FIELD_TYPE_DATE: case MetadataSearchFilter::KMC_FIELD_TYPE_INT: $metadataField = MetadataPlugin::getSphinxFieldName(MetadataPlugin::SPHINX_EXPENDER_FIELD_INT) . $xPaths[$field]->getSearchIndex(); break; default: KalturaLog::ERR("Missing field type: " . $xPaths[$field]->getType()); return null; } return $metadataField; }
/** * Parse the XML and update the list of search values * * @param Metadata $metadata * * @return array */ public static function getSearchValues(Metadata $metadata, $searchTexts = array()) { KalturaLog::debug("Parsing metadata [" . $metadata->getId() . "] search values"); $key = $metadata->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA); $xmlPath = kFileSyncUtils::getLocalFilePathForKey($key); $xml = new DOMDocument(); $xml->load($xmlPath); $xPath = new DOMXPath($xml); $profileFields = MetadataProfileFieldPeer::retrieveActiveByMetadataProfileId($metadata->getMetadataProfileId()); KalturaLog::debug("Metadata fields [" . count($profileFields) . "] found"); $searchItems = array(); $textItems = array(); foreach ($profileFields as $profileField) { $nodes = $xPath->query($profileField->getXpath()); if (!$nodes->length) { continue; } if ($profileField->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_DATE) { continue; } $searchItemValues = array(); foreach ($nodes as $node) { $searchItemValues[] = $node->nodeValue; } if (!count($searchItemValues)) { continue; } if ($profileField->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_TEXT) { $textItem = implode(' ', $searchItemValues); $textItems[] = $textItem; if (iconv_strlen($textItem, 'UTF-8') < 128) { $searchItems[$profileField->getId()] = $searchItemValues; } } else { $searchItems[$profileField->getId()] = $searchItemValues; } } foreach ($searchItems as $key => $searchItem) { foreach ($searchItem as $searchPhrase) { $searchTexts[] = MetadataPlugin::PLUGIN_NAME . '_' . "{$key} {$searchPhrase} " . kMetadataManager::SEARCH_TEXT_SUFFIX; } } if (count($textItems)) { if (!isset($searchTexts['text'])) { $searchTexts['text'] = MetadataPlugin::PLUGIN_NAME . '_text'; } $searchTexts['text'] .= ' ' . implode(' ', $textItems); } $searchTexts['text'] .= ' ' . kMetadataManager::SEARCH_TEXT_SUFFIX; // KalturaLog::debug('Search Texts: ' . print_r($searchTexts, true)); $ret = array(); foreach ($searchTexts as $index => $value) { if (is_int($index)) { $ret[$index] = $value; } } if (isset($searchTexts['text'])) { $ret['text'] = $searchTexts['text']; } return $ret; }
/** * 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) { foreach ($nodes as $node) { if (!is_null($profileField->getSearchIndex())) { $searchValues[MetadataPlugin::getSphinxFieldName(MetadataPlugin::SPHINX_EXPENDER_FIELD_INT) . $profileField->getSearchIndex()] = $node->nodeValue; } break; } continue; } $searchItemValues = array(); foreach ($nodes as $node) { $searchItemValues[] = $node->nodeValue; } if (!count($searchItemValues)) { continue; } if ($profileField->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_TEXT) { $textItems[] = implode(' ', $searchItemValues); $searchItems[$profileField->getId()] = array(); foreach ($searchItemValues as $searchItemValue) { if (iconv_strlen($searchItemValue, 'UTF-8') >= 128) { continue; } $searchItems[$profileField->getId()][] = $searchItemValue; } } 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)) { if (!isset($searchTexts['text'])) { $searchTexts['text'] = MetadataPlugin::PLUGIN_NAME . '_text'; } $searchTexts['text'] .= ' ' . implode(' ', $textItems); $searchTexts['text'] .= ' ' . kMetadataManager::SEARCH_TEXT_SUFFIX . '_text'; } $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; }