From 0251e34f07f9a6f2e5545f00754c5db700997643 Mon Sep 17 00:00:00 2001 From: Marc Egger <marc.egger@uzh.ch> Date: Tue, 6 Oct 2020 13:39:23 +0200 Subject: [PATCH] Report As File: add auto backup whenever a sync happens --- extension/Classes/Core/Form/FormAsFile.php | 18 +++--- extension/Classes/Core/Helper/Path.php | 1 + .../Classes/Core/Report/ReportAsFile.php | 59 ++++++++++++++++++- 3 files changed, 66 insertions(+), 12 deletions(-) diff --git a/extension/Classes/Core/Form/FormAsFile.php b/extension/Classes/Core/Form/FormAsFile.php index f3015a02b..958af1bf1 100644 --- a/extension/Classes/Core/Form/FormAsFile.php +++ b/extension/Classes/Core/Form/FormAsFile.php @@ -143,25 +143,25 @@ class FormAsFile } /** - * Create copy of given form file in form/backup. If given file does not exist, do nothing. + * Create copy of given form file in form/_backup. If given file does not exist, do nothing. * New file name: <formName>.YYYMMDDhhmmss.file.json * - * @param string $pathFileName + * @param string $cwdToFormFile * @throws \UserFormException */ - private static function backupFormFile(string $pathFileName) + private static function backupFormFile(string $cwdToFormFile) { - if (file_exists($pathFileName)) + if (file_exists($cwdToFormFile)) { - if (!is_readable($pathFileName)) { - Thrower::userFormException('Error while trying to backup form file.', "Form file is not readable: $pathFileName"); + if (!is_readable($cwdToFormFile)) { + Thrower::userFormException('Error while trying to backup form file.', "Form file is not readable: $cwdToFormFile"); } // copy file - $cwdToBackupFile = self::newBackupPathFileName(basename($pathFileName, '.json'), 'file'); - $success = copy($pathFileName, $cwdToBackupFile); + $cwdToBackupFile = self::newBackupPathFileName(basename($cwdToFormFile, '.json'), 'file'); + $success = copy($cwdToFormFile, $cwdToBackupFile); if ($success === false) { - Thrower::userFormException('Error while trying to backup form file.', "Can't copy file $pathFileName to $cwdToBackupFile"); + Thrower::userFormException('Error while trying to backup form file.', "Can't copy file $cwdToFormFile to $cwdToBackupFile"); } } } diff --git a/extension/Classes/Core/Helper/Path.php b/extension/Classes/Core/Helper/Path.php index 979923030..39ef5693b 100644 --- a/extension/Classes/Core/Helper/Path.php +++ b/extension/Classes/Core/Helper/Path.php @@ -52,6 +52,7 @@ class Path const PROJECT_TO_FORM = 'form'; const FORM_TO_FORM_BACKUP = '_backup'; const PROJECT_DIR_TO_REPORT = 'report'; + const REPORT_FILE_TO_BACKUP = '_backup'; // The path from a directory containing a report file to the directory containing backups of that report file // Config const APP_TO_TYPO3_CONF = 'typo3conf'; diff --git a/extension/Classes/Core/Report/ReportAsFile.php b/extension/Classes/Core/Report/ReportAsFile.php index 130121d0f..4b9c44f17 100644 --- a/extension/Classes/Core/Report/ReportAsFile.php +++ b/extension/Classes/Core/Report/ReportAsFile.php @@ -3,6 +3,7 @@ namespace IMATHUZH\Qfq\Core\Report; use IMATHUZH\Qfq\Core\Database\Database; +use IMATHUZH\Qfq\Core\Exception\Thrower; use IMATHUZH\Qfq\Core\Helper\HelperFile; use IMATHUZH\Qfq\Core\Helper\Path; use IMATHUZH\Qfq\Core\Helper\Sanitize; @@ -158,8 +159,8 @@ class ReportAsFile $ttContent = $databaseT3->sql($sql, ROW_EXPECT_1, [$uid], "Typo3 content element not found. Uid: $uid"); - $reportPathFileNameFull = self::parseFileKeyword($ttContent['bodytext']); - if ($reportPathFileNameFull === null) { + $cwdToReportFile = self::parseFileKeyword($ttContent['bodytext']); + if ($cwdToReportFile === null) { throw new \UserReportException(json_encode([ ERROR_MESSAGE_TO_USER => "No report file defined.", ERROR_MESSAGE_TO_DEVELOPER => "The keyword '" . TOKEN_REPORT_FILE . "' is not present in the typo3 content element with id $uid"]), @@ -167,7 +168,59 @@ class ReportAsFile } // Update report file - HelperFile::file_put_contents($reportPathFileNameFull, $newContent); + self::backupReportFile($cwdToReportFile); + HelperFile::file_put_contents($cwdToReportFile, $newContent); + } + + /** + * Create copy of given report file in _backup directory. If given file does not exist, do nothing. + * New file name: <reportFileName>.YYYMMDDhhmmss.json + * + * @param string $cwdToReportFile + * @throws \UserFormException + */ + private static function backupReportFile(string $cwdToReportFile) + { + if (file_exists($cwdToReportFile)) + { + if (!is_readable($cwdToReportFile)) { + Thrower::userFormException('Error while trying to backup report file.', "Report file is not readable: $cwdToReportFile"); + } + + // copy file + $cwdToBackupFile = self::newBackupPathFileName($cwdToReportFile); + $success = copy($cwdToReportFile, $cwdToBackupFile); + if ($success === false) { + Thrower::userFormException('Error while trying to backup report file.', "Can't copy file $cwdToReportFile to $cwdToBackupFile"); + } + } + } + + /** + * Return the path to a (non-existing) report backup file with name: + * <reportFileName>.YYYMMDDhhmmss.<tag>.qfqr + * + * @param string $cwdToReportFile + * @return string + * @throws \UserFormException + */ + private static function newBackupPathFileName(string $cwdToReportFile): string + { + // create backup path if not exists + $cwdToBackup = Path::join(dirname($cwdToReportFile), Path::REPORT_FILE_TO_BACKUP); + if (!is_dir($cwdToBackup)) { + $success = mkdir($cwdToBackup, 0777, true); + if ($success === false) { + Thrower::userFormException('Error while trying to backup report file.', "Can't create backup path: $cwdToBackup"); + } + } + + // throw exception if backup file exists + $cwdToBackupFile = Path::join($cwdToBackup, basename($cwdToReportFile, REPORT_FILE_EXTENSION) . '.' . date('YmdHis') . ".json"); + if (file_exists($cwdToBackupFile)) { + Thrower::userFormException('Error while trying to backup report file.', "Backup file already exists: $cwdToBackupFile"); + } + return $cwdToBackupFile; } /** -- GitLab