diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2015-07-13 17:44:44 +0200 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2015-07-13 17:44:44 +0200 |
commit | 17f2cf72af4173409ee294a204de4221b0275428 (patch) | |
tree | 366d2621adfe0c4b862d0afba607076c5d3e30e0 /vendor/fguillot/picofeed/lib/PicoFeed/Parser/Rss20.php | |
parent | 5ee4dcd035cbf8c00ff95f02fce6ff6b3dc61aaf (diff) |
update picofeed
Diffstat (limited to 'vendor/fguillot/picofeed/lib/PicoFeed/Parser/Rss20.php')
-rw-r--r-- | vendor/fguillot/picofeed/lib/PicoFeed/Parser/Rss20.php | 126 |
1 files changed, 70 insertions, 56 deletions
diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Rss20.php b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Rss20.php index 2529b5984..f4c5ae314 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Rss20.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Rss20.php @@ -15,6 +15,16 @@ use PicoFeed\Client\Url; class Rss20 extends Parser { /** + * Supported namespaces + */ + protected $namespaces = array( + 'dc' => 'http://purl.org/dc/elements/1.1/', + 'content' => 'http://purl.org/rss/1.0/modules/content/', + 'feedburner' => 'http://rssnamespace.org/feedburner/ext/1.0', + 'atom' => 'http://www.w3.org/2005/Atom' + ); + + /** * Get the path to the items XML tree * * @access public @@ -23,7 +33,7 @@ class Rss20 extends Parser */ public function getItemsTree(SimpleXMLElement $xml) { - return $xml->channel->item; + return XmlParser::getXPathResult($xml, 'channel/item'); } /** @@ -47,7 +57,8 @@ class Rss20 extends Parser */ public function findSiteUrl(SimpleXMLElement $xml, Feed $feed) { - $feed->site_url = (string) $xml->channel->link; + $site_url = XmlParser::getXPathResult($xml, 'channel/link'); + $feed->site_url = (string) current($site_url); } /** @@ -59,7 +70,8 @@ class Rss20 extends Parser */ public function findFeedDescription(SimpleXMLElement $xml, Feed $feed) { - $feed->description = (string) $xml->channel->description; + $description = XmlParser::getXPathResult($xml, 'channel/description'); + $feed->description = (string) current($description); } /** @@ -71,9 +83,8 @@ class Rss20 extends Parser */ public function findFeedLogo(SimpleXMLElement $xml, Feed $feed) { - if (isset($xml->channel->image->url)) { - $feed->logo = (string) $xml->channel->image->url; - } + $logo = XmlParser::getXPathResult($xml, 'channel/image/url'); + $feed->logo = (string) current($logo); } /** @@ -97,7 +108,8 @@ class Rss20 extends Parser */ public function findFeedTitle(SimpleXMLElement $xml, Feed $feed) { - $feed->title = Filter::stripWhiteSpace((string) $xml->channel->title) ?: $feed->getSiteUrl(); + $title = XmlParser::getXPathResult($xml, 'channel/title'); + $feed->title = Filter::stripWhiteSpace((string) current($title)) ?: $feed->getSiteUrl(); } /** @@ -109,7 +121,8 @@ class Rss20 extends Parser */ public function findFeedLanguage(SimpleXMLElement $xml, Feed $feed) { - $feed->language = isset($xml->channel->language) ? (string) $xml->channel->language : ''; + $language = XmlParser::getXPathResult($xml, 'channel/language'); + $feed->language = (string) current($language); } /** @@ -133,8 +146,21 @@ class Rss20 extends Parser */ public function findFeedDate(SimpleXMLElement $xml, Feed $feed) { - $date = isset($xml->channel->pubDate) ? $xml->channel->pubDate : $xml->channel->lastBuildDate; - $feed->date = $this->date->getDateTime((string) $date); + $publish_date = XmlParser::getXPathResult($xml, 'channel/pubDate'); + $update_date = XmlParser::getXPathResult($xml, 'channel/lastBuildDate'); + + $published = ! empty($publish_date) ? $this->date->getDateTime((string) current($publish_date)) : null; + $updated = ! empty($update_date) ? $this->date->getDateTime((string) current($update_date)) : null; + + if ($published === null && $updated === null) { + $feed->date = $this->date->getCurrentDateTime(); // We use the current date if there is no date for the feed + } + else if ($published !== null && $updated !== null) { + $feed->date = max($published, $updated); // We use the most recent date between published and updated + } + else { + $feed->date = $updated ?: $published; + } } /** @@ -147,17 +173,9 @@ class Rss20 extends Parser */ public function findItemDate(SimpleXMLElement $entry, Item $item, Feed $feed) { - $date = XmlParser::getNamespaceValue($entry, $this->namespaces, 'date'); + $date = XmlParser::getXPathResult($entry, 'pubDate'); - if (empty($date)) { - $date = XmlParser::getNamespaceValue($entry, $this->namespaces, 'updated'); - } - - if (empty($date)) { - $date = (string) $entry->pubDate; - } - - $item->date = empty($date) ? $feed->getDate() : $this->date->getDateTime($date); + $item->date = empty($date) ? $feed->getDate() : $this->date->getDateTime((string) current($date)); } /** @@ -169,11 +187,8 @@ class Rss20 extends Parser */ public function findItemTitle(SimpleXMLElement $entry, Item $item) { - $item->title = Filter::stripWhiteSpace((string) $entry->title); - - if (empty($item->title)) { - $item->title = $item->url; - } + $title = XmlParser::getXPathResult($entry, 'title'); + $item->title = Filter::stripWhiteSpace((string) current($title)) ?: $item->url; } /** @@ -186,16 +201,12 @@ class Rss20 extends Parser */ public function findItemAuthor(SimpleXMLElement $xml, SimpleXMLElement $entry, Item $item) { - $item->author = XmlParser::getNamespaceValue($entry, $this->namespaces, 'creator'); + $author = XmlParser::getXPathResult($entry, 'dc:creator', $this->namespaces) + ?: XmlParser::getXPathResult($entry, 'author') + ?: XmlParser::getXPathResult($xml, 'channel/dc:creator', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'channel/managingEditor'); - if (empty($item->author)) { - if (isset($entry->author)) { - $item->author = (string) $entry->author; - } - else if (isset($xml->channel->webMaster)) { - $item->author = (string) $xml->channel->webMaster; - } - } + $item->author = (string) current($author); } /** @@ -207,13 +218,13 @@ class Rss20 extends Parser */ public function findItemContent(SimpleXMLElement $entry, Item $item) { - $content = XmlParser::getNamespaceValue($entry, $this->namespaces, 'encoded'); + $content = XmlParser::getXPathResult($entry, 'content:encoded', $this->namespaces); - if (trim($content) === '' && $entry->description->count() > 0) { - $content = (string) $entry->description; + if (trim((string) current($content)) === '') { + $content = XmlParser::getXPathResult($entry, 'description'); } - $item->content = $content; + $item->content = (string) current($content); } /** @@ -225,17 +236,19 @@ class Rss20 extends Parser */ public function findItemUrl(SimpleXMLElement $entry, Item $item) { - $links = array( - XmlParser::getNamespaceValue($entry, $this->namespaces, 'origLink'), - isset($entry->link) ? (string) $entry->link : '', - XmlParser::getNamespaceValue($entry, $this->namespaces, 'link', 'href'), - isset($entry->guid) ? (string) $entry->guid : '', - ); - - foreach ($links as $link) { - if (! empty($link) && filter_var($link, FILTER_VALIDATE_URL) !== false) { + $link = XmlParser::getXPathResult($entry, 'feedburner:origLink', $this->namespaces) + ?: XmlParser::getXPathResult($entry, 'link') + ?: XmlParser::getXPathResult($entry, 'atom:link/@href', $this->namespaces); + + if (! empty($link)) { + $item->url = trim((string) current($link)); + } + else { + $link = XmlParser::getXPathResult($entry, 'guid'); + $link = trim((string) current($link)); + + if (filter_var($link, FILTER_VALIDATE_URL) !== false) { $item->url = $link; - break; } } } @@ -250,7 +263,7 @@ class Rss20 extends Parser */ public function findItemId(SimpleXMLElement $entry, Item $item, Feed $feed) { - $id = (string) $entry->guid; + $id = (string) current(XmlParser::getXPathResult($entry, 'guid')); if ($id) { $item->id = $this->generateId($id); @@ -273,15 +286,14 @@ class Rss20 extends Parser public function findItemEnclosure(SimpleXMLElement $entry, Item $item, Feed $feed) { if (isset($entry->enclosure)) { + $enclosure_url = XmlParser::getXPathResult($entry, 'feedburner:origEnclosureLink', $this->namespaces) + ?: XmlParser::getXPathResult($entry, 'enclosure/@url'); - $item->enclosure_url = XmlParser::getNamespaceValue($entry->enclosure, $this->namespaces, 'origEnclosureLink'); + $enclosure_type = XmlParser::getXPathResult($entry, 'enclosure/@type'); - if (empty($item->enclosure_url)) { - $item->enclosure_url = isset($entry->enclosure['url']) ? (string) $entry->enclosure['url'] : ''; - } - $item->enclosure_type = isset($entry->enclosure['type']) ? (string) $entry->enclosure['type'] : ''; - $item->enclosure_url = Url::resolve($item->enclosure_url, $feed->getSiteUrl()); + $item->enclosure_url = Url::resolve((string) current($enclosure_url), $feed->getSiteUrl()); + $item->enclosure_type = (string) current($enclosure_type); } } @@ -295,6 +307,8 @@ class Rss20 extends Parser */ public function findItemLanguage(SimpleXMLElement $entry, Item $item, Feed $feed) { - $item->language = $feed->language; + $language = XmlParser::getXPathResult($entry, 'dc:language', $this->namespaces); + + $item->language = (string) current($language) ?: $feed->language; } } |