miniflux-legacy/vendor/PicoFeed/Import.php

170 lines
4.1 KiB
PHP
Raw Normal View History

2013-02-18 03:48:21 +01:00
<?php
namespace PicoFeed;
2014-10-19 20:42:31 +02:00
use SimpleXmlElement;
use StdClass;
2014-05-20 20:20:27 +02:00
/**
* 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
2014-10-19 20:42:31 +02:00
$entry = new StdClass;
$entry->category = $this->findCategory($tree);
$entry->title = $this->findTitle($item);
$entry->feed_url = $this->findFeedUrl($item);
$entry->site_url = $this->findSiteUrl($item, $entry);
$entry->type = $this->findType($item);
$entry->description = $this->findDescription($item, $entry);
2013-03-20 05:19:12 +01:00
$this->items[] = $entry;
}
2013-02-18 03:48:21 +01:00
}
}
}
2014-10-19 20:42:31 +02:00
/**
* Find category
*
* @access public
* @param SimpleXmlElement $tree XML tree
* @return string
*/
public function findCategory(SimpleXmlElement $tree)
{
return isset($tree['title']) ? (string) $tree['title'] : (string) $tree['text'];
}
/**
* Find title
*
* @access public
* @param SimpleXmlElement $item XML tree
* @return string
*/
public function findTitle(SimpleXmlElement $item)
{
return isset($item['title']) ? (string) $item['title'] : (string) $item['text'];
}
/**
* Find feed url
*
* @access public
* @param SimpleXmlElement $item XML tree
* @return string
*/
public function findFeedUrl(SimpleXmlElement $item)
{
return (string) $item['xmlUrl'];
}
/**
* Find site url
*
* @access public
* @param SimpleXmlElement $item XML tree
* @param StdClass $entry Feed entry
* @return string
*/
public function findSiteUrl(SimpleXmlElement $item, StdClass $entry)
{
return isset($item['htmlUrl']) ? (string) $item['htmlUrl'] : $entry->feed_url;
}
/**
* Find type
*
* @access public
* @param SimpleXmlElement $item XML tree
* @return string
*/
public function findType(SimpleXmlElement $item)
{
return isset($item['version']) ? (string) $item['version'] : isset($item['type']) ? (string) $item['type'] : 'rss';
}
/**
* Find description
*
* @access public
* @param SimpleXmlElement $item XML tree
* @param StdClass $entry Feed entry
* @return string
*/
public function findDescription(SimpleXmlElement $item, StdClass $entry)
{
return isset($item['description']) ? (string) $item['description'] : $entry->title;
}
2014-05-20 20:20:27 +02:00
}