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 | |
parent | 5ee4dcd035cbf8c00ff95f02fce6ff6b3dc61aaf (diff) |
update picofeed
Diffstat (limited to 'vendor/fguillot/picofeed/lib/PicoFeed/Parser')
7 files changed, 446 insertions, 166 deletions
diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Atom.php b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Atom.php index 0e53d2778..6950d9afd 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Atom.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Atom.php @@ -15,6 +15,13 @@ use PicoFeed\Client\Url; class Atom extends Parser { /** + * Supported namespaces + */ + protected $namespaces = array( + 'atom' => 'http://www.w3.org/2005/Atom', + ); + + /** * Get the path to the items XML tree * * @access public @@ -23,7 +30,8 @@ class Atom extends Parser */ public function getItemsTree(SimpleXMLElement $xml) { - return $xml->entry; + return XmlParser::getXPathResult($xml, 'atom:entry', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'entry'); } /** @@ -59,7 +67,10 @@ class Atom extends Parser */ public function findFeedDescription(SimpleXMLElement $xml, Feed $feed) { - $feed->description = (string) $xml->subtitle; + $description = XmlParser::getXPathResult($xml, 'atom:subtitle', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'subtitle'); + + $feed->description = (string) current($description); } /** @@ -71,7 +82,10 @@ class Atom extends Parser */ public function findFeedLogo(SimpleXMLElement $xml, Feed $feed) { - $feed->logo = (string) $xml->logo; + $logo = XmlParser::getXPathResult($xml, 'atom:logo', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'logo'); + + $feed->logo = (string) current($logo); } /** @@ -83,7 +97,10 @@ class Atom extends Parser */ public function findFeedIcon(SimpleXMLElement $xml, Feed $feed) { - $feed->icon = (string) $xml->icon; + $icon = XmlParser::getXPathResult($xml, 'atom:icon', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'icon'); + + $feed->icon = (string) current($icon); } /** @@ -95,7 +112,10 @@ class Atom extends Parser */ public function findFeedTitle(SimpleXMLElement $xml, Feed $feed) { - $feed->title = Filter::stripWhiteSpace((string) $xml->title) ?: $feed->getSiteUrl(); + $title = XmlParser::getXPathResult($xml, 'atom:title', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'title'); + + $feed->title = Filter::stripWhiteSpace((string) current($title)) ?: $feed->getSiteUrl(); } /** @@ -107,7 +127,10 @@ class Atom extends Parser */ public function findFeedLanguage(SimpleXMLElement $xml, Feed $feed) { - $feed->language = XmlParser::getXmlLang($this->content); + $language = XmlParser::getXPathResult($xml, '*[not(self::atom:entry)]/@xml:lang', $this->namespaces) + ?: XmlParser::getXPathResult($xml, '@xml:lang'); + + $feed->language = (string) current($language); } /** @@ -119,7 +142,10 @@ class Atom extends Parser */ public function findFeedId(SimpleXMLElement $xml, Feed $feed) { - $feed->id = (string) $xml->id; + $id = XmlParser::getXPathResult($xml, 'atom:id', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'id'); + + $feed->id = (string) current($id); } /** @@ -131,7 +157,10 @@ class Atom extends Parser */ public function findFeedDate(SimpleXMLElement $xml, Feed $feed) { - $feed->date = $this->date->getDateTime((string) $xml->updated); + $updated = XmlParser::getXPathResult($xml, 'atom:updated', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'updated'); + + $feed->date = $this->date->getDateTime((string) current($updated)); } /** @@ -144,11 +173,17 @@ class Atom extends Parser */ public function findItemDate(SimpleXMLElement $entry, Item $item, Feed $feed) { - $published = isset($entry->published) ? $this->date->getDateTime((string) $entry->published) : null; - $updated = isset($entry->updated) ? $this->date->getDateTime((string) $entry->updated) : null; + $published = XmlParser::getXPathResult($entry, 'atom:published', $this->namespaces) + ?: XmlParser::getXPathResult($entry, 'published'); + + $updated = XmlParser::getXPathResult($entry, 'atom:updated', $this->namespaces) + ?: XmlParser::getXPathResult($entry, 'updated'); + + $published = ! empty($published) ? $this->date->getDateTime((string) current($published)) : null; + $updated = ! empty($updated) ? $this->date->getDateTime((string) current($updated)) : null; if ($published === null && $updated === null) { - $item->date = $feed->getDate(); // We use the feed date if there is no date for the item + $item->date = $feed->getDate(); // We use the feed date if there is no date for the item } else if ($published !== null && $updated !== null) { $item->date = max($published, $updated); // We use the most recent date between published and updated @@ -167,11 +202,10 @@ class Atom extends Parser */ public function findItemTitle(SimpleXMLElement $entry, Item $item) { - $item->title = Filter::stripWhiteSpace((string) $entry->title); + $title = XmlParser::getXPathResult($entry, 'atom:title', $this->namespaces) + ?: XmlParser::getXPathResult($entry, 'title'); - if (empty($item->title)) { - $item->title = $item->url; - } + $item->title = Filter::stripWhiteSpace((string) current($title)) ?: $item->url; } /** @@ -184,12 +218,12 @@ class Atom extends Parser */ public function findItemAuthor(SimpleXMLElement $xml, SimpleXMLElement $entry, Item $item) { - if (isset($entry->author->name)) { - $item->author = (string) $entry->author->name; - } - else { - $item->author = (string) $xml->author->name; - } + $author = XmlParser::getXPathResult($entry, 'atom:author/atom:name', $this->namespaces) + ?: XmlParser::getXPathResult($entry, 'author/name') + ?: XmlParser::getXPathResult($xml, 'atom:author/atom:name', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'author/name'); + + $item->author = (string) current($author); } /** @@ -226,10 +260,11 @@ class Atom extends Parser */ public function findItemId(SimpleXMLElement $entry, Item $item, Feed $feed) { - $id = (string) $entry->id; + $id = XmlParser::getXPathResult($entry, 'atom:id', $this->namespaces) + ?: XmlParser::getXPathResult($entry, 'id'); - if ($id) { - $item->id = $this->generateId($id); + if (! empty($id)) { + $item->id = $this->generateId((string) current($id)); } else { $item->id = $this->generateId( @@ -266,13 +301,9 @@ class Atom extends Parser */ public function findItemLanguage(SimpleXMLElement $entry, Item $item, Feed $feed) { - $language = (string) $entry->attributes('xml', true)->{'lang'}; - - if ($language === '') { - $language = $feed->language; - } + $language = XmlParser::getXPathResult($entry, './/@xml:lang'); - $item->language = $language; + $item->language = (string) current($language) ?: $feed->language; } /** @@ -309,7 +340,10 @@ class Atom extends Parser */ private function findLink(SimpleXMLElement $xml, $rel) { - foreach ($xml->link as $link) { + $links = XmlParser::getXPathResult($xml, 'atom:link', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'link'); + + foreach ($links as $link) { if ($rel === (string) $link['rel']) { return $link; } @@ -327,19 +361,27 @@ class Atom extends Parser */ private function getContent(SimpleXMLElement $entry) { - if (isset($entry->content) && ! empty($entry->content)) { + $content = current( + XmlParser::getXPathResult($entry, 'atom:content', $this->namespaces) + ?: XmlParser::getXPathResult($entry, 'content') + ); - if (count($entry->content->children())) { - return (string) $entry->content->asXML(); - } - else { - return (string) $entry->content; + if (! empty($content) && count($content->children())) { + $xml_string = ''; + + foreach($content->children() as $child) { + $xml_string .= $child->asXML(); } + + return $xml_string; } - else if (isset($entry->summary) && ! empty($entry->summary)) { - return (string) $entry->summary; + else if (trim((string) $content) !== '') { + return (string) $content; } - return ''; + $summary = XmlParser::getXPathResult($entry, 'atom:summary', $this->namespaces) + ?: XmlParser::getXPathResult($entry, 'summary'); + + return (string) current($summary); } } diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/DateParser.php b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/DateParser.php index 92d0012ac..4612a8613 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/DateParser.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/DateParser.php @@ -88,7 +88,7 @@ class DateParser * @access public * @param string $format Date format * @param string $value Original date value - * @return DateTime + * @return DateTime|boolean */ public function getValidDate($format, $value) { diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Item.php b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Item.php index d891ef41c..c9dff5a6f 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Item.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Item.php @@ -121,23 +121,24 @@ class Item * @access public * @param string $tag Tag name (examples: guid, media:content) * @param string $attribute Tag attribute - * @return string + * @return array|false Tag values or error */ public function getTag($tag, $attribute = '') { - // Get namespaced value - if (strpos($tag, ':') !== false) { - list(,$tag) = explode(':', $tag); - return XmlParser::getNamespaceValue($this->xml, $this->namespaces, $tag, $attribute); + // convert to xPath attribute query + if ($attribute !== '') { + $attribute = '/@'.$attribute; } - // Return attribute value - if (! empty($attribute)) { - return (string) $this->xml->{$tag}[$attribute]; + // construct query + $query = './/'.$tag.$attribute; + $elements = XmlParser::getXPathResult($this->xml, $query, $this->namespaces); + + if ($elements === false) { // xPath error + return false; } - // Return tag content - return (string) $this->xml->$tag; + return array_map(function ($element) { return (string) $element;}, $elements); } /** @@ -198,7 +199,7 @@ class Item * Get date * * @access public - * $return integer + * $return \DateTime */ public function getDate() { diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Parser.php b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Parser.php index 810494b70..918cdef33 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Parser.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Parser.php @@ -58,7 +58,7 @@ abstract class Parser protected $fallback_url = ''; /** - * XML namespaces + * XML namespaces supported by parser * * @access protected * @var array @@ -66,6 +66,14 @@ abstract class Parser protected $namespaces = array(); /** + * XML namespaces used in document + * + * @access protected + * @var array + */ + protected $used_namespaces = array(); + + /** * Enable the content filtering * * @access private @@ -117,9 +125,6 @@ abstract class Parser // Encode everything in UTF-8 Logger::setMessage(get_called_class().': HTTP Encoding "'.$http_encoding.'" ; XML Encoding "'.$xml_encoding.'"'); $this->content = Encoding::convert($this->content, $xml_encoding ?: $http_encoding); - - // Workarounds - $this->content = Filter::normalizeData($this->content); } /** @@ -135,12 +140,19 @@ abstract class Parser $xml = XmlParser::getSimpleXml($this->content); if ($xml === false) { - Logger::setMessage(get_called_class().': XML parsing error'); - Logger::setMessage(XmlParser::getErrors()); - throw new MalformedXmlException('XML parsing error'); + Logger::setMessage(get_called_class().': Applying XML workarounds'); + $this->content = Filter::normalizeData($this->content); + $xml = XmlParser::getSimpleXml($this->content); + + if ($xml === false) { + Logger::setMessage(get_called_class().': XML parsing error'); + Logger::setMessage(XmlParser::getErrors()); + throw new MalformedXmlException('XML parsing error'); + } } - $this->namespaces = $xml->getNamespaces(true); + $this->used_namespaces = $xml->getNamespaces(true); + $xml = $this->registerSupportedNamespaces($xml); $feed = new Feed; @@ -160,9 +172,11 @@ abstract class Parser foreach ($this->getItemsTree($xml) as $entry) { + $entry = $this->registerSupportedNamespaces($entry); + $item = new Item; $item->xml = $entry; - $item->namespaces = $this->namespaces; + $item->namespaces = $this->used_namespaces; $this->findItemAuthor($xml, $entry, $item); @@ -418,6 +432,22 @@ abstract class Parser } /** + * Register all supported namespaces to be used within an xpath query + * + * @access public + * @param SimpleXMLElement $xml Feed xml + * @return SimpleXMLElement + */ + public function registerSupportedNamespaces(SimpleXMLElement $xml) + { + foreach ($this->namespaces as $prefix => $ns) { + $xml->registerXPathNamespace($prefix, $ns); + } + + return $xml; + } + + /** * Find the feed url * * @access public diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Rss10.php b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Rss10.php index 16c66a016..32fc54e93 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Rss10.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Rss10.php @@ -3,6 +3,7 @@ namespace PicoFeed\Parser; use SimpleXMLElement; +use PicoFeed\Filter\Filter; /** * RSS 1.0 parser @@ -10,9 +11,19 @@ use SimpleXMLElement; * @author Frederic Guillot * @package Parser */ -class Rss10 extends Rss20 +class Rss10 extends Parser { /** + * Supported namespaces + */ + protected $namespaces = array( + 'rss' => 'http://purl.org/rss/1.0/', + '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', + ); + + /** * Get the path to the items XML tree * * @access public @@ -21,19 +32,92 @@ class Rss10 extends Rss20 */ public function getItemsTree(SimpleXMLElement $xml) { - return $xml->item; + return XmlParser::getXPathResult($xml, 'rss:item', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'item'); } /** - * Find the feed date + * Find the feed url * * @access public - * @param SimpleXMLElement $xml Feed xml + * @param SimpleXMLElement $xml Feed xml * @param \PicoFeed\Parser\Feed $feed Feed object */ - public function findFeedDate(SimpleXMLElement $xml, Feed $feed) + public function findFeedUrl(SimpleXMLElement $xml, Feed $feed) { - $feed->date = $this->date->getDateTime(XmlParser::getNamespaceValue($xml->channel, $this->namespaces, 'date')); + $feed->feed_url = ''; + } + + /** + * Find the site url + * + * @access public + * @param SimpleXMLElement $xml Feed xml + * @param \PicoFeed\Parser\Feed $feed Feed object + */ + public function findSiteUrl(SimpleXMLElement $xml, Feed $feed) + { + $site_url = XmlParser::getXPathResult($xml, 'rss:channel/rss:link', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'channel/link'); + + $feed->site_url = (string) current($site_url); + } + + /** + * Find the feed description + * + * @access public + * @param SimpleXMLElement $xml Feed xml + * @param \PicoFeed\Parser\Feed $feed Feed object + */ + public function findFeedDescription(SimpleXMLElement $xml, Feed $feed) + { + $description = XmlParser::getXPathResult($xml, 'rss:channel/rss:description', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'channel/description'); + + $feed->description = (string) current($description); + } + + /** + * Find the feed logo url + * + * @access public + * @param SimpleXMLElement $xml Feed xml + * @param \PicoFeed\Parser\Feed $feed Feed object + */ + public function findFeedLogo(SimpleXMLElement $xml, Feed $feed) + { + $logo = XmlParser::getXPathResult($xml, 'rss:image/rss:url', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'image/url'); + + $feed->logo = (string) current($logo); + } + + /** + * Find the feed icon + * + * @access public + * @param SimpleXMLElement $xml Feed xml + * @param \PicoFeed\Parser\Feed $feed Feed object + */ + public function findFeedIcon(SimpleXMLElement $xml, Feed $feed) + { + $feed->icon = ''; + } + + /** + * Find the feed title + * + * @access public + * @param SimpleXMLElement $xml Feed xml + * @param \PicoFeed\Parser\Feed $feed Feed object + */ + public function findFeedTitle(SimpleXMLElement $xml, Feed $feed) + { + $title = XmlParser::getXPathResult($xml, 'rss:channel/rss:title', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'channel/title'); + + $feed->title = Filter::stripWhiteSpace((string) current($title)) ?: $feed->getSiteUrl(); } /** @@ -45,14 +129,126 @@ class Rss10 extends Rss20 */ public function findFeedLanguage(SimpleXMLElement $xml, Feed $feed) { - $feed->language = XmlParser::getNamespaceValue($xml->channel, $this->namespaces, 'language'); + $language = XmlParser::getXPathResult($xml, 'rss:channel/dc:language', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'channel/dc:language', $this->namespaces); + + $feed->language = (string) current($language); + } + + /** + * Find the feed id + * + * @access public + * @param SimpleXMLElement $xml Feed xml + * @param \PicoFeed\Parser\Feed $feed Feed object + */ + public function findFeedId(SimpleXMLElement $xml, Feed $feed) + { + $feed->id = $feed->getFeedUrl() ?: $feed->getSiteUrl(); + } + + /** + * Find the feed date + * + * @access public + * @param SimpleXMLElement $xml Feed xml + * @param \PicoFeed\Parser\Feed $feed Feed object + */ + public function findFeedDate(SimpleXMLElement $xml, Feed $feed) + { + $date = XmlParser::getXPathResult($xml, 'rss:channel/dc:date', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'channel/dc:date', $this->namespaces); + + $feed->date = $this->date->getDateTime((string) current($date)); + } + + /** + * Find the item date + * + * @access public + * @param SimpleXMLElement $entry Feed item + * @param Item $item Item object + * @param \PicoFeed\Parser\Feed $feed Feed object + */ + public function findItemDate(SimpleXMLElement $entry, Item $item, Feed $feed) + { + $date = XmlParser::getXPathResult($entry, 'dc:date', $this->namespaces); + + $item->date = empty($date) ? $feed->getDate() : $this->date->getDateTime((string) current($date)); + } + + /** + * Find the item title + * + * @access public + * @param SimpleXMLElement $entry Feed item + * @param \PicoFeed\Parser\Item $item Item object + */ + public function findItemTitle(SimpleXMLElement $entry, Item $item) + { + $title = XmlParser::getXPathResult($entry, 'rss:title', $this->namespaces) + ?: XmlParser::getXPathResult($entry, 'title'); + + $item->title = Filter::stripWhiteSpace((string) current($title)) ?: $item->url; + } + + /** + * Find the item author + * + * @access public + * @param SimpleXMLElement $xml Feed + * @param SimpleXMLElement $entry Feed item + * @param \PicoFeed\Parser\Item $item Item object + */ + public function findItemAuthor(SimpleXMLElement $xml, SimpleXMLElement $entry, Item $item) + { + $author = XmlParser::getXPathResult($entry, 'dc:creator', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'rss:channel/dc:creator', $this->namespaces) + ?: XmlParser::getXPathResult($xml, 'channel/dc:creator', $this->namespaces); + + $item->author = (string) current($author); + } + + /** + * Find the item content + * + * @access public + * @param SimpleXMLElement $entry Feed item + * @param \PicoFeed\Parser\Item $item Item object + */ + public function findItemContent(SimpleXMLElement $entry, Item $item) + { + $content = XmlParser::getXPathResult($entry, 'content:encoded', $this->namespaces); + + if (trim((string) current($content)) === '') { + $content = XmlParser::getXPathResult($entry, 'rss:description', $this->namespaces) + ?: XmlParser::getXPathResult($entry, 'description'); + } + + $item->content = (string) current($content); + } + + /** + * Find the item URL + * + * @access public + * @param SimpleXMLElement $entry Feed item + * @param \PicoFeed\Parser\Item $item Item object + */ + public function findItemUrl(SimpleXMLElement $entry, Item $item) + { + $link = XmlParser::getXPathResult($entry, 'feedburner:origLink', $this->namespaces) + ?: XmlParser::getXPathResult($entry, 'rss:link', $this->namespaces) + ?: XmlParser::getXPathResult($entry, 'link'); + + $item->url = trim((string) current($link)); } /** * Genereate the item id * * @access public - * @param SimpleXMLElement $entry Feed item + * @param SimpleXMLElement $entry Feed item * @param \PicoFeed\Parser\Item $item Item object * @param \PicoFeed\Parser\Feed $feed Feed object */ @@ -67,11 +263,26 @@ class Rss10 extends Rss20 * Find the item enclosure * * @access public - * @param SimpleXMLElement $entry Feed item + * @param SimpleXMLElement $entry Feed item * @param \PicoFeed\Parser\Item $item Item object * @param \PicoFeed\Parser\Feed $feed Feed object */ public function findItemEnclosure(SimpleXMLElement $entry, Item $item, Feed $feed) { } + + /** + * Find the item language + * + * @access public + * @param SimpleXMLElement $entry Feed item + * @param \PicoFeed\Parser\Item $item Item object + * @param \PicoFeed\Parser\Feed $feed Feed object + */ + public function findItemLanguage(SimpleXMLElement $entry, Item $item, Feed $feed) + { + $language = XmlParser::getXPathResult($entry, 'dc:language', $this->namespaces); + + $item->language = (string) current($language) ?: $feed->language; + } } 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)) ?: $ |