From 88e8df278f3639e48a77567866ec3a09f5b0cfce Mon Sep 17 00:00:00 2001 From: digitalpenguin Date: Fri, 15 Oct 2021 13:50:15 +0800 Subject: [PATCH 1/5] Detects an upgrade and moves configurable directories to the paths specified in config.inc.php --- src/BaseCommand.php | 1 + src/Command/UpgradeModxCommand.php | 1 + src/Mixins/DownloadModx.php | 30 ++++++++++++++++++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/BaseCommand.php b/src/BaseCommand.php index d2312d0..74631c3 100644 --- a/src/BaseCommand.php +++ b/src/BaseCommand.php @@ -26,6 +26,7 @@ abstract class BaseCommand extends Command public $loadConfig = true; public $loadMODX = true; + public $isUpgrade = false; /** * Initializes the command just after the input has been validated. diff --git a/src/Command/UpgradeModxCommand.php b/src/Command/UpgradeModxCommand.php index 69c7eae..c2a8788 100644 --- a/src/Command/UpgradeModxCommand.php +++ b/src/Command/UpgradeModxCommand.php @@ -19,6 +19,7 @@ class UpgradeModxCommand extends BaseCommand public $loadConfig = false; public $loadModx = true; + public $isUpgrade = true; protected function configure() { diff --git a/src/Mixins/DownloadModx.php b/src/Mixins/DownloadModx.php index 05df46a..952e0f3 100644 --- a/src/Mixins/DownloadModx.php +++ b/src/Mixins/DownloadModx.php @@ -73,6 +73,7 @@ protected function download($version) * Unzips the package zip to the current directory * * @param $package + * @throws \Exception */ protected function unzip($package) { @@ -131,14 +132,39 @@ protected function retrieveFromCache($version) { $version = 'modx-' . str_replace('modx-', '', $version); - $path = GITIFY_CACHE_DIR . $version; if (!file_exists($path) || !is_dir($path)) { $this->download($version); } - exec("cp -r $path/* ./"); + // Handle potential custom paths on upgrade + if ($this->isUpgrade) { + require_once './config.core.php'; + if (MODX_CORE_PATH) { + require_once MODX_CORE_PATH . 'config/config.inc.php'; + // Need to avoid the easier route with rsync as it may not be installed + $paths = [ + 'core' => MODX_CORE_PATH, + 'connectors' => MODX_CONNECTORS_PATH, + 'manager' => MODX_MANAGER_PATH, + 'assets' => MODX_ASSETS_PATH + ]; + foreach ($paths as $k => $customPath) { + // Copy each dir to path specified in config file then remove that dir from source + exec("cp -r $path/$k/* $customPath"); + exec("rm -rf $path/$k"); + } + // Now copy remaining contents + exec("cp -r $path/* ./"); + + // Re-extract package to source dir, so it's ready for another run. + $this->unzip($path . '.zip'); + } + } + else { + exec("cp -r $path/* ./"); + } } /** From bfab5860d82c19736f9629c48ba694497518f73c Mon Sep 17 00:00:00 2001 From: digitalpenguin Date: Fri, 15 Oct 2021 14:33:34 +0800 Subject: [PATCH 2/5] Don't overwrite config.core.php files. --- src/Mixins/DownloadModx.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Mixins/DownloadModx.php b/src/Mixins/DownloadModx.php index 952e0f3..4f0135c 100644 --- a/src/Mixins/DownloadModx.php +++ b/src/Mixins/DownloadModx.php @@ -109,7 +109,7 @@ protected function fetchUrl($version) $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); - curl_setopt($ch, CURLOPT_TIMEOUT, 10); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); curl_exec($ch); @@ -147,14 +147,20 @@ protected function retrieveFromCache($version) $paths = [ 'core' => MODX_CORE_PATH, 'connectors' => MODX_CONNECTORS_PATH, - 'manager' => MODX_MANAGER_PATH, - 'assets' => MODX_ASSETS_PATH + 'manager' => MODX_MANAGER_PATH ]; foreach ($paths as $k => $customPath) { + // Throw out default config files + if (file_exists("$path/$k/config.core.php")) { + unlink("$path/$k/config.core.php"); + } // Copy each dir to path specified in config file then remove that dir from source exec("cp -r $path/$k/* $customPath"); exec("rm -rf $path/$k"); } + + unlink("$path/config.core.php"); + // Now copy remaining contents exec("cp -r $path/* ./"); From 453361027a1bdfc83653272e8788aaa90eacda30 Mon Sep 17 00:00:00 2001 From: digitalpenguin Date: Fri, 15 Oct 2021 14:40:35 +0800 Subject: [PATCH 3/5] Add a check to remove setup directory if it remains after upgrade. --- src/Command/UpgradeModxCommand.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Command/UpgradeModxCommand.php b/src/Command/UpgradeModxCommand.php index c2a8788..ed67454 100644 --- a/src/Command/UpgradeModxCommand.php +++ b/src/Command/UpgradeModxCommand.php @@ -66,6 +66,11 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln("Warning:: could not clean up the setup config file, please remove this manually."); } + // Remove setup directory if upgrade process left it there. + if (is_dir("{$wd}setup/")) { + exec("rm -rf {$wd}setup/"); + } + $output->writeln('Done! ' . $this->getRunStats()); return 0; From 3cdf26fdcb1a90d13d6ae924a8266e6141b50342 Mon Sep 17 00:00:00 2001 From: digitalpenguin Date: Fri, 15 Oct 2021 14:55:28 +0800 Subject: [PATCH 4/5] Wipe cache on upgrade --- src/Mixins/DownloadModx.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Mixins/DownloadModx.php b/src/Mixins/DownloadModx.php index 4f0135c..fce4c19 100644 --- a/src/Mixins/DownloadModx.php +++ b/src/Mixins/DownloadModx.php @@ -164,6 +164,11 @@ protected function retrieveFromCache($version) // Now copy remaining contents exec("cp -r $path/* ./"); + // Hard wipe cache + if (is_dir(MODX_CORE_PATH . 'cache/')) { + exec('rm -rf ' . MODX_CORE_PATH . 'cache/*'); + } + // Re-extract package to source dir, so it's ready for another run. $this->unzip($path . '.zip'); } From 07e7fd0a2b011d6b97b5e83cea9a9478566114c3 Mon Sep 17 00:00:00 2001 From: digitalpenguin Date: Fri, 15 Oct 2021 14:58:01 +0800 Subject: [PATCH 5/5] Prevent doctype being displayed on successful upgrade --- src/Command/UpgradeModxCommand.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Command/UpgradeModxCommand.php b/src/Command/UpgradeModxCommand.php index ed67454..186ac33 100644 --- a/src/Command/UpgradeModxCommand.php +++ b/src/Command/UpgradeModxCommand.php @@ -59,7 +59,6 @@ protected function execute(InputInterface $input, OutputInterface $output) // Actually run the CLI setup exec("php -d date.timezone={$tz} {$wd}setup/index.php --installmode=upgrade --config={$config}", $setupOutput); - $output->writeln($setupOutput[0]); // Try to clean up the config file if (!unlink($config)) {