move some old-style handlers to new callback ones

This commit is contained in:
Andrew Dolgov 2021-02-08 16:14:48 +03:00
parent 1d5c8ee500
commit 3b52cea811
8 changed files with 114 additions and 81 deletions

View File

@ -31,14 +31,14 @@ class Article extends Handler_Protected {
$pluginhost->load_all(PluginHost::KIND_ALL, $owner_uid); $pluginhost->load_all(PluginHost::KIND_ALL, $owner_uid);
//$pluginhost->load_data(); //$pluginhost->load_data();
foreach ($pluginhost->get_hooks(PluginHost::HOOK_GET_FULL_TEXT) as $p) { $pluginhost->run_hooks_callback(PluginHost::HOOK_GET_FULL_TEXT,
$extracted_content = $p->hook_get_full_text($url); function ($result) use (&$content) {
if ($result) {
if ($extracted_content) { $content = $result;
$content = $extracted_content; return true;
break; }
} },
} $url);
} }
$content_hash = sha1($content); $content_hash = sha1($content);

View File

@ -399,9 +399,8 @@ class DiskCache {
$tmppluginhost->load(PLUGINS, PluginHost::KIND_SYSTEM); $tmppluginhost->load(PLUGINS, PluginHost::KIND_SYSTEM);
//$tmppluginhost->load_data(); //$tmppluginhost->load_data();
foreach ($tmppluginhost->get_hooks(PluginHost::HOOK_SEND_LOCAL_FILE) as $plugin) { if ($tmppluginhost->run_hooks_until(PluginHost::HOOK_SEND_LOCAL_FILE, true, $filename))
if ($plugin->hook_send_local_file($filename)) return true; return true;
}
header("Content-type: $mimetype"); header("Content-type: $mimetype");

View File

@ -135,7 +135,7 @@ class PluginHost {
$method = strtolower($hook); $method = strtolower($hook);
foreach ($this->get_hooks($hook) as $plugin) { foreach ($this->get_hooks($hook) as $plugin) {
Debug::log("invoking: " . get_class($plugin) . "->$hook()", Debug::$LOG_VERBOSE); //Debug::log("invoking: " . get_class($plugin) . "->$hook()", Debug::$LOG_VERBOSE);
try { try {
$plugin->$method(...$args); $plugin->$method(...$args);
@ -147,6 +147,26 @@ class PluginHost {
} }
} }
function run_hooks_until($hook, $check, ...$args) {
$method = strtolower($hook);
foreach ($this->get_hooks($hook) as $plugin) {
try {
$result = $plugin->$method(...$args);
if ($result == $check)
return true;
} catch (Exception $ex) {
user_error($ex, E_USER_WARNING);
} catch (Error $err) {
user_error($err, E_USER_WARNING);
}
}
return false;
}
function run_hooks_callback($hook, $callback, ...$args) { function run_hooks_callback($hook, $callback, ...$args) {
$method = strtolower($hook); $method = strtolower($hook);
@ -154,7 +174,25 @@ class PluginHost {
//Debug::log("invoking: " . get_class($plugin) . "->$hook()", Debug::$LOG_VERBOSE); //Debug::log("invoking: " . get_class($plugin) . "->$hook()", Debug::$LOG_VERBOSE);
try { try {
$callback($plugin->$method(...$args), $plugin); if ($callback($plugin->$method(...$args), $plugin))
break;
} catch (Exception $ex) {
user_error($ex, E_USER_WARNING);
} catch (Error $err) {
user_error($err, E_USER_WARNING);
}
}
}
function chain_hooks_callback($hook, $callback, &...$args) {
$method = strtolower($hook);
foreach ($this->get_hooks($hook) as $plugin) {
//Debug::log("invoking: " . get_class($plugin) . "->$hook()", Debug::$LOG_VERBOSE);
try {
if ($callback($plugin->$method(...$args), $plugin))
break;
} catch (Exception $ex) { } catch (Exception $ex) {
user_error($ex, E_USER_WARNING); user_error($ex, E_USER_WARNING);
} catch (Error $err) { } catch (Error $err) {

View File

@ -140,9 +140,13 @@ class Pref_Filters extends Handler_Protected {
$line["content_preview"] = truncate_string(strip_tags($line["content"]), 200, '…'); $line["content_preview"] = truncate_string(strip_tags($line["content"]), 200, '…');
foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) { $excerpt_length = 100;
$line = $p->hook_query_headlines($line, 100);
} PluginHost::getInstance()->chain_hooks_callback(PluginHost::HOOK_QUERY_HEADLINES,
function ($result) use (&$line) {
$line = $result;
},
$line, $excerpt_length);
$content_preview = $line["content_preview"]; $content_preview = $line["content_preview"];

View File

@ -641,9 +641,11 @@ class RPC extends Handler_Protected {
"help_dialog" => __("Show help dialog")) "help_dialog" => __("Show help dialog"))
); );
foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_HOTKEY_INFO) as $plugin) { PluginHost::getInstance()->chain_hooks_callback(PluginHost::HOOK_HOTKEY_INFO,
$hotkeys = $plugin->hook_hotkey_info($hotkeys); function ($result) use (&$hotkeys) {
} $hotkeys = $result;
},
$hotkeys);
return $hotkeys; return $hotkeys;
} }
@ -712,9 +714,11 @@ class RPC extends Handler_Protected {
"?" => "help_dialog", "?" => "help_dialog",
); );
foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_HOTKEY_MAP) as $plugin) { PluginHost::getInstance()->chain_hooks_callback(PluginHost::HOOK_HOTKEY_MAP,
$hotkeys = $plugin->hook_hotkey_map($hotkeys); function ($result) use (&$hotkeys) {
} $hotkeys = $result;
},
$hotkeys);
$prefixes = array(); $prefixes = array();

