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

140 lines
5.1 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 Rss20 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;
}
2013-04-07 16:58:46 +02:00
$namespaces = $xml->getNamespaces(true);
2013-04-05 05:34:07 +02:00
2013-06-27 01:30:46 +02:00
if ($xml->channel->link && $xml->channel->link->count() > 1) {
2013-04-12 21:57:54 +02:00
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->url;
2013-04-05 05:34:07 +02:00
$this->id = $this->url;
2013-10-24 00:39:21 +02:00
$this->updated = $this->parseDate(isset($xml->channel->pubDate) ? (string) $xml->channel->pubDate : (string) $xml->channel->lastBuildDate);
2013-04-05 05:34:07 +02:00
\PicoFeed\Logging::log(\get_called_class().': Title => '.$this->title);
\PicoFeed\Logging::log(\get_called_class().': Url => '.$this->url);
2013-06-27 01:30:46 +02:00
// RSS feed might be empty
2013-08-30 01:34:11 +02:00
if (! $xml->channel->item) {
\PicoFeed\Logging::log(\get_called_class().': feed empty or malformed');
return $this;
}
2013-06-27 01:30:46 +02:00
2013-04-05 05:34:07 +02:00
foreach ($xml->channel->item as $entry) {
2013-04-07 16:58:46 +02:00
$item = new \StdClass;
$item->title = $this->stripWhiteSpace((string) $entry->title);
2013-04-07 16:58:46 +02:00
$item->url = '';
$item->author= '';
$item->updated = '';
$item->content = '';
2014-02-18 04:04:49 +01:00
$item->enclosure = '';
$item->enclosure_type = '';
2013-04-05 05:34:07 +02:00
2013-04-07 16:58:46 +02:00
foreach ($namespaces as $name => $url) {
2013-04-05 05:34:07 +02:00
2013-04-07 16:58:46 +02:00
$namespace = $entry->children($namespaces[$name]);
2013-04-05 05:34:07 +02:00
2013-04-07 16:58:46 +02:00
if (! $item->author && ! empty($namespace->creator)) $item->author = (string) $namespace->creator;
2013-10-24 00:39:21 +02:00
if (! $item->updated && ! empty($namespace->date)) $item->updated = $this->parseDate((string) $namespace->date);
if (! $item->updated && ! empty($namespace->updated)) $item->updated = $this->parseDate((string) $namespace->updated);
2013-04-07 16:58:46 +02:00
if (! $item->content && ! empty($namespace->encoded)) $item->content = (string) $namespace->encoded;
2014-02-18 04:04:49 +01:00
// Get FeedBurner original links
if (! $item->url && ! empty($namespace->origLink)) $item->url = (string) $namespace->origLink;
if (! $item->enclosure && ! empty($namespace->origEnclosureLink)) $item->enclosure = (string) $namespace->origEnclosureLink;
2013-04-05 05:34:07 +02:00
}
if (empty($item->url)) {
if (isset($entry->link)) {
$item->url = (string) $entry->link;
}
else if (isset($entry->guid)) {
$item->url = (string) $entry->guid;
}
}
2013-10-24 00:39:21 +02:00
if (empty($item->updated)) $item->updated = $this->parseDate((string) $entry->pubDate) ?: $this->updated;
2013-04-05 05:34:07 +02:00
2013-04-07 16:58:46 +02:00
if (empty($item->content)) {
$item->content = isset($entry->description) ? (string) $entry->description : '';
2013-04-05 05:34:07 +02:00
}
2013-04-07 16:58:46 +02:00
if (empty($item->author)) {
2013-04-05 05:34:07 +02:00
if (isset($entry->author)) {
2013-04-07 16:58:46 +02:00
$item->author = (string) $entry->author;
2013-04-05 05:34:07 +02:00
}
else if (isset($xml->channel->webMaster)) {
2013-04-07 16:58:46 +02:00
$item->author = (string) $xml->channel->webMaster;
2013-04-05 05:34:07 +02:00
}
}
2013-04-07 16:58:46 +02:00
if (isset($entry->guid) && isset($entry->guid['isPermaLink']) && (string) $entry->guid['isPermaLink'] != 'false') {
$id = (string) $entry->guid;
2013-12-18 02:56:53 +01:00
$item->id = $this->generateId($id !== '' && $id !== $item->url ? $id : $item->url, $this->isExcludedFromId($this->url) ? '' : $this->url);
2013-04-07 16:58:46 +02:00
}
else {
2013-12-18 02:56:53 +01:00
$item->id = $this->generateId($item->url, $this->isExcludedFromId($this->url) ? '' : $this->url);
2013-04-07 16:58:46 +02:00
}
2013-04-05 05:34:07 +02:00
2013-05-06 03:44:03 +02:00
if (empty($item->title)) $item->title = $item->url;
// if optional enclosure tag with multimedia provided, capture here
if (isset($entry->enclosure)) {
2014-02-18 04:04:49 +01:00
if (! $item->enclosure) {
$item->enclosure = isset($entry->enclosure['url']) ? (string) $entry->enclosure['url'] : '';
}
$item->enclosure_type = isset($entry->enclosure['type']) ? (string) $entry->enclosure['type'] : '';
if (\PicoFeed\Filter::isRelativePath($item->enclosure)) {
$item->enclosure = \PicoFeed\Filter::getAbsoluteUrl($item->enclosure, $this->url);
}
}
2013-04-07 16:58:46 +02:00
$item->content = $this->filterHtml($item->content, $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;
}
}