2016-08-24 03:38:09 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-25 03:17:58 +02:00
|
|
|
namespace Miniflux\Handler\Opml;
|
2016-08-24 03:38:09 +02:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
use Miniflux\Model;
|
|
|
|
use PicoDb\Database;
|
2016-08-24 03:38:09 +02:00
|
|
|
use PicoFeed\Serialization\Subscription;
|
|
|
|
use PicoFeed\Serialization\SubscriptionList;
|
|
|
|
use PicoFeed\Serialization\SubscriptionListBuilder;
|
2016-12-26 15:44:53 +01:00
|
|
|
use PicoFeed\Serialization\SubscriptionListParser;
|
2016-08-24 03:38:09 +02:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function export_all_feeds($user_id)
|
2016-08-24 03:38:09 +02:00
|
|
|
{
|
2016-12-26 15:44:53 +01:00
|
|
|
$feeds = Model\Feed\get_feeds($user_id);
|
2016-08-24 03:38:09 +02:00
|
|
|
$subscriptionList = SubscriptionList::create()->setTitle(t('Subscriptions'));
|
|
|
|
|
|
|
|
foreach ($feeds as $feed) {
|
2016-12-26 15:44:53 +01:00
|
|
|
$groups = Model\Group\get_feed_groups($feed['id']);
|
2016-08-24 03:38:09 +02:00
|
|
|
$category = '';
|
|
|
|
|
|
|
|
if (!empty($groups)) {
|
|
|
|
$category = $groups[0]['title'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$subscriptionList->addSubscription(Subscription::create()
|
|
|
|
->setTitle($feed['title'])
|
|
|
|
->setSiteUrl($feed['site_url'])
|
|
|
|
->setFeedUrl($feed['feed_url'])
|
|
|
|
->setCategory($category)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SubscriptionListBuilder::create($subscriptionList)->build();
|
|
|
|
}
|
2016-12-26 15:44:53 +01:00
|
|
|
|
|
|
|
function import_opml($user_id, $content)
|
|
|
|
{
|
|
|
|
$subscriptionList = SubscriptionListParser::create($content)->parse();
|
|
|
|
|
|
|
|
$db = Database::getInstance('db');
|
|
|
|
$db->startTransaction();
|
|
|
|
|
|
|
|
foreach ($subscriptionList->subscriptions as $subscription) {
|
|
|
|
if (! $db->table('feeds')->eq('user_id', $user_id)->eq('feed_url', $subscription->getFeedUrl())->exists()) {
|
|
|
|
$db->table('feeds')->insert(array(
|
2017-01-14 17:20:01 +01:00
|
|
|
'user_id' => $user_id,
|
|
|
|
'title' => $subscription->getTitle(),
|
2016-12-26 15:44:53 +01:00
|
|
|
'site_url' => $subscription->getSiteUrl(),
|
|
|
|
'feed_url' => $subscription->getFeedUrl(),
|
|
|
|
));
|
|
|
|
|
|
|
|
if ($subscription->getCategory() !== '') {
|
|
|
|
$feed_id = $db->getLastId();
|
|
|
|
$group_id = Model\Group\get_group_id_from_title($user_id, $subscription->getCategory());
|
|
|
|
|
|
|
|
if (empty($group_id)) {
|
|
|
|
$group_id = Model\Group\create_group($user_id, $subscription->getCategory());
|
|
|
|
}
|
|
|
|
|
|
|
|
Model\Group\associate_feed_groups($feed_id, array($group_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$db->closeTransaction();
|
|
|
|
return true;
|
|
|
|
}
|