diff --git a/.env.example.ini b/.env.example.ini index b0dc222..a214a34 100755 --- a/.env.example.ini +++ b/.env.example.ini @@ -1,17 +1,20 @@ -date_time_zone = "Europe/Stockholm" +[client_api] +base_url = "" +api_key = "" +verify_peer = true -[api_database] +[client_time_available] +time_zone = "Europe/Stockholm" +available_to_hour = 0; +reply_average_hours = 0; +available_from_hour = 0; + +[server_database] host = "" user = "" pass = "" db = "" -[api_forgejo] +[server_forgejo] base_url = "" -cache_file = "" -scan_profiles = "" - -[api_client] -base_url = "https://api.vlw.one/" -api_key = "" -verify_peer = 0 \ No newline at end of file +scan_profiles = "" \ No newline at end of file diff --git a/composer.lock b/composer.lock index b79220e..b422e8d 100755 --- a/composer.lock +++ b/composer.lock @@ -11,15 +11,9 @@ "version": "dev-master", "source": { "type": "git", - "url": "https://github.com/VictorWesterlund/reflect-client-php.git", + "url": "https://codeberg.org/reflect/client-php", "reference": "89a8c041044c8c60cefafc4716d5d61b96c43e06" }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/VictorWesterlund/reflect-client-php/zipball/89a8c041044c8c60cefafc4716d5d61b96c43e06", - "reference": "89a8c041044c8c60cefafc4716d5d61b96c43e06", - "shasum": "" - }, "default-branch": true, "type": "library", "autoload": { @@ -38,10 +32,6 @@ } ], "description": "Extendable PHP interface for communicating with Reflect API over HTTP or UNIX sockets", - "support": { - "issues": "https://github.com/VictorWesterlund/reflect-client-php/issues", - "source": "https://github.com/VictorWesterlund/reflect-client-php/tree/3.0.6" - }, "time": "2024-04-06T14:55:04+00:00" }, { @@ -78,7 +68,7 @@ "source": { "type": "git", "url": "https://codeberg.org/vlw/php-mysql", - "reference": "64c7bae3cf6124dcb64c9e8ef93425be3602e82a" + "reference": "c64eb96049907da60dc9f237d26aef0e531b0015" }, "default-branch": true, "type": "library", @@ -98,7 +88,7 @@ } ], "description": "Abstraction library for common MySQL/MariaDB DML operations with php-mysqli", - "time": "2025-01-16T13:53:30+00:00" + "time": "2025-01-30T09:33:10+00:00" }, { "name": "vlw/xenum", @@ -135,7 +125,8 @@ "stability-flags": { "reflect/client": 20, "reflect/plugin-rules": 20, - "vlw/mysql": 20 + "vlw/mysql": 20, + "vlw/xenum": 20 }, "prefer-stable": false, "prefer-lowest": false, diff --git a/endpoints/about/languages/DELETE.php b/endpoints/about/languages/DELETE.php index 0aedeff..a1e5247 100644 --- a/endpoints/about/languages/DELETE.php +++ b/endpoints/about/languages/DELETE.php @@ -1,21 +1,40 @@ ruleset = new Ruleset(strict: true); + $this->ruleset->validate_or_exit(); + + parent::__construct(); + } + + private function languages(): array { + $resp = (new Call(Endpoints::ABOUT_LANGUAGES->value))->get(); + + return array_column($resp->output(), LanguagesTable::ID->value); + } // Delete languages cache file if it exists public function main(): Response { - // Bail out if cache is not used - if (empty($_ENV["forgejo_languages"]["cache_file"])) { - return new Response(MSG_OK); + $this->db->for(LanguagesTable::NAME); + + foreach ($this->languages() as $language){ + $this->db->delete([LanguagesTable::ID->value => $language]); } - return unlink($_ENV["forgejo_languages"]["cache_file"]) ? new Response(MSG_OK) : new Response(MSG_FAIL, 404); + return new Response(); } } \ No newline at end of file diff --git a/endpoints/about/languages/GET.php b/endpoints/about/languages/GET.php index ddbc0c7..92e3b29 100644 --- a/endpoints/about/languages/GET.php +++ b/endpoints/about/languages/GET.php @@ -1,48 +1,54 @@ ruleset = new Ruleset(strict: true); $this->ruleset->GET([ - (new Rules(PARM_FORCE_RECACHE)) + (new Rules(LanguagesTable::ID->value)) + ->type(Type::STRING) + ->min(1) + ->max(parent::SIZE_VARCHAR), + + (new Rules(LanguagesTable::BYTES->value)) + ->type(Type::NUMBER) + ->min(1) + ->max(parent::SIZE_UINT32), + + (new Rules(FORGEJO_UPDATE_CACHE_PARAM)) ->type(Type::BOOLEAN) ->default(false) ]); $this->ruleset->validate_or_exit(); - } - private static function cache_exists(): bool { - return file_exists($_ENV["forgejo_languages"]["cache_file"]); - } - - private static function load_cache(): array { - return json_decode(file_get_contents($_ENV["forgejo_languages"]["cache_file"]), true); + parent::__construct(); } public function main(): Response { - // Delete cache file if force flag is set - if ($_GET[PARM_FORCE_RECACHE]) { - (new Call(Endpoints::ABOUT_LANGUAGES->value))->delete(); + // Refresh the language cache if param is set + if ($_GET[FORGEJO_UPDATE_CACHE_PARAM]) { + (new Call(Endpoints::ABOUT_LANGUAGES->value))->post(); } - return self::cache_exists() - // Return languages from cache - ? new Response(self::load_cache()) - // Fetch and return languages (and generate cache file if enabled) - : new Response((new Call(Endpoints::ABOUT_LANGUAGES->value))->post()); + return $this->list(LanguagesTable::NAME, LanguagesTable::values(), [ + LanguagesTable::BYTES->value => Order::DESC + ]); } } \ No newline at end of file diff --git a/endpoints/about/languages/POST.php b/endpoints/about/languages/POST.php index 2ff6625..d75189b 100644 --- a/endpoints/about/languages/POST.php +++ b/endpoints/about/languages/POST.php @@ -1,19 +1,29 @@ ruleset = new Ruleset(strict: true); + $this->ruleset->validate_or_exit(); + + parent::__construct(); + } // Fetch JSON from URL private static function fetch_json(string $url): array { @@ -22,20 +32,23 @@ // Fetch JSON from a Forgejo endpoint private static function fetch_endpoint(string $endpoint): array { - $url = $_ENV["forgejo"]["base_url"] . $endpoint; + $url = $_ENV["server_forgejo"]["base_url"] . $endpoint; return self::fetch_json($url); } // Write $this->languages to a JSON file - private function cache_languages(): int|bool { - $cache_filename = $_ENV["forgejo_languages"]["cache_file"]; + private function cache_languages(): void { + // Delete existing cache + (new Call(Endpoints::ABOUT_LANGUAGES->value))->delete(); - // Bail out if cache file is not configured - if (empty($cache_filename)) { - return true; + $this->db->for(LanguagesTable::NAME); + + foreach ($this->languages as $language => $bytes) { + $this->db->insert([ + LanguagesTable::ID->value => $language, + LanguagesTable::BYTES->value => $bytes + ]); } - - return file_put_contents($cache_filename, json_encode($this->languages)); } // Fetch and add languages to total from a fully-qualified Forgejo URL @@ -70,13 +83,10 @@ // Add languages from all public repositories for profiles in config private function add_repositories_from_config_profiles(): void { - foreach(explode(",", $_ENV["forgejo_languages"]["scan_profiles"]) as $profile) { + foreach(explode(",", $_ENV["server_forgejo"]["scan_profiles"]) as $profile) { // Resolve user data from username $user = self::fetch_endpoint(sprintf(FORGEJO_ENDPOINT_USER, $profile)); - - if (!$this->add_public_repositores($user["id"])) { - $this->errors[] = $profile; - } + $this->add_public_repositores($user["id"]); } } @@ -86,11 +96,7 @@ // Sort langauges bytes tally by largest in descending order arsort($this->languages); - // Save languages to cache - if (!$this->cache_languages()) { - $this->errors[] = ERRNO_CACHE_FAILED; - } - + $this->cache_languages(); return new Response($this->languages); } } \ No newline at end of file diff --git a/endpoints/battlestation/GET.php b/endpoints/battlestation/GET.php index fcb88b3..b238306 100644 --- a/endpoints/battlestation/GET.php +++ b/endpoints/battlestation/GET.php @@ -7,7 +7,7 @@ use VLW\Database\Tables\Battlestation\Config\ConfigModel; require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Battlestation/Config/Config.php"); + require_once Path::root("src/Database/Tables/Battlestation/Config/Config.php"); class GET_Battlestation extends Database { protected Ruleset $ruleset; @@ -27,7 +27,7 @@ (new Rules(ConfigModel::FRIENDLY_NAME->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH) + ->max(parent::SIZE_VARCHAR) ]); parent::__construct(Databases::BATTLESTATION, $this->ruleset); diff --git a/endpoints/battlestation/chassis/GET.php b/endpoints/battlestation/chassis/GET.php index b525dd5..4f8618a 100644 --- a/endpoints/battlestation/chassis/GET.php +++ b/endpoints/battlestation/chassis/GET.php @@ -8,8 +8,8 @@ use VLW\Database\Tables\Battlestation\Config\ChassisMbTable; require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Battlestation/Chassis.php"); - require_once Path::root("src/Database/Models/Battlestation/Config/ChassisMb.php"); + require_once Path::root("src/Database/Tables/Battlestation/Chassis.php"); + require_once Path::root("src/Database/Tables/Battlestation/Config/ChassisMb.php"); class GET_BattlestationChassis extends Database { private const REL_MOTHERBOARDS = "motherboards"; @@ -31,12 +31,12 @@ (new Rules(ChassisTable::VENDOR_NAME->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(ChassisTable::VENDOR_MODEL->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(ChassisTable::STORAGE_TWOINCHFIVE->value)) ->type(Type::NUMBER) diff --git a/endpoints/battlestation/coolers/GET.php b/endpoints/battlestation/coolers/GET.php index c209033..79dbe5d 100644 --- a/endpoints/battlestation/coolers/GET.php +++ b/endpoints/battlestation/coolers/GET.php @@ -10,8 +10,8 @@ use VLW\Database\Tables\Battlestation\Config\MbCpuCoolerModel; require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Battlestation/Coolers.php"); - require_once Path::root("src/Database/Models/Battlestation/Config/MbCpuCooler.php"); + require_once Path::root("src/Database/Tables/Battlestation/Coolers.php"); + require_once Path::root("src/Database/Tables/Battlestation/Config/MbCpuCooler.php"); class GET_BattlestationCoolers extends Database { private const REL_MOTHERBOARDS = "motherboards"; @@ -48,12 +48,12 @@ (new Rules(CoolerModel::VENDOR_NAME->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(CoolerModel::VENDOR_MODEL->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(CoolerModel::IS_RETIRED->value)) ->type(Type::BOOLEAN) diff --git a/endpoints/battlestation/cpu/GET.php b/endpoints/battlestation/cpu/GET.php index 39b0362..6c98fa0 100644 --- a/endpoints/battlestation/cpu/GET.php +++ b/endpoints/battlestation/cpu/GET.php @@ -11,8 +11,8 @@ use VLW\Database\Tables\Battlestation\Config\MbCpuCoolerModel; require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Battlestation/Cpu.php"); - require_once Path::root("src/Database/Models/Battlestation/Config/MbCpuCooler.php"); + require_once Path::root("src/Database/Tables/Battlestation/Cpu.php"); + require_once Path::root("src/Database/Tables/Battlestation/Config/MbCpuCooler.php"); class GET_BattlestationCpu extends Database { private const REL_MOTHERBOARDS = "motherboards"; @@ -57,12 +57,12 @@ (new Rules(CpuTable::VENDOR_NAME->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(CpuTable::VENDOR_MODEL->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(CpuTable::IS_RETIRED->value)) ->type(Type::BOOLEAN) diff --git a/endpoints/battlestation/dram/GET.php b/endpoints/battlestation/dram/GET.php index 75b04b6..a847e63 100644 --- a/endpoints/battlestation/dram/GET.php +++ b/endpoints/battlestation/dram/GET.php @@ -12,8 +12,8 @@ use VLW\Database\Tables\Battlestation\Config\MbDramTable; require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Battlestation/Dram.php"); - require_once Path::root("src/Database/Models/Battlestation/Config/MbDram.php"); + require_once Path::root("src/Database/Tables/Battlestation/Dram.php"); + require_once Path::root("src/Database/Tables/Battlestation/Config/MbDram.php"); class GET_BattlestationDram extends Database { private const REL_MOTHERBOARDS = "motherboards"; @@ -55,12 +55,12 @@ (new Rules(DramTable::VENDOR_NAME->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(DramTable::VENDOR_MODEL->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(DramTable::IS_RETIRED->value)) ->type(Type::BOOLEAN) diff --git a/endpoints/battlestation/gpu/GET.php b/endpoints/battlestation/gpu/GET.php index af4c86c..f203c9f 100644 --- a/endpoints/battlestation/gpu/GET.php +++ b/endpoints/battlestation/gpu/GET.php @@ -8,8 +8,8 @@ use VLW\Database\Tables\Battlestation\Config\MbGpuTable; require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Battlestation/Gpu.php"); - require_once Path::root("src/Database/Models/Battlestation/Config/MbGpu.php"); + require_once Path::root("src/Database/Tables/Battlestation/Gpu.php"); + require_once Path::root("src/Database/Tables/Battlestation/Config/MbGpu.php"); class GET_BattlestationGpu extends Database { private const REL_MOTHERBOARDS = "motherboards"; @@ -35,22 +35,22 @@ (new Rules(GpuTable::VENDOR_NAME->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(GpuTable::VENDOR_MODEL->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(GpuTable::VENDOR_CHIP_NAME->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(GpuTable::VENDOR_CHIP_MODEL->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(GpuTable::IS_RETIRED->value)) ->type(Type::BOOLEAN) diff --git a/endpoints/battlestation/mb/GET.php b/endpoints/battlestation/mb/GET.php index 6ea1129..d2a41a0 100644 --- a/endpoints/battlestation/mb/GET.php +++ b/endpoints/battlestation/mb/GET.php @@ -18,13 +18,13 @@ }; require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Battlestation/Mb.php"); - require_once Path::root("src/Database/Models/Battlestation/Config/MbPsu.php"); - require_once Path::root("src/Database/Models/Battlestation/Config/MbGpu.php"); - require_once Path::root("src/Database/Models/Battlestation/Config/MbDram.php"); - require_once Path::root("src/Database/Models/Battlestation/Config/MbStorage.php"); - require_once Path::root("src/Database/Models/Battlestation/Config/ChassisMb.php"); - require_once Path::root("src/Database/Models/Battlestation/Config/MbCpuCooler.php"); + require_once Path::root("src/Database/Tables/Battlestation/Mb.php"); + require_once Path::root("src/Database/Tables/Battlestation/Config/MbPsu.php"); + require_once Path::root("src/Database/Tables/Battlestation/Config/MbGpu.php"); + require_once Path::root("src/Database/Tables/Battlestation/Config/MbDram.php"); + require_once Path::root("src/Database/Tables/Battlestation/Config/MbStorage.php"); + require_once Path::root("src/Database/Tables/Battlestation/Config/ChassisMb.php"); + require_once Path::root("src/Database/Tables/Battlestation/Config/MbCpuCooler.php"); class GET_BattlestationMb extends Database { private const REL_CPU = "cpus"; @@ -54,30 +54,30 @@ (new Rules(MbTable::VENDOR_NAME->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(MbTable::VENDOR_MODEL->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(MbTable::NETWORK_ETHERNET->value)) ->type(Type::NULL) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(MbTable::NETWORK_WLAN->value)) ->type(Type::NULL) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(MbTable::NETWORK_BLUETOOTH->value)) ->type(Type::NULL) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(MbTable::IS_RETIRED->value)) ->type(Type::BOOLEAN) diff --git a/endpoints/battlestation/psu/GET.php b/endpoints/battlestation/psu/GET.php index a94ef8c..f988b20 100644 --- a/endpoints/battlestation/psu/GET.php +++ b/endpoints/battlestation/psu/GET.php @@ -11,8 +11,8 @@ use VLW\Database\Tables\Battlestation\Config\MbPsuTable; require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Battlestation/Psu.php"); - require_once Path::root("src/Database/Models/Battlestation/Config/MbPsu.php"); + require_once Path::root("src/Database/Tables/Battlestation/Psu.php"); + require_once Path::root("src/Database/Tables/Battlestation/Config/MbPsu.php"); class GET_BattlestationPsu extends Database { private const REL_MOTHERBOARDS = "motherboards"; @@ -34,7 +34,7 @@ (new Rules(PsuTable::POWER->value)) ->type(Type::NUMBER) ->min(1) - ->max(parent::MYSQL_INT_MAX_LENGTH), + ->max(parent::SIZE_UINT8), (new Rules(PsuTable::EIGHTYPLUS_RATING->value)) ->type(Type::ENUM, EightyplusRatingEnum::names()), @@ -42,12 +42,12 @@ (new Rules(PsuTable::VENDOR_NAME->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(PsuTable::VENDOR_MODEL->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(PsuTable::IS_RETIRED->value)) ->type(Type::BOOLEAN) diff --git a/endpoints/battlestation/storage/GET.php b/endpoints/battlestation/storage/GET.php index d423e4a..4425788 100644 --- a/endpoints/battlestation/storage/GET.php +++ b/endpoints/battlestation/storage/GET.php @@ -13,8 +13,8 @@ use VLW\Database\Tables\Battlestation\Config\MbStorageTable; require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Battlestation/Storage.php"); - require_once Path::root("src/Database/Models/Battlestation/Config/MbStorage.php"); + require_once Path::root("src/Database/Tables/Battlestation/Storage.php"); + require_once Path::root("src/Database/Tables/Battlestation/Config/MbStorage.php"); class GET_BattlestationStorage extends Database { private const REL_MOTHERBOARDS = "motherboards"; @@ -39,7 +39,7 @@ (new Rules(StorageTable::DISK_SIZE->value)) ->type(Type::NUMBER) ->min(1) - ->max(parent::MYSQL_INT_MAX_LENGTH), + ->max(parent::SIZE_UINT8), (new Rules(StorageTable::DISK_INTERFACE->value)) ->type(Type::ENUM, StorageDiskInterfaceEnum::names()), @@ -50,12 +50,12 @@ (new Rules(StorageTable::VENDOR_NAME->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(StorageTable::VENDOR_MODEL->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(StorageTable::IS_RETIRED->value)) ->type(Type::BOOLEAN) diff --git a/endpoints/messages/POST.php b/endpoints/messages/POST.php index f7312ea..9512460 100644 --- a/endpoints/messages/POST.php +++ b/endpoints/messages/POST.php @@ -7,7 +7,7 @@ use VLW\Database\Tables\Messages\MessagesTable; require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Messages/Messages.php"); + require_once Path::root("src/Database/Tables/Messages/Messages.php"); class POST_Messages extends Database { protected Ruleset $ruleset; @@ -25,23 +25,19 @@ ->required() ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_TEXT_MAX_LENGTH) + ->max(parent::SIZE_TEXT) ]); - $ruleset->validate_or_exit(); + $this->ruleset->validate_or_exit(); parent::__construct(); } public function main(): Response { - // Use copy of request body as entity - $entity = $_POST; + $_POST[MessagesTable::TIMESTAMP_CREATED->value] = time(); - $entity[MessagesTable::ID->value] = parent::gen_uuid4(); - $entity[MessagesTable::DATE_CREATED->value] = time(); - - return $this->db->for(MessagesTable::NAME)->insert($entity) === true - ? new Response($entity[MessagesTable::ID->value], 201) - : new Response("Failed to create message", 500); + return $this->db->for(MessagesTable::NAME)->insert($_POST) === true + ? new Response(null, 201) + : new Response("Failed to send message", 500); } } \ No newline at end of file diff --git a/endpoints/search/GET.php b/endpoints/search/GET.php index 5ac87a5..0954712 100644 --- a/endpoints/search/GET.php +++ b/endpoints/search/GET.php @@ -11,7 +11,7 @@ require_once Path::root("src/Endpoints.php"); require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Work/Work.php"); + require_once Path::root("src/Database/Tables/Work/Work.php"); class GET_Search extends Database { const GET_QUERY = "q"; @@ -26,10 +26,10 @@ ->required() ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH) + ->max(parent::SIZE_VARCHAR) ]); - $ruleset->validate_or_exit(); + $this->ruleset->validate_or_exit(); parent::__construct(); } diff --git a/endpoints/work/DELETE.php b/endpoints/work/DELETE.php index 31be30f..39ba963 100644 --- a/endpoints/work/DELETE.php +++ b/endpoints/work/DELETE.php @@ -9,7 +9,7 @@ require_once Path::root("src/Endpoints.php"); require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Work.php"); + require_once Path::root("src/Database/Tables/Work.php"); class DELETE_Work extends Database { protected Ruleset $ruleset; @@ -21,17 +21,17 @@ (new Rules(WorkTable::ID->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(WorkTable::TITLE->value)) ->type(Type::STRING) ->min(3) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(WorkTable::SUMMARY->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_TEXT_MAX_LENGTH), + ->max(parent::SIZE_TEXT), (new Rules(WorkTable::IS_LISTED->value)) ->type(Type::BOOLEAN), @@ -39,15 +39,15 @@ (new Rules(WorkTable::DATE_MODIFIED->value)) ->type(Type::NUMBER) ->min(1) - ->max(parent::MYSQL_INT_MAX_LENGTH), + ->max(parent::SIZE_UINT8), (new Rules(WorkTable::DATE_CREATED->value)) ->type(Type::NUMBER) ->min(1) - ->max(parent::MYSQL_INT_MAX_LENGTH) + ->max(parent::SIZE_UINT8) ]); - $ruleset->validate_or_exit(); + $this->ruleset->validate_or_exit(); parent::__construct(); } diff --git a/endpoints/work/GET.php b/endpoints/work/GET.php index cd374c7..548a67d 100644 --- a/endpoints/work/GET.php +++ b/endpoints/work/GET.php @@ -1,5 +1,6 @@ value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(WorkTable::TITLE->value)) ->type(Type::STRING) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(WorkTable::SUMMARY->value)) ->type(Type::STRING) - ->max(parent::MYSQL_TEXT_MAX_LENGTH), + ->max(parent::SIZE_TEXT), - (new Rules(WorkTable::IS_LISTED->value)) - ->type(Type::BOOLEAN) - ->default(true), - - (new Rules(WorkTable::DATE_MODIFIED->value)) - ->type(Type::NUMBER) + (new Rules(WorkTable::CREATED->value)) + ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_INT_MAX_LENGTH), - - (new Rules(WorkTable::DATE_CREATED->value)) - ->type(Type::NUMBER) - ->min(1) - ->max(parent::MYSQL_INT_MAX_LENGTH) + ->max(parent::SIZE_VARCHAR) ]); - $ruleset->validate_or_exit(); + $this->ruleset->validate_or_exit(); parent::__construct(); } public function main(): Response { - return $this->list(WorkTable::NAME, WorkTable::values()); + return $this->list(WorkTable::NAME, WorkTable::values(), [ + WorkTable::CREATED->value => Order::DESC + ]); } } \ No newline at end of file diff --git a/endpoints/work/PATCH.php b/endpoints/work/PATCH.php index 2e3c5e4..1ccaaf0 100644 --- a/endpoints/work/PATCH.php +++ b/endpoints/work/PATCH.php @@ -13,8 +13,8 @@ require_once Path::root("src/Endpoints.php"); require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Work/Work.php"); - require_once Path::root("src/Database/Models/Work/WorkPermalinks.php"); + require_once Path::root("src/Database/Tables/Work/Work.php"); + require_once Path::root("src/Database/Tables/Work/WorkPermalinks.php"); class PATCH_Work extends Database { protected Ruleset $ruleset; @@ -27,19 +27,19 @@ ->required() ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH) + ->max(parent::SIZE_VARCHAR) ]); $this->ruleset->POST([ (new Rules(WorkTable::TITLE->value)) ->type(Type::STRING) ->min(3) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(WorkTable::SUMMARY->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_TEXT_MAX_LENGTH), + ->max(parent::SIZE_TEXT), (new Rules(WorkTable::IS_LISTED->value)) ->type(Type::BOOLEAN), @@ -47,13 +47,13 @@ (new Rules(WorkTable::DATE_MODIFIED->value)) ->type(Type::NUMBER) ->min(1) - ->max(parent::MYSQL_INT_MAX_LENGTH) + ->max(parent::SIZE_UINT8) ->default(time()), (new Rules(WorkTable::DATE_CREATED->value)) ->type(Type::NUMBER) ->min(1) - ->max(parent::MYSQL_INT_MAX_LENGTH) + ->max(parent::SIZE_UINT8) ]); parent::__construct(); diff --git a/endpoints/work/POST.php b/endpoints/work/POST.php index 2c602ec..1890b48 100644 --- a/endpoints/work/POST.php +++ b/endpoints/work/POST.php @@ -13,8 +13,8 @@ require_once Path::root("src/Endpoints.php"); require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Work/Work.php"); - require_once Path::root("src/Database/Models/Work/WorkPermalinks.php"); + require_once Path::root("src/Database/Tables/Work/Work.php"); + require_once Path::root("src/Database/Tables/Work/WorkPermalinks.php"); class POST_Work extends Database { protected Ruleset $ruleset; @@ -26,13 +26,13 @@ (new Rules(WorkTable::TITLE->value)) ->type(Type::STRING) ->min(3) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH) + ->max(parent::SIZE_VARCHAR) ->default(null), (new Rules(WorkTable::SUMMARY->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_TEXT_MAX_LENGTH) + ->max(parent::SIZE_TEXT) ->default(null), (new Rules(WorkTable::IS_LISTED->value)) @@ -42,11 +42,11 @@ (new Rules(WorkTable::DATE_CREATED->value)) ->type(Type::NUMBER) ->min(1) - ->max(parent::MYSQL_INT_MAX_LENGTH) + ->max(parent::SIZE_UINT8) ->default(time()) ]); - $ruleset->validate_or_exit(); + $this->ruleset->validate_or_exit(); parent::__construct(); } diff --git a/endpoints/work/actions/DELETE.php b/endpoints/work/actions/DELETE.php index b13d3b2..ea93a30 100644 --- a/endpoints/work/actions/DELETE.php +++ b/endpoints/work/actions/DELETE.php @@ -8,7 +8,7 @@ use VLW\Database\Tables\Work\ActionsTable; require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/WorkActions.php"); + require_once Path::root("src/Database/Tables/WorkActions.php"); class DELETE_WorkActions extends Database { protected Ruleset $ruleset; @@ -20,7 +20,7 @@ $this->ruleset->POST([ (new Rules(ActionsTable::REF_WORK_ID->value)) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH) + ->max(parent::SIZE_VARCHAR) ]); } diff --git a/endpoints/work/actions/GET.php b/endpoints/work/actions/GET.php index acb2475..8fa5e39 100644 --- a/endpoints/work/actions/GET.php +++ b/endpoints/work/actions/GET.php @@ -1,5 +1,6 @@ value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH) + ->max(parent::SIZE_VARCHAR) ]); - $ruleset->validate_or_exit(); + $this->ruleset->validate_or_exit(); parent::__construct(); } public function main(): Response { - $response = $this->db->for(ActionsTable::NAME) - ->where($_GET) - ->select([ - ActionsTable::REF_WORK_ID->value, - ActionsTable::DISPLAY_TEXT->value, - ActionsTable::HREF->value, - ActionsTable::CLASS_LIST->value - ]); - - return $response->num_rows > 0 - ? new Response(parent::index_array_by_key($response->fetch_all(MYSQLI_ASSOC), ActionsTable::REF_WORK_ID->value)) - : new Response([], 404); + return $this->list(ActionsTable::NAME, ActionsTable::values(), [ + ActionsTable::ORDER_IDX->value => Order::DESC + ]); } } \ No newline at end of file diff --git a/endpoints/work/actions/POST.php b/endpoints/work/actions/POST.php index 5e319d8..1e023fd 100644 --- a/endpoints/work/actions/POST.php +++ b/endpoints/work/actions/POST.php @@ -13,8 +13,8 @@ require_once Path::root("src/Endpoints.php"); require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Work/Work.php"); - require_once Path::root("src/Database/Models/Work/WorkActions.php"); + require_once Path::root("src/Database/Tables/Work/Work.php"); + require_once Path::root("src/Database/Tables/Work/WorkActions.php"); class POST_WorkActions extends Database { protected Ruleset $ruleset; @@ -26,20 +26,20 @@ (new Rules(ActionsTable::REF_WORK_ID->value)) ->required() ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(ActionsTable::DISPLAY_TEXT->value)) ->required() ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(ActionsTable::HREF->value)) ->required() ->type(Type::STRING) ->type(Type::NULL) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(ActionsTable::CLASS_LIST->value)) ->type(Type::ARRAY) @@ -47,7 +47,7 @@ ->default([]) ]); - $ruleset->validate_or_exit(); + $this->ruleset->validate_or_exit(); parent::__construct(); } diff --git a/endpoints/work/permalinks/GET.php b/endpoints/work/permalinks/GET.php index 2b06d2a..853671d 100644 --- a/endpoints/work/permalinks/GET.php +++ b/endpoints/work/permalinks/GET.php @@ -7,7 +7,7 @@ use VLW\Database\Tables\Work\PermalinksTable; require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Work/WorkPermalinks.php"); + require_once Path::root("src/Database/Tables/Work/WorkPermalinks.php"); class GET_WorkPermalinks extends Database { protected Ruleset $ruleset; @@ -19,15 +19,15 @@ (new Rules(PermalinksTable::ID->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(PermalinksTable::REF_WORK_ID->value)) ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH) + ->max(parent::SIZE_VARCHAR) ]); - $ruleset->validate_or_exit(); + $this->ruleset->validate_or_exit(); parent::__construct(); } diff --git a/endpoints/work/permalinks/POST.php b/endpoints/work/permalinks/POST.php index 59f339d..d20575f 100644 --- a/endpoints/work/permalinks/POST.php +++ b/endpoints/work/permalinks/POST.php @@ -8,7 +8,7 @@ use VLW\Database\Tables\Work\PermalinksTable; require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Work/WorkPermalinks.php"); + require_once Path::root("src/Database/Tables/Work/WorkPermalinks.php"); class POST_WorkPermalinks extends Database { protected Ruleset $ruleset; @@ -21,22 +21,22 @@ ->required() ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(PermalinksTable::REF_WORK_ID->value)) ->required() ->type(Type::STRING) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(PermalinksTable::DATE_CREATED->value)) ->type(Type::NUMBER) ->min(1) - ->max(parent::MYSQL_INT_MAX_LENGTH) + ->max(parent::SIZE_UINT8) ->default(time()) ]); - $ruleset->validate_or_exit(); + $this->ruleset->validate_or_exit(); parent::__construct(); } diff --git a/endpoints/work/tags/DELETE.php b/endpoints/work/tags/DELETE.php index 53549c9..b5fbed0 100644 --- a/endpoints/work/tags/DELETE.php +++ b/endpoints/work/tags/DELETE.php @@ -8,7 +8,7 @@ use VLW\Database\Tables\Work\TagsTable; require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Work/WorkTags.php"); + require_once Path::root("src/Database/Tables/Work/WorkTags.php"); class DELETE_WorkTags extends Database { private Ruleset $ruleset; @@ -19,13 +19,13 @@ $this->ruleset->GET([ (new Rules(TagsTable::REF_WORK_ID->value)) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(TagsTable::NAME->value)) ->type(Type::ENUM, TagsNameEnum::names()) ]); - $ruleset->validate_or_exit(); + $this->ruleset->validate_or_exit(); parent::__construct(); } diff --git a/endpoints/work/tags/GET.php b/endpoints/work/tags/GET.php index 2e788a7..7d6ff52 100644 --- a/endpoints/work/tags/GET.php +++ b/endpoints/work/tags/GET.php @@ -4,13 +4,10 @@ use ReflectRules\{Ruleset, Rules, Type}; use VLW\Database\Database; - use VLW\Database\Tables\Work\{ - TagsTable, - TagsNameEnum - }; + use VLW\Database\Tables\Work\{TagsTable, TagsLabelEnum}; require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Work/WorkTags.php"); + require_once Path::root("src/Database/Tables/Work/Tags.php"); class GET_WorkTags extends Database { private Ruleset $ruleset; @@ -21,27 +18,18 @@ $this->ruleset->GET([ (new Rules(TagsTable::REF_WORK_ID->value)) ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), - (new Rules(TagsTable::NAME->value)) - ->type(Type::ENUM, TagsNameEnum::names()) + (new Rules(TagsTable::LABEL->value)) + ->type(Type::ENUM, TagsLabelEnum::names()) ]); - $ruleset->validate_or_exit(); + $this->ruleset->validate_or_exit(); parent::__construct(); } public function main(): Response { - $response = $this->db->for(TagsTable::NAME) - ->where($_GET) - ->select([ - TagsTable::REF_WORK_ID->value, - TagsTable::NAME->value - ]); - - return $response->num_rows > 0 - ? new Response(parent::index_array_by_key($response->fetch_all(MYSQLI_ASSOC), TagsTable::REF_WORK_ID->value)) - : new Response([], 404); + return $this->list(TagsTable::NAME, TagsTable::values()); } } \ No newline at end of file diff --git a/endpoints/work/tags/POST.php b/endpoints/work/tags/POST.php index 2856686..1396e2e 100644 --- a/endpoints/work/tags/POST.php +++ b/endpoints/work/tags/POST.php @@ -14,8 +14,8 @@ require_once Path::root("src/Endpoints.php"); require_once Path::root("src/Database/Database.php"); - require_once Path::root("src/Database/Models/Work/Work.php"); - require_once Path::root("src/Database/Models/Work/WorkTags.php"); + require_once Path::root("src/Database/Tables/Work/Work.php"); + require_once Path::root("src/Database/Tables/Work/WorkTags.php"); class POST_WorkTags extends Database { protected Ruleset $ruleset; @@ -27,14 +27,14 @@ (new Rules(TagsTable::REF_WORK_ID->value)) ->required() ->min(1) - ->max(parent::MYSQL_VARCHAR_MAX_LENGTH), + ->max(parent::SIZE_VARCHAR), (new Rules(TagsTable::NAME->value)) ->required() ->type(Type::ENUM, TagsNameEnum::names()) ]); - $ruleset->validate_or_exit(); + $this->ruleset->validate_or_exit(); parent::__construct(); } diff --git a/endpoints/work/timeline/GET.php b/endpoints/work/timeline/GET.php new file mode 100644 index 0000000..2a5af72 --- /dev/null +++ b/endpoints/work/timeline/GET.php @@ -0,0 +1,53 @@ +ruleset = new Ruleset(strict: true); + + $this->ruleset->GET([ + (new Rules(TimelineTable::REF_WORK_ID->value)) + ->type(Type::STRING) + ->min(1) + ->max(parent::SIZE_VARCHAR), + + (new Rules(TimelineTable::YEAR->value)) + ->type(Type::NUMBER) + ->min(0) + ->max(parent::SIZE_UINT16), + + (new Rules(TimelineTable::MONTH->value)) + ->type(Type::NUMBER) + ->min(0) + ->max(parent::SIZE_UINT8), + + (new Rules(TimelineTable::DAY->value)) + ->type(Type::NUMBER) + ->min(0) + ->max(parent::SIZE_UINT8) + ]); + + $this->ruleset->validate_or_exit(); + + parent::__construct(); + } + + public function main(): Response { + return $this->list(TimelineTable::NAME, TimelineTable::values(), [ + TimelineTable::YEAR->value => Order::DESC, + TimelineTable::MONTH->value => Order::DESC, + TimelineTable::DAY->value => Order::DESC + ]); + } + } \ No newline at end of file diff --git a/public/about.php b/public/about.php index 4851da7..918cf22 100644 --- a/public/about.php +++ b/public/about.php @@ -1,64 +1,39 @@ resp = $this->call(Endpoints::ABOUT_LANGUAGES->value)->get(); - - // We got a response from endpoint - if ($this->resp->ok) { - $this->languages = $this->resp->json(); - $this->total_bytes = array_sum($this->languages); - } + $this->total_bytes = array_sum(array_map(fn(Language $language): int => $language->bytes(), parent::all())); } - // Return all languages as (string) language => (int) language_bytes - public function all(): array { - return $this->languages; + public function percent(Language $language, int $mode = PHP_ROUND_HALF_UP): int { + return round(($language->bytes() / $this->total_bytes) * 100, 0, $mode); } - // Return percent of total for all languages - public function get_percent(string $lang, int $mode = PHP_ROUND_HALF_UP): int { - return round(($this->languages[$lang] / $this->total_bytes) * 100, 0, $mode); + public function percent_string(Language $language): string { + return ($this->percent($language) > 1 ? $this->percent($language) : "<1") . "%"; } - // Return language bytes as percent of whole - public function get_percent_str(string $lang): string { - $percent = $this->get_percent($lang, PHP_ROUND_HALF_DOWN); - return ($percent > 1 ? $percent : "<1") . "%"; - } - - // Return languages bytes as a multiple-byte decimal unit - public function get_bytes_str(string $lang): string { - $bytes = $this->languages[$lang]; - + public function bytes_si_string(Language $language): string { // Calculate factor for unit - $factor = floor((strlen($bytes) - 1) / 3); + $factor = floor((strlen($language->bytes()) - 1) / 3); // Divide by radix 10 - $format = $bytes / pow(1000, $factor); + $format = $language->bytes() / pow(1000, $factor); - return round($format) . " " . BYTE_UNITS[$factor]; + return round($format) . " " . FORGEJO_SI_BYTE_MULTIPLE[$factor]; } - }; + } ?> @@ -74,32 +49,30 @@
- all() as $lang => $bytes): ?> - - get_percent_str($lang) ?>
( bytes)
- + +
+ percent_string($language) ?> id ?>
(bytes() ?> bytes)
- all() as $lang => $bytes): ?> - - all() as $lang => $bytes): ?> - - get_percent_str($lang) ?>
( bytes)
- + +
+ percent_string($language) ?> id ?>
(bytes() ?> bytes)
diff --git a/public/about/battlestation.php b/public/about/battlestation.php index 127abf3..26dcba0 100644 --- a/public/about/battlestation.php +++ b/public/about/battlestation.php @@ -29,22 +29,22 @@ require_once VV::root("api/src/Endpoints.php"); // Load hardware database models - require_once VV::root("api/src/Database/Models/Battlestation/Mb.php"); - require_once VV::root("api/src/Database/Models/Battlestation/Cpu.php"); - require_once VV::root("api/src/Database/Models/Battlestation/Gpu.php"); - require_once VV::root("api/src/Database/Models/Battlestation/Psu.php"); - require_once VV::root("api/src/Database/Models/Battlestation/Dram.php"); - require_once VV::root("api/src/Database/Models/Battlestation/Storage.php"); - require_once VV::root("api/src/Database/Models/Battlestation/Chassis.php"); + require_once VV::root("api/src/Database/Tables/Battlestation/Mb.php"); + require_once VV::root("api/src/Database/Tables/Battlestation/Cpu.php"); + require_once VV::root("api/src/Database/Tables/Battlestation/Gpu.php"); + require_once VV::root("api/src/Database/Tables/Battlestation/Psu.php"); + require_once VV::root("api/src/Database/Tables/Battlestation/Dram.php"); + require_once VV::root("api/src/Database/Tables/Battlestation/Storage.php"); + require_once VV::root("api/src/Database/Tables/Battlestation/Chassis.php"); // Load hardware config database models - require_once VV::root("api/src/Database/Models/Battlestation/Config/MbPsu.php"); - require_once VV::root("api/src/Database/Models/Battlestation/Config/MbGpu.php"); - require_once VV::root("api/src/Database/Models/Battlestation/Config/MbDram.php"); - require_once VV::root("api/src/Database/Models/Battlestation/Config/Config.php"); - require_once VV::root("api/src/Database/Models/Battlestation/Config/MbStorage.php"); - require_once VV::root("api/src/Database/Models/Battlestation/Config/ChassisMb.php"); - require_once VV::root("api/src/Database/Models/Battlestation/Config/MbCpuCooler.php"); + require_once VV::root("api/src/Database/Tables/Battlestation/Config/MbPsu.php"); + require_once VV::root("api/src/Database/Tables/Battlestation/Config/MbGpu.php"); + require_once VV::root("api/src/Database/Tables/Battlestation/Config/MbDram.php"); + require_once VV::root("api/src/Database/Tables/Battlestation/Config/Config.php"); + require_once VV::root("api/src/Database/Tables/Battlestation/Config/MbStorage.php"); + require_once VV::root("api/src/Database/Tables/Battlestation/Config/ChassisMb.php"); + require_once VV::root("api/src/Database/Tables/Battlestation/Config/MbCpuCooler.php"); const GIGA = 0x3B9ACA00; const MEGA = 0xF4240; diff --git a/public/contact.php b/public/contact.php index 26dc109..c76edef 100644 --- a/public/contact.php +++ b/public/contact.php @@ -2,58 +2,43 @@ use Vegvisir\Path; - use VLW\Client\API; - use VLW\API\Endpoints; - + use VLW\API\{Client, Endpoints}; use VLW\Database\Tables\Messages\MessagesTable; - require_once VV::root("src/client/API.php"); - require_once VV::root("api/src/Endpoints.php"); - - require_once VV::root("api/src/Database/Models/Messages/Messages.php"); - - // Connect to VLW API - $api = new API(); - - class Date extends DateTimeImmutable { - private const AVAILABLE_TO_HOUR = 16; - private const AVAILABLE_FROM_HOUR = 10; - - private const REPLY_TIME_HOURS_AVAILABLE = 2; + require_once VV::root("src/API/API.php"); + require_once VV::root("src/API/Endpoints.php"); + require_once VV::root("src/Database/Tables/Messages/Messages.php"); + $date = new class extends DateTimeImmutable { public function __construct() { // Set DateTime for configured timezone - parent::__construct("now", new DateTimeZone($_ENV["time"]["date_time_zone"])); + parent::__construct("now", new DateTimeZone($_ENV["client_time_available"]["time_zone"])); } // Return current hour in 24-hour format - private function get_hour(): int { + private function hour(): int { return (int) $this->format("G"); } // Returns true if current time is between available from and to hours public function is_available(): bool { - return $this->get_hour() >= self::AVAILABLE_FROM_HOUR && $this->get_hour() < self::AVAILABLE_TO_HOUR; + return $this->hour() >= $_ENV["client_time_available"]["available_from_hour"] && $this->hour() < $_ENV["client_time_available"]["available_to_hour"]; } public function get_estimated_reply_hours(): int { // I'm available! Return the estimated reply time for that if ($this->is_available()) { - return self::REPLY_TIME_HOURS_AVAILABLE; + return $_ENV["client_time_available"]["reply_average_hours"]; } - $hour = $this->get_hour(); - - return $hour < self::AVAILABLE_FROM_HOUR + return $this->hour() < $_ENV["client_time_available"]["available_from_hour"] // Return hours past midnight until I become available (clamped to estimated reply hours) - ? max(self::AVAILABLE_FROM_HOUR - $hour, self::REPLY_TIME_HOURS_AVAILABLE) + ? max($_ENV["client_time_available"]["available_from_hour"] - $this->hour(), $_ENV["client_time_available"]["reply_average_hours"]) // Return hours before midnight until I become available (clamped to estimated reply hours) - : max(self::AVAILABLE_FROM_HOUR + (24 - $hour), self::REPLY_TIME_HOURS_AVAILABLE); + : max($_ENV["client_time_available"]["available_from_hour"] + (24 - $this->hour()), $_ENV["client_time_available"]["reply_average_hours"]); } } - $date = new Date(); - ?>
@@ -103,7 +88,7 @@ call(Endpoints::MESSAGES->value)->post([ + $send = (new Client())->call(Endpoints::MESSAGES->value)->post([ MessagesTable::EMAIL->value => $_POST[MessagesTable::EMAIL->value], MessagesTable::MESSAGE->value => $_POST[MessagesTable::MESSAGE->value] ]); diff --git a/public/search.php b/public/search.php index 5775029..c3b27d2 100644 --- a/public/search.php +++ b/public/search.php @@ -13,8 +13,8 @@ require_once VV::root("src/client/API.php"); require_once VV::root("api/src/Endpoints.php"); - require_once VV::root("api/src/Database/Models/Work/Work.php"); - require_once VV::root("api/src/Database/Models/Work/WorkActions.php"); + require_once VV::root("api/src/Database/Tables/Work/Work.php"); + require_once VV::root("api/src/Database/Tables/Work/WorkActions.php"); // Search endpoint query paramter const SEARCH_PARAM = "q"; diff --git a/public/work.php b/public/work.php index 6aae4f1..a33f801 100644 --- a/public/work.php +++ b/public/work.php @@ -1,43 +1,10 @@ resp = $this->call(Endpoints::WORK->value)->params([ - WorkTable::IS_LISTED->value => true - ])->get(); - } - - private function get_item(string $key): array { - $idx = array_search($key, array_column($this->resp->json(), WorkTable::ID->value)); - return $this->resp->json()[$idx]; - } - - public function get_summary(string $key): string { - return $this->resp->ok ? $this->get_item($key)[WorkTable::SUMMARY->value] : self::ERROR_MSG; - } - } + require_once VV::root("src/Consts.php"); + require_once VV::root("src/Database/Models/Work/Work.php"); ?> @@ -48,7 +15,7 @@

vegvisir

-

get_summary("vlw/vegvisir") ?>

+

summary() ?>

-

get_summary("vlw/reflect") ?>

+

summary() ?>

vlw/php-mysql

-

get_summary("vlw/php-mysql") ?>

+

summary() ?>

Website for iCellate Medical

-

get_summary("icellate/website") ?>

+

summary() ?>

Modernizing GeneMate by iCellate

-

get_summary("icellate/genemate") ?>

+

summary() ?>

Custom pages for Deltaco AB

-

get_summary("deltaco/asyncapp") ?>

+

summary() ?>

- - - +
diff --git a/src/API/API.php b/src/API/API.php index 2f5dd7d..f3c10f1 100644 --- a/src/API/API.php +++ b/src/API/API.php @@ -10,9 +10,9 @@ public function __construct() { parent::__construct( - $_ENV["api"]["base_url"], - $_ENV["api"]["api_key"], - $_ENV["api"]["verify_peer"] + $_ENV["client_api"]["base_url"], + $_ENV["client_api"]["api_key"], + $_ENV["client_api"]["verify_peer"] ); } } \ No newline at end of file diff --git a/src/API/Endpoints.php b/src/API/Endpoints.php index eac03ea..5c6ecbd 100644 --- a/src/API/Endpoints.php +++ b/src/API/Endpoints.php @@ -13,6 +13,7 @@ case MESSAGES = "/messages"; case WORK_TAGS = "/work/tags"; case WORK_ACTIONS = "/work/actions"; + case WORK_TIMELINE = "/work/timeline"; case BATTLESTATION = "/battlestation"; case ABOUT_LANGUAGES = "/about/languages"; case BATTLESTATION_MB = "/battlestation/mb"; diff --git a/src/Consts.php b/src/Consts.php new file mode 100644 index 0000000..615e2f5 --- /dev/null +++ b/src/Consts.php @@ -0,0 +1,28 @@ +db = new MySQL( - $_ENV["api_database"]["host"], - $_ENV["api_database"]["user"], - $_ENV["api_database"]["pass"], - $_ENV["api_database"]["db"], + $_ENV["server_database"]["host"], + $_ENV["server_database"]["user"], + $_ENV["server_database"]["pass"], + $_ENV["server_database"]["db"], ); } // Return all rows from a table using $_GET paramters as the WHERE clause as a Reflect\Response public function list(string $table, array $columns, array $order = null): Response { - $resp = $this->for($table)->where($_GET); + $resp = $this->db->for($table)->where(array_intersect_key($_GET, array_flip($columns))); // Optionally order rows by columns if ($order) { diff --git a/src/Database/Models/About/Language.php b/src/Database/Models/About/Language.php new file mode 100644 index 0000000..f264b54 --- /dev/null +++ b/src/Database/Models/About/Language.php @@ -0,0 +1,30 @@ +value => $this->id + ]); + } + + public static function all(array $params = []): array { + return array_map(fn(array $item): Language => new Language($item[LanguagesTable::ID->value]), parent::list(Endpoints::ABOUT_LANGUAGES, $params)); + } + + public function bytes(): int { + return $this->get(LanguagesTable::BYTES->value); + } + } \ No newline at end of file diff --git a/src/Database/Models/Model.php b/src/Database/Models/Model.php index 79327cf..5bcc1a7 100644 --- a/src/Database/Models/Model.php +++ b/src/Database/Models/Model.php @@ -10,13 +10,17 @@ require_once VV::root("src/API/Endpoints.php"); abstract class Model { - private readonly array $row; + abstract public static function all(array $params = []): array; + + private array $row; public function __construct( - public readonly Endpoints $endpoint, + public readonly ?Endpoints $endpoint = null, private readonly array $params = [] ) { - $this->row = self::first(self::list($endpoint, $params)); + if ($this->endpoint) { + $this->assign(self::first(self::list($endpoint, $params))); + } } public static function first(array $array): array { @@ -24,11 +28,16 @@ } public static function list(Endpoints $endpoint, array $params = []): array { - $resp = (new API())->call($endpoint->value)->params($params)->get(); + $resp = (new Client())->call($endpoint->value)->params($params)->get(); return $resp->ok ? $resp->json() : []; } + public function assign(array $row): self { + $this->row = $row; + return $this; + } + public function get(string $key): mixed { - return $this->data[$key] ?? null; + return $this->row[$key] ?? null; } } \ No newline at end of file diff --git a/src/Database/Models/Work/Action.php b/src/Database/Models/Work/Action.php new file mode 100644 index 0000000..fa1afb2 --- /dev/null +++ b/src/Database/Models/Work/Action.php @@ -0,0 +1,51 @@ + (new Action())->assign($item), parent::list(Endpoints::WORK_ACTIONS, $params)); + } + + public static function from(Work $work): array { + return self::all([ + ActionsTable::REF_WORK_ID->value => $work->id + ]); + } + + public function icon_prepended(): ?string { + return $this->get(ActionsTable::ICON_PREPENDED->value); + } + + public function icon_appended(): ?string { + return $this->get(ActionsTable::ICON_APPENDED->value); + } + + public function display_text(): string { + return $this->get(ActionsTable::DISPLAY_TEXT->value); + } + + public function href(): ?string { + return $this->get(ActionsTable::HREF->value); + } + + public function classes(): array { + return $this->get(ActionsTable::CLASS_LIST->value) ? explode(",", $this->get(ActionsTable::CLASS_LIST->value)) : []; + } + } \ No newline at end of file diff --git a/src/Database/Models/Work/Tag.php b/src/Database/Models/Work/Tag.php new file mode 100644 index 0000000..83df2ed --- /dev/null +++ b/src/Database/Models/Work/Tag.php @@ -0,0 +1,35 @@ + (new Tag())->assign($item), parent::list(Endpoints::WORK_TAGS, $params)); + } + + public static function from(Work $work): array { + return self::all([ + TagsTable::REF_WORK_ID->value => $work->id + ]); + } + + public function label(): TagsLabelEnum { + return TagsLabelEnum::from($this->get(TagsTable::LABEL->value)); + } + } \ No newline at end of file diff --git a/src/Database/Models/Timeline/Timeline.php b/src/Database/Models/Work/Timeline.php similarity index 61% rename from src/Database/Models/Timeline/Timeline.php rename to src/Database/Models/Work/Timeline.php index 02ab358..08c5613 100644 --- a/src/Database/Models/Timeline/Timeline.php +++ b/src/Database/Models/Work/Timeline.php @@ -1,26 +1,28 @@ value => $this->id + parent::__construct(Endpoints::WORK_TIMELINE, [ + TimelineTable::REF_WORK_ID->value => $this->id ]); } - public static function list(): array { - return array_map(fn(array $item): Timeline => new Timeline($item[TimelineTable::ID->value]), parent::list(Endpoints::TIMELINE)); + public static function all(array $params = []): array { + return array_map(fn(array $item): Timeline => new Timeline($item[TimelineTable::REF_WORK_ID->value]), parent::list(Endpoints::WORK_TIMELINE, $params)); } public function work(): Work { diff --git a/src/Database/Models/Work/Work.php b/src/Database/Models/Work/Work.php index 1d33d21..c94f643 100644 --- a/src/Database/Models/Work/Work.php +++ b/src/Database/Models/Work/Work.php @@ -2,4 +2,47 @@ namespace VLW\Database\Models\Work; - class Work \ No newline at end of file + use \VV; + + use VLW\API\Endpoints; + use VLW\Database\Models\Model; + use VLW\Database\Tables\Work\WorkTable; + use VLW\Database\Models\Work\{Tag, Action}; + + require_once VV::root("src/API/Endpoints.php"); + require_once VV::root("src/Database/Models/Model.php"); + require_once VV::root("src/Database/Models/Work/Tag.php"); + require_once VV::root("src/Database/Tables/Work/Work.php"); + require_once VV::root("src/Database/Models/Work/Action.php"); + + class Work extends Model { + public function __construct(public readonly string $id) { + parent::__construct(Endpoints::WORK, [ + WorkTable::ID->value => $this->id + ]); + } + + public static function all(array $params = []): array { + return array_map(fn(array $item): Work => new Work($item[WorkTable::ID->value]), parent::list(Endpoints::WORK, $params)); + } + + public function title(): ?string { + return $this->get(WorkTable::TITLE->value); + } + + public function summary(): string { + return $this->get(WorkTable::SUMMARY->value); + } + + public function created(): DateTimeImmutable { + return new DateTimeImmutable($this->get(WorkTable::CREATED->value)); + } + + public function tags(): array { + return Tag::from($this); + } + + public function actions(): array { + return Action::from($this); + } + } \ No newline at end of file diff --git a/src/Database/Tables/About/Languages.php b/src/Database/Tables/About/Languages.php new file mode 100644 index 0000000..54f4566 --- /dev/null +++ b/src/Database/Tables/About/Languages.php @@ -0,0 +1,14 @@ +