Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F15358051
D17669.id42501.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
D17669.id42501.diff
View Options
diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -390,6 +390,7 @@
'PhutilSearchQueryCompiler' => 'search/PhutilSearchQueryCompiler.php',
'PhutilSearchQueryCompilerSyntaxException' => 'search/PhutilSearchQueryCompilerSyntaxException.php',
'PhutilSearchQueryCompilerTestCase' => 'search/__tests__/PhutilSearchQueryCompilerTestCase.php',
+ 'PhutilSearchQueryToken' => 'search/PhutilSearchQueryToken.php',
'PhutilSearchStemmer' => 'search/PhutilSearchStemmer.php',
'PhutilSearchStemmerTestCase' => 'search/__tests__/PhutilSearchStemmerTestCase.php',
'PhutilServiceProfiler' => 'serviceprofiler/PhutilServiceProfiler.php',
@@ -1003,6 +1004,7 @@
'PhutilSearchQueryCompiler' => 'Phobject',
'PhutilSearchQueryCompilerSyntaxException' => 'Exception',
'PhutilSearchQueryCompilerTestCase' => 'PhutilTestCase',
+ 'PhutilSearchQueryToken' => 'Phobject',
'PhutilSearchStemmer' => 'Phobject',
'PhutilSearchStemmerTestCase' => 'PhutilTestCase',
'PhutilServiceProfiler' => 'Phobject',
diff --git a/src/search/PhutilSearchQueryCompiler.php b/src/search/PhutilSearchQueryCompiler.php
--- a/src/search/PhutilSearchQueryCompiler.php
+++ b/src/search/PhutilSearchQueryCompiler.php
@@ -19,15 +19,6 @@
return $this->operators;
}
- public function setQuery($query) {
- $this->query = $query;
- return $this;
- }
-
- public function getQuery() {
- return $this->query;
- }
-
public function setStemmer(PhutilSearchStemmer $stemmer) {
$this->stemmer = $stemmer;
return $this;
@@ -37,9 +28,8 @@
return $this->stemmer;
}
- public function compileQuery() {
- $query = $this->getQuery();
- $tokens = $this->tokenizeQuery($query);
+ public function compileQuery(array $tokens) {
+ assert_instances_of($tokens, 'PhutilSearchQueryToken');
$result = array();
foreach ($tokens as $token) {
@@ -49,13 +39,12 @@
return $this->compileRenderedTokens($result);
}
- public function compileLiteralQuery() {
- $query = $this->getQuery();
- $tokens = $this->tokenizeQuery($query);
+ public function compileLiteralQuery(array $tokens) {
+ assert_instances_of($tokens, 'PhutilSearchQueryToken');
$result = array();
foreach ($tokens as $token) {
- if (!$token['quoted']) {
+ if (!$token->isQuoted()) {
continue;
}
$result[] = $this->renderToken($token);
@@ -64,13 +53,12 @@
return $this->compileRenderedTokens($result);
}
- public function compileStemmedQuery() {
- $query = $this->getQuery();
- $tokens = $this->tokenizeQuery($query);
+ public function compileStemmedQuery(array $tokens) {
+ assert_instances_of($tokens, 'PhutilSearchQueryToken');
$result = array();
foreach ($tokens as $token) {
- if ($token['quoted']) {
+ if ($token->isQuoted()) {
continue;
}
$result[] = $this->renderToken($token, $this->getStemmer());
@@ -88,6 +76,17 @@
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) {
$maximum_bytes = 1024;
@@ -233,16 +232,16 @@
}
private function renderToken(
- array $token,
+ PhutilSearchQueryToken $token,
PhutilSearchStemmer $stemmer = null) {
- $value = $token['value'];
+ $value = $token->getValue();
if ($stemmer) {
$value = $stemmer->stemToken($value);
}
$value = $this->quoteToken($value);
- $operator = $token['operator'];
+ $operator = $token->getOperator();
$prefix = $this->getOperatorPrefix($operator);
$value = $prefix.$value;
@@ -283,5 +282,4 @@
return $open_quote.$value.$close_quote;
}
-
}
diff --git a/src/search/PhutilSearchQueryToken.php b/src/search/PhutilSearchQueryToken.php
new file mode 100644
--- /dev/null
+++ b/src/search/PhutilSearchQueryToken.php
@@ -0,0 +1,31 @@
+<?php
+
+final class PhutilSearchQueryToken extends Phobject {
+
+ private $isQuoted;
+ private $value;
+ private $operator;
+
+ public static function newFromDictionary(array $dictionary) {
+ $token = new self();
+
+ $token->isQuoted = $dictionary['quoted'];
+ $token->operator = $dictionary['operator'];
+ $token->value = $dictionary['value'];
+
+ return $token;
+ }
+
+ public function isQuoted() {
+ return $this->isQuoted;
+ }
+
+ public function getValue() {
+ return $this->value;
+ }
+
+ public function getOperator() {
+ return $this->operator;
+ }
+
+}
diff --git a/src/search/__tests__/PhutilSearchQueryCompilerTestCase.php b/src/search/__tests__/PhutilSearchQueryCompilerTestCase.php
--- a/src/search/__tests__/PhutilSearchQueryCompilerTestCase.php
+++ b/src/search/__tests__/PhutilSearchQueryCompilerTestCase.php
@@ -99,8 +99,7 @@
$stemmed_query = null;
try {
- $compiler = id(new PhutilSearchQueryCompiler())
- ->setQuery($input);
+ $compiler = new PhutilSearchQueryCompiler();
if ($operators !== null) {
$compiler->setOperators($operators);
@@ -110,11 +109,13 @@
$compiler->setStemmer($stemmer);
}
+ $tokens = $compiler->newTokens($input);
+
if ($stemmer) {
- $literal_query = $compiler->compileLiteralQuery();
- $stemmed_query = $compiler->compileStemmedQuery();
+ $literal_query = $compiler->compileLiteralQuery($tokens);
+ $stemmed_query = $compiler->compileStemmedQuery($tokens);
} else {
- $query = $compiler->compileQuery();
+ $query = $compiler->compileQuery($tokens);
}
} catch (PhutilSearchQueryCompilerSyntaxException $ex) {
$caught = $ex;
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Mar 12, 7:11 AM (13 h, 24 m ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7383621
Default Alt Text
D17669.id42501.diff (5 KB)
Attached To
Mode
D17669: Make the query compiler emit intermediate tokens
Attached
Detach File
Event Timeline
Log In to Comment