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

308 lines
8.8 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
*
* @author Frederic Guillot
* @package Parser
2014-03-17 02:35:57 +01:00
*/
2014-05-20 20:20:27 +02:00
class Rss20 extends Parser
2013-04-05 05:34:07 +02:00
{
2014-03-17 02:35:57 +01:00
/**
2014-05-20 20:20:27 +02:00
* Get the path to the items XML tree
2014-03-17 02:35:57 +01:00
*
* @access public
2014-05-20 20:20:27 +02:00
* @param SimpleXMLElement $xml Feed xml
* @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-06-21 15:56:36 +02:00
$items = array();
if (isset($xml->channel->item)) {
$items = $xml->channel->item;
}
return $items;
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
*
* @access public
* @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
*
* @access public
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
*/
public function findSiteUrl(SimpleXMLElement $xml, Feed $feed)
{
$feed->site_url = (string) $xml->channel->link;
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
*
* @access public
* @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)
{
$feed->description = (string) $xml->channel->description;
}
/**
* Find the feed logo url
*
* @access public
* @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)
{
if (isset($xml->channel->image->url)) {
$feed->logo = (string) $xml->channel->image->url;
}
}
/**
* 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 = '';
}
2014-05-20 20:20:27 +02:00
/**
* Find the feed title
*
* @access public
* @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)
{
$feed->title = Filter::stripWhiteSpace((string) $xml->channel->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
*
* @access public
* @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)
{
$feed->language = isset($xml->channel->language) ? (string) $xml->channel->language : '';
}
2014-05-20 20:20:27 +02:00
/**
* Find the feed id
*
* @access public
* @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
*
* @access public
* @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)
{
$date = isset($xml->channel->pubDate) ? $xml->channel->pubDate : $xml->channel->lastBuildDate;
2015-03-01 19:56:11 +01:00
$feed->date = $this->date->getDateTime((string) $date);
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
*
* @access public
* @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
{
2014-10-19 20:42:31 +02:00
$date = XmlParser::getNamespaceValue($entry, $this->namespaces, 'date');
2013-04-05 05:34:07 +02:00
2014-05-20 20:20:27 +02:00
if (empty($date)) {
2014-10-19 20:42:31 +02:00
$date = XmlParser::getNamespaceValue($entry, $this->namespaces, 'updated');
2014-05-20 20:20:27 +02:00
}
2013-04-05 05:34:07 +02:00
2014-05-20 20:20:27 +02:00
if (empty($date)) {
$date = (string) $entry->pubDate;
}
2013-04-05 05:34:07 +02:00
$item->date = empty($date) ? $feed->getDate() : $this->date->getDateTime($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
*
* @access public
* @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)
{
2014-10-19 20:42:31 +02:00
$item->title = Filter::stripWhiteSpace((string) $entry->title);
2013-04-05 05:34:07 +02:00
2014-05-20 20:20:27 +02:00
if (empty($item->title)) {
$item->title = $item->url;
}
}
2014-05-20 20:20:27 +02:00
/**
* Find the item author
*
* @access public
* @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)
{
2014-10-19 20:42:31 +02:00
$item->author = XmlParser::getNamespaceValue($entry, $this->namespaces, 'creator');
2014-05-20 20:20:27 +02:00
if (empty($item->author)) {
if (isset($entry->author)) {
$item->author = (string) $entry->author;
}
else if (isset($xml->channel->webMaster)) {
$item->author = (string) $xml->channel->webMaster;
}
2014-05-20 20:20:27 +02:00
}
}
2014-05-20 20:20:27 +02:00
/**
* Find the item content
*
* @access public
* @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)
{
2014-10-19 20:42:31 +02:00
$content = XmlParser::getNamespaceValue($entry, $this->namespaces, 'encoded');
2013-04-05 05:34:07 +02:00
2015-03-01 19:56:11 +01:00
if (trim($content) === '' && $entry->description->count() > 0) {
2014-05-20 20:20:27 +02:00
$content = (string) $entry->description;
}
2013-04-05 05:34:07 +02:00
2014-10-19 20:42:31 +02:00
$item->content = $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
*
* @access public
* @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)
{
$links = array(
2014-10-19 20:42:31 +02:00
XmlParser::getNamespaceValue($entry, $this->namespaces, 'origLink'),
isset($entry->link) ? (string) $entry->link : '',
2014-10-19 20:42:31 +02:00
XmlParser::getNamespaceValue($entry, $this->namespaces, 'link', 'href'),
isset($entry->guid) ? (string) $entry->guid : '',
);
foreach ($links as $link) {
2015-07-03 03:03:00 +02:00
$link = trim($link);
if (! empty($link) && filter_var($link, FILTER_VALIDATE_URL) !== false) {
$item->url = $link;
break;
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
*
* @access public
* @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 = (string) $entry->guid;
2014-02-18 04:04:49 +01:00
if ($id) {
$item->id = $this->generateId($id);
2014-05-29 01:16:36 +02:00
}
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
*
* @access public
* @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)) {
2014-02-18 04:04:49 +01:00
2014-10-19 20:42:31 +02:00
$item->enclosure_url = XmlParser::getNamespaceValue($entry->enclosure, $this->namespaces, 'origEnclosureLink');
2014-05-20 20:20:27 +02:00
if (empty($item->enclosure_url)) {
$item->enclosure_url = isset($entry->enclosure['url']) ? (string) $entry->enclosure['url'] : '';
}
2014-05-20 20:20:27 +02:00
$item->enclosure_type = isset($entry->enclosure['type']) ? (string) $entry->enclosure['type'] : '';
$item->enclosure_url = Url::resolve($item->enclosure_url, $feed->getSiteUrl());
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
*
* @access public
* @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)
{
$item->language = $feed->language;
2013-04-05 05:34:07 +02:00
}
2014-05-20 20:20:27 +02:00
}