From b87744534a5250e9f839997f8eceb5b86b8c0e5c Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 11 Aug 2015 23:28:41 +0300 Subject: [PATCH] add plugin-based filter actions (see example plugin in attic) bump schema --- classes/handler/public.php | 8 +++--- classes/pluginhost.php | 18 +++++++++++- classes/pref/filters.php | 37 ++++++++++++++++++++++-- classes/pref/prefs.php | 4 +-- include/functions.php | 14 +++++---- include/rssfuncs.php | 54 ++++++++++++++++++++++++++--------- js/functions.js | 20 +++++++++---- schema/ttrss_schema_mysql.sql | 5 +++- schema/ttrss_schema_pgsql.sql | 5 +++- schema/versions/mysql/129.sql | 8 ++++++ schema/versions/pgsql/129.sql | 8 ++++++ 11 files changed, 146 insertions(+), 35 deletions(-) create mode 100644 schema/versions/mysql/129.sql create mode 100644 schema/versions/pgsql/129.sql diff --git a/classes/handler/public.php b/classes/handler/public.php index 1bf088701..632cece81 100644 --- a/classes/handler/public.php +++ b/classes/handler/public.php @@ -1004,10 +1004,10 @@ class Handler_Public extends Handler { print "

Database update required

"; - print "

"; - printf("Your Tiny Tiny RSS database needs update to the latest version: %d to %d.", - $updater->getSchemaVersion(), SCHEMA_VERSION); - print "

"; + print_notice("

". + sprintf("Your Tiny Tiny RSS database needs update to the latest version: %d to %d.", + $updater->getSchemaVersion(), SCHEMA_VERSION). + "

"); print_warning("Please backup your database before proceeding."); diff --git a/classes/pluginhost.php b/classes/pluginhost.php index c4ec1871d..75620a191 100644 --- a/classes/pluginhost.php +++ b/classes/pluginhost.php @@ -8,6 +8,7 @@ class PluginHost { private $storage = array(); private $feeds = array(); private $api_methods = array(); + private $plugin_actions = array(); private $owner_uid; private $debug; private $last_registered; @@ -47,6 +48,7 @@ class PluginHost { const HOOK_SUBSCRIBE_FEED = 27; const HOOK_HEADLINES_BEFORE = 28; const HOOK_RENDER_ENCLOSURE = 29; + const HOOK_ARTICLE_FILTER_ACTION = 30; const KIND_ALL = 1; const KIND_SYSTEM = 2; @@ -98,7 +100,7 @@ class PluginHost { } function get_plugin($name) { - return $this->plugins[$name]; + return $this->plugins[strtolower($name)]; } function run_hooks($type, $method, $args) { @@ -415,5 +417,19 @@ class PluginHost { function get_api_method($name) { return $this->api_methods[$name]; } + + function add_filter_action($sender, $action_name, $action_desc) { + $sender_class = get_class($sender); + + if (!isset($this->plugin_actions[$sender_class])) + $this->plugin_actions[$sender_class] = array(); + + array_push($this->plugin_actions[$sender_class], + array("action" => $action_name, "description" => $action_desc, "sender" => $sender)); + } + + function get_filter_actions() { + return $this->plugin_actions; + } } ?> diff --git a/classes/pref/filters.php b/classes/pref/filters.php index a84340877..a2a2d0928 100644 --- a/classes/pref/filters.php +++ b/classes/pref/filters.php @@ -519,6 +519,21 @@ class Pref_Filters extends Handler_Protected { $action["action_id"] == 7) $title .= ": " . $action["action_param"]; + if ($action["action_id"] == 9) { + list ($pfclass, $pfaction) = explode(":", $action["action_param"]); + + $filter_actions = PluginHost::getInstance()->get_filter_actions(); + + foreach ($filter_actions as $fclass => $factions) { + foreach ($factions as $faction) { + if ($pfaction == $faction["action"] && $pfclass == $fclass) { + $title .= ": " . $fclass . ": " . $faction["description"]; + break; + } + } + } + } + return $title; } @@ -989,16 +1004,18 @@ class Pref_Filters extends Handler_Protected { print ""; - $param_box_hidden = ($action_id == 7 || $action_id == 4 || $action_id == 6) ? + $param_box_hidden = ($action_id == 7 || $action_id == 4 || $action_id == 6 || $action_id == 9) ? "" : "display : none"; $param_hidden = ($action_id == 4 || $action_id == 6) ? "" : "display : none"; $label_param_hidden = ($action_id == 7) ? "" : "display : none"; + $plugin_param_hidden = ($action_id == 9) ? "" : "display : none"; print ""; - print " " . __("with parameters:") . " "; + print " "; + //print " " . __("with parameters:") . " "; print ""; @@ -1007,6 +1024,22 @@ class Pref_Filters extends Handler_Protected { "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\" dojoType=\"dijit.form.Select\""); + $filter_actions = PluginHost::getInstance()->get_filter_actions(); + $filter_action_hash = array(); + + foreach ($filter_actions as $fclass => $factions) { + foreach ($factions as $faction) { + + $filter_action_hash[$fclass . ":" . $faction["action"]] = + $fclass . ": " . $faction["description"]; + } + + } + + print_select_hash("filterDlg_actionParamPlugin", $action_param, $filter_action_hash, + "style=\"$plugin_param_hidden\" dojoType=\"dijit.form.Select\"", + "action_param_plugin"); + print ""; print " "; // tiny layout hack diff --git a/classes/pref/prefs.php b/classes/pref/prefs.php index fe88218ba..41dc536f5 100644 --- a/classes/pref/prefs.php +++ b/classes/pref/prefs.php @@ -752,7 +752,7 @@ class Pref_Prefs extends Handler_Protected { foreach ($tmppluginhost->get_plugins() as $name => $plugin) { $about = $plugin->about(); - if ($about[3] && strpos($name, "example") === FALSE) { + if ($about[3]) { if (in_array($name, $system_enabled)) { $checked = "checked='1'"; } else { @@ -802,7 +802,7 @@ class Pref_Prefs extends Handler_Protected { foreach ($tmppluginhost->get_plugins() as $name => $plugin) { $about = $plugin->about(); - if (!$about[3] && strpos($name, "example") === FALSE) { + if (!$about[3]) { if (in_array($name, $system_enabled)) { $checked = "checked='1'"; diff --git a/include/functions.php b/include/functions.php index d90855ea6..6d183abaa 100644 --- a/include/functions.php +++ b/include/functions.php @@ -1,6 +1,6 @@ "; + function print_select($id, $default, $values, $attributes = "", $name = "") { + if (!$name) $name = $id; + + print ""; } - function print_select_hash($id, $default, $values, $attributes = "") { - print ""; foreach (array_keys($values) as $v) { if ($v == $default) $sel = 'selected="selected"'; diff --git a/include/rssfuncs.php b/include/rssfuncs.php index 6532fb270..a922516cd 100644 --- a/include/rssfuncs.php +++ b/include/rssfuncs.php @@ -773,6 +773,47 @@ } } + /* Collect article tags here so we could filter by them: */ + + $article_filters = get_article_filters($filters, $article["title"], + $article["content"], $article["link"], 0, $article["author"], + $article["tags"]); + + if ($debug_enabled) { + _debug("article filters: ", $debug_enabled); + if (count($article_filters) != 0) { + print_r($article_filters); + } + } + + $plugin_filter_names = find_article_filters($article_filters, "plugin"); + $plugin_filter_actions = $pluginhost->get_filter_actions(); + + if (count($plugin_filter_names) > 0) { + _debug("applying plugin filter actions...", $debug_enabled); + + foreach ($plugin_filter_names as $pfn) { + list($pfclass,$pfaction) = explode(":", $pfn["param"]); + + if (isset($plugin_filter_actions[$pfclass])) { + $plugin = $pluginhost->get_plugin($pfclass); + + _debug("... $pfclass: $pfaction", $debug_enabled); + + if ($plugin) { + $start = microtime(true); + $article = $plugin->hook_article_filter_action($article, $pfaction); + + _debug("=== " . sprintf("%.4f (sec)", microtime(true) - $start), $debug_enabled); + } else { + _debug("??? $pfclass: plugin object not found."); + } + } else { + _debug("??? $pfclass: filter plugin not registered."); + } + } + } + $entry_tags = $article["tags"]; $entry_guid = db_escape_string($entry_guid); $entry_title = db_escape_string($article["title"]); @@ -875,19 +916,6 @@ $dupcheck_qpart = ""; } - /* Collect article tags here so we could filter by them: */ - - $article_filters = get_article_filters($filters, $entry_title, - $entry_content, $entry_link, $entry_timestamp, $entry_author, - $entry_tags); - - if ($debug_enabled) { - _debug("article filters: ", $debug_enabled); - if (count($article_filters) != 0) { - print_r($article_filters); - } - } - if (find_article_filter($article_filters, "filter")) { //db_query("COMMIT"); // close transaction in progress continue; diff --git a/js/functions.js b/js/functions.js index 98a531851..a0f954e07 100644 --- a/js/functions.js +++ b/js/functions.js @@ -591,15 +591,21 @@ function filterDlgCheckAction(sender) { } // if selected action supports parameters, enable params field - if (action == 4 || action == 6 || action == 7) { + if (action == 4 || action == 6 || action == 7 || action == 9) { new Effect.Appear(action_param, {duration : 0.5}); - if (action != 7) { - Element.show(dijit.byId("filterDlg_actionParam").domNode); - Element.hide(dijit.byId("filterDlg_actionParamLabel").domNode); - } else { + + Element.hide(dijit.byId("filterDlg_actionParam").domNode); + Element.hide(dijit.byId("filterDlg_actionParamLabel").domNode); + Element.hide(dijit.byId("filterDlg_actionParamPlugin").domNode); + + if (action == 7) { Element.show(dijit.byId("filterDlg_actionParamLabel").domNode); - Element.hide(dijit.byId("filterDlg_actionParam").domNode); + } else if (action == 9) { + Element.show(dijit.byId("filterDlg_actionParamPlugin").domNode); + } else { + Element.show(dijit.byId("filterDlg_actionParam").domNode); } + } else { Element.hide(action_param); } @@ -966,6 +972,8 @@ function createNewActionElement(parentNode, replaceNode) { if (form.action_id.value == 7) { form.action_param.value = form.action_param_label.value; + } else if (form.action_id.value == 9) { + form.action_param.value = form.action_param_plugin.value; } var query = "backend.php?op=pref-filters&method=printactionname&action="+ diff --git a/schema/ttrss_schema_mysql.sql b/schema/ttrss_schema_mysql.sql index 8a6f7d681..296049083 100644 --- a/schema/ttrss_schema_mysql.sql +++ b/schema/ttrss_schema_mysql.sql @@ -232,6 +232,9 @@ insert into ttrss_filter_actions (id,name,description) values (7, 'label', insert into ttrss_filter_actions (id,name,description) values (8, 'stop', 'Stop / Do nothing'); +insert into ttrss_filter_actions (id,name,description) values (9, 'plugin', + 'Invoke plugin'); + create table ttrss_filters2(id integer primary key auto_increment, owner_uid integer not null, match_any_rule boolean not null default false, @@ -278,7 +281,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 (127); +insert into ttrss_version values (129); create table ttrss_enclosures (id integer primary key auto_increment, content_url text not null, diff --git a/schema/ttrss_schema_pgsql.sql b/schema/ttrss_schema_pgsql.sql index 9dafa693e..b53d375cc 100644 --- a/schema/ttrss_schema_pgsql.sql +++ b/schema/ttrss_schema_pgsql.sql @@ -228,6 +228,9 @@ insert into ttrss_filter_actions (id,name,description) values (7, 'label', insert into ttrss_filter_actions (id,name,description) values (8, 'stop', 'Stop / Do nothing'); +insert into ttrss_filter_actions (id,name,description) values (9, 'plugin', + 'Invoke plugin'); + 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, @@ -260,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 (127); +insert into ttrss_version values (129); create table ttrss_enclosures (id serial not null primary key, content_url text not null, diff --git a/schema/versions/mysql/129.sql b/schema/versions/mysql/129.sql new file mode 100644 index 000000000..2ebec0dc9 --- /dev/null +++ b/schema/versions/mysql/129.sql @@ -0,0 +1,8 @@ +BEGIN; + +insert into ttrss_filter_actions (id,name,description) values (9, 'plugin', + 'Invoke plugin'); + +UPDATE ttrss_version SET schema_version = 129; + +COMMIT; diff --git a/schema/versions/pgsql/129.sql b/schema/versions/pgsql/129.sql new file mode 100644 index 000000000..2ebec0dc9 --- /dev/null +++ b/schema/versions/pgsql/129.sql @@ -0,0 +1,8 @@ +BEGIN; + +insert into ttrss_filter_actions (id,name,description) values (9, 'plugin', + 'Invoke plugin'); + +UPDATE ttrss_version SET schema_version = 129; + +COMMIT;