diff options
Diffstat (limited to '3rdparty/fguillot/picofeed/lib/PicoFeed/Serialization')
-rw-r--r-- | 3rdparty/fguillot/picofeed/lib/PicoFeed/Serialization/Export.php | 132 | ||||
-rw-r--r-- | 3rdparty/fguillot/picofeed/lib/PicoFeed/Serialization/Import.php | 172 |
2 files changed, 304 insertions, 0 deletions
diff --git a/3rdparty/fguillot/picofeed/lib/PicoFeed/Serialization/Export.php b/3rdparty/fguillot/picofeed/lib/PicoFeed/Serialization/Export.php new file mode 100644 index 000000000..4a3b978fa --- /dev/null +++ b/3rdparty/fguillot/picofeed/lib/PicoFeed/Serialization/Export.php @@ -0,0 +1,132 @@ +<?php + +namespace PicoFeed\Serialization; + +use SimpleXMLElement; + +/** + * OPML export class + * + * @author Frederic Guillot + * @package Serialization + */ +class Export +{ + /** + * List of feeds to exports + * + * @access private + * @var array + */ + private $content = array(); + + /** + * List of required properties for each feed + * + * @access private + * @var array + */ + private $required_fields = array( + 'title', + 'site_url', + 'feed_url', + ); + + /** + * Constructor + * + * @access public + * @param array $content List of feeds + */ + public function __construct(array $content) + { + $this->content = $content; + } + + /** + * Get the OPML document + * + * @access public + * @return string + */ + public function execute() + { + $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><opml/>'); + + $head = $xml->addChild('head'); + $head->addChild('title', 'OPML Export'); + + $body = $xml->addChild('body'); + + foreach ($this->content as $category => $values) { + + if (is_string($category)) { + $this->createCategory($body, $category, $values); + } + else { + $this->createEntry($body, $values); + } + } + + return $xml->asXML(); + } + + /** + * Create a feed entry + * + * @access public + * @param SimpleXMLElement $parent Parent Element + * @param array $feed Feed properties + */ + public function createEntry(SimpleXMLElement $parent, array $feed) + { + $valid = true; + + foreach ($this->required_fields as $field) { + if (! isset($feed[$field])) { + $valid = false; + break; + } + } + + if ($valid) { + $outline = $parent->addChild('outline'); + $outline->addAttribute('xmlUrl', $feed['feed_url']); + $outline->addAttribute('htmlUrl', $feed['site_url']); + $outline->addAttribute('title', $feed['title']); + $outline->addAttribute('text', $feed['title']); + $outline->addAttribute('description', isset($feed['description']) ? $feed['description'] : $feed['title']); + $outline->addAttribute('type', 'rss'); + $outline->addAttribute('version', 'RSS'); + } + } + + /** + * Create entries for a feed list + * + * @access public + * @param SimpleXMLElement $parent Parent Element + * @param array $feeds Feed list + */ + public function createEntries(SimpleXMLElement $parent, array $feeds) + { + foreach ($feeds as $feed) { + $this->createEntry($parent, $feed); + } + } + + /** + * Create a category entry + * + * @access public + * @param SimpleXMLElement $parent Parent Element + * @param string $category Category + * @param array $feed Feed properties + */ + public function createCategory(SimpleXMLElement $parent, $category, array $feeds) + { + $outline = $parent->addChild('outline'); + $outline->addAttribute('text', $category); + $this->createEntries($outline, $feeds); + } +} diff --git a/3rdparty/fguillot/picofeed/lib/PicoFeed/Serialization/Import.php b/3rdparty/fguillot/picofeed/lib/PicoFeed/Serialization/Import.php new file mode 100644 index 000000000..8de2d8511 --- /dev/null +++ b/3rdparty/fguillot/picofeed/lib/PicoFeed/Serialization/Import.php @@ -0,0 +1,172 @@ +<?php + +namespace PicoFeed\Serialization; + +use SimpleXmlElement; +use StdClass; + +use PicoFeed\Logging\Logging; +use PicoFeed\Parser\XmlParser; + +/** + * OPML Import + * + * @author Frederic Guillot + * @package Serialization + */ +class Import +{ + /** + * OPML file content + * + * @access private + * @var string + */ + private $content = ''; + + /** + * Subscriptions + * + * @access private + * @var array + */ + private $items = array(); + + /** + * Constructor + * + * @access public + * @param string $content OPML file content + */ + public function __construct($content) + { + $this->content = $content; + } + + /** + * Parse the OPML file + * + * @access public + * @return array|false + */ + public function execute() + { + Logging::setMessage(get_called_class().': start importation'); + + $xml = XmlParser::getSimpleXml(trim($this->content)); + + if ($xml === false || $xml->getName() !== 'opml' || ! isset($xml->body)) { + Logging::setMessage(get_called_class().': OPML tag not found or malformed XML document'); + return false; + } + + $this->parseEntries($xml->body); + Logging::setMessage(get_called_class().': '.count($this->items).' subscriptions found'); + + return $this->items; + } + + /** + * Parse each entries of the subscription list + * + * @access public + * @param SimpleXMLElement $tree XML node + */ + public function parseEntries($tree) + { + if (isset($tree->outline)) { + + foreach ($tree->outline as $item) { + + if (isset($item->outline)) { + $this->parseEntries($item); + } + else if ((isset($item['text']) || isset($item['title'])) && isset($item['xmlUrl'])) { + + $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); + $this->items[] = $entry; + } + } + } + } + + /** + * 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; + } +} |