From 4b994c97ad878cc3886ffbea1a6a2bf6b4a98def Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Thu, 4 Apr 2013 13:12:07 +0200 Subject: autopurge all read items which are not starred if there are more than 1000 --- backgroundjob/task.php | 1 + bl/feedbl.php | 26 ++++++++++++++++++++++++-- bl/folderbl.php | 2 +- db/feedmapper.php | 24 ++++++++++++++++++++++-- dependencyinjection/dicontainer.php | 10 ++++++++-- js/Gruntfile.coffee | 2 +- tests/bl/FeedBlTest.php | 37 +++++++++++++++++++++++++++++-------- tests/db/FeedMapperTest.php | 26 ++++++++++++++++++++++++++ 8 files changed, 112 insertions(+), 16 deletions(-) diff --git a/backgroundjob/task.php b/backgroundjob/task.php index 3e9aadfd4..363657e61 100644 --- a/backgroundjob/task.php +++ b/backgroundjob/task.php @@ -35,6 +35,7 @@ class Task { static public function run() { $container = new DIContainer(); $container['FeedBl']->updateAll(); + $container['FeedBl']->autoPurgeOld(); } diff --git a/bl/feedbl.php b/bl/feedbl.php index 661f1f8f5..07cf5df8f 100644 --- a/bl/feedbl.php +++ b/bl/feedbl.php @@ -39,13 +39,16 @@ class FeedBl extends Bl { private $feedFetcher; private $itemMapper; private $api; + private $autoPurgeCount; public function __construct(FeedMapper $feedMapper, Fetcher $feedFetcher, - ItemMapper $itemMapper, API $api){ + ItemMapper $itemMapper, API $api, + $autoPurgeCount=0){ parent::__construct($feedMapper); $this->feedFetcher = $feedFetcher; $this->itemMapper = $itemMapper; $this->api = $api; + $this->autoPurgeCount = $autoPurgeCount; } @@ -154,5 +157,24 @@ class FeedBl extends Bl { $this->mapper->update($feed); } - // TODO: delete associated items + + /** + * This method deletes all unread feeds that are not starred and over the + * count of $this->autoPurgeCount starting by the oldest. This is to clean + * up the database so that old entries dont spam your db. As criteria for + * old, the id is taken + */ + public function autoPurgeOld(){ + $readAndNotStarred = + $this->mapper->getReadOlderThanThreshold($this->autoPurgeCount); + + // delete entries with a lower id than last item + if($this->autoPurgeCount > 0 + && isset($readAndNotStarred[$this->autoPurgeCount-1])){ + $this->mapper->deleteReadOlderThanId( + $readAndNotStarred[$this->autoPurgeCount-1]->getId()); + } + } + + } diff --git a/bl/folderbl.php b/bl/folderbl.php index c17c516ba..3fcf48c7f 100644 --- a/bl/folderbl.php +++ b/bl/folderbl.php @@ -81,6 +81,6 @@ class FolderBl extends Bl { $this->mapper->update($folder); } - // TODO: delete associated items + } diff --git a/db/feedmapper.php b/db/feedmapper.php index d84cebad1..ba47d0a8a 100644 --- a/db/feedmapper.php +++ b/db/feedmapper.php @@ -57,8 +57,8 @@ class FeedMapper extends Mapper implements IMapper { } - private function findAllRows($sql, $params=array()){ - $result = $this->execute($sql, $params); + private function findAllRows($sql, $params=array(), $limit=null){ + $result = $this->execute($sql, $params, $limit); $feeds = array(); while($row = $result->fetchRow()){ @@ -121,4 +121,24 @@ class FeedMapper extends Mapper implements IMapper { $this->execute($sql, $params); } + + public function getReadOlderThanThreshold($threshold){ + + // we want items that are not starred and not unread + $status = StatusFlag::STARRED | StatusFlag::UNREAD; + $sql = 'SELECT * FROM `*PREFIX*news_items` ' . + 'WHERE NOT ((`status` & ?) > 0)'; + + $params = array($status); + return $this->findAllRows($sql, $params, $threshold); + } + + + public function deleteReadOlderThanId($id){ + $sql = 'DELETE FROM `*PREFIX*news_items` WHERE `id` < ?'; + $params = array($id); + $this->execute($sql, $params); + } + + } \ No newline at end of file diff --git a/dependencyinjection/dicontainer.php b/dependencyinjection/dicontainer.php index 6dfc0308c..4d5a6420c 100644 --- a/dependencyinjection/dicontainer.php +++ b/dependencyinjection/dicontainer.php @@ -61,6 +61,11 @@ class DIContainer extends BaseContainer { // tell parent container about the app name parent::__construct('news'); + /** + * Configuration values + */ + $this['autoPurgeCount'] = 1000; + /** * CONTROLLERS @@ -101,8 +106,9 @@ class DIContainer extends BaseContainer { }); $this['FeedBl'] = $this->share(function($c){ - return new FeedBl($c['FeedMapper'], $c['Fetcher'], - $c['ItemMapper'], $c['API']); + return new FeedBl($c['FeedMapper'], $c['Fetcher'], + $c['ItemMapper'], $c['API'], + $c['autoPurgeCount']); }); $this['ItemBl'] = $this->share(function($c){ diff --git a/js/Gruntfile.coffee b/js/Gruntfile.coffee index c5268a3ee..8ea32dcd4 100644 --- a/js/Gruntfile.coffee +++ b/js/Gruntfile.coffee @@ -42,7 +42,7 @@ module.exports = (grunt) -> '<%= meta.pkg.author.name %> <<%= meta.pkg.author.email %>>\n' + ' *\n' + ' * This file is licensed under the Affero General Public License version 3 or later.\n' + - ' * See the COPYING-README file\n' + + ' * See the COPYING file\n' + ' *\n' + ' */\n\n' build: 'build/' diff --git a/tests/bl/FeedBlTest.php b/tests/bl/FeedBlTest.php index 818562fb9..5ad02e789 100644 --- a/tests/bl/FeedBlTest.php +++ b/tests/bl/FeedBlTest.php @@ -41,15 +41,16 @@ use \OCA\News\Utility\FetcherException; class FeedBlTest extends \OCA\AppFramework\Utility\TestUtility { - protected $api; - protected $mapper; - protected $bl; - protected $user; - protected $response; - protected $fetcher; - protected $itemMapper; + private $mapper; + private $bl; + private $user; + private $response; + private $fetcher; + private $itemMapper; + private $threshold; protected function setUp(){ + $this->threshold = 2; $this->api = $this->getAPIMock(); $this->mapper = $this->getMockBuilder('\OCA\News\Db\FeedMapper') ->disableOriginalConstructor() @@ -61,9 +62,11 @@ class FeedBlTest extends \OCA\AppFramework\Utility\TestUtility { ->disableOriginalConstructor() ->getMock(); $this->bl = new FeedBl($this->mapper, - $this->fetcher, $this->itemMapper, $this->api); + $this->fetcher, $this->itemMapper, $this->api, + $this->threshold); $this->user = 'jack'; $response = 'hi'; + } @@ -302,6 +305,24 @@ class FeedBlTest extends \OCA\AppFramework\Utility\TestUtility { } + public function testAutoPurgeOldWillPurgeOld(){ + $feed = new Feed(); + $feed->setId(3); + $unread = array( + new Feed(), $feed + ); + $this->mapper->expects($this->once()) + ->method('getReadOlderThanThreshold') + ->with($this->equalTo($this->threshold)) + ->will($this->returnValue($unread)); + $this->mapper->expects($this->once()) + ->method('deleteReadOlderThanId') + ->with($this->equalTo($feed->getId())); + + $result = $this->bl->autoPurgeOld(); + + } + } } \ No newline at end of file diff --git a/tests/db/FeedMapperTest.php b/tests/db/FeedMapperTest.php index 11afbbe7f..de414d9c8 100644 --- a/tests/db/FeedMapperTest.php +++ b/tests/db/FeedMapperTest.php @@ -249,4 +249,30 @@ class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { } + public function testGetReadOlderThanThreshold(){ + $status = StatusFlag::STARRED | StatusFlag::UNREAD; + $sql = 'SELECT * FROM `*PREFIX*news_items` ' . + 'WHERE NOT ((`status` & ?) > 0)'; + $threshold = 10; + $feed = new Feed(); + $feed->setId(30); + $rows = array(array('id' => 30)); + $params = array($status); + + $this->setMapperResult($sql, $params, $rows); + $result = $this->mapper->getReadOlderThanThreshold($threshold); + + $this->assertEquals($feed->getId(), $result[0]->getId()); + } + + + public function testDeleteReadOlderThanId(){ + $id = 10; + $sql = 'DELETE FROM `*PREFIX*news_items` WHERE `id` < ?'; + $params = array($id); + + $this->setMapperResult($sql, $params); + $this->mapper->deleteReadOlderThanId($id); + } + } \ No newline at end of file -- cgit v1.2.3