Changeset View
Changeset View
Standalone View
Standalone View
src/applications/search/compiler/PhutilSearchQueryCompiler.php
Show First 20 Lines • Show All 237 Lines • ▼ Show 20 Lines | private function tokenizeQuery($query) { | ||||
} | } | ||||
if ($is_quoted) { | if ($is_quoted) { | ||||
throw new PhutilSearchQueryCompilerSyntaxException( | throw new PhutilSearchQueryCompilerSyntaxException( | ||||
pht( | pht( | ||||
'Query contains unmatched double quotes.')); | 'Query contains unmatched double quotes.')); | ||||
} | } | ||||
if ($mode == 'operator') { | // If the input query has trailing space, like "a b ", we may exit the | ||||
throw new PhutilSearchQueryCompilerSyntaxException( | // parser without a final token. | ||||
pht( | if ($current_function !== null || $current_operator || $current_token) { | ||||
'Query contains operator ("%s") with no search term.', | |||||
implode('', $current_operator))); | |||||
} | |||||
$token = array( | $token = array( | ||||
'operator' => $current_operator, | 'operator' => $current_operator, | ||||
'quoted' => false, | 'quoted' => false, | ||||
'value' => $current_token, | 'value' => $current_token, | ||||
); | ); | ||||
if ($enable_functions) { | if ($enable_functions) { | ||||
$token['function'] = $current_function; | $token['function'] = $current_function; | ||||
} | } | ||||
$tokens[] = $token; | $tokens[] = $token; | ||||
} | |||||
$results = array(); | $results = array(); | ||||
foreach ($tokens as $token) { | foreach ($tokens as $token) { | ||||
$value = implode('', $token['value']); | $value = implode('', $token['value']); | ||||
$operator_string = implode('', $token['operator']); | $operator_string = implode('', $token['operator']); | ||||
if (!strlen($value)) { | |||||
continue; | |||||
} | |||||
$is_quoted = $token['quoted']; | $is_quoted = $token['quoted']; | ||||
switch ($operator_string) { | switch ($operator_string) { | ||||
case '-': | case '-': | ||||
$operator = self::OPERATOR_NOT; | $operator = self::OPERATOR_NOT; | ||||
break; | break; | ||||
case '~': | case '~': | ||||
$operator = self::OPERATOR_SUBSTRING; | $operator = self::OPERATOR_SUBSTRING; | ||||
Show All 17 Lines | foreach ($tokens as $token) { | ||||
break; | break; | ||||
default: | default: | ||||
throw new PhutilSearchQueryCompilerSyntaxException( | throw new PhutilSearchQueryCompilerSyntaxException( | ||||
pht( | pht( | ||||
'Query has an invalid sequence of operators ("%s").', | 'Query has an invalid sequence of operators ("%s").', | ||||
$operator_string)); | $operator_string)); | ||||
} | } | ||||
if (!strlen($value)) { | |||||
$require_value = $is_quoted; | |||||
switch ($operator) { | |||||
default: | |||||
$require_value = true; | |||||
break; | |||||
} | |||||
if ($require_value) { | |||||
throw new PhutilSearchQueryCompilerSyntaxException( | |||||
pht( | |||||
'Query contains a token ("%s") with no search term. Query '. | |||||
'tokens specify text to search for.', | |||||
$this->getDisplayToken($token))); | |||||
} | |||||
} | |||||
$result = array( | $result = array( | ||||
'operator' => $operator, | 'operator' => $operator, | ||||
'quoted' => $is_quoted, | 'quoted' => $is_quoted, | ||||
'value' => $value, | 'value' => $value, | ||||
); | ); | ||||
if ($enable_functions) { | if ($enable_functions) { | ||||
$result['function'] = $token['function']; | $result['function'] = $token['function']; | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 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; | ||||
} | } | ||||
private function getDisplayToken(array $token) { | |||||
if (isset($token['function'])) { | |||||
$function = $token['function'].':'; | |||||
} else { | |||||
$function = ''; | |||||
} | |||||
$operator_string = implode('', $token['operator']); | |||||
$value = implode('', $token['value']); | |||||
$is_quoted = $token['quoted']; | |||||
if ($is_quoted) { | |||||
$value = $this->quoteToken($value); | |||||
} | |||||
return sprintf('%s%s%s', $function, $operator_string, $value); | |||||
} | |||||
} | } |