]> git.wh0rd.org Git - tt-rss.git/commitdiff
let us rejoice on being able to create double negative filters (refs #631)
authorAndrew Dolgov <fox@madoka.volgo-balt.ru>
Mon, 25 Mar 2013 15:46:43 +0000 (19:46 +0400)
committerAndrew Dolgov <fox@madoka.volgo-balt.ru>
Mon, 25 Mar 2013 15:46:43 +0000 (19:46 +0400)
classes/pref/filters.php
include/functions.php
include/rssfuncs.php
schema/ttrss_schema_mysql.sql
schema/ttrss_schema_pgsql.sql
schema/versions/mysql/107.sql [new file with mode: 0644]
schema/versions/pgsql/107.sql [new file with mode: 0644]

index c97628e51ffa90dde783b48cb4cd02a5d57039c0..f9828705e6c2465d77c6570ce1e487336998e662 100644 (file)
@@ -14,6 +14,9 @@ class Pref_Filters extends Handler_Protected {
                $filter["enabled"] = true;
                $filter["match_any_rule"] = sql_bool_to_bool(
                        checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["match_any_rule"])));
+               $filter["inverse"] = sql_bool_to_bool(
+                       checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["inverse"])));
+
                $filter["rules"] = array();
 
                $result = db_query($this->link, "SELECT id,name FROM ttrss_filter_types");
