示例#1
0
 protected function createSphinxSearchPhrase($partnerScope = null, $text, $metadataProfileFieldId = null)
 {
     $prefix = '@' . MetadataPlugin::getSphinxFieldName(MetadataPlugin::SPHINX_EXPANDER_FIELD_DATA) . ' ';
     if (is_null($partnerScope) || count($partnerScope) != 1) {
         return $prefix . MetadataSearchFilter::createSphinxSearchCondition(null, $text, false);
     }
     if ($partnerScope == baseObjectFilter::MATCH_KALTURA_NETWORK_AND_PRIVATE) {
         $partnerScope = kCurrentContext::getCurrentPartnerId();
     }
     return $prefix . MetadataSearchFilter::createSphinxSearchCondition($partnerScope, $text, false, $metadataProfileFieldId);
 }
示例#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;
 }
 /**
  * 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;
 }
 public function getFreeTextConditions($freeTexts)
 {
     KalturaLog::debug("freeText [{$freeTexts}]");
     $additionalConditions = array();
     if (preg_match('/^"[^"]+"$/', $freeTexts)) {
         $freeText = str_replace('"', '', $freeTexts);
         $freeText = SphinxUtils::escapeString($freeText);
         $freeText = "^{$freeText}\$";
         $additionalConditions[] = '@' . MetadataPlugin::getSphinxFieldName(MetadataPlugin::SPHINX_EXPANDER_FIELD_DATA) . ' ' . MetadataPlugin::PLUGIN_NAME . "_text << ( {$freeTexts} ) << " . kMetadataManager::SEARCH_TEXT_SUFFIX . '_text';
         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[] = '@' . MetadataPlugin::getSphinxFieldName(MetadataPlugin::SPHINX_EXPANDER_FIELD_DATA) . ' ' . MetadataPlugin::PLUGIN_NAME . "_text << ( {$freeText} ) << " . kMetadataManager::SEARCH_TEXT_SUFFIX . '_text';
         }
         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[] = '@' . MetadataPlugin::getSphinxFieldName(MetadataPlugin::SPHINX_EXPANDER_FIELD_DATA) . ' ' . MetadataPlugin::PLUGIN_NAME . "_text << ( {$freeTextExpr} ) << " . kMetadataManager::SEARCH_TEXT_SUFFIX . '_text';
     return $additionalConditions;
 }
 public static function getSphinxSchema()
 {
     $kalturaEntryFields = array();
     $searchIndexes = kConf::get('search_indexes');
     foreach ($searchIndexes as $indexName => $indexLimit) {
         for ($i = 0; $i < $indexLimit; $i++) {
             $kalturaEntryFields[MetadataPlugin::getSphinxFieldName(MetadataPlugin::SPHINX_EXPENDER_FIELD_INT) . $i] = SphinxFieldType::RT_ATTR_UINT;
         }
         $sphinxSchema[kSphinxSearchManager::getSphinxIndexName($indexName)]['fields'] = $kalturaEntryFields;
     }
     return $sphinxSchema;
 }