Commit edfda4bd authored by Marc Egger's avatar Marc Egger
Browse files

FormAsFile.php: add logging to qfq.log whenever file/database-record is deleted or overwritten

parent c17824b9
Pipeline #3979 failed with stages
in 3 minutes and 21 seconds
......@@ -1027,14 +1027,6 @@ class Database {
}
/**
* @return string
* @throws \UserFormException
*/
public function getQfqLogFile() {
return Path::absoluteQfqLogFile();
}
/**
* Selects in table 'Split' all records with tableName='$tableName' and xId='$xId'. Deletes all referenced files and records.
*
......
......@@ -170,8 +170,7 @@ class DatabaseUpdate {
FormAsFIle::exportAllForms($this->db, true);
}
$qfqLog = $this->db->getQfqLogFile();
Logger::logMessage(date('Y.m.d H:i:s ') . ": Updated from QFQ version '$old' to '$new'", $qfqLog);
Logger::logMessage(date('Y.m.d H:i:s ') . ": Updated from QFQ version '$old' to '$new'", Path::absoluteQfqLogFile());
// Finally write the latest version number.
$versionInfo[QFQ_VERSION_KEY] = $new;
......
......@@ -6,6 +6,7 @@ namespace IMATHUZH\Qfq\Core\Form;
use IMATHUZH\Qfq\Core\Database\Database;
use IMATHUZH\Qfq\Core\Exception\Thrower;
use IMATHUZH\Qfq\Core\Helper\HelperFile;
use IMATHUZH\Qfq\Core\Helper\Logger;
use IMATHUZH\Qfq\Core\Helper\OnArray;
use IMATHUZH\Qfq\Core\Helper\OnString;
use IMATHUZH\Qfq\Core\Helper\Path;
......@@ -39,12 +40,12 @@ class FormAsFile
ERROR_MESSAGE_TO_DEVELOPER => "Form definition file not found or no permission to read file: '$cwdToFormFile'"]),
ERROR_FORM_NOT_FOUND);
if(!file_exists($cwdToFormFile)) {
self::deleteFormDB($formName, $database);
self::deleteFormDB($formName, $database, "No corresponding form file found.");
throw $fileReadException;
}
$fileStatsNew = self::formFileStatsJson($cwdToFormFile);
if ($fileStatsNew === false) {
self::deleteFormDB($formName, $database);
self::deleteFormDB($formName, $database, "Failed to read form file stats.");
throw $fileReadException;
}
list($sql, $parameterArray) = SqlQuery::selectFormByName($formName, [F_ID, F_FILE_STATS]);
......@@ -57,7 +58,7 @@ class FormAsFile
// Read form file
$fileContents = file_get_contents($cwdToFormFile);
if ($fileContents === false) {
self::deleteFormDB($formName, $database);
self::deleteFormDB($formName, $database, "Failed to read form file.");
throw $fileReadException;
}
$formFromFile = json_decode($fileContents, true);
......@@ -101,7 +102,7 @@ class FormAsFile
$formIdNew = $database->sql($sqlFormInsert, ROW_REGULAR, $parameterArrayFormInsert);
// Delete stale formElements with the new form id (these should not exist, but better make sure)
self::deleteFormElementsDBWithFormId($formIdNew, $database);
self::deleteFormElementsDBWithFormId($formIdNew, $database, "Inserted new form with id $formIdNew.");
// Insert FormElements to DB and collect container ids
$containerIds = []; // array(container_name => id)
......@@ -129,7 +130,7 @@ class FormAsFile
// Delete old form if everything went well
if ($formIdOld !== null) {
self::deleteFormDBWithId($formIdOld, $database);
self::deleteFormDBWithId($formIdOld, $database, "New version of form $formName was imported successfully. New form id: $formIdNew");
}
// Replace temporary name of new form
......@@ -166,6 +167,7 @@ class FormAsFile
$pathFileName = self::formPathFileName($formName, $database);
self::backupFormFile($pathFileName);
HelperFile::file_put_contents($pathFileName, $formJson);
Logger::logMessage(date('Y.m.d H:i:s ') . ": Overwrote form file '$pathFileName'. Reason: Export new version of form from database.", Path::absoluteQfqLogFile());
// some column names where adjusted => import form
if ($adjustContainerName) {
......@@ -236,11 +238,12 @@ class FormAsFile
*
* @param $formName
* @param Database $database
* @param string $logMessageReason
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/
public static function deleteFormFile($formName, Database $database) // : void
public static function deleteFormFile($formName, Database $database, string $logMessageReason = '') // : void
{
self::enforceFormFileWritable($formName, $database);
$pathFileName = self::formPathFileName($formName, $database);
......@@ -253,6 +256,7 @@ class FormAsFile
ERROR_MESSAGE_TO_DEVELOPER => "Can't delete form file '$pathFileName'"]),
ERROR_IO_WRITE_FILE);
}
Logger::logMessage(date('Y.m.d H:i:s ') . ": Removed form file '$pathFileName'. Reason: $logMessageReason", Path::absoluteQfqLogFile());
}
}
......@@ -378,7 +382,7 @@ class FormAsFile
$formNamesDB = self::queryAllFormNames($database);
$formsToDelete = array_diff($formNamesDB, $formFileNames);
foreach ($formsToDelete as $formToDelete) {
self::deleteFormDB($formToDelete, $database);
self::deleteFormDB($formToDelete, $database, "No corresponding form file found.");
}
}
}
......@@ -403,7 +407,7 @@ class FormAsFile
$formFileNames = self::formFileNames($database);
$filesToDelete = array_diff($formFileNames, $formNamesDB);
foreach ($filesToDelete as $fileToDelete) {
self::deleteFormFile($fileToDelete, $database);
self::deleteFormFile($fileToDelete, $database, "Export all forms from database. No form with name '$fileToDelete' in database.");
}
}
}
......@@ -442,37 +446,42 @@ class FormAsFile
*
* @param string $formName
* @param Database $database
* @param string $logMessageReason
* @return void
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/
private static function deleteFormDB(string $formName, Database $database) // : void
private static function deleteFormDB(string $formName, Database $database, string $logMessageReason = '') // : void
{
list($sql, $parameterArray) = SqlQuery::selectFormByName($formName, [F_ID]);
$formFromDb = $database->sql($sql, ROW_EXPECT_0_1,
$parameterArray, "Multiple forms with the same name: '$formName'");
if (array_key_exists(F_ID, $formFromDb)) {
self::deleteFormDBWithId($formFromDb[F_ID], $database);
self::deleteFormDBWithId($formFromDb[F_ID], $database, $logMessageReason . " Form name '$formName'.");
}
}
/**
* Delete form with given id and its form elements from DB
*
* @param $formId
* @param int $formId
* @param Database $database
* @param string $logMessageReason
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/
private static function deleteFormDBWithId(int $formId, Database $database) // : void
private static function deleteFormDBWithId(int $formId, Database $database, string $logMessageReason = '') // : void
{
self::backupFormDb($formId, $database);
$F_ID = F_ID; // can't use constants in strings directly
$TABLE_NAME_FORM = TABLE_NAME_FORM; // can't use constants in strings directly
$database->sql("DELETE FROM `$TABLE_NAME_FORM` WHERE `$F_ID`=? LIMIT 1", ROW_REGULAR, [$formId]);
self::deleteFormElementsDBWithFormId($formId, $database);
$rowsAffected = $database->sql("DELETE FROM `$TABLE_NAME_FORM` WHERE `$F_ID`=? LIMIT 1", ROW_REGULAR, [$formId]);
if ($rowsAffected > 0) {
Logger::logMessage(date('Y.m.d H:i:s ') . ": Remove form with id $formId from database. Reason: $logMessageReason", Path::absoluteQfqLogFile());
}
self::deleteFormElementsDBWithFormId($formId, $database, 'Remove form. ' . $logMessageReason);
}
/**
......@@ -493,21 +502,24 @@ class FormAsFile
}
/**
* Delete form elements with given formId from DB
*
* @param $formId
* @param int $formId
* @param Database $database
* @param string $logMessageReason
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/
private static function deleteFormElementsDBWithFormId(int $formId, Database $database) // : void
private static function deleteFormElementsDBWithFormId(int $formId, Database $database, string $logMessageReason = '') // : void
{
$TABLE_NAME_FORM_ELEMENT = TABLE_NAME_FORM_ELEMENT; // can't use constants in strings directly
$FE_FORM_ID = FE_FORM_ID; // can't use constants in strings directly
$database->sql("DELETE FROM `$TABLE_NAME_FORM_ELEMENT` WHERE `$FE_FORM_ID`=?", ROW_REGULAR, [$formId]);
$affectedRows = $database->sql("DELETE FROM `$TABLE_NAME_FORM_ELEMENT` WHERE `$FE_FORM_ID`=?", ROW_REGULAR, [$formId]);
if ($affectedRows > 0) {
Logger::logMessage(date('Y.m.d H:i:s ') . ": Removed $affectedRows formElements with formId $formId from database. Reason: $logMessageReason", Path::absoluteQfqLogFile());
}
}
/**
......
......@@ -686,7 +686,7 @@ class QuickFormQuery {
if (TABLE_NAME_FORM_ELEMENT === ($this->formSpec[F_TABLE_NAME] ?? '')) {
FormAsFile::exportForm($formFileName, $this->dbArray[$this->dbIndexQfq]);
} else {
FormAsFile::deleteFormFile($formFileName, $this->dbArray[$this->dbIndexQfq]);
FormAsFile::deleteFormFile($formFileName, $this->dbArray[$this->dbIndexQfq], 'Form was deleted using form-editor.');
}
break;
}
......@@ -694,7 +694,7 @@ class QuickFormQuery {
// delete old form file if form name was changed
if (isset($formFileNameDelete)) {
FormAsFile::deleteFormFile($formFileNameDelete, $this->dbArray[$this->dbIndexQfq]);
FormAsFile::deleteFormFile($formFileNameDelete, $this->dbArray[$this->dbIndexQfq], "Form was renamed to: '$formFileName'.");
}
return $data;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment