miniflux-legacy/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Rss10.php

278 lines
8.6 KiB
PHP
Raw Normal View History

2013-04-05 05:34:07 +02:00
<?php
namespace PicoFeed\Parser;
2014-05-20 20:20:27 +02:00
use SimpleXMLElement;
2015-07-19 17:19:26 +02:00
use PicoFeed\Filter\Filter;
2014-05-20 20:20:27 +02:00
/**
* RSS 1.0 parser.
2014-05-20 20:20:27 +02:00
*
* @author Frederic Guillot
*/
2015-07-19 17:19:26 +02:00
class Rss10 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(
'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',
);
2014-05-20 20:20:27 +02:00
/**
* Get the path to the items XML tree.
*
* @param SimpleXMLElement $xml Feed xml
2014-05-20 20:20:27 +02:00
*
* @return SimpleXMLElement
*/
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, 'rss:item', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'item')
?: $xml->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
*/
2015-07-19 17:19:26 +02:00
public function findFeedUrl(SimpleXMLElement $xml, Feed $feed)
2014-05-20 20:20:27 +02:00
{
$feed->setFeedUrl('');
2015-07-19 17:19:26 +02:00
}
/**
* Find the site url.
2015-07-19 17:19:26 +02:00
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
2015-07-19 17:19:26 +02:00
*/
public function findSiteUrl(SimpleXMLElement $xml, Feed $feed)
{
$value = XmlParser::getXPathResult($xml, 'rss:channel/rss:link', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'channel/link')
?: $xml->channel->link;
2015-07-19 17:19:26 +02:00
$feed->setSiteUrl(XmlParser::getValue($value));
2015-07-19 17:19:26 +02:00
}
/**
* Find the feed description.
2015-07-19 17:19:26 +02:00
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
2015-07-19 17:19:26 +02:00
*/
public function findFeedDescription(SimpleXMLElement $xml, Feed $feed)
{
$description = XmlParser::getXPathResult($xml, 'rss:channel/rss:description', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'channel/description')
?: $xml->channel->description;
2015-07-19 17:19:26 +02:00
$feed->setDescription(XmlParser::getValue($description));
2015-07-19 17:19:26 +02:00
}
/**
* Find the feed logo url.
2015-07-19 17:19:26 +02:00
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
2015-07-19 17:19:26 +02:00
*/
public function findFeedLogo(SimpleXMLElement $xml, Feed $feed)
{
$logo = XmlParser::getXPathResult($xml, 'rss:image/rss:url', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'image/url');
2015-07-19 17:19:26 +02:00
$feed->setLogo(XmlParser::getValue($logo));
2015-07-19 17:19:26 +02:00
}
/**
* Find the feed icon.
2015-07-19 17:19:26 +02:00
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
2015-07-19 17:19:26 +02:00
*/
public function findFeedIcon(SimpleXMLElement $xml, Feed $feed)
{
$feed->setIcon('');
2015-07-19 17:19:26 +02:00
}
/**
* Find the feed title.
2015-07-19 17:19:26 +02:00
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
2015-07-19 17:19:26 +02:00
*/
public function findFeedTitle(SimpleXMLElement $xml, Feed $feed)
{
$title = XmlParser::getXPathResult($xml, 'rss:channel/rss:title', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'channel/title')
?: $xml->channel->title;
2015-07-19 17:19:26 +02:00
$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)
{
2015-07-19 17:19:26 +02:00
$language = XmlParser::getXPathResult($xml, 'rss:channel/dc:language', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'channel/dc:language', $this->namespaces);
$feed->setLanguage(XmlParser::getValue($language));
2015-07-19 17:19:26 +02:00
}
/**
* Find the feed id.
2015-07-19 17:19:26 +02:00
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
2015-07-19 17:19:26 +02:00
*/
public function findFeedId(SimpleXMLElement $xml, Feed $feed)
{
$feed->setId($feed->getFeedUrl() ?: $feed->getSiteUrl());
2015-07-19 17:19:26 +02:00
}
/**
* Find the feed date.
2015-07-19 17:19:26 +02:00
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
2015-07-19 17:19:26 +02:00
*/
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->setDate($this->getDateParser()->getDateTime(XmlParser::getValue($date)));
2015-07-19 17:19:26 +02:00
}
/**
* Find the item date.
2015-07-19 17:19:26 +02:00
*
* @param SimpleXMLElement $entry Feed item
* @param Item $item Item object
* @param \PicoFeed\Parser\Feed $feed Feed object
2015-07-19 17:19:26 +02:00
*/
public function findItemDate(SimpleXMLElement $entry, Item $item, Feed $feed)
{
$date = XmlParser::getXPathResult($entry, 'dc:date', $this->namespaces);
$item->setDate(empty($date) ? $feed->getDate() : $this->getDateParser()->getDateTime(XmlParser::getValue($date)));
2015-07-19 17:19:26 +02:00
}
/**
* Find the item title.
2015-07-19 17:19:26 +02:00
*
* @param SimpleXMLElement $entry Feed item
* @param \PicoFeed\Parser\Item $item Item object
2015-07-19 17:19:26 +02:00
*/
public function findItemTitle(SimpleXMLElement $entry, Item $item)
{
$title = XmlParser::getXPathResult($entry, 'rss:title', $this->namespaces)
?: XmlParser::getXPathResult($entry, 'title')
?: $entry->title;
2015-07-19 17:19:26 +02:00
$item->setTitle(Filter::stripWhiteSpace(XmlParser::getValue($title)) ?: $item->getUrl());
2015-07-19 17:19:26 +02:00
}
/**
* Find the item author.
2015-07-19 17:19:26 +02:00
*
* @param SimpleXMLElement $xml Feed
* @param SimpleXMLElement $entry Feed item
* @param \PicoFeed\Parser\Item $item Item object
2015-07-19 17:19:26 +02:00
*/
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->setAuthor(XmlParser::getValue($author));
2015-07-19 17:19:26 +02:00
}
/**
* Find the item content.
2015-07-19 17:19:26 +02:00
*
* @param SimpleXMLElement $entry Feed item
* @param \PicoFeed\Parser\Item $item Item object
2015-07-19 17:19:26 +02:00
*/
public function findItemContent(SimpleXMLElement $entry, Item $item)
{
$content = XmlParser::getXPathResult($entry, 'content:encoded', $this->namespaces);
if (XmlParser::getValue($content) === '') {
2015-07-19 17:19:26 +02:00
$content = XmlParser::getXPathResult($entry, 'rss:description', $this->namespaces)
?: XmlParser::getXPathResult($entry, 'description')
?: $entry->description;
2015-07-19 17:19:26 +02:00
}
$item->setContent(XmlParser::getValue($content));
2015-07-19 17:19:26 +02:00
}
/**
* Find the item URL.
2015-07-19 17:19:26 +02:00
*
* @param SimpleXMLElement $entry Feed item
* @param \PicoFeed\Parser\Item $item Item object
2015-07-19 17:19:26 +02:00
*/
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')
?: $entry->link;
2015-07-19 17:19:26 +02:00
$item->setUrl(XmlParser::getValue($link));
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)
{
$item->setId($this->generateId(
$item->getTitle(), $item->getUrl(), $item->getContent()
));
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 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)
{
2013-04-05 05:34:07 +02:00
}
2015-07-19 17:19:26 +02:00
/**
* Find the item language.
2015-07-19 17:19:26 +02:00
*
* @param SimpleXMLElement $entry Feed item
* @param \PicoFeed\Parser\Item $item Item object
* @param \PicoFeed\Parser\Feed $feed Feed object
2015-07-19 17:19:26 +02:00
*/
public function findItemLanguage(SimpleXMLElement $entry, Item $item, Feed $feed)
{
$language = XmlParser::getXPathResult($entry, 'dc:language', $this->namespaces);
$item->setLanguage(XmlParser::getValue($language) ?: $feed->getLanguage());
2015-07-19 17:19:26 +02:00
}
2014-05-20 20:20:27 +02:00
}