From 14890de35a19b44df6537e601240fe38ff6a9ed9 Mon Sep 17 00:00:00 2001 From: nicosomb Date: Fri, 19 Apr 2013 15:46:04 +0200 Subject: [PATCH] =?UTF-8?q?cr=C3=A9ation=20d'une=20classe=20store()=20pour?= =?UTF-8?q?=20g=C3=A9rer=20tout=20type=20de=20stockage=20:=20sqlite,=20fil?= =?UTF-8?q?e,=20mysql,=20etc.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/config.php | 10 +-- inc/functions.php | 144 +++++-------------------------------- inc/store/file.class.php | 51 +++++++++++++ inc/store/sqlite.class.php | 136 +++++++++++++++++++++++++++++++++++ inc/store/store.class.php | 51 +++++++++++++ 5 files changed, 262 insertions(+), 130 deletions(-) create mode 100644 inc/store/file.class.php create mode 100644 inc/store/sqlite.class.php create mode 100644 inc/store/store.class.php diff --git a/inc/config.php b/inc/config.php index c63b07b..cf3529c 100644 --- a/inc/config.php +++ b/inc/config.php @@ -8,26 +8,28 @@ * @license http://www.wtfpl.net/ see COPYING file */ -define ('POCHE_VERSION', '0.11'); +define ('POCHE_VERSION', '0.2'); if (!is_dir('db/')) { @mkdir('db/',0705); } -define ('DB_PATH', 'sqlite:./db/poche.sqlite'); define ('ABS_PATH', 'assets/'); define ('CONVERT_LINKS_FOOTNOTES', TRUE); define ('DOWNLOAD_PICTURES', TRUE); +$storage_type = 'sqlite'; # sqlite or file -include 'db.php'; include 'functions.php'; require_once 'Readability.php'; require_once 'Encoding.php'; require_once 'rain.tpl.class.php'; require_once 'MyTool.class.php'; require_once 'Session.class.php'; +require_once 'store/store.class.php'; +require_once 'store/sqlite.class.php'; +require_once 'store/file.class.php'; -$db = new db(DB_PATH); +$store = new $storage_type(); # initialisation de RainTPL raintpl::$tpl_dir = './tpl/'; diff --git a/inc/functions.php b/inc/functions.php index ef1fc0e..df7e9b1 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -228,20 +228,20 @@ function remove_directory($directory) function display_view($view, $id = 0, $full_head = 'yes') { - global $tpl; + global $tpl, $store; switch ($view) { case 'view': - $entry = get_article($id); + $entry = $store->retrieveOneById($id); if ($entry != NULL) { - $tpl->assign('id', $entry[0]['id']); - $tpl->assign('url', $entry[0]['url']); - $tpl->assign('title', $entry[0]['title']); - $tpl->assign('content', $entry[0]['content']); - $tpl->assign('is_fav', $entry[0]['is_fav']); - $tpl->assign('is_read', $entry[0]['is_read']); + $tpl->assign('id', $entry['id']); + $tpl->assign('url', $entry['url']); + $tpl->assign('title', $entry['title']); + $tpl->assign('content', $entry['content']); + $tpl->assign('is_fav', $entry['is_fav']); + $tpl->assign('is_read', $entry['is_read']); $tpl->assign('load_all_js', 0); $tpl->draw('view'); } @@ -252,7 +252,7 @@ function display_view($view, $id = 0, $full_head = 'yes') logm('view link #' . $id); break; default: # home view - $entries = get_entries($view); + $entries = $store->getEntriesByView($view); $tpl->assign('entries', $entries); @@ -277,7 +277,7 @@ function display_view($view, $id = 0, $full_head = 'yes') */ function action_to_do($action, $url, $id = 0) { - global $db; + global $store; switch ($action) { @@ -286,139 +286,31 @@ function action_to_do($action, $url, $id = 0) continue; if($parametres_url = prepare_url($url)) { - $sql_action = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)'; - $params_action = array($url, $parametres_url['title'], $parametres_url['content']); + $store->add($url, $parametres_url['title'], $parametres_url['content']); + $last_id = $store->getLastId(); + if (DOWNLOAD_PICTURES) { + $content = filtre_picture($parametres_url['content'], $url, $last_id); + } } logm('add link ' . $url); break; case 'delete': remove_directory(ABS_PATH . $id); - $sql_action = "DELETE FROM entries WHERE id=?"; - $params_action = array($id); + $store->deleteById($id); logm('delete link #' . $id); break; case 'toggle_fav' : - $sql_action = "UPDATE entries SET is_fav=~is_fav WHERE id=?"; - $params_action = array($id); + $store->favoriteById($id); logm('mark as favorite link #' . $id); break; case 'toggle_archive' : - $sql_action = "UPDATE entries SET is_read=~is_read WHERE id=?"; - $params_action = array($id); + $store->archiveById($id); logm('archive link #' . $id); break; default: break; } - - try - { - # action query - if (isset($sql_action)) - { - $query = $db->getHandle()->prepare($sql_action); - $query->execute($params_action); - # if we add a link, we have to download pictures - if ($action == 'add') { - $last_id = $db->getHandle()->lastInsertId(); - if (DOWNLOAD_PICTURES) { - $content = filtre_picture($parametres_url['content'], $url, $last_id); - $sql_update = "UPDATE entries SET content=? WHERE id=?"; - $params_update = array($content, $last_id); - $query_update = $db->getHandle()->prepare($sql_update); - $query_update->execute($params_update); - } - } - } - } - catch (Exception $e) - { - logm('action query error : '.$e->getMessage()); - } -} - -/** - * Détermine quels liens afficher : home, fav ou archives - */ -function get_entries($view) -{ - global $db; - - switch ($_SESSION['sort']) - { - case 'ia': - $order = 'ORDER BY id'; - break; - case 'id': - $order = 'ORDER BY id DESC'; - break; - case 'ta': - $order = 'ORDER BY lower(title)'; - break; - case 'td': - $order = 'ORDER BY lower(title) DESC'; - break; - default: - $order = 'ORDER BY id'; - break; - } - - switch ($view) - { - case 'archive': - $sql = "SELECT * FROM entries WHERE is_read=? " . $order; - $params = array(-1); - break; - case 'fav' : - $sql = "SELECT * FROM entries WHERE is_fav=? " . $order; - $params = array(-1); - break; - default: - $sql = "SELECT * FROM entries WHERE is_read=? " . $order; - $params = array(0); - break; - } - - # view query - try - { - $query = $db->getHandle()->prepare($sql); - $query->execute($params); - $entries = $query->fetchAll(); - } - catch (Exception $e) - { - logm('view query error : '.$e->getMessage()); - } - - return $entries; -} - -/** - * Récupère un article en fonction d'un ID - */ -function get_article($id) -{ - global $db; - - $entry = NULL; - $sql = "SELECT * FROM entries WHERE id=?"; - $params = array(intval($id)); - - # view article query - try - { - $query = $db->getHandle()->prepare($sql); - $query->execute($params); - $entry = $query->fetchAll(); - } - catch (Exception $e) - { - logm('get article query error : '.$e->getMessage()); - } - - return $entry; } function logm($message) diff --git a/inc/store/file.class.php b/inc/store/file.class.php new file mode 100644 index 0000000..ad20937 --- /dev/null +++ b/inc/store/file.class.php @@ -0,0 +1,51 @@ + + * @copyright 2013 + * @license http://www.wtfpl.net/ see COPYING file + */ + +class File extends Store { + function __construct() { + + } + + public function add() { + + } + + public function retrieveOneById($id) { + + } + + public function retrieveOneByURL($url) { + + } + + public function deleteById($id) { + + } + + public function favoriteById($id) { + + } + + public function archiveById($id) { + + } + + public function getEntriesByView($view) { + + } + + public function getLastId() { + + } + + public function updateContentById($id) { + + } +} diff --git a/inc/store/sqlite.class.php b/inc/store/sqlite.class.php new file mode 100644 index 0000000..51054bc --- /dev/null +++ b/inc/store/sqlite.class.php @@ -0,0 +1,136 @@ + + * @copyright 2013 + * @license http://www.wtfpl.net/ see COPYING file + */ + +class Sqlite extends Store { + + public static $db_path = 'sqlite:./db/poche.sqlite'; + var $handle; + + function __construct() { + parent::__construct(); + + $this->handle = new PDO(self::$db_path); + $this->handle->exec('CREATE TABLE IF NOT EXISTS "entries" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , "title" VARCHAR, "url" VARCHAR UNIQUE , "is_read" INTEGER DEFAULT 0, "is_fav" INTEGER DEFAULT 0, "content" BLOB)'); + $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } + + private function getHandle() { + return $this->handle; + } + + private function executeQuery($sql, $params) { + try + { + $query = $this->getHandle()->prepare($sql); + $query->execute($params); + return $query; + } + catch (Exception $e) + { + logm('execute query error : '.$e->getMessage()); + } + } + + public function retrieveOneById($id) { + parent::__construct(); + + $entry = NULL; + $sql = "SELECT * FROM entries WHERE id=?"; + $params = array(intval($id)); + $query = $this->executeQuery($sql, $params); + $entry = $query->fetchAll(); + + return $entry[0]; + } + + public function getEntriesByView($view) { + parent::__construct(); + + switch ($_SESSION['sort']) + { + case 'ia': + $order = 'ORDER BY id'; + break; + case 'id': + $order = 'ORDER BY id DESC'; + break; + case 'ta': + $order = 'ORDER BY lower(title)'; + break; + case 'td': + $order = 'ORDER BY lower(title) DESC'; + break; + default: + $order = 'ORDER BY id'; + break; + } + + switch ($view) + { + case 'archive': + $sql = "SELECT * FROM entries WHERE is_read=? " . $order; + $params = array(-1); + break; + case 'fav' : + $sql = "SELECT * FROM entries WHERE is_fav=? " . $order; + $params = array(-1); + break; + default: + $sql = "SELECT * FROM entries WHERE is_read=? " . $order; + $params = array(0); + break; + } + + $query = $this->executeQuery($sql, $params); + $entries = $query->fetchAll(); + + return $entries; + } + + public function add() { + parent::__construct(); + $sql_action = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)'; + $params_action = array($url, $parametres_url['title'], $parametres_url['content']); + $query = $this->executeQuery($sql_action, $params_action); + } + + public function deleteById($id) { + parent::__construct(); + $sql_action = "DELETE FROM entries WHERE id=?"; + $params_action = array($id); + $query = $this->executeQuery($sql_action, $params_action); + } + + public function favoriteById($id) { + parent::__construct(); + $sql_action = "UPDATE entries SET is_fav=~is_fav WHERE id=?"; + $params_action = array($id); + $query = $this->executeQuery($sql_action, $params_action); + } + + public function archiveById($id) { + parent::__construct(); + $sql_action = "UPDATE entries SET is_read=~is_read WHERE id=?"; + $params_action = array($id); + $query = $this->executeQuery($sql_action, $params_action); + } + + public function getLastId() { + parent::__construct(); + return $this->getHandle()->lastInsertId(); + } + + public function updateContentById($id) { + parent::__construct(); + $sql_update = "UPDATE entries SET content=? WHERE id=?"; + $params_update = array($content, $id); + $query = $this->executeQuery($sql_update, $params_update); + } +} \ No newline at end of file diff --git a/inc/store/store.class.php b/inc/store/store.class.php new file mode 100644 index 0000000..ae3cb34 --- /dev/null +++ b/inc/store/store.class.php @@ -0,0 +1,51 @@ + + * @copyright 2013 + * @license http://www.wtfpl.net/ see COPYING file + */ + +class Store { + function __construct() { + + } + + public function add() { + + } + + public function retrieveOneById($id) { + + } + + public function retrieveOneByURL($url) { + + } + + public function deleteById($id) { + + } + + public function favoriteById($id) { + + } + + public function archiveById($id) { + + } + + public function getEntriesByView($view) { + + } + + public function getLastId() { + + } + + public function updateContentById($id) { + + } +}