summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBernhard Posselt <nukeawhale@gmail.com>2013-09-02 02:43:25 +0200
committerBernhard Posselt <nukeawhale@gmail.com>2013-09-02 02:43:32 +0200
commit38297af11f458b30af3cbdc42cf3407d6aff44ab (patch)
tree6c61f09234182c78fe291bcd439284c8e77e7ffe
parent1b144268cbb49854aaba1c04c773dcf2f9cf0b7a (diff)
move configuration values into a config file, fix #167
-rw-r--r--CHANGELOG1
-rw-r--r--README.rst35
-rw-r--r--dependencyinjection/dicontainer.php35
-rw-r--r--tests/unit/utility/ConfigTest.php143
-rw-r--r--utility/config.php147
5 files changed, 332 insertions, 29 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 7dd059a33..a396953bf 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -8,6 +8,7 @@ owncloud-news (1.401)
* Throw proper error codes when creating invalid folders through the API
* More whitespace to fit ownCloud 6 design better
* Increased unread count from 99+ to maximum of 999+ because there is now more space
+* Use a configuration file in data/news/config/config.json to not overwrite uservalues on update
owncloud-news (1.206)
* Also handle URLErrors in updater script that are thrown when the domain of a feed is not found
diff --git a/README.rst b/README.rst
index 3e7f89081..f4fb68959 100644
--- a/README.rst
+++ b/README.rst
@@ -147,33 +147,28 @@ This problem is related to opcode caching, `check the issue tracker for how to s
Configuration
-------------
-This will be in a separate config file in the future but for now you can tweak the following things.
+All configuration values are set inside :file:`owncloud/data/news/config/config.json`
-:file:`dependencyinjection/dicontainer.php`
+The configuration is in **JSON** and looks like this:
-* To use a custom update/cron script you need to disable the cronjob which is run by ownCloud by default::
+.. code-block:: js
- $this['useCronUpdates'] = false;
+ {
+ "autoPurgeMinimumInterval": 60,
+ "autoPurgeCount": 200,
+ "simplePieCacheDuration": 1800,
+ "feedFetcherTimeout": 60,
+ "useCronUpdates": true
+ }
-* To cache feeds longer increase::
+* **autoPurgeMinimumInterval**: Minimum amount of seconds after deleted feeds and folders are removed from the database.
+* **autoPurgeCount**: To let people have more read and unstarred items per feed before they are purged increase this value
+* **simplePieCacheDuration**: Amount of seconds to cache feeds
+* **feedFetcherTimeout**: Maximum number of seconds to wait for an RSS or Atom feed to load. If a feed takes longer than that number of seconds to update, the update will be aborted
+* **useCronUpdates**: To use a custom update/cron script you need to disable the cronjob which is run by ownCloud by default by setting this to false
- $this['simplePieCacheDuration'] = 30*60; // seconds
-* To let people have more read items per feed before they are purged increase::
- $this['autoPurgeCount'] = 200; // per feed
-
-:file:`js/app/app.coffee`
-
-All changes in the coffee file have to be compiled by using::
-
- make
-
-in the **js/** directory
-
-* To increase the interval when the app fetches new entries from database(!, not the webpage, thats set by the backgroundjob interval) change::
-
- feedUpdateInterval: 1000*60*3 # miliseconds
diff --git a/dependencyinjection/dicontainer.php b/dependencyinjection/dicontainer.php
index de49236c6..697ee0b88 100644
--- a/dependencyinjection/dicontainer.php
+++ b/dependencyinjection/dicontainer.php
@@ -25,6 +25,8 @@
namespace OCA\News\DependencyInjection;
+use \OC\Files\View;
+
use \OCA\AppFramework\DependencyInjection\DIContainer as BaseContainer;
use \OCA\AppFramework\Middleware\MiddlewareDispatcher;
@@ -49,6 +51,7 @@ use \OCA\News\External\FolderAPI;
use \OCA\News\External\FeedAPI;
use \OCA\News\External\ItemAPI;
+use \OCA\News\Utility\Config;
use \OCA\News\Utility\Fetcher;
use \OCA\News\Utility\FeedFetcher;
use \OCA\News\Utility\TwitterFetcher;
@@ -77,14 +80,29 @@ class DIContainer extends BaseContainer {
/**
* Configuration values
*/
- $this['autoPurgeMinimumInterval'] = 60; // seconds, used to define how
- // long deleted folders and feeds
- // 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['configView'] = $this->share(function($c) {
+ $view = new View('/news/config');
+ if (!$view->file_exists('')) {
+ $view->mkdir('');
+ }
+
+ return $view;
+ });
+
+ $this['Config'] = $this->share(function($c) {
+ $config = new Config($c['configView'], $c['API']);
+ $config->read('config.json', true);
+ return $config;
+ });
+
+ // set by config class from config file
+ // look up defaults in utility/config.php
+ $this['autoPurgeMinimumInterval'] = $this['Config']->getAutoPurgeMinimumInterval();
+ $this['autoPurgeCount'] = $this['Config']->getAutoPurgeCount();
+ $this['simplePieCacheDuration'] = $this['Config']->getSimplePieCacheDuration();
+ $this['feedFetcherTimeout'] = $this['Config']->getFeedFetcherTimeout();
+ $this['useCronUpdates'] = $this['Config']->getUseCronUpdates();
+
$this['simplePieCacheDirectory'] = $this->share(function($c) {
$directory = $c['API']->getSystemValue('datadirectory') .
@@ -94,7 +112,6 @@ class DIContainer extends BaseContainer {
mkdir($directory, 0770, true);
}
return $directory;
-
});
$this['HTMLPurifier'] = $this->share(function($c) {
diff --git a/tests/unit/utility/ConfigTest.php b/tests/unit/utility/ConfigTest.php
new file mode 100644
index 000000000..849fe37fd
--- /dev/null
+++ b/tests/unit/utility/ConfigTest.php
@@ -0,0 +1,143 @@
+<?php
+
+/**
+* ownCloud - News
+*
+* @author Alessandro Cosentino
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+*
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+
+namespace OCA\News\Utility;
+
+require_once(__DIR__ . "/../../classloader.php");
+
+
+class ConfigFetcherTest extends \OCA\AppFramework\Utility\TestUtility {
+
+ private $fileSystem;
+ private $config;
+ private $configPath;
+
+ public function setUp() {
+ $this->api = $this->getMockBuilder(
+ '\OCA\AppFramework\Core\API')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->fileSystem = $this->getMock('FileSystem', array(
+ 'file_get_contents',
+ 'file_put_contents',
+ 'file_exists'
+ ));
+ $this->config = new Config($this->fileSystem, $this->api);
+ $this->configPath = 'config.json';
+ }
+
+
+ public function testDefaults() {
+ $this->assertEquals(60, $this->config->getAutoPurgeMinimumInterval());
+ $this->assertEquals(200, $this->config->getAutoPurgeCount());
+ $this->assertEquals(30*60, $this->config->getSimplePieCacheDuration());
+ $this->assertEquals(60, $this->config->getFeedFetcherTimeout());
+ $this->assertEquals(true, $this->config->getUseCronUpdates());
+ }
+
+
+ public function testRead () {
+ $this->fileSystem->expects($this->once())
+ ->method('file_get_contents')
+ ->with($this->equalTo($this->configPath))
+ ->will($this->returnValue('{"autoPurgeCount": 3}'));
+
+ $this->config->read($this->configPath);
+
+ $this->assertEquals(3, $this->config->getAutoPurgeCount());
+ }
+
+
+ public function testReadLogsInvalidValue() {
+ $this->fileSystem->expects($this->once())
+ ->method('file_get_contents')
+ ->with($this->equalTo($this->configPath))
+ ->will($this->returnValue('{"autoPurgeCounts": 3}'));
+ $this->api->expects($this->once())
+ ->method('log')
+ ->with($this->equalTo('Configuration value "autoPurgeCounts" ' .
+ 'does not exist. Ignored value.'),
+ $this->equalTo('warn'));
+
+ $this->config->read($this->configPath);
+ }
+
+
+ public function testReadLogsInvalidJSON() {
+ $this->fileSystem->expects($this->once())
+ ->method('file_get_contents')
+ ->with($this->equalTo($this->configPath))
+ ->will($this->returnValue(null));
+ $this->api->expects($this->once())
+ ->method('log')
+ ->with($this->equalTo('Configuration contains invalid JSON'),
+ $this->equalTo('warn'));
+
+ $this->config->read($this->configPath);
+ }
+
+
+ public function testWrite () {
+ $json = "{\n" .
+ " \"autoPurgeMinimumInterval\": 60,\n" .
+ " \"autoPurgeCount\": 3,\n" .
+ " \"simplePieCacheDuration\": 1800,\n" .
+ " \"feedFetcherTimeout\": 60,\n" .
+ " \"useCronUpdates\": true\n" .
+ "}";
+ $this->config->setAutoPurgeCount(3);
+
+ $this->fileSystem->expects($this->once())
+ ->method('file_put_contents')
+ ->with($this->equalTo($this->configPath),
+ $this->equalTo($json));
+
+ $this->config->write($this->configPath);
+ }
+
+
+ public function testReadingNonExistentConfigWillWriteDefaults() {
+ $this->fileSystem->expects($this->once())
+ ->method('file_exists')
+ ->with($this->equalTo($this->configPath))
+ ->will($this->returnValue(false));
+
+ $json = "{\n" .
+ " \"autoPurgeMinimumInterval\": 60,\n" .
+ " \"autoPurgeCount\": 200,\n" .
+ " \"simplePieCacheDuration\": 1800,\n" .
+ " \"feedFetcherTimeout\": 60,\n" .
+ " \"useCronUpdates\": true\n" .
+ "}";
+
+ $this->fileSystem->expects($this->once())
+ ->method('file_put_contents')
+ ->with($this->equalTo($this->configPath),
+ $this->equalTo($json));
+
+ $this->config->read($this->configPath, true);
+ }
+
+} \ No newline at end of file
diff --git a/utility/config.php b/utility/config.php
new file mode 100644
index 000000000..e00ad940f
--- /dev/null
+++ b/utility/config.php
@@ -0,0 +1,147 @@
+<?php
+
+/**
+* ownCloud - News
+*
+* @author Alessandro Cosentino
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+*
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+
+namespace OCA\News\Utility;
+
+use \OCA\AppFramework\Core\API;
+
+
+class Config {
+
+ private $fileSystem;
+ private $autoPurgeMinimumInterval; // seconds, used to define how
+ // long deleted folders and feeds
+ // should still be kept for an
+ // undo actions
+ private $autoPurgeCount; // number of allowed unread articles per feed
+ private $simplePieCacheDuration; // seconds
+ private $feedFetcherTimeout; // seconds
+ private $useCronUpdates; // turn off updates run by owncloud cronjob
+ private $api;
+
+
+ public function __construct($fileSystem, API $api) {
+ $this->fileSystem = $fileSystem;
+ $this->autoPurgeMinimumInterval = 60;
+ $this->autoPurgeCount = 200;
+ $this->simplePieCacheDuration = 30*60;
+ $this->feedFetcherTimeout = 60;
+ $this->useCronUpdates = true;
+ $this->api = $api;
+ }
+
+
+ public function getAutoPurgeMinimumInterval() {
+ return $this->autoPurgeMinimumInterval;
+ }
+
+
+ public function getAutoPurgeCount() {
+ return $this->autoPurgeCount;
+ }
+
+
+ public function getSimplePieCacheDuration() {
+ return $this->simplePieCacheDuration;
+ }
+
+
+ public function getFeedFetcherTimeout() {
+ return $this->feedFetcherTimeout;
+ }
+
+
+ public function getUseCronUpdates() {
+ return $this->useCronUpdates;
+ }
+
+
+ public function setAutoPurgeMinimumInterval($value) {
+ $this->autoPurgeMinimumInterval = $value;
+ }
+
+
+ public function setAutoPurgeCount($value) {
+ $this->autoPurgeCount = $value;
+ }
+
+
+ public function setSimplePieCacheDuration($value) {
+ $this->simplePieCacheDuration = $value;
+ }
+
+
+ public function setFeedFetcherTimeout($value) {
+ $this->feedFetcherTimeout = $value;
+ }
+
+
+ public function setUseCronUpdates($value) {
+ $this->useCronUpdates = $value;
+ }
+
+
+ public function read($configPath, $createIfNotExists=false) {
+ if($createIfNotExists && !$this->fileSystem->file_exists($configPath)) {
+
+ $this->write($configPath);
+
+ } else {
+
+ $content = $this->fileSystem->file_get_contents($configPath);
+ $configValues = json_decode($content, true);
+
+ if($configValues === null) {
+ $this->api->log('Configuration contains invalid JSON' , 'warn');
+ } else {
+
+ foreach($configValues as $key => $value) {
+ if(property_exists($this, $key)) {
+ $this->$key = $value;
+ } else {
+ $this->api->log('Configuration value "' . $key .
+ '" does not exist. Ignored value.' , 'warn');
+ }
+ }
+
+ }
+ }
+ }
+
+
+ public function write($configPath) {
+ $json = json_encode(array(
+ "autoPurgeMinimumInterval" => $this->autoPurgeMinimumInterval,
+ "autoPurgeCount" => $this->autoPurgeCount,
+ "simplePieCacheDuration" => $this->simplePieCacheDuration,
+ "feedFetcherTimeout" => $this->feedFetcherTimeout,
+ "useCronUpdates" => $this->useCronUpdates,
+ ), JSON_PRETTY_PRINT);
+
+ $this->fileSystem->file_put_contents($configPath, $json);
+ }
+
+
+} \ No newline at end of file