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

98 lines
3.1 KiB
PHP

<?php
namespace PicoFeed\Parsers;
class Atom extends \PicoFeed\Parser
{
public function execute()
{
\PicoFeed\Logging::log(\get_called_class().': begin parsing');
\libxml_use_internal_errors(true);
$xml = \simplexml_load_string($this->content);
if ($xml === false) {
\PicoFeed\Logging::log(\get_called_class().': XML parsing error');
\PicoFeed\Logging::log($this->getXmlErrors());
return false;
}
$this->url = $this->getUrl($xml);
$this->title = $this->stripWhiteSpace((string) $xml->title) ?: $this->url;
$this->id = (string) $xml->id;
$this->updated = $this->parseDate((string) $xml->updated);
$author = (string) $xml->author->name;
\PicoFeed\Logging::log(\get_called_class().': Title => '.$this->title);
\PicoFeed\Logging::log(\get_called_class().': Url => '.$this->url);
foreach ($xml->entry as $entry) {
if (isset($entry->author->name)) {
$author = (string) $entry->author->name;
}
$id = (string) $entry->id;
$item = new \StdClass;
$item->url = $this->getUrl($entry);
$item->id = $this->generateId($id !== $item->url ? $id : $item->url, $this->isExcludedFromId($this->url) ? '' : $this->url);
$item->title = $this->stripWhiteSpace((string) $entry->title);
$item->updated = $this->parseDate((string) $entry->updated);
$item->author = $author;
$item->content = $this->filterHtml($this->getContent($entry), $item->url);
if (empty($item->title)) $item->title = $item->url;
// Try to find an enclosure
foreach ($entry->link as $link) {
if ((string) $link['rel'] === 'enclosure') {
$item->enclosure = (string) $link['href'];
$item->enclosure_type = (string) $link['type'];
if (\PicoFeed\Filter::isRelativePath($item->enclosure)) {
$item->enclosure = \PicoFeed\Filter::getAbsoluteUrl($item->enclosure, $this->url);
}
break;
}
}
$this->items[] = $item;
}
\PicoFeed\Logging::log(\get_called_class().': parsing finished ('.count($this->items).' items)');
return $this;
}
public function getContent($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 '';
}
public function getUrl($xml)
{
foreach ($xml->link as $link) {
if ((string) $link['type'] === 'text/html' || (string) $link['type'] === 'application/xhtml+xml') {
return (string) $link['href'];
}
}
return (string) $xml->link['href'];
}
}