]> git.wh0rd.org Git - tt-rss.git/commitdiff
add experimental key/value storage for plugins
authorAndrew Dolgov <fox@madoka.volgo-balt.ru>
Thu, 27 Dec 2012 12:55:25 +0000 (16:55 +0400)
committerAndrew Dolgov <fox@madoka.volgo-balt.ru>
Thu, 27 Dec 2012 12:55:25 +0000 (16:55 +0400)
classes/pluginhost.php
include/functions.php
plugins/example/example.php

index d97dfa66660a4f36568885d1295088e179b1d58d..e43b39f9dd404fd8ea0443225a4866dc45e66927 100644 (file)
@@ -1,10 +1,17 @@
 <?php
+/* create table ttrss_plugin_storage
+       (id serial not null primary key, name varchar(100) not null,
+               owner_uid integer not null references ttrss_users(id) ON DELETE CASCADE,
+               content text not null) - not in schema yet
+*/
 class PluginHost {
        private $link;
        private $hooks = array();
        private $plugins = array();
        private $handlers = array();
        private $commands = array();
+       private $storage = array();
+       private $owner_uid;
 
        const HOOK_ARTICLE_BUTTON = 1;
        const HOOK_ARTICLE_FILTER = 2;
@@ -21,6 +28,10 @@ class PluginHost {
 
        function __construct($link) {
                $this->link = $link;
+
+               $this->storage = $_SESSION["plugin_storage"];
+
+               if (!$this->storage) $this->storage = array();
        }
 
        private function register_plugin($name, $plugin) {
@@ -75,9 +86,11 @@ class PluginHost {
                $this->load(join(",", $plugins), $kind);
        }
 
-       function load($classlist, $kind) {
+       function load($classlist, $kind, $owner_uid = false) {
                $plugins = explode(",", $classlist);
 
+               $this->owner_uid = (int) $owner_uid;
+
                foreach ($plugins as $class) {
                        $class = trim($class);
                        $class_file = strtolower(basename($class));
@@ -194,5 +207,76 @@ class PluginHost {
                }
        }
 
+       function load_data($force = false) {
+               if ($this->owner_uid && (!$_SESSION["plugin_storage"] || $force))  {
+                       $plugin = db_escape_string($plugin);
+
+                       $result = db_query($this->link, "SELECT name, content FROM ttrss_plugin_storage
+                               WHERE owner_uid = '".$this->owner_uid."'");
+
+                       while ($line = db_fetch_assoc($result)) {
+                               $this->storage[$line["name"]] = unserialize($line["content"]);
+                       }
+
+                       $_SESSION["plugin_storage"] = $this->storage;
+               }
+       }
+
+       private function save_data($plugin) {
+               if ($this->owner_uid) {
+                       $plugin = db_escape_string($plugin);
+
+                       db_query($this->link, "BEGIN");
+
+                       $result = db_query($this->link,"SELECT id FROM ttrss_plugin_storage WHERE
+                               owner_uid= '".$this->owner_uid."' AND name = '$plugin'");
+
+                       if (!isset($this->storage[$plugin]))
+                               $this->storage[$plugin] = array();
+
+                       $content = db_escape_string(serialize($this->storage[$plugin]));
+
+                       if (db_num_rows($result) != 0) {
+                               db_query($this->link, "UPDATE ttrss_plugin_storage SET content = '$content'
+                                       WHERE owner_uid= '".$this->owner_uid."' AND name = '$plugin'");
+
+                       } else {
+                               db_query($this->link, "INSERT INTO ttrss_plugin_storage
+                                       (name,owner_uid,content) VALUES
+                                       ('$plugin','".$this->owner_uid."','$content')");
+                       }
+
+                       db_query($this->link, "COMMIT");
+               }
+       }
+
+       function set($sender, $name, $value, $sync = true) {
+               $idx = get_class($sender);
+
+               if (!isset($this->storage[$idx]))
+                       $this->storage[$idx] = array();
+
+               $this->storage[$idx][$name] = $value;
+
+               $_SESSION["plugin_storage"] = $this->storage;
+
+               if ($sync) $this->save_data(get_class($sender));
+       }
+
+       function get($sender, $name, $default_value) {
+               $idx = get_class($sender);
+
+               if (isset($this->storage[$idx][$name])) {
+                       return $this->storage[$idx][$name];
+               } else {
+                       return $default_value;
+               }
+       }
+
+       function get_all($sender) {
+               $idx = get_class($sender);
+
+               return $this->storage[$idx];
+       }
 }
 ?>
index f6ef7c2b370ca02ac9cf302f9824d968c36d305c..b382b4069fb3fb4ceca54122be8c094e28e0db44 100644 (file)
                        $plugins = get_pref($link, "_ENABLED_PLUGINS", $owner_uid);
 
                        global $pluginhost;
-                       $pluginhost->load($plugins, $pluginhost::KIND_USER);
+                       $pluginhost->load($plugins, $pluginhost::KIND_USER, $owner_uid);
+                       $pluginhost->load_data();
                }
        }
 
index eef604b4f599631a3e057a3d9e62be02c642fecc..f3788ae8c6fafec02be2f208d8751f5b349457f0 100644 (file)
@@ -23,7 +23,9 @@ class Example extends Plugin {
        function save() {
                $example_value = db_escape_string($_POST["example_value"]);
 
-               echo "Value set to $example_value (not really)";
+               $this->host->set($this, "example", $example_value);
+
+               echo "Value set to $example_value";
        }
 
        function get_prefs_js() {
@@ -35,6 +37,13 @@ class Example extends Plugin {
 
                print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("Example Pane")."\">";
 
+               print "<br/>";
+
+//             print_r($this->host->set($this, "example", rand(0,100)));
+//             print_r($this->host->get_all($this));
+
+               $value = $this->host->get($this, "example");
+
                print "<form dojoType=\"dijit.form.Form\">";
 
                print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
@@ -47,7 +56,7 @@ class Example extends Plugin {
                                                notify_info(transport.responseText);
                                        }
                                });
-                               this.reset();
+                               //this.reset();
                        }
                        </script>";
 
@@ -58,7 +67,7 @@ class Example extends Plugin {
                print "<table width=\"100%\" class=\"prefPrefsList\">";
 
                        print "<tr><td width=\"40%\">".__("Sample value")."</td>";
-                       print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" required=\"1\" name=\"example_value\"></td></tr>";
+                       print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" required=\"1\" name=\"example_value\" value=\"$value\"></td></tr>";
 
                        print "</table>";