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

124 lines
3.7 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-03-17 02:35:57 +01:00
/**
* Atom parser
*
* @author Frederic Guillot
* @package parser
*/
2013-06-29 03:50:15 +02:00
class Atom extends \PicoFeed\Parser
2013-04-05 05:34:07 +02:00
{
2014-03-17 02:35:57 +01:00
/**
* Parse the document
*
* @access public
* @return mixed Atom instance or false
*/
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;
}
2014-03-17 02:35:57 +01:00
$this->language = $this->getXmlLang($this->content);
2013-04-05 05:34:07 +02:00
$this->url = $this->getUrl($xml);
$this->title = $this->stripWhiteSpace((string) $xml->title) ?: $this->url;
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;
\PicoFeed\Logging::log(\get_called_class().': Title => '.$this->title);
\PicoFeed\Logging::log(\get_called_class().': Url => '.$this->url);
2013-04-05 05:34:07 +02:00
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);
2014-03-17 02:35:57 +01:00
$item->language = $this->language;
2013-04-05 05:34:07 +02:00
2013-05-06 03:44:03 +02:00
if (empty($item->title)) $item->title = $item->url;
2014-02-18 04:04:49 +01:00
// 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;
}
}
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;
}
2014-03-17 02:35:57 +01:00
/**
* Get the entry content
*
* @access public
* @param SimpleXMLElement $entry XML Entry
* @return string
*/
2013-04-05 05:34:07 +02:00
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 '';
}
2014-03-17 02:35:57 +01:00
/**
* Get the URL from a link tag
*
* @access public
* @param SimpleXMLElement $xml XML tag
* @return string
*/
2013-04-05 05:34:07 +02:00
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'];
}
}