news/service/feedservice.php

455 строки
16 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
2014-12-17 21:20:21 +03:00
use HTMLPurifier;
use OCP\ILogger;
use OCP\IL10N;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
2014-05-13 22:14:00 +04:00
use OCA\News\Db\Feed;
use OCA\News\Db\Item;
use OCA\News\Db\FeedMapper;
use OCA\News\Db\ItemMapper;
use OCA\News\Fetcher\Fetcher;
use OCA\News\Fetcher\FetcherException;
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;
private $itemMapper;
private $feedMapper;
private $logger;
private $l10n;
private $timeFactory;
private $autoPurgeMinimumInterval;
private $purifier;
private $loggerParams;
public function __construct(FeedMapper $feedMapper,
Fetcher $feedFetcher,
ItemMapper $itemMapper,
ILogger $logger,
IL10N $l10n,
2014-12-17 21:20:21 +03:00
ITimeFactory $timeFactory,
Config $config,
2014-12-17 21:20:21 +03:00
HTMLPurifier $purifier,
$LoggerParameters){
parent::__construct($feedMapper);
$this->feedFetcher = $feedFetcher;
$this->itemMapper = $itemMapper;
$this->logger = $logger;
$this->l10n = $l10n;
$this->timeFactory = $timeFactory;
$this->autoPurgeMinimumInterval =
$config->getAutoPurgeMinimumInterval();
$this->purifier = $purifier;
$this->feedMapper = $feedMapper;
2014-12-17 21:20:21 +03:00
$this->loggerParams = $LoggerParameters;
}
/**
* Finds all feeds of a user
* @param string $userId the name of the user
* @return Feed[]
*/
public function findAll($userId){
return $this->feedMapper->findAllFromUser($userId);
}
/**
* Finds all feeds from all users
* @return array of feeds
*/
public function findAllFromAllUsers() {
return $this->feedMapper->findAll();
}
/**
* 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
* @param string $title if given, this is used for the opml feed title
2016-03-25 17:31:17 +03:00
* @param string $basicAuthUser if given, basic auth is set for this feed
* @param string $basicAuthPassword if given, basic auth is set for this
* feed. Ignored if user is null or an empty string
* @throws ServiceConflictException if the feed exists already
* @throws ServiceNotFoundException if the url points to an invalid feed
* @return Feed the newly created feed
*/
2016-03-25 17:31:17 +03:00
public function create($feedUrl, $folderId, $userId, $title=null,
$basicAuthUser=null, $basicAuthPassword=null){
// first try if the feed exists already
try {
2016-03-25 17:31:17 +03:00
list($feed, $items) = $this->feedFetcher->fetch($feedUrl, true,
null, null, false, $basicAuthUser,
$basicAuthPassword);
// try again if feed exists depending on the reported link
try {
$this->feedMapper->findByUrlHash($feed->getUrlHash(), $userId);
throw new ServiceConflictException(
$this->l10n->t('Can not add feed: Exists already'));
// If no matching feed was found everything was ok
} catch(DoesNotExistException $ex){}
// insert feed
$itemCount = count($items);
2016-03-25 17:31:17 +03:00
$feed->setBasicAuthUser($basicAuthUser);
$feed->setBasicAuthPassword($basicAuthPassword);
$feed->setFolderId($folderId);
$feed->setUserId($userId);
$feed->setArticlesPerUpdate($itemCount);
2015-03-22 18:06:19 +03:00
if ($title !== null && $title !== '') {
$feed->setTitle($title);
}
$feed = $this->feedMapper->insert($feed);
// insert items in reverse order because the first one is usually
// the newest item
$unreadCount = 0;
for($i=$itemCount-1; $i>=0; $i--){
$item = $items[$i];
$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->setBody($this->purifier->purify($item->getBody()));
$this->itemMapper->insert($item);
}
}
// set unread count
$feed->setUnreadCount($unreadCount);
return $feed;
} catch(FetcherException $ex){
$this->logger->debug($ex->getMessage(), $this->loggerParams);
throw new ServiceNotFoundException($ex->getMessage());
}
}
/**
* Runs all the feed updates
*/
public function updateAll(){
// TODO: this method is not covered by any tests
$feeds = $this->feedMapper->findAll();
foreach($feeds as $feed){
try {
$this->update($feed->getId(), $feed->getUserId());
} catch(\Exception $ex){
// something is really wrong here, log it
$this->logger->error(
'Unexpected error when updating feed ' . $ex->getMessage(),
$this->loggerParams
);
}
}
}
/**
* Updates a single feed
* @param int $feedId the id of the feed that should be updated
* @param string $userId the id of the user
* @param bool $forceUpdate update even if the article exists already
* @throws ServiceNotFoundException if the feed does not exist
* @return Feed the updated feed entity
*/
public function update($feedId, $userId, $forceUpdate=false){
2015-01-23 16:32:53 +03:00
$existingFeed = $this->find($feedId, $userId);
2015-01-23 16:32:53 +03:00
if($existingFeed->getPreventUpdate() === true) {
return $existingFeed;
}
2015-01-23 16:32:53 +03:00
// for backwards compability it can be that the location is not set
// yet, if so use the url
$location = $existingFeed->getLocation();
if (!$location) {
$location = $existingFeed->getUrl();
}
try {
list($fetchedFeed, $items) = $this->feedFetcher->fetch(
$location,
false,
$existingFeed->getLastModified(),
$existingFeed->getEtag(),
2016-03-25 17:31:17 +03:00
$existingFeed->getFullTextEnabled(),
$existingFeed->getBasicAuthUser(),
$existingFeed->getBasicAuthPassword()
2015-01-23 16:32:53 +03:00
);
// if there is no feed it means that no update took place
if (!$fetchedFeed) {
return $existingFeed;
2014-10-30 14:41:54 +03:00
}
2015-01-23 16:32:53 +03:00
// update number of articles on every feed update
$itemCount = count($items);
2014-10-22 13:35:12 +04:00
2015-01-23 16:32:53 +03:00
// this is needed to adjust to updates that add more items
// than when the feed was created. You can't update the count
// if it's lower because it may be due to the caching headers
// that were sent as the request and it might cause unwanted
// deletion and reappearing of feeds
if ($itemCount > $existingFeed->getArticlesPerUpdate()) {
$existingFeed->setArticlesPerUpdate($itemCount);
}
2015-01-23 16:32:53 +03:00
$existingFeed->setLastModified($fetchedFeed->getLastModified());
$existingFeed->setEtag($fetchedFeed->getEtag());
$existingFeed->setLocation($fetchedFeed->getLocation());
2015-01-23 16:32:53 +03:00
// insert items in reverse order because the first one is
// usually the newest item
for($i=$itemCount-1; $i>=0; $i--){
$item = $items[$i];
$item->setFeedId($existingFeed->getId());
2015-01-23 16:32:53 +03:00
try {
2015-10-25 16:11:23 +03:00
$dbItem = $this->itemMapper->findByGuidHash(
2015-01-23 16:32:53 +03:00
$item->getGuidHash(), $feedId, $userId
);
2015-10-25 16:11:23 +03:00
// in case of update
if ($forceUpdate ||
$item->getPubDate() > $dbItem->getPubDate()) {
$dbItem->setTitle($item->getTitle());
$dbItem->setUrl($item->getUrl());
$dbItem->setAuthor($item->getAuthor());
$dbItem->setSearchIndex($item->getSearchIndex());
$dbItem->setRtl($item->getRtl());
$dbItem->setLastModified($item->getLastModified());
$dbItem->setPubDate($item->getPubDate());
$dbItem->setEnclosureMime($item->getEnclosureMime());
$dbItem->setEnclosureLink($item->getEnclosureLink());
$dbItem->setBody(
$this->purifier->purify($item->getBody())
);
2015-11-01 16:25:05 +03:00
// update modes: 0 nothing, 1 set unread
if ($existingFeed->getUpdateMode() === 1) {
$dbItem->setUnread();
}
2015-10-25 16:11:23 +03:00
$this->itemMapper->update($dbItem);
}
2015-01-23 16:32:53 +03:00
} catch(DoesNotExistException $ex){
$item->setBody(
$this->purifier->purify($item->getBody())
);
$this->itemMapper->insert($item);
}
}
// mark feed as successfully updated
$existingFeed->setUpdateErrorCount(0);
2015-11-27 23:44:48 +03:00
$existingFeed->setLastUpdateError('');
2015-01-23 16:32:53 +03:00
} catch(FetcherException $ex){
$existingFeed->setUpdateErrorCount(
$existingFeed->getUpdateErrorCount()+1
2015-01-23 16:32:53 +03:00
);
2015-11-27 23:44:48 +03:00
$existingFeed->setLastUpdateError($ex->getMessage());
}
$this->feedMapper->update($existingFeed);
2015-01-23 16:32:53 +03:00
return $this->find($feedId, $userId);
}
/**
* 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()));
2015-08-26 14:03:55 +03:00
$item->generateSearchIndex();
$this->itemMapper->insert($item);
}
}
if($createdFeed) {
return $this->feedMapper->findByUrlHash($urlHash, $userId);
}
2014-06-25 13:22:08 +04:00
return null;
}
/**
* Use this to mark a feed as deleted. That way it can be un-deleted
* @param int $feedId the id of the feed that should be deleted
* @param string $userId the name of the user for security reasons
* @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
* @param int $feedId the id of the feed that should be restored
* @param string $userId the name of the user for security reasons
* @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
* 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);
}
2015-01-23 16:32:53 +03:00
/**
2015-11-01 16:25:05 +03:00
* @param $feedId
* @param $userId
* @param $diff an array containing the fields to update, e.g.:
* [
* 'ordering' => 1,
* 'fullTextEnabled' => true,
* 'pinned' => true,
2015-11-01 17:16:15 +03:00
* 'updateMode' => 0,
* 'title' => 'title'
2015-11-01 16:25:05 +03:00
* ]
* @throws ServiceNotFoundException if feed does not exist
2015-01-23 16:32:53 +03:00
*/
2015-11-01 17:16:15 +03:00
public function patch($feedId, $userId, $diff=[]) {
2015-11-01 16:25:05 +03:00
$feed = $this->find($feedId, $userId);
2015-01-23 16:32:53 +03:00
2015-11-01 17:18:32 +03:00
foreach ($diff as $attribute => $value) {
$method = 'set' . ucfirst($attribute);
$feed->$method($value);
2015-11-01 16:25:05 +03:00
}
2015-09-06 16:42:51 +03:00
2015-11-01 16:25:05 +03:00
// special feed updates
if (array_key_exists('fullTextEnabled', $diff)) {
// disable caching for the next update
$feed->setEtag('');
$feed->setLastModified(0);
$this->feedMapper->update($feed);
return $this->update($feedId, $userId, true);
}
2015-09-06 16:42:51 +03:00
2015-11-01 16:25:05 +03:00
return $this->feedMapper->update($feed);
}
2013-03-02 22:50:33 +04:00
}