예제 #1
0
 private function applyMatchCondition($conditionStr, IKalturaIndexQuery $query)
 {
     $matchCondition = $this->createSphinxMatchPhrase($conditionStr);
     $query->addMatch("({$matchCondition})");
 }
 public function apply(IKalturaIndexQuery $query)
 {
     $this->currentQuery = $query;
     $field = $this->getTable() . '.' . $this->getColumn();
     // Can apply criterion
     KalturaLog::debug("Applies criterion [{$field}]");
     $comparison = $this->getComparison();
     if ($comparison == Criteria::CUSTOM || $comparison == Criteria::CUSTOM_EQUAL || $comparison == Criteria::ISNOTNULL) {
         KalturaLog::debug("Skip criterion[{$field}] unhandled comparison [{$comparison}]");
         return false;
     }
     if (!$this->criteria->hasSphinxFieldName($field)) {
         KalturaLog::debug("Skip criterion[{$field}] has no sphinx field");
         return false;
     }
     $value = $this->getValue();
     $sphinxField = $this->criteria->getSphinxFieldName($field);
     $type = $this->criteria->getSphinxFieldType($sphinxField);
     // Update value & comparison in case of id field
     if ($field == $this->criteria->getIdField()) {
         if ($comparison == Criteria::EQUAL) {
             $comparison = Criteria::IN;
         }
         if ($comparison == Criteria::NOT_EQUAL) {
             $comparison = Criteria::NOT_IN;
         }
         if (!is_array($value)) {
             $value = explode(',', $value);
         }
         $ids = array();
         foreach ($value as $val) {
             $ids[$val] = $this->criteria->getTranslateIndexId($val);
         }
         $value = $ids;
         $this->criteria->setIds($comparison, $ids);
     }
     $valStr = print_r($value, true);
     KalturaLog::debug("Attach criterion[{$field}] as sphinx field[{$sphinxField}] of type [{$type}] and comparison[{$comparison}] for value[{$valStr}]");
     // update comparison in case of null
     if (is_null($value)) {
         if ($comparison == Criteria::EQUAL) {
             $comparison = Criteria::ISNULL;
         } elseif ($comparison == Criteria::NOT_EQUAL) {
             $comparison = Criteria::ISNOTNULL;
         }
     }
     // update value in case of Date
     if (is_string($value)) {
         // needed since otherwise the switch statement doesn't work as expected otherwise
         switch ($value) {
             case Criteria::CURRENT_DATE:
                 $d = getdate();
                 $value = mktime(0, 0, 0, $d['mon'], $d['mday'], $d['year']);
                 break;
             case Criteria::CURRENT_TIME:
             case Criteria::CURRENT_TIMESTAMP:
                 $value = time();
                 break;
         }
     }
     if ($type == IIndexable::FIELD_TYPE_STRING) {
         $match = $this->getStringMatchClause($sphinxField, $comparison, $value);
         KalturaLog::debug("Add match criterion[{$field}] as sphinx field[{$sphinxField}] of type [{$type}] match [{$match}] line [" . __LINE__ . "]");
         $this->addMatch($match);
     } else {
         $condition = $this->getNonStringClause($sphinxField, $type, $comparison, $value);
         KalturaLog::debug("Add condition criterion[{$field}] as sphinx field[{$sphinxField}] of type [{$type}] condition [{$condition}] line [" . __LINE__ . "]");
         $this->addCondition($condition);
     }
     $clauses = $this->getClauses();
     foreach ($clauses as $index => $clause) {
         if (!$clause instanceof SphinxCriterion) {
             KalturaLog::debug("Clause [" . $clause->getColumn() . "] is not Sphinx criterion");
             return false;
         }
         if (!$clause->apply($this)) {
             KalturaLog::debug("Failed to apply clause [" . $clause->getColumn() . "]");
             return false;
         }
     }
     if (count($this->matchClause)) {
         $matchesClause = implode(' ', $this->matchClause);
         if ($this->hasOr() && count($this->matchClause) > 1) {
             $matchesClause = "({$matchesClause})";
         }
         $match = $this->getSelfMatchOperator() . $matchesClause;
         KalturaLog::debug("Add match criterion[{$field}] as sphinx field[{$sphinxField}] of type [{$type}] match [{$match}] line [" . __LINE__ . "]");
         $query->addMatch($match);
     }
     if (count($this->conditionClause)) {
         $attributesClause = implode($this->getSelfConjunction(), array_unique($this->conditionClause));
         if (!strlen(trim($attributesClause))) {
             return true;
         }
         if (!$this->hasOr()) {
             $where = $attributesClause;
             KalturaLog::debug("Add where criterion[{$field}] as sphinx field[{$sphinxField}] of type [{$type}] where [{$where}] line [" . __LINE__ . "]");
             $query->addWhere($where);
             return true;
         }
         // Reduce null
         $expSimplifications = array("({$sphinxField} <> 0 AND {$sphinxField} <= {$value}) OR ({$sphinxField} = 0)" => "{$sphinxField} <= {$value}", "({$sphinxField} <> 0 AND {$sphinxField} < {$value}) OR ({$sphinxField} = 0)" => "{$sphinxField} < {$value}");
         if (isset($expSimplifications[$attributesClause])) {
             KalturaLog::debug("Simplifying expression [{$attributesClause}] => [{$expSimplifications[$attributesClause]}]");
             // We move it to the 'where' since where is allegedly faster than in the condition.
             $where = $this->getSelfConjunction() . $expSimplifications[$attributesClause];
             KalturaLog::debug("Add where criterion[{$field}] as sphinx field[{$sphinxField}] of type [{$type}] where [{$where}] line [" . __LINE__ . "]");
             $query->addWhere($where);
         } else {
             if ($this->needsBrackets()) {
                 $attributesClause = "({$attributesClause})";
             }
             $condition = $this->getSelfConjunction() . $attributesClause;
             KalturaLog::debug("Add condition criterion[{$field}] as sphinx field[{$sphinxField}] of type [{$type}] condition [{$condition}] line [" . __LINE__ . "]");
             $query->addCondition($condition);
         }
     }
     return true;
 }
 public function applyCondition(IKalturaIndexQuery $query)
 {
     $this->parentQuery = $query;
     if (!$this->condition) {
         if (count($this->items)) {
             $queryDestination = $this;
             if ($this->type == self::SEARCH_AND) {
                 $queryDestination = $query;
             }
             foreach ($this->items as $item) {
                 KalturaLog::debug("item type: " . get_class($item));
                 if ($item instanceof AdvancedSearchFilterItem) {
                     $item->applyCondition($queryDestination);
                 }
             }
             if ($this->type == self::SEARCH_OR && count($this->matchClause)) {
                 $matchClause = array_unique($this->matchClause);
                 $this->condition = '( ' . implode(' | ', $matchClause) . ' )';
             }
         }
     }
     if ($this->condition) {
         $query->addMatch($this->condition);
     }
 }
 private function addCondition($conditionStr, IKalturaIndexQuery $query)
 {
     if (!is_null($conditionStr)) {
         $query->addMatch("(" . $this->createSphinxMatchPhrase($conditionStr) . ")");
     }
 }
 public function applyCondition(IKalturaIndexQuery $query)
 {
     $condition = $this->getCondition();
     KalturaLog::debug("condition [" . print_r($condition, true) . "]");
     $key = '@' . ContentDistributionSphinxPlugin::getSphinxFieldName(ContentDistributionPlugin::SPHINX_EXPANDER_FIELD_DATA);
     $query->addMatch("({$key} {$condition})");
 }
 public function applyCondition(IKalturaIndexQuery $query, $xPaths = null)
 {
     $this->parentQuery = $query;
     if (!$this->condition) {
         if (is_null($xPaths)) {
             $xPaths = array();
             $profileFields = MetadataProfileFieldPeer::retrieveActiveByMetadataProfileId($this->metadataProfileId);
             foreach ($profileFields as $profileField) {
                 $xPaths[$profileField->getXpath()] = $profileField;
             }
         }
         $dataConditions = array();
         $subConditions = array();
         $pluginName = MetadataPlugin::PLUGIN_NAME;
         if (count($this->items)) {
             foreach ($this->items as $item) {
                 $dataCondition = null;
                 if ($item instanceof AdvancedSearchFilterComparableCondition) {
                     /* @var $item AdvancedSearchFilterComparableCondition  */
                     $field = $item->getField();
                     if (!isset($xPaths[$field])) {
                         $this->addCondition('1 <> 1');
                         KalturaLog::ERR("Missing field: {$field} in xpath array: " . print_r($xPaths, true));
                         continue;
                     }
                     switch ($item->getComparison()) {
                         case KalturaSearchConditionComparison::EQUEL:
                             $comparison = ' = ';
                             break;
                         case KalturaSearchConditionComparison::GREATER_THAN:
                             $comparison = ' > ';
                             break;
                         case KalturaSearchConditionComparison::GREATER_THAN_OR_EQUEL:
                             $comparison = ' >= ';
                             break;
                         case KalturaSearchConditionComparison::LESS_THAN:
                             $comparison = " < ";
                             break;
                         case KalturaSearchConditionComparison::LESS_THAN_OR_EQUEL:
                             $comparison = " <= ";
                             break;
                         default:
                             KalturaLog::ERR("Missing comparison type");
                             continue;
                     }
                     $metadataField = $this->getMetadataSearchField($field, $xPaths);
                     if (!$metadataField) {
                         KalturaLog::ERR("Missing metadataField for {$field} in xpath array: " . print_r($xPaths, true));
                         continue;
                     }
                     $value = $item->getValue();
                     if (!is_numeric($value)) {
                         switch ($value) {
                             case Criteria::CURRENT_DATE:
                                 $d = getdate();
                                 $value = mktime(0, 0, 0, $d['mon'], $d['mday'], $d['year']);
                                 break;
                             case Criteria::CURRENT_TIME:
                             case Criteria::CURRENT_TIMESTAMP:
                                 $value = time();
                                 break;
                             default:
                                 if ($xPaths[$field]->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_DATE || $xPaths[$field]->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_INT) {
                                     $this->addCondition('1 <> 1');
                                     KalturaLog::ERR("wrong search value: {$field} is numeric. search value: " . print_r($item->getValue(), true));
                                     continue;
                                 }
                                 $value = SphinxUtils::escapeString($value);
                                 break;
                         }
                     }
                     $newCondition = $metadataField . $comparison . $value;
                     if ($item->getComparison() != KalturaSearchConditionComparison::EQUEL) {
                         $newCondition = "({$newCondition} AND {$metadataField} <> 0)";
                     }
                     $this->addCondition($newCondition);
                 } elseif ($item instanceof AdvancedSearchFilterCondition) {
                     $field = $item->getField();
                     if (!isset($xPaths[$field])) {
                         $this->addCondition('1 <> 1');
                         KalturaLog::ERR("Missing field: {$field} in xpath array: " . print_r($xPaths, true));
                         continue;
                     }
                     $value = $item->getValue();
                     $value = SphinxUtils::escapeString($value);
                     $fieldId = $xPaths[$field]->getId();
                     // any value in the field
                     if (trim($value) == '*') {
                         $dataCondition = "{$pluginName}_{$fieldId}";
                     } elseif ($xPaths[$field]->getType() == self::KMC_FIELD_TYPE_LIST) {
                         $dataCondition = "\\\"{$pluginName}_{$fieldId} {$value} " . kMetadataManager::SEARCH_TEXT_SUFFIX . "_{$fieldId}" . "\\\"";
                     } else {
                         $dataCondition = "{$pluginName}_{$fieldId} << ( {$value} ) << " . kMetadataManager::SEARCH_TEXT_SUFFIX . "_{$fieldId}";
                     }
                     kalturalog::debug("add {$dataCondition}");
                     $dataConditions[] = "( {$dataCondition} )";
                 } elseif ($item instanceof MetadataSearchFilter) {
                     $item->applyCondition($this, $xPaths);
                 }
             }
         }
         if (count($dataConditions)) {
             $glue = $this->type == MetadataSearchFilter::SEARCH_AND ? ' ' : ' | ';
             $dataConditions = array_unique($dataConditions);
             $key = '@' . $this->getMetadataSearchField();
             $value = implode($glue, $dataConditions);
             $this->addMatch("{$key} {$value}");
         }
         $matchClause = array_unique($this->matchClause);
         $glue = $this->type == self::SEARCH_AND ? ' ' : ' | ';
         $this->condition = implode($glue, $matchClause);
         if ($this->type == self::SEARCH_OR) {
             $this->condition = "( {$this->condition} )";
         }
     }
     if ($this->condition) {
         $query->addMatch($this->condition);
     }
     if (isset($this->orderBy)) {
         $orderByField = substr($this->orderBy, 1);
         $orderByAscending = $this->orderBy[0] == '+' ? true : false;
         $metadataField = $this->getMetadataSearchField($orderByField);
         if ($metadataField) {
             if ($orderByAscending) {
                 $query->addOrderBy($metadataField, Criteria::ASC);
             } else {
                 $query->addOrderBy($metadataField, Criteria::DESC);
             }
         }
     }
 }
 private function addCondition($conditionStr, IKalturaIndexQuery $query)
 {
     if (!is_null($conditionStr)) {
         $condition = "ca_prefix<<{$conditionStr}<<ca_sufix";
         KalturaLog::debug("condition [" . print_r($condition, true) . "]");
         $key = '@' . CaptionSearchPlugin::getSearchFieldName(CaptionSearchPlugin::SEARCH_FIELD_DATA);
         $query->addMatch("({$key} {$condition})");
     }
 }