View File

@ -279,10 +279,11 @@ class RSSUtils {
$pluginhost->load($user_plugins, PluginHost::KIND_USER, $owner_uid); $pluginhost->load($user_plugins, PluginHost::KIND_USER, $owner_uid);
//$pluginhost->load_data(); //$pluginhost->load_data();
$basic_info = array(); $basic_info = [];
foreach ($pluginhost->get_hooks(PluginHost::HOOK_FEED_BASIC_INFO) as $plugin) {
$basic_info = $plugin->hook_feed_basic_info($basic_info, $fetch_url, $owner_uid, $feed, $auth_login, $auth_pass); $pluginhost->run_hooks_callback(PluginHost::HOOK_FEED_BASIC_INFO, function ($result) use (&$basic_info) {
} $basic_info = $result;
}, $basic_info, $fetch_url, $owner_uid, $feed, $auth_login, $auth_pass);
if (!$basic_info) { if (!$basic_info) {
$feed_data = UrlHelper::fetch($fetch_url, false, $feed_data = UrlHelper::fetch($fetch_url, false,
@ -810,27 +811,16 @@ class RSSUtils {
$start_ts = microtime(true); $start_ts = microtime(true);
PluginHost::getInstance()->run_hooks_callback(PluginHost::HOOK_ARTICLE_FILTER, $pluginhost->chain_hooks_callback(PluginHost::HOOK_ARTICLE_FILTER,
function ($result, $plugin) use (&$article, &$entry_plugin_data, $start_ts) { function ($result, $plugin) use (&$article, &$entry_plugin_data, $start_ts) {
$article = $result; $article = $result;
$entry_plugin_data .= mb_strtolower(get_class($plugin)) . ","; $entry_plugin_data .= mb_strtolower(get_class($plugin)) . ",";
Debug::log(sprintf("=== %.4f (sec) %s", microtime(true) - $start_ts, get_class($plugin)), Debug::log(sprintf("=== %.4f (sec) %s", microtime(true) - $start_ts, get_class($plugin)),
Debug::$LOG_VERBOSE); Debug::$LOG_VERBOSE);
}, },
$article); $article);
/* foreach ($pluginhost->get_hooks(PluginHost::HOOK_ARTICLE_FILTER) as $plugin) {
Debug::log("... " . get_class($plugin), Debug::$LOG_VERBOSE);
$start = microtime(true);
$article = $plugin->hook_article_filter($article);
Debug::log(sprintf("=== %.4f (sec)", microtime(true) - $start), Debug::$LOG_VERBOSE);
$entry_plugin_data .= mb_strtolower(get_class($plugin)) . ",";
} */
if (Debug::get_loglevel() >= 3) { if (Debug::get_loglevel() >= 3) {
print "processed content: "; print "processed content: ";

View File

@ -41,14 +41,10 @@ class Sanitizer {
} }
public static function iframe_whitelisted($entry) { public static function iframe_whitelisted($entry) {
@$src = parse_url($entry->getAttribute("src"), PHP_URL_HOST); $src = parse_url($entry->getAttribute("src"), PHP_URL_HOST);
if ($src) { if (!empty($src))
foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_IFRAME_WHITELISTED) as $plugin) { return PluginHost::getInstance()->run_hooks_until(PluginHost::HOOK_IFRAME_WHITELISTED, true, $src);
if ($plugin->hook_iframe_whitelisted($src))
return true;
}
}
return false; return false;
} }
@ -153,16 +149,17 @@ class Sanitizer {
$disallowed_attributes = array('id', 'style', 'class', 'width', 'height', 'allow'); $disallowed_attributes = array('id', 'style', 'class', 'width', 'height', 'allow');
foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SANITIZE) as $plugin) { PluginHost::getInstance()->chain_hooks_callback(PluginHost::HOOK_SANITIZE,
$retval = $plugin->hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id); function ($result) use (&$doc, &$allowed_elements, &$disallowed_attributes) {
if (is_array($retval)) { if (is_array($result)) {
$doc = $retval[0]; $doc = $result[0];
$allowed_elements = $retval[1]; $allowed_elements = $result[1];
$disallowed_attributes = $retval[2]; $disallowed_attributes = $result[2];
} else { } else {
$doc = $retval; $doc = $result;
} }
} },
$doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id);
$doc->removeChild($doc->firstChild); //remove doctype $doc->removeChild($doc->firstChild); //remove doctype
$doc = self::strip_harmful_tags($doc, $allowed_elements, $disallowed_attributes); $doc = self::strip_harmful_tags($doc, $allowed_elements, $disallowed_attributes);

View File

@ -153,9 +153,9 @@
<img src='images/indicator_tiny.gif'/> <img src='images/indicator_tiny.gif'/>
<?php echo __("Loading, please wait..."); ?></div> <?php echo __("Loading, please wait..."); ?></div>
<?php <?php
foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FEED_TREE) as $p) { PluginHost::getInstance()->run_hooks_callback(PluginHost::HOOK_FEED_TREE, function ($result) {
echo $p->hook_feed_tree(); echo $result;
} });
?> ?>
<div id="feedTree"></div> <div id="feedTree"></div>
</div> </div>
@ -174,9 +174,10 @@
title="<?php echo __('Updates are available from Git.') ?>">new_releases</i> title="<?php echo __('Updates are available from Git.') ?>">new_releases</i>
<?php <?php
foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_MAIN_TOOLBAR_BUTTON) as $p) {
echo $p->hook_main_toolbar_button(); PluginHost::getInstance()->run_hooks_callback(PluginHost::HOOK_MAIN_TOOLBAR_BUTTON, function ($result) {
} echo $result;
});
?> ?>
<form id="toolbar-headlines" action="" style="order : 10" onsubmit='return false'> <form id="toolbar-headlines" action="" style="order : 10" onsubmit='return false'>
@ -206,13 +207,13 @@
<option value="date_reverse"><?php echo __('Oldest first') ?></option> <option value="date_reverse"><?php echo __('Oldest first') ?></option>
<option value="title"><?php echo __('Title') ?></option> <option value="title"><?php echo __('Title') ?></option>
<?php foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_HEADLINES_CUSTOM_SORT_MAP) as $p) { <?php
$sort_map = $p->hook_headlines_custom_sort_map(); PluginHost::getInstance()->run_hooks_callback(PluginHost::HOOK_HEADLINES_CUSTOM_SORT_MAP, function ($result) {
foreach ($result as $sort_value => $sort_title) {
foreach ($sort_map as $sort_value => $sort_title) { print "<option value=\"" . htmlspecialchars($sort_value) . "\">$sort_title</option>";
print "<option value=\"" . htmlspecialchars($sort_value) . "\">$sort_title</option>"; }
} });
} ?> ?>
</select> </select>
<div dojoType="fox.form.ComboButton" onclick="Feeds.catchupCurrent()"> <div dojoType="fox.form.ComboButton" onclick="Feeds.catchupCurrent()">
@ -235,9 +236,9 @@
<div class="action-chooser" style="order : 30"> <div class="action-chooser" style="order : 30">
<?php <?php
foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_TOOLBAR_BUTTON) as $p) { PluginHost::getInstance()->run_hooks_callback(PluginHost::HOOK_TOOLBAR_BUTTON, function ($result) {
echo $p->hook_toolbar_button(); echo $result;
} });
?> ?>
<div dojoType="fox.form.DropDownButton" class="action-button" title="<?php echo __('Actions...') ?>"> <div dojoType="fox.form.DropDownButton" class="action-button" title="<?php echo __('Actions...') ?>">
@ -257,9 +258,9 @@
<div dojoType="dijit.MenuItem" onclick="App.onActionSelected('qmcHKhelp')"><?php echo __('Keyboard shortcuts help') ?></div> <div dojoType="dijit.MenuItem" onclick="App.onActionSelected('qmcHKhelp')"><?php echo __('Keyboard shortcuts help') ?></div>
<?php <?php
foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ACTION_ITEM) as $p) { PluginHost::getInstance()->run_hooks_callback(PluginHost::HOOK_ACTION_ITEM, function ($result) {
echo $p->hook_action_item(); echo $result;
} });
?> ?>
<?php if (empty($_SESSION["hide_logout"])) { ?> <?php if (empty($_SESSION["hide_logout"])) { ?>