miniflux-legacy/vendor/PicoFeed/Parsers/Rss20.php

301 lines
8.4 KiB
PHP
Raw Normal View History

2013-04-05 05:34:07 +02:00
<?php
2013-06-29 03:50:15 +02:00
namespace PicoFeed\Parsers;
2013-04-05 05:34:07 +02:00
2014-05-20 20:20:27 +02:00
use SimpleXMLElement;
use PicoFeed\Parser;
use PicoFeed\XmlParser;
use PicoFeed\Logging;
use PicoFeed\Feed;
2014-10-19 20:42:31 +02:00
use PicoFeed\Filter;
2014-05-20 20:20:27 +02:00
use PicoFeed\Item;
2014-10-19 20:42:31 +02:00
use PicoFeed\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-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
{
2014-05-20 20:20:27 +02:00
return $xml->channel->item;
}
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\Feed $feed Feed object
*/
public function findFeedUrl(SimpleXMLElement $xml, Feed $feed)
{
2013-06-27 01:30:46 +02:00
if ($xml->channel->link && $xml->channel->link->count() > 1) {
2013-04-12 21:57:54 +02:00
foreach ($xml->channel->link as $xml_link) {
$link = (string) $xml_link;
if ($link !== '') {
2014-05-20 20:20:27 +02:00
$feed->url = $link;
2013-04-12 21:57:54 +02:00
break;
}
}
}
else {
2014-05-20 20:20:27 +02:00
$feed->url = (string) $xml->channel->link;
2013-04-12 21:57:54 +02:00
}
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\Feed $feed Feed object
*/
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\Feed $feed Feed object
*/
public function findFeedLogo(SimpleXMLElement $xml, Feed $feed)
{
if (isset($xml->channel->image->url)) {
$feed->logo = (string) $xml->channel->image->url;
}
}
2014-05-20 20:20:27 +02:00
/**
* Find the feed title
*
* @access public
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Feed $feed Feed object
*/
public function findFeedTitle(SimpleXMLElement $xml, Feed $feed)
{
2014-10-19 20:42:31 +02:00
$feed->title = Filter::stripWhiteSpace((string) $xml->channel->title) ?: $feed->url;
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\Feed $feed Feed object
*/
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\Feed $feed Feed object
*/
public function findFeedId(SimpleXMLElement $xml, Feed $feed)
{
$feed->id = $feed->url;
}
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\Feed $feed Feed object
*/
public function findFeedDate(SimpleXMLElement $xml, Feed $feed)
{
$date = isset($xml->channel->pubDate) ? $xml->channel->pubDate : $xml->channel->lastBuildDate;
$feed->date = $this->parseDate((string) $date);
}
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 \PicoFeed\Item $item Item object
*/
public function findItemDate(SimpleXMLElement $entry, Item $item)
{
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
2014-05-20 20:20:27 +02:00
$item->date = $this->parseDate($date);
}
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\Item $item Item object
*/
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\Item $item Item object
*/
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\Item $item Item object
*/
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
2014-05-20 20:20:27 +02:00
if (empty($content) && $entry->description->count() > 0) {
$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\Item $item Item object
*/
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) {
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\Item $item Item object
* @param \PicoFeed\Feed $feed Feed object
*/
public function findItemId(SimpleXMLElement $entry, Item $item, Feed $feed)
{
2014-05-29 01:16:36 +02:00
$item_permalink = $item->url;
2013-05-06 03:44:03 +02:00
2014-05-20 20:20:27 +02:00
if ($this->isExcludedFromId($feed->url)) {
$feed_permalink = '';
}
else {
$feed_permalink = $feed->url;
}
2014-02-18 04:04:49 +01:00
if ($entry->guid->count() > 0 && ((string) $entry->guid['isPermaLink'] === 'false' || ! isset($entry->guid['isPermaLink']))) {
2014-05-29 01:16:36 +02:00
$item->id = $this->generateId($item_permalink, $feed_permalink, (string) $entry->guid);
}
else {
$item->id = $this->generateId($item_permalink, $feed_permalink);
}
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\Item $item Item object
* @param \PicoFeed\Feed $feed Feed object
*/
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'] : '';
2014-10-19 20:42:31 +02:00
$item->enclosure_url = Url::resolve($item->enclosure_url, $feed->url);
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\Item $item Item object
* @param \PicoFeed\Feed $feed Feed object
*/
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
}