From a0eba92cc7fe296359c46b495a5dca0b84c17a16 Mon Sep 17 00:00:00 2001 From: d47081 Date: Thu, 27 Oct 2022 15:30:31 +0300 Subject: [PATCH] implement optional trends block --- config-default.php | 5 +++++ library/sqlite.php | 34 ++++++++++++++++++++++++++++++++++ public/index.php | 42 ++++++++++++++++++++++++++++++++++++++++++ public/index.phtml | 12 ++++++++++++ 4 files changed, 93 insertions(+) diff --git a/config-default.php b/config-default.php index d43ae2a..e6bb98d 100644 --- a/config-default.php +++ b/config-default.php @@ -10,6 +10,11 @@ define('BASE_URL', 'https://kvazar.today/'); define('PAGE_LIMIT', 10); define('CACHE_ENABLED', false); +define('TRENDS_ENABLED', false); // alpha +define('TRENDS_SECONDS_OFFSET', 2592000); +define('TRENDS_MIN_LENGHT', 4); +define('TRENDS_LIMIT', 40); + // Database define('DB_NAME', '../kvazar.sqlite'); define('DB_USERNAME', ''); diff --git a/library/sqlite.php b/library/sqlite.php index 76e054c..fa4aaf1 100644 --- a/library/sqlite.php +++ b/library/sqlite.php @@ -231,4 +231,38 @@ class SQLite { return false; } } + + public function getTrends(int $offset = 0) { + + try { + + $query = $this->_db->prepare('SELECT `block`.`blockId` AS `block`, + `namespace`.`hash` AS `namehash`, + `data`.`time` AS `time`, + `data`.`key` AS `key`, + `data`.`value` AS `value`, + `data`.`txid` AS `txid` + + FROM `data` + JOIN `block` ON (`block`.`blockId` = `data`.`blockId`) + JOIN `namespace` ON (`namespace`.`nameSpaceId` = `data`.`nameSpaceId`) + + WHERE `data`.`ns` = "0" + AND `data`.`time` >= ' . (int) $offset . ' + -- AND `data`.`deleted` = "0" -- + + ORDER BY `block`.`blockId` DESC'); + + $query->execute(); + + $result = $query->fetchAll(); + + return $result ? $result : []; + + } catch(PDOException $e) { + + trigger_error($e->getMessage()); + return false; + } + } } diff --git a/public/index.php b/public/index.php index dc28e30..d3cd76a 100755 --- a/public/index.php +++ b/public/index.php @@ -61,6 +61,48 @@ if ($ns) { $namespaceValue = false; } +$trends = []; + +if (TRENDS_ENABLED) { + + foreach ($db->getTrends(time() - TRENDS_SECONDS_OFFSET) as $value) { + + foreach ((array) explode(' ', strip_tags(html_entity_decode(nl2br(trim($value['key']))))) as $trend) { + + if (strlen($trend) >= TRENDS_MIN_LENGHT) { + + $trend = strtolower($trend); + + if (isset($trends[$trend])) { + $trends[$trend]++; + } else { + $trends[$trend] = 1; + } + } + } + + foreach ((array) explode(' ', strip_tags(html_entity_decode(nl2br(trim($value['value']))))) as $trend) { + + if (strlen($trend) >= TRENDS_MIN_LENGHT) { + + $trend = strtolower($trend); + + if (isset($trends[$trend])) { + $trends[$trend]++; + } else { + $trends[$trend] = 1; + } + } + } + } + + arsort($trends); + + $trends = array_slice($trends, 0, TRENDS_LIMIT); + + $trends = array_flip($trends); +} + $data = []; foreach ($db->getData($ns, $tx, $query, $limit, PAGE_LIMIT) as $value) { $data[] = [ diff --git a/public/index.phtml b/public/index.phtml index 11d126c..5bdd177 100755 --- a/public/index.phtml +++ b/public/index.phtml @@ -44,6 +44,18 @@
+ +
+
+ + # + +
+
+
+
+
+