more work on user-selectable plugins; properly process system and user plugins

This commit is contained in:
Andrew Dolgov 2012-12-25 10:02:08 +04:00
parent de612e7a38
commit d2a421e3cb
23 changed files with 92 additions and 60 deletions

View File

@ -3,9 +3,22 @@ class Plugin {
private $link; private $link;
private $host; private $host;
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;
} }
function about() {
// version, name, description, author, is_system
return array(1.0, "plugin", "No description", "No author", false);
}
function get_js() {
return "";
}
function get_prefs_js() {
return "";
}
} }
?> ?>

View File

@ -14,6 +14,10 @@ class PluginHost {
const HOOK_FEED_PARSED = 6; const HOOK_FEED_PARSED = 6;
const HOOK_UPDATE_TASK = 7; const HOOK_UPDATE_TASK = 7;
const KIND_ALL = 1;
const KIND_SYSTEM = 2;
const KIND_USER = 3;
function __construct($link) { function __construct($link) {
$this->link = $link; $this->link = $link;
} }
@ -65,12 +69,12 @@ class PluginHost {
return array(); return array();
} }
} }
function load_all() { function load_all($kind) {
$plugins = array_map("basename", glob("plugins/*")); $plugins = array_map("basename", glob("plugins/*"));
$this->load(join(",", $plugins)); $this->load(join(",", $plugins), $kind);
} }
function load($classlist) { function load($classlist, $kind) {
$plugins = explode(",", $classlist); $plugins = explode(",", $classlist);
foreach ($plugins as $class) { foreach ($plugins as $class) {
@ -84,14 +88,31 @@ class PluginHost {
if (class_exists($class) && is_subclass_of($class, "Plugin")) { if (class_exists($class) && is_subclass_of($class, "Plugin")) {
$plugin = new $class($this); $plugin = new $class($this);
$this->register_plugin($class, $plugin); switch ($kind) {
case $this::KIND_SYSTEM:
if ($this->is_system($plugin)) {
$plugin->init($this);
$this->register_plugin($class, $plugin);
}
break;
case $this::KIND_USER:
if (!$this->is_system($plugin)) {
$plugin->init($this);
$this->register_plugin($class, $plugin);
}
break;
case $this::KIND_ALL:
$plugin->init($this);
$this->register_plugin($class, $plugin);
break;
}
} }
} }
} }
} }
function is_system($plugin) { function is_system($plugin) {
$about = $plugin->_about(); $about = $plugin->about();
return @$about[3]; return @$about[3];
} }
@ -134,22 +155,17 @@ class PluginHost {
return false; return false;
} }
// only system plugins are allowed to modify commands
function add_command($command, $description, $sender) { function add_command($command, $description, $sender) {
$command = "-" . str_replace("-", "_", strtolower($command)); $command = "-" . str_replace("-", "_", strtolower($command));
if ($this->is_system($sender)) { $this->commands[$command] = array("description" => $description,
$this->commands[$command] = array("description" => $description, "class" => $sender);
"class" => $sender);
}
} }
function del_command($command) { function del_command($command) {
$command = "-" . strtolower($command); $command = "-" . strtolower($command);
if ($this->is_system($sender)) { unset($this->commands[$command]);
unset($this->commands[$command]);
}
} }
function lookup_command($command) { function lookup_command($command) {

View File

@ -670,10 +670,10 @@ class Pref_Prefs extends Handler_Protected {
$user_enabled = array_map("trim", explode(",", get_pref($this->link, "_ENABLED_PLUGINS"))); $user_enabled = array_map("trim", explode(",", get_pref($this->link, "_ENABLED_PLUGINS")));
$tmppluginhost = new PluginHost($link); $tmppluginhost = new PluginHost($link);
$tmppluginhost->load_all(); $tmppluginhost->load_all($tmppluginhost::KIND_ALL);
foreach ($tmppluginhost->get_plugins() as $name => $plugin) { foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
$about = $plugin->_about(); $about = $plugin->about();
if ($about[3]) { if ($about[3]) {
if (in_array($name, $system_enabled)) { if (in_array($name, $system_enabled)) {
@ -709,7 +709,7 @@ class Pref_Prefs extends Handler_Protected {
foreach ($tmppluginhost->get_plugins() as $name => $plugin) { foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
$about = $plugin->_about(); $about = $plugin->about();
if (!$about[3]) { if (!$about[3]) {

View File

@ -172,7 +172,8 @@
// after login, or content encoding errors, disable it. // after login, or content encoding errors, disable it.
define('PLUGINS', 'note'); define('PLUGINS', 'note');
// Plugins to load. Check plugins/ directory for additional information. // Comma-separated list of plugins to load for all users. System plugins have to be specified
// here, user plugins may be loaded per-user using Preferences/Plugins.
define('FEEDBACK_URL', ''); define('FEEDBACK_URL', '');
// Displays an URL for users to provide feedback or comments regarding // Displays an URL for users to provide feedback or comments regarding

View File

@ -716,7 +716,7 @@
$plugins = get_pref($link, "_ENABLED_PLUGINS", $owner_uid); $plugins = get_pref($link, "_ENABLED_PLUGINS", $owner_uid);
global $pluginhost; global $pluginhost;
$pluginhost->load($plugins); $pluginhost->load($plugins, $pluginhost::KIND_USER);
} }
} }
@ -3353,7 +3353,7 @@
global $pluginhost; global $pluginhost;
$pluginhost = new PluginHost($link); $pluginhost = new PluginHost($link);
$pluginhost->load(PLUGINS); $pluginhost->load(PLUGINS, $pluginhost::KIND_ALL);
return true; return true;
} else { } else {

View File

@ -263,7 +263,14 @@
if (!$rss->error()) { if (!$rss->error()) {
global $pluginhost; // We use local pluginhost here because we need to load different per-user feed plugins
$user_plugins = get_pref($link, "_ENABLED_PLUGINS", $owner_uid);
$pluginhost = new PluginHost($link);
$pluginhost->load(PLUGINS, $pluginhost::KIND_ALL);
$pluginhost->load($plugins, $pluginhost::KIND_USER);
$pluginhost->run_hooks($pluginhost::HOOK_FEED_PARSED, "hook_feed_parsed", $rss); $pluginhost->run_hooks($pluginhost::HOOK_FEED_PARSED, "hook_feed_parsed", $rss);
if ($debug_enabled) { if ($debug_enabled) {
@ -538,7 +545,6 @@
"tags" => $entry_tags, "tags" => $entry_tags,
"author" => $entry_author); "author" => $entry_author);
global $pluginhost;
foreach ($pluginhost->get_hooks($pluginhost::HOOK_ARTICLE_FILTER) as $plugin) { foreach ($pluginhost->get_hooks($pluginhost::HOOK_ARTICLE_FILTER) as $plugin) {
$article = $plugin->hook_article_filter($article); $article = $plugin->hook_article_filter($article);
} }

View File

@ -4,22 +4,18 @@ class Digest extends Plugin implements IHandler {
private $link; private $link;
private $host; private $host;
function _about() { function about() {
return array(1.0, return array(1.0,
"Digest mode for tt-rss (tablet friendly UI)", "Digest mode for tt-rss (tablet friendly UI)",
"fox", "fox",
true); true);
} }
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;
$host->add_handler("digest", "*", $this); $host->add_handler("digest", "*", $this);
//$host->add_handler("rpc", "digestinit", $this);
//$host->add_handler("rpc", "digestupdate", $this);
//$host->add_handler("rpc", "digestgetcontents", $this);
} }
function index() { function index() {

View File

@ -6,14 +6,14 @@ class Example extends Plugin {
private $link; private $link;
private $host; private $host;
function _about() { function about() {
return array(1.0, return array(1.0,
"Example plugin #1", "Example plugin #1",
"fox", "fox",
true); true);
} }
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;

View File

@ -7,14 +7,14 @@ class Example_Feed extends Plugin {
private $link; private $link;
private $host; private $host;
function _about() { function about() {
return array(1.0, return array(1.0,
"Example feed plugin", "Example feed plugin",
"fox", "fox",
true); true);
} }
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;

View File

@ -15,14 +15,14 @@ class Example_Routing extends Plugin implements IHandler {
private $link; private $link;
private $host; private $host;
function _about() { function about() {
return array(1.0, return array(1.0,
"Example routing plugin", "Example routing plugin",
"fox", "fox",
true); true);
} }
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;

View File

@ -3,14 +3,14 @@ class Flattr extends Plugin {
private $link; private $link;
private $host; private $host;
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;
$host->add_hook($host::HOOK_ARTICLE_BUTTON, $this); $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
} }
function _about() { function about() {
return array(1.0, return array(1.0,
"Share on Flattr plugin", "Share on Flattr plugin",
"Nic Honing"); "Nic Honing");

View File

@ -3,14 +3,14 @@ class GooglePlus extends Plugin {
private $link; private $link;
private $host; private $host;
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;
$host->add_hook($host::HOOK_ARTICLE_BUTTON, $this); $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
} }
function _about() { function about() {
return array(1.0, return array(1.0,
"Share on Google+ plugin", "Share on Google+ plugin",
"homolibere"); "homolibere");

View File

@ -3,14 +3,14 @@ class Identica extends Plugin {
private $link; private $link;
private $host; private $host;
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;
$host->add_hook($host::HOOK_ARTICLE_BUTTON, $this); $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
} }
function _about() { function about() {
return array(1.0, return array(1.0,
"Share on Identi.ca", "Share on Identi.ca",
"fox"); "fox");

View File

@ -4,7 +4,7 @@ class Import_Export extends Plugin implements IHandler {
private $link; private $link;
private $host; private $host;
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;
@ -12,7 +12,7 @@ class Import_Export extends Plugin implements IHandler {
$host->add_command("xml-import", "USER FILE: import articles from XML", $this); $host->add_command("xml-import", "USER FILE: import articles from XML", $this);
} }
function _about() { function about() {
return array(1.0, return array(1.0,
"Imports and exports user data using a neutral XML format", "Imports and exports user data using a neutral XML format",
"fox"); "fox");

View File

@ -10,14 +10,14 @@ class Instances extends Plugin implements IHandler {
2 => "Invalid object received", 2 => "Invalid object received",
16 => "Access denied" ); 16 => "Access denied" );
function _about() { function about() {
return array(1.0, return array(1.0,
"Support for linking tt-rss instances together and sharing popular feeds.", "Support for linking tt-rss instances together and sharing popular feeds.",
"fox", "fox",
true); true);
} }
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;

View File

@ -4,13 +4,13 @@ class Mail extends Plugin {
private $link; private $link;
private $host; private $host;
function _about() { function about() {
return array(1.0, return array(1.0,
"Adds a share article via email button", "Adds a share article via email button",
"fox"); "fox");
} }
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;

View File

@ -3,13 +3,13 @@ class Note extends Plugin {
private $link; private $link;
private $host; private $host;
function _about() { function about() {
return array(1.0, return array(1.0,
"Adds support for setting article notes", "Adds support for setting article notes",
"fox"); "fox");
} }
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;

View File

@ -3,13 +3,13 @@ class Pinterest extends Plugin {
private $link; private $link;
private $host; private $host;
function _about() { function about() {
return array(1.0, return array(1.0,
"Share article via Pinterest", "Share article via Pinterest",
"?"); "?");
} }
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;

View File

@ -4,13 +4,13 @@ class Pocket extends Plugin {
private $link; private $link;
private $host; private $host;
function _about() { function about() {
return array(1.0, return array(1.0,
"Share article via Pocket (formerly Read It Later)", "Share article via Pocket (formerly Read It Later)",
"?"); "?");
} }
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;

View File

@ -4,13 +4,13 @@ class RedditImgur extends Plugin {
private $link; private $link;
private $host; private $host;
function _about() { function about() {
return array(1.0, return array(1.0,
"Inline image links in Reddit RSS feeds", "Inline image links in Reddit RSS feeds",
"fox"); "fox");
} }
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;

View File

@ -3,13 +3,13 @@ class Share extends Plugin {
private $link; private $link;
private $host; private $host;
function _about() { function about() {
return array(1.0, return array(1.0,
"Share article by unique URL", "Share article by unique URL",
"fox"); "fox");
} }
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;

View File

@ -4,14 +4,14 @@ class Updater extends Plugin {
private $link; private $link;
private $host; private $host;
function _about() { function about() {
return array(1.0, return array(1.0,
"Updates tt-rss installation to latest version.", "Updates tt-rss installation to latest version.",
"fox", "fox",
true); true);
} }
function __construct($host) { function init($host) {
$this->link = $host->get_link(); $this->link = $host->get_link();
$this->host = $host; $this->host = $host;

View File

@ -260,9 +260,9 @@
if (in_array("-list-plugins", $op)) { if (in_array("-list-plugins", $op)) {
$tmppluginhost = new PluginHost($link); $tmppluginhost = new PluginHost($link);
$tmppluginhost->load_all(); $tmppluginhost->load_all($tmppluginhost::KIND_ALL);
foreach ($tmppluginhost->get_plugins() as $name => $plugin) { foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
$about = $plugin->_about(); $about = $plugin->about();
printf("%-60s - v%.2f (by %s)\n%s\n\n", printf("%-60s - v%.2f (by %s)\n%s\n\n",
$name, $about[0], $about[2], $about[1]); $name, $about[0], $about[2], $about[1]);