2013-04-05 05:34:07 +02:00
|
|
|
<?php
|
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
namespace PicoFeed\Parser;
|
2013-04-05 05:34:07 +02:00
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
use SimpleXMLElement;
|
2014-12-24 03:28:26 +01:00
|
|
|
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
|
2014-12-24 03:28:26 +01:00
|
|
|
* @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
|
|
|
{
|
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
|
2014-12-24 03:28:26 +01: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)
|
|
|
|
{
|
2014-12-24 03:28:26 +01:00
|
|
|
$feed->feed_url = '';
|
|
|
|
}
|
2013-04-12 21:57:54 +02:00
|
|
|
|
2014-12-24 03:28:26 +01: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
|
2014-12-24 03:28:26 +01: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)
|
|
|
|
{
|
|
|
|
$feed->description = (string) $xml->channel->description;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the feed logo url
|
|
|
|
*
|
|
|
|
* @access public
|
2014-12-24 03:28:26 +01: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)
|
|
|
|
{
|
|
|
|
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
|
2014-12-24 03:28:26 +01: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)
|
|
|
|
{
|
2014-12-24 03:28:26 +01:00
|
|
|
$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
|
2014-12-24 03:28:26 +01: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)
|
|
|
|
{
|
|
|
|
$feed->language = isset($xml->channel->language) ? (string) $xml->channel->language : '';
|
|
|
|
}
|
2014-01-12 01:46:30 +01:00
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Find the feed id
|
|
|
|
*
|
|
|
|
* @access public
|
2014-12-24 03:28:26 +01: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)
|
|
|
|
{
|
2014-12-24 03:28:26 +01:00
|
|
|
$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
|
2014-12-24 03:28:26 +01: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)
|
|
|
|
{
|
|
|
|
$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
|
2014-12-24 03:28:26 +01:00
|
|
|
* @param SimpleXMLElement $entry Feed item
|
|
|
|
* @param \PicoFeed\Parser\Item $item Item object
|
2014-05-20 20:20:27 +02:00
|
|
|
*/
|
|
|
|
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
|
2014-12-24 03:28:26 +01: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)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
}
|
2013-06-29 19:41:36 +02:00
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Find the item author
|
|
|
|
*
|
|
|
|
* @access public
|
2014-12-24 03:28:26 +01: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)
|
|
|
|
{
|
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;
|
2013-06-29 19:41:36 +02:00
|
|
|
}
|
2014-05-20 20:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-29 19:41:36 +02:00
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Find the item content
|
|
|
|
*
|
|
|
|
* @access public
|
2014-12-24 03:28:26 +01: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)
|
|
|
|
{
|
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
|
2014-12-24 03:28:26 +01: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)
|
|
|
|
{
|
2014-05-24 18:21:07 +02:00
|
|
|
$links = array(
|
2014-10-19 20:42:31 +02:00
|
|
|
XmlParser::getNamespaceValue($entry, $this->namespaces, 'origLink'),
|
2014-05-24 18:21:07 +02:00
|
|
|
isset($entry->link) ? (string) $entry->link : '',
|
2014-10-19 20:42:31 +02:00
|
|
|
XmlParser::getNamespaceValue($entry, $this->namespaces, 'link', 'href'),
|
2014-05-24 18:21:07 +02:00
|
|
|
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
|
2014-12-24 03:28:26 +01: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)
|
|
|
|
{
|
2014-12-24 03:28:26 +01:00
|
|
|
$id = (string) $entry->guid;
|
2014-02-18 04:04:49 +01:00
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
if ($id) {
|
|
|
|
$item->id = $this->generateId($id);
|
2014-05-29 01:16:36 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-12-24 03:28:26 +01:00
|
|
|
$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
|
2014-12-24 03:28:26 +01: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)) {
|
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-02-17 19:41:22 +01:00
|
|
|
}
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
$item->enclosure_type = isset($entry->enclosure['type']) ? (string) $entry->enclosure['type'] : '';
|
2014-12-24 03:28:26 +01:00
|
|
|
$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
|
2014-12-24 03:28:26 +01:00
|
|
|
* @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
|
|
|
}
|