diff --git a/init.sql b/init.sql index 5476b34f..5731376c 100644 --- a/init.sql +++ b/init.sql @@ -2,6 +2,11 @@ create table customCategory ( id char(32) not null, content mediumtext not null, created datetime not null default CURRENT_TIMESTAMP, - lastAccess datetime not null default CURRENT_TIMESTAMP, primary key(id) ); + +create table customCategoryAccess ( + id char(32) not null, + ts datetime not null default CURRENT_TIMESTAMP, + foreign key(id) references customCategory(id) on delete cascade +); diff --git a/src/customCategory.php b/src/customCategory.php index cb3083ca..7b4b8ff6 100644 --- a/src/customCategory.php +++ b/src/customCategory.php @@ -14,10 +14,7 @@ function ajax_customCategory ($param) { $result = $row['content']; $stmt->closeCursor(); - $stmt = $db->prepare("update customCategory set lastAccess=:now where id=:id"); - $stmt->bindValue(':id', $param['id']); - $stmt->bindValue(':now', (new DateTime())->format('Y-m-d H:i:s'), PDO::PARAM_STR); - $stmt->execute(); + customCategoryUpdateAccess($param['id']); return $result; } @@ -28,12 +25,32 @@ function ajax_customCategory ($param) { if ($param['content']) { $id = md5($param['content']); - //$stmt = $db->prepare("insert into customCategory (id, content) values (:id, :content) on duplicate key update lastAccess=:now"); - $stmt = $db->prepare("insert into customCategory (id, content) values (:id, :content) on conflict(id) do update set lastAccess=:now"); + $stmt = $db->prepare("insert or ignore into customCategory (id, content) values (:id, :content)"); $stmt->bindValue(':id', $id, PDO::PARAM_STR); $stmt->bindValue(':content', $param['content'], PDO::PARAM_STR); - $stmt->bindValue(':now', (new DateTime())->format('Y-m-d H:i:s'), PDO::PARAM_STR); $result = $stmt->execute(); + + customCategoryUpdateAccess($id); + return $result; } } + +function customCategoryUpdateAccess ($id) { + global $db; + + if (!isset($_SESSION['customCategoryAccess'])) { + $_SESSION['customCategoryAccess'] = []; + } + + // update access per session only once a day + if (array_key_exists($id, $_SESSION['customCategoryAccess']) && $_SESSION['customCategoryAccess'][$id] > time() - 86400) { + return; + } + + $_SESSION['customCategoryAccess'][$id] = time(); + + $stmt = $db->prepare("insert into customCategoryAccess (id) values (:id)"); + $stmt->bindValue(':id', $id); + $stmt->execute(); +}