<commit_before><?php
/**
 * ownCloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> 2012
 * @copyright <NAME> 2012, 2014
 */

namespace OCA\News\BusinessLayer;
use \OCA\News\Core\Logger;
use \OCA\News\Db\DoesNotExistException;
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\ArticleEnhancer\Enhancer;
use \OCA\News\Utility\Config;


class FeedBusinessLayer extends BusinessLayer {

	private $feedFetcher;
	private $itemMapper;
	private $logger;
	private $l10n;
	private $timeFactory;
	private $autoPurgeMinimumInterval;
	private $enhancer;
	private $purifier;

	public function __construct(FeedMapper $feedMapper, 
	                            Fetcher $feedFetcher,
		                        ItemMapper $itemMapper, 
		                        Logger $logger,
		                        $l10n,
		                        $timeFactory,
		                        Config $config,
		                        Enhancer $enhancer,
		                        $purifier){
		parent::__construct($feedMapper);
		$this->feedFetcher = $feedFetcher;
		$this->itemMapper = $itemMapper;
		$this->logger = $logger;
		$this->l10n = $l10n;
		$this->timeFactory = $timeFactory;
		$this->autoPurgeMinimumInterval = $config->getAutoPurgeMinimumInterval();
		$this->enhancer = $enhancer;
		$this->purifier = $purifier;
	}

	/**
	 * Finds all feeds of a user
	 * @param string $userId the name of the user
	 * @return array of feeds
	 */
	public function findAll($userId){
		return $this->mapper->findAllFromUser($userId);
	}


	/**
	 * Finds all feeds from all users
	 * @return array of feeds
	 */
	public function findAllFromAllUsers() {
		return $this->mapper->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
	 * @throws BusinessLayerConflictException if the feed exists already
	 * @throws BusinessLayerException if the url points to an invalid feed
	 * @return Feed the newly created feed
	 */
	public function create($feedUrl, $folderId, $userId){
		// first try if the feed exists already
		try {
			list($feed, $items) = $this->feedFetcher->fetch($feedUrl);

			// try again if feed exists depending on the reported link
			try {
				$this->mapper->findByUrlHash($feed->getUrlHash(), $userId);
				throw new BusinessLayerConflictException(
					$this->l10n->t('Can not add feed: Exists already'));
			} catch(DoesNotExistException $ex){}

			// insert feed
			$feed->setFolderId($folderId);
			$feed->setUserId($userId);
			$feed->setArticlesPerUpdate(count($items));
			$feed = $this->mapper->insert($feed);

			// 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];
				$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);
				}
			}

			// set unread count
			$feed->setUnreadCount($unreadCount);

			return $feed;
		} catch(FetcherException $ex){
			$this->logger->log($ex->getMessage(), 'debug');
			throw new BusinessLayerException(
				$this->l10n->t(
					'Can not add feed: URL does not exist, SSL Certificate can not be validated
					or feed has invalid xml'));
		}
	}


	/**
	 * Runs all the feed updates
	 */
	public function updateAll(){
		// TODO: this method is not covered by any tests
		$feeds = $this->mapper->findAll();
		foreach($feeds as $feed){
			try {
				$this->update($feed->getId(), $feed->getUserId());
			} catch(BusinessLayerException $ex){<commit_msg>Update feedbusinesslayer.php<commit_after><?php
/**
 * ownCloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> 2012
 * @copyright <NAME> 2012, 2014
 */

namespace OCA\News\BusinessLayer;
use \OCA\News\Core\Logger;
use \OCA\News\Db\DoesNotExistException;
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\ArticleEnhancer\Enhancer;
use \OCA\News\Utility\Config;


class FeedBusinessLayer extends BusinessLayer {

	private $feedFetcher;
	private $itemMapper;
	private $logger;
	private $l10n;
	private $timeFactory;
	private $autoPurgeMinimumInterval;
	private $enhancer;
	private $purifier;

	public function __construct(FeedMapper $feedMapper, 
	                            Fetcher $feedFetcher,
		                        ItemMapper $itemMapper, 
		                        Logger $logger,
		                        $l10n,
		                        $timeFactory,
		                        Config $config,
		                        Enhancer $enhancer,
		                        $purifier){
		parent::__construct($feedMapper);
		$this->feedFetcher = $feedFetcher;
		$this->itemMapper = $itemMapper;
		$this->logger = $logger;
		$this->l10n = $l10n;
		$this->timeFactory = $timeFactory;
		$this->autoPurgeMinimumInterval = $config->getAutoPurgeMinimumInterval();
		$this->enhancer = $enhancer;
		$this->purifier = $purifier;
	}

	/**
	 * Finds all feeds of a user
	 * @param string $userId the name of the user
	 * @return array of feeds
	 */
	public function findAll($userId){
		return $this->mapper->findAllFromUser($userId);
	}


	/**
	 * Finds all feeds from all users
	 * @return array of feeds
	 */
	public function findAllFromAllUsers() {
		return $this->mapper->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
	 * @throws BusinessLayerConflictException if the feed exists already
	 * @throws BusinessLayerException if the url points to an invalid feed
	 * @return Feed the newly created feed
	 */
	public function create($feedUrl, $folderId, $userId){
		// first try if the feed exists already
		try {
			list($feed, $items) = $this->feedFetcher->fetch($feedUrl);

			// try again if feed exists depending on the reported link
			try {
				$this->mapper->findByUrlHash($feed->getUrlHash(), $userId);
				throw new BusinessLayerConflictException(
					$this->l10n->t('Can not add feed: Exists already'));
			} catch(DoesNotExistException $ex){}

			// insert feed
			$feed->setFolderId($folderId);
			$feed->setUserId($userId);
			$feed->setArticlesPerUpdate(count($items));
			$feed = $this->mapper->insert($feed);

			// 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];
				$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);
				}
			}

			// set unread count
			$feed->setUnreadCount($unreadCount);

			return $feed;
		} catch(FetcherException $ex){
			$this->logger->log($ex->getMessage(), 'debug');
			throw new BusinessLayerException(
				$this->l10n->t(
					'Can not add feed: URL does not exist, SSL Certificate can not be validated ' .
					'or feed has invalid xml'));
		}
	}


	/**
	 * Runs all the feed updates
	 */
	public function updateAll(){
		// TODO: this method is not covered by any tests
		$feeds = $this->mapper->findAll();
		foreach($feeds as $feed){
			try {
				$this->update($feed->getId(), $feed->getUserId());
			} catch(BusinessLayerException $ex){
{'fraction_non_alphanumeric': 0.13584554547569203, 'fraction_numerical': 0.006978367062107467, 'mean_word_length': 3.056132075471698, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5267912', 'n_tokens_mistral': 3093, 'n_tokens_neox': 2688, 'n_words': 734}