From de63e3799a28a51aa132c7c9e1dea44b2dfde800 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sat, 27 Feb 2021 17:29:41 +0300 Subject: only show plugin update buttons when needed --- classes/pref/prefs.php | 77 ++++++++++++++++++++++++++++++++++++++++++++------ js/PrefHelpers.js | 29 ++++++++++++++++++- 2 files changed, 96 insertions(+), 10 deletions(-) diff --git a/classes/pref/prefs.php b/classes/pref/prefs.php index 7ab9966ba..3bdcebebc 100644 --- a/classes/pref/prefs.php +++ b/classes/pref/prefs.php @@ -841,9 +841,6 @@ class Pref_Prefs extends Handler_Protected { } else if (in_array($name, $user_enabled)) { $is_checked = "checked='1'"; } - - $can_update = is_dir(dirname(dirname(__DIR__)) . "/plugins.local/$name/.git"); - ?>
@@ -855,8 +852,10 @@ class Pref_Prefs extends Handler_Protected { - = 10 && $can_update) { ?> - @@ -901,6 +900,12 @@ class Pref_Prefs extends Handler_Protected { } + = 10) { ?> + + + @@ -940,14 +945,14 @@ class Pref_Prefs extends Handler_Protected {
- = 10) { ?> - @@ -1088,6 +1093,33 @@ class Pref_Prefs extends Handler_Protected { set_pref(Prefs::_ENABLED_PLUGINS, $plugins); } + private function _plugin_needs_update($root_dir, $plugin_name) { + $plugin_dir = "$root_dir/plugins.local/" . basename($plugin_name); + $rv = []; + + if (is_dir($plugin_dir) && is_dir("$plugin_dir/.git")) { + $pipes = []; + + $descriptorspec = [ + //0 => ["pipe", "r"], // STDIN + 1 => ["pipe", "w"], // STDOUT + 2 => ["pipe", "w"], // STDERR + ]; + + $proc = proc_open("git fetch -q origin -a && git log HEAD..origin/master --oneline", $descriptorspec, $pipes, $plugin_dir); + + if (is_resource($proc)) { + $rv["o"] = stream_get_contents($pipes[1]); + $rv["e"] = stream_get_contents($pipes[2]); + $status = proc_close($proc); + $rv["s"] = $status; + } + } + + return $rv; + } + + private function _update_plugin($root_dir, $plugin_name) { $plugin_dir = "$root_dir/plugins.local/" . basename($plugin_name); $rv = []; @@ -1096,12 +1128,12 @@ class Pref_Prefs extends Handler_Protected { $pipes = []; $descriptorspec = [ - 0 => ["pipe", "r"], // STDIN + //0 => ["pipe", "r"], // STDIN 1 => ["pipe", "w"], // STDOUT 2 => ["pipe", "w"], // STDERR ]; - $proc = proc_open("git pull --ff-only -q origin master", $descriptorspec, $pipes, $plugin_dir); + $proc = proc_open("git fetch origin -a && git log HEAD..origin/master --oneline && git pull --ff-only origin master", $descriptorspec, $pipes, $plugin_dir); if (is_resource($proc)) { $rv["o"] = stream_get_contents($pipes[1]); @@ -1114,6 +1146,33 @@ class Pref_Prefs extends Handler_Protected { return $rv; } + function checkForPluginUpdates() { + if ($_SESSION["access_level"] >= 10) { + $plugin_name = $_REQUEST["name"] ?? ""; + + # we're in classes/pref/ + $root_dir = dirname(dirname(__DIR__)); + + $rv = []; + + if (!empty($plugin_name)) { + array_push($rv, ["plugin" => $plugin_name, "rv" => $this->_plugin_needs_update($root_dir, $plugin_name)]); + } else { + $plugin_dirs = array_filter(glob("$root_dir/plugins.local/*"), "is_dir"); + + foreach ($plugin_dirs as $dir) { + if (is_dir("$dir/.git")) { + $plugin_name = basename($dir); + + array_push($rv, ["plugin" => $plugin_name, "rv" => $this->_plugin_needs_update($root_dir, $plugin_name)]); + } + } + } + + print json_encode($rv); + } + } + function updateLocalPlugins() { if ($_SESSION["access_level"] >= 10) { $plugin_name = $_REQUEST["name"] ?? ""; diff --git a/js/PrefHelpers.js b/js/PrefHelpers.js index 125cc20d0..ff7766d8a 100644 --- a/js/PrefHelpers.js +++ b/js/PrefHelpers.js @@ -295,7 +295,34 @@ const Helpers = { }); } }, - updateLocal: function(name = null) { + checkForUpdate: function(name = null) { + Notify.progress("Checking for plugin updates..."); + + xhr.json("backend.php", {op: "pref-prefs", method: "checkForPluginUpdates", name: name}, (reply) => { + Notify.close(); + + if (reply) { + let plugins_with_updates = 0; + + reply.forEach((p) => { + if (p.rv.o) { + const button = dijit.getEnclosingWidget(App.find(`*[data-update-btn-for-plugin="${p.plugin}"]`)); + + if (button) { + button.domNode.show(); + ++plugins_with_updates; + } + } + }); + + if (plugins_with_updates > 0) + App.find(".update-all-plugins-btn").show(); + } else { + Notify.error("Unable to check for plugin updates."); + } + }); + }, + update: function(name = null) { const msg = name ? __("Update %p using git?").replace("%p", name) : __("Update all local plugins using git?"); -- cgit v1.2.3