This commit is contained in:
Bernhard Posselt 2013-04-04 13:23:03 +02:00
Родитель 3ec631a5c7
Коммит 1f2391b0cd
9 изменённых файлов: 101 добавлений и 76 удалений

Просмотреть файл

@ -35,7 +35,7 @@ class Task {
static public function run() {
$container = new DIContainer();
$container['FeedBl']->updateAll();
$container['FeedBl']->autoPurgeOld();
$container['ItemBl']->autoPurgeOld();
}

Просмотреть файл

@ -39,16 +39,13 @@ class FeedBl extends Bl {
private $feedFetcher;
private $itemMapper;
private $api;
private $autoPurgeCount;
public function __construct(FeedMapper $feedMapper, Fetcher $feedFetcher,
ItemMapper $itemMapper, API $api,
$autoPurgeCount=0){
ItemMapper $itemMapper, API $api){
parent::__construct($feedMapper);
$this->feedFetcher = $feedFetcher;
$this->itemMapper = $itemMapper;
$this->api = $api;
$this->autoPurgeCount = $autoPurgeCount;
}
@ -158,23 +155,4 @@ class FeedBl extends Bl {
}
/**
* 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());
}
}
}

Просмотреть файл

@ -34,10 +34,13 @@ use \OCA\News\Db\FeedType;
class ItemBl extends Bl {
private $statusFlag;
private $autoPurgeCount;
public function __construct(ItemMapper $itemMapper, StatusFlag $statusFlag){
public function __construct(ItemMapper $itemMapper, StatusFlag $statusFlag,
$autoPurgeCount=0){
parent::__construct($itemMapper);
$this->statusFlag = $statusFlag;
$this->autoPurgeCount = $autoPurgeCount;
}
@ -118,4 +121,23 @@ class ItemBl extends Bl {
}
/**
* 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());
}
}
}

Просмотреть файл

@ -183,4 +183,25 @@ class ItemMapper extends Mapper implements IMapper {
}
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){
$status = StatusFlag::STARRED | StatusFlag::UNREAD;
$sql = 'DELETE FROM `*PREFIX*news_items` WHERE `id` < ? ' .
'AND NOT ((`status` & ?) > 0)';
$params = array($id, $status);
$this->execute($sql, $params);
}
}

Просмотреть файл

@ -107,12 +107,12 @@ class DIContainer extends BaseContainer {
$this['FeedBl'] = $this->share(function($c){
return new FeedBl($c['FeedMapper'], $c['Fetcher'],
$c['ItemMapper'], $c['API'],
$c['autoPurgeCount']);
$c['ItemMapper'], $c['API']);
});
$this['ItemBl'] = $this->share(function($c){
return new ItemBl($c['ItemMapper'], $c['StatusFlag']);
return new ItemBl($c['ItemMapper'], $c['StatusFlag'],
$c['autoPurgeCount']);
});

Просмотреть файл

@ -50,7 +50,6 @@ class FeedBlTest extends \OCA\AppFramework\Utility\TestUtility {
private $threshold;
protected function setUp(){
$this->threshold = 2;
$this->api = $this->getAPIMock();
$this->mapper = $this->getMockBuilder('\OCA\News\Db\FeedMapper')
->disableOriginalConstructor()
@ -62,8 +61,7 @@ class FeedBlTest extends \OCA\AppFramework\Utility\TestUtility {
->disableOriginalConstructor()
->getMock();
$this->bl = new FeedBl($this->mapper,
$this->fetcher, $this->itemMapper, $this->api,
$this->threshold);
$this->fetcher, $this->itemMapper, $this->api);
$this->user = 'jack';
$response = 'hi';
@ -305,23 +303,6 @@ 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();
}
}

Просмотреть файл

@ -55,7 +55,8 @@ class ItemBlTest extends \OCA\AppFramework\Utility\TestUtility {
$statusFlag->expects($this->any())
->method('typeToStatus')
->will($this->returnValue($this->status));
$this->bl = new ItemBl($this->mapper, $statusFlag);
$this->threshold = 2;
$this->bl = new ItemBl($this->mapper, $statusFlag, $this->threshold);
$this->user = 'jack';
$response = 'hi';
$this->id = 3;
@ -245,6 +246,26 @@ class ItemBlTest extends \OCA\AppFramework\Utility\TestUtility {
$this->bl->readFeed($feedId, $highestItemId, $this->user);
}
public function testAutoPurgeOldWillPurgeOld(){
$item = new Item();
$item->setId(3);
$unread = array(
new Item(), $item
);
$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($item->getId()));
$result = $this->bl->autoPurgeOld();
}
}

Просмотреть файл

@ -249,32 +249,6 @@ 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;
$status = StatusFlag::STARRED | StatusFlag::UNREAD;
$sql = 'DELETE FROM `*PREFIX*news_items` WHERE `id` < ? ' .
'AND NOT ((`status` & ?) > 0)';
$params = array($id, $status);
$this->setMapperResult($sql, $params);
$this->mapper->deleteReadOlderThanId($id);
}
}

Просмотреть файл

@ -262,4 +262,32 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
$this->assertEquals($this->items[0], $result);
}
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;
$status = StatusFlag::STARRED | StatusFlag::UNREAD;
$sql = 'DELETE FROM `*PREFIX*news_items` WHERE `id` < ? ' .
'AND NOT ((`status` & ?) > 0)';
$params = array($id, $status);
$this->setMapperResult($sql, $params);
$this->mapper->deleteReadOlderThanId($id);
}
}