From 2aa9b8f0265ebad443eba32c8248bb95dd8c0cb9 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 3 Sep 2016 22:17:36 +0200 Subject: [PATCH] [Bridge] Extend RssExpander to load ATOM formats --- lib/Bridge.php | 53 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index 2a83630..e889d10 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -605,9 +605,16 @@ abstract class RssExpander extends HttpCachingBridgeAbstract { $rssContent = simplexml_load_string($content); $this->debugMessage('loaded RSS from ' . $name); - // TODO insert RSS format detection - // For now we always assume RSS 2.0 - $this->collect_RSS_2_0_data($rssContent); + + if(isset($rssContent->channel[0])){ // RSS format + // TODO insert RSS format detection + // For now we always assume RSS 2.0 + $this->collect_RSS_2_0_data($rssContent); + } elseif(isset($rssContent->entry[0])){ // ATOM format + $this->collect_ATOM_data($rssContent); + } else { // Unknown format + $this->returnServerError('The feed format is unknown!'); + } } protected function collect_RSS_2_0_data($rssContent){ @@ -620,6 +627,14 @@ abstract class RssExpander extends HttpCachingBridgeAbstract { } } + protected function collect_ATOM_data($content){ + $this->load_ATOM_feed_data($content); + foreach($content->entry as $item){ + $this->debugMessage('parsing item ' . var_export($item, true)); + $this->items[] = $this->parseRSSItem($item); + } + } + protected function RSS_2_0_time_to_timestamp($item){ return DateTime::createFromFormat('D, d M Y H:i:s e', $item->pubDate)->getTimestamp(); } @@ -631,6 +646,38 @@ abstract class RssExpander extends HttpCachingBridgeAbstract { $this->description = trim($rssContent->description); } + protected function load_ATOM_feed_data($content){ + $this->name = $content->title; + + // Find most best link (only one, or first of 'alternate') + if(!isset($content->link)){ + $this->uri = ''; + } elseif (count($content->link) === 1){ + $this->uri = $content->link[0]['href']; + } else { + $this->uri = ''; + foreach($content->link as $link){ + if(strtolower($link['rel']) === 'alternate'){ + $this->uri = $link['rel']; + break; + } + } + } + + if(isset($content->subtitle)) + $this->description = $content->subtitle; + } + + protected function parseATOMItem($feedItem){ + $item = array(); + if(isset($feedItem->id)) $item['uri'] = $feedItem->id; + if(isset($feedItem->title)) $item['title'] = $feedItem->title; + if(isset($feedItem->updated)) $item['timestamp'] = strtotime($feedItem->updated); + if(isset($feedItem->author)) $item['author'] = $feedItem->author->name; + if(isset($feedItem->content)) $item['content'] = $feedItem->content; + return $item; + } + /** * Method should return, from a source RSS item given by lastRSS, one of our Items objects * @param $item the input rss item