@@ -214,6 +217,7 @@ class Pref_Filters extends Handler_Protected {
 
                $enabled = sql_bool_to_bool(db_fetch_result($result, 0, "enabled"));
                $match_any_rule = sql_bool_to_bool(db_fetch_result($result, 0, "match_any_rule"));
+               $inverse = sql_bool_to_bool(db_fetch_result($result, 0, "inverse"));
 
                print "<form id=\"filter_edit_form\" onsubmit='return false'>";
 
@@ -257,6 +261,7 @@ class Pref_Filters extends Handler_Protected {
                        unset($line["cat_id"]);
                        unset($line["filter_id"]);
                        unset($line["id"]);
+                       if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
 
                        $data = htmlspecialchars(json_encode($line));
 
@@ -330,6 +335,15 @@ class Pref_Filters extends Handler_Protected {
                print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
                                <label for=\"match_any_rule\">".__('Match any rule')."</label>";
 
+               if ($inverse) {
+                       $checked = "checked=\"1\"";
+               } else {
+                       $checked = "";
+               }
+
+               print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
+                               <label for=\"inverse\">".__('Inverse matching')."</label>";
+
                print "<p/>";
 
                print "<div class=\"dlgButtons\">";
@@ -372,7 +386,8 @@ class Pref_Filters extends Handler_Protected {
                        WHERE id = ".(int)$rule["filter_type"]);
                $filter_type = db_fetch_result($result, 0, "description");
 
-               return T_sprintf("%s on %s in %s", strip_tags($rule["reg_exp"]), $filter_type, $feed);
+               return T_sprintf("%s on %s in %s %s", strip_tags($rule["reg_exp"]),
+                       $filter_type, $feed, isset($rule["inverse"]) ? __("(inverse)") : "");
        }
 
        function printRuleName() {
@@ -406,9 +421,11 @@ class Pref_Filters extends Handler_Protected {
                $filter_id = db_escape_string($this->link, $_REQUEST["id"]);
                $enabled = checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["enabled"]));
                $match_any_rule = checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["match_any_rule"]));
+               $inverse = checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["inverse"]));
 
                $result = db_query($this->link, "UPDATE ttrss_filters2 SET enabled = $enabled,
-                       match_any_rule = $match_any_rule
+                       match_any_rule = $match_any_rule,
+                       inverse = $inverse
                        WHERE id = '$filter_id'
                        AND owner_uid = ". $_SESSION["uid"]);
 
@@ -458,6 +475,8 @@ class Pref_Filters extends Handler_Protected {
                                if ($rule) {
 
                                        $reg_exp = strip_tags(db_escape_string($this->link, trim($rule["reg_exp"])));
+                                       $inverse = isset($rule["inverse"]) ? "true" : "false";
+
                                        $filter_type = (int) db_escape_string($this->link, trim($rule["filter_type"]));
                                        $feed_id = db_escape_string($this->link, trim($rule["feed_id"]));
 
@@ -477,8 +496,8 @@ class Pref_Filters extends Handler_Protected {
                                        }
 
                                        $query = "INSERT INTO ttrss_filters2_rules
-                                               (filter_id, reg_exp,filter_type,feed_id,cat_id,cat_filter) VALUES
-                                               ('$filter_id', '$reg_exp', '$filter_type', $feed_id, $cat_id, $cat_filter)";
+                                               (filter_id, reg_exp,filter_type,feed_id,cat_id,cat_filter,inverse) VALUES
+                                               ('$filter_id', '$reg_exp', '$filter_type', $feed_id, $cat_id, $cat_filter, $inverse)";
 
                                        db_query($this->link, $query);
                                }
@@ -710,10 +729,8 @@ class Pref_Filters extends Handler_Protected {
                print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
                                <label for=\"match_any_rule\">".__('Match any rule')."</label>";
 
-               print "<p/>";
-
-/*             print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
-       <label for=\"inverse\">".__('Inverse match')."</label><hr/>"; */
+               print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
+                               <label for=\"inverse\">".__('Inverse matching')."</label>";
 
 //             print "</div>";
 
@@ -739,10 +756,12 @@ class Pref_Filters extends Handler_Protected {
                        $reg_exp = htmlspecialchars($rule["reg_exp"]);
                        $filter_type = $rule["filter_type"];
                        $feed_id = $rule["feed_id"];
+                       $inverse_checked = isset($rule["inverse"]) ? "checked" : "";
                } else {
                        $reg_exp = "";
                        $filter_type = 1;
                        $feed_id = 0;
+                       $inverse_checked = "";
                }
 
                if (strpos($feed_id, "CAT:") === 0) {
@@ -787,6 +806,13 @@ class Pref_Filters extends Handler_Protected {
                        'dojoType="dijit.form.FilteringSelect"');
                print "</span>";
 
+               print "<p>";
+
+               print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
+                        name=\"inverse\" $inverse_checked/>";
+
+               print "<label for=\"filterDlg_inverse\">".__("Inverse matching")."</label>";
+
                print "</div>";
 
                print "<div class=\"dlgButtons\">";
index b52531f6662a8fdd11fa3b13272d2acb62141bb9..9a855a9bca470ab269cf2cba0aa1e69b061f27b9 100644 (file)
@@ -1,6 +1,6 @@
 <?php
        define('EXPECTED_CONFIG_VERSION', 26);
-       define('SCHEMA_VERSION', 106);
+       define('SCHEMA_VERSION', 107);
 
        $fetch_last_error = false;
        $pluginhost = false;
                        $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
+                               r.reg_exp, r.inverse, 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
                                $rule = array();
                                $rule["reg_exp"] = $rule_line["reg_exp"];
                                $rule["type"] = $rule_line["type_name"];
+                               $rule["inverse"] = sql_bool_to_bool($rule_line["inverse"]);
 
                                array_push($rules, $rule);
                        }
 
                        $filter = array();
                        $filter["match_any_rule"] = sql_bool_to_bool($line["match_any_rule"]);
+                       $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
                        $filter["rules"] = $rules;
                        $filter["actions"] = $actions;
 
 
                                $rule['reg_exp'] = db_escape_string($link, $rule['reg_exp']);
 
-                               switch ($rule["type"]) {
+                                       switch ($rule["type"]) {
                                        case "title":
                                                $qpart = "LOWER(ttrss_entries.title) $reg_qpart LOWER('".
                                                        $rule['reg_exp'] . "')";
                                        $qpart .= " AND $cat_qpart";
                                }
 
+                               if (isset($rule['inverse'])) $qpart = "NOT ($qpart)";
+
                                array_push($query, "($qpart)");
 
                        }
                }
 
                if (count($query) > 0) {
-                       return "(" . join($filter["match_any_rule"] ? "OR" : "AND", $query) . ")";
+                       $fullquery = "(" . join($filter["match_any_rule"] ? "OR" : "AND", $query) . ")";
                } else {
-                       return "(false)";
+                       $fullquery = "(false)";
                }
+
+               if ($filter['inverse']) $fullquery = "(NOT $fullquery)";
+
+               return $fullquery;
        }
 
        if (!function_exists('gzdecode')) {
index 92bfaa02384e8d86242c87f1745c1e248dfdb320..11aa0e4ba1419bb627f8f29f60983283adf86072 100644 (file)
 
                foreach ($filters as $filter) {
                        $match_any_rule = $filter["match_any_rule"];
+                       $inverse = $filter["inverse"];
                        $filter_match = false;
 
                        foreach ($filter["rules"] as $rule) {
                                $match = false;
                                $reg_exp = $rule["reg_exp"];
+                               $rule_inverse = $rule["inverse"];
 
                                if (!$reg_exp)
                                        continue;
                                        break;
                                }
 
+                               if ($rule_inverse) $match = !$match;
+
                                if ($match_any_rule) {
                                        if ($match) {
                                                $filter_match = true;
                                }
                        }
 
+                       if ($inverse) $filter_match = !$filter_match;
+
                        if ($filter_match) {
                                foreach ($filter["actions"] AS $action) {
                                        array_push($matches, $action);
index 4388654787c42e1f88bae7385f1bb6211f3ca5cc..0d33587489a67a56a5e9dc32ac923cdf81de7c01 100644 (file)
@@ -274,12 +274,14 @@ create table ttrss_filters2(id integer primary key auto_increment,
        owner_uid integer not null,
        match_any_rule boolean not null default false,
        enabled boolean not null default true,
+       inverse bool not null default false,
        index(owner_uid),
        foreign key (owner_uid) references ttrss_users(id) ON DELETE CASCADE) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
 
 create table ttrss_filters2_rules(id integer primary key auto_increment,
        filter_id integer not null references ttrss_filters2(id) on delete cascade,
        reg_exp varchar(250) not null,
+       inverse bool not null default false,
        filter_type integer not null,
        feed_id integer default null,
        cat_id integer default null,
@@ -313,7 +315,7 @@ create table ttrss_tags (id integer primary key auto_increment,
 
 create table ttrss_version (schema_version int not null) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
 
-insert into ttrss_version values (106);
+insert into ttrss_version values (107);
 
 create table ttrss_enclosures (id integer primary key auto_increment,
        content_url text not null,
index 3895ccc56353fb8855dd42f3ae00d759ce3b57ae..895c7e0f46da7dd69de415add7be3705745d0e2d 100644 (file)
@@ -236,11 +236,13 @@ create table ttrss_filters (id serial not null primary key,
 create table ttrss_filters2(id serial not null primary key,
        owner_uid integer not null references ttrss_users(id) on delete cascade,
        match_any_rule boolean not null default false,
+       inverse boolean not null default false,
        enabled boolean not null default true);
 
 create table ttrss_filters2_rules(id serial not null primary key,
        filter_id integer not null references ttrss_filters2(id) on delete cascade,
        reg_exp varchar(250) not null,
+       inverse boolean not null default false,
        filter_type integer not null references ttrss_filter_types(id),
        feed_id integer references ttrss_feeds(id) on delete cascade default null,
        cat_id integer references ttrss_feed_categories(id) on delete cascade default null,
@@ -261,7 +263,7 @@ create index ttrss_tags_post_int_id_idx on ttrss_tags(post_int_id);
 
 create table ttrss_version (schema_version int not null);
 
-insert into ttrss_version values (106);
+insert into ttrss_version values (107);
 
 create table ttrss_enclosures (id serial not null primary key,
        content_url text not null,
diff --git a/schema/versions/mysql/107.sql b/schema/versions/mysql/107.sql
new file mode 100644 (file)
index 0000000..9283d40
--- /dev/null
@@ -0,0 +1,15 @@
+begin;
+
+alter table ttrss_filters2 add column inverse bool;
+update ttrss_filters2 set inverse = false;
+alter table ttrss_filters2 change inverse inverse bool not null;
+alter table ttrss_filters2 alter column inverse set default false;
+
+alter table ttrss_filters2_rules add column inverse bool;
+update ttrss_filters2_rules set inverse = false;
+alter table ttrss_filters2_rules change inverse inverse bool not null;
+alter table ttrss_filters2_rules alter column inverse set default false;
+
+update ttrss_version set schema_version = 107;
+
+commit;
diff --git a/schema/versions/pgsql/107.sql b/schema/versions/pgsql/107.sql
new file mode 100644 (file)
index 0000000..a4a537c
--- /dev/null
@@ -0,0 +1,15 @@
+begin;
+
+alter table ttrss_filters2 add column inverse boolean;
+update ttrss_filters2 set inverse = false;
+alter table ttrss_filters2 alter column inverse set not null;
+alter table ttrss_filters2 alter column inverse set default false;
+
+alter table ttrss_filters2_rules add column inverse boolean;
+update ttrss_filters2_rules set inverse = false;
+alter table ttrss_filters2_rules alter column inverse set not null;
+alter table ttrss_filters2_rules alter column inverse set default false;
+
+update ttrss_version set schema_version = 107;
+
+commit;