From 361840203b217b03eca1839bfe11a172a5cccd40 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 18 Apr 2011 13:04:24 +0400 Subject: [PATCH] filter_test: proper implementation using queryFeedHeadlines --- functions.php | 65 ++++++++++++++++++++++++++++++++++++++-- modules/pref-filters.php | 53 ++++++++++++++------------------ 2 files changed, 85 insertions(+), 33 deletions(-) diff --git a/functions.php b/functions.php index b9974437..43e0e715 100644 --- a/functions.php +++ b/functions.php @@ -2338,7 +2338,7 @@ } function sql_bool_to_bool($s) { - if ($s == "t" || $s == "1") { + if ($s == "t" || $s == "1" || $s == "true") { return true; } else { return false; @@ -3369,7 +3369,7 @@ return $search_query_part; } - function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) { + function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0, $filter = false) { if (!$owner_uid) $owner_uid = $_SESSION["uid"]; @@ -3394,6 +3394,12 @@ $search_query_part = ""; } + if ($filter) { + $filter_query_part = filter_to_sql($filter); + } else { + $filter_query_part = ""; + } + $view_query_part = ""; if ($view_mode == "adaptive" || $view_query_part == "noscores") { @@ -3623,6 +3629,7 @@ ttrss_user_entries.ref_id = ttrss_entries.id AND ttrss_user_entries.owner_uid = '$owner_uid' AND $search_query_part + $filter_query_part $view_query_part $query_strategy_part ORDER BY $order_by $limit_query_part $offset_query_part"; @@ -7245,4 +7252,58 @@ return $result; } + function filter_to_sql($filter) { + $query = ""; + + if (DB_TYPE == "pgsql") + $reg_qpart = "~"; + else + $reg_qpart = "REGEXP"; + + switch ($filter["type"]) { + case "title": + $query = "LOWER(ttrss_entries.title) $reg_qpart LOWER('". + $filter['reg_exp'] . "')"; + break; + case "content": + $query = "LOWER(ttrss_entries.content) $reg_qpart LOWER('". + $filter['reg_exp'] . "')"; + break; + case "both": + $query = "LOWER(ttrss_entries.title) $reg_qpart LOWER('". + $filter['reg_exp'] . "') OR LOWER(" . + "ttrss_entries.content) $reg_qpart LOWER('" . $filter['reg_exp'] . "')"; + break; + case "tag": + $query = "LOWER(ttrss_user_entries.tag_cache) $reg_qpart LOWER('". + $filter['reg_exp'] . "')"; + break; + case "link": + $query = "LOWER(ttrss_entries.link) $reg_qpart LOWER('". + $filter['reg_exp'] . "')"; + break; + case "date": + + if ($filter["filter_param"] == "before") + $cmp_qpart = "<"; + else + $cmp_qpart = ">="; + + $timestamp = date("Y-m-d H:N:s", strtotime($filter["reg_exp"])); + $query = "ttrss_entries.date_entered $cmp_qpart '$timestamp'"; + break; + case "author": + $query = "LOWER(ttrss_entries.author) $reg_qpart LOWER('". + $filter['reg_exp'] . "')"; + break; + } + + if ($filter["inverse"]) + $query = "NOT ($query)"; + + if ($query) + $query .= " AND "; + + return $query; + } ?> diff --git a/modules/pref-filters.php b/modules/pref-filters.php index bba4ff71..860d18f1 100644 --- a/modules/pref-filters.php +++ b/modules/pref-filters.php @@ -12,10 +12,10 @@ $filter["reg_exp"] = $reg_exp; $filter["action"] = $action_name; - + $filter["type"] = $type_name; $filter["action_param"] = $action_param; $filter["filter_param"] = $filter_param; - $filter["inverse"] = sql_bool_to_bool($inverse); + $filter["inverse"] = $inverse; $filters[$type_name] = array($filter); @@ -27,8 +27,8 @@ $feed_title = getFeedTitle($link, $feed); $qfh_ret = queryFeedHeadlines($link, $feed, - 300, "", false, false, false, - false, "date_entered DESC", 0, $_SESSION["uid"]); + 30, "", false, false, false, + false, "date_entered DESC", 0, $_SESSION["uid"], $filter); $result = $qfh_ret[0]; @@ -45,43 +45,34 @@ $entry_timestamp = strtotime($line["updated"]); $entry_tags = get_article_tags($link, $line["id"], $_SESSION["uid"]); - $article_filters = get_article_filters($filters, $line["title"], - $line["content_preview"], $line["link"], - $entry_timestamp, $line["author"], $entry_tags); - - if (count($article_filters) != 0) { - - $content_preview = truncate_string( - strip_tags($line["content_preview"]), 100, '...'); + $content_preview = truncate_string( + strip_tags($line["content_preview"]), 100, '...'); - if ($line["feed_title"]) - $feed_title = $line["feed_title"]; + if ($line["feed_title"]) + $feed_title = $line["feed_title"]; - print ""; + print ""; - print ""; - print ""; + print ""; + print ""; - print $line["title"]; - print " ("; - print "" . $feed_title . ""; - print "): "; - print "" . $content_preview . ""; + print $line["title"]; + print " ("; + print "" . $feed_title . ""; + print "): "; + print "" . $content_preview . ""; + print $line["date_entered"]; - print ""; - - $found++; - } + print ""; - if ($found >= 30) - break; + $found++; } if ($found == 0) { print "" . - __("No recent articles matching this filter has been found.") . ""; + __("No articles matching this filter has been found.") . ""; } print ""; -- 2.39.5