From 3c22b954b49da1d4bbfb75860400984b77f5e2e9 Mon Sep 17 00:00:00 2001 From: Olle Haerstedt Date: Tue, 25 Sep 2018 14:36:36 +0200 Subject: [PATCH] Dev: Rename destdir to tempdir --- .../admin/PluginManagerController.php | 13 ----- .../ExtensionInstaller/ExtensionInstaller.php | 18 +++++-- .../ExtensionInstaller/FileFetcher.php | 12 ++++- .../FileFetcherUploadZip.php | 51 +++++++++++-------- .../ExtensionInstaller/PluginInstaller.php | 14 +++-- 5 files changed, 64 insertions(+), 44 deletions(-) diff --git a/application/controllers/admin/PluginManagerController.php b/application/controllers/admin/PluginManagerController.php index 30eb3e67a7b..fd31da42f10 100644 --- a/application/controllers/admin/PluginManagerController.php +++ b/application/controllers/admin/PluginManagerController.php @@ -448,19 +448,6 @@ public function uploadConfirm() $installer->abort(); $this->errorAndRedirect($ex->getMessage()); } - - /* - // NB: destdir is location of tmp/ folder. - - if (empty($destdir)) { - $this->errorAndRedirect(gT('Could not find plugin destination folder.')); - } - - // Clear destdir. - - $abortUrl = $this->getPluginManagerUrl('abortUploadedPlugin'); - - */ } /** diff --git a/application/libraries/ExtensionInstaller/ExtensionInstaller.php b/application/libraries/ExtensionInstaller/ExtensionInstaller.php index 4ccc0a22adf..209a4bf1f33 100644 --- a/application/libraries/ExtensionInstaller/ExtensionInstaller.php +++ b/application/libraries/ExtensionInstaller/ExtensionInstaller.php @@ -10,7 +10,7 @@ * - Read config.xml * - If config.xml is valid and the extension compatible with current version of LimeSurvey, then * -- Copy files to correct folder (depends on extension type) - * -- Install database row (depends on extension type) + * -- Insert database row (depends on extension type) * * @since 2018-09-24 * @author Olle Haerstedt @@ -18,6 +18,7 @@ abstract class ExtensionInstaller { /** + * Class responsible for fetching files from source. * @var FileFetcher */ public $fileFetcher; @@ -32,22 +33,31 @@ public function setFileFetcher(FileFetcher $fileFetcher) } /** - * @return array [boolean $result, string $errorMessage] + * Order the file fetcher to fetch files. + * @return void + * @throws Exception */ abstract public function fetchFiles(); /** - * @todo Should return wrapper class for XML. - * @return SimpleXMLElement + * Get the configuration from temp dir. + * Before an extension is installed, we need to read the config + * file. That's why the extension if fetched into a temp folder + * first. + * @return ExtensionConfig */ abstract public function getConfig(); /** + * Install extension, which includes moving files + * from temp dir to final dir, and creating the necessary + * database changes. * @return void */ abstract public function install(); /** + * Installation procedure was not completed, abort changes. * @return void */ abstract public function abort(); diff --git a/application/libraries/ExtensionInstaller/FileFetcher.php b/application/libraries/ExtensionInstaller/FileFetcher.php index 3bef5ebe040..714867b5111 100644 --- a/application/libraries/ExtensionInstaller/FileFetcher.php +++ b/application/libraries/ExtensionInstaller/FileFetcher.php @@ -21,6 +21,7 @@ abstract class FileFetcher * Set source for this file fetcher. * Can be ZIP file name, git repo URL, folder name, etc. * @param string $source + * @return void */ abstract public function setSource($source); @@ -31,13 +32,20 @@ abstract public function setSource($source); abstract public function fetch(); /** - * @todo Should return wrapper class for XML. - * @return SimpleXMLElement|null + * Move files from tmp/ folder to final destination. + * @param string $destdir + * @return void + */ + abstract public function move($destdir); + + /** + * @return ExtensionConfig * @throws Exception if config cannot be parsed. */ abstract public function getConfig(); /** + * Abort procedure, remove temporary files. * @return void */ abstract public function abort(); diff --git a/application/libraries/ExtensionInstaller/FileFetcherUploadZip.php b/application/libraries/ExtensionInstaller/FileFetcherUploadZip.php index 2922b3ed3a6..6cc20a11358 100644 --- a/application/libraries/ExtensionInstaller/FileFetcherUploadZip.php +++ b/application/libraries/ExtensionInstaller/FileFetcherUploadZip.php @@ -35,7 +35,14 @@ public function fetch() { $this->checkFileSizeError(); $this->checkZipBom(); - $this->extractZipFile($this->getDestdir()); + $this->extractZipFile($this->getTempdir()); + } + + /** + * @param string $destdir + */ + public function move($destdir) + { } /** @@ -44,12 +51,12 @@ public function fetch() */ public function getConfig() { - $destdir = $this->getDestdir(); - if (empty($destdir)) { + $tmpdir = $this->getTempdir(); + if (empty($tmpdir)) { throw new \Exception(gT('No destination folder, cannot read configuration file.')); } - $configFile = $destdir . '/config.xml'; + $configFile = $tmpdir . '/config.xml'; if (!file_exists($configFile)) { throw new \Exception(gT('Configuration file config.xml does not exist.')); @@ -80,39 +87,39 @@ public function setUnzipFilter($filterName) public function abort() { // Remove any files. - $destdir = $this->getDestdir(); - if ($destdir) { - rmdirr($destdir); + $tmpdir = $this->getTempdir(); + if ($tmpdir) { + rmdirr($tmpdir); } // Reset user state. - $this->clearDestdir(); + $this->clearTmpdir(); } /** - * Get tmp destdir for extension to unzip in. + * Get tmp tmpdir for extension to unzip in. * @return string */ - protected function getDestdir() + protected function getTempdir() { // NB: Since the installation procedure can span several page reloads, - // we save the destdir in the user session. - $destdir = App()->user->getState('filefetcheruploadzip_destdir'); - if (empty($destdir)) { + // we save the tmpdir in the user session. + $tmpdir = App()->user->getState('filefetcheruploadzip_tmpdir'); + if (empty($tmpdir)) { $tempdir = \Yii::app()->getConfig("tempdir"); - $destdir = createRandomTempDir($tempdir, 'install_'); - App()->user->setState('filefetcheruploadzip_destdir', $destdir); + $tmpdir = createRandomTempDir($tempdir, 'install_'); + App()->user->setState('filefetcheruploadzip_tmpdir', $tmpdir); } - return $destdir; + return $tmpdir; } /** - * Set user session destdir to null. + * Set user session tmpdir to null. * @return void */ - protected function clearDestdir() + protected function clearTmpdir() { - App()->user->setState('filefetcheruploadzip_destdir', null); + App()->user->setState('filefetcheruploadzip_tmpdir', null); } /** @@ -150,10 +157,10 @@ protected function checkZipBom() } /** - * @param string $destdir + * @param string $tmpdir * @return void */ - protected function extractZipFile($destdir) + protected function extractZipFile($tmpdir) { \Yii::import('application.helpers.common_helper', true); \Yii::app()->loadLibrary('admin.pclzip'); @@ -171,7 +178,7 @@ protected function extractZipFile($destdir) $zip = new \PclZip($_FILES['the_file']['tmp_name']); $aExtractResult = $zip->extract( PCLZIP_OPT_PATH, - $destdir, + $tmpdir, PCLZIP_CB_PRE_EXTRACT, $this->filterName ); diff --git a/application/libraries/ExtensionInstaller/PluginInstaller.php b/application/libraries/ExtensionInstaller/PluginInstaller.php index 912cdc9cd56..aef72a28274 100644 --- a/application/libraries/ExtensionInstaller/PluginInstaller.php +++ b/application/libraries/ExtensionInstaller/PluginInstaller.php @@ -19,18 +19,26 @@ class PluginInstaller extends ExtensionInstaller public function fetchFiles() { if (empty($this->fileFetcher)) { - throw new \InvalidArgumentException('fileFetcher is not set'); + throw new \InvalidArgumentException(gT('fileFetcher is not set')); } $this->fileFetcher->fetch(); } /** - * + * Install unzipped package into correct folder. + * @return void */ public function install() { - + if (empty($this->fileFetcher)) { + throw new \InvalidArgumentException(gT('fileFetcher is not set')); + } + + $this->fileFetcher->move($destdir); + + $pluginManager = App()->getPluginManager(); + list($result, $errorMessage) = $pluginManager->installUploadedPlugin($destdir); } /**