miniflux-legacy/vendor/PicoFeed/Import.php

71 lines
2.1 KiB
PHP
Raw Normal View History

2013-02-18 03:48:21 +01:00
<?php
namespace PicoFeed;
2014-03-17 02:35:57 +01:00
require_once __DIR__.'/Logging.php';
2013-02-18 03:48:21 +01:00
class Import
{
private $content = '';
private $items = array();
public function __construct($content)
{
$this->content = $content;
}
public function execute()
{
2013-08-30 01:34:11 +02:00
\PicoFeed\Logging::log(\get_called_class().': start importation');
2013-02-18 03:48:21 +01:00
try {
\libxml_use_internal_errors(true);
$xml = new \SimpleXMLElement(trim($this->content));
2013-03-20 05:19:12 +01:00
if ($xml->getName() !== 'opml' || ! isset($xml->body)) {
2013-08-30 01:34:11 +02:00
\PicoFeed\Logging::log(\get_called_class().': OPML tag not found');
2013-02-18 03:48:21 +01:00
return false;
}
$this->parseEntries($xml->body);
2013-08-30 01:34:11 +02:00
\PicoFeed\Logging::log(\get_called_class().': '.count($this->items).' subscriptions found');
2013-02-18 03:48:21 +01:00
}
catch (\Exception $e) {
2013-08-30 01:34:11 +02:00
\PicoFeed\Logging::log(\get_called_class().': '.$e->getMessage());
2013-02-18 03:48:21 +01:00
return false;
}
return $this->items;
}
public function parseEntries($tree)
{
2013-03-20 05:19:12 +01:00
if (isset($tree->outline)) {
2013-02-18 03:48:21 +01:00
2013-03-20 05:19:12 +01:00
foreach ($tree->outline as $item) {
2013-02-18 03:48:21 +01:00
2013-03-20 05:19:12 +01:00
if (isset($item->outline)) {
$this->parseEntries($item);
}
2013-03-21 03:30:44 +01:00
else if ((isset($item['text']) || isset($item['title'])) && isset($item['xmlUrl'])) {
2013-03-20 05:19:12 +01:00
$entry = new \StdClass;
$entry->category = isset($tree['title']) ? (string) $tree['title'] : (string) $tree['text'];
2013-03-20 05:19:12 +01:00
$entry->title = isset($item['title']) ? (string) $item['title'] : (string) $item['text'];
$entry->feed_url = (string) $item['xmlUrl'];
2013-03-21 03:30:44 +01:00
$entry->site_url = isset($item['htmlUrl']) ? (string) $item['htmlUrl'] : $entry->feed_url;
2013-03-20 05:19:12 +01:00
$entry->type = isset($item['version']) ? (string) $item['version'] : isset($item['type']) ? (string) $item['type'] : 'rss';
$entry->description = isset($item['description']) ? (string) $item['description'] : $entry->title;
$this->items[] = $entry;
}
2013-02-18 03:48:21 +01:00
}
}
}
}