Skip to content

Commit

Permalink
fix(form): search engine and accented chars
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Nov 23, 2018
1 parent accae14 commit 0583a5f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
21 changes: 21 additions & 0 deletions inc/common.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,25 @@ public static function getMax(CommonDBTM $item, $condition, $fieldName) {
}
return (int) $line[$fieldName];
}

/**
* Prepare keywords for a fulltext search in boolean mode
* takes into account strings in double quotes
*
* @param string $keywords
* @return string
*/
public static function prepareBooleanKeywords($keywords) {
// @see https://stackoverflow.com/questions/2202435/php-explode-the-string-but-treat-words-in-quotes-as-a-single-word
preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $keywords, $matches);
$matches = $matches[0];
foreach ($matches as &$keyword) {
if (strpos($keyword, '"') === 0) {
// keyword does not begins with a double quote (assume it does not ends with this char)
$keyword .= '*';
}
}

return implode(' ', $matches);
}
}
7 changes: 4 additions & 3 deletions inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,6 @@ public function showFormList($rootCategory = 0, $keywords = '', $helpdeskHome =
}

// Find forms accessible by the current user
$keywords = preg_replace("/[^A-Za-z0-9 ]/", '', $keywords);
if (!empty($keywords)) {
// Determine the optimal search mode
$searchMode = "BOOLEAN MODE";
Expand All @@ -696,13 +695,15 @@ public function showFormList($rootCategory = 0, $keywords = '', $helpdeskHome =
$row = $DB->fetch_assoc($result);
if ($row['Rows'] > 20) {
$searchMode = "NATURAL LANGUAGE MODE";
} else {
$keywords = PluginFormcreatorCommon::prepareBooleanKeywords($keywords);
}
}
$keywords = $DB->escape($keywords);
$highWeightedMatch = " MATCH($table_form.`name`, $table_form.`description`)
AGAINST('$keywords*' IN $searchMode)";
AGAINST('$keywords' IN $searchMode)";
$lowWeightedMatch = " MATCH($table_question.`name`, $table_question.`description`)
AGAINST('$keywords*' IN $searchMode)";
AGAINST('$keywords' IN $searchMode)";
$where_form .= " AND ($highWeightedMatch OR $lowWeightedMatch)";
}
$query_forms = "SELECT
Expand Down

0 comments on commit 0583a5f

Please sign in to comment.