]> git.wh0rd.org - tt-rss.git/blobdiff - include/functions.php
implement multiple rule/action filters
[tt-rss.git] / include / functions.php
index 69b481ac46c5c46b192722d34b10e891c5f6e341..ac0276fc7607b180ece39b616d8d9d1af1d675ba 100644 (file)
@@ -1,6 +1,6 @@
 <?php
-       define('EXPECTED_CONFIG_VERSION', 25);
-       define('SCHEMA_VERSION', 94);
+       define('EXPECTED_CONFIG_VERSION', 26);
+       define('SCHEMA_VERSION', 96);
 
        $fetch_last_error = false;
 
        function get_article_filters($filters, $title, $content, $link, $timestamp, $author, $tags) {
                $matches = array();
 
-               if ($filters["title"]) {
-                       foreach ($filters["title"] as $filter) {
-                               $reg_exp = $filter["reg_exp"];
-                               $inverse = $filter["inverse"];
-                               if ((!$inverse && @preg_match("/$reg_exp/i", $title)) ||
-                                               ($inverse && !@preg_match("/$reg_exp/i", $title))) {
+               foreach ($filters as $filter) {
+                       $match_any_rule = $filter["match_any_rule"];
+                       $filter_match = false;
 
-                                       array_push($matches, array($filter["action"], $filter["action_param"]));
-                               }
-                       }
-               }
+                       foreach ($filter["rules"] as $rule) {
+                               $match = false;
+                               $reg_exp = $rule["reg_exp"];
 
-               if ($filters["content"]) {
-                       foreach ($filters["content"] as $filter) {
-                               $reg_exp = $filter["reg_exp"];
-                               $inverse = $filter["inverse"];
+                               if (!$reg_exp)
+                                       continue;
 
-                               if ((!$inverse && @preg_match("/$reg_exp/i", $content)) ||
-                                               ($inverse && !@preg_match("/$reg_exp/i", $content))) {
-
-                                       array_push($matches, array($filter["action"], $filter["action_param"]));
+                               switch ($rule["type"]) {
+                               case "title":
+                                       $match = @preg_match("/$reg_exp/i", $title);
+                                       break;
+                               case "content":
+                                       $match = @preg_match("/$reg_exp/i", $content);
+                                       break;
+                               case "both":
+                                       $match = (@preg_match("/$reg_exp/i", $title) || @preg_match("/$reg_exp/i", $title));
+                                       break;
+                               case "link":
+                                       $match = @preg_match("/$reg_exp/i", $link);
+                                       break;
+                               case "author":
+                                       $match = @preg_match("/$reg_exp/i", $author);
+                                       break;
+                               case "tag":
+                                       $tag_string = join(",", $tags);
+                                       $match = @preg_match("/$reg_exp/i", $tag_string);
+                                       break;
                                }
-                       }
-               }
-
-               if ($filters["both"]) {
-                       foreach ($filters["both"] as $filter) {
-                               $reg_exp = $filter["reg_exp"];
-                               $inverse = $filter["inverse"];
 
-                               if ($inverse) {
-                                       if (!@preg_match("/$reg_exp/i", $title) && !preg_match("/$reg_exp/i", $content)) {
-                                               array_push($matches, array($filter["action"], $filter["action_param"]));
+                               if ($match_any_rule) {
+                                       if ($match) {
+                                               $filter_match = true;
+                                               break;
                                        }
                                } else {
-                                       if (@preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) {
-                                               array_push($matches, array($filter["action"], $filter["action_param"]));
-                                       }
-                               }
-                       }
-               }
-
-               if ($filters["link"]) {
-                       $reg_exp = $filter["reg_exp"];
-                       foreach ($filters["link"] as $filter) {
-                               $reg_exp = $filter["reg_exp"];
-                               $inverse = $filter["inverse"];
-
-                               if ((!$inverse && @preg_match("/$reg_exp/i", $link)) ||
-                                               ($inverse && !@preg_match("/$reg_exp/i", $link))) {
-
-                                       array_push($matches, array($filter["action"], $filter["action_param"]));
-                               }
-                       }
-               }
-
-               if ($filters["date"]) {
-                       $reg_exp = $filter["reg_exp"];
-                       foreach ($filters["date"] as $filter) {
-                               $date_modifier = $filter["filter_param"];
-                               $inverse = $filter["inverse"];
-                               $check_timestamp = strtotime($filter["reg_exp"]);
-
-                               # no-op when timestamp doesn't parse to prevent misfires
-
-                               if ($check_timestamp) {
-                                       $match_ok = false;
-
-                                       if ($date_modifier == "before" && $timestamp < $check_timestamp ||
-                                               $date_modifier == "after" && $timestamp > $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 ($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"]));
+                       if ($filter_match) {
+                               foreach ($filter["actions"] AS $action) {
+                                       array_push($matches, $action);
                                }
                        }
                }
 
-
                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;
 
        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);
                        };
                }
        }
 
                if ($is_cat) {
                        return getCategoryUnread($link, $n_feed, $owner_uid);
-               } if ($feed != "0" && $n_feed == 0) {
+               } else if ($n_feed == -6) {
+                       return 0;
+               } else if ($feed != "0" && $n_feed == 0) {
 
                        $feed = db_escape_string($feed);
 
        }
 
        function print_feed_select($link, $id, $default_id = "",
-               $attributes = "", $include_all_feeds = true) {
+               $attributes = "", $include_all_feeds = true,
+               $root_id = false, $nest_level = 0) {
 
-               print "<select id=\"$id\" name=\"$id\" $attributes>";
-               if ($include_all_feeds) {
-                       print "<option value=\"0\">".__('All feeds')."</option>";
+               if (!$root_id) {
+                       print "<select id=\"$id\" name=\"$id\" $attributes>";
+                       if ($include_all_feeds) {
+                               $is_selected = ("0" == $default_id) ? "selected=\"1\"" : "";
+                               print "<option $is_selected value=\"0\">".__('All feeds')."</option>";
+                       }
                }
 
-               $result = db_query($link, "SELECT id,title FROM ttrss_feeds
-                       WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
+               if (get_pref($link, 'ENABLE_FEED_CATS')) {
 
-               if (db_num_rows($result) > 0 && $include_all_feeds) {
-                       print "<option disabled>--------</option>";
-               }
+                       if ($root_id)
+                               $parent_qpart = "parent_cat = '$root_id'";
+                       else
+                               $parent_qpart = "parent_cat IS NULL";
 
-               while ($line = db_fetch_assoc($result)) {
-                       if ($line["id"] == $default_id) {
-                               $is_selected = "selected=\"1\"";
-                       } else {
-                               $is_selected = "";
+                       $result = db_query($link, "SELECT id,title,
+                               (SELECT COUNT(id) FROM ttrss_feed_categories AS c2 WHERE
+                                       c2.parent_cat = ttrss_feed_categories.id) AS num_children
+                               FROM ttrss_feed_categories
+                               WHERE owner_uid = ".$_SESSION["uid"]." AND $parent_qpart ORDER BY title");
+
+                       while ($line = db_fetch_assoc($result)) {
+
+                               for ($i = 0; $i < $nest_level; $i++)
+                                       $line["title"] = " - " . $line["title"];
+
+                               $is_selected = ("CAT:".$line["id"] == $default_id) ? "selected=\"1\"" : "";
+
+                               printf("<option $is_selected value='CAT:%d'>%s</option>",
+                                       $line["id"], htmlspecialchars($line["title"]));
+
+                               if ($line["num_children"] > 0)
+                                       print_feed_select($link, $id, $default_id, $attributes,
+                                               $include_all_feeds, $line["id"], $nest_level+1);
+
+                               $feed_result = db_query($link, "SELECT id,title FROM ttrss_feeds
+                                       WHERE cat_id = '".$line["id"]."' AND owner_uid = ".$_SESSION["uid"] . " ORDER BY title");
+
+                               while ($fline = db_fetch_assoc($feed_result)) {
+                                       $is_selected = ($fline["id"] == $default_id) ? "selected=\"1\"" : "";
+
+                                       $fline["title"] = " + " . $fline["title"];
+
+                                       for ($i = 0; $i < $nest_level; $i++)
+                                               $fline["title"] = " - " . $fline["title"];
+
+                                       printf("<option $is_selected value='%d'>%s</option>",
+                                               $fline["id"], htmlspecialchars($fline["title"]));
+                               }
+                       }
+
+                       if (!$root_id) {
+                               $is_selected = ($default_id == "CAT:0") ? "selected=\"1\"" : "";
+
+                               printf("<option $is_selected value='CAT:0'>%s</option>",
+                                       __("Uncategorized"));
+
+                               $feed_result = db_query($link, "SELECT id,title FROM ttrss_feeds
+                                       WHERE cat_id IS NULL AND owner_uid = ".$_SESSION["uid"] . " ORDER BY title");
+
+                               while ($fline = db_fetch_assoc($feed_result)) {
+                                       $is_selected = ($fline["id"] == $default_id && !$default_is_cat) ? "selected=\"1\"" : "";
+
+                                       $fline["title"] = " + " . $fline["title"];
+
+                                       for ($i = 0; $i < $nest_level; $i++)
+                                               $fline["title"] = " - " . $fline["title"];
+
+                                       printf("<option $is_selected value='%d'>%s</option>",
+                                               $fline["id"], htmlspecialchars($fline["title"]));
+                               }
                        }
 
-                       $title = truncate_string(htmlspecialchars($line["title"]), 40);
+               } else {
+                       $result = db_query($link, "SELECT id,title FROM ttrss_feeds
+                               WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
 
-                       printf("<option $is_selected value='%d'>%s</option>",
-                               $line["id"], $title);
+                       while ($line = db_fetch_assoc($result)) {
+
+                               $is_selected = ($line["id"] == $default_id) ? "selected=\"1\"" : "";
+
+                               printf("<option $is_selected value='%d'>%s</option>",
+                                       $line["id"], htmlspecialchars($line["title"]));
+                       }
                }
 
-               print "</select>";
+               if (!$root_id) {
+                       print "</select>";
+               }
        }
 
        function print_feed_cat_select($link, $id, $default_id,
                case -4:
                        return "images/tag.png";
                        break;
+               case -6:
+                       return "images/recently_read.png";
+                       break;
                default:
                        if ($id < -10) {
                                return "images/label.png";
                        return __("All articles");
                } else if ($id === 0 || $id === "0") {
                        return __("Archived articles");
+               } else if ($id == -6) {
+                       return __("Recently read");
                } else if ($id < -10) {
                        $label_id = -$id - 11;
                        $result = db_query($link, "SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'");
                                $data['new_version_available'] = (int) ($new_version_details != false);
 
                                $_SESSION["last_version_check"] = time();
+                               $_SESSION["version_data"] = $new_version_details;
                }
 
                return $data;
                                                ttrss_user_labels2.article_id = ref_id";
 
                                }
-
+                       } else if ($feed == -6) { // recently read
+                               $query_strategy_part = "unread = false AND last_read IS NOT NULL";
+                               $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+                               $override_order = "last_read DESC";
                        } else if ($feed == -3) { // fresh virtual feed
                                $query_strategy_part = "unread = true AND score >= 0";
 
                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();
+
+                       while ($rule_line = db_fetch_assoc($result2)) {
+#                              print_r($rule_line);
+
+                               $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);
 
-                       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"]);
+                               $action = array();
+                               $action["type"] = $action_line["type_name"];
+                               $action["param"] = $action_line["action_param"];
 
-                               array_push($filters[$line["name"]], $filter);
+                               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;
        }
 
                                }
                        }
 
-                       $rv .= "<div dojoType=\"dijit.form.DropDownButton\">".
+                       $rv .= "<br/><div dojoType=\"dijit.form.DropDownButton\">".
                                "<span>" . __('Attachments')."</span>";
                        $rv .= "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
 
        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;
+               }
+
+       }
+
 ?>