From 01c73d14514924d2590a56ad76dfa334e5a2e499 Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Thu, 6 Aug 2015 09:34:55 +0200 Subject: [PATCH 1/7] Update Grav when pressing the "Update Grav Now" button --- classes/controller.php | 30 +++++++++++++++++++ classes/gpm.php | 29 ++++++++++++++++++ themes/grav/js/admin-all.js | 4 +-- themes/grav/templates/partials/base.html.twig | 2 +- 4 files changed, 62 insertions(+), 3 deletions(-) diff --git a/classes/controller.php b/classes/controller.php index ffe67242..8f63efc5 100644 --- a/classes/controller.php +++ b/classes/controller.php @@ -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 * diff --git a/classes/gpm.php b/classes/gpm.php index f83e559c..f64b36a3 100644 --- a/classes/gpm.php +++ b/classes/gpm.php @@ -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; + } } \ No newline at end of file diff --git a/themes/grav/js/admin-all.js b/themes/grav/js/admin-all.js index ad32bd3c..a513151f 100644 --- a/themes/grav/js/admin-all.js +++ b/themes/grav/js/admin-all.js @@ -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 = ' '; content = 'Grav v{available} is now available! (Current: v{version}) ', - button = ''; + button = ''; content = jQuery.substitute(content, {available: grav.available, version: grav.version}); $('[data-gpm-grav]').addClass('grav').html('

' + icon + content + button + '

'); diff --git a/themes/grav/templates/partials/base.html.twig b/themes/grav/templates/partials/base.html.twig index d79d5fa4..a8026bf9 100644 --- a/themes/grav/templates/partials/base.html.twig +++ b/themes/grav/templates/partials/base.html.twig @@ -77,7 +77,7 @@

Grav v{{ admin.gpm.grav.getVersion() }} is now available! (Current: v{{ constant('GRAV_VERSION') }}) - +

{% endif %} From f2fbd1a06e10a3626f69856462d72d060c95fa7d Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Thu, 6 Aug 2015 15:15:40 +0200 Subject: [PATCH 2/7] Remove unneeded code --- admin.php | 3 --- themes/grav/templates/partials/base.html.twig | 7 ------- 2 files changed, 10 deletions(-) diff --git a/admin.php b/admin.php index 5ce62c00..838c78f4 100644 --- a/admin.php +++ b/admin.php @@ -211,9 +211,6 @@ class AdminPlugin extends Plugin $twig->twig_vars['base_url'] = $twig->twig_vars['base_url_relative']; $twig->twig_vars['admin'] = $this->admin; - // fake grav update - $twig->twig_vars['grav_update'] = array('current' => '0.9.1', 'available' => '0.9.1'); - switch ($this->template) { case 'dashboard': $twig->twig_vars['popularity'] = $this->popularity; diff --git a/themes/grav/templates/partials/base.html.twig b/themes/grav/templates/partials/base.html.twig index a8026bf9..06633c8c 100644 --- a/themes/grav/templates/partials/base.html.twig +++ b/themes/grav/templates/partials/base.html.twig @@ -73,13 +73,6 @@
- {% if admin.gpm.grav.isUpdatable() %} -

- - Grav v{{ admin.gpm.grav.getVersion() }} is now available! (Current: v{{ constant('GRAV_VERSION') }}) - -

- {% endif %}
From ee3bde664a3a46690fd10a2744e3c0b402748a9e Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Thu, 6 Aug 2015 15:15:59 +0200 Subject: [PATCH 3/7] Add feedback during/after upgrade --- themes/grav/js/admin-all.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/themes/grav/js/admin-all.js b/themes/grav/js/admin-all.js index a513151f..687425e1 100644 --- a/themes/grav/js/admin-all.js +++ b/themes/grav/js/admin-all.js @@ -123,6 +123,10 @@ $(function () { toastErrors: true, success: function(result, status) { toastr.success(result.message); + + if (url.indexOf('task:updategrav') !== -1) { + $('[data-gpm-grav]').remove(); + } } }).always(function() { GPMRefresh(); @@ -223,12 +227,16 @@ $(function () { if (grav.isUpdatable) { var icon = ' '; content = 'Grav v{available} is now available! (Current: v{version}) ', - button = ''; + button = ''; content = jQuery.substitute(content, {available: grav.available, version: grav.version}); $('[data-gpm-grav]').addClass('grav').html('

' + icon + content + button + '

'); } + $('#grav-update-button').on('click', function() { + $(this).html('Updating... please wait, downloading 2MB+..'); + }); + // dashboard if ($('.updates-chart').length) { var missing = (resources.total + (grav.isUpdatable ? 1 : 0)) * 100 / (installed + (grav.isUpdatable ? 1 : 0)), From 473511d948636b649bc8896a22bd3ab6f507a0f9 Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Thu, 6 Aug 2015 15:16:22 +0200 Subject: [PATCH 4/7] Manage permissions --- classes/controller.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/classes/controller.php b/classes/controller.php index 8f63efc5..53fbf9f7 100644 --- a/classes/controller.php +++ b/classes/controller.php @@ -691,7 +691,9 @@ class AdminController { require_once __DIR__ . '/gpm.php'; - //TODO: manage permissions + if (!$this->authoriseTask('install grav', ['admin.super'])) { + return; + } $result = \Grav\Plugin\Admin\Gpm::selfupgrade(); From 66764b225518aa4066e6b3afd6229568591c2ca3 Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Thu, 6 Aug 2015 16:35:45 +0200 Subject: [PATCH 5/7] Show error message if Grav is symlinked, and stop the upgrade process --- classes/controller.php | 5 +++++ classes/gpm.php | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/classes/controller.php b/classes/controller.php index 53fbf9f7..97c1ce53 100644 --- a/classes/controller.php +++ b/classes/controller.php @@ -695,6 +695,11 @@ class AdminController return; } + if (is_link(ROOT_DIR . 'index.php')) { + $this->admin->json_response = ['status' => 'error', 'message' => 'Cannot upgrade: Grav is symlinked. Please upgrade manually']; + return false; + } + $result = \Grav\Plugin\Admin\Gpm::selfupgrade(); if ($result) { diff --git a/classes/gpm.php b/classes/gpm.php index f64b36a3..ab39cd0c 100644 --- a/classes/gpm.php +++ b/classes/gpm.php @@ -173,7 +173,7 @@ class Gpm $file = self::_downloadSelfupgrade($update, $tmp); Installer::install($file, GRAV_ROOT, - ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => false]); + ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true]); $errorCode = Installer::lastErrorCode(); @@ -185,4 +185,4 @@ class Gpm return true; } -} \ No newline at end of file +} From 7b705d35950265ad7cd6a758b666b5e0111656ec Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Thu, 6 Aug 2015 17:23:08 +0200 Subject: [PATCH 6/7] A better update success message. Update the version in the footer. --- classes/controller.php | 12 ++---------- themes/grav/js/admin-all.js | 13 ++++++++++--- themes/grav/templates/partials/base.html.twig | 2 +- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/classes/controller.php b/classes/controller.php index 97c1ce53..4a30cc68 100644 --- a/classes/controller.php +++ b/classes/controller.php @@ -703,19 +703,11 @@ class AdminController $result = \Grav\Plugin\Admin\Gpm::selfupgrade(); if ($result) { - $this->admin->json_response = ['status' => 'success', 'message' => 'Everything updated']; + $this->admin->json_response = ['status' => 'success', 'message' => 'Grav was successfully updated to ']; } else { - $this->admin->json_response = ['status' => 'error', 'message' => 'Updates failed']; + $this->admin->json_response = ['status' => 'error', 'message' => 'Grav update 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; } diff --git a/themes/grav/js/admin-all.js b/themes/grav/js/admin-all.js index 687425e1..7618c731 100644 --- a/themes/grav/js/admin-all.js +++ b/themes/grav/js/admin-all.js @@ -122,10 +122,16 @@ $(function () { url: url, toastErrors: true, success: function(result, status) { - toastr.success(result.message); - if (url.indexOf('task:updategrav') !== -1) { - $('[data-gpm-grav]').remove(); + if (result.status == 'success') { + $('[data-gpm-grav]').remove(); + toastr.success(result.message + window.grav_available_version); + $('#footer .grav-version').html(window.grav_available_version); + } else { + toastr.success(result.message); + } + } else { + toastr.success(result.message); } } }).always(function() { @@ -231,6 +237,7 @@ $(function () { content = jQuery.substitute(content, {available: grav.available, version: grav.version}); $('[data-gpm-grav]').addClass('grav').html('

' + icon + content + button + '

'); + window.grav_available_version = grav.available; } $('#grav-update-button').on('click', function() { diff --git a/themes/grav/templates/partials/base.html.twig b/themes/grav/templates/partials/base.html.twig index 06633c8c..c5ccd4a8 100644 --- a/themes/grav/templates/partials/base.html.twig +++ b/themes/grav/templates/partials/base.html.twig @@ -90,7 +90,7 @@ {% block content_bottom %}{% endblock %}
- Grav version {{ constant('GRAV_VERSION') }} was made with by RocketTheme. + Grav version {{ constant('GRAV_VERSION') }} was made with by RocketTheme.
From 93a315632a4bc3bd4e478a38377c7bfacdef104a Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Thu, 6 Aug 2015 17:56:45 +0200 Subject: [PATCH 7/7] Fix #12 link to rockettheme.com, thanks @symac --- themes/grav/templates/partials/base.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/grav/templates/partials/base.html.twig b/themes/grav/templates/partials/base.html.twig index c5ccd4a8..cf3ddfe1 100644 --- a/themes/grav/templates/partials/base.html.twig +++ b/themes/grav/templates/partials/base.html.twig @@ -90,7 +90,7 @@ {% block content_bottom %}{% endblock %}