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

163 lines
3.9 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.
2014-05-20 20:20:27 +02:00
*
* @author Frederic Guillot
*/
2013-02-18 03:48:21 +01:00
class Import
{
2014-05-20 20:20:27 +02:00
/**
* OPML file content.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
2013-02-18 03:48:21 +01:00
private $content = '';
2014-05-20 20:20:27 +02:00
/**
* Subscriptions.
2014-05-20 20:20:27 +02:00
*
* @var array
*/
private $items = array();
2013-02-18 03:48:21 +01:00
2014-05-20 20:20:27 +02:00
/**
* Constructor.
2014-05-20 20:20:27 +02:00
*
* @param string $content OPML file content
2014-05-20 20:20:27 +02:00
*/
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.
2014-05-20 20:20:27 +02:00
*
* @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
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.
2014-05-20 20:20:27 +02:00
*
* @param SimpleXMLElement $tree XML node
2014-05-20 20:20:27 +02:00
*/
public function parseEntries(SimpleXMLElement $tree)
2013-02-18 03:48:21 +01:00
{
2013-03-20 05:19:12 +01:00
if (isset($tree->outline)) {
foreach ($tree->outline as $item) {
if (isset($item->outline)) {
$this->parseEntries($item);
} elseif ((isset($item['text']) || isset($item['title'])) && isset($item['xmlUrl'])) {
$entry = new StdClass();
2014-10-19 20:42:31 +02:00
$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.
*
* @param SimpleXmlElement $tree XML tree
2014-10-19 20:42:31 +02:00
*
* @return string
*/
public function findCategory(SimpleXmlElement $tree)
{
return isset($tree['title']) ? (string) $tree['title'] : (string) $tree['text'];
}
/**
* Find title.
*
* @param SimpleXmlElement $item XML tree
2014-10-19 20:42:31 +02:00
*
* @return string
*/
public function findTitle(SimpleXmlElement $item)
{
return isset($item['title']) ? (string) $item['title'] : (string) $item['text'];
}
/**
* Find feed url.
*
* @param SimpleXmlElement $item XML tree
2014-10-19 20:42:31 +02:00
*
* @return string
*/
public function findFeedUrl(SimpleXmlElement $item)
{
return (string) $item['xmlUrl'];
}
/**
* Find site url.
*
* @param SimpleXmlElement $item XML tree
* @param StdClass $entry Feed entry
2014-10-19 20:42:31 +02:00
*
* @return string
*/
public function findSiteUrl(SimpleXmlElement $item, StdClass $entry)
{
return isset($item['htmlUrl']) ? (string) $item['htmlUrl'] : $entry->feed_url;
}
/**
* Find type.
*
* @param SimpleXmlElement $item XML tree
2014-10-19 20:42:31 +02:00
*
* @return string
*/
public function findType(SimpleXmlElement $item)
{
return isset($item['version']) ? (string) $item['version'] : isset($item['type']) ? (string) $item['type'] : 'rss';
}
/**
* Find description.
*
* @param SimpleXmlElement $item XML tree
* @param StdClass $entry Feed entry
2014-10-19 20:42:31 +02:00
*
* @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
}