Skip to content

Commit

Permalink
refactor(getDataPath): create a method in Wiki
Browse files Browse the repository at this point in the history
  • Loading branch information
J9rem committed Sep 15, 2023
1 parent 83ec534 commit 3ca1c5e
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 44 deletions.
25 changes: 20 additions & 5 deletions includes/YesWiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand Down
8 changes: 3 additions & 5 deletions includes/services/TemplateEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __construct(
}
}
}

// Core templates
$corePaths = [];
$corePaths[] = 'custom/templates/core/';
Expand All @@ -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
]);

Expand Down Expand Up @@ -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');
}
Expand Down
12 changes: 2 additions & 10 deletions tools/attach/libs/attach.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions tools/autoupdate/app/AutoUpdate.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

namespace AutoUpdate;

use YesWiki\Core\Service\ConfigurationService;

class AutoUpdate
{
const DEFAULT_REPO = 'https://repository.yeswiki.net/';
const DEFAULT_VERS = 'Cercopitheque'; // Pour gérer les vielles version de
public const DEFAULT_REPO = 'https://repository.yeswiki.net/';
public const DEFAULT_VERS = 'Cercopitheque'; // Pour gérer les vielles version de
// YesWiki
private $wiki;
public $repository = null;
Expand All @@ -25,7 +26,7 @@ public function __construct($wiki)
*/
public function initRepository($requestedVersion='')
{
$this->repository = new Repository($this->repositoryAddress($requestedVersion));
$this->repository = new Repository($this->repositoryAddress($requestedVersion), $this->wiki->getDataPath());
return $this->repository->load();
}

Expand Down
14 changes: 9 additions & 5 deletions tools/autoupdate/app/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion tools/autoupdate/app/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions tools/autoupdate/app/PackageCollection.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<?php

namespace AutoUpdate;

class PackageCollection extends Collection
{
const THEME_CLASS = 'AutoUpdate\PackageTheme';
const TOOL_CLASS = 'AutoUpdate\PackageTool';
const CORE_CLASS = 'AutoUpdate\PackageCore';
public const THEME_CLASS = 'AutoUpdate\PackageTheme';
public const TOOL_CLASS = 'AutoUpdate\PackageTool';
public const CORE_CLASS = 'AutoUpdate\PackageCore';

public function add($release, $address, $file, $description, $documentation, $minimalPhpVersion = null)
public function add($release, $address, $file, $description, $documentation, $minimalPhpVersion = null, string $dataPath = '')
{
$className = $this->getPackageType($file);
$package = new $className(
$release,
$address . $file,
$description,
$documentation,
$minimalPhpVersion
$minimalPhpVersion,
$dataPath
);
$this->list[$package->name] = $package;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/autoupdate/app/PackageCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions tools/autoupdate/app/PackageExt.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<?php

namespace AutoUpdate;

abstract class PackageExt extends Package
{
const INFOS_FILENAME = "infos.json";
public const INFOS_FILENAME = "infos.json";

protected $infos = null;

public $deleteLink;

abstract protected function localPath();

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 = $this->installed();
$this->localPath = $this->localPath();
$this->updateAvailable = $this->updateAvailable();
Expand Down Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions tools/autoupdate/app/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
);
}

Expand Down
2 changes: 1 addition & 1 deletion tools/bazar/services/ExternalBazarService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3ca1c5e

Please sign in to comment.