From 30a7f69083efc7f1c24a916bf69f97698a7434b7 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 5 May 2024 01:11:32 +0300 Subject: [PATCH] implement channel alias --- config/example.json | 2 ++ src/Controller/Server/Nex.php | 24 +++++++++++++++---- src/Model/Database.php | 45 +++++++++++++++++++++++++---------- src/crawler.php | 1 + 4 files changed, 54 insertions(+), 18 deletions(-) diff --git a/config/example.json b/config/example.json index ba1fa01..e4da1f8 100644 --- a/config/example.json +++ b/config/example.json @@ -62,6 +62,7 @@ [ { "source":"https://www.omglinux.com/feed", + "alias":"omglinux.gmi", "enabled":true, "item": { @@ -93,6 +94,7 @@ }, { "source":"https://omgubuntu.co.uk/feed", + "alias":"omgubuntu.gmi", "enabled":false, "item": { diff --git a/src/Controller/Server/Nex.php b/src/Controller/Server/Nex.php index 2db87f9..5f1c5fc 100644 --- a/src/Controller/Server/Nex.php +++ b/src/Controller/Server/Nex.php @@ -123,10 +123,22 @@ class Nex implements MessageComponentInterface $lines[] = sprintf( '=> %s', $channelItem->link - ) . PHP_EOL; + ); } } + // Get channel info + if ($channel = $this->_database->getChannel($channelItem->channelId)) + { + $lines[] = sprintf( + '=> /%s %s', + urlencode( + $channel->alias + ), + $channel->title + ); + } + // Build response $response = implode( PHP_EOL, @@ -136,12 +148,12 @@ class Nex implements MessageComponentInterface break; // Chanel - case (bool) preg_match('/^\/(?\d+)\/($|index\.gmi)$/i', $request, $attribute): + case (bool) preg_match('/^\/(?.+)$/i', $request, $attribute): $lines = []; // Get channel info - if ($channel = $this->_database->getChannel($attribute['id'])) + if ($channel = $this->_database->getChannelByAlias($attribute['alias'])) { if ($channel->title) { @@ -208,8 +220,10 @@ class Nex implements MessageComponentInterface foreach ((array) $this->_database->getChannels() as $channel) { $lines[] = sprintf( - '=> /%d/index.gmi %s', - $channel->id, + '=> /%s %s', + urlencode( + $channel->alias + ), $channel->title ); } diff --git a/src/Model/Database.php b/src/Model/Database.php index 2ae7b1b..80b793b 100644 --- a/src/Model/Database.php +++ b/src/Model/Database.php @@ -16,17 +16,15 @@ class Database $this->_database = new \PDO( sprintf( 'sqlite:%s', - realpath( - str_starts_with( - $database, - DIRECTORY_SEPARATOR - ) ? $database - : __DIR__ . - DIRECTORY_SEPARATOR . '..'. - DIRECTORY_SEPARATOR . '..'. - DIRECTORY_SEPARATOR . 'config'. - DIRECTORY_SEPARATOR . $database - ) + str_starts_with( + $database, + DIRECTORY_SEPARATOR + ) ? $database + : __DIR__ . + DIRECTORY_SEPARATOR . '..'. + DIRECTORY_SEPARATOR . '..'. + DIRECTORY_SEPARATOR . 'config'. + DIRECTORY_SEPARATOR . $database ), $username, $password @@ -47,6 +45,7 @@ class Database ( "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "time" INTEGER NOT NULL, + "alias" VARCHAR NOT NULL, "source" TEXT NOT NULL, "link" TEXT, "title" TEXT, @@ -102,6 +101,24 @@ class Database return null; } + public function getChannelByAlias( + string $alias + ): ?object + { + $query = $this->_database->prepare( + 'SELECT * FROM `channel` WHERE `alias` LIKE ? LIMIT 1' + ); + + $query->execute([$alias]); + + if ($result = $query->fetch()) + { + return $result; + } + + return null; + } + public function getChannelIdBySource( string $source ): ?int @@ -125,6 +142,7 @@ class Database } public function addChannel( + string $alias, string $source, ?string $link, ?string $title, @@ -133,12 +151,13 @@ class Database ): ?int { $query = $this->_database->prepare( - 'INSERT INTO `channel` (`source`, `link`, `title`, `description`, `time`) - VALUES (:source, :link, :title, :description, :time)' + 'INSERT INTO `channel` (`alias`, `source`, `link`, `title`, `description`, `time`) + VALUES (:alias, :source, :link, :title, :description, :time)' ); $query->execute( [ + ':alias' => $alias, ':source' => $source, ':link' => $link, ':title' => $title, diff --git a/src/crawler.php b/src/crawler.php index d38b60f..1593bc5 100644 --- a/src/crawler.php +++ b/src/crawler.php @@ -72,6 +72,7 @@ foreach ($config->crawler->channel as $channel) { // Create new one if not exists $channelId = $database->addChannel( + $channel->alias, $channel->source, isset($remoteChannel->link) ? (string) $remoteChannel->link : null, isset($remoteChannel->title) ? (string) $remoteChannel->title : null,