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
* 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 non wanted field (beginning by -),
* Do not search if the word is smaller than a parameter.
*/
class fts
{
///////////////////////////
//// PROPERTIES ////
///////////////////////////
// {{{
/** The minimum length of a token to search
*/
public $minLength = 3;
@@ -29,42 +34,61 @@ class fts
/** The regexes created by the parser
*/
private $regexes = null;
// }}}
/** 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;
}
////////////////////////////
//// CONSTRUCTOR ////
////////////////////////////
/** The constructor check the availability of the MB module
*/
public function __construct ()
// {{{
{
if (! function_exists ("mb_strlen"))
throw new \Exception ("PHP don't have the MB Support. Please add it !",
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
* @param string $query The text to found in the database
* @return array The operator and the associated regex value to search
*/
public function search ($query)
// {{{
{
$query = trim ($query);
$this->tokens = $this->tokenizer ($query);
@@ -74,6 +98,32 @@ class fts
$this->tokensMin["minuses"]);
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
* @param string $query The text to found in the database
@@ -83,6 +133,7 @@ class fts
* @return array The result of the query
*/
public function searchSQL ($query, $dblayeroo, $fields)
// {{{
{
$regexes = $this->search ($query);
if (empty ($regexes["operator"]))
@@ -125,13 +176,18 @@ class fts
}
return $dbl->execute ();
}
// }}}
///////////////////////////////
//// PRIVATE METHODS ////
///////////////////////////////
/** Create the regex associated to the provided tokens and minuses
* @param array $tokens The token list
* @param array $minuses The minuses list
* @return array The operator and the associated regex value to search
*/
private function regex ($tokens, $minuses)
// {{{
{
if (! is_array ($tokens))
throw new \Exception ("Invalid tokens provided to fts:tokenMinLength",
@@ -153,6 +209,7 @@ class fts
}
return array ("operator"=>$operator, "value"=>$value);
}
// }}}
/** Remove the tokens with too small length. Remove the not desired minuses
* too.
@@ -161,6 +218,7 @@ class fts
* @return array tokens and minuses
*/
private function tokenMinLength ($tokens, $minuses)
// {{{
{
if (! is_array ($tokens))
throw new \Exception ("Invalid tokens provided to fts:tokenMinLength",
@@ -180,12 +238,14 @@ class fts
}
return array ("tokens"=>$newTokens, "minuses"=>$newMinuses);
}
// }}}
/** Return an array with the $query tokenized
* @param string $query The text to tokenize
* @return array tokens and minuses
*/
private function tokenizer ($query)
// {{{
{
if (! is_string ($query))
throw new \Exception ("Invalid query provided to fts:tokenizer", 500);
@@ -239,4 +299,5 @@ class fts
if ($debug) print_r ($tokens);
return array ("tokens"=>$tokens, "minuses"=>$minuses);
}
// }}}
}