From 369dbc19d6ae35c97a2306ae4821c7812e2f24b2 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 17 Aug 2012 14:20:55 +0400 Subject: [PATCH] rework class system to use subdirectories add placeholder plugin/hook system --- backend.php | 2 + classes/article.php | 2 +- classes/{auth_base.php => auth/base.php} | 0 .../{auth_internal.php => auth/internal.php} | 0 classes/{auth_remote.php => auth/remote.php} | 0 classes/{plugin_button.php => button.php} | 2 +- classes/{mail_button.php => button/mail.php} | 2 +- classes/{note_button.php => button/note.php} | 2 +- .../{share_button.php => button/share.php} | 2 +- .../{tweet_button.php => button/tweet.php} | 2 +- classes/feeds.php | 18 +++++++- .../protected.php} | 2 +- .../public.php} | 2 +- classes/opml.php | 2 +- classes/plugin.php | 21 +++++++++ classes/plugin/example.php | 11 +++++ classes/plugins.php | 44 +++++++++++++++++++ classes/{pref_feeds.php => pref/feeds.php} | 2 +- .../{pref_filters.php => pref/filters.php} | 2 +- .../instances.php} | 2 +- classes/{pref_labels.php => pref/labels.php} | 2 +- classes/{pref_prefs.php => pref/prefs.php} | 2 +- classes/{pref_users.php => pref/users.php} | 2 +- classes/rpc.php | 4 +- include/functions.php | 12 ++++- include/rssfuncs.php | 5 +++ public.php | 2 +- update.php | 2 + update_daemon2.php | 2 + 29 files changed, 131 insertions(+), 22 deletions(-) rename classes/{auth_base.php => auth/base.php} (100%) rename classes/{auth_internal.php => auth/internal.php} (100%) rename classes/{auth_remote.php => auth/remote.php} (100%) rename classes/{plugin_button.php => button.php} (80%) rename classes/{mail_button.php => button/mail.php} (99%) rename classes/{note_button.php => button/note.php} (98%) rename classes/{share_button.php => button/share.php} (97%) rename classes/{tweet_button.php => button/tweet.php} (94%) rename classes/{protected_handler.php => handler/protected.php} (69%) rename classes/{public_handler.php => handler/public.php} (99%) create mode 100644 classes/plugin.php create mode 100644 classes/plugin/example.php create mode 100644 classes/plugins.php rename classes/{pref_feeds.php => pref/feeds.php} (99%) rename classes/{pref_filters.php => pref/filters.php} (99%) rename classes/{pref_instances.php => pref/instances.php} (99%) rename classes/{pref_labels.php => pref/labels.php} (99%) rename classes/{pref_prefs.php => pref/prefs.php} (99%) rename classes/{pref_users.php => pref/users.php} (99%) diff --git a/backend.php b/backend.php index 13568d16..b0ef5e0f 100644 --- a/backend.php +++ b/backend.php @@ -77,6 +77,8 @@ return; } + $plugins = new Plugins($link); + $purge_intervals = array( 0 => __("Use default"), -1 => __("Never purge"), diff --git a/classes/article.php b/classes/article.php index 30f0c7d1..16619c9a 100644 --- a/classes/article.php +++ b/classes/article.php @@ -1,5 +1,5 @@ link, "images/art-pub-note.png")."\" style=\"cursor : pointer\" style=\"cursor : pointer\" diff --git a/classes/share_button.php b/classes/button/share.php similarity index 97% rename from classes/share_button.php rename to classes/button/share.php index 9402eddb..74d7128d 100644 --- a/classes/share_button.php +++ b/classes/button/share.php @@ -1,5 +1,5 @@ link, 'images/art-share.png')."\" class='tagsPic' style=\"cursor : pointer\" diff --git a/classes/tweet_button.php b/classes/button/tweet.php similarity index 94% rename from classes/tweet_button.php rename to classes/button/tweet.php index 470bc63a..3157fb77 100644 --- a/classes/tweet_button.php +++ b/classes/button/tweet.php @@ -1,5 +1,5 @@ link, 'images/art-tweet.png')."\" class='tagsPic' style=\"cursor : pointer\" diff --git a/classes/feeds.php b/classes/feeds.php index a3062565..7598b0af 100644 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -1,5 +1,5 @@ hook('headlines_before', $reply); + if (get_pref($this->link, 'COMBINED_DISPLAY_MODE')) { $button_plugins = array(); foreach (explode(",", ARTICLE_BUTTON_PLUGINS) as $p) { - $pclass = trim("${p}_button"); + $pclass = trim("button_${p}"); if (class_exists($pclass)) { $plugin = new $pclass($link); @@ -245,6 +249,12 @@ class Feeds extends Protected_Handler { while ($line = db_fetch_assoc($result)) { + if (get_pref($this->link, 'COMBINED_DISPLAY_MODE')) { + $plugins->hook('cdm_article_before', $line); + } else { + $plugins->hook('headlines_row', $line); + } + $class = ($lnum % 2) ? "even" : "odd"; $id = $line["id"]; @@ -673,11 +683,15 @@ class Feeds extends Protected_Handler { $reply['content'] .= ""; + $plugins->hook('cdm_article_after', $reply['content']); + } ++$lnum; } + $plugins->hook('headlines_after', $reply); + if ($_REQUEST["debug"]) $timing_info = print_checkpoint("PE", $timing_info); } else { diff --git a/classes/protected_handler.php b/classes/handler/protected.php similarity index 69% rename from classes/protected_handler.php rename to classes/handler/protected.php index 5d8d690c..4ce86534 100644 --- a/classes/protected_handler.php +++ b/classes/handler/protected.php @@ -1,5 +1,5 @@ link = $link; + $this->handler = $handler; + $this->initialize(); + } + + function initialize() { + + + } + + function add_listener($hook) { + $this->handler->add_listener($hook, $this); + } +} +?> diff --git a/classes/plugin/example.php b/classes/plugin/example.php new file mode 100644 index 00000000..e10781aa --- /dev/null +++ b/classes/plugin/example.php @@ -0,0 +1,11 @@ +add_listener('article_before'); + } + + function article_before(&$line) { + $line["title"] = "EXAMPLE/REPLACED:" . $line["title"]; + } + } +?> diff --git a/classes/plugins.php b/classes/plugins.php new file mode 100644 index 00000000..6f3720ca --- /dev/null +++ b/classes/plugins.php @@ -0,0 +1,44 @@ +link = $link; + $this->listeners = array(); + $this->load_plugins(); + } + + function load_plugins() { + if (defined('_ENABLE_PLUGINS')) { + $plugins = explode(",", _ENABLE_PLUGINS); + + foreach ($plugins as $p) { + $plugin_class = "plugin_$p"; + if (class_exists($plugin_class)) { + $plugin = new $plugin_class($this->link, $this); + } + } + } + } + + function add_listener($hook_name, $plugin) { + if (!is_array($this->listeners[$hook_name])) + $this->listeners[$hook_name] = array(); + + array_push($this->listeners[$hook_name], $plugin); + } + + function hook($hook_name, &$params) { + if (is_array($this->listeners[$hook_name])) { + foreach ($this->listeners[$hook_name] as $p) { + if (method_exists($p, $hook_name)) { + $p->$hook_name($params); + } + } + } + } + +} +?> diff --git a/classes/pref_feeds.php b/classes/pref/feeds.php similarity index 99% rename from classes/pref_feeds.php rename to classes/pref/feeds.php index b1056119..5899115b 100644 --- a/classes/pref_feeds.php +++ b/classes/pref/feeds.php @@ -1,5 +1,5 @@ hook('article_before', $line); + if ($line["icon_url"]) { $feed_icon = ""; } else { @@ -3359,7 +3365,7 @@ $button_plugins = explode(",", ARTICLE_BUTTON_PLUGINS); foreach ($button_plugins as $p) { - $pclass = trim("${p}_button"); + $pclass = trim("button_${p}"); if (class_exists($pclass)) { $plugin = new $pclass($link); @@ -3468,6 +3474,8 @@ $rv['content'] .= ""; } + $plugins->hook('article_after', $rv); + return $rv; } diff --git a/include/rssfuncs.php b/include/rssfuncs.php index 59fa3d54..4ad08df3 100644 --- a/include/rssfuncs.php +++ b/include/rssfuncs.php @@ -212,6 +212,8 @@ function update_rss_feed($link, $feed, $ignore_daemon = false, $no_cache = false, $override_url = false) { + global $plugins; + require_once "lib/simplepie/simplepie.inc"; require_once "lib/magpierss/rss_fetch.inc"; require_once 'lib/magpierss/rss_utils.inc'; @@ -557,6 +559,9 @@ } foreach ($iterator as $item) { + $hook_params = array("item" => &$item, "feed" => $feed); + + $plugins->hook('rss_update_item', $hook_params); if ($_REQUEST['xdebug'] == 2) { print_r($item); diff --git a/public.php b/public.php index a17fa595..bf13fdd5 100644 --- a/public.php +++ b/public.php @@ -40,7 +40,7 @@ $method = $_REQUEST["op"]; - $handler = new Public_Handler($link, $_REQUEST); + $handler = new Handler_Public($link, $_REQUEST); if ($handler->before($method)) { if ($method && method_exists($handler, $method)) { diff --git a/update.php b/update.php index 292b5c1b..2c7f347d 100755 --- a/update.php +++ b/update.php @@ -56,6 +56,8 @@ init_connection($link); + $plugins = new Plugins($link); + if (in_array("-feeds", $op)) { // Update all feeds needing a update. update_daemon_common($link); diff --git a/update_daemon2.php b/update_daemon2.php index 6410709a..b79a1a2b 100755 --- a/update_daemon2.php +++ b/update_daemon2.php @@ -189,6 +189,8 @@ if (!init_connection($link)) return; + $plugins = new Plugins($link); + // We disable stamp file, since it is of no use in a multiprocess update. // not really, tho for the time being -fox if (!make_stampfile('update_daemon.stamp')) { -- 2.39.5