From 7d294b9be86bf8eca226618f39d3c79ecb1754a3 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Thu, 11 Apr 2024 19:07:39 -0400 Subject: [PATCH] Add date for RSS feeds (#4) - First cut of user message add/display --- src/lib/Data.php | 4 ++-- src/lib/Feed.php | 16 +++++++++++----- src/public/feed.php | 12 +++++++----- src/start.php | 26 +++++++++++++++++++++----- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/lib/Data.php b/src/lib/Data.php index 0969f36..59b9595 100644 --- a/src/lib/Data.php +++ b/src/lib/Data.php @@ -118,10 +118,10 @@ class Data { * * @param string $url The URL for the RSS feed * @param string $title The title of the RSS feed - * @param string $updatedOn The date/time the RSS feed was last updated (from the XML, not when we checked) + * @param ?string $updatedOn The date/time the RSS feed was last updated (from the XML, not when we checked) * @return int The ID of the added feed */ - public static function addFeed(string $url, string $title, string $updatedOn): int { + public static function addFeed(string $url, string $title, ?string $updatedOn): int { $db = self::getConnection(); $query = $db->prepare(<<<'SQL' INSERT INTO feed ( diff --git a/src/lib/Feed.php b/src/lib/Feed.php index fc8120d..70574d8 100644 --- a/src/lib/Feed.php +++ b/src/lib/Feed.php @@ -5,10 +5,10 @@ class Feed { /** @var string The XML namespace for Atom feeds */ - public const ATOM_NS = 'http://www.w3.org/2005/Atom'; + public const string ATOM_NS = 'http://www.w3.org/2005/Atom'; - /** @var string The XML namespace for the `` tag that allows HTML content in a feed */ - public const CONTENT_NS = 'http://purl.org/rss/1.0/modules/content/'; + /** @var string The XML namespace for the `` tag that allows HTML content in a feed */ + public const string CONTENT_NS = 'http://purl.org/rss/1.0/modules/content/'; /** * When parsing XML into a DOMDocument, errors are presented as warnings; this creates an exception for them @@ -142,8 +142,14 @@ class Feed { $channel = $feed['ok']->getElementsByTagName('channel')->item(0); if (!$channel instanceof DOMElement) return [ 'error' => "Channel element not found ($channel->nodeType)" ]; - $feedId = Data::addFeed($feed['url'], self::eltValue($channel, 'title'), - self::eltValue($channel, 'lastBuildDate')); + // In Atom feeds, lastBuildDate contains the last time an item in the feed was updated; if that is not present, + // use the pubDate element instead + $updated = self::eltValue($channel, 'lastBuildDate'); + if ($updated == 'lastBuildDate not found') { + $updated = self::eltValue($channel, 'pubDate'); + if ($updated == 'pubDate not found') $updated = null; + } + $feedId = Data::addFeed($feed['url'], self::eltValue($channel, 'title'), $updated); $result = self::updateItems($feedId, $channel); if (array_key_exists('error', $result)) return $result; diff --git a/src/public/feed.php b/src/public/feed.php index 1092b7f..82ac36a 100644 --- a/src/public/feed.php +++ b/src/public/feed.php @@ -12,7 +12,11 @@ Security::verifyUser(); if ($_SERVER['REQUEST_METHOD'] == 'POST') { // TODO: get feed, add if new, reject if existing but not owned by this user, update otherwise $result = Feed::add($_POST['url']); - echo '
'; var_dump($result); echo '
'; + if (array_key_exists('ok', $result)) { + add_message('INFO', 'Feed added successfully'); + } else { + add_message('ERROR', $result['error']); + } $feed = [ 'id' => $_POST['id'], 'url' => $_POST['url'] ]; $title = 'TODO'; } else { @@ -20,9 +24,8 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $feed = [ 'id' => $_GET['id'], 'url' => '' ]; $title = 'Add RSS Feed'; } -page_head($title); -?> +page_head($title); ?>

@@ -33,6 +36,5 @@ page_head($title);
-
- $level, 'message' => $message]; +} + /** * Render the page title * @param string $title The title of the page being displayed @@ -29,14 +40,19 @@ function page_head(string $title): void {
Add Feed'; - if ($_REQUEST['FRC_USER_EMAIL'] != 'solouser@example.com') { - echo " | {$_REQUEST['FRC_USER_EMAIL']}"; - } + if ($_REQUEST['FRC_USER_EMAIL'] != 'solouser@example.com') echo " | {$_REQUEST['FRC_USER_EMAIL']}"; } ?>
-
- +
+ {$msg['level']}
"?> + +