Add date for RSS feeds (#4)

- First cut of user message add/display
This commit is contained in:
2024-04-11 19:07:39 -04:00
parent 8ca4bf2109
commit 7d294b9be8
4 changed files with 41 additions and 17 deletions

View File

@@ -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 (

View File

@@ -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 `<content>` 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 `<content:encoded>` 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;