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 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 9 deletions(-) (limited to 'classes/pref/prefs.php') 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"] ?? ""; -- cgit v1.2.3-54-g00ecf