From 10534df0697c05e9fb5f2df462a9626d2e4357e5 Mon Sep 17 00:00:00 2001 From: yggverse Date: Fri, 2 Aug 2024 22:00:28 +0300 Subject: [PATCH] implement separated models --- src/Abstract/Model/Connection.php | 2 +- src/Entity/Browser.php | 6 +- .../Browser/Bookmark/Container/Content.php | 2 +- .../Bookmark/Container/Navbar/Delete.php | 2 +- src/Entity/Browser/Container/Page.php | 2 +- .../Container/Page/Navbar/Bookmark.php | 4 +- .../Page/Navbar/Request/Completion.php | 4 +- src/Entity/Browser/Container/Tab.php | 2 +- .../Browser/History/Container/Content.php | 2 +- .../History/Container/Navbar/Delete.php | 2 +- src/Model/Database.php | 538 ++---------------- src/Model/Database/Auth.php | 31 + src/Model/Database/Bookmark.php | 155 +++++ src/Model/Database/Cache.php | 173 ++++++ src/Model/Database/History.php | 142 +++++ src/Model/Database/Identity.php | 32 ++ src/Model/Database/Session.php | 73 +++ 17 files changed, 657 insertions(+), 515 deletions(-) create mode 100644 src/Model/Database/Auth.php create mode 100644 src/Model/Database/Bookmark.php create mode 100644 src/Model/Database/Cache.php create mode 100644 src/Model/Database/History.php create mode 100644 src/Model/Database/Identity.php create mode 100644 src/Model/Database/Session.php diff --git a/src/Abstract/Model/Connection.php b/src/Abstract/Model/Connection.php index a5d52290..176d75b6 100644 --- a/src/Abstract/Model/Connection.php +++ b/src/Abstract/Model/Connection.php @@ -239,7 +239,7 @@ abstract class Connection implements \Yggverse\Yoda\Interface\Model\Connection string $request ): ?object { - return $this->_database->getCache( + return $this->_database->cache->get( $request ); } diff --git a/src/Entity/Browser.php b/src/Entity/Browser.php index 22347571..505311f3 100644 --- a/src/Entity/Browser.php +++ b/src/Entity/Browser.php @@ -88,19 +88,19 @@ class Browser if ($pid === 0) { // Reset previous records - $this->database->cleanSession(); + $this->database->session->clean(); foreach ($this->container->tab->pages as $page) { // Save page session data - $this->database->addSession( + $this->database->session->add( $page->navbar->request->getValue() ); // Cache connection pool data if ($page->connection) { - $this->database->renewCache( + $this->database->cache->renew( $page->navbar->request->getValue(), $page->connection->getMime(), $page->connection->getTitle(), diff --git a/src/Entity/Browser/Bookmark/Container/Content.php b/src/Entity/Browser/Bookmark/Container/Content.php index e00bec29..1946b295 100644 --- a/src/Entity/Browser/Bookmark/Container/Content.php +++ b/src/Entity/Browser/Bookmark/Container/Content.php @@ -76,7 +76,7 @@ class Content { $this->table->data->clear(); - if ($records = $this->container->bookmark->browser->database->findBookmark($filter)) + if ($records = $this->container->bookmark->browser->database->bookmark->find($filter)) { foreach ($records as $record) { diff --git a/src/Entity/Browser/Bookmark/Container/Navbar/Delete.php b/src/Entity/Browser/Bookmark/Container/Navbar/Delete.php index 305f4b99..e04cd753 100644 --- a/src/Entity/Browser/Bookmark/Container/Navbar/Delete.php +++ b/src/Entity/Browser/Bookmark/Container/Navbar/Delete.php @@ -20,7 +20,7 @@ class Delete extends Button { if ($id = $this->navbar->container->content->table->getSelectedId()) { - $this->navbar->container->bookmark->browser->database->deleteBookmark( + $this->navbar->container->bookmark->browser->database->bookmark->delete( $id ); } diff --git a/src/Entity/Browser/Container/Page.php b/src/Entity/Browser/Container/Page.php index 6f2916a4..941171e4 100644 --- a/src/Entity/Browser/Container/Page.php +++ b/src/Entity/Browser/Container/Page.php @@ -279,7 +279,7 @@ class Page if ($pid === 0) { - $this->container->browser->database->renewHistory( + $this->container->browser->database->history->renew( $this->navbar->request->getValue(), $this->title->getValue() ); diff --git a/src/Entity/Browser/Container/Page/Navbar/Bookmark.php b/src/Entity/Browser/Container/Page/Navbar/Bookmark.php index a49f9f15..74393190 100644 --- a/src/Entity/Browser/Container/Page/Navbar/Bookmark.php +++ b/src/Entity/Browser/Container/Page/Navbar/Bookmark.php @@ -25,7 +25,7 @@ class Bookmark extends Button ): void { $this->setImage( - $this->navbar->page->container->browser->database->toggleBookmark( + $this->navbar->page->container->browser->database->bookmark->toggle( $this->navbar->request->getValue(), $this->navbar->page->title->getValue() ) ? self::_IMAGE_STARRED_YES : self::_IMAGE_STARRED_NON @@ -35,7 +35,7 @@ class Bookmark extends Button public function refresh(): void { $this->setImage( - $this->navbar->page->container->browser->database->getBookmark( + $this->navbar->page->container->browser->database->bookmark->get( $this->navbar->request->getValue() ) ? self::_IMAGE_STARRED_YES : self::_IMAGE_STARRED_NON ); diff --git a/src/Entity/Browser/Container/Page/Navbar/Request/Completion.php b/src/Entity/Browser/Container/Page/Navbar/Request/Completion.php index 9e81cd46..064f38e7 100644 --- a/src/Entity/Browser/Container/Page/Navbar/Request/Completion.php +++ b/src/Entity/Browser/Container/Page/Navbar/Request/Completion.php @@ -68,7 +68,7 @@ class Completion { $this->suggestion->clear(); - foreach ($this->request->navbar->page->container->browser->database->findBookmark( + foreach ($this->request->navbar->page->container->browser->database->bookmark->find( $this->request->getValue(), $offset, $limit @@ -77,7 +77,7 @@ class Completion $suggestions[] = $history->request; } - foreach ($this->request->navbar->page->container->browser->database->findHistory( + foreach ($this->request->navbar->page->container->browser->database->history->find( $this->request->getValue(), $offset, $limit diff --git a/src/Entity/Browser/Container/Tab.php b/src/Entity/Browser/Container/Tab.php index ca932af8..b6b56b08 100644 --- a/src/Entity/Browser/Container/Tab.php +++ b/src/Entity/Browser/Container/Tab.php @@ -42,7 +42,7 @@ class Tab ); // Restore previous session - foreach ($this->container->browser->database->getSession() as $session) + foreach ($this->container->browser->database->session->get() as $session) { $this->append( $session->request, diff --git a/src/Entity/Browser/History/Container/Content.php b/src/Entity/Browser/History/Container/Content.php index 12b68d28..d21f187c 100644 --- a/src/Entity/Browser/History/Container/Content.php +++ b/src/Entity/Browser/History/Container/Content.php @@ -76,7 +76,7 @@ class Content { $this->table->data->clear(); - if ($records = $this->container->history->browser->database->findHistory($filter)) + if ($records = $this->container->history->browser->database->history->find($filter)) { foreach ($records as $record) { diff --git a/src/Entity/Browser/History/Container/Navbar/Delete.php b/src/Entity/Browser/History/Container/Navbar/Delete.php index d51b0dc5..18e8b34f 100644 --- a/src/Entity/Browser/History/Container/Navbar/Delete.php +++ b/src/Entity/Browser/History/Container/Navbar/Delete.php @@ -20,7 +20,7 @@ class Delete extends Button { if ($id = $this->navbar->container->content->table->getSelectedId()) { - $this->navbar->container->history->browser->database->deleteHistory( + $this->navbar->container->history->browser->database->history->delete( $id ); } diff --git a/src/Model/Database.php b/src/Model/Database.php index 616ef300..1fd83da7 100644 --- a/src/Model/Database.php +++ b/src/Model/Database.php @@ -4,13 +4,20 @@ declare(strict_types=1); namespace Yggverse\Yoda\Model; -use \PDO; +use \Pdo; class Database { - private PDO $_connection; + // Dependencies + public Pdo $connection; - private bool $_exists; + // Requirements + public Database\Auth $auth; + public Database\Bookmark $bookmark; + public Database\Cache $cache; + public Database\History $history; + public Database\Identity $identity; + public Database\Session $session; public function __construct( string $filename, @@ -18,12 +25,12 @@ class Database ?string $password = null ) { // Status - $this->_exists = file_exists( + $exists = file_exists( $filename ); - // Init database connection - $this->_connection = new PDO( + // Init dependencies + $this->connection = new Pdo( sprintf( 'sqlite:%s', $filename @@ -32,519 +39,48 @@ class Database $password ); - $this->_connection->setAttribute( - PDO::ATTR_ERRMODE, - PDO::ERRMODE_EXCEPTION + $this->connection->setAttribute( + Pdo::ATTR_ERRMODE, + Pdo::ERRMODE_EXCEPTION ); - $this->_connection->setAttribute( - PDO::ATTR_DEFAULT_FETCH_MODE, - PDO::FETCH_OBJ + $this->connection->setAttribute( + Pdo::ATTR_DEFAULT_FETCH_MODE, + Pdo::FETCH_OBJ ); - // Init tables - $this->_connection->query(' - CREATE TABLE IF NOT EXISTS `auth` - ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - `time` INTEGER NOT NULL, - `active` INTEGER NOT NULL, - `identity` INTEGER NOT NULL, - `request` VARCHAR(1024) NOT NULL - ) - '); - - $this->_connection->query(' - CREATE TABLE IF NOT EXISTS `bookmark` - ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - `time` INTEGER NOT NULL, - `request` VARCHAR(1024) UNIQUE, - `title` VARCHAR(255) - ) - '); - - $this->_connection->query(' - CREATE TABLE IF NOT EXISTS `cache` - ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - `time` INTEGER NOT NULL, - `request` VARCHAR(1024) UNIQUE, - `mime` VARCHAR(255), - `title` VARCHAR(255), - `subtitle` VARCHAR(255), - `tooltip` VARCHAR(255), - `data` BLOB - ); - '); - - $this->_connection->query(' - CREATE TABLE IF NOT EXISTS `history` - ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - `time` INTEGER NOT NULL, - `url` VARCHAR(1024) NOT NULL, - `title` VARCHAR(255) - ) - '); - - $this->_connection->query(' - CREATE TABLE IF NOT EXISTS `identity` - ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - `time` INTEGER NOT NULL, - `active` INTEGER NOT NULL, - `name` VARCHAR(255), - `crt` TEXT NOT NULL, - `key` TEXT NOT NULL - ) - '); - - $this->_connection->query(' - CREATE TABLE IF NOT EXISTS `session` - ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - `time` INTEGER NOT NULL, - `request` VARCHAR(1024) - ); - '); - - // Initial setup - if (!$this->_exists) - { - // Add gemini protocol homepage - $this->addSession( - 'gemini://geminiprotocol.net/' - ); - - // Add yggverse homepage - $this->addSession( - 'gemini://yggverse.cities.yesterweb.org/' - ); - } - } - - // Bookmark - public function addBookmark( - ?string $request = null, - ?string $title = null, - ?int $time = null - ): int - { - $query = $this->_connection->prepare( - 'INSERT INTO `bookmark` ( - `time`, - `request`, - `title` - ) VALUES ( - :time, - :request, - :title - )' - ); - - $query->execute( - [ - ':time' => $time ? $time : time(), - ':request' => $request, - ':title' => $title - ] - ); - - return intval( - $this->_connection->lastInsertId() - ); - } - - public function getBookmark( - ?string $request = null - ): ?object - { - $query = $this->_connection->prepare( - 'SELECT * FROM `bookmark` WHERE `request` LIKE :request' - ); - - $query->execute( - [ - ':request' => $request - ] - ); - - if ($record = $query->fetch()) - { - return $record; - } - - return null; - } - - public function findBookmark( - ?string $value = null, - int $start = 0, - int $limit = 1000 - ): array - { - $query = $this->_connection->prepare( - sprintf( - 'SELECT * FROM `bookmark` - WHERE `request` LIKE :value OR `title` LIKE :value - ORDER BY `id` DESC - LIMIT %d,%d', - $start, - $limit - ) - ); - - $query->execute( - [ - ':value' => sprintf( - '%%%s%%', - strval( - $value - ) - ) - ] - ); - - return $query->fetchAll(); - } - - public function deleteBookmark( - int $id - ): int - { - $query = $this->_connection->query( - sprintf( - 'DELETE FROM `bookmark` WHERE `id` = %d', - $id - ) - ); - - return $query->rowCount(); - } - - public function toggleBookmark( - ?string $request = null, - ?string $title = null, - ?int $time = null - ): bool - { - if ($record = $this->getBookmark($request)) - { - $this->deleteBookmark( - $record->id - ); - - return false; - } - - else - { - $this->addBookmark( - $request, - $title, - $time - ); - - return true; - } - } - - // Cache - public function addCache( - ?string $request = null, - ?string $mime = null, - ?string $title = null, - ?string $subtitle = null, - ?string $tooltip = null, - ?string $data = null, - ?int $time = null - ): int - { - $query = $this->_connection->prepare( - 'INSERT INTO `cache` ( - `time`, - `request`, - `mime`, - `title`, - `subtitle`, - `tooltip`, - `data` - ) VALUES ( - :time, - :request, - :mime, - :title, - :subtitle, - :tooltip, - :data - )' - ); - - $query->execute( - [ - ':time' => $time ? $time : time(), - ':request' => $request, - ':mime' => $mime, - ':title' => $title, - ':subtitle' => $subtitle, - ':tooltip' => $tooltip, - ':data' => $data - ] - ); - - return intval( - $this->_connection->lastInsertId() - ); - } - - public function getCache( - string $request = '' - ): ?object - { - $query = $this->_connection->prepare( - 'SELECT * FROM `cache` WHERE `request` LIKE :request' - ); - - $query->execute( - [ - ':request' => $request - ] - ); - - if ($cache = $query->fetch()) - { - return $cache; - } - - return null; - } - - public function deleteCache( - int $id - ): int - { - $query = $this->_connection->query( - sprintf( - 'DELETE FROM `cache` WHERE `id` = %d', - $id - ) + // Init requirements + $this->auth = new Database\Auth( + $this->connection ); - return $query->rowCount(); - } - - public function cleanCache( - int $timeout = 0 - ): int - { - $query = $this->_connection->query( - sprintf( - 'DELETE FROM `cache` WHERE `time` + %d < %d', - $timeout, - time() - ) + $this->bookmark = new Database\Bookmark( + $this->connection ); - return $query->rowCount(); - } - - public function renewCache( - string $request, - ?string $mime = null, - ?string $title = null, - ?string $subtitle = null, - ?string $tooltip = null, - ?string $data = null, - ?int $time = null - ): void - { - // Find same records match URL - $query = $this->_connection->prepare( - 'SELECT * FROM `cache` WHERE `request` LIKE :request' + $this->cache = new Database\Cache( + $this->connection ); - $query->execute( - [ - ':request' => $request - ] + $this->history = new Database\History( + $this->connection ); - // Drop previous records - foreach ($query->fetchAll() as $record) - { - $this->deleteCache( - $record->id - ); - } - - // Add new record - $this->addCache( - $request, - $mime, - $title, - $subtitle, - $tooltip, - $data, - $time + $this->identity = new Database\Identity( + $this->connection ); - } - // History - public function addHistory( - string $url, - ?string $title = null - ): int - { - $query = $this->_connection->prepare( - 'INSERT INTO `history` (`time`, `url`, `title`) VALUES (:time, :url, :title)' + $this->session = new Database\Session( + $this->connection ); - $query->execute( - [ - ':time' => time(), - ':url' => $url, - ':title' => $title - ] - ); - - return intval( - $this->_connection->lastInsertId() - ); - } - - public function findHistory( - string $value = '', - int $start = 0, - int $limit = 1000 - ): array - { - $query = $this->_connection->prepare( - sprintf( - 'SELECT * FROM `history` - WHERE `url` LIKE :value OR `title` LIKE :value - ORDER BY `id` DESC - LIMIT %d,%d', - $start, - $limit - ) - ); - - $query->execute( - [ - ':value' => sprintf( - '%%%s%%', - $value - ) - ] - ); - - return $query->fetchAll(); - } - - public function deleteHistory( - int $id - ): int - { - $query = $this->_connection->query( - sprintf( - 'DELETE FROM `history` WHERE `id` = %d', - $id - ) - ); - - return $query->rowCount(); - } - - public function cleanHistory( - int $timeout = 0 - ): int - { - $query = $this->_connection->query( - sprintf( - 'DELETE FROM `history` WHERE `time` + %d < %d', - $timeout, - time() - ) - - ); - - return $query->rowCount(); - } - - public function renewHistory( - string $url, - ?string $title = null - ): void - { - // Find same records match URL - $query = $this->_connection->prepare( - 'SELECT * FROM `history` WHERE `url` LIKE :url' - ); - - $query->execute( - [ - ':url' => $url - ] - ); - - // Drop previous records - foreach ($query->fetchAll() as $record) + // Init data + if (!$exists) { - $this->deleteHistory( - $record->id + // Open yggverse homepage + $this->session->add( + 'gemini://yggverse.cities.yesterweb.org/' ); } - - // Add new record - $this->addHistory( - $url, - $title - ); - } - - // Session - public function addSession( - ?string $request = null, - ?int $time = null - ): int - { - $query = $this->_connection->prepare( - 'INSERT INTO `session` (`time`, `request`) VALUES (:time, :request)' - ); - - $query->execute( - [ - ':time' => $time ? $time : time(), - ':request' => $request - ] - ); - - return intval( - $this->_connection->lastInsertId() - ); - } - - public function getSession(): array - { - $query = $this->_connection->query( - 'SELECT * FROM `session`' - ); - - if ($session = $query->fetchAll()) - { - return $session; - } - - return []; - } - - public function cleanSession(): int - { - $query = $this->_connection->query( - 'DELETE FROM `session`' - ); - - return $query->rowCount(); } } \ No newline at end of file diff --git a/src/Model/Database/Auth.php b/src/Model/Database/Auth.php new file mode 100644 index 00000000..94b91c11 --- /dev/null +++ b/src/Model/Database/Auth.php @@ -0,0 +1,31 @@ +connection = $connection; + + // Init database structure + $this->connection->query(' + CREATE TABLE IF NOT EXISTS `auth` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `time` INTEGER NOT NULL, + `active` INTEGER NOT NULL, + `identity` INTEGER NOT NULL, + `request` VARCHAR(1024) NOT NULL + ) + '); + } +} \ No newline at end of file diff --git a/src/Model/Database/Bookmark.php b/src/Model/Database/Bookmark.php new file mode 100644 index 00000000..8e94cb3b --- /dev/null +++ b/src/Model/Database/Bookmark.php @@ -0,0 +1,155 @@ +connection = $connection; + + // Init database structure + $this->connection->query(' + CREATE TABLE IF NOT EXISTS `bookmark` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `time` INTEGER NOT NULL, + `request` VARCHAR(1024) UNIQUE, + `title` VARCHAR(255) + ) + '); + } + + public function add( + ?string $request = null, + ?string $title = null, + ?int $time = null + ): int + { + $query = $this->connection->prepare( + 'INSERT INTO `bookmark` ( + `time`, + `request`, + `title` + ) VALUES ( + :time, + :request, + :title + )' + ); + + $query->execute( + [ + ':time' => $time ? $time : time(), + ':request' => $request, + ':title' => $title + ] + ); + + return intval( + $this->connection->lastInsertId() + ); + } + + public function get( + ?string $request = null + ): ?object + { + $query = $this->connection->prepare( + 'SELECT * FROM `bookmark` WHERE `request` LIKE :request' + ); + + $query->execute( + [ + ':request' => $request + ] + ); + + if ($record = $query->fetch()) + { + return $record; + } + + return null; + } + + public function find( + ?string $value = null, + int $start = 0, + int $limit = 1000 + ): array + { + $query = $this->connection->prepare( + sprintf( + 'SELECT * FROM `bookmark` + WHERE `request` LIKE :value OR `title` LIKE :value + ORDER BY `id` DESC + LIMIT %d,%d', + $start, + $limit + ) + ); + + $query->execute( + [ + ':value' => sprintf( + '%%%s%%', + strval( + $value + ) + ) + ] + ); + + return $query->fetchAll(); + } + + public function delete( + int $id + ): int + { + $query = $this->connection->query( + sprintf( + 'DELETE FROM `bookmark` WHERE `id` = %d', + $id + ) + ); + + return $query->rowCount(); + } + + public function toggle( + ?string $request = null, + ?string $title = null, + ?int $time = null + ): bool + { + if ($record = $this->get($request)) + { + $this->delete( + $record->id + ); + + return false; + } + + else + { + $this->add( + $request, + $title, + $time + ); + + return true; + } + } +} \ No newline at end of file diff --git a/src/Model/Database/Cache.php b/src/Model/Database/Cache.php new file mode 100644 index 00000000..c180a5c9 --- /dev/null +++ b/src/Model/Database/Cache.php @@ -0,0 +1,173 @@ +connection = $connection; + + // Init database structure + $this->connection->query(' + CREATE TABLE IF NOT EXISTS `cache` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `time` INTEGER NOT NULL, + `request` VARCHAR(1024) UNIQUE, + `mime` VARCHAR(255), + `title` VARCHAR(255), + `subtitle` VARCHAR(255), + `tooltip` VARCHAR(255), + `data` BLOB + ) + '); + } + + public function add( + ?string $request = null, + ?string $mime = null, + ?string $title = null, + ?string $subtitle = null, + ?string $tooltip = null, + ?string $data = null, + ?int $time = null + ): int + { + $query = $this->connection->prepare( + 'INSERT INTO `cache` ( + `time`, + `request`, + `mime`, + `title`, + `subtitle`, + `tooltip`, + `data` + ) VALUES ( + :time, + :request, + :mime, + :title, + :subtitle, + :tooltip, + :data + )' + ); + + $query->execute( + [ + ':time' => $time ? $time : time(), + ':request' => $request, + ':mime' => $mime, + ':title' => $title, + ':subtitle' => $subtitle, + ':tooltip' => $tooltip, + ':data' => $data + ] + ); + + return intval( + $this->connection->lastInsertId() + ); + } + + public function get( + string $request = '' + ): ?object + { + $query = $this->connection->prepare( + 'SELECT * FROM `cache` WHERE `request` LIKE :request' + ); + + $query->execute( + [ + ':request' => $request + ] + ); + + if ($cache = $query->fetch()) + { + return $cache; + } + + return null; + } + + public function delete( + int $id + ): int + { + $query = $this->connection->query( + sprintf( + 'DELETE FROM `cache` WHERE `id` = %d', + $id + ) + ); + + return $query->rowCount(); + } + + public function clean( + int $timeout = 0 + ): int + { + $query = $this->connection->query( + sprintf( + 'DELETE FROM `cache` WHERE `time` + %d < %d', + $timeout, + time() + ) + ); + + return $query->rowCount(); + } + + public function renew( + string $request, + ?string $mime = null, + ?string $title = null, + ?string $subtitle = null, + ?string $tooltip = null, + ?string $data = null, + ?int $time = null + ): void + { + // Find same records match URL + $query = $this->connection->prepare( + 'SELECT * FROM `cache` WHERE `request` LIKE :request' + ); + + $query->execute( + [ + ':request' => $request + ] + ); + + // Drop previous records + foreach ($query->fetchAll() as $record) + { + $this->delete( + $record->id + ); + } + + // Add new record + $this->add( + $request, + $mime, + $title, + $subtitle, + $tooltip, + $data, + $time + ); + } +} \ No newline at end of file diff --git a/src/Model/Database/History.php b/src/Model/Database/History.php new file mode 100644 index 00000000..9a6ccd5d --- /dev/null +++ b/src/Model/Database/History.php @@ -0,0 +1,142 @@ +connection = $connection; + + // Init database structure + $this->connection->query(' + CREATE TABLE IF NOT EXISTS `history` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `time` INTEGER NOT NULL, + `url` VARCHAR(1024) NOT NULL, + `title` VARCHAR(255) + ) + '); + } + + public function add( + string $url, + ?string $title = null + ): int + { + $query = $this->connection->prepare( + 'INSERT INTO `history` (`time`, `url`, `title`) VALUES (:time, :url, :title)' + ); + + $query->execute( + [ + ':time' => time(), + ':url' => $url, + ':title' => $title + ] + ); + + return intval( + $this->connection->lastInsertId() + ); + } + + public function find( + string $value = '', + int $start = 0, + int $limit = 1000 + ): array + { + $query = $this->connection->prepare( + sprintf( + 'SELECT * FROM `history` + WHERE `url` LIKE :value OR `title` LIKE :value + ORDER BY `id` DESC + LIMIT %d,%d', + $start, + $limit + ) + ); + + $query->execute( + [ + ':value' => sprintf( + '%%%s%%', + $value + ) + ] + ); + + return $query->fetchAll(); + } + + public function delete( + int $id + ): int + { + $query = $this->connection->query( + sprintf( + 'DELETE FROM `history` WHERE `id` = %d', + $id + ) + ); + + return $query->rowCount(); + } + + public function clean( + int $timeout = 0 + ): int + { + $query = $this->connection->query( + sprintf( + 'DELETE FROM `history` WHERE `time` + %d < %d', + $timeout, + time() + ) + + ); + + return $query->rowCount(); + } + + public function renew( + string $url, + ?string $title = null + ): void + { + // Find same records match URL + $query = $this->connection->prepare( + 'SELECT * FROM `history` WHERE `url` LIKE :url' + ); + + $query->execute( + [ + ':url' => $url + ] + ); + + // Drop previous records + foreach ($query->fetchAll() as $record) + { + $this->delete( + $record->id + ); + } + + // Add new record + $this->add( + $url, + $title + ); + } +} \ No newline at end of file diff --git a/src/Model/Database/Identity.php b/src/Model/Database/Identity.php new file mode 100644 index 00000000..aadd72e4 --- /dev/null +++ b/src/Model/Database/Identity.php @@ -0,0 +1,32 @@ +connection = $connection; + + // Init database structure + $this->connection->query(' + CREATE TABLE IF NOT EXISTS `identity` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `time` INTEGER NOT NULL, + `active` INTEGER NOT NULL, + `name` VARCHAR(255), + `crt` TEXT NOT NULL, + `key` TEXT NOT NULL + ) + '); + } +} \ No newline at end of file diff --git a/src/Model/Database/Session.php b/src/Model/Database/Session.php new file mode 100644 index 00000000..92e8c1c5 --- /dev/null +++ b/src/Model/Database/Session.php @@ -0,0 +1,73 @@ +connection = $connection; + + // Init database structure + $this->connection->query(' + CREATE TABLE IF NOT EXISTS `session` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `time` INTEGER NOT NULL, + `request` VARCHAR(1024) + ) + '); + } + + public function add( + ?string $request = null, + ?int $time = null + ): int + { + $query = $this->connection->prepare( + 'INSERT INTO `session` (`time`, `request`) VALUES (:time, :request)' + ); + + $query->execute( + [ + ':time' => $time ? $time : time(), + ':request' => $request + ] + ); + + return intval( + $this->connection->lastInsertId() + ); + } + + public function get(): array + { + $query = $this->connection->query( + 'SELECT * FROM `session`' + ); + + if ($session = $query->fetchAll()) + { + return $session; + } + + return []; + } + + public function clean(): int + { + $query = $this->connection->query( + 'DELETE FROM `session`' + ); + + return $query->rowCount(); + } +} \ No newline at end of file