gpm functionality in admin

This commit is contained in:
Gert
2015-04-13 21:37:12 +02:00
parent 6cb3749c56
commit cb77f0f848
8 changed files with 385 additions and 111 deletions

View File

@@ -336,10 +336,91 @@ class AdminController
*/
public function taskInstall()
{
$mode = $this->view === 'plugins' ? 'plugin' : 'theme';
require_once __DIR__ . '/gpm.php';
$package = $this->route;
$this->admin->setMessage("Actual installation is not hooked up to GPM yet.");
$result = \Grav\Plugin\Admin\Gpm::install($package, []);
if ($result) {
$this->admin->setMessage("Installation successful.");
} else {
$this->admin->setMessage("Installation failed.");
}
$this->post = array('_redirect' => $this->view . '/' . $this->route);
return true;
}
/**
* Handles updating plugins and themes
*
* @return bool True is the action was performed
*/
public function taskUpdate()
{
require_once __DIR__ . '/gpm.php';
$package = $this->route;
// Update multi mode
if (!$package) {
$package = [];
if ($this->view === 'plugins' || $this->view === 'update') {
$package = $this->admin->gpm()->getUpdatablePlugins();
}
if ($this->view === 'themes' || $this->view === 'update') {
$package = array_merge($package, $this->admin->gpm()->getUpdatableThemes());
}
}
$result = \Grav\Plugin\Admin\Gpm::update($package, []);
if ($this->view === 'update') {
if ($result) {
$this->admin->json_response = ['success', 'Everything updated'];
} else {
$this->admin->json_response = ['error', 'Updates failed'];
}
} else {
if ($result) {
$this->admin->setMessage("Installation successful.");
} else {
$this->admin->setMessage("Installation failed.");
}
$this->post = array('_redirect' => $this->view . '/' . $this->route);
}
return true;
}
/**
* Handles uninstalling plugins and themes
*
* @return bool True is the action was performed
*/
public function taskUninstall()
{
require_once __DIR__ . '/gpm.php';
$package = $this->route;
$result = \Grav\Plugin\Admin\Gpm::uninstall($package, []);
if ($result) {
$this->admin->setMessage("Uninstall successful.");
} else {
$this->admin->setMessage("Uninstall failed.");
}
$this->post = array('_redirect' => $this->view);
return true;
}

159
classes/gpm.php Normal file
View File

@@ -0,0 +1,159 @@
<?php
namespace Grav\Plugin\Admin;
use Grav\Common\GravTrait;
use Grav\Common\GPM\GPM as GravGPM;
use Grav\Common\GPM\Installer;
use Grav\Common\GPM\Response;
use Grav\Common\Filesystem\Folder;
use Grav\Common\GPM\Common\Package;
class Gpm
{
use GravTrait;
// Probably should move this to Grav DI container?
protected static $GPM;
public static function GPM()
{
if (!static::$GPM) {
static::$GPM = new GravGPM();
}
return static::$GPM;
}
/**
* Default options for the install
* @var array
*/
protected static $options = [
'destination' => GRAV_ROOT,
'overwrite' => true,
'ignore_symlinks' => true,
'skip_invalid' => true,
'install_deps' => true
];
public static function install($packages, $options)
{
$options = array_merge(self::$options, $options);
if (
!Installer::isGravInstance($options['destination']) ||
!Installer::isValidDestination($options['destination'], [Installer::EXISTS, Installer::IS_LINK])
) {
return false;
}
$packages = is_array($packages) ? $packages : [ $packages ];
$count = count($packages);
$packages = array_filter(array_map(function ($p) {
return !is_string($p) ? $p instanceof Package ? $p : false : self::GPM()->findPackage($p);
}, $packages));
if (!$options['skip_invalid'] && $count !== count($packages)) {
return false;
}
foreach ($packages as $package) {
if (isset($package->dependencies) && $options['install_deps']) {
$result = static::install($package->dependencies, $options);
if (!$result) {
return false;
}
}
// Check destination
Installer::isValidDestination($options['destination'] . DS . $package->install_path);
if (Installer::lastErrorCode() === Installer::EXISTS && !$options['overwrite']) {
return false;
}
if (Installer::lastErrorCode() === Installer::IS_LINK && !$options['ignore_symlinks']) {
return false;
}
$local = static::download($package);
Installer::install($local, $options['destination'], ['install_path' => $package->install_path]);
Folder::delete(dirname($local));
$errorCode = Installer::lastErrorCode();
if (Installer::lastErrorCode() & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) {
return false;
}
}
return true;
}
public static function update($packages, $options)
{
$options['overwrite'] = true;
return static::install($packages, $options);
}
public static function uninstall($packages, $options)
{
$options = array_merge(self::$options, $options);
$packages = is_array($packages) ? $packages : [ $packages ];
$count = count($packages);
$packages = array_filter(array_map(function ($p) {
if (is_string($p)) {
$p = strtolower($p);
$plugin = static::GPM()->getInstalledPlugin($p);
$p = $plugin ?: static::GPM()->getInstalledTheme($p);
}
return $p instanceof Package ? $p : false;
}, $packages));
if (!$options['skip_invalid'] && $count !== count($packages)) {
return false;
}
foreach ($packages as $package) {
$location = self::getGrav()['locator']->findResource($package->package_type . '://' . $package->slug);
// Check destination
Installer::isValidDestination($location);
if (Installer::lastErrorCode() === Installer::IS_LINK && !$options['ignore_symlinks']) {
return false;
}
Installer::uninstall($location);
$errorCode = Installer::lastErrorCode();
if ($errorCode && $errorCode !== Installer::IS_LINK && $errorCode !== Installer::EXISTS) {
return false;
}
}
return true;
}
private static function download($package)
{
$contents = Response::get($package->zipball_url, []);
$cache_dir = self::getGrav()['locator']->findResource('cache://', true);
$cache_dir = $cache_dir . DS . 'tmp/Grav-' . uniqid();
Folder::mkdir($cache_dir);
$filename = $package->slug . basename($package->zipball_url);
file_put_contents($cache_dir . DS . $filename, $contents);
return $cache_dir . DS . $filename;
}
}

