2023-01-09 20:27:01 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Core\API {
|
|
|
|
|
2024-04-23 12:14:28 +02:00
|
|
|
use Core\Objects\Context;
|
|
|
|
|
2023-01-09 20:27:01 +01:00
|
|
|
abstract class DatabaseAPI extends Request {
|
|
|
|
|
2024-04-23 12:14:28 +02:00
|
|
|
public function __construct(Context $context, bool $externalCall = false, array $params = array()) {
|
|
|
|
parent::__construct($context, $externalCall, $params);
|
|
|
|
}
|
|
|
|
|
2023-01-09 20:27:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Core\API\Database {
|
|
|
|
|
|
|
|
use Core\API\DatabaseAPI;
|
2024-04-22 13:05:35 +02:00
|
|
|
use Core\API\Parameter\RegexType;
|
2023-01-09 20:27:01 +01:00
|
|
|
use Core\Objects\Context;
|
2023-01-10 22:12:05 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Controller\DatabaseEntity;
|
2023-01-16 21:47:23 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Group;
|
2023-01-09 20:27:01 +01:00
|
|
|
|
|
|
|
class Status extends DatabaseAPI {
|
|
|
|
|
|
|
|
public function __construct(Context $context, bool $externalCall = false) {
|
|
|
|
parent::__construct($context, $externalCall, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _execute(): bool {
|
|
|
|
$sql = $this->context->getSQL();
|
|
|
|
$status = $sql->getStatus();
|
|
|
|
$this->result["status"] = $status;
|
|
|
|
return true;
|
|
|
|
}
|
2023-01-16 21:47:23 +01:00
|
|
|
|
2024-04-23 12:14:28 +02:00
|
|
|
public static function getDescription(): string {
|
|
|
|
return "Allows users to view the database status";
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getDefaultPermittedGroups(): array {
|
|
|
|
return [Group::ADMIN];
|
2023-01-16 21:47:23 +01:00
|
|
|
}
|
2023-01-09 20:27:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class Migrate extends DatabaseAPI {
|
|
|
|
public function __construct(Context $context, bool $externalCall = false) {
|
|
|
|
parent::__construct($context, $externalCall, [
|
2024-04-22 13:05:35 +02:00
|
|
|
"className" => new RegexType("className", "[a-zA-Z][a-zA-Z0-9]{0,256}")
|
2023-01-09 20:27:01 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _execute(): bool {
|
|
|
|
$className = $this->getParam("className");
|
|
|
|
$class = null;
|
|
|
|
foreach (["Site", "Core"] as $baseDir) {
|
|
|
|
$classPath = "\\$baseDir\\Objects\\DatabaseEntity\\$className";
|
|
|
|
if (isClass($classPath)) {
|
|
|
|
$class = new \ReflectionClass($classPath);
|
2023-01-10 22:12:05 +01:00
|
|
|
if (!$class->isSubclassOf(DatabaseEntity::class)) {
|
|
|
|
$class = null;
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-09 20:27:01 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($class === null) {
|
|
|
|
return $this->createError("Class not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = $this->context->getSQL();
|
|
|
|
$handler = call_user_func("$classPath::getHandler", $sql, null, true);
|
|
|
|
$persistables = array_merge([
|
|
|
|
$handler->getTableName() => $handler
|
|
|
|
], $handler->getNMRelations());
|
|
|
|
|
|
|
|
foreach ($persistables as $tableName => $persistable) {
|
|
|
|
// first check if table exists
|
|
|
|
if (!$sql->tableExists($tableName)) {
|
|
|
|
$sql->startTransaction();
|
|
|
|
$success = true;
|
|
|
|
try {
|
|
|
|
foreach ($persistable->getCreateQueries($sql) as $query) {
|
|
|
|
if (!$query->execute()) {
|
|
|
|
$this->lastError = "Error migrating table: " . $sql->getLastError();
|
|
|
|
$success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
$success = false;
|
|
|
|
$this->lastError = "Error migrating table: " . $ex->getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$success) {
|
|
|
|
$sql->rollback();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$sql->commit();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: Alter table ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-01-16 21:47:23 +01:00
|
|
|
|
2024-04-23 12:14:28 +02:00
|
|
|
public static function getDescription(): string {
|
|
|
|
return "Allows users to migrate the database structure";
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getDefaultPermittedGroups(): array {
|
|
|
|
return [Group::ADMIN];
|
2023-01-16 21:47:23 +01:00
|
|
|
}
|
2023-01-09 20:27:01 +01:00
|
|
|
}
|
|
|
|
}
|