/** * Import articles * @param array $json the array with json * @param string $userId the username * @return Feed if one had to be created for nonexistent feeds */ public function importArticles($json, $userId) { $url = 'http://owncloud/nofeed'; $urlHash = md5($url); // build assoc array for fast access $feeds = $this->findAll($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); $item->setLastModified($this->timeFactory->getTime()); $feedLink = $entry['feedLink']; // this is not set on the item yet if(array_key_exists($feedLink, $feedsDict)) { $feed = $feedsDict[$feedLink]; $item->setFeedId($feed->getId()); } elseif(array_key_exists($url, $feedsDict)) { $feed = $feedsDict[$url]; $item->setFeedId($feed->getId()); } else { $createdFeed = true; $feed = new Feed(); $feed->setUserId($userId); $feed->setLink($url); $feed->setUrl($url); $feed->setTitle($this->l10n->t('Articles without feed')); $feed->setAdded($this->timeFactory->getTime()); $feed->setFolderId(0); $feed->setPreventUpdate(true); $feed = $this->feedMapper->insert($feed); $item->setFeedId($feed->getId()); $feedsDict[$feed->getLink()] = $feed; } try { // if item exists, copy the status $existingItem = $this->itemMapper->findByGuidHash( $item->getGuidHash(), $feed->getId(), $userId); $existingItem->setStatus($item->getStatus()); $this->itemMapper->update($existingItem); } catch(DoesNotExistException $ex){ $item->setBody($this->purifier->purify($item->getBody())); $this->itemMapper->insert($item); } } if($createdFeed) { return $this->feedMapper->findByUrlHash($urlHash, $userId); } return null; }
public function testImportArticlesCreatesOwnFeedWhenNotFound() { $url = 'http://owncloud/args'; $feed = new Feed(); $feed->setId(3); $feed->setUserId($this->user); $feed->setUrl($url); $feed->setLink($url); $feed->setTitle('Articles without feed'); $feed->setAdded($this->time); $feed->setFolderId(0); $feed->setPreventUpdate(true); $feeds = [$feed]; $item = new Item(); $item->setFeedId(3); $item->setAuthor('john'); $item->setGuid('s'); $item->setGuidHash('s'); $item->setTitle('hey'); $item->setPubDate(333); $item->setBody('come over'); $item->setEnclosureMime('mime'); $item->setEnclosureLink('lin'); $item->setUnread(); $item->setUnstarred(); $item->setLastModified($this->time); $json = $item->toExport(['feed3' => $feed]); $json2 = $json; // believe it or not this copies stuff :D $json2['feedLink'] = 'http://test.com'; $items = [$json, $json2]; $insertFeed = new Feed(); $insertFeed->setLink('http://owncloud/nofeed'); $insertFeed->setUrl('http://owncloud/nofeed'); $insertFeed->setUserId($this->user); $insertFeed->setTitle('Articles without feed'); $insertFeed->setAdded($this->time); $insertFeed->setPreventUpdate(true); $insertFeed->setFolderId(0); $this->l10n->expects($this->once())->method('t')->will($this->returnValue('Articles without feed')); $this->feedMapper->expects($this->once())->method('findAllFromUser')->with($this->equalTo($this->user))->will($this->returnValue($feeds)); $this->feedMapper->expects($this->once())->method('insert')->with($this->equalTo($insertFeed))->will($this->returnValue($insertFeed)); $this->itemMapper->expects($this->at(0))->method('findByGuidHash')->will($this->throwException(new DoesNotExistException('yo'))); $this->purifier->expects($this->once())->method('purify')->with($this->equalTo($item->getBody()))->will($this->returnValue($item->getBody())); $this->itemMapper->expects($this->at(1))->method('insert')->with($this->equalTo($item)); $this->itemMapper->expects($this->at(2))->method('findByGuidHash')->will($this->returnValue($item)); $this->itemMapper->expects($this->at(3))->method('update')->with($this->equalTo($item)); $this->feedMapper->expects($this->once())->method('findByUrlHash')->will($this->returnValue($feed)); $result = $this->feedService->importArticles($items, $this->user); $this->assertEquals($feed, $result); }
private function createFeed($feed) { $newFeed = new Feed(); $newFeed->setUserId($this->userId); $newFeed->setFolderId($feed['folderId']); $newFeed->setTitle($feed['title']); $newFeed->setUrl($feed['url']); $newFeed->setLocation($feed['location']); $newFeed->setFaviconLink($feed['faviconLink']); $newFeed->setAdded($feed['added']); $newFeed->setLink($feed['link']); $newFeed->setPreventUpdate($feed['preventUpdate']); $newFeed->setDeletedAt($feed['deletedAt']); $newFeed->setArticlesPerUpdate($feed['articlesPerUpdate']); $newFeed->setLastModified($feed['lastModified']); $newFeed->setEtag($feed['etag']); return $this->feedMapper->insert($newFeed); }