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

271 lines
8.4 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');
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
{
2015-07-19 17:19:26 +02:00
$feed->feed_url = '';
}
/**
* 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)
{
$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.
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');
$feed->description = (string) current($description);
}
/**
* 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');
$feed->logo = (string) current($logo);
}
/**
* 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->icon = '';
}
/**
* 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');
$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, 'rss:channel/dc:language', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'channel/dc:language', $this->namespaces);
$feed->language = (string) current($language);
}
/**
* 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->id = $feed->getFeedUrl() ?: $feed->getSiteUrl();
}
/**
* 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->date = $this->getDateParser()->getDateTime((string) current($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->date = empty($date) ? $feed->getDate() : $this->getDateParser()->getDateTime((string) current($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');
$item->title = Filter::stripWhiteSpace((string) current($title)) ?: $item->url;
}
/**
* 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->author = (string) current($author);
}
/**
* 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 (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.
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');
$item->url = trim((string) current($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->id = $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->language = (string) current($language) ?: $feed->language;
}
2014-05-20 20:20:27 +02:00
}