Commit 0c1f3ec9 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Interpret double-quoted phrases in user search queries as single terms



Change-Id: Ie46e9d05ee6f7774be604f1ba8d35c0feddfa00d
Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent c0f4f45c
......@@ -109,7 +109,7 @@ class PluginSearchInternal extends PluginSearch {
}
$sql .= "
LEFT OUTER JOIN {usr_account_preference} h ON (u.id = h.usr AND h.field = 'hiderealname')";
$querydata = split(' ', preg_replace('/\s\s+/', ' ', strtolower(trim($query_string))));
$querydata = self::split_query_string(strtolower(trim($query_string)));
$hidenameallowed = get_config('userscanhiderealnames') ? 'TRUE' : 'FALSE';
$searchusernamesallowed = get_config('searchusernames') ? 'TRUE' : 'FALSE';
......@@ -192,6 +192,45 @@ class PluginSearchInternal extends PluginSearch {
return $alias . '.' . $field . ' ' . db_ilike() . " '%' || ? || '%'";
}
/**
* Split a query string into search terms.
*
* Contents of double-quoted strings are counted as a single term,
* '"' can be entered as '\"', '\' as '\\'.
*/
private static function split_query_string($query) {
$terms = array();
// Split string on unescaped double quotes
$quotesplit = preg_split('/(?<!\\\)(\")/', $query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$inphrase = false;
foreach ($quotesplit as $q) {
if ($q == '"') {
$inphrase = !$inphrase;
continue;
}
// Remove escaping
$q = preg_replace(array('/\x5C(?!\x5C)/u', '/\x5C\x5C/u'), array('','\\'), $q);
if ($inphrase) {
if ($trimmed = trim($q)) {
$terms[] = $trimmed;
}
}
else {
// Split unquoted sequences on spaces
foreach (preg_split('/\s+/', $q, -1, PREG_SPLIT_NO_EMPTY) as $word) {
$terms[] = $word;
}
}
}
return $terms;
}
private static function prepare_search_user_options($options) {
if (isset($options['group'])) {
$options['group'] = intval($options['group']);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment