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

273 lines
7.0 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\Filter;
use PicoFeed\Feed;
use PicoFeed\Item;
2014-03-17 02:35:57 +01:00
/**
* Atom parser
*
* @author Frederic Guillot
* @package parser
*/
2014-05-20 20:20:27 +02:00
class Atom 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->entry;
}
2013-08-30 01:34:11 +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)
{
$feed->url = $this->getLink($xml);
}
2013-04-05 05:34:07 +02:00
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)
{
$feed->title = $this->stripWhiteSpace((string) $xml->title) ?: $feed->url;
}
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 = $this->getXmlLang($this->content);
}
2013-04-05 05:34:07 +02:00
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 = (string) $xml->id;
}
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)
{
$feed->date = $this->parseDate((string) $xml->updated);
}
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
*/
public function findItemDate(SimpleXMLElement $entry, Item $item)
{
$item->date = $this->parseDate((string) $entry->updated);
}
2013-04-05 05:34:07 +02:00
2014-05-20 20:20:27 +02:00
/**
* Find the item title
*
* @access public
* @param SimpleXMLElement $entry Feed item
* @param Item $item Item object
*/
public function findItemTitle(SimpleXMLElement $entry, Item $item)
{
$item->title = $this->stripWhiteSpace((string) $entry->title);
2014-02-18 04:04:49 +01:00
2014-05-20 20:20:27 +02:00
if (empty($item->title)) {
$item->title = $item->url;
2013-04-05 05:34:07 +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
/**
* 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)
{
if (isset($entry->author->name)) {
$item->author = (string) $entry->author->name;
}
else {
$item->author = (string) $xml->author->name;
}
}
2013-08-30 01:34:11 +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)
{
$item->content = $this->filterHtml($this->getContent($entry), $item->url);
2013-04-05 05:34:07 +02:00
}
2014-03-17 02:35:57 +01:00
/**
2014-05-20 20:20:27 +02:00
* Find the item URL
2014-03-17 02:35:57 +01:00
*
* @access public
2014-05-20 20:20:27 +02:00
* @param SimpleXMLElement $entry Feed item
* @param \PicoFeed\Item $item Item object
2014-03-17 02:35:57 +01:00
*/
2014-05-20 20:20:27 +02:00
public function findItemUrl(SimpleXMLElement $entry, Item $item)
2013-04-05 05:34:07 +02:00
{
2014-05-20 20:20:27 +02:00
$item->url = $this->getLink($entry);
}
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)
{
$id = (string) $entry->id;
if ($id !== $item->url) {
$item_permalink = $id;
2013-04-05 05:34:07 +02:00
}
2014-05-20 20:20:27 +02:00
else {
$item_permalink = $item->url;
2013-04-05 05:34:07 +02:00
}
2014-05-20 20:20:27 +02:00
if ($this->isExcludedFromId($feed->url)) {
$feed_permalink = '';
}
else {
$feed_permalink = $feed->url;
}
$item->id = $this->generateId($item_permalink, $feed_permalink);
}
/**
* 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)
{
foreach ($entry->link as $link) {
if ((string) $link['rel'] === 'enclosure') {
$item->enclosure_url = (string) $link['href'];
$item->enclosure_type = (string) $link['type'];
if (Filter::isRelativePath($item->enclosure_url)) {
$item->enclosure_url = Filter::getAbsoluteUrl($item->enclosure_url, $feed->url);
}
break;
}
}
}
/**
* 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-03-17 02:35:57 +01:00
/**
* Get the URL from a link tag
*
* @access public
2014-05-20 20:20:27 +02:00
* @param SimpleXMLElement $xml XML tag
2014-03-17 02:35:57 +01:00
* @return string
*/
2014-05-20 20:20:27 +02:00
public function getLink(SimpleXMLElement $xml)
2013-04-05 05:34:07 +02:00
{
foreach ($xml->link as $link) {
2013-04-13 03:09:05 +02:00
if ((string) $link['type'] === 'text/html' || (string) $link['type'] === 'application/xhtml+xml') {
2013-04-05 05:34:07 +02:00
return (string) $link['href'];
}
}
return (string) $xml->link['href'];
}
2014-05-20 20:20:27 +02:00
/**
* Get the entry content
*
* @access public
* @param SimpleXMLElement $entry XML Entry
* @return string
*/
public function getContent(SimpleXMLElement $entry)
{
if (isset($entry->content) && ! empty($entry->content)) {
if (count($entry->content->children())) {
return (string) $entry->content->asXML();
}
else {
return (string) $entry->content;
}
}
else if (isset($entry->summary) && ! empty($entry->summary)) {
return (string) $entry->summary;
}
return '';
}
}