2013-02-18 03:48:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PicoFeed;
|
|
|
|
|
2014-03-17 02:35:57 +01:00
|
|
|
require_once __DIR__.'/Logging.php';
|
2014-05-20 20:20:27 +02:00
|
|
|
require_once __DIR__.'/XmlParser.php';
|
2014-03-17 02:35:57 +01:00
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
use PicoFeed\Logging;
|
|
|
|
use PicoFeed\XmlParser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OPML Import
|
|
|
|
*
|
|
|
|
* @author Frederic Guillot
|
|
|
|
* @package picofeed
|
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
class Import
|
|
|
|
{
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* OPML file content
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
private $content = '';
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Subscriptions
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $items = array();
|
2013-02-18 03:48:21 +01:00
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $content OPML file content
|
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
public function __construct($content)
|
|
|
|
{
|
|
|
|
$this->content = $content;
|
|
|
|
}
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Parse the OPML file
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array|false
|
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
public function execute()
|
|
|
|
{
|
2014-05-20 20:20:27 +02:00
|
|
|
Logging::setMessage(get_called_class().': start importation');
|
2013-02-18 03:48:21 +01:00
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
$xml = XmlParser::getSimpleXml(trim($this->content));
|
2013-02-18 03:48:21 +01:00
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
if ($xml === false || $xml->getName() !== 'opml' || ! isset($xml->body)) {
|
|
|
|
Logging::setMessage(get_called_class().': OPML tag not found or malformed XML document');
|
2013-02-18 03:48:21 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
$this->parseEntries($xml->body);
|
|
|
|
Logging::setMessage(get_called_class().': '.count($this->items).' subscriptions found');
|
|
|
|
|
2013-02-18 03:48:21 +01:00
|
|
|
return $this->items;
|
|
|
|
}
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Parse each entries of the subscription list
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param SimpleXMLElement $tree XML node
|
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
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;
|
2013-08-16 02:22:56 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-20 20:20:27 +02:00
|
|
|
}
|