implement filter testing

master
Andrew Dolgov 12 years ago
parent 9299102f43
commit 4e02f58294

@ -8,97 +8,102 @@ class Pref_Filters extends Handler_Protected {
return array_search($method, $csrf_ignored) !== false; return array_search($method, $csrf_ignored) !== false;
} }
/* function filter_test($filter_type, $reg_exp, function testFilter() {
$action_id, $action_param, $filter_param, $inverse, $feed_id, $cat_id, $filter = array();
$cat_filter) {
$result = db_query($this->link, "SELECT name FROM ttrss_filter_types WHERE $filter["enabled"] = true;
id = " . $filter_type); $filter["match_any_rule"] = sql_bool_to_bool(
$type_name = db_fetch_result($result, 0, "name"); checkbox_to_sql_bool(db_escape_string($_REQUEST["match_any_rule"])));
$filter["rules"] = array();
$result = db_query($this->link, "SELECT name FROM ttrss_filter_actions WHERE $result = db_query($this->link, "SELECT id,name FROM ttrss_filter_types");
id = " . $action_id); $type_name = db_fetch_result($result, 0, "name");
$action_name = db_fetch_result($result, 0, "name");
$filter["reg_exp"] = $reg_exp; $filter_types = array();
$filter["action"] = $action_name; while ($line = db_fetch_assoc($result)) {
$filter["type"] = $type_name; $filter_types[$line["id"]] = $line["name"];
$filter["action_param"] = $action_param; }
$filter["filter_param"] = $filter_param;
$filter["inverse"] = $inverse;
$filters[$type_name] = array($filter); $rctr = 0;
foreach ($_REQUEST["rule"] AS $r) {
$rule = json_decode($r, true);
if ($feed_id) if ($rule && $rctr < 5) {
$feed = $feed_id; $rule["type"] = $filter_types[$rule["filter_type"]];
else unset($rule["filter_type"]);
$feed = -4;
$regexp_valid = preg_match('/' . $filter['reg_exp'] . '/', if (strpos($rule["feed_id"], "CAT:") === 0) {
$filter['reg_exp']) !== FALSE; $rule["cat_id"] = (int) substr($rule["feed_id"], 4);
unset($rule["feed_id"]);
}
print __("Articles matching this filter:"); array_push($filter["rules"], $rule);
print "<div class=\"filterTestHolder\">"; ++$rctr;
print "<table width=\"100%\" cellspacing=\"0\" id=\"prefErrorFeedList\">"; } else {
break;
}
}
if ($regexp_valid) { $feed_title = getFeedTitle($this->link, $feed);
$feed_title = getFeedTitle($this->link, $feed); $qfh_ret = queryFeedHeadlines($this->link, -4, 30, "", false, false, false,
false, "date_entered DESC", 0, $_SESSION["uid"], $filter);
$qfh_ret = queryFeedHeadlines($this->link, $cat_filter ? $cat_id : $feed, $result = $qfh_ret[0];
30, "", $cat_filter, false, false,
false, "date_entered DESC", 0, $_SESSION["uid"], $filter);
$result = $qfh_ret[0]; $articles = array();
$found = 0;
$articles = array(); print __("Articles matching this filter:");
$found = 0;
while ($line = db_fetch_assoc($result)) { print "<div class=\"filterTestHolder\">";
print "<table width=\"100%\" cellspacing=\"0\" id=\"prefErrorFeedList\">";
$entry_timestamp = strtotime($line["updated"]); while ($line = db_fetch_assoc($result)) {
$entry_tags = get_article_tags($this->link, $line["id"], $_SESSION["uid"]);
$content_preview = truncate_string( $entry_timestamp = strtotime($line["updated"]);
strip_tags($line["content_preview"]), 100, '...'); $entry_tags = get_article_tags($this->link, $line["id"], $_SESSION["uid"]);
if ($line["feed_title"]) $content_preview = truncate_string(
$feed_title = $line["feed_title"]; strip_tags($line["content_preview"]), 100, '...');
print "<tr>"; if ($line["feed_title"])
$feed_title = $line["feed_title"];
print "<td width='5%' align='center'><input print "<tr>";
dojoType=\"dijit.form.CheckBox\" checked=\"1\"
disabled=\"1\" type=\"checkbox\"></td>";
print "<td>";
print $line["title"]; print "<td width='5%' align='center'><input
print "&nbsp;("; dojoType=\"dijit.form.CheckBox\" checked=\"1\"
print "<b>" . $feed_title . "</b>"; disabled=\"1\" type=\"checkbox\"></td>";
print "):&nbsp;"; print "<td>";
print "<span class=\"insensitive\">" . $content_preview . "</span>";
print " " . mb_substr($line["date_entered"], 0, 16);
print "</td></tr>"; print $line["title"];
print "&nbsp;(";
print "<b>" . $feed_title . "</b>";
print "):&nbsp;";
print "<span class=\"insensitive\">" . $content_preview . "</span>";
print " " . mb_substr($line["date_entered"], 0, 16);
$found++; print "</td></tr>";
}
if ($found == 0) { $found++;
print "<tr><td align='center'>" . }
__("No articles matching this filter has been found.") . "</td></tr>";
}
} else {
print "<tr><td align='center' class='error'>" .
__("Invalid regular expression.") . "</td></tr>";
if ($found == 0) {
print "<tr><td align='center'>" .
__("No recent articles matching this filter have been found.") . "</td></tr>";
} }
print "</table>"; print "</table></div>";
print "<div style='text-align : center'>";
print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
__('Close this window')."</button>";
print "</div>"; print "</div>";
} */ }
function getfiltertree() { function getfiltertree() {
$root = array(); $root = array();
@ -239,11 +244,14 @@ class Pref_Filters extends Handler_Protected {
while ($line = db_fetch_assoc($rules_result)) { while ($line = db_fetch_assoc($rules_result)) {
if (sql_bool_to_bool($line["cat_filter"])) { if (sql_bool_to_bool($line["cat_filter"])) {
unset($line["cat_filter"]);
$line["feed_id"] = "CAT:" . (int)$line["cat_id"]; $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
unset($line["cat_id"]);
} }
unset($line["cat_filter"]);
unset($line["cat_id"]);
unset($line["filter_id"]);
unset($line["id"]);
$data = htmlspecialchars(json_encode($line)); $data = htmlspecialchars(json_encode($line));
print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>". print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
@ -283,6 +291,10 @@ class Pref_Filters extends Handler_Protected {
while ($line = db_fetch_assoc($actions_result)) { while ($line = db_fetch_assoc($actions_result)) {
$line["action_param_label"] = $line["action_param"]; $line["action_param_label"] = $line["action_param"];
unset($line["filter_id"]);
unset($line["id"]);
$data = htmlspecialchars(json_encode($line)); $data = htmlspecialchars(json_encode($line));
print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>". print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
@ -321,8 +333,8 @@ class Pref_Filters extends Handler_Protected {
__('Remove')."</button>"; __('Remove')."</button>";
print "</div>"; print "</div>";
# print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">". print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
# __('Test')."</button> "; __('Test')."</button> ";
print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">". print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
__('Save')."</button> "; __('Save')."</button> ";
@ -379,6 +391,9 @@ class Pref_Filters extends Handler_Protected {
} }
function editSave() { function editSave() {
if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
return $this->testFilter();
}
# print_r($_REQUEST); # print_r($_REQUEST);
@ -491,6 +506,10 @@ class Pref_Filters extends Handler_Protected {
} }
function add() { function add() {
if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
return $this->testFilter();
}
# print_r($_REQUEST); # print_r($_REQUEST);
$enabled = checkbox_to_sql_bool($_REQUEST["enabled"]); $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
@ -689,8 +708,8 @@ class Pref_Filters extends Handler_Protected {
print "<div class=\"dlgButtons\">"; print "<div class=\"dlgButtons\">";
# print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">". print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
# __('Test')."</button> "; __('Test')."</button> ";
print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">". print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
__('Create')."</button> "; __('Create')."</button> ";

@ -2281,7 +2281,16 @@
} }
if ($filter) { if ($filter) {
$filter_query_part = filter_to_sql($filter);
if (DB_TYPE == "pgsql") {
$query_strategy_part .= " AND updated > NOW() - INTERVAL '14 days' ";
} else {
$query_strategy_part .= " AND updated > DATE_SUB(NOW(), INTERVAL 14 DAY) ";
}
$override_order = "updated DESC";
$filter_query_part = filter_to_sql($filter) . " AND";
} else { } else {
$filter_query_part = ""; $filter_query_part = "";
} }
@ -5049,71 +5058,66 @@
} }
function filter_to_sql($filter) { function filter_to_sql($filter) {
$query = ""; $query = array();
$regexp_valid = preg_match('/' . $filter['reg_exp'] . '/', if (DB_TYPE == "pgsql")
$filter['reg_exp']) !== FALSE; $reg_qpart = "~";
else
$reg_qpart = "REGEXP";
if ($regexp_valid) { foreach ($filter["rules"] AS $rule) {
$regexp_valid = preg_match('/' . $rule['reg_exp'] . '/',
$rule['reg_exp']) !== FALSE;
if (DB_TYPE == "pgsql") if ($regexp_valid) {
$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") $rule['reg_exp'] = db_escape_string($rule['reg_exp']);
$cmp_qpart = "<";
else
$cmp_qpart = ">=";
$timestamp = date("Y-m-d H:N:s", strtotime($filter["reg_exp"])); switch ($rule["type"]) {
$query = "ttrss_entries.date_entered $cmp_qpart '$timestamp'"; case "title":
break; $qpart = "LOWER(ttrss_entries.title) $reg_qpart LOWER('".
case "author": $rule['reg_exp'] . "')";
$query = "LOWER(ttrss_entries.author) $reg_qpart LOWER('". break;
$filter['reg_exp'] . "')"; case "content":
break; $qpart = "LOWER(ttrss_entries.content) $reg_qpart LOWER('".
} $rule['reg_exp'] . "')";
break;
case "both":
$qpart = "LOWER(ttrss_entries.title) $reg_qpart LOWER('".
$rule['reg_exp'] . "') OR LOWER(" .
"ttrss_entries.content) $reg_qpart LOWER('" . $rule['reg_exp'] . "')";
break;
case "tag":
$qpart = "LOWER(ttrss_user_entries.tag_cache) $reg_qpart LOWER('".
$rule['reg_exp'] . "')";
break;
case "link":
$qpart = "LOWER(ttrss_entries.link) $reg_qpart LOWER('".
$rule['reg_exp'] . "')";
break;
case "author":
$qpart = "LOWER(ttrss_entries.author) $reg_qpart LOWER('".
$rule['reg_exp'] . "')";
break;
}
if ($filter["inverse"]) if (isset($rule["feed_id"])) {
$query = "NOT ($query)"; $qpart .= " AND feed_id " . ($rule["feed_id"] ? '= ' . $rule["feed_id"] : 'IS NULL');
}
if ($query) { if (isset($rule["cat_id"])) {
if (DB_TYPE == "pgsql") { $qpart .= " AND cat_id " . ($rule["cat_id"] ? '= ' . $rule["cat_id"] : 'IS NULL');
$query = " ($query) AND ttrss_entries.date_entered > NOW() - INTERVAL '14 days'";
} else {
$query = " ($query) AND ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL 14 DAY)";
} }
$query .= " AND ";
array_push($query, "($qpart)");
} }
}
return $query; if (count($query) > 0) {
return "(" . join($filter["match_any_rule"] ? "OR" : "AND", $query) . ")";
} else { } else {
return false; return "(false)";
} }
} }

@ -1111,6 +1111,20 @@ function quickAddFilter() {
id: "filterEditDlg", id: "filterEditDlg",
title: __("Create Filter"), title: __("Create Filter"),
style: "width: 600px", style: "width: 600px",
test: function() {
var query = "backend.php?" + dojo.formToQuery("filter_new_form") + "&savemode=test";
if (dijit.byId("filterTestDlg"))
dijit.byId("filterTestDlg").destroyRecursive();
var test_dlg = new dijit.Dialog({
id: "filterTestDlg",
title: "Test Filter",
style: "width: 600px",
href: query});
test_dlg.show();
},
selectRules: function(select) { selectRules: function(select) {
$$("#filterDlg_Matches input[type=checkbox]").each(function(e) { $$("#filterDlg_Matches input[type=checkbox]").each(function(e) {
e.checked = select; e.checked = select;

@ -136,6 +136,20 @@ function editFilter(id) {
id: "filterEditDlg", id: "filterEditDlg",
title: __("Edit Filter"), title: __("Edit Filter"),
style: "width: 600px", style: "width: 600px",
test: function() {
var query = "backend.php?" + dojo.formToQuery("filter_edit_form") + "&savemode=test";
if (dijit.byId("filterTestDlg"))
dijit.byId("filterTestDlg").destroyRecursive();
var test_dlg = new dijit.Dialog({
id: "filterTestDlg",
title: "Test Filter",
style: "width: 600px",
href: query});
test_dlg.show();
},
selectRules: function(select) { selectRules: function(select) {
$$("#filterDlg_Matches input[type=checkbox]").each(function(e) { $$("#filterDlg_Matches input[type=checkbox]").each(function(e) {
e.checked = select; e.checked = select;

Loading…
Cancel
Save