création d'une classe store() pour gérer tout type de stockage : sqlite, file, mysql, etc.

This commit is contained in:
nicosomb 2013-04-19 15:46:04 +02:00
parent ff4d8c8c1e
commit 14890de35a
5 changed files with 262 additions and 130 deletions

View file

@ -8,26 +8,28 @@
* @license http://www.wtfpl.net/ see COPYING file * @license http://www.wtfpl.net/ see COPYING file
*/ */
define ('POCHE_VERSION', '0.11'); define ('POCHE_VERSION', '0.2');
if (!is_dir('db/')) { if (!is_dir('db/')) {
@mkdir('db/',0705); @mkdir('db/',0705);
} }
define ('DB_PATH', 'sqlite:./db/poche.sqlite');
define ('ABS_PATH', 'assets/'); define ('ABS_PATH', 'assets/');
define ('CONVERT_LINKS_FOOTNOTES', TRUE); define ('CONVERT_LINKS_FOOTNOTES', TRUE);
define ('DOWNLOAD_PICTURES', TRUE); define ('DOWNLOAD_PICTURES', TRUE);
$storage_type = 'sqlite'; # sqlite or file
include 'db.php';
include 'functions.php'; include 'functions.php';
require_once 'Readability.php'; require_once 'Readability.php';
require_once 'Encoding.php'; require_once 'Encoding.php';
require_once 'rain.tpl.class.php'; require_once 'rain.tpl.class.php';
require_once 'MyTool.class.php'; require_once 'MyTool.class.php';
require_once 'Session.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 # initialisation de RainTPL
raintpl::$tpl_dir = './tpl/'; raintpl::$tpl_dir = './tpl/';

View file

@ -228,20 +228,20 @@ function remove_directory($directory)
function display_view($view, $id = 0, $full_head = 'yes') function display_view($view, $id = 0, $full_head = 'yes')
{ {
global $tpl; global $tpl, $store;
switch ($view) switch ($view)
{ {
case 'view': case 'view':
$entry = get_article($id); $entry = $store->retrieveOneById($id);
if ($entry != NULL) { if ($entry != NULL) {
$tpl->assign('id', $entry[0]['id']); $tpl->assign('id', $entry['id']);
$tpl->assign('url', $entry[0]['url']); $tpl->assign('url', $entry['url']);
$tpl->assign('title', $entry[0]['title']); $tpl->assign('title', $entry['title']);
$tpl->assign('content', $entry[0]['content']); $tpl->assign('content', $entry['content']);
$tpl->assign('is_fav', $entry[0]['is_fav']); $tpl->assign('is_fav', $entry['is_fav']);
$tpl->assign('is_read', $entry[0]['is_read']); $tpl->assign('is_read', $entry['is_read']);
$tpl->assign('load_all_js', 0); $tpl->assign('load_all_js', 0);
$tpl->draw('view'); $tpl->draw('view');
} }
@ -252,7 +252,7 @@ function display_view($view, $id = 0, $full_head = 'yes')
logm('view link #' . $id); logm('view link #' . $id);
break; break;
default: # home view default: # home view
$entries = get_entries($view); $entries = $store->getEntriesByView($view);
$tpl->assign('entries', $entries); $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) function action_to_do($action, $url, $id = 0)
{ {
global $db; global $store;
switch ($action) switch ($action)
{ {
@ -286,139 +286,31 @@ function action_to_do($action, $url, $id = 0)
continue; continue;
if($parametres_url = prepare_url($url)) { if($parametres_url = prepare_url($url)) {
$sql_action = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)'; $store->add($url, $parametres_url['title'], $parametres_url['content']);
$params_action = array($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); logm('add link ' . $url);
break; break;
case 'delete': case 'delete':
remove_directory(ABS_PATH . $id); remove_directory(ABS_PATH . $id);
$sql_action = "DELETE FROM entries WHERE id=?"; $store->deleteById($id);
$params_action = array($id);
logm('delete link #' . $id); logm('delete link #' . $id);
break; break;
case 'toggle_fav' : case 'toggle_fav' :
$sql_action = "UPDATE entries SET is_fav=~is_fav WHERE id=?"; $store->favoriteById($id);
$params_action = array($id);
logm('mark as favorite link #' . $id); logm('mark as favorite link #' . $id);
break; break;
case 'toggle_archive' : case 'toggle_archive' :
$sql_action = "UPDATE entries SET is_read=~is_read WHERE id=?"; $store->archiveById($id);
$params_action = array($id);
logm('archive link #' . $id); logm('archive link #' . $id);
break; break;
default: default:
break; 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) function logm($message)

51
inc/store/file.class.php Normal file
View file

@ -0,0 +1,51 @@
<?php
/**
* poche, a read it later open source system
*
* @category poche
* @author Nicolas Lœuillet <support@inthepoche.com>
* @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) {
}
}

136
inc/store/sqlite.class.php Normal file
View file

@ -0,0 +1,136 @@
<?php
/**
* poche, a read it later open source system
*
* @category poche
* @author Nicolas Lœuillet <support@inthepoche.com>
* @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);
}
}

51
inc/store/store.class.php Normal file
View file

@ -0,0 +1,51 @@
<?php
/**
* poche, a read it later open source system
*
* @category poche
* @author Nicolas Lœuillet <support@inthepoche.com>
* @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) {
}
}