From 6aff7845751e1671f436da6686209b414fdcfcc4 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 30 Aug 2012 18:50:56 +0400 Subject: implement multiple rule/action filters --- include/functions.php | 249 ++++++++++++++++++++++++-------------------------- include/rssfuncs.php | 8 +- 2 files changed, 121 insertions(+), 136 deletions(-) (limited to 'include') diff --git a/include/functions.php b/include/functions.php index ddd166f3a..ac0276fc7 100644 --- a/include/functions.php +++ b/include/functions.php @@ -1,6 +1,6 @@ $check_timestamp) { - $match_ok = true; - } - - if ($inverse) $match_ok = !$match_ok; - - if ($match_ok) { - array_push($matches, array($filter["action"], $filter["action_param"])); + $filter_match = $match; + if (!$match) { + break; } } } - } - - if ($filters["author"]) { - foreach ($filters["author"] as $filter) { - $reg_exp = $filter["reg_exp"]; - $inverse = $filter["inverse"]; - if ((!$inverse && @preg_match("/$reg_exp/i", $author)) || - ($inverse && !@preg_match("/$reg_exp/i", $author))) { - array_push($matches, array($filter["action"], $filter["action_param"])); + if ($filter_match) { + foreach ($filter["actions"] AS $action) { + array_push($matches, $action); } } } - if ($filters["tag"]) { - - $tag_string = join(",", $tags); - - foreach ($filters["tag"] as $filter) { - $reg_exp = $filter["reg_exp"]; - $inverse = $filter["inverse"]; - - if ((!$inverse && @preg_match("/$reg_exp/i", $tag_string)) || - ($inverse && !@preg_match("/$reg_exp/i", $tag_string))) { - - array_push($matches, array($filter["action"], $filter["action_param"])); - } - } - } - - return $matches; } function find_article_filter($filters, $filter_name) { foreach ($filters as $f) { - if ($f[0] == $filter_name) { + if ($f["type"] == $filter_name) { return $f; }; } return false; } + function find_article_filters($filters, $filter_name) { + $results = array(); + + foreach ($filters as $f) { + if ($f["type"] == $filter_name) { + array_push($results, $f); + }; + } + return $results; + } + function calculate_article_score($filters) { $score = 0; foreach ($filters as $f) { - if ($f[0] == "score") { - $score += $f[1]; + if ($f["type"] == "score") { + $score += $f["param"]; }; } return $score; @@ -604,8 +557,8 @@ function assign_article_to_labels($link, $id, $filters, $owner_uid) { foreach ($filters as $f) { - if ($f[0] == "label") { - label_add_article($link, $id, $f[1], $owner_uid); + if ($f["type"] == "label") { + label_add_article($link, $id, $f["param"], $owner_uid); }; } } @@ -3650,46 +3603,66 @@ return $text; } - function load_filters($link, $feed, $owner_uid, $action_id = false) { + function load_filters($link, $feed_id, $owner_uid, $action_id = false) { $filters = array(); + $cat_id = getFeedCategory($link, $feed_id); - if ($action_id) $ftype_query_part = "action_id = '$action_id' AND"; - - $result = db_query($link, "SELECT reg_exp, - ttrss_filter_types.name AS name, - ttrss_filter_actions.name AS action, - inverse, - action_param, - filter_param - FROM ttrss_filters - LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = '$feed'), - ttrss_filter_types,ttrss_filter_actions - WHERE - enabled = true AND - $ftype_query_part - ttrss_filters.owner_uid = $owner_uid AND - ttrss_filter_types.id = filter_type AND - ttrss_filter_actions.id = action_id AND - ((cat_filter = true AND ttrss_feeds.cat_id = ttrss_filters.cat_id) OR - (cat_filter = true AND ttrss_feeds.cat_id IS NULL AND - ttrss_filters.cat_id IS NULL) OR - (cat_filter = false AND (feed_id IS NULL OR feed_id = '$feed'))) - ORDER BY reg_exp"); + $result = db_query($link, "SELECT * FROM ttrss_filters2 WHERE + owner_uid = $owner_uid AND enabled = true"); while ($line = db_fetch_assoc($result)) { + $filter_id = $line["id"]; + + $result2 = db_query($link, "SELECT + r.reg_exp, r.feed_id, r.cat_id, r.cat_filter, t.name AS type_name + FROM ttrss_filters2_rules AS r, + ttrss_filter_types AS t + WHERE + (cat_id IS NULL OR cat_id = '$cat_id') AND + (feed_id IS NULL OR feed_id = '$feed_id') AND + filter_type = t.id AND filter_id = '$filter_id'"); + + $rules = array(); + $actions = array(); - if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); - $filter["reg_exp"] = $line["reg_exp"]; - $filter["action"] = $line["action"]; - $filter["action_param"] = $line["action_param"]; - $filter["filter_param"] = $line["filter_param"]; - $filter["inverse"] = sql_bool_to_bool($line["inverse"]); + while ($rule_line = db_fetch_assoc($result2)) { +# print_r($rule_line); - array_push($filters[$line["name"]], $filter); + $rule = array(); + $rule["reg_exp"] = $rule_line["reg_exp"]; + $rule["type"] = $rule_line["type_name"]; + + array_push($rules, $rule); + } + + $result2 = db_query($link, "SELECT a.action_param,t.name AS type_name + FROM ttrss_filters2_actions AS a, + ttrss_filter_actions AS t + WHERE + action_id = t.id AND filter_id = '$filter_id'"); + + while ($action_line = db_fetch_assoc($result2)) { +# print_r($action_line); + + $action = array(); + $action["type"] = $action_line["type_name"]; + $action["param"] = $action_line["action_param"]; + + array_push($actions, $action); } + $filter = array(); + $filter["match_any_rule"] = sql_bool_to_bool($line["match_any_rule"]); + $filter["rules"] = $rules; + $filter["actions"] = $actions; + + if (count($rules) > 0 && count($actions) > 0) { + array_push($filters, $filter); + } + } + return $filters; } @@ -5595,4 +5568,16 @@ return $tempname; } + function getFeedCategory($link, $feed) { + $result = db_query($link, "SELECT cat_id FROM ttrss_feeds + WHERE id = '$feed'"); + + if (db_num_rows($result) > 0) { + return db_fetch_result($result, 0, "cat_id"); + } else { + return false; + } + + } + ?> diff --git a/include/rssfuncs.php b/include/rssfuncs.php index 241896f8b..47e0c68f3 100644 --- a/include/rssfuncs.php +++ b/include/rssfuncs.php @@ -818,8 +818,8 @@ $entry_tags[$i] = mb_strtolower($entry_tags[$i], 'utf-8'); if ($debug_enabled) { - _debug("update_rss_feed: unfiltered tags found:"); - print_r($entry_tags); + //_debug("update_rss_feed: unfiltered tags found:"); + //print_r($entry_tags); } # sanitize content @@ -1190,9 +1190,9 @@ // check for manual tags (we have to do it here since they're loaded from filters) foreach ($article_filters as $f) { - if ($f[0] == "tag") { + if ($f["type"] == "tag") { - $manual_tags = trim_array(explode(",", $f[1])); + $manual_tags = trim_array(explode(",", $f["param"])); foreach ($manual_tags as $tag) { if (tag_is_valid($tag)) { -- cgit v1.2.3