implement channel alias

This commit is contained in:
yggverse 2024-05-05 01:11:32 +03:00
parent 0c6e5ce8ae
commit 30a7f69083
4 changed files with 54 additions and 18 deletions

View file

@ -62,6 +62,7 @@
[ [
{ {
"source":"https://www.omglinux.com/feed", "source":"https://www.omglinux.com/feed",
"alias":"omglinux.gmi",
"enabled":true, "enabled":true,
"item": "item":
{ {
@ -93,6 +94,7 @@
}, },
{ {
"source":"https://omgubuntu.co.uk/feed", "source":"https://omgubuntu.co.uk/feed",
"alias":"omgubuntu.gmi",
"enabled":false, "enabled":false,
"item": "item":
{ {

View file

@ -123,10 +123,22 @@ class Nex implements MessageComponentInterface
$lines[] = sprintf( $lines[] = sprintf(
'=> %s', '=> %s',
$channelItem->link $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 // Build response
$response = implode( $response = implode(
PHP_EOL, PHP_EOL,
@ -136,12 +148,12 @@ class Nex implements MessageComponentInterface
break; break;
// Chanel // Chanel
case (bool) preg_match('/^\/(?<id>\d+)\/($|index\.gmi)$/i', $request, $attribute): case (bool) preg_match('/^\/(?<alias>.+)$/i', $request, $attribute):
$lines = []; $lines = [];
// Get channel info // Get channel info
if ($channel = $this->_database->getChannel($attribute['id'])) if ($channel = $this->_database->getChannelByAlias($attribute['alias']))
{ {
if ($channel->title) if ($channel->title)
{ {
@ -208,8 +220,10 @@ class Nex implements MessageComponentInterface
foreach ((array) $this->_database->getChannels() as $channel) foreach ((array) $this->_database->getChannels() as $channel)
{ {
$lines[] = sprintf( $lines[] = sprintf(
'=> /%d/index.gmi %s', '=> /%s %s',
$channel->id, urlencode(
$channel->alias
),
$channel->title $channel->title
); );
} }

View file

@ -16,7 +16,6 @@ class Database
$this->_database = new \PDO( $this->_database = new \PDO(
sprintf( sprintf(
'sqlite:%s', 'sqlite:%s',
realpath(
str_starts_with( str_starts_with(
$database, $database,
DIRECTORY_SEPARATOR DIRECTORY_SEPARATOR
@ -26,7 +25,6 @@ class Database
DIRECTORY_SEPARATOR . '..'. DIRECTORY_SEPARATOR . '..'.
DIRECTORY_SEPARATOR . 'config'. DIRECTORY_SEPARATOR . 'config'.
DIRECTORY_SEPARATOR . $database DIRECTORY_SEPARATOR . $database
)
), ),
$username, $username,
$password $password
@ -47,6 +45,7 @@ class Database
( (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"time" INTEGER NOT NULL, "time" INTEGER NOT NULL,
"alias" VARCHAR NOT NULL,
"source" TEXT NOT NULL, "source" TEXT NOT NULL,
"link" TEXT, "link" TEXT,
"title" TEXT, "title" TEXT,
@ -102,6 +101,24 @@ class Database
return null; 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( public function getChannelIdBySource(
string $source string $source
): ?int ): ?int
@ -125,6 +142,7 @@ class Database
} }
public function addChannel( public function addChannel(
string $alias,
string $source, string $source,
?string $link, ?string $link,
?string $title, ?string $title,
@ -133,12 +151,13 @@ class Database
): ?int ): ?int
{ {
$query = $this->_database->prepare( $query = $this->_database->prepare(
'INSERT INTO `channel` (`source`, `link`, `title`, `description`, `time`) 'INSERT INTO `channel` (`alias`, `source`, `link`, `title`, `description`, `time`)
VALUES (:source, :link, :title, :description, :time)' VALUES (:alias, :source, :link, :title, :description, :time)'
); );
$query->execute( $query->execute(
[ [
':alias' => $alias,
':source' => $source, ':source' => $source,
':link' => $link, ':link' => $link,
':title' => $title, ':title' => $title,

View file

@ -72,6 +72,7 @@ foreach ($config->crawler->channel as $channel)
{ {
// Create new one if not exists // Create new one if not exists
$channelId = $database->addChannel( $channelId = $database->addChannel(
$channel->alias,
$channel->source, $channel->source,
isset($remoteChannel->link) ? (string) $remoteChannel->link : null, isset($remoteChannel->link) ? (string) $remoteChannel->link : null,
isset($remoteChannel->title) ? (string) $remoteChannel->title : null, isset($remoteChannel->title) ? (string) $remoteChannel->title : null,