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

325 lines
8.7 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
*
* @author Frederic Guillot
* @package Parser
2014-03-17 02:35:57 +01:00
*/
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\Parser\Feed $feed Feed object
2014-05-20 20:20:27 +02:00
*/
public function findFeedUrl(SimpleXMLElement $xml, Feed $feed)
{
$feed->feed_url = $this->getUrl($xml, 'self');
}
/**
* 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 = $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
*
* @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->subtitle;
}
/**
* 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)
{
$feed->logo = (string) $xml->logo;
}
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->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)
{
2014-10-19 20:42:31 +02:00
$feed->language = XmlParser::getXmlLang($this->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 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 = (string) $xml->id;
}
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)
{
$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
2014-05-20 20:20:27 +02:00
*/
public function findItemDate(SimpleXMLElement $entry, Item $item)
{
$published = isset($entry->published) ? $this->parseDate((string) $entry->published) : 0;
$updated = isset($entry->updated) ? $this->parseDate((string) $entry->updated) : 0;
$item->date = max($published, $updated) ?: time();
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
*
* @access public
* @param SimpleXMLElement $entry Feed item
2014-10-19 20:42:31 +02:00
* @param 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);
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\Parser\Item $item Item object
2014-05-20 20:20:27 +02:00
*/
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\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
$item->content = $this->getContent($entry);
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\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->url = $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
*
* @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->id;
if ($id) {
$item->id = $this->generateId($id);
2014-05-20 20:20:27 +02:00
}
else {
$item->id = $this->generateId(
$item->getTitle(), $item->getUrl(), $item->getContent()
);
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)
{
$enclosure = $this->findLink($entry, 'enclosure');
2014-05-20 20:20:27 +02:00
if ($enclosure) {
$item->enclosure_url = Url::resolve((string) $enclosure['href'], $feed->getSiteUrl());
$item->enclosure_type = (string) $enclosure['type'];
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)
{
$language = (string) $entry->attributes('xml', true)->{'lang'};
if ($language === '') {
$language = $feed->language;
}
$item->language = $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 private
* @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
*
* @access private
* @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
{
foreach ($xml->link 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
*
* @access private
2014-05-20 20:20:27 +02:00
* @param SimpleXMLElement $entry XML Entry
* @return string
*/
private function getContent(SimpleXMLElement $entry)
2014-05-20 20:20:27 +02:00
{
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 '';
}
}