Skip to content

Commit

Permalink
Dev: Rename destdir to tempdir
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Sep 25, 2018
1 parent 2d39813 commit 3c22b95
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 44 deletions.
13 changes: 0 additions & 13 deletions application/controllers/admin/PluginManagerController.php
Expand Up @@ -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');
*/
}

/**
Expand Down
18 changes: 14 additions & 4 deletions application/libraries/ExtensionInstaller/ExtensionInstaller.php
Expand Up @@ -10,14 +10,15 @@
* - 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
*/
abstract class ExtensionInstaller
{
/**
* Class responsible for fetching files from source.
* @var FileFetcher
*/
public $fileFetcher;
Expand All @@ -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();
Expand Down
12 changes: 10 additions & 2 deletions application/libraries/ExtensionInstaller/FileFetcher.php
Expand Up @@ -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);

Expand All @@ -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();
Expand Down
51 changes: 29 additions & 22 deletions application/libraries/ExtensionInstaller/FileFetcherUploadZip.php
Expand Up @@ -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)
{
}

/**
Expand All @@ -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.'));
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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');
Expand All @@ -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
);
Expand Down
14 changes: 11 additions & 3 deletions application/libraries/ExtensionInstaller/PluginInstaller.php
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 3c22b95

Please sign in to comment.