add basic updater for stuff in plugins.local

master
Andrew Dolgov 4 years ago
parent 5edcbf2e9b
commit dfff2cef7b

@ -135,6 +135,9 @@
} else { } else {
if (method_exists($handler, "catchall")) { if (method_exists($handler, "catchall")) {
$handler->catchall($method); $handler->catchall($method);
} else {
header("Content-Type: text/json");
print Errors::to_json(Errors::E_UNKNOWN_METHOD, ["info" => get_class($handler) . "->$method"]);
} }
} }
$handler->after(); $handler->after();
@ -154,6 +157,6 @@
} }
header("Content-Type: text/json"); header("Content-Type: text/json");
print Errors::to_json(Errors::E_UNKNOWN_METHOD); print Errors::to_json(Errors::E_UNKNOWN_METHOD, ["info" => get_class($handler) . "->$method"]);
?> ?>

@ -842,6 +842,8 @@ class Pref_Prefs extends Handler_Protected {
$is_checked = "checked='1'"; $is_checked = "checked='1'";
} }
$can_update = is_dir(dirname(dirname(__DIR__)) . "/plugins.local/$name/.git");
?> ?>
<fieldset class='prefs plugin'> <fieldset class='prefs plugin'>
@ -853,12 +855,19 @@ class Pref_Prefs extends Handler_Protected {
</input> </input>
</label> </label>
<?php if ($_SESSION["access_level"] >= 10 && $can_update) { ?>
<button dojoType='dijit.form.Button' onclick='Helpers.Plugins.updateLocal("<?= htmlspecialchars($name) ?>")'>
<?= \Controls\icon("update") ?>
<?= __("Update") ?>
</button>
<?php } ?>
<?php if (count($tmppluginhost->get_all($plugin)) > 0) { <?php if (count($tmppluginhost->get_all($plugin)) > 0) {
if (in_array($name, $system_enabled) || in_array($name, $user_enabled)) { ?> if (in_array($name, $system_enabled) || in_array($name, $user_enabled)) { ?>
<button dojoType='dijit.form.Button' <button dojoType='dijit.form.Button'
onclick='Helpers.Prefs.clearPluginData("<?= htmlspecialchars($name) ?>")'> onclick='Helpers.Plugins.clearPluginData("<?= htmlspecialchars($name) ?>")'>
<i class='material-icons'>clear</i> <?= __("Clear data") ?></button> <i class='material-icons'>clear</i> <?= __("Clear data") ?></button>
<?php } <?php }
} ?> } ?>
<?php if ($about[4] ?? false) { ?> <?php if ($about[4] ?? false) { ?>
@ -937,6 +946,12 @@ class Pref_Prefs extends Handler_Protected {
<button dojoType='dijit.form.Button' class='alt-primary' type='submit'> <button dojoType='dijit.form.Button' class='alt-primary' type='submit'>
<?= __("Enable selected plugins") ?> <?= __("Enable selected plugins") ?>
</button> </button>
<?php if ($_SESSION["access_level"] >= 10) { ?>
<button dojoType='dijit.form.Button' onclick="Helpers.Plugins.updateLocal()">
<?= \Controls\icon("update") ?>
<?= __("Update local plugins") ?>
</button>
<?php } ?>
</div> </div>
</div> </div>
</form> </form>
@ -1073,6 +1088,59 @@ class Pref_Prefs extends Handler_Protected {
set_pref(Prefs::_ENABLED_PLUGINS, $plugins); set_pref(Prefs::_ENABLED_PLUGINS, $plugins);
} }
private function _update_plugin($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 pull --ff-only -q origin master", $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;
}
function updateLocalPlugins() {
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->_update_plugin($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->_update_plugin($root_dir, $plugin_name)]);
}
}
}
print json_encode($rv);
}
}
function clearplugindata() { function clearplugindata() {
$name = clean($_REQUEST["name"]); $name = clean($_REQUEST["name"]);

@ -278,20 +278,72 @@ const Helpers = {
}); });
} }
}, },
refresh: function() {
xhr.post("backend.php", { op: "pref-prefs" }, (reply) => {
dijit.byId('prefsTab').attr('content', reply);
Notify.close();
});
},
},
Plugins: {
clearPluginData: function(name) { clearPluginData: function(name) {
if (confirm(__("Clear stored data for this plugin?"))) { if (confirm(__("Clear stored data for this plugin?"))) {
Notify.progress("Loading, please wait..."); Notify.progress("Loading, please wait...");
xhr.post("backend.php", {op: "pref-prefs", method: "clearplugindata", name: name}, () => { xhr.post("backend.php", {op: "pref-prefs", method: "clearPluginData", name: name}, () => {
Helpers.Prefs.refresh(); Helpers.Prefs.refresh();
}); });
} }
}, },
refresh: function() { updateLocal: function(name = null) {
xhr.post("backend.php", { op: "pref-prefs" }, (reply) => { const msg = name ? __("Update %p using git?").replace("%p", name) :
dijit.byId('prefsTab').attr('content', reply); __("Update all local plugins using git?");
Notify.close();
}); if (confirm(msg)) {
const dialog = new fox.SingleUseDialog({
title: __("Plugin Updater"),
content: `
<ul class="panel panel-scrollable update-results">
<li>${__("Loading, please wait...")}</li>
</ul>
<footer class="text-center">
${App.FormFields.submit_tag(__("Close this window"))}
</footer>
`,
});
const tmph = dojo.connect(dialog, 'onShow', function () {
dojo.disconnect(tmph);
xhr.json("backend.php", {op: "pref-prefs", method: "updateLocalPlugins", name: name}, (reply) => {
const container = dialog.domNode.querySelector(".update-results");
if (!reply) {
container.innerHTML = __("Operation failed: check event log.");
} else {
container.innerHTML = "";
reply.forEach((p) => {
container.innerHTML +=
`
<li><h3 style="margin-top: 0">${p.plugin}</h3>
${p.rv.e ? `<pre class="small text-error">${p.rv.e}</pre>` : ''}
${p.rv.o ? `<pre class="small text-success">${p.rv.o}</pre>` : ''}
<p class="small">
${p.rv.s ? __("Exited with RC: %d").replace("%d", p.rv.s) : __("OK")}
</p>
</li>
`
});
}
});
});
dialog.show();
}
}, },
}, },
OPML: { OPML: {

Loading…
Cancel
Save