Change from SimpleXML to DOM (#4)

This API is more reliable, and should help when implementing the "load a site's HTML and look for feed links" functionality coming before the final release
This commit is contained in:
2024-04-10 20:50:45 -04:00
parent 0530ed0dc9
commit 8ca4bf2109
2 changed files with 115 additions and 50 deletions

View File

@@ -99,8 +99,23 @@ class Data {
$query->execute();
}
/**
* Parse/format a date/time from a string
*
* @param ?string $value The date/time to be parsed and formatted
* @return string|null The date/time in `DateTimeInterface::ATOM` format, or `null` if the input cannot be parsed
*/
private static function formatDate(?string $value): ?string {
try {
return $value ? (new DateTimeImmutable($value))->format(DateTimeInterface::ATOM) : null;
} catch (Exception) {
return null;
}
}
/**
* Add an RSS feed
*
* @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)
@@ -108,28 +123,25 @@ class Data {
*/
public static function addFeed(string $url, string $title, string $updatedOn): int {
$db = self::getConnection();
if ($updatedOn) {
try {
$updated = (new DateTimeImmutable($updatedOn))->format(DateTimeInterface::ATOM);
} catch (Exception) {
$updated = null;
}
} else {
$updated = null;
}
$query = $db->prepare('INSERT INTO feed (user_id, url, title, updated_on, checked_on)'
. ' VALUES (:user, :url, :title, :updated, :checked)');
$query->bindValue(':user', $_REQUEST['FRC_USER_ID']);
$query->bindValue(':url', $url);
$query->bindValue(':title', $title);
$query->bindValue(':updated', $updated);
$query->bindValue(':checked', (new DateTimeImmutable())->format(DateTimeInterface::ATOM));
$query = $db->prepare(<<<'SQL'
INSERT INTO feed (
user_id, url, title, updated_on, checked_on
) VALUES (
:user, :url, :title, :updated, :checked
)
SQL);
$query->bindValue(':user', $_REQUEST['FRC_USER_ID']);
$query->bindValue(':url', $url);
$query->bindValue(':title', $title);
$query->bindValue(':updated', self::formatDate($updatedOn));
$query->bindValue(':checked', self::formatDate('now'));
$result = $query->execute();
return $result ? $db->lastInsertRowID() : -1;
}
/**
* Does a feed item already exist?
*
* @param int $feedId The ID of the feed to which the item belongs
* @param string $guid The GUID from the RSS feed, uniquely identifying the item
* @return bool True if the item exists, false if not
@@ -145,28 +157,34 @@ class Data {
/**
* Add a feed item
*
* @param int $feedId The ID of the feed to which the item should be added
* @param string $guid The GUID from the RSS feed (uses link if `<guid>` not specified)
* @param string $link The link to this item
* @param string $title The title of the item
* @param string $published The date/time the item was published
* @param string $publishedOn The date/time the item was published
* @param ?string $updatedOn The date/time the item was last updated
* @param string $content The content of the item
* @param bool $isEncoded Whether the content has HTML (true) or is plaintext (false)
* @throws Exception If the published date is not valid
*/
public static function addItem(int $feedId, string $guid, string $link, string $title, string $published,
string $content, bool $isEncoded): void {
public static function addItem(int $feedId, string $guid, string $link, string $title, string $publishedOn,
?string $updatedOn, string $content, bool $isEncoded): void {
$db = self::getConnection();
$query = $db->prepare(
'INSERT INTO item (feed_id, item_guid, item_link, title, published_on, content, is_encoded)'
. ' VALUES (:feed, :guid, :link, :title, :published, :content, :encoded)');
$query->bindValue(':feed', $feedId);
$query->bindValue(':guid', $guid);
$query->bindValue(':link', $link);
$query->bindValue(':title', $title);
$query->bindValue(':published', (new DateTimeImmutable($published))->format(DateTimeInterface::ATOM));
$query->bindValue(':content', $content);
$query->bindValue(':encoded', $isEncoded);
$query = $db->prepare(<<<'SQL'
INSERT INTO item (
feed_id, item_guid, item_link, title, published_on, updated_on, content, is_encoded
) VALUES (
:feed, :guid, :link, :title, :published, :updated, :content, :encoded
)
SQL);
$query->bindValue(':feed', $feedId);
$query->bindValue(':guid', $guid);
$query->bindValue(':link', $link);
$query->bindValue(':title', $title);
$query->bindValue(':published', self::formatDate($publishedOn));
$query->bindValue(':updated', self::formatDate($updatedOn));
$query->bindValue(':content', $content);
$query->bindValue(':encoded', $isEncoded);
$query->execute();
}
}