From 3ca1c5e2eb214d34e8ec2ebe8cbd3ee827d6b327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Dufraisse?= Date: Tue, 6 Dec 2022 12:06:01 +0100 Subject: [PATCH] refactor(getDataPath): create a method in Wiki --- includes/YesWiki.php | 25 +++++++++++++++---- includes/services/TemplateEngine.php | 8 +++--- tools/attach/libs/attach.lib.php | 12 ++------- tools/autoupdate/app/AutoUpdate.php | 7 +++--- tools/autoupdate/app/Files.php | 14 +++++++---- tools/autoupdate/app/Package.php | 3 ++- tools/autoupdate/app/PackageCollection.php | 12 +++++---- tools/autoupdate/app/PackageCore.php | 4 +-- tools/autoupdate/app/PackageExt.php | 9 ++++--- tools/autoupdate/app/Repository.php | 10 +++++--- tools/bazar/services/ExternalBazarService.php | 2 +- 11 files changed, 62 insertions(+), 44 deletions(-) diff --git a/includes/YesWiki.php b/includes/YesWiki.php index 76df72653..105ab9ad1 100755 --- a/includes/YesWiki.php +++ b/includes/YesWiki.php @@ -15,6 +15,7 @@ use Throwable; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -393,8 +394,8 @@ public function getBaseUrl($useDataPath = false) $url = explode('wakka.php', $this->config['base_url']); $url = explode('index.php', $url[0]); $url = preg_replace(array('/\/\?$/', '/\/$/'), '', $url[0]); - if ($useDataPath && !empty($this->config['dataPath']) ) { - // we add an imaginary folder in order to retrieve yeswiki assets from yeswiki's source folder. web servers need to be configured to redirect yeswiki-assets to the main yeswiki folder + if ($useDataPath && !empty($this->getDataPath())) { + // we add an imaginary folder in order to retrieve yeswiki assets from yeswiki's source folder. web servers need to be configured to redirect yeswiki-assets to the main yeswiki folder $url .= '/yeswiki-assets'; } return $url; @@ -403,13 +404,27 @@ public function getBaseUrl($useDataPath = false) public function getLocalPath($folder = '') { $dataFolders = ['', 'cache', 'files', 'custom']; - if (in_array($folder, $dataFolders) && !empty($this->config['dataPath']) ) { - // we add an imaginary folder in order to retrieve yeswiki assets from yeswiki's source folder. web servers need to be configured to redirect yeswiki-assets to the main yeswiki folder - $folder = $this->config['dataPath'].'/'.$folder ; + $dataPath = $this->getDataPath(); + if (in_array($folder, $dataFolders) && !empty($dataPath)) { + // we add an imaginary folder in order to retrieve yeswiki assets from yeswiki's source folder. web servers need to be configured to redirect yeswiki-assets to the main yeswiki folder + $folder = "$dataPath/$folder" ; } return $folder; } + public function getDataPath(): string + { + $params = $this->services->get(ParameterBagInterface::class); + if (!$params->has('dataPath')) { + return ''; + } + $dataPath = $params->get('dataPath'); + if (empty($dataPath)) { + return ''; + } + return "$dataPath/"; + } + public function Redirect($url) { header("Location: $url"); diff --git a/includes/services/TemplateEngine.php b/includes/services/TemplateEngine.php index 119ebdac8..8bd66d339 100644 --- a/includes/services/TemplateEngine.php +++ b/includes/services/TemplateEngine.php @@ -66,7 +66,7 @@ public function __construct( } } } - + // Core templates $corePaths = []; $corePaths[] = 'custom/templates/core/'; @@ -81,11 +81,9 @@ public function __construct( } } - $dataPath = (!empty($this->wiki->config['dataPath'])) ? $this->wiki->config['dataPath'].'/' : ''; - // Set up twig $this->twig = new \Twig\Environment($this->twigLoader, [ - 'cache' => $dataPath.'cache/templates/', + 'cache' => $this->wiki->getDataPath().'cache/templates/', 'auto_reload' => true ]); @@ -151,7 +149,7 @@ public function __construct( throw new Exception("`urlImage` should be called with `height` key in params!"); } $options = array_merge(['mode' => 'fit','refresh'=>false], $options); - + if (!class_exists('attach')) { include('tools/attach/libs/attach.lib.php'); } diff --git a/tools/attach/libs/attach.lib.php b/tools/attach/libs/attach.lib.php index 717947330..6a47ee385 100644 --- a/tools/attach/libs/attach.lib.php +++ b/tools/attach/libs/attach.lib.php @@ -144,11 +144,7 @@ public function GetUploadPath() $this->mkdir_recursif($path); } } - if (!empty($this->wiki->config['dataPath'])) { - return $this->wiki->config['dataPath'].'/'.$path; - } else { - return $path; - } + return "{$this->wiki->getDataPath()}$path"; } /** * Calcul le repertoire de cache en fonction du safe_mode @@ -163,11 +159,7 @@ public function GetCachePath() $this->mkdir_recursif($path); } } - if (!empty($this->wiki->config['dataPath'])) { - return $this->wiki->config['dataPath'].'/'.$path; - } else { - return $path; - } + return "{$this->wiki->getDataPath()}$path"; } /** * Calcule le nom complet du fichier attaché en fonction du safe_mode, du nom et de la date de diff --git a/tools/autoupdate/app/AutoUpdate.php b/tools/autoupdate/app/AutoUpdate.php index 40224b48b..0b117f749 100644 --- a/tools/autoupdate/app/AutoUpdate.php +++ b/tools/autoupdate/app/AutoUpdate.php @@ -1,12 +1,13 @@ repository = new Repository($this->repositoryAddress($requestedVersion)); + $this->repository = new Repository($this->repositoryAddress($requestedVersion), $this->wiki->getDataPath()); return $this->repository->load(); } diff --git a/tools/autoupdate/app/Files.php b/tools/autoupdate/app/Files.php index 4ffd63a25..e7bf6d370 100644 --- a/tools/autoupdate/app/Files.php +++ b/tools/autoupdate/app/Files.php @@ -4,11 +4,16 @@ class Files { + protected $cachePath; + + public function __construct(string $dataPath = '') + { + $this->cachePath = "{$dataPath}cache"; + } + protected function tmpdir() { - $cachePath = (!empty($this->wiki->config['dataPath'])) ? $this->wiki->config['dataPath'].'/cache' : 'cache'; - - $path = tempnam(realpath($cachePath), 'yeswiki_'); + $path = tempnam(realpath($this->cachePath), 'yeswiki_'); if (is_file($path)) { unlink($path); @@ -74,8 +79,7 @@ protected function isWritable($path) public function download($sourceUrl, $destPath = null, $timeoutInSec = 5) { if ($destPath === null) { - $cachePath = (!empty($this->wiki->config['dataPath'])) ? $this->wiki->config['dataPath'].'/cache' : 'cache'; - $destPath = tempnam($cachePath, 'tmp_to_delete_'); + $destPath = tempnam($this->cachePath, 'tmp_to_delete_'); } $fp = fopen($destPath, 'wb'); $ch = curl_init($sourceUrl); diff --git a/tools/autoupdate/app/Package.php b/tools/autoupdate/app/Package.php index da23e837e..197e7bd35 100644 --- a/tools/autoupdate/app/Package.php +++ b/tools/autoupdate/app/Package.php @@ -34,8 +34,9 @@ abstract protected function localRelease(); protected $localPath; - public function __construct($release, $address, $desc, $doc, $minimalPhpVersion = null) + public function __construct($release, $address, $desc, $doc, $minimalPhpVersion = null, string $dataPath = '') { + parent::__construct($dataPath); $this->release = $release; $this->address = $address; $this->description = $desc; diff --git a/tools/autoupdate/app/PackageCollection.php b/tools/autoupdate/app/PackageCollection.php index 6d246af73..ce9d9cb52 100644 --- a/tools/autoupdate/app/PackageCollection.php +++ b/tools/autoupdate/app/PackageCollection.php @@ -1,13 +1,14 @@ getPackageType($file); $package = new $className( @@ -15,7 +16,8 @@ public function add($release, $address, $file, $description, $documentation, $mi $address . $file, $description, $documentation, - $minimalPhpVersion + $minimalPhpVersion, + $dataPath ); $this->list[$package->name] = $package; } diff --git a/tools/autoupdate/app/PackageCore.php b/tools/autoupdate/app/PackageCore.php index e3578b33b..b5da49561 100644 --- a/tools/autoupdate/app/PackageCore.php +++ b/tools/autoupdate/app/PackageCore.php @@ -37,9 +37,9 @@ class PackageCore extends Package 'files/PageHeader_bandeau_20200101000000_29991231000000.png', ]; - public function __construct($release, $address, $desc, $doc, $minimalPhpVersion = null) + public function __construct($release, $address, $desc, $doc, $minimalPhpVersion = null, string $dataPath = '') { - parent::__construct($release, $address, $desc, $doc, $minimalPhpVersion); + parent::__construct($release, $address, $desc, $doc, $minimalPhpVersion, $dataPath); $this->installed = true; $this->localPath = realpath(dirname($_SERVER["SCRIPT_FILENAME"])); $this->name = $this::CORE_NAME; diff --git a/tools/autoupdate/app/PackageExt.php b/tools/autoupdate/app/PackageExt.php index 97ce60e7a..bf9f75b1e 100644 --- a/tools/autoupdate/app/PackageExt.php +++ b/tools/autoupdate/app/PackageExt.php @@ -1,9 +1,10 @@ installed = $this->installed(); $this->localPath = $this->localPath(); $this->updateAvailable = $this->updateAvailable(); @@ -46,7 +47,7 @@ public function upgrade() // get the first subfolder extracted from the zip (it contains everything) $dirs = array_filter(glob($this->extractionPath.'/*'), 'is_dir'); $extractionPath = $dirs[0].'/'; - + $this->copy( $extractionPath, $desPath diff --git a/tools/autoupdate/app/Repository.php b/tools/autoupdate/app/Repository.php index 1cc3ef9fc..748e79aae 100644 --- a/tools/autoupdate/app/Repository.php +++ b/tools/autoupdate/app/Repository.php @@ -9,10 +9,13 @@ class Repository extends PackageCollection private $address; private $fileHandler; - public function __construct($address) + protected $dataPath; + + public function __construct($address, string $dataPath = '') { + $this->dataPath = $dataPath; $this->address = $address . '/'; - $this->fileHandler = new \AutoUpdate\Files(); + $this->fileHandler = new \AutoUpdate\Files($dataPath); } public function load() @@ -43,7 +46,8 @@ public function load() $packageInfos['file'], $packageInfos['description'], $packageInfos['documentation'], - $packageInfos['minimal_php_version'] ?? null + $packageInfos['minimal_php_version'] ?? null, + $this->dataPath ); } diff --git a/tools/bazar/services/ExternalBazarService.php b/tools/bazar/services/ExternalBazarService.php index 296224f22..050bf2fce 100644 --- a/tools/bazar/services/ExternalBazarService.php +++ b/tools/bazar/services/ExternalBazarService.php @@ -75,7 +75,7 @@ public function __construct( ImportService $importService ) { $this->wiki = $wiki; - $this->cachePath = (!empty($this->wiki->config['dataPath'])) ? $this->wiki->config['dataPath'].'/cache' : 'cache'; + $this->cachePath = $this->wiki->getLocalPath('cache'); $this->params = $params; $this->formManager = $formManager; $this->importService = $importService;