miniflux-legacy/vendor/fguillot/picofeed/lib/PicoFeed/Serialization/Import.php

172 lines
4.2 KiB
PHP
Raw Normal View History

2013-02-18 03:48:21 +01:00
<?php
namespace PicoFeed\Serialization;
2013-02-18 03:48:21 +01:00
2014-10-19 20:42:31 +02:00
use SimpleXmlElement;
use StdClass;
use PicoFeed\Logging\Logger;
use PicoFeed\Parser\XmlParser;
2014-05-20 20:20:27 +02:00
/**
* OPML Import
*
* @author Frederic Guillot
* @package Serialization
2014-05-20 20:20:27 +02:00
*/
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()
{
Logger::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)) {
Logger::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);
Logger::setMessage(get_called_class().': '.count($this->items).' subscriptions found');
2014-05-20 20:20:27 +02:00
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
}