diff --git a/.gitignore b/.gitignore index 5d1a6776..155a5dbc 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ assets/atom/* assets/render/* assets/global/* +assets/manual/* assets/css/tagcolors.css assets/js/* database/* diff --git a/app/class/Controllerinfo.php b/app/class/Controllerinfo.php index 646c0ca6..15bbbf7e 100644 --- a/app/class/Controllerinfo.php +++ b/app/class/Controllerinfo.php @@ -21,16 +21,31 @@ public function __construct($render) public function desktop() { if ($this->user->isinvite()) { - if (file_exists(Model::MAN_FILE)) { - $render = new Servicerender($this->router, $this->pagemanager, true); - $htmlman = file_get_contents(Model::MAN_FILE); - $htmlman = $render->rendermanual($htmlman); + $version = getversion(); + $mandir = Model::MAN_RENDER_DIR; + try { + $manual = Fs::readfile("$mandir/manual_$version.html"); + $summary = Fs::readfile("$mandir/summary_$version.html"); + } catch (RuntimeException $e) { + try { + $mansrc = Fs::readfile(Model::MAN_FILE); + $render = new Servicerender($this->router, $this->pagemanager, true); + $manual = $render->rendermanual($mansrc); - $sum = new Summary(['min' => 2, 'max' => 4, 'sum' => $render->sum()]); - $summary = $sum->sumparser(); + $sum = new Summary(['min' => 2, 'max' => 4, 'sum' => $render->sum()]); + $summary = $sum->sumparser(); - $this->showtemplate('info', ['version' => getversion(), 'manual' => $htmlman, 'summary' => $summary]); + Fs::folderflush($mandir); + Fs::dircheck($mandir, true, 0775); + + Fs::writefile("$mandir/manual_$version.html", $manual); + Fs::writefile("$mandir/summary_$version.html", $summary); + } catch (RuntimeException $e) { + $manual = '⚠️ Error while trying to access MANUAL.md file.'; + $summary = ''; + } } + $this->showtemplate('info', ['version' => getversion(), 'manual' => $manual, 'summary' => $summary]); } } } diff --git a/app/class/Fs.php b/app/class/Fs.php index 786949e5..7ef9c573 100644 --- a/app/class/Fs.php +++ b/app/class/Fs.php @@ -3,6 +3,8 @@ namespace Wcms; use DomainException; +use LogicException; +use RuntimeException; /** * File system related tools. No Wcms specific param should be set here. @@ -119,6 +121,27 @@ public static function accessfile(string $path, bool $createdir = false): bool } } + /** + * Read file as string + * + * @param string $path the file path + * @return string the file as string + * + * @throws Notfoundexception if the diven path is not a file + * @throws Fileexception if an error reading the file occured + */ + public static function readfile(string $path): string + { + if (!is_file($path)) { + throw new Notfoundexception("The given path `$path` is not a file"); + } + $file = file_get_contents($path); + if ($file === false) { + throw new Fileexception("Error when reading file `$path`"); + } + return $file; + } + /** @@ -151,7 +174,7 @@ public static function recursecopy($src, $dst, $perm = Model::FOLDER_PERMISSION) * @param string $file Filename to delete * * @throws Notfoundexception If file does not exist - * @throws Fileexception Id file cannot be deleted + * @throws Fileexception If file cannot be deleted * @throws Unlinkexception If PHP unlink function fails for another reason */ public static function deletefile(string $file): void @@ -167,4 +190,29 @@ public static function deletefile(string $file): void throw new Unlinkexception($file); } } + + /** + * Delete all files in a given folder + * + * @throws Notfoundexception If folder does not exist + * @throws Fileexception If a file cannot be deleted + * @throws Unlinkexception If PHP unlink function fails for another reason + */ + public static function folderflush(string $path): void + { + $path = trim($path, '/'); + $files = glob("$path/*"); + if ($files === false) { + throw new Notfoundexception("Error while trying to scan directory $path"); + } + try { + foreach ($files as $file) { + self::deletefile($file); + } + } catch (Notfoundexception $e) { + throw new LogicException( + 'Problem with Fs::folderflush(), path given to Fs::deletefile() does not exist.' + ); + } + } } diff --git a/app/class/Model.php b/app/class/Model.php index ffe7cafe..1d41acc0 100644 --- a/app/class/Model.php +++ b/app/class/Model.php @@ -6,6 +6,7 @@ abstract class Model { public const CONFIG_FILE = 'config.json'; public const MAN_FILE = 'MANUAL.md'; + public const MAN_RENDER_DIR = 'assets/manual/'; public const ASSETS_CSS_DIR = 'assets/css/'; public const ASSETS_ATOM_DIR = 'assets/atom/'; public const COLORS_FILE = self::ASSETS_CSS_DIR . 'tagcolors.css'; diff --git a/app/fn/fn.php b/app/fn/fn.php index 05f27425..686930a2 100644 --- a/app/fn/fn.php +++ b/app/fn/fn.php @@ -88,7 +88,11 @@ function isreportingerrors() return function_exists('Sentry\init') && !empty(Wcms\Config::sentrydsn()); } - +/** + * Get W version using VERSION file in root directory. + * @return string W's current version. + * If the file cannot be read, `unknown` is used as output. + */ function getversion(): string { if (file_exists('VERSION')) {