Parcourir la source

add plugin-based filter actions (see example plugin in attic)
bump schema

Andrew Dolgov il y a 8 ans
Parent
commit
b87744534a

+ 4 - 4
classes/handler/public.php

@@ -1004,10 +1004,10 @@ class Handler_Public extends Handler {
 
 						print "<h2>Database update required</h2>";
 
-						print "<h3>";
-						printf("Your Tiny Tiny RSS database needs update to the latest version: %d to %d.",
-							$updater->getSchemaVersion(), SCHEMA_VERSION);
-						print "</h3>";
+						print_notice("<h4>".
+						sprintf("Your Tiny Tiny RSS database needs update to the latest version: %d to %d.",
+							$updater->getSchemaVersion(), SCHEMA_VERSION).
+						"</h4>");
 
 						print_warning("Please backup your database before proceeding.");
 

+ 17 - 1
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;
+	}
 }
 ?>

+ 35 - 2
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 "</select>";
 
-		$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 "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
-		print " " . __("with parameters:") . " ";
+		print " ";
+		//print " " . __("with parameters:") . " ";
 		print "<input dojoType=\"dijit.form.TextBox\"
 			id=\"filterDlg_actionParam\" style=\"$param_hidden\"
 			name=\"action_param\" value=\"$action_param\">";
@@ -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 "</span>";
 
 		print "&nbsp;"; // tiny layout hack

+ 2 - 2
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'";

+ 9 - 5
include/functions.php

@@ -1,6 +1,6 @@
 <?php
 	define('EXPECTED_CONFIG_VERSION', 26);
-	define('SCHEMA_VERSION', 128);
+	define('SCHEMA_VERSION', 129);
 
 	define('LABEL_BASE_INDEX', -1024);
 	define('PLUGIN_FEED_BASE_INDEX', -128);
@@ -580,8 +580,10 @@
 		}
 	}
 
-	function print_select($id, $default, $values, $attributes = "") {
-		print "<select name=\"$id\" id=\"$id\" $attributes>";
+	function print_select($id, $default, $values, $attributes = "", $name = "") {
+		if (!$name) $name = $id;
+
+		print "<select name=\"$name\" id=\"$id\" $attributes>";
 		foreach ($values as $v) {
 			if ($v == $default)
 				$sel = "selected=\"1\"";
@@ -595,8 +597,10 @@
 		print "</select>";
 	}
 
-	function print_select_hash($id, $default, $values, $attributes = "") {
-		print "<select name=\"$id\" id='$id' $attributes>";
+	function print_select_hash($id, $default, $values, $attributes = "", $name = "") {
+		if (!$name) $name = $id;
+
+		print "<select name=\"$name\" id='$id' $attributes>";
 		foreach (array_keys($values) as $v) {
 			if ($v == $default)
 				$sel = 'selected="selected"';

+ 41 - 13
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;

+ 14 - 6
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="+

+ 4 - 1
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,

+ 4 - 1
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,

+ 8 - 0
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;

+ 8 - 0
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;