From 9507e6a3ee7303a74f9c36b8d87cd0101c26d313 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Fri, 2 Aug 2013 15:31:55 +0200 Subject: make cron updater optional, created updater class for better updating handling, created a route for the api to clean up old stuff --- appinfo/routes.php | 6 +++ backgroundjob/task.php | 11 ++++-- dependencyinjection/dicontainer.php | 64 ++++++++++++++++-------------- external/newsapi.php | 17 +++++++- tests/unit/external/NewsAPITest.php | 18 ++++++++- tests/unit/utility/UpdaterTest.php | 77 +++++++++++++++++++++++++++++++++++++ utility/updater.php | 62 +++++++++++++++++++++++++++++ 7 files changed, 220 insertions(+), 35 deletions(-) create mode 100644 tests/unit/utility/UpdaterTest.php create mode 100644 utility/updater.php diff --git a/appinfo/routes.php b/appinfo/routes.php index 25eb04dc2..d60f63424 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -233,6 +233,12 @@ $this->create('news_api_version', '/api/v1-2/version')->get()->action( } ); +$this->create('news_api_cleanup', '/api/v1-2/cleanUp')->get()->action( + function($params) { + return App::main('NewsAPI', 'cleanUp', $params, new DIContainer()); + } +); + /** * Folder API */ diff --git a/backgroundjob/task.php b/backgroundjob/task.php index 24470c1a6..d432aa8c2 100644 --- a/backgroundjob/task.php +++ b/backgroundjob/task.php @@ -34,10 +34,13 @@ class Task { static public function run() { $container = new DIContainer(); - $container['FolderBusinessLayer']->purgeDeleted(); - $container['FeedBusinessLayer']->purgeDeleted(); - $container['ItemBusinessLayer']->autoPurgeOld(); - $container['FeedBusinessLayer']->updateAll(); + + // make it possible to turn off cron updates if you use an external + // script to execute updates in paralell + if ($container['useCronUpdates']) { + $container['Updater']->cleanUp(); + $container['Updater']->update(); + } } diff --git a/dependencyinjection/dicontainer.php b/dependencyinjection/dicontainer.php index 6a6ca0832..963cef7a1 100644 --- a/dependencyinjection/dicontainer.php +++ b/dependencyinjection/dicontainer.php @@ -53,6 +53,7 @@ use \OCA\News\Utility\FeedFetcher; use \OCA\News\Utility\TwitterFetcher; use \OCA\News\Utility\OPMLExporter; use \OCA\News\Utility\ImportParser; +use \OCA\News\Utility\Updater; require_once __DIR__ . '/../3rdparty/htmlpurifier/library/HTMLPurifier.auto.php'; @@ -70,18 +71,19 @@ class DIContainer extends BaseContainer { /** * Configuration values */ - $this['autoPurgeMinimumInterval'] = 60; // seconds, used to define how + $this['autoPurgeMinimumInterval'] = 60; // seconds, used to define how // long deleted folders and feeds - // should still be kept for an + // should still be kept for an // undo actions $this['autoPurgeCount'] = 200; // number of allowed unread articles per feed $this['simplePieCacheDuration'] = 30*60; // seconds $this['feedFetcherTimeout'] = 60; // seconds + $this['useCronUpdates'] = true; $this['simplePieCacheDirectory'] = $this->share(function($c) { - $directory = $c['API']->getSystemValue('datadirectory') . + $directory = $c['API']->getSystemValue('datadirectory') . '/news/cache/simplepie'; - + if(!is_dir($directory)) { mkdir($directory, 0770, true); } @@ -90,9 +92,9 @@ class DIContainer extends BaseContainer { }); $this['HTMLPurifier'] = $this->share(function($c) { - $directory = $c['API']->getSystemValue('datadirectory') . + $directory = $c['API']->getSystemValue('datadirectory') . '/news/cache/purifier'; - + if(!is_dir($directory)) { mkdir($directory, 0770, true); } @@ -100,13 +102,13 @@ class DIContainer extends BaseContainer { $config = \HTMLPurifier_Config::createDefault(); $config->set('Cache.SerializerPath', $directory); $config->set('HTML.SafeIframe', true); - $config->set('URI.SafeIframeRegexp', + $config->set('URI.SafeIframeRegexp', '%^http://(www.youtube(?:-nocookie)?.com/embed/|player.vimeo.com/video/)%'); //allow YouTube and Vimeo return new \HTMLPurifier($config); }); - /** + /** * CONTROLLERS */ $this['PageController'] = $this->share(function($c){ @@ -114,34 +116,34 @@ class DIContainer extends BaseContainer { }); $this['FolderController'] = $this->share(function($c){ - return new FolderController($c['API'], $c['Request'], - $c['FolderBusinessLayer'], + return new FolderController($c['API'], $c['Request'], + $c['FolderBusinessLayer'], $c['FeedBusinessLayer'], $c['ItemBusinessLayer']); }); $this['FeedController'] = $this->share(function($c){ - return new FeedController($c['API'], $c['Request'], - $c['FolderBusinessLayer'], - $c['FeedBusinessLayer'], + return new FeedController($c['API'], $c['Request'], + $c['FolderBusinessLayer'], + $c['FeedBusinessLayer'], $c['ItemBusinessLayer']); }); $this['ItemController'] = $this->share(function($c){ - return new ItemController($c['API'], $c['Request'], + return new ItemController($c['API'], $c['Request'], $c['FeedBusinessLayer'], $c['ItemBusinessLayer']); }); $this['ExportController'] = $this->share(function($c){ - return new ExportController($c['API'], $c['Request'], + return new ExportController($c['API'], $c['Request'], $c['FeedBusinessLayer'], - $c['FolderBusinessLayer'], + $c['FolderBusinessLayer'], $c['OPMLExporter']); }); $this['UserSettingsController'] = $this->share(function($c){ - return new UserSettingsController($c['API'], $c['Request'], + return new UserSettingsController($c['API'], $c['Request'], $c['ItemBusinessLayer']); }); @@ -151,7 +153,7 @@ class DIContainer extends BaseContainer { */ $this['FolderBusinessLayer'] = $this->share(function($c){ return new FolderBusinessLayer( - $c['FolderMapper'], + $c['FolderMapper'], $c['API'], $c['TimeFactory'], $c['autoPurgeMinimumInterval']); @@ -159,20 +161,20 @@ class DIContainer extends BaseContainer { $this['FeedBusinessLayer'] = $this->share(function($c){ return new FeedBusinessLayer( - $c['FeedMapper'], + $c['FeedMapper'], $c['Fetcher'], - $c['ItemMapper'], - $c['API'], + $c['ItemMapper'], + $c['API'], $c['TimeFactory'], - $c['ImportParser'], + $c['ImportParser'], $c['autoPurgeMinimumInterval']); }); $this['ItemBusinessLayer'] = $this->share(function($c){ return new ItemBusinessLayer( - $c['ItemMapper'], + $c['ItemMapper'], $c['StatusFlag'], - $c['TimeFactory'], + $c['TimeFactory'], $c['autoPurgeCount']); }); @@ -197,24 +199,24 @@ class DIContainer extends BaseContainer { * External API */ $this['NewsAPI'] = $this->share(function($c){ - return new NewsAPI($c['API'], $c['Request']); + return new NewsAPI($c['API'], $c['Request'], $c['Updater']); }); $this['FolderAPI'] = $this->share(function($c){ - return new FolderAPI($c['API'], $c['Request'], + return new FolderAPI($c['API'], $c['Request'], $c['FolderBusinessLayer'], $c['ItemBusinessLayer']); }); $this['FeedAPI'] = $this->share(function($c){ return new FeedAPI($c['API'], $c['Request'], - $c['FolderBusinessLayer'], + $c['FolderBusinessLayer'], $c['FeedBusinessLayer'], $c['ItemBusinessLayer']); }); $this['ItemAPI'] = $this->share(function($c){ - return new ItemAPI($c['API'], $c['Request'], + return new ItemAPI($c['API'], $c['Request'], $c['ItemBusinessLayer']); }); @@ -234,7 +236,7 @@ class DIContainer extends BaseContainer { $this['FeedFetcher'] = $this->share(function($c){ return new FeedFetcher( - $c['API'], + $c['API'], $c['SimplePieAPIFactory'], $c['FaviconFetcher'], $c['TimeFactory'], @@ -260,6 +262,10 @@ class DIContainer extends BaseContainer { return new OPMLExporter(); }); + $this['Updater'] = $this->share(function($c){ + return new Updater(); + }); + } } diff --git a/external/newsapi.php b/external/newsapi.php index cb6231b80..331b65556 100644 --- a/external/newsapi.php +++ b/external/newsapi.php @@ -30,12 +30,16 @@ use \OCA\AppFramework\Controller\Controller; use \OCA\AppFramework\Http\Request; use \OCA\AppFramework\Http\JSONResponse; +use \OCA\News\Utility\Updater; + class NewsAPI extends Controller { + private $updater; - public function __construct(API $api, Request $request){ + public function __construct(API $api, Request $request, Updater $updater){ parent::__construct($api, $request); + $this->updater = $updater; } @@ -52,4 +56,15 @@ class NewsAPI extends Controller { } + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @CSRFExemption + * @Ajax + * @API + */ + public function cleanUp() { + $this->updater->cleanUp(); + } + } diff --git a/tests/unit/external/NewsAPITest.php b/tests/unit/external/NewsAPITest.php index e1fd2e50a..b5cd808e3 100644 --- a/tests/unit/external/NewsAPITest.php +++ b/tests/unit/external/NewsAPITest.php @@ -36,6 +36,7 @@ class NewsAPITest extends ControllerTestUtility { private $api; private $request; private $newsAPI; + private $updater; protected function setUp() { $this->api = $this->getMockBuilder( @@ -46,7 +47,11 @@ class NewsAPITest extends ControllerTestUtility { '\OCA\AppFramework\Http\Request') ->disableOriginalConstructor() ->getMock(); - $this->newsAPI = new NewsAPI($this->api, $this->request); + $this->updater = $this->getMockBuilder( + '\OCA\News\Utility\Updater') + ->disableOriginalConstructor() + ->getMock(); + $this->newsAPI = new NewsAPI($this->api, $this->request, $this->updater); } @@ -60,6 +65,10 @@ class NewsAPITest extends ControllerTestUtility { $this->assertDefaultAnnotations('version'); } + public function testCleanUpAnnotations(){ + $this->assertDefaultAnnotations('cleanUp'); + } + public function testGetVersion(){ $this->api->expects($this->once()) ->method('getAppValue') @@ -74,4 +83,11 @@ class NewsAPITest extends ControllerTestUtility { } + public function testCleanUp(){ + $this->updater->expects($this->once()) + ->method('cleanUp'); + $this->newsAPI->cleanUp(); + } + + } diff --git a/tests/unit/utility/UpdaterTest.php b/tests/unit/utility/UpdaterTest.php new file mode 100644 index 000000000..a5191e9f2 --- /dev/null +++ b/tests/unit/utility/UpdaterTest.php @@ -0,0 +1,77 @@ +. +* +*/ + +namespace OCA\News\Utility; + +use \OCA\News\BusinessLayer\FolderBusinessLayer; +use \OCA\News\BusinessLayer\FeedBusinessLayer; +use \OCA\News\BusinessLayer\ItemBusinessLayer; + + +require_once(__DIR__ . "/../../classloader.php"); + + +class UpdaterTest extends \PHPUnit_Framework_TestCase { + + private $folderBusinessLayer; + private $feedBusinessLayer; + private $itemBusinessLayer; + private $updater; + + protected function setUp() { + $this->folderBusinessLayer = $this->getMockBuilder( + '\OCA\News\BusinessLayer\FolderBusinessLayer') + ->disableOriginalConstructor() + ->getMock(); + $this->feedBusinessLayer = $this->getMockBuilder( + '\OCA\News\BusinessLayer\FeedBusinessLayer') + ->disableOriginalConstructor() + ->getMock(); + $this->itemBusinessLayer = $this->getMockBuilder( + '\OCA\News\BusinessLayer\ItemBusinessLayer') + ->disableOriginalConstructor() + ->getMock(); + $this->updater = new Updater($this->folderBusinessLayer, + $this->feedBusinessLayer, + $this->itemBusinessLayer); + } + + public function testCleanUp() { + $this->folderBusinessLayer->expects($this->once()) + ->method('purgeDeleted'); + $this->feedBusinessLayer->expects($this->once()) + ->method('purgeDeleted'); + $this->itemBusinessLayer->expects($this->once()) + ->method('autoPurgeOld'); + $this->updater->cleanUp(); + } + + + public function testUpdate() { + $this->feedBusinessLayer->expects($this->once()) + ->method('updateAll'); + $this->updater->update(); + } +} \ No newline at end of file diff --git a/utility/updater.php b/utility/updater.php new file mode 100644 index 000000000..96296bd06 --- /dev/null +++ b/utility/updater.php @@ -0,0 +1,62 @@ +. +* +*/ + + +namespace OCA\News\Utility; + +use \OCA\News\BusinessLayer\FolderBusinessLayer; +use \OCA\News\BusinessLayer\FeedBusinessLayer; +use \OCA\News\BusinessLayer\ItemBusinessLayer; + + +class Updater { + + + private $folderBusinessLayer; + private $feedBusinessLayer; + private $itemBusinessLayer; + + public function __construct(FolderBusinessLayer $folderBusinessLayer, + FeedBusinessLayer $feedBusinessLayer, + ItemBusinessLayer $itemBusinessLayer) { + $this->folderBusinessLayer = $folderBusinessLayer; + $this->feedBusinessLayer = $feedBusinessLayer; + $this->itemBusinessLayer = $itemBusinessLayer; + } + + + public function cleanUp() { + $this->folderBusinessLayer->purgeDeleted(); + $this->feedBusinessLayer->purgeDeleted(); + $this->itemBusinessLayer->autoPurgeOld(); + } + + + public function update() { + $this->feedBusinessLayer->updateAll(); + } + + +} \ No newline at end of file -- cgit v1.2.3