Changeset View
Changeset View
Standalone View
Standalone View
src/search/PhutilSearchQueryCompiler.php
| Show All 13 Lines | public function setOperators($operators) { | ||||
| $this->operators = $operators; | $this->operators = $operators; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getOperators() { | public function getOperators() { | ||||
| return $this->operators; | return $this->operators; | ||||
| } | } | ||||
| public function setQuery($query) { | |||||
| $this->query = $query; | |||||
| return $this; | |||||
| } | |||||
| public function getQuery() { | |||||
| return $this->query; | |||||
| } | |||||
| public function setStemmer(PhutilSearchStemmer $stemmer) { | public function setStemmer(PhutilSearchStemmer $stemmer) { | ||||
| $this->stemmer = $stemmer; | $this->stemmer = $stemmer; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getStemmer() { | public function getStemmer() { | ||||
| return $this->stemmer; | return $this->stemmer; | ||||
| } | } | ||||
| public function compileQuery() { | public function compileQuery(array $tokens) { | ||||
| $query = $this->getQuery(); | assert_instances_of($tokens, 'PhutilSearchQueryToken'); | ||||
| $tokens = $this->tokenizeQuery($query); | |||||
| $result = array(); | $result = array(); | ||||
| foreach ($tokens as $token) { | foreach ($tokens as $token) { | ||||
| $result[] = $this->renderToken($token); | $result[] = $this->renderToken($token); | ||||
| } | } | ||||
| return $this->compileRenderedTokens($result); | return $this->compileRenderedTokens($result); | ||||
| } | } | ||||
| public function compileLiteralQuery() { | public function compileLiteralQuery(array $tokens) { | ||||
| $query = $this->getQuery(); | assert_instances_of($tokens, 'PhutilSearchQueryToken'); | ||||
| $tokens = $this->tokenizeQuery($query); | |||||
| $result = array(); | $result = array(); | ||||
| foreach ($tokens as $token) { | foreach ($tokens as $token) { | ||||
| if (!$token['quoted']) { | if (!$token->isQuoted()) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| $result[] = $this->renderToken($token); | $result[] = $this->renderToken($token); | ||||
| } | } | ||||
| return $this->compileRenderedTokens($result); | return $this->compileRenderedTokens($result); | ||||
| } | } | ||||
| public function compileStemmedQuery() { | public function compileStemmedQuery(array $tokens) { | ||||
| $query = $this->getQuery(); | assert_instances_of($tokens, 'PhutilSearchQueryToken'); | ||||
| $tokens = $this->tokenizeQuery($query); | |||||
| $result = array(); | $result = array(); | ||||
| foreach ($tokens as $token) { | foreach ($tokens as $token) { | ||||
| if ($token['quoted']) { | if ($token->isQuoted()) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| $result[] = $this->renderToken($token, $this->getStemmer()); | $result[] = $this->renderToken($token, $this->getStemmer()); | ||||
| } | } | ||||
| return $this->compileRenderedTokens($result); | return $this->compileRenderedTokens($result); | ||||
| } | } | ||||
| private function compileRenderedTokens(array $list) { | private function compileRenderedTokens(array $list) { | ||||
| if (!$list) { | if (!$list) { | ||||
| return null; | return null; | ||||
| } | } | ||||
| $list = array_unique($list); | $list = array_unique($list); | ||||
| return implode(' ', $list); | return implode(' ', $list); | ||||
| } | } | ||||
| public function newTokens($query) { | |||||
| $results = $this->tokenizeQuery($query); | |||||
| $tokens = array(); | |||||
| foreach ($results as $result) { | |||||
| $tokens[] = PhutilSearchQueryToken::newFromDictionary($result); | |||||
| } | |||||
| return $tokens; | |||||
| } | |||||
| private function tokenizeQuery($query) { | private function tokenizeQuery($query) { | ||||
| $maximum_bytes = 1024; | $maximum_bytes = 1024; | ||||
| $query_bytes = strlen($query); | $query_bytes = strlen($query); | ||||
| if ($query_bytes > $maximum_bytes) { | if ($query_bytes > $maximum_bytes) { | ||||
| throw new PhutilSearchQueryCompilerSyntaxException( | throw new PhutilSearchQueryCompilerSyntaxException( | ||||
| pht( | pht( | ||||
| 'Query is too long (%s bytes, maximum is %s bytes).', | 'Query is too long (%s bytes, maximum is %s bytes).', | ||||
| ▲ Show 20 Lines • Show All 129 Lines • ▼ Show 20 Lines | foreach ($tokens as $token) { | ||||
| 'value' => $value, | 'value' => $value, | ||||
| ); | ); | ||||
| } | } | ||||
| return $results; | return $results; | ||||
| } | } | ||||
| private function renderToken( | private function renderToken( | ||||
| array $token, | PhutilSearchQueryToken $token, | ||||
| PhutilSearchStemmer $stemmer = null) { | PhutilSearchStemmer $stemmer = null) { | ||||
| $value = $token['value']; | $value = $token->getValue(); | ||||
| if ($stemmer) { | if ($stemmer) { | ||||
| $value = $stemmer->stemToken($value); | $value = $stemmer->stemToken($value); | ||||
| } | } | ||||
| $value = $this->quoteToken($value); | $value = $this->quoteToken($value); | ||||
| $operator = $token['operator']; | $operator = $token->getOperator(); | ||||
| $prefix = $this->getOperatorPrefix($operator); | $prefix = $this->getOperatorPrefix($operator); | ||||
| $value = $prefix.$value; | $value = $prefix.$value; | ||||
| return $value; | return $value; | ||||
| } | } | ||||
| private function getOperatorPrefix($operator) { | private function getOperatorPrefix($operator) { | ||||
| Show All 24 Lines | private function quoteToken($value) { | ||||
| $operators = $this->operators; | $operators = $this->operators; | ||||
| $open_quote = $this->operators[10]; | $open_quote = $this->operators[10]; | ||||
| $close_quote = $this->operators[11]; | $close_quote = $this->operators[11]; | ||||
| return $open_quote.$value.$close_quote; | return $open_quote.$value.$close_quote; | ||||
| } | } | ||||
| } | } | ||||