Update Grav when pressing the "Update Grav Now" button

This commit is contained in:
Flavio Copes
2015-08-06 09:34:55 +02:00
parent 1945099227
commit 01c73d1451
4 changed files with 62 additions and 3 deletions

View File

@@ -682,6 +682,36 @@ class AdminController
return true;
}
/**
* Handles updating Grav
*
* @return bool True is the action was performed
*/
public function taskUpdategrav()
{
require_once __DIR__ . '/gpm.php';
//TODO: manage permissions
$result = \Grav\Plugin\Admin\Gpm::selfupgrade();
if ($result) {
$this->admin->json_response = ['status' => 'success', 'message' => 'Everything updated'];
} else {
$this->admin->json_response = ['status' => 'error', 'message' => 'Updates failed'];
}
if ($result) {
$this->admin->setMessage("Installation successful.", 'info');
} else {
$this->admin->setMessage("Installation failed.", 'error');
}
//$this->post = array('_redirect' => $this->view . '/' . $this->route);
return true;
}
/**
* Handles updating plugins and themes
*

View File

@@ -5,6 +5,7 @@ use Grav\Common\GravTrait;
use Grav\Common\GPM\GPM as GravGPM;
use Grav\Common\GPM\Installer;
use Grav\Common\GPM\Response;
use Grav\Common\GPM\Upgrader;
use Grav\Common\Filesystem\Folder;
use Grav\Common\GPM\Common\Package;
@@ -156,4 +157,32 @@ class Gpm
return $cache_dir . DS . $filename;
}
private static function _downloadSelfupgrade($package, $tmp)
{
$output = Response::get($package['download'], []);
Folder::mkdir($tmp);
file_put_contents($tmp . DS . $package['name'], $output);
return $tmp . DS . $package['name'];
}
public static function selfupgrade() {
$upgrader = new Upgrader();
$update = $upgrader->getAssets()['grav-update'];
$tmp = CACHE_DIR . 'tmp/Grav-' . uniqid();
$file = self::_downloadSelfupgrade($update, $tmp);
Installer::install($file, GRAV_ROOT,
['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => false]);
$errorCode = Installer::lastErrorCode();
Folder::delete($tmp);
if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) {
return false;
}
return true;
}
}

View File

@@ -112,7 +112,7 @@ $(function () {
});
// Update plugins/themes
$('[data-maintenance-update]').on('click', function(e) {
$(document).on('click', '[data-maintenance-update]', function(e) {
$(this).attr('disabled','disabled').find('> .fa').removeClass('fa-cloud-download').addClass('fa-refresh fa-spin');
var url = $(this).data('maintenanceUpdate');
@@ -223,7 +223,7 @@ $(function () {
if (grav.isUpdatable) {
var icon = '<i class="fa fa-bullhorn"></i> ';
content = 'Grav <b>v{available}</b> is now available! <span class="less">(Current: v{version})</span> ',
button = '<button class="button button-small secondary" data-gpm-update="grav">Update Grav Now</button>';
button = '<button data-maintenance-update="' + GravAdmin.config.base_url_relative + '/update.json/task:updategrav" class="button button-small secondary">Update Grav Now</button>';
content = jQuery.substitute(content, {available: grav.available, version: grav.version});
$('[data-gpm-grav]').addClass('grav').html('<p>' + icon + content + button + '</p>');

View File

@@ -77,7 +77,7 @@
<p>
<i class="fa fa-bullhorn"></i>
Grav <b>v{{ admin.gpm.grav.getVersion() }}</b> is now available! <span class="less">(Current: v{{ constant('GRAV_VERSION') }})</span>
<button class="button button-small secondary">Update Grav Now</button>
<button data-maintenance-update="{{ base_url_relative }}/update.json/task:update" class="button button-small secondary">Update Grav Now</button>
</p>
{% endif %}
</div>