miniflux-legacy/vendor/miniflux/picofeed/lib/PicoFeed/Parser/Rss20.php

318 lines
10 KiB
PHP
Raw Permalink Normal View History

2013-04-05 05:34:07 +02:00
<?php
namespace PicoFeed\Parser;
2013-04-05 05:34:07 +02:00
2014-05-20 20:20:27 +02:00
use SimpleXMLElement;
use PicoFeed\Filter\Filter;
use PicoFeed\Client\Url;
2014-05-20 20:20:27 +02:00
2014-03-17 02:35:57 +01:00
/**
* RSS 2.0 Parser.
2014-03-17 02:35:57 +01:00
*
2016-07-29 03:14:51 +02:00
* @package PicoFeed\Parser
2014-03-17 02:35:57 +01:00
* @author Frederic Guillot
*/
2014-05-20 20:20:27 +02:00
class Rss20 extends Parser
2013-04-05 05:34:07 +02:00
{
2015-07-19 17:19:26 +02:00
/**
* Supported namespaces.
2015-07-19 17:19:26 +02:00
*/
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',
2015-07-19 17:19:26 +02:00
);
2014-03-17 02:35:57 +01:00
/**
* Get the path to the items XML tree.
*
* @param SimpleXMLElement $xml Feed xml
2017-06-21 01:41:55 +02:00
* @return SimpleXMLElement[]
2014-03-17 02:35:57 +01:00
*/
2014-05-20 20:20:27 +02:00
public function getItemsTree(SimpleXMLElement $xml)
2013-04-05 05:34:07 +02:00
{
2015-07-19 17:19:26 +02:00
return XmlParser::getXPathResult($xml, 'channel/item');
2014-05-20 20:20:27 +02:00
}
2013-04-05 05:34:07 +02:00
2014-05-20 20:20:27 +02:00
/**
* Find the feed url.
2014-05-20 20:20:27 +02:00
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
2014-05-20 20:20:27 +02:00
*/
public function findFeedUrl(SimpleXMLElement $xml, Feed $feed)
{
$feed->setFeedUrl('');
}
2013-04-12 21:57:54 +02:00
/**
* Find the site url.
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
*/
public function findSiteUrl(SimpleXMLElement $xml, Feed $feed)
{
$value = XmlParser::getXPathResult($xml, 'channel/link');
$feed->setSiteUrl(XmlParser::getValue($value));
2014-05-20 20:20:27 +02:00
}
2013-04-12 21:57:54 +02:00
2014-10-19 20:42:31 +02:00
/**
* Find the feed description.
2014-10-19 20:42:31 +02:00
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
2014-10-19 20:42:31 +02:00
*/
public function findFeedDescription(SimpleXMLElement $xml, Feed $feed)
{
$value = XmlParser::getXPathResult($xml, 'channel/description');
$feed->setDescription(XmlParser::getValue($value));
2014-10-19 20:42:31 +02:00
}
/**
* Find the feed logo url.
2014-10-19 20:42:31 +02:00
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
2014-10-19 20:42:31 +02:00
*/
public function findFeedLogo(SimpleXMLElement $xml, Feed $feed)
{
$value = XmlParser::getXPathResult($xml, 'channel/image/url');
$feed->setLogo(XmlParser::getValue($value));
2014-10-19 20:42:31 +02:00
}
/**
* Find the feed icon.
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
*/
public function findFeedIcon(SimpleXMLElement $xml, Feed $feed)
{
$feed->setIcon('');
}
2014-05-20 20:20:27 +02:00
/**
* Find the feed title.
2014-05-20 20:20:27 +02:00
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
2014-05-20 20:20:27 +02:00
*/
public function findFeedTitle(SimpleXMLElement $xml, Feed $feed)
{
2015-07-19 17:19:26 +02:00
$title = XmlParser::getXPathResult($xml, 'channel/title');
$feed->setTitle(Filter::stripWhiteSpace(XmlParser::getValue($title)) ?: $feed->getSiteUrl());
2014-05-20 20:20:27 +02:00
}
2013-04-05 05:34:07 +02:00
2014-05-20 20:20:27 +02:00
/**
* Find the feed language.
2014-05-20 20:20:27 +02:00
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
2014-05-20 20:20:27 +02:00
*/
public function findFeedLanguage(SimpleXMLElement $xml, Feed $feed)
{
$value = XmlParser::getXPathResult($xml, 'channel/language');
$feed->setLanguage(XmlParser::getValue($value));
2014-05-20 20:20:27 +02:00
}
2014-05-20 20:20:27 +02:00
/**
* Find the feed id.
2014-05-20 20:20:27 +02:00
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
2014-05-20 20:20:27 +02:00
*/
public function findFeedId(SimpleXMLElement $xml, Feed $feed)
{
$feed->setId($feed->getFeedUrl() ?: $feed->getSiteUrl());
2014-05-20 20:20:27 +02:00
}
2013-06-27 01:30:46 +02:00
2014-05-20 20:20:27 +02:00
/**
* Find the feed date.
2014-05-20 20:20:27 +02:00
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
2014-05-20 20:20:27 +02:00
*/
public function findFeedDate(SimpleXMLElement $xml, Feed $feed)
{
2015-07-19 17:19:26 +02:00
$publish_date = XmlParser::getXPathResult($xml, 'channel/pubDate');
$update_date = XmlParser::getXPathResult($xml, 'channel/lastBuildDate');
$published = !empty($publish_date) ? $this->getDateParser()->getDateTime(XmlParser::getValue($publish_date)) : null;
$updated = !empty($update_date) ? $this->getDateParser()->getDateTime(XmlParser::getValue($update_date)) : null;
2015-07-19 17:19:26 +02:00
if ($published === null && $updated === null) {
$feed->setDate($this->getDateParser()->getCurrentDateTime()); // We use the current date if there is no date for the feed
} elseif ($published !== null && $updated !== null) {
$feed->setDate(max($published, $updated)); // We use the most recent date between published and updated
} else {
$feed->setDate($updated ?: $published);
2015-07-19 17:19:26 +02:00
}
2014-05-20 20:20:27 +02:00
}
2013-04-05 05:34:07 +02:00
2014-05-20 20:20:27 +02:00
/**
2016-07-29 03:14:51 +02:00
* Find the item published date.
2014-05-20 20:20:27 +02:00
*
* @param SimpleXMLElement $entry Feed item
* @param Item $item Item object
* @param \PicoFeed\Parser\Feed $feed Feed object
2014-05-20 20:20:27 +02:00
*/
2016-07-29 03:14:51 +02:00
public function findItemPublishedDate(SimpleXMLElement $entry, Item $item, Feed $feed)
2014-05-20 20:20:27 +02:00
{
2015-07-19 17:19:26 +02:00
$date = XmlParser::getXPathResult($entry, 'pubDate');
2013-04-05 05:34:07 +02:00
2016-07-29 03:14:51 +02:00
$item->setPublishedDate(!empty($date) ? $this->getDateParser()->getDateTime(XmlParser::getValue($date)) : null);
}
/**
* Find the item updated date.
*
* @param SimpleXMLElement $entry Feed item
* @param Item $item Item object
* @param \PicoFeed\Parser\Feed $feed Feed object
*/
public function findItemUpdatedDate(SimpleXMLElement $entry, Item $item, Feed $feed)
{
if ($item->publishedDate === null) {
$this->findItemPublishedDate($entry, $item, $feed);
}
$item->setUpdatedDate($item->getPublishedDate()); // No updated date in RSS 2.0 specifications
2014-05-20 20:20:27 +02:00
}
2014-02-18 04:04:49 +01:00
2014-05-20 20:20:27 +02:00
/**
* Find the item title.
2014-05-20 20:20:27 +02:00
*
* @param SimpleXMLElement $entry Feed item
* @param \PicoFeed\Parser\Item $item Item object
2014-05-20 20:20:27 +02:00
*/
public function findItemTitle(SimpleXMLElement $entry, Item $item)
{
$value = XmlParser::getXPathResult($entry, 'title');
$item->setTitle(Filter::stripWhiteSpace(XmlParser::getValue($value)) ?: $item->getUrl());
2014-05-20 20:20:27 +02:00
}
2014-05-20 20:20:27 +02:00
/**
* Find the item author.
2014-05-20 20:20:27 +02:00
*
* @param SimpleXMLElement $xml Feed
* @param SimpleXMLElement $entry Feed item
* @param \PicoFeed\Parser\Item $item Item object
2014-05-20 20:20:27 +02:00
*/
public function findItemAuthor(SimpleXMLElement $xml, SimpleXMLElement $entry, Item $item)
{
$value = XmlParser::getXPathResult($entry, 'dc:creator', $this->namespaces)
2015-07-19 17:19:26 +02:00
?: XmlParser::getXPathResult($entry, 'author')
?: XmlParser::getXPathResult($xml, 'channel/dc:creator', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'channel/managingEditor');
2014-05-20 20:20:27 +02:00
$item->setAuthor(XmlParser::getValue($value));
2014-05-20 20:20:27 +02:00
}
2014-05-20 20:20:27 +02:00
/**
* Find the item content.
2014-05-20 20:20:27 +02:00
*
* @param SimpleXMLElement $entry Feed item
* @param \PicoFeed\Parser\Item $item Item object
2014-05-20 20:20:27 +02:00
*/
public function findItemContent(SimpleXMLElement $entry, Item $item)
{
2015-07-19 17:19:26 +02:00
$content = XmlParser::getXPathResult($entry, 'content:encoded', $this->namespaces);
2013-04-05 05:34:07 +02:00
if (XmlParser::getValue($content) === '') {
2015-07-19 17:19:26 +02:00
$content = XmlParser::getXPathResult($entry, 'description');
2014-05-20 20:20:27 +02:00
}
2013-04-05 05:34:07 +02:00
$item->setContent(XmlParser::getValue($content));
2014-05-20 20:20:27 +02:00
}
2013-04-05 05:34:07 +02:00
2014-05-20 20:20:27 +02:00
/**
* Find the item URL.
2014-05-20 20:20:27 +02:00
*
* @param SimpleXMLElement $entry Feed item
* @param \PicoFeed\Parser\Item $item Item object
2014-05-20 20:20:27 +02:00
*/
public function findItemUrl(SimpleXMLElement $entry, Item $item)
{
2015-07-19 17:19:26 +02:00
$link = XmlParser::getXPathResult($entry, 'feedburner:origLink', $this->namespaces)
?: XmlParser::getXPathResult($entry, 'link')
?: XmlParser::getXPathResult($entry, 'atom:link/@href', $this->namespaces);
if (!empty($link)) {
$item->setUrl(XmlParser::getValue($link));
} else {
2015-07-19 17:19:26 +02:00
$link = XmlParser::getXPathResult($entry, 'guid');
$link = XmlParser::getValue($link);
2015-07-19 17:19:26 +02:00
if (filter_var($link, FILTER_VALIDATE_URL) !== false) {
$item->setUrl($link);
2013-04-07 16:58:46 +02:00
}
2014-05-20 20:20:27 +02:00
}
}
2013-04-05 05:34:07 +02:00
2014-05-20 20:20:27 +02:00
/**
* Genereate the item id.
2014-05-20 20:20:27 +02:00
*
* @param SimpleXMLElement $entry Feed item
* @param \PicoFeed\Parser\Item $item Item object
* @param \PicoFeed\Parser\Feed $feed Feed object
2014-05-20 20:20:27 +02:00
*/
public function findItemId(SimpleXMLElement $entry, Item $item, Feed $feed)
{
$id = XmlParser::getValue(XmlParser::getXPathResult($entry, 'guid'));
2014-02-18 04:04:49 +01:00
if ($id) {
$item->setId($this->generateId($id));
} else {
$item->setId($this->generateId(
$item->getTitle(), $item->getUrl(), $item->getContent()
));
2014-05-29 01:16:36 +02:00
}
2014-05-20 20:20:27 +02:00
}
2014-02-18 04:04:49 +01:00
2014-05-20 20:20:27 +02:00
/**
* Find the item enclosure.
2014-05-20 20:20:27 +02:00
*
* @param SimpleXMLElement $entry Feed item
* @param \PicoFeed\Parser\Item $item Item object
* @param \PicoFeed\Parser\Feed $feed Feed object
2014-05-20 20:20:27 +02:00
*/
public function findItemEnclosure(SimpleXMLElement $entry, Item $item, Feed $feed)
{
if (isset($entry->enclosure)) {
$type = XmlParser::getXPathResult($entry, 'enclosure/@type');
$url = XmlParser::getXPathResult($entry, 'feedburner:origEnclosureLink', $this->namespaces)
?: XmlParser::getXPathResult($entry, 'enclosure/@url');
2014-02-18 04:04:49 +01:00
$item->setEnclosureUrl(Url::resolve(XmlParser::getValue($url), $feed->getSiteUrl()));
$item->setEnclosureType(XmlParser::getValue($type));
2014-05-20 20:20:27 +02:00
}
}
2013-08-30 01:34:11 +02:00
2014-05-20 20:20:27 +02:00
/**
* Find the item language.
2014-05-20 20:20:27 +02:00
*
* @param SimpleXMLElement $entry Feed item
* @param \PicoFeed\Parser\Item $item Item object
* @param \PicoFeed\Parser\Feed $feed Feed object
2014-05-20 20:20:27 +02:00
*/
public function findItemLanguage(SimpleXMLElement $entry, Item $item, Feed $feed)
{
2015-07-19 17:19:26 +02:00
$language = XmlParser::getXPathResult($entry, 'dc:language', $this->namespaces);
$item->setLanguage(XmlParser::getValue($language) ?: $feed->getLanguage());
2013-04-05 05:34:07 +02:00
}
2017-06-12 03:08:00 +02:00
/**
* Find the item categories.
*
* @param SimpleXMLElement $entry Feed item
* @param Item $item Item object
* @param Feed $feed Feed object
*/
public function findItemCategories(SimpleXMLElement $entry, Item $item, Feed $feed)
{
$categories = XmlParser::getXPathResult($entry, 'category');
$item->setCategoriesFromXml($categories);
}
2014-05-20 20:20:27 +02:00
}