From ccc0315ef0a115fb9111823b351bbcbdf3182f66 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 30 Apr 2019 14:39:08 +0300 Subject: [PATCH] better tsquery support: 1. report query syntax errors properly 2. fall back to implicit &-joining only if no joiners are detected in user query, otherwise permit full tsquery syntax --- classes/feeds.php | 49 ++++++++++++++++++++++++++----------------- include/functions.php | 12 +++++++++-- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/classes/feeds.php b/classes/feeds.php index 2481ac6b5..98bfa0923 100755 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -210,6 +210,8 @@ class Feeds extends Handler_Protected { $highlight_words = $qfh_ret[5]; $reply['first_id'] = $qfh_ret[6]; $reply['is_vfeed'] = $qfh_ret[7]; + $query_error_override = $qfh_ret[8]; + $reply['search_query'] = [$search, $search_language]; $reply['vfeed_group_enabled'] = $vfeed_group_enabled; @@ -387,22 +389,26 @@ class Feeds extends Handler_Protected { if (is_object($result)) { - switch ($view_mode) { - case "unread": - $message = __("No unread articles found to display."); - break; - case "updated": - $message = __("No updated articles found to display."); - break; - case "marked": - $message = __("No starred articles found to display."); - break; - default: - if ($feed < LABEL_BASE_INDEX) { - $message = __("No articles found to display. You can assign articles to labels manually from article header context menu (applies to all selected articles) or use a filter."); - } else { - $message = __("No articles found to display."); - } + if ($query_error_override) { + $message = $query_error_override; + } else { + switch ($view_mode) { + case "unread": + $message = __("No unread articles found to display."); + break; + case "updated": + $message = __("No updated articles found to display."); + break; + case "marked": + $message = __("No starred articles found to display."); + break; + default: + if ($feed < LABEL_BASE_INDEX) { + $message = __("No articles found to display. You can assign articles to labels manually from article header context menu (applies to all selected articles) or use a filter."); + } else { + $message = __("No articles found to display."); + } + } } if (!$offset && $message) { @@ -1421,10 +1427,13 @@ class Feeds extends Handler_Protected { $ext_tables_part = ""; $limit_query_part = ""; + $query_error_override = ""; - $search_words = array(); + $search_words = []; if ($search) { + $search_query_part = ""; + foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SEARCH) as $plugin) { list($search_query_part, $search_words) = $plugin->hook_search($search); break; @@ -1442,6 +1451,8 @@ class Feeds extends Handler_Protected { } catch (PDOException $e) { // looks like tsquery syntax is invalid $search_query_part = "false"; + + $query_error_override = T_sprintf("Incorrect search syntax: %s.", implode(" ", $search_words)); } } @@ -1736,7 +1747,7 @@ class Feeds extends Handler_Protected { $first_id = (int)$row["id"]; if ($offset > 0 && $first_id && $check_first_id && $first_id != $check_first_id) { - return array(-1, $feed_title, $feed_site_url, $last_error, $last_updated, $search_words, $first_id, $vfeed_query_part != ""); + return array(-1, $feed_title, $feed_site_url, $last_error, $last_updated, $search_words, $first_id, $vfeed_query_part != "", $query_error_override); } } } @@ -1825,7 +1836,7 @@ class Feeds extends Handler_Protected { $res = $pdo->query($query); } - return array($res, $feed_title, $feed_site_url, $last_error, $last_updated, $search_words, $first_id, $vfeed_query_part != ""); + return array($res, $feed_title, $feed_site_url, $last_error, $last_updated, $search_words, $first_id, $vfeed_query_part != "", $query_error_override); } diff --git a/include/functions.php b/include/functions.php index 963018858..6dc9990e8 100644 --- a/include/functions.php +++ b/include/functions.php @@ -1486,11 +1486,19 @@ } if (count($search_query_leftover) > 0) { - $search_query_leftover = $pdo->quote(implode(" & ", $search_query_leftover)); if (DB_TYPE == "pgsql") { + + // if there's no joiners consider this a "simple" search and + // concatenate everything with &, otherwise don't try to mess with tsquery syntax + if (preg_match("/[&|]/", implode(" " , $search_query_leftover))) { + $tsquery = $pdo->quote(implode(" ", $search_query_leftover)); + } else { + $tsquery = $pdo->quote(implode(" & ", $search_query_leftover)); + } + array_push($query_keywords, - "(tsvector_combined @@ to_tsquery($search_language, $search_query_leftover))"); + "(tsvector_combined @@ to_tsquery($search_language, $tsquery))"); } }