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

87 lines
2.4 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
2013-06-29 03:50:15 +02:00
class Atom extends \PicoFeed\Parser
2013-04-05 05:34:07 +02:00
{
public function execute()
{
2013-08-30 01:34:11 +02:00
\PicoFeed\Logging::log(\get_called_class().': begin parsing');
2013-04-05 05:34:07 +02:00
\libxml_use_internal_errors(true);
$xml = \simplexml_load_string($this->content);
if ($xml === false) {
2013-08-30 01:34:11 +02:00
\PicoFeed\Logging::log(\get_called_class().': XML parsing error');
\PicoFeed\Logging::log($this->getXmlErrors());
2013-04-05 05:34:07 +02:00
return false;
}
$this->url = $this->getUrl($xml);
$this->title = $this->stripWhiteSpace((string) $xml->title);
2013-04-05 05:34:07 +02:00
$this->id = (string) $xml->id;
2013-10-24 00:39:21 +02:00
$this->updated = $this->parseDate((string) $xml->updated);
2013-04-05 05:34:07 +02:00
$author = (string) $xml->author->name;
foreach ($xml->entry as $entry) {
if (isset($entry->author->name)) {
$author = (string) $entry->author->name;
2013-04-05 05:34:07 +02:00
}
$id = (string) $entry->id;
2013-04-05 05:34:07 +02:00
$item = new \StdClass;
$item->url = $this->getUrl($entry);
2013-12-18 02:56:53 +01:00
$item->id = $this->generateId($id !== $item->url ? $id : $item->url, $this->isExcludedFromId($this->url) ? '' : $this->url);
$item->title = $this->stripWhiteSpace((string) $entry->title);
2013-10-24 00:39:21 +02:00
$item->updated = $this->parseDate((string) $entry->updated);
2013-04-05 05:34:07 +02:00
$item->author = $author;
$item->content = $this->filterHtml($this->getContent($entry), $item->url);
2013-05-06 03:44:03 +02:00
if (empty($item->title)) $item->title = $item->url;
2013-04-05 05:34:07 +02:00
$this->items[] = $item;
}
2013-08-30 01:34:11 +02:00
\PicoFeed\Logging::log(\get_called_class().': parsing finished ('.count($this->items).' items)');
2013-04-05 05:34:07 +02:00
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) {
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'];
}
}