7
pages/admin/update.md Normal file
View File

@@ -0,0 +1,7 @@
---
title: Cache
access:
admin.maintenance: true
admin.super: true
---

View File

@@ -87,6 +87,29 @@ $(function () {
});
});
// Cache Clear
$('[data-maintenance-update]').on('click', function(e) {
$(this).attr('disabled','disabled').find('> .fa').removeClass('fa-cloud-download').addClass('fa-refresh fa-spin');
var url = $(this).data('maintenanceUpdate');
ajaxRequest({
dataType: "json",
url: url,
success: function(result, status) {
if (result.status == 'success') {
toastr.success(result.message);
} else {
toastr.error(result.message);
}
}
}).complete(function() {
GPMRefresh();
$('[data-maintenance-update]').removeAttr('disabled').find('> .fa').removeClass('fa-refresh fa-spin').addClass('fa-cloud-download');
});
});
var GPMRefresh = function () {
ajaxRequest({
dataType: "JSON",
url: window.location.href,
@@ -104,8 +127,6 @@ $(function () {
installed = response.payload.installed,
resources = response.payload.resources;
//console.log(grav, resources);
// grav updatable
if (grav.isUpdatable) {
var icon = '<i class="fa fa-bullhorn"></i> ';
@@ -127,7 +148,7 @@ $(function () {
var length,
icon = '<i class="fa fa-bullhorn"></i>',
content = '{updates} of your {type} have an <strong>update available</strong>',
button = '<button class="button button-small secondary">Update {Type}</button>',
button = '<a href="{location}/task:update" class="button button-small secondary">Update {Type}</a>',
plugins = $('.grav-update.plugins'),
themes = $('.grav-update.themes'),
sidebar = {plugins: $('#admin-menu a[href$="/plugins"]'), themes: $('#admin-menu a[href$="/themes"]')};
@@ -151,7 +172,7 @@ $(function () {
// list page
if (plugins[0] && (length = Object.keys(resources.plugins).length)) {
content = jQuery.substitute(content, {updates: length, type: 'plugins'});
button = jQuery.substitute(button, {Type: 'All Plugins'});
button = jQuery.substitute(button, {Type: 'All Plugins', location: GravAdmin.config.base_url_relative + '/plugins'});
plugins.html('<p>' + icon + content + button + '</p>');
var plugin, url;
@@ -165,7 +186,7 @@ $(function () {
if (themes[0] && (length = Object.keys(resources.themes).length)) {
content = jQuery.substitute(content, {updates: length, type: 'themes'});
button = jQuery.substitute(button, {Type: 'All Themes'});
button = jQuery.substitute(button, {Type: 'All Themes', location: GravAdmin.config.base_url_relative + '/themes'});
themes.html('<p>' + icon + content + button + '</p>');
var theme, url;
@@ -191,10 +212,12 @@ $(function () {
content = '<strong>v{available}</strong> of this ' + type + ' is now available!';
content = jQuery.substitute(content, {available: resources[type + 's'][slug].available});
button = jQuery.substitute(button, {Type: Type});
button = jQuery.substitute(button, {Type: Type, location: GravAdmin.config.base_url_relative + '/' + type + 's/' + slug});
$(details).html('<p>' + icon + content + button + '</p>');
}
}
}
});
};
GPMRefresh();
});

View File

@@ -47,7 +47,7 @@
</div>
</div>
<div class="flush-bottom button-bar">
<button href="#" class="button"><i class="fa fa-cloud-download"></i> Update</button>
<button data-maintenance-update="{{ base_url_relative }}/update.json/task:update" class="button"><i class="fa fa-cloud-download"></i> Update</button>
<button href="#" class="button"><i class="fa fa-database"></i> Backup</button>
</div>
</div>

View File

@@ -85,7 +85,7 @@
<div class="button-bar danger">
<span class="danger-zone"></span>
<a class="button" href="#"><i class="fa fa-fw fa-warning"></i>Remove Plugin</a>
<a class="button" href="{{ base_url_relative }}/plugins/{{ plugin.slug }}/task:uninstall"><i class="fa fa-fw fa-warning"></i>Remove Plugin</a>
</div>
{% else %}
<div class="button-bar success">

View File

@@ -1,5 +1,5 @@
{% set gpm = admin.gpm() %}
{% set installed = gpm.isPluginInstalled(admin.route) %}
{% set installed = gpm.isThemeInstalled(admin.route) %}
<div class="grav-update theme" data-gpm-theme="{{ admin.route }}">
{% if installed and gpm.isThemeUpdatable(admin.route) %}
@@ -90,7 +90,7 @@
<div class="button-bar danger">
<span class="danger-zone"></span>
<a class="button" href="#"><i class="fa fa-fw fa-warning"></i>Remove Theme</a>
<a class="button" href="{{ base_url_relative }}/themes/{{ theme.slug }}/task:uninstall"><i class="fa fa-fw fa-warning"></i>Remove Theme</a>
</div>
{% else %}
<div class="button-bar success">

View File

@@ -0,0 +1,4 @@
{% if admin.json_response %}
{% set output = {'status':admin.json_response[0], 'message':admin.json_response[1] } %}
{{ output|json_encode }}
{% endif %}