diff --git a/fts.php b/fts.php index ffa8e9a..d1c4a36 100644 --- a/fts.php +++ b/fts.php @@ -12,6 +12,7 @@ * Manage the sentences (enclosed in quotes), or the standalone words, * Manage the non wanted field (beginning by -), * Do not search if the word is smaller than a parameter. + * Each sentence or word can be modified by external methods separately */ class fts { @@ -35,6 +36,14 @@ class fts /** The regexes created by the parser */ private $regexes = null; + + /** The callable method to run on each word of the query + */ + private $callTokenWord = null; + + /** The callable method to run on each sentence of the query + */ + private $callTokenSentence = null; // }}} //////////////////////////// @@ -81,10 +90,43 @@ class fts } // }}} + /** Set the method to call on tokens word only + * The method must return the token updated + * @param callable $method The callable method + * @return $this + */ + public function callTokenWord ($callable) + // {{{ + { + if (! is_callable ($callable)) + throw new \Exception (dgettext ("domframework", + "SSE : callTokenWord : provided method is not callable"), 500); + $this->callTokenWord = $callable; + return $this; + } + // }}} + + /** Set the method to call on tokens sentence only + * The method must return the token updated + * @param callable $method The callable method + * @return $this + */ + public function callTokenSentence ($callable) + // {{{ + { + if (! is_callable ($callable)) + throw new \Exception (dgettext ("domframework", + "SSE : callTokenSentence : provided method is not callable"), 500); + $this->callTokenSentence = $callable; + return $this; + } + // }}} + ////////////////////////////// //// PUBLIC METHODS //// ////////////////////////////// - /** Search the text provided in $query in the database + /** Explode the query text provided in $query, to be used to search in + * database, file... * @param string $query The text to found in the database * @return array The operator and the associated regex value to search */ @@ -93,6 +135,14 @@ class fts { $query = trim ($query); $this->tokens = $this->tokenizer ($query); + foreach ($this->tokens["tokens"] as $key => &$token) + { + if ($this->tokens["sentences"][$key] === true && $this->callTokenSentence) + $token = call_user_func ($this->callTokenSentence, $token); + elseif ($this->tokens["sentences"][$key] === false && + $this->callTokenWord) + $token = call_user_func ($this->callTokenWord, $token); + } $this->tokensMin = $this->tokenMinLength ($this->tokens["tokens"], $this->tokens["minuses"]); $this->regexes = $this->regex ($this->tokensMin["tokens"], @@ -101,6 +151,28 @@ class fts } // }}} + /** Construct the query based on the tokens. + * The tokens can be updated by methods so the query may be modified by the + * external methods + * @return string + */ + public function getQuery () + // {{{ + { + $res = ""; + foreach ($this->tokens["tokens"] as $key => $token) + { + if ($key > 0) + $res .= " "; + if ($this->tokens["sentences"][$key] === true) + $res .= "\"$token\""; + else + $res .= "$token"; + } + return $res; + } + // }}} + /** Return $line if the $query match against $line, or false if not * @param string $line The line to examine * @param string $query The query to apply on it @@ -252,6 +324,7 @@ class fts throw new \Exception ("Invalid query provided to fts:tokenizer", 500); $debug = false; $tokens = array (); + $sentences = array (); $minuses = array (); // Look for sentences $offset = 0; @@ -281,6 +354,7 @@ class fts " with $nbchars chars)\n"; $token = substr ($query, $offset + 1, $nbchars); $tokens[] = $token; + $sentences[] = true; $minuses[] = $minus; $offset = $end + 1; continue; @@ -294,11 +368,14 @@ class fts if ($debug) echo "WORD FOUND (Start $offset with $nbchars chars)\n"; $token = substr ($query, $offset, $nbchars); $tokens[] = $token; + $sentences[] = false; $minuses[] = $minus; $offset = $end + 1; } if ($debug) print_r ($tokens); - return array ("tokens"=>$tokens, "minuses"=>$minuses); + return array ("tokens" => $tokens, + "sentences" => $sentences, + "minuses" => $minuses); } // }}} }