miniflux-legacy/vendor/PicoFeed/Parsers/Rss20.php
2013-07-06 14:29:45 -04:00

112 lines
3.5 KiB
PHP

<?php
namespace PicoFeed\Parsers;
class Rss20 extends \PicoFeed\Parser
{
public function execute()
{
\libxml_use_internal_errors(true);
$xml = \simplexml_load_string($this->content);
if ($xml === false) {
\PicoFeed\Logging::log($this->getXmlErrors());
return false;
}
$namespaces = $xml->getNamespaces(true);
if ($xml->channel->link && $xml->channel->link->count() > 1) {
foreach ($xml->channel->link as $xml_link) {
$link = (string) $xml_link;
if ($link !== '') {
$this->url = (string) $link;
break;
}
}
}
else {
$this->url = (string) $xml->channel->link;
}
$this->title = $this->stripWhiteSpace((string) $xml->channel->title);
$this->id = $this->url;
$this->updated = isset($xml->channel->pubDate) ? (string) $xml->channel->pubDate : (string) $xml->channel->lastBuildDate;
$this->updated = $this->updated ? strtotime($this->updated) : time();
// RSS feed might be empty
if (! $xml->channel->item) return $this;
foreach ($xml->channel->item as $entry) {
$item = new \StdClass;
$item->title = $this->stripWhiteSpace((string) $entry->title);
$item->url = '';
$item->author= '';
$item->updated = '';
$item->content = '';
foreach ($namespaces as $name => $url) {
$namespace = $entry->children($namespaces[$name]);
if (! $item->url && ! empty($namespace->origLink)) $item->url = (string) $namespace->origLink;
if (! $item->author && ! empty($namespace->creator)) $item->author = (string) $namespace->creator;
if (! $item->updated && ! empty($namespace->date)) $item->updated = strtotime((string) $namespace->date);
if (! $item->updated && ! empty($namespace->updated)) $item->updated = strtotime((string) $namespace->updated);
if (! $item->content && ! empty($namespace->encoded)) $item->content = (string) $namespace->encoded;
}
if (empty($item->url)) {
if (isset($entry->link)) {
$item->url = (string) $entry->link;
}
else if (isset($entry->guid)) {
$item->url = (string) $entry->guid;
}
}
if (empty($item->updated)) $item->updated = strtotime((string) $entry->pubDate) ?: $this->updated;
if (empty($item->content)) {
$item->content = isset($entry->description) ? (string) $entry->description : '';
}
if (empty($item->author)) {
if (isset($entry->author)) {
$item->author = (string) $entry->author;
}
else if (isset($xml->channel->webMaster)) {
$item->author = (string) $xml->channel->webMaster;
}
}
if (isset($entry->guid) && isset($entry->guid['isPermaLink']) && (string) $entry->guid['isPermaLink'] != 'false') {
$item->id = (string) $entry->guid;
}
else {
$item->id = $item->url;
}
if (empty($item->title)) $item->title = $item->url;
$item->content = $this->filterHtml($item->content, $item->url);
$this->items[] = $item;
}
return $this;
}
}