miniflux-legacy/vendor/miniflux/picofeed/lib/PicoFeed/Parser/Atom.php

383 lines
12 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
/**
* Atom parser.
2014-03-17 02:35:57 +01:00
*
2016-07-29 03:14:51 +02:00
* @package PicoFeed\Parser
2014-03-17 02:35:57 +01:00
* @author Frederic Guillot
*/
2014-05-20 20:20:27 +02:00
class Atom 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(
'atom' => 'http://www.w3.org/2005/Atom',
);
2014-03-17 02:35:57 +01:00
/**
* Get the path to the items XML tree.
*
* @param SimpleXMLElement $xml Feed xml
2014-03-17 02:35:57 +01:00
*
2014-05-20 20:20:27 +02:00
* @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-07-19 17:19:26 +02:00
return XmlParser::getXPathResult($xml, 'atom:entry', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'entry');
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 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
*/
public function findFeedUrl(SimpleXMLElement $xml, Feed $feed)
{
$feed->setFeedUrl($this->getUrl($xml, 'self'));
}
/**
* Find the site url.
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
*/
public function findSiteUrl(SimpleXMLElement $xml, Feed $feed)
{
$feed->setSiteUrl($this->getUrl($xml, 'alternate', true));
2014-05-20 20:20:27 +02:00
}
2013-04-05 05:34:07 +02:00
2014-10-19 20:42:31 +02:00
/**
* Find the feed description.
2014-10-19 20:42:31 +02: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)
{
2015-07-19 17:19:26 +02:00
$description = XmlParser::getXPathResult($xml, 'atom:subtitle', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'subtitle');
$feed->setDescription(XmlParser::getValue($description));
2014-10-19 20:42:31 +02:00
}
/**
* Find the feed logo url.
2014-10-19 20:42:31 +02: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)
{
2015-07-19 17:19:26 +02:00
$logo = XmlParser::getXPathResult($xml, 'atom:logo', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'logo');
$feed->setLogo(XmlParser::getValue($logo));
2014-10-19 20:42:31 +02:00
}
/**
* Find the feed icon.
*
* @param SimpleXMLElement $xml Feed xml
* @param \PicoFeed\Parser\Feed $feed Feed object
*/
public function findFeedIcon(SimpleXMLElement $xml, Feed $feed)
{
2015-07-19 17:19:26 +02:00
$icon = XmlParser::getXPathResult($xml, 'atom:icon', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'icon');
$feed->setIcon(XmlParser::getValue($icon));
}
2014-05-20 20:20:27 +02:00
/**
* Find the feed title.
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 findFeedTitle(SimpleXMLElement $xml, Feed $feed)
{
2015-07-19 17:19:26 +02:00
$title = XmlParser::getXPathResult($xml, 'atom:title', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'title');
$feed->setTitle(Filter::stripWhiteSpace(XmlParser::getValue($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, '*[not(self::atom:entry)]/@xml:lang', $this->namespaces)
?: XmlParser::getXPathResult($xml, '@xml:lang');
$feed->setLanguage(XmlParser::getValue($language));
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 id.
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 findFeedId(SimpleXMLElement $xml, Feed $feed)
{
2015-07-19 17:19:26 +02:00
$id = XmlParser::getXPathResult($xml, 'atom:id', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'id');
$feed->setId(XmlParser::getValue($id));
2014-05-20 20:20:27 +02:00
}
2014-05-20 20:20:27 +02:00
/**
* Find the feed date.
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 findFeedDate(SimpleXMLElement $xml, Feed $feed)
{
2015-07-19 17:19:26 +02:00
$updated = XmlParser::getXPathResult($xml, 'atom:updated', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'updated');
$feed->setDate($this->getDateParser()->getDateTime(XmlParser::getValue($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
/**
2016-07-29 03:14:51 +02:00
* Find the item published date.
2014-05-20 20:20:27 +02:00
*
* @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
*/
2016-07-29 03:14:51 +02:00
public function findItemPublishedDate(SimpleXMLElement $entry, Item $item, Feed $feed)
2014-05-20 20:20:27 +02:00
{
2016-07-29 03:14:51 +02:00
$date = XmlParser::getXPathResult($entry, 'atom:published', $this->namespaces)
?: XmlParser::getXPathResult($entry, 'published');
$item->setPublishedDate(!empty($date) ? $this->getDateParser()->getDateTime((string) current($date)) : null);
}
/**
* Find the item updated date.
*
* @param SimpleXMLElement $entry Feed item
* @param Item $item Item object
* @param \PicoFeed\Parser\Feed $feed Feed object
*/
public function findItemUpdatedDate(SimpleXMLElement $entry, Item $item, Feed $feed)
{
$date = XmlParser::getXPathResult($entry, 'atom:updated', $this->namespaces)
?: XmlParser::getXPathResult($entry, 'updated');
$item->setUpdatedDate(!empty($date) ? $this->getDateParser()->getDateTime((string) current($date)) : null);
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 title.
2014-05-20 20:20:27 +02:00
*
* @param SimpleXMLElement $entry Feed item
* @param Item $item Item object
2014-05-20 20:20:27 +02:00
*/
public function findItemTitle(SimpleXMLElement $entry, Item $item)
{
2015-07-19 17:19:26 +02:00
$title = XmlParser::getXPathResult($entry, 'atom:title', $this->namespaces)
?: XmlParser::getXPathResult($entry, 'title');
2014-02-18 04:04:49 +01:00
$item->setTitle(Filter::stripWhiteSpace(XmlParser::getValue($title)) ?: $item->getUrl());
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.
2014-05-20 20:20:27 +02: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)
{
2015-07-19 17:19:26 +02:00
$author = XmlParser::getXPathResult($entry, 'atom:author/atom:name', $this->namespaces)
?: XmlParser::getXPathResult($entry, 'author/name')
?: XmlParser::getXPathResult($xml, 'atom:author/atom:name', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'author/name');
$item->setAuthor(XmlParser::getValue($author));
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 content.
2014-05-20 20:20:27 +02: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)
{
$item->setContent($this->getContent($entry));
2013-04-05 05:34:07 +02:00
}
2014-03-17 02:35:57 +01:00
/**
* Find the item URL.
2014-03-17 02:35:57 +01:00
*
* @param SimpleXMLElement $entry Feed item
* @param \PicoFeed\Parser\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
{
$item->setUrl($this->getUrl($entry, 'alternate', true));
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)
{
2015-07-19 17:19:26 +02:00
$id = XmlParser::getXPathResult($entry, 'atom:id', $this->namespaces)
?: XmlParser::getXPathResult($entry, 'id');
2014-05-20 20:20:27 +02:00
if (!empty($id)) {
$item->setId($this->generateId(XmlParser::getValue($id)));
} else {
$item->setId($this->generateId(
$item->getTitle(), $item->getUrl(), $item->getContent()
));
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)
{
$enclosure = $this->findLink($entry, 'enclosure');
2014-05-20 20:20:27 +02:00
if ($enclosure) {
$item->setEnclosureUrl(Url::resolve((string) $enclosure['href'], $feed->getSiteUrl()));
$item->setEnclosureType((string) $enclosure['type']);
2014-05-20 20:20:27 +02:00
}
}
/**
* Find the item language.
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 findItemLanguage(SimpleXMLElement $entry, Item $item, Feed $feed)
{
2015-07-19 17:19:26 +02:00
$language = XmlParser::getXPathResult($entry, './/@xml:lang');
$item->setLanguage(XmlParser::getValue($language) ?: $feed->getLanguage());
2013-04-05 05:34:07 +02:00
}
2017-06-12 03:08:00 +02:00
/**
* Find the item categories.
*
* @param SimpleXMLElement $entry Feed item
* @param Item $item Item object
* @param Feed $feed Feed object
*/
public function findItemCategories(SimpleXMLElement $entry, Item $item, Feed $feed)
{
$categories = XmlParser::getXPathResult($entry, 'atom:category/@term', $this->namespaces)
?: XmlParser::getXPathResult($entry, 'category/@term');
$item->setCategoriesFromXml($categories);
}
2014-03-17 02:35:57 +01:00
/**
* Get the URL from a link tag.
*
* @param SimpleXMLElement $xml XML tag
* @param string $rel Link relationship: alternate, enclosure, related, self, via
2014-03-17 02:35:57 +01:00
*
* @return string
*/
private function getUrl(SimpleXMLElement $xml, $rel, $fallback = false)
{
$link = $this->findLink($xml, $rel);
if ($link) {
return (string) $link['href'];
}
if ($fallback) {
$link = $this->findLink($xml, '');
return $link ? (string) $link['href'] : '';
}
return '';
}
/**
* Get a link tag that match a relationship.
*
* @param SimpleXMLElement $xml XML tag
* @param string $rel Link relationship: alternate, enclosure, related, self, via
*
* @return SimpleXMLElement|null
*/
private function findLink(SimpleXMLElement $xml, $rel)
2013-04-05 05:34:07 +02:00
{
2015-07-19 17:19:26 +02:00
$links = XmlParser::getXPathResult($xml, 'atom:link', $this->namespaces)
?: XmlParser::getXPathResult($xml, 'link');
foreach ($links as $link) {
if ($rel === (string) $link['rel']) {
return $link;
2013-04-05 05:34:07 +02:00
}
}
return null;
2013-04-05 05:34:07 +02:00
}
2014-05-20 20:20:27 +02:00
/**
* Get the entry content.
*
* @param SimpleXMLElement $entry XML Entry
2014-05-20 20:20:27 +02:00
*
* @return string
*/
private function getContent(SimpleXMLElement $entry)
2014-05-20 20:20:27 +02:00
{
2015-07-19 17:19:26 +02:00
$content = current(
XmlParser::getXPathResult($entry, 'atom:content', $this->namespaces)
?: XmlParser::getXPathResult($entry, 'content')
);
2014-05-20 20:20:27 +02:00
if (!empty($content) && count($content->children())) {
2015-07-19 17:19:26 +02:00
$xml_string = '';
foreach ($content->children() as $child) {
2015-07-19 17:19:26 +02:00
$xml_string .= $child->asXML();
2014-05-20 20:20:27 +02:00
}
2015-07-19 17:19:26 +02:00
return $xml_string;
} elseif (trim((string) $content) !== '') {
2015-07-19 17:19:26 +02:00
return (string) $content;
2014-05-20 20:20:27 +02:00
}
2015-07-19 17:19:26 +02:00
$summary = XmlParser::getXPathResult($entry, 'atom:summary', $this->namespaces)
?: XmlParser::getXPathResult($entry, 'summary');
return (string) current($summary);
2014-05-20 20:20:27 +02:00
}
}