Add date for RSS feeds (#4)

- First cut of user message add/display
This commit is contained in:
Daniel J. Summers 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 $url The URL for the RSS feed
* @param string $title The title of 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 * @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(); $db = self::getConnection();
$query = $db->prepare(<<<'SQL' $query = $db->prepare(<<<'SQL'
INSERT INTO feed ( INSERT INTO feed (

View File

@ -5,10 +5,10 @@
class Feed { class Feed {
/** @var string The XML namespace for Atom feeds */ /** @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 */ /** @var string The XML namespace for the `<content:encoded>` tag that allows HTML content in a feed */
public const CONTENT_NS = 'http://purl.org/rss/1.0/modules/content/'; 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 * 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); $channel = $feed['ok']->getElementsByTagName('channel')->item(0);
if (!$channel instanceof DOMElement) return [ 'error' => "Channel element not found ($channel->nodeType)" ]; if (!$channel instanceof DOMElement) return [ 'error' => "Channel element not found ($channel->nodeType)" ];
$feedId = Data::addFeed($feed['url'], self::eltValue($channel, 'title'), // In Atom feeds, lastBuildDate contains the last time an item in the feed was updated; if that is not present,
self::eltValue($channel, 'lastBuildDate')); // 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); $result = self::updateItems($feedId, $channel);
if (array_key_exists('error', $result)) return $result; if (array_key_exists('error', $result)) return $result;

View File

@ -12,7 +12,11 @@ Security::verifyUser();
if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// TODO: get feed, add if new, reject if existing but not owned by this user, update otherwise // TODO: get feed, add if new, reject if existing but not owned by this user, update otherwise
$result = Feed::add($_POST['url']); $result = Feed::add($_POST['url']);
echo '<pre>'; var_dump($result); echo '</pre>'; 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'] ]; $feed = [ 'id' => $_POST['id'], 'url' => $_POST['url'] ];
$title = 'TODO'; $title = 'TODO';
} else { } else {
@ -20,9 +24,8 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$feed = [ 'id' => $_GET['id'], 'url' => '' ]; $feed = [ 'id' => $_GET['id'], 'url' => '' ];
$title = 'Add RSS Feed'; $title = 'Add RSS Feed';
} }
page_head($title);
?> page_head($title); ?>
<h1><?=$title?></h1> <h1><?=$title?></h1>
<article> <article>
<form method=POST action=/feed hx-post=/feed> <form method=POST action=/feed hx-post=/feed>
@ -33,6 +36,5 @@ page_head($title);
</label><br> </label><br>
<button type=submit>Save</button> <button type=submit>Save</button>
</form> </form>
</article> </article><?php
<?php
page_foot(); page_foot();

View File

@ -12,6 +12,17 @@ require 'user-config.php';
Data::ensureDb(); Data::ensureDb();
/**
* Add a message to be displayed at the top of the page
*
* @param string $level The level (type) of the message
* @param string $message The message itself
*/
function add_message(string $level, string $message): void {
if (!array_key_exists('USER_MSG', $_REQUEST)) $_REQUEST['USER_MSG'] = array();
$_REQUEST['USER_MSG'][] = ['level' => $level, 'message' => $message];
}
/** /**
* Render the page title * Render the page title
* @param string $title The title of the page being displayed * @param string $title The title of the page being displayed
@ -29,14 +40,19 @@ function page_head(string $title): void {
<div><?php <div><?php
if (array_key_exists('FRC_USER_ID', $_REQUEST)) { if (array_key_exists('FRC_USER_ID', $_REQUEST)) {
echo '<a href=/feed?id=new>Add Feed</a>'; echo '<a href=/feed?id=new>Add Feed</a>';
if ($_REQUEST['FRC_USER_EMAIL'] != 'solouser@example.com') { if ($_REQUEST['FRC_USER_EMAIL'] != 'solouser@example.com') echo " | {$_REQUEST['FRC_USER_EMAIL']}";
echo " | {$_REQUEST['FRC_USER_EMAIL']}";
}
} ?> } ?>
</div> </div>
</header> </header>
<main hx-target=this> <main hx-target=this><?php
<?php if (array_key_exists('USER_MSG', $_REQUEST)) {
foreach ($_REQUEST['USER_MSG'] as $msg) { ?>
<div>
<?=$msg['level'] == 'INFO' ? '' : "<strong>{$msg['level']}</strong><br>"?>
<?=$msg['message']?>
</div><?php
}
}
} }
/** /**