fts : Allow to look if a string match against a search string

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@6075 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2020-09-03 13:35:19 +00:00
parent 8113322022
commit 0dcba9466d

105
fts.php
View File

@@ -6,13 +6,18 @@
/** The Full Text Search /** The Full Text Search
* Analyze the provided search text (like a search engine), and create the * Analyze the provided search text (like a search engine), and create the
* sql query to found the answer. * sql query to found the answer. It also allow to check if a sentence is valid
* against a searched text
* Manage the sentences (enclosed in quotes), or the standalone words, * Manage the sentences (enclosed in quotes), or the standalone words,
* Manage the non wanted field (beginning by -), * Manage the non wanted field (beginning by -),
* Do not search if the word is smaller than a parameter. * Do not search if the word is smaller than a parameter.
*/ */
class fts class fts
{ {
///////////////////////////
//// PROPERTIES ////
///////////////////////////
// {{{
/** The minimum length of a token to search /** The minimum length of a token to search
*/ */
public $minLength = 3; public $minLength = 3;
@@ -29,42 +34,61 @@ class fts
/** The regexes created by the parser /** The regexes created by the parser
*/ */
private $regexes = null; private $regexes = null;
// }}}
/** Get the tokens store after the search ////////////////////////////
*/ //// CONSTRUCTOR ////
public function getTokens () ////////////////////////////
{
return $this->tokens;
}
/** Get the tokens store after the search, without the too small ones
*/
public function getTokensMin ()
{
return $this->tokensMin;
}
/** Get the regexes defined after the analyzer
*/
public function getRegexes ()
{
return $this->regexes;
}
/** The constructor check the availability of the MB module /** The constructor check the availability of the MB module
*/ */
public function __construct () public function __construct ()
// {{{
{ {
if (! function_exists ("mb_strlen")) if (! function_exists ("mb_strlen"))
throw new \Exception ("PHP don't have the MB Support. Please add it !", throw new \Exception ("PHP don't have the MB Support. Please add it !",
500); 500);
} }
// }}}
////////////////////////
//// GETTERS ////
////////////////////////
/** Get the tokens store after the search
*/
public function getTokens ()
// {{{
{
return $this->tokens;
}
// }}}
/** Get the tokens store after the search, without the too small ones
*/
public function getTokensMin ()
// {{{
{
return $this->tokensMin;
}
// }}}
/** Get the regexes defined after the analyzer
*/
public function getRegexes ()
// {{{
{
return $this->regexes;
}
// }}}
//////////////////////////////
//// PUBLIC METHODS ////
//////////////////////////////
/** Search the text provided in $query in the database /** Search the text provided in $query in the database
* @param string $query The text to found in the database * @param string $query The text to found in the database
* @return array The operator and the associated regex value to search * @return array The operator and the associated regex value to search
*/ */
public function search ($query) public function search ($query)
// {{{
{ {
$query = trim ($query); $query = trim ($query);
$this->tokens = $this->tokenizer ($query); $this->tokens = $this->tokenizer ($query);
@@ -74,6 +98,32 @@ class fts
$this->tokensMin["minuses"]); $this->tokensMin["minuses"]);
return $this->regexes; return $this->regexes;
} }
// }}}
/** 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
* @return string|false The $line if match, false
*/
public function searchString ($line, $query)
// {{{
{
$regexes = $this->search ($query);
if (empty ($this->tokens))
return false;
foreach ($this->tokens["tokens"] as $key => $searchPart)
{
if (trim ($searchPart) === "")
continue;
$match = (strpos ($line, $searchPart) !== false);
if ($this->tokens["minuses"][$key] === "" && $match == 0)
return false;
if ($this->tokens["minuses"][$key] === "-" && $match == 1)
return false;
}
return $line;
}
// }}}
/** Search in SQL /** Search in SQL
* @param string $query The text to found in the database * @param string $query The text to found in the database
@@ -83,6 +133,7 @@ class fts
* @return array The result of the query * @return array The result of the query
*/ */
public function searchSQL ($query, $dblayeroo, $fields) public function searchSQL ($query, $dblayeroo, $fields)
// {{{
{ {
$regexes = $this->search ($query); $regexes = $this->search ($query);
if (empty ($regexes["operator"])) if (empty ($regexes["operator"]))
@@ -125,13 +176,18 @@ class fts
} }
return $dbl->execute (); return $dbl->execute ();
} }
// }}}
///////////////////////////////
//// PRIVATE METHODS ////
///////////////////////////////
/** Create the regex associated to the provided tokens and minuses /** Create the regex associated to the provided tokens and minuses
* @param array $tokens The token list * @param array $tokens The token list
* @param array $minuses The minuses list * @param array $minuses The minuses list
* @return array The operator and the associated regex value to search * @return array The operator and the associated regex value to search
*/ */
private function regex ($tokens, $minuses) private function regex ($tokens, $minuses)
// {{{
{ {
if (! is_array ($tokens)) if (! is_array ($tokens))
throw new \Exception ("Invalid tokens provided to fts:tokenMinLength", throw new \Exception ("Invalid tokens provided to fts:tokenMinLength",
@@ -153,6 +209,7 @@ class fts
} }
return array ("operator"=>$operator, "value"=>$value); return array ("operator"=>$operator, "value"=>$value);
} }
// }}}
/** Remove the tokens with too small length. Remove the not desired minuses /** Remove the tokens with too small length. Remove the not desired minuses
* too. * too.
@@ -161,6 +218,7 @@ class fts
* @return array tokens and minuses * @return array tokens and minuses
*/ */
private function tokenMinLength ($tokens, $minuses) private function tokenMinLength ($tokens, $minuses)
// {{{
{ {
if (! is_array ($tokens)) if (! is_array ($tokens))
throw new \Exception ("Invalid tokens provided to fts:tokenMinLength", throw new \Exception ("Invalid tokens provided to fts:tokenMinLength",
@@ -180,12 +238,14 @@ class fts
} }
return array ("tokens"=>$newTokens, "minuses"=>$newMinuses); return array ("tokens"=>$newTokens, "minuses"=>$newMinuses);
} }
// }}}
/** Return an array with the $query tokenized /** Return an array with the $query tokenized
* @param string $query The text to tokenize * @param string $query The text to tokenize
* @return array tokens and minuses * @return array tokens and minuses
*/ */
private function tokenizer ($query) private function tokenizer ($query)
// {{{
{ {
if (! is_string ($query)) if (! is_string ($query))
throw new \Exception ("Invalid query provided to fts:tokenizer", 500); throw new \Exception ("Invalid query provided to fts:tokenizer", 500);
@@ -239,4 +299,5 @@ class fts
if ($debug) print_r ($tokens); if ($debug) print_r ($tokens);
return array ("tokens"=>$tokens, "minuses"=>$minuses); return array ("tokens"=>$tokens, "minuses"=>$minuses);
} }
// }}}
} }