From 4b6554b870a5df12bced7aa4decd2cb3c913992c Mon Sep 17 00:00:00 2001 From: Roman Date: Sat, 4 May 2024 15:07:24 +0200 Subject: [PATCH] adjusted database and install scripts --- Core/Configuration/CreateDatabase.class.php | 39 +++++------- Core/Configuration/DatabaseScript.class.php | 9 --- .../Patch/2021_04_08-EntityLog.php | 47 ++++++++++++++ .../Patch/EntityLog_2021_04_08.class.php | 63 ------------------- Core/Documents/Install.class.php | 6 +- cli.php | 39 +++++------- docker-compose.yml | 1 - js/install.js | 14 ++--- 8 files changed, 88 insertions(+), 130 deletions(-) delete mode 100644 Core/Configuration/DatabaseScript.class.php create mode 100644 Core/Configuration/Patch/2021_04_08-EntityLog.php delete mode 100644 Core/Configuration/Patch/EntityLog_2021_04_08.class.php diff --git a/Core/Configuration/CreateDatabase.class.php b/Core/Configuration/CreateDatabase.class.php index 3dfb342..fdd0f33 100644 --- a/Core/Configuration/CreateDatabase.class.php +++ b/Core/Configuration/CreateDatabase.class.php @@ -7,12 +7,12 @@ use Core\Driver\SQL\SQL; use Core\Objects\DatabaseEntity\Controller\DatabaseEntity; use PHPUnit\Util\Exception; -class CreateDatabase extends DatabaseScript { +class CreateDatabase { public static function createQueries(SQL $sql): array { $queries = array(); - self::loadEntities($queries, $sql); + self::loadEntities($sql, $queries); $queries[] = $sql->createTable("Settings") ->addString("name", 32) @@ -33,34 +33,25 @@ class CreateDatabase extends DatabaseScript { ->primaryKey("method") ->addBool("is_core", false); - self::loadDefaultACL($queries, $sql); - self::loadPatches($queries, $sql); + self::loadDefaultACL($sql, $queries); + self::loadPatches($sql, $queries); return $queries; } - private static function loadPatches(&$queries, $sql) { - $baseDirs = ["Core", "Site"]; - foreach ($baseDirs as $baseDir) { - $patchDirectory = "./$baseDir/Configuration/Patch/"; - if (file_exists($patchDirectory) && is_dir($patchDirectory)) { - $scan_arr = scandir($patchDirectory); - $files_arr = array_diff($scan_arr, array('.', '..')); - foreach ($files_arr as $file) { - $suffix = ".class.php"; - if (endsWith($file, $suffix)) { - $className = substr($file, 0, strlen($file) - strlen($suffix)); - $className = "\\$baseDir\\Configuration\\Patch\\$className"; - $method = "$className::createQueries"; - $patchQueries = call_user_func($method, $sql); - foreach ($patchQueries as $query) $queries[] = $query; - } - } - } + private static function loadPatches(SQL $sql, array &$queries): void { + $patchFiles = array_merge( + glob('Core/Configuration/Patch/*.php'), + glob('Site/Configuration/Patch/*.php') + ); + + sort($patchFiles); + foreach ($patchFiles as $file) { + @include_once $file; } } - private static function loadEntities(&$queries, $sql) { + private static function loadEntities(SQL $sql, array &$queries): void { $persistables = []; $baseDirs = ["Core", "Site"]; foreach ($baseDirs as $baseDir) { @@ -113,7 +104,7 @@ class CreateDatabase extends DatabaseScript { } } - public static function loadDefaultACL(array &$queries, SQL $sql): void { + public static function loadDefaultACL(SQL $sql, array &$queries): void { $query = $sql->insert("ApiPermission", ["method", "groups", "description", "is_core"]); foreach (Request::getApiEndpoints() as $reflectionClass) { diff --git a/Core/Configuration/DatabaseScript.class.php b/Core/Configuration/DatabaseScript.class.php deleted file mode 100644 index 93c69b4..0000000 --- a/Core/Configuration/DatabaseScript.class.php +++ /dev/null @@ -1,9 +0,0 @@ -createTable("EntityLog") + ->addInt("entityId") + ->addString("tableName") + ->addDateTime("modified", false, $sql->now()) + ->addInt("lifetime", false, 90); + +$insertProcedure = $sql->createProcedure("InsertEntityLog") + ->param(new CurrentTable()) + ->param(new IntColumn("id")) + ->param(new IntColumn("lifetime", false, 90)) + ->returns(new Trigger()) + ->exec(array( + $sql->insert("EntityLog", ["entityId", "tableName", "lifetime"]) + ->addRow(new CurrentColumn("id"), new CurrentTable(), new CurrentColumn("lifetime")) + )); + +$updateProcedure = $sql->createProcedure("UpdateEntityLog") + ->param(new CurrentTable()) + ->param(new IntColumn("id")) + ->returns(new Trigger()) + ->exec(array( + $sql->update("EntityLog") + ->set("modified", $sql->now()) + ->whereEq("entityId", new CurrentColumn("id")) + ->whereEq("tableName", new CurrentTable()) + )); + +$deleteProcedure = $sql->createProcedure("DeleteEntityLog") + ->param(new CurrentTable()) + ->param(new IntColumn("id")) + ->returns(new Trigger()) + ->exec(array( + $sql->delete("EntityLog") + ->whereEq("entityId", new CurrentColumn("id")) + ->whereEq("tableName", new CurrentTable()) + )); + +$queries[] = $insertProcedure; +$queries[] = $updateProcedure; +$queries[] = $deleteProcedure; diff --git a/Core/Configuration/Patch/EntityLog_2021_04_08.class.php b/Core/Configuration/Patch/EntityLog_2021_04_08.class.php deleted file mode 100644 index eb4d451..0000000 --- a/Core/Configuration/Patch/EntityLog_2021_04_08.class.php +++ /dev/null @@ -1,63 +0,0 @@ -createTable("EntityLog") - ->addInt("entityId") - ->addString("tableName") - ->addDateTime("modified", false, $sql->now()) - ->addInt("lifetime", false, 90); - - $insertProcedure = $sql->createProcedure("InsertEntityLog") - ->param(new CurrentTable()) - ->param(new IntColumn("id")) - ->param(new IntColumn("lifetime", false, 90)) - ->returns(new Trigger()) - ->exec(array( - $sql->insert("EntityLog", ["entityId", "tableName", "lifetime"]) - ->addRow(new CurrentColumn("id"), new CurrentTable(), new CurrentColumn("lifetime")) - )); - - $updateProcedure = $sql->createProcedure("UpdateEntityLog") - ->param(new CurrentTable()) - ->param(new IntColumn("id")) - ->returns(new Trigger()) - ->exec(array( - $sql->update("EntityLog") - ->set("modified", $sql->now()) - ->whereEq("entityId", new CurrentColumn("id")) - ->whereEq("tableName", new CurrentTable()) - )); - - $deleteProcedure = $sql->createProcedure("DeleteEntityLog") - ->param(new CurrentTable()) - ->param(new IntColumn("id")) - ->returns(new Trigger()) - ->exec(array( - $sql->delete("EntityLog") - ->whereEq("entityId", new CurrentColumn("id")) - ->whereEq("tableName", new CurrentTable()) - )); - - $queries[] = $insertProcedure; - $queries[] = $updateProcedure; - $queries[] = $deleteProcedure; - - return $queries; - } - -} diff --git a/Core/Documents/Install.class.php b/Core/Documents/Install.class.php index 1526007..e7a988c 100644 --- a/Core/Documents/Install.class.php +++ b/Core/Documents/Install.class.php @@ -161,6 +161,7 @@ namespace Documents\Install { return self::CHECKING_REQUIREMENTS; } + // TODO: also check the presence of react dist? $externalDir = $this->getExternalDirectory(); $autoload = implode(DIRECTORY_SEPARATOR, [$externalDir, "vendor", "autoload.php"]); if (!is_file($autoload)) { @@ -238,6 +239,7 @@ namespace Documents\Install { "/Site/Configuration", "/Core/External/vendor", "/files/uploaded", + "/react", ]; $nonWritableDirectories = []; @@ -801,7 +803,7 @@ namespace Documents\Install { $progressText = htmlspecialchars($currentView["progressText"]); $class = ["my-3"]; if (!in_array($this->currentStep, [self::CHECKING_REQUIREMENTS, self::INSTALL_DEPENDENCIES])) { - $class[] = "hidden"; + $class[] = "d-none"; } $html .= html_tag("div", ["class" => $class, "id" => "progressText"], [$progressText, $spinnerIcon], false); @@ -851,7 +853,7 @@ namespace Documents\Install { $attrs = ["id" => $id, "class" => ["m-1", "btn", "btn-$type"]]; if (isset($button["hidden"]) && $button["hidden"]) { - $attrs["class"][] = "hidden"; + $attrs["class"][] = "d-none"; } if (isset($button["disabled"]) && $button["disabled"]) { diff --git a/cli.php b/cli.php index 436fb3c..4d6aa3a 100755 --- a/cli.php +++ b/cli.php @@ -8,7 +8,6 @@ require_once 'Core/datetime.php'; include_once 'Core/constants.php'; use Core\API\Request; -use Core\Configuration\DatabaseScript; use Core\Driver\SQL\Column\Column; use Core\Driver\SQL\Condition\Compare; use Core\Driver\SQL\Condition\CondIn; @@ -79,23 +78,9 @@ function printHelp(array $argv): void { } } -function applyPatch(\Core\Driver\SQL\SQL $sql, string $patchName): bool { - $class = str_replace('/', '\\', $patchName); - $className = "\\Core\\Configuration\\$class"; - $classPath = getClassPath($className); - if (!file_exists($classPath) || !is_readable($classPath)) { - printLine("Database script file does not exist or is not readable"); - return false; - } - - include_once $classPath; - $obj = new $className(); - if (!($obj instanceof DatabaseScript)) { - printLine("Not a database script"); - return false; - } - - $queries = $obj->createQueries($sql); +function applyPatch(SQL $sql, string $patchFile): bool { + $queries = []; + @include_once $patchFile; foreach ($queries as $query) { if (!$query->execute($sql)) { printLine($sql->getLastError()); @@ -288,7 +273,10 @@ function onMaintenance(array $argv): void { _exit("Maintenance disabled"); } else if ($action === "update") { $logger->info("Update started"); - $oldPatchFiles = glob('Core/Configuration/Patch/*.php'); + $oldPatchFiles = array_merge( + glob('Core/Configuration/Patch/*.php'), + glob('Site/Configuration/Patch/*.php') + ); printLine("$ git remote -v"); exec("git remote -v", $gitRemote, $ret); if ($ret !== 0) { @@ -351,16 +339,19 @@ function onMaintenance(array $argv): void { die(); } - // TODO: also collect patches from Site/Configuration/Patch ... and what about database entities? - $newPatchFiles = glob('Core/Configuration/Patch/*.php'); + // TODO: how to handle modified database entities? + $newPatchFiles = array_merge( + glob('Core/Configuration/Patch/*.php'), + glob('Site/Configuration/Patch/*.php') + ); $newPatchFiles = array_diff($newPatchFiles, $oldPatchFiles); if (count($newPatchFiles) > 0) { if ($sql) { printLine("Applying new database patches"); + sort($newPatchFiles); foreach ($newPatchFiles as $patchFile) { - if (preg_match("/Core\/Configuration\/(Patch\/.*)\.class\.php/", $patchFile, $match)) { - $patchName = $match[1]; - applyPatch($sql, $patchName); + if (preg_match("/(Core|Site)\/Configuration\/Patch\/(.*)\.php/", $patchFile, $match)) { + applyPatch($sql, $patchFile); } } } else { diff --git a/docker-compose.yml b/docker-compose.yml index faf503c..3c10852 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,3 @@ -version: "3.9" services: web: container_name: webbase-web diff --git a/js/install.js b/js/install.js index a6774b1..b38ed6f 100644 --- a/js/install.js +++ b/js/install.js @@ -27,7 +27,7 @@ function setState(state) { icon = 'fas fa-times-circle'; text = "Failed"; color = "danger"; - $("#btnRetry").show(); + $("#btnRetry").removeClass("d-none"); break; default: @@ -74,7 +74,7 @@ function sendRequest(params, done) { setState(ERROR); statusBox.addClass("alert-danger"); statusBox.html("An error occurred during installation. Try restarting the process."); - statusBox.show(); + statusBox.removeClass("d-none"); }).always(function() { if(done) done(success); }); @@ -82,15 +82,15 @@ function sendRequest(params, done) { function retry() { let progressText = $("#progressText"); - let wasHidden = progressText.hasClass("hidden"); - $("#btnRetry").hide(); - progressText.removeClass("hidden"); + let wasHidden = progressText.hasClass("d-none"); + $("#btnRetry").addClass("d-none"); + progressText.removeClass("d-none"); sendRequest({ }, function(success) { if (wasHidden) { - $("#progressText").addClass("hidden"); + $("#progressText").addClass("d-none"); } if(!success) { - $("#btnRetry").show(); + $("#btnRetry").removeClass("d-none"); } }); }