summaryrefslogtreecommitdiffstats
path: root/lib/Service/ImportService.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Service/ImportService.php')
-rw-r--r--lib/Service/ImportService.php125
1 files changed, 125 insertions, 0 deletions
diff --git a/lib/Service/ImportService.php b/lib/Service/ImportService.php
new file mode 100644
index 000000000..c3334fad2
--- /dev/null
+++ b/lib/Service/ImportService.php
@@ -0,0 +1,125 @@
+<?php
+/**
+ * Nextcloud - News
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Alessandro Cosentino <cosenal@gmail.com>
+ * @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @copyright 2012 Alessandro Cosentino
+ * @copyright 2012-2014 Bernhard Posselt
+ */
+
+namespace OCA\News\Service;
+
+use \OCA\News\Db\Item;
+use \OCA\News\Db\Feed;
+
+use \Psr\Log\LoggerInterface;
+use \HTMLPurifier;
+
+/**
+ * Class ImportService
+ *
+ * @package OCA\News\Service
+ */
+class ImportService
+{
+ /**
+ * Items service.
+ *
+ * @var ItemServiceV2
+ */
+ protected $itemService;
+ /**
+ * Feeds service.
+ *
+ * @var FeedServiceV2
+ */
+ protected $feedService;
+ /**
+ * @var LoggerInterface
+ */
+ protected $logger;
+ /**
+ * @var HTMLPurifier
+ */
+ protected $purifier;
+
+ /**
+ * FeedService constructor.
+ *
+ * @param FeedServiceV2 $feedService Service for feeds
+ * @param ItemServiceV2 $itemService Service to manage items
+ * @param HTMLPurifier $purifier HTML Purifier
+ * @param LoggerInterface $logger Logger
+ */
+ public function __construct(
+ FeedServiceV2 $feedService,
+ ItemServiceV2 $itemService,
+ HTMLPurifier $purifier,
+ LoggerInterface $logger
+ ) {
+ $this->itemService = $itemService;
+ $this->feedService = $feedService;
+ $this->purifier = $purifier;
+ $this->logger = $logger;
+ }
+
+ /**
+ * @param string $userId
+ * @param array $json
+ *
+ * @return array|null
+ */
+ public function importArticles(string $userId, array $json)
+ {
+ $url = 'http://nextcloud/nofeed';
+
+ // build assoc array for fast access
+ $feeds = $this->feedService->findAllForUser($userId);
+ $feedsDict = [];
+ foreach ($feeds as $feed) {
+ $feedsDict[$feed->getLink()] = $feed;
+ }
+
+ $createdFeed = false;
+
+ // loop over all items and get the corresponding feed
+ // if the feed does not exist, create a separate feed for them
+ foreach ($json as $entry) {
+ $item = Item::fromImport($entry);
+ $feedLink = $entry['feedLink']; // this is not set on the item yet
+
+ if (array_key_exists($feedLink, $feedsDict)) {
+ $feed = $feedsDict[$feedLink];
+ } else {
+ $createdFeed = true;
+ $feed = new Feed();
+ $feed->setUserId($userId)
+ ->setUrlHash(md5($url))
+ ->setLink($url)
+ ->setUrl($url)
+ ->setTitle('Articles without feed')
+ ->setAdded(time())
+ ->setFolderId(null)
+ ->setPreventUpdate(true);
+
+ $feed = $this->feedService->insert($feed);
+ $feedsDict[$feed->getLink()] = $feed;
+ }
+
+ $item->setFeedId($feed->getId())
+ ->setBody($this->purifier->purify($item->getBody()))
+ ->generateSearchIndex();
+ $this->itemService->insertOrUpdate($item);
+ }
+
+ if (!$createdFeed) {
+ return null;
+ }
+
+ return $this->feedService->findByURL($userId, $url);
+ }
+}