From 5f759ab77aa5ee2b009a62990203c2e9d4b05b77 Mon Sep 17 00:00:00 2001 From: rurban Date: Mon, 14 Nov 2005 22:30:17 +0000 Subject: [PATCH] fix VOID, add tsearch2 support git-svn-id: svn://svn.code.sf.net/p/phpwiki/code/trunk@4977 96ab9672-09ca-45d6-a79d-3d69d39ca109 --- lib/TextSearchQuery.php | 48 ++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/lib/TextSearchQuery.php b/lib/TextSearchQuery.php index 9b0d72d50..0c60a9c1b 100644 --- a/lib/TextSearchQuery.php +++ b/lib/TextSearchQuery.php @@ -1,4 +1,4 @@ -_sql_clause($leaf) . ")"; return join(" $node->op ", $subclauses); default: - assert($node->op == VOID); + assert($node->op == 'VOID'); return '1=1'; } } @@ -217,6 +217,38 @@ class TextSearchQuery { } } + /* + postgresql tsearch2 uses no WHERE operators, just & | and ! in the searchstring + */ + function makeTsearch2SqlClauseObj(&$sql_search_cb) { + $this->_sql_clause_cb = $sql_search_cb; + return $this->_Tsearch2Sql_clause_obj($this->_tree); + } + + function _Tsearch2Sql_clause_obj($node) { + // TODO: "such a phrase" + switch ($node->op) { + case 'NOT': + return "!" . $node->leaves[0]; + case 'AND': + $subclauses = array(); + foreach ($node->leaves as $leaf) + $subclauses[] = $this->_Tsearch2Sql_clause_obj($leaf); + return join("&", $subclauses); + case 'OR': + $subclauses = array(); + foreach ($node->leaves as $leaf) + $subclauses[] = $this->_Tsearch2Sql_clause_obj($leaf); + return join("|", $subclauses); + case 'VOID': + return ''; + case 'ALL': + return '1'; + default: + return $this->_sql_clause_cb->call($node); + } + } + function sql() { return '%'.$this->_sql_quote($this->word).'%'; } /** @@ -319,7 +351,7 @@ extends TextSearchQuery_node function regexp() { return '(?=.*' . preg_quote($this->word, '/') . ')'; } - function highlight_words($negated = false) { + function highlight_words ($negated = false) { return $negated ? array() : array($this->word); } function _sql_quote() { @@ -339,28 +371,28 @@ class TextSearchQuery_node_starts_with extends TextSearchQuery_node_word { var $op = "STARTS_WITH"; function regexp() { return '(?=.*\b' . preg_quote($this->word, '/') . ')'; } - function sql() { return $this->_sql_quote($this->word).'%'; } + function sql () { return $this->_sql_quote($this->word).'%'; } } class TextSearchQuery_node_ends_with extends TextSearchQuery_node_word { var $op = "ENDS_WITH"; function regexp() { return '(?=.*' . preg_quote($this->word, '/') . '\b)'; } - function sql() { return '%'.$this->_sql_quote($this->word); } + function sql () { return '%'.$this->_sql_quote($this->word); } } class TextSearchQuery_node_exact extends TextSearchQuery_node_word { var $op = "EXACT"; function regexp() { return '(?=\b' . preg_quote($this->word, '/') . '\b)'; } - function sql() { return $this->_sql_squote($this->word); } + function sql () { return $this->_sql_squote($this->word); } } class TextSearchQuery_node_regex // posix regex. FIXME! extends TextSearchQuery_node_word { var $op = "REGEX"; // using REGEXP or ~ extension function regexp() { return '(?=.*\b' . $this->word . '\b)'; } - function sql() { return $this->_sql_quote($this->word); } + function sql () { return $this->_sql_quote($this->word); } } class TextSearchQuery_node_regex_glob @@ -407,7 +439,7 @@ extends TextSearchQuery_node return '(?!' . $leaf->regexp() . ')'; } - function highlight_words($negated = false) { + function highlight_words ($negated = false) { return $this->leaves[0]->highlight_words(!$negated); } } -- 2.45.0