Add editing of feed URL (#4)
- Move feed-specific database calls to Feed class - Detect when feed items have been updated - Add const keys for $_REQUEST values
This commit is contained in:
211
src/lib/Feed.php
211
src/lib/Feed.php
@@ -29,16 +29,16 @@ class Feed {
|
||||
* Parse a feed into an XML tree
|
||||
*
|
||||
* @param string $content The feed's RSS content
|
||||
* @return array|DOMDocument[]|string[] [ 'ok' => feed ] if successful, [ 'error' => message] if not
|
||||
* @return array|DOMDocument[]|string[] ['ok' => feed] if successful, ['error' => message] if not
|
||||
*/
|
||||
public static function parseFeed(string $content): array {
|
||||
set_error_handler(self::xmlParseError(...));
|
||||
try {
|
||||
$feed = new DOMDocument();
|
||||
$feed->loadXML($content);
|
||||
return [ 'ok' => $feed ];
|
||||
return ['ok' => $feed];
|
||||
} catch (DOMException $ex) {
|
||||
return [ 'error' => $ex->getMessage() ];
|
||||
return ['error' => $ex->getMessage()];
|
||||
} finally {
|
||||
restore_error_handler();
|
||||
}
|
||||
@@ -48,8 +48,8 @@ class Feed {
|
||||
* Retrieve the feed
|
||||
*
|
||||
* @param string $url
|
||||
* @return array|DOMDocument[]|string[] [ 'ok' => feedXml, 'url' => actualUrl ] if successful,
|
||||
* [ 'error' => message ] if not
|
||||
* @return array|DOMDocument[]|string[] ['ok' => feedXml, 'url' => actualUrl] if successful, ['error' => message] if
|
||||
* not
|
||||
*/
|
||||
public static function retrieveFeed(string $url): array {
|
||||
$feedReq = curl_init($url);
|
||||
@@ -93,53 +93,123 @@ class Feed {
|
||||
return $tags->length == 0 ? "$tagName not found" : $tags->item(0)->textContent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the fields we need to keep from the feed
|
||||
*
|
||||
* @param DOMElement $item The item from the feed
|
||||
* @return array The fields for the item as an associative array
|
||||
*/
|
||||
private static function itemFields(DOMElement $item): array {
|
||||
$itemGuid = self::eltValue($item, 'guid');
|
||||
$updNodes = $item->getElementsByTagNameNS(self::ATOM_NS, 'updated');
|
||||
$encNodes = $item->getElementsByTagNameNS(self::CONTENT_NS, 'encoded');
|
||||
return [
|
||||
'guid' => $itemGuid == 'guid not found' ? self::eltValue($item, 'link') : $itemGuid,
|
||||
'title' => self::eltValue($item, 'title'),
|
||||
'link' => self::eltValue($item, 'link'),
|
||||
'published' => Data::formatDate(self::eltValue($item, 'pubDate')),
|
||||
'updated' => Data::formatDate($updNodes->length > 0 ? $updNodes->item(0)->textContent : null),
|
||||
'content' => $encNodes->length > 0 ? $encNodes->item(0)->textContent
|
||||
: self::eltValue($item, 'description'),
|
||||
'isEncoded' => $encNodes->length > 0
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a feed item
|
||||
*
|
||||
* @param int $itemId The ID of the item to be updated
|
||||
* @param array $item The fields from the updated item
|
||||
* @param SQLite3 $db A database connection to use for the update
|
||||
*/
|
||||
private static function updateItem(int $itemId, array $item, SQLite3 $db): void {
|
||||
$query = $db->prepare(<<<'SQL'
|
||||
UPDATE item
|
||||
SET title = :title,
|
||||
published_on = :published,
|
||||
updated_on = :updated,
|
||||
content = :content,
|
||||
is_encoded = :encoded,
|
||||
is_read = 0
|
||||
WHERE id = :id
|
||||
SQL);
|
||||
$query->bindValue(':title', $item['title']);
|
||||
$query->bindValue(':published', $item['published']);
|
||||
$query->bindValue(':updated', $item['updated']);
|
||||
$query->bindValue(':content', $item['content']);
|
||||
$query->bindValue(':encoded', $item['isEncoded']);
|
||||
$query->bindValue(':id', $itemId);
|
||||
$query->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a feed item
|
||||
*
|
||||
* @param int $feedId The ID of the feed to which the item should be added
|
||||
* @param array $item The fields for the item
|
||||
* @param SQLite3 $db A database connection to use for the addition
|
||||
*/
|
||||
private static function addItem(int $feedId, array $item, SQLite3 $db): void {
|
||||
$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', $item['guid']);
|
||||
$query->bindValue(':link', $item['link']);
|
||||
$query->bindValue(':title', $item['title']);
|
||||
$query->bindValue(':published', $item['published']);
|
||||
$query->bindValue(':updated', $item['updated']);
|
||||
$query->bindValue(':content', $item['content']);
|
||||
$query->bindValue(':encoded', $item['isEncoded']);
|
||||
$query->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a feed's items
|
||||
*
|
||||
* @param int $feedId The ID of the feed to which these items belong
|
||||
* @param DOMElement $channel The RSS feed items
|
||||
* @return array [ 'ok' => true ] if successful, [ 'error' => message ] if not
|
||||
* @return array ['ok' => true] if successful, ['error' => message] if not
|
||||
*/
|
||||
public static function updateItems(int $feedId, DOMElement $channel): array {
|
||||
public static function updateItems(int $feedId, DOMElement $channel, SQLite3 $db): array {
|
||||
try {
|
||||
foreach ($channel->getElementsByTagName('item') as $item) {
|
||||
$itemGuid = self::eltValue($item, 'guid');
|
||||
if ($itemGuid == 'guid not found') $itemGuid = self::eltValue($item, 'link');
|
||||
$isNew = !Data::itemExists($feedId, $itemGuid);
|
||||
if ($isNew) {
|
||||
$title = self::eltValue($item, 'title');
|
||||
$link = self::eltValue($item, 'link');
|
||||
$published = self::eltValue($item, 'pubDate');
|
||||
$updNodes = $item->getElementsByTagNameNS(self::ATOM_NS, 'updated');
|
||||
$updated = $updNodes->length > 0 ? $updNodes->item(0)->textContent : null;
|
||||
$encNodes = $item->getElementsByTagNameNS(self::CONTENT_NS, 'encoded');
|
||||
if ($encNodes->length > 0) {
|
||||
$content = $encNodes->item(0)->textContent;
|
||||
$isEncoded = true;
|
||||
} else {
|
||||
$content = self::eltValue($item, 'description');
|
||||
$isEncoded = false;
|
||||
foreach ($channel->getElementsByTagName('item') as $rawItem) {
|
||||
$item = self::itemFields($rawItem);
|
||||
$existsQuery = $db->prepare(
|
||||
'SELECT id, published_on, updated_on FROM item WHERE feed_id = :feed AND item_guid = :guid');
|
||||
$existsQuery->bindValue(':feed', $feedId);
|
||||
$existsQuery->bindValue(':guid', $item['guid']);
|
||||
$exists = $existsQuery->execute();
|
||||
if ($exists) {
|
||||
$existing = $exists->fetchArray(SQLITE3_ASSOC);
|
||||
if ( $existing
|
||||
&& ( $existing['published_on'] != $item['published']
|
||||
|| $existing['updated_on'] ?? '' != $item['updated'] ?? '')) {
|
||||
self::updateItem($existing['id'], $item, $db);
|
||||
}
|
||||
Data::addItem($feedId, $itemGuid, $link, $title, $published, $updated, $content, $isEncoded);
|
||||
} // TODO: else check updated date; may want to return that from the isNew check instead
|
||||
} else {
|
||||
self::addItem($feedId, $item, $db);
|
||||
}
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
return [ 'error' => $ex->getMessage() ];
|
||||
return ['error' => $ex->getMessage()];
|
||||
}
|
||||
return [ 'ok', true ];
|
||||
return ['ok', true];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an RSS feed
|
||||
* Find the `<channel>` element and derive the published/last updated date from the feed
|
||||
*
|
||||
* @param string $url The URL of the RSS feed to add
|
||||
* @return array [ 'ok' => true ] if successful, [ 'error' => message ] if not
|
||||
* @param DOMDocument $feed The feed from which the information should be extracted
|
||||
* @return array|string[]|DOMElement[] ['channel' => channel, 'updated' => date] if successful, ['error' => message]
|
||||
* if not
|
||||
*/
|
||||
public static function add(string $url): array {
|
||||
$feed = self::retrieveFeed($url);
|
||||
if (array_key_exists('error', $feed)) return $feed;
|
||||
|
||||
$channel = $feed['ok']->getElementsByTagName('channel')->item(0);
|
||||
private static function findChannelAndDate(DOMDocument $feed): array {
|
||||
$channel = $feed->getElementsByTagName('channel')->item(0);
|
||||
if (!$channel instanceof DOMElement) return [ 'error' => "Channel element not found ($channel->nodeType)" ];
|
||||
|
||||
// In Atom feeds, lastBuildDate contains the last time an item in the feed was updated; if that is not present,
|
||||
@@ -149,11 +219,74 @@ class Feed {
|
||||
$updated = self::eltValue($channel, 'pubDate');
|
||||
if ($updated == 'pubDate not found') $updated = null;
|
||||
}
|
||||
$feedId = Data::addFeed($feed['url'], self::eltValue($channel, 'title'), $updated);
|
||||
return ['channel' => $channel, 'updated' => Data::formatDate($updated)];
|
||||
}
|
||||
|
||||
$result = self::updateItems($feedId, $channel);
|
||||
/**
|
||||
* Add an RSS feed
|
||||
*
|
||||
* @param string $url The URL of the RSS feed to add
|
||||
* @return array ['ok' => feedId] if successful, ['error' => message] if not
|
||||
*/
|
||||
public static function add(string $url, SQLite3 $db): array {
|
||||
$feed = self::retrieveFeed($url);
|
||||
if (array_key_exists('error', $feed)) return $feed;
|
||||
|
||||
$channelAndDate = self::findChannelAndDate($feed['ok']);
|
||||
if (array_key_exists('error', $channelAndDate)) return $channelAndDate;
|
||||
$channel = $channelAndDate['channel'];
|
||||
|
||||
$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[Key::USER_ID]);
|
||||
$query->bindValue(':url', $feed['url']);
|
||||
$query->bindValue(':title', self::eltValue($channel, 'title'));
|
||||
$query->bindValue(':updated', $channelAndDate['updated']);
|
||||
$query->bindValue(':checked', Data::formatDate('now'));
|
||||
$result = $query->execute();
|
||||
|
||||
$feedId = $result ? $db->lastInsertRowID() : -1;
|
||||
if ($feedId < 0) return ['error' => $db->lastErrorMsg()];
|
||||
|
||||
$result = self::updateItems($feedId, $channel, $db);
|
||||
if (array_key_exists('error', $result)) return $result;
|
||||
|
||||
return [ 'ok' => true ];
|
||||
return ['ok' => $feedId];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an RSS feed
|
||||
*
|
||||
* @param array $existing The existing RSS feed
|
||||
* @param string $url The URL with which the existing feed should be modified
|
||||
* @return bool[]|string[] [ 'ok' => true ] if successful, [ 'error' => message ] if not
|
||||
*/
|
||||
public static function update(array $existing, string $url, SQLite3 $db): array {
|
||||
$feed = self::retrieveFeed($url);
|
||||
if (array_key_exists('error', $feed)) return $feed;
|
||||
|
||||
$channelAndDate = self::findChannelAndDate($feed['ok']);
|
||||
if (array_key_exists('error', $channelAndDate)) return $channelAndDate;
|
||||
$channel = $channelAndDate['channel'];
|
||||
|
||||
$query = $db->prepare(<<<'SQL'
|
||||
UPDATE feed
|
||||
SET url = :url, title = :title, updated_on = :updated, checked_on = :checked
|
||||
WHERE id = :id AND user_id = :user
|
||||
SQL);
|
||||
$query->bindValue(':url', $feed['url']);
|
||||
$query->bindValue(':title', self::eltValue($channel, 'title'));
|
||||
$query->bindValue(':updated', $channelAndDate['updated']);
|
||||
$query->bindValue(':checked', Data::formatDate('now'));
|
||||
$query->bindValue(':id', $existing['id']);
|
||||
$query->bindValue(':user', $_REQUEST[Key::USER_ID]);
|
||||
$query->execute();
|
||||
|
||||
$result = self::updateItems($existing['id'], $channel, $db);
|
||||
if (array_key_exists('error', $result)) return $result;
|
||||
|
||||
return ['ok' => true];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user