news/service/feedservice.php

386 строки
11 KiB
PHP
Исходник Обычный вид История

2013-03-02 22:50:33 +04:00
<?php
2013-03-21 16:17:58 +04:00
/**
* ownCloud - 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 Alessandro Cosentino 2012
* @copyright Bernhard Posselt 2012, 2014
*/
2013-03-02 22:50:33 +04:00
2014-05-15 05:41:49 +04:00
namespace OCA\News\Service;
2014-05-13 22:14:00 +04:00
use \OCP\ILogger;
use \OCP\IL10N;
use \OCP\AppFramework\Db\DoesNotExistException;
2013-03-21 16:17:58 +04:00
use \OCA\News\Db\Feed;
use \OCA\News\Db\Item;
2013-03-21 20:07:57 +04:00
use \OCA\News\Db\FeedMapper;
2013-03-25 15:54:38 +04:00
use \OCA\News\Db\ItemMapper;
use \OCA\News\Fetcher\Fetcher;
use \OCA\News\Fetcher\FetcherException;
use \OCA\News\ArticleEnhancer\Enhancer;
2014-10-21 13:11:20 +04:00
use \OCA\News\Config\Config;
2013-08-28 19:26:38 +04:00
2014-05-15 05:41:49 +04:00
class FeedService extends Service {
2013-03-02 22:50:33 +04:00
private $feedFetcher;
2013-03-25 15:54:38 +04:00
private $itemMapper;
private $feedMapper;
private $logger;
private $l10n;
private $timeFactory;
private $autoPurgeMinimumInterval;
2013-08-28 19:26:38 +04:00
private $enhancer;
private $purifier;
2014-05-13 22:14:00 +04:00
private $loggerParams;
2014-05-23 03:25:55 +04:00
public function __construct(FeedMapper $feedMapper,
Fetcher $feedFetcher,
2014-05-23 03:25:55 +04:00
ItemMapper $itemMapper,
2014-05-13 22:14:00 +04:00
ILogger $logger,
IL10N $l10n,
$timeFactory,
Config $config,
Enhancer $enhancer,
2014-05-13 22:14:00 +04:00
$purifier,
$loggerParams){
2013-03-21 16:17:58 +04:00
parent::__construct($feedMapper);
$this->feedFetcher = $feedFetcher;
2013-03-25 15:54:38 +04:00
$this->itemMapper = $itemMapper;
$this->logger = $logger;
$this->l10n = $l10n;
$this->timeFactory = $timeFactory;
$this->autoPurgeMinimumInterval = $config->getAutoPurgeMinimumInterval();
2013-08-28 19:26:38 +04:00
$this->enhancer = $enhancer;
$this->purifier = $purifier;
$this->feedMapper = $feedMapper;
2014-05-13 22:14:00 +04:00
$this->loggerParams = $loggerParams;
2013-03-08 17:50:10 +04:00
}
2013-06-05 00:45:19 +04:00
/**
* Finds all feeds of a user
* @param string $userId the name of the user
2014-06-26 13:33:44 +04:00
* @return Feed[]
2013-06-05 00:45:19 +04:00
*/
public function findAll($userId){
return $this->feedMapper->findAllFromUser($userId);
2013-03-21 20:00:19 +04:00
}
/**
* Finds all feeds from all users
* @return array of feeds
*/
public function findAllFromAllUsers() {
return $this->feedMapper->findAll();
}
/**
2013-06-05 00:45:19 +04:00
* Creates a new feed
* @param string $feedUrl the url to the feed
* @param int $folderId the folder where it should be put into, 0 for root folder
* @param string $userId for which user the feed should be created
2014-05-23 03:25:55 +04:00
* @param string $title if given, this is used for the opml feed title
2014-05-15 05:41:49 +04:00
* @throws ServiceConflictException if the feed exists already
* @throws ServiceNotFoundException if the url points to an invalid feed
2013-06-05 00:45:19 +04:00
* @return Feed the newly created feed
*/
2014-05-23 03:25:55 +04:00
public function create($feedUrl, $folderId, $userId, $title=null){
// first try if the feed exists already
2013-03-22 15:35:30 +04:00
try {
list($feed, $items) = $this->feedFetcher->fetch($feedUrl);
2013-06-05 00:45:19 +04:00
// try again if feed exists depending on the reported link
try {
$this->feedMapper->findByUrlHash($feed->getUrlHash(), $userId);
2014-05-15 05:41:49 +04:00
throw new ServiceConflictException(
$this->l10n->t('Can not add feed: Exists already'));
2014-05-13 00:48:25 +04:00
2014-06-26 13:33:44 +04:00
// If no matching feed was found everything was ok
} catch(DoesNotExistException $ex){}
2013-03-22 15:35:30 +04:00
// insert feed
$feed->setFolderId($folderId);
2013-03-26 02:23:56 +04:00
$feed->setUserId($userId);
$feed->setArticlesPerUpdate(count($items));
2014-05-23 03:25:55 +04:00
if ($title) {
$feed->setTitle($title);
}
$feed = $this->feedMapper->insert($feed);
2013-03-22 15:35:30 +04:00
// insert items in reverse order because the first one is usually the
// newest item
$unreadCount = 0;
for($i=count($items)-1; $i>=0; $i--){
$item = $items[$i];
2013-03-22 15:35:30 +04:00
$item->setFeedId($feed->getId());
// check if item exists (guidhash is the same)
// and ignore it if it does
try {
$this->itemMapper->findByGuidHash(
$item->getGuidHash(), $item->getFeedId(), $userId);
continue;
} catch(DoesNotExistException $ex){
$unreadCount += 1;
$item = $this->enhancer->enhance($item, $feed->getLink());
$item->setBody($this->purifier->purify($item->getBody()));
$this->itemMapper->insert($item);
}
2013-03-22 15:35:30 +04:00
}
2013-03-26 21:21:00 +04:00
// set unread count
$feed->setUnreadCount($unreadCount);
2013-06-05 00:45:19 +04:00
2013-03-22 15:35:30 +04:00
return $feed;
} catch(FetcherException $ex){
2014-09-29 16:08:27 +04:00
$this->logger->debug($ex->getMessage(), $this->loggerParams);
2014-05-15 05:41:49 +04:00
throw new ServiceNotFoundException(
$this->l10n->t(
2014-05-03 23:19:50 +04:00
'Can not add feed: URL does not exist, SSL Certificate can not be validated ' .
'or feed has invalid xml'));
2013-03-22 15:35:30 +04:00
}
2013-03-21 20:00:19 +04:00
}
2013-03-25 15:54:38 +04:00
2013-06-05 00:45:19 +04:00
/**
* Runs all the feed updates
*/
2013-03-25 14:48:15 +04:00
public function updateAll(){
2013-06-05 00:45:19 +04:00
// TODO: this method is not covered by any tests
$feeds = $this->feedMapper->findAll();
2013-03-25 14:48:15 +04:00
foreach($feeds as $feed){
2013-03-25 15:54:38 +04:00
try {
2013-03-26 02:23:56 +04:00
$this->update($feed->getId(), $feed->getUserId());
} catch(\Exception $ex){
2014-09-29 16:08:27 +04:00
$this->logger->debug('Could not update feed ' . $ex->getMessage(),
2014-05-13 22:14:00 +04:00
$this->loggerParams);
2013-03-25 15:54:38 +04:00
}
2013-03-25 14:48:15 +04:00
}
}
2013-03-21 20:00:19 +04:00
/**
2013-06-05 00:45:19 +04:00
* Updates a single feed
* @param int $feedId the id of the feed that should be updated
* @param string $userId the id of the user
2014-05-15 05:41:49 +04:00
* @throws ServiceNotFoundException if the feed does not exist
2013-06-05 00:45:19 +04:00
* @return Feed the updated feed entity
*/
2013-03-21 23:38:09 +04:00
public function update($feedId, $userId){
2013-03-25 15:54:38 +04:00
try {
$existingFeed = $this->feedMapper->find($feedId, $userId);
if($existingFeed->getPreventUpdate() === true) {
2014-06-25 13:22:08 +04:00
return $existingFeed;
}
try {
2014-05-13 00:57:53 +04:00
list(, $items) = $this->feedFetcher->fetch(
$existingFeed->getUrl(), false);
// update number of articles on every feed update
if($existingFeed->getArticlesPerUpdate() !== count($items)) {
$existingFeed->setArticlesPerUpdate(count($items));
$this->feedMapper->update($existingFeed);
}
2013-06-05 00:45:19 +04:00
// insert items in reverse order because the first one is usually
// the newest item
for($i=count($items)-1; $i>=0; $i--){
$item = $items[$i];
$item->setFeedId($existingFeed->getId());
try {
$this->itemMapper->findByGuidHash($item->getGuidHash(), $feedId, $userId);
} catch(DoesNotExistException $ex){
2014-05-23 03:25:55 +04:00
$item = $this->enhancer->enhance($item,
$existingFeed->getLink());
$item->setBody($this->purifier->purify($item->getBody()));
2013-03-27 16:02:18 +04:00
$this->itemMapper->insert($item);
}
2013-03-25 15:54:38 +04:00
}
} catch(FetcherException $ex){
// failed updating is not really a problem, so only log it
2014-05-13 22:14:00 +04:00
2014-09-29 16:08:27 +04:00
$this->logger->debug('Can not update feed with url ' . $existingFeed->getUrl() .
2014-05-13 22:14:00 +04:00
': Not found or bad source', $this->loggerParams);
2014-09-29 16:08:27 +04:00
$this->logger->debug($ex->getMessage(), $this->loggerParams);
}
2013-06-05 00:45:19 +04:00
return $this->feedMapper->find($feedId, $userId);
2013-06-05 00:45:19 +04:00
} catch (DoesNotExistException $ex){
2014-05-15 05:41:49 +04:00
throw new ServiceNotFoundException('Feed does not exist');
}
2014-06-25 13:22:08 +04:00
2013-03-21 20:00:19 +04:00
}
/**
2013-06-05 00:45:19 +04:00
* Moves a feed into a different folder
* @param int $feedId the id of the feed that should be moved
* @param int $folderId the id of the folder where the feed should be moved to
* @param string $userId the name of the user whose feed should be moved
2014-05-15 05:41:49 +04:00
* @throws ServiceNotFoundException if the feed does not exist
*/
2013-03-21 23:38:09 +04:00
public function move($feedId, $folderId, $userId){
$feed = $this->find($feedId, $userId);
$feed->setFolderId($folderId);
$this->feedMapper->update($feed);
2013-03-21 20:00:19 +04:00
}
2013-11-12 13:46:31 +04:00
/**
* Rename a feed
* @param int $feedId the id of the feed that should be moved
* @param string $feedTitle the new title of the feed
* @param string $userId the name of the user whose feed should be renamed
2014-05-15 05:41:49 +04:00
* @throws ServiceNotFoundException if the feed does not exist
2013-11-12 13:46:31 +04:00
*/
public function rename($feedId, $feedTitle, $userId) {
$feed = $this->find($feedId, $userId);
$feed->setTitle($feedTitle);
$this->feedMapper->update($feed);
2013-11-12 13:46:31 +04:00
}
/**
* Import articles
* @param array $json the array with json
2014-06-26 13:33:44 +04:00
* @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);
2014-05-14 19:32:49 +04:00
$feedsDict = [];
foreach($feeds as $feed) {
$feedsDict[$feed->getLink()] = $feed;
}
2013-09-12 02:59:39 +04:00
$createdFeed = false;
// loop over all items and get the corresponding feed
2014-06-26 13:33:44 +04:00
// 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];
2014-05-23 03:25:55 +04:00
$item->setFeedId($feed->getId());
} else {
2013-09-12 02:59:39 +04:00
$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);
2014-05-23 03:25:55 +04:00
$feed->setPreventUpdate(true);
$feed = $this->feedMapper->insert($feed);
$item->setFeedId($feed->getId());
$feedsDict[$feed->getLink()] = $feed;
}
try {
2013-09-12 02:59:39 +04:00
// 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);
}
}
2013-09-12 02:59:39 +04:00
if($createdFeed) {
return $this->feedMapper->findByUrlHash($urlHash, $userId);
}
2014-06-25 13:22:08 +04:00
return null;
}
/**
2014-06-26 13:33:44 +04:00
* Use this to mark a feed as deleted. That way it can be un-deleted
2013-06-05 00:45:19 +04:00
* @param int $feedId the id of the feed that should be deleted
* @param string $userId the name of the user for security reasons
2014-05-15 05:41:49 +04:00
* @throws ServiceNotFoundException when feed does not exist
*/
public function markDeleted($feedId, $userId) {
$feed = $this->find($feedId, $userId);
$feed->setDeletedAt($this->timeFactory->getTime());
$this->feedMapper->update($feed);
}
/**
* Use this to undo a feed deletion
2013-06-05 00:45:19 +04:00
* @param int $feedId the id of the feed that should be restored
* @param string $userId the name of the user for security reasons
2014-05-15 05:41:49 +04:00
* @throws ServiceNotFoundException when feed does not exist
*/
public function unmarkDeleted($feedId, $userId) {
$feed = $this->find($feedId, $userId);
$feed->setDeletedAt(0);
$this->feedMapper->update($feed);
}
/**
* Deletes all deleted feeds
* @param string $userId if given it purges only feeds of that user
* @param boolean $useInterval defaults to true, if true it only purges
2013-06-05 00:45:19 +04:00
* entries in a given interval to give the user a chance to undo the
* deletion
*/
public function purgeDeleted($userId=null, $useInterval=true) {
$deleteOlderThan = null;
if ($useInterval) {
$now = $this->timeFactory->getTime();
$deleteOlderThan = $now - $this->autoPurgeMinimumInterval;
}
$toDelete = $this->feedMapper->getToDelete($deleteOlderThan, $userId);
foreach ($toDelete as $feed) {
$this->feedMapper->delete($feed);
}
}
/**
* Deletes all feeds of a user, delete items first since the user_id
* is not defined in there
* @param string $userId the name of the user
*/
public function deleteUser($userId) {
$this->feedMapper->deleteUser($userId);
}
2013-03-02 22:50:33 +04:00
}