From 8241177556c0c4a3b003720666750a9628c5ae62 Mon Sep 17 00:00:00 2001 From: Mathias Kresin Date: Wed, 28 Jan 2015 05:26:36 +0100 Subject: [PATCH] switch config table to key/value store --- fever/index.php | 5 +---- models/config.php | 10 ++++------ models/database.php | 2 +- models/schema.php | 46 ++++++++++++++++++++++++++++++++++++++++++++- models/user.php | 5 ++--- 5 files changed, 53 insertions(+), 15 deletions(-) diff --git a/fever/index.php b/fever/index.php index b43173c..abb2b43 100644 --- a/fever/index.php +++ b/fever/index.php @@ -39,10 +39,7 @@ function auth() } } - $credentials = Database::get('db')->table('config') - ->columns('username', 'fever_token') - ->findOne(); - + $credentials = Database::get('db')->hashtable('settings')->get('username', 'fever_token'); $api_key = md5($credentials['username'].':'.$credentials['fever_token']); $response = array( diff --git a/models/config.php b/models/config.php index bfbe939..46a1f28 100644 --- a/models/config.php +++ b/models/config.php @@ -238,14 +238,14 @@ function new_tokens() 'fever_token' => substr(generate_token(), 0, 8), ); - return Database::get('db')->table('config')->update($values); + return Database::get('db')->hashtable('settings')->put($values); } // Get a config value from the DB or from the session function get($name) { if (! isset($_SESSION)) { - return Database::get('db')->table('config')->findOneColumn($name); + return current(Database::get('db')->hashtable('settings')->get($name)); } else { @@ -264,9 +264,7 @@ function get($name) // Get all config parameters function get_all() { - $config = Database::get('db') - ->table('config') - ->findOne(); + $config = Database::get('db')->hashtable('settings')->get(); unset($config['password']); @@ -322,7 +320,7 @@ function save(array $values) Database::get('db')->table('items')->update(array('content' => '')); } - if (Database::get('db')->table('config')->update($values)) { + if (Database::get('db')->hashtable('settings')->put($values)) { reload(); return true; } diff --git a/models/database.php b/models/database.php index 70ed0a8..a567057 100644 --- a/models/database.php +++ b/models/database.php @@ -26,7 +26,7 @@ function create($filename, $username, $password) 'password' => password_hash($password, PASSWORD_BCRYPT) ); - $db->table('config')->update($credentials); + $db->hashtable('settings')->put($credentials); return true; } diff --git a/models/schema.php b/models/schema.php index 7bc56ec..ea2d372 100644 --- a/models/schema.php +++ b/models/schema.php @@ -5,7 +5,51 @@ namespace Schema; use PDO; use Model\Config; -const VERSION = 34; +const VERSION = 35; + +function version_35($pdo) +{ + $pdo->exec('DELETE FROM favicons WHERE icon = ""'); + + $pdo->exec(' + CREATE TABLE settings ( + "key" TEXT NOT NULL UNIQUE, + "value" TEXT Default NULL, + PRIMARY KEY(key) + ) + '); + + $pdo->exec(" + INSERT INTO settings (key,value) + SELECT 'username', username FROM config UNION + SELECT 'password', password FROM config UNION + SELECT 'language', language FROM config UNION + SELECT 'autoflush', autoflush FROM config UNION + SELECT 'nocontent', nocontent FROM config UNION + SELECT 'items_per_page', items_per_page FROM config UNION + SELECT 'theme', theme FROM config UNION + SELECT 'api_token', api_token FROM config UNION + SELECT 'feed_token', feed_token FROM config UNION + SELECT 'items_sorting_direction', items_sorting_direction FROM config UNION + SELECT 'redirect_nothing_to_read', redirect_nothing_to_read FROM config UNION + SELECT 'timezone', timezone FROM config UNION + SELECT 'auto_update_url', auto_update_url FROM config UNION + SELECT 'bookmarklet_token', bookmarklet_token FROM config UNION + SELECT 'items_display_mode', items_display_mode FROM config UNION + SELECT 'fever_token', fever_token FROM config UNION + SELECT 'autoflush_unread', autoflush_unread FROM config UNION + SELECT 'pinboard_enabled', pinboard_enabled FROM config UNION + SELECT 'pinboard_token', pinboard_token FROM config UNION + SELECT 'pinboard_tags', pinboard_tags FROM config UNION + SELECT 'instapaper_enabled', instapaper_enabled FROM config UNION + SELECT 'instapaper_username', instapaper_username FROM config UNION + SELECT 'instapaper_password', instapaper_password FROM config UNION + SELECT 'image_proxy', image_proxy FROM config UNION + SELECT 'favicons', favicons FROM config + "); + + $pdo->exec('DROP TABLE config'); +} function version_34($pdo) { diff --git a/models/user.php b/models/user.php index 2ae28e0..21b44e9 100644 --- a/models/user.php +++ b/models/user.php @@ -27,9 +27,8 @@ function logout() function getCredentials() { return Database::get('db') - ->table('config') - ->columns('username', 'password') - ->findOne(); + ->hashtable('settings') + ->get('username', 'password'); } // Validate authentication