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

292 lines
9.2 KiB
PHP
Raw 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
*
* @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
2014-03-17 02:35:57 +01:00
*
2014-05-20 20:20:27 +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->feed_url = '';
}
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)
{
2015-07-19 17:19:26 +02:00
$site_url = XmlParser::getXPathResult($xml, 'channel/link');
$feed->site_url = (string) current($site_url);
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)
{
2015-07-19 17:19:26 +02:00
$description = XmlParser::getXPathResult($xml, 'channel/description');
$feed->description = (string) current($description);
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)
{
2015-07-19 17:19:26 +02:00
$logo = XmlParser::getXPathResult($xml, 'channel/image/url');
$feed->logo = (string) current($logo);
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->icon = '';
}
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->title = Filter::stripWhiteSpace((string) current($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, 'channel/language');
$feed->language = (string) current($language);
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->id = $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->date->getDateTime((string) current($publish_date)) : null;
$updated = !empty($update_date) ? $this->date->getDateTime((string) current($update_date)) : null;
2015-07-19 17:19:26 +02:00
if ($published === null && $updated === null) {
$feed->date = $this->date->getCurrentDateTime(); // We use the current date if there is no date for the feed
} elseif ($published !== null && $updated !== null) {
2015-07-19 17:19:26 +02:00
$feed->date = max($published, $updated); // We use the most recent date between published and updated
} else {
2015-07-19 17:19:26 +02:00
$feed->date = $updated ?: $published;
}
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 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
*/
public function findItemDate(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
2015-07-19 17:19:26 +02:00
$item->date = empty($date) ? $feed->getDate() : $this->date->getDateTime((string) current($date));
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)
{
2015-07-19 17:19:26 +02:00
$title = XmlParser::getXPathResult($entry, 'title');
$item->title = Filter::stripWhiteSpace((string) current($title)) ?: $item->url;
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)
{
2015-07-19 17:19:26 +02:00
$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');
2014-05-20 20:20:27 +02:00
2015-07-19 17:19:26 +02:00
$item->author = (string) current($author);
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
2015-07-19 17:19:26 +02:00
if (trim((string) current($content)) === '') {
$content = XmlParser::getXPathResult($entry, 'description');
2014-05-20 20:20:27 +02:00
}
2013-04-05 05:34:07 +02:00
2015-07-19 17:19:26 +02:00
$item->content = (string) current($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)) {
2015-07-19 17:19:26 +02:00
$item->url = trim((string) current($link));
} else {
2015-07-19 17:19:26 +02:00
$link = XmlParser::getXPathResult($entry, 'guid');
$link = trim((string) current($link));
if (filter_var($link, FILTER_VALIDATE_URL) !== false) {
$item->url = $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)
{
2015-07-19 17:19:26 +02:00
$id = (string) current(XmlParser::getXPathResult($entry, 'guid'));
2014-02-18 04:04:49 +01:00
if ($id) {
$item->id = $this->generateId($id);
} else {
$item->id = $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)) {
2015-07-19 17:19:26 +02:00
$enclosure_url = XmlParser::getXPathResult($entry, 'feedburner:origEnclosureLink', $this->namespaces)
?: XmlParser::getXPathResult($entry, 'enclosure/@url');
2014-02-18 04:04:49 +01:00
2015-07-19 17:19:26 +02:00
$enclosure_type = XmlParser::getXPathResult($entry, 'enclosure/@type');
2014-05-20 20:20:27 +02:00
2015-07-19 17:19:26 +02:00
$item->enclosure_url = Url::resolve((string) current($enclosure_url), $feed->getSiteUrl());
$item->enclosure_type = (string) current($enclosure_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->language = (string) current($language) ?: $feed->language;
2013-04-05 05:34:07 +02:00
}
2014-05-20 20:20:27 +02:00
}