From fdf037e7287616b5211f8f8f7c8ce85b260d781e Mon Sep 17 00:00:00 2001 From: Accalia Elementia Date: Tue, 24 May 2022 11:07:50 -0400 Subject: Use Feed Link as GUID when Feed omits Guid. (#1785) * Use Feed Link as GUID when Feed omits Guid. As noted in nextcloud/news#1702 some feeds omit the GUID and are therefore not a valid RSS feed. nextcloud/news#1738 resolved the issue to allow valid feeds to update correctly when an invalid feed is present. This commit allows parsing of the invalid feed as well by assuming that the item link of the feed is unique to the feed and using it in place of the GUID when the feed omits the GUID. This will allow NextCloud News to accept and behave like many other popular feed aggregators when presented with such an invalid feed. Signed-off-by: Accalia * Add basic Logging when using fallback guid Signed-off-by: Accalia * Add basic Logging when using fallback guid - Fix Fatfinger Typo Signed-off-by: Accalia * Add basic Logging when using fallback guid - Update tests to account for additional logging Signed-off-by: Accalia --- lib/Fetcher/FeedFetcher.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/Fetcher/FeedFetcher.php b/lib/Fetcher/FeedFetcher.php index a6ddbacb5..27421f525 100755 --- a/lib/Fetcher/FeedFetcher.php +++ b/lib/Fetcher/FeedFetcher.php @@ -233,11 +233,25 @@ class FeedFetcher implements IFeedFetcher { $item = new Item(); $item->setUnread(true); - $item->setUrl($parsedItem->getLink()); - if ($parsedItem->getPublicId() == null) { + $itemLink = $parsedItem->getLink(); + $itemTitle = $parsedItem->getTitle(); + $item->setUrl($itemLink); + $publicId = $parsedItem->getPublicId(); + if ($publicId == null) { + // Fallback on using the URL as the guid for the feed item if no guid provided by feed + $this->logger->debug( + "Feed item {title} with link {link} did not expose a guid, falling back to using link as guid", + [ + 'title' => $itemTitle, + 'link' => $itemLink + ] + ); + $publicId = $itemLink; + } + if ($publicId == null) { throw new ReadErrorException("Malformed feed: item has no GUID"); } - $item->setGuid($parsedItem->getPublicId()); + $item->setGuid($publicId); $item->setGuidHash(md5($item->getGuid())); $lastModified = $parsedItem->getLastModified() ?? new DateTime(); @@ -255,8 +269,8 @@ class FeedFetcher implements IFeedFetcher $item->setRtl($RTL); // unescape content because angularjs helps against XSS - if ($parsedItem->getTitle() !== null) { - $item->setTitle($this->decodeTwice($parsedItem->getTitle())); + if ($itemTitle !== null) { + $item->setTitle($this->decodeTwice($itemTitle)); } $author = $parsedItem->getAuthor(); if ($author !== null && $author->getName() !== null) { -- cgit v1.2.3