This commit is contained in:
Marcel Kapfer (mmk2410) 2016-04-18 09:56:52 +02:00
parent cb491341df
commit 9947a5f033
391 changed files with 0 additions and 15712 deletions

View file

@ -1,122 +0,0 @@
<?php
namespace PicoFeed\Serialization;
use SimpleXMLElement;
/**
* OPML export class.
*
* @author Frederic Guillot
*/
class Export
{
/**
* List of feeds to exports.
*
* @var array
*/
private $content = array();
/**
* List of required properties for each feed.
*
* @var array
*/
private $required_fields = array(
'title',
'site_url',
'feed_url',
);
/**
* Constructor.
*
* @param array $content List of feeds
*/
public function __construct(array $content)
{
$this->content = $content;
}
/**
* Get the OPML document.
*
* @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.
*
* @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.
*
* @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.
*
* @param SimpleXMLElement $parent Parent Element
* @param string $category Category
* @param array $feeds Feed properties
*/
public function createCategory(SimpleXMLElement $parent, $category, array $feeds)
{
$outline = $parent->addChild('outline');
$outline->addAttribute('text', $category);
$this->createEntries($outline, $feeds);
}
}

View file

@ -1,162 +0,0 @@
<?php
namespace PicoFeed\Serialization;
use SimpleXmlElement;
use StdClass;
use PicoFeed\Logging\Logger;
use PicoFeed\Parser\XmlParser;
/**
* OPML Import.
*
* @author Frederic Guillot
*/
class Import
{
/**
* OPML file content.
*
* @var string
*/
private $content = '';
/**
* Subscriptions.
*
* @var array
*/
private $items = array();
/**
* Constructor.
*
* @param string $content OPML file content
*/
public function __construct($content)
{
$this->content = $content;
}
/**
* Parse the OPML file.
*
* @return array|false
*/
public function execute()
{
Logger::setMessage(get_called_class().': start importation');
$xml = XmlParser::getSimpleXml(trim($this->content));
if ($xml === false || $xml->getName() !== 'opml' || !isset($xml->body)) {
Logger::setMessage(get_called_class().': OPML tag not found or malformed XML document');
return false;
}
$this->parseEntries($xml->body);
Logger::setMessage(get_called_class().': '.count($this->items).' subscriptions found');
return $this->items;
}
/**
* Parse each entries of the subscription list.
*
* @param SimpleXMLElement $tree XML node
*/
public function parseEntries(SimpleXMLElement $tree)
{
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();
$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.
*
* @param SimpleXmlElement $tree XML tree
*
* @return string
*/
public function findCategory(SimpleXmlElement $tree)
{
return isset($tree['title']) ? (string) $tree['title'] : (string) $tree['text'];
}
/**
* Find title.
*
* @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.
*
* @param SimpleXmlElement $item XML tree
*
* @return string
*/
public function findFeedUrl(SimpleXmlElement $item)
{
return (string) $item['xmlUrl'];
}
/**
* Find site url.
*
* @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.
*
* @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.
*
* @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;
}
}