aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornicosomb <nicolas@loeuillet.org>2013-04-19 15:46:04 +0200
committernicosomb <nicolas@loeuillet.org>2013-04-19 15:46:04 +0200
commit14890de35a19b44df6537e601240fe38ff6a9ed9 (patch)
tree7802fdcce30bab91c7f97f73fd0456cc0c5d8bd3
parentff4d8c8c1efca0759330906419cb5f36de86d156 (diff)
downloadwallabag-14890de35a19b44df6537e601240fe38ff6a9ed9.tar.gz
wallabag-14890de35a19b44df6537e601240fe38ff6a9ed9.tar.zst
wallabag-14890de35a19b44df6537e601240fe38ff6a9ed9.zip
création d'une classe store() pour gérer tout type de stockage : sqlite, file, mysql, etc.
-rw-r--r--inc/config.php10
-rw-r--r--inc/functions.php144
-rw-r--r--inc/store/file.class.php51
-rw-r--r--inc/store/sqlite.class.php136
-rw-r--r--inc/store/store.class.php51
5 files changed, 262 insertions, 130 deletions
diff --git a/inc/config.php b/inc/config.php
index c63b07b9..cf3529cb 100644
--- a/inc/config.php
+++ b/inc/config.php
@@ -8,26 +8,28 @@
8 * @license http://www.wtfpl.net/ see COPYING file 8 * @license http://www.wtfpl.net/ see COPYING file
9 */ 9 */
10 10
11define ('POCHE_VERSION', '0.11'); 11define ('POCHE_VERSION', '0.2');
12 12
13if (!is_dir('db/')) { 13if (!is_dir('db/')) {
14 @mkdir('db/',0705); 14 @mkdir('db/',0705);
15} 15}
16 16
17define ('DB_PATH', 'sqlite:./db/poche.sqlite');
18define ('ABS_PATH', 'assets/'); 17define ('ABS_PATH', 'assets/');
19define ('CONVERT_LINKS_FOOTNOTES', TRUE); 18define ('CONVERT_LINKS_FOOTNOTES', TRUE);
20define ('DOWNLOAD_PICTURES', TRUE); 19define ('DOWNLOAD_PICTURES', TRUE);
20$storage_type = 'sqlite'; # sqlite or file
21 21
22include 'db.php';
23include 'functions.php'; 22include 'functions.php';
24require_once 'Readability.php'; 23require_once 'Readability.php';
25require_once 'Encoding.php'; 24require_once 'Encoding.php';
26require_once 'rain.tpl.class.php'; 25require_once 'rain.tpl.class.php';
27require_once 'MyTool.class.php'; 26require_once 'MyTool.class.php';
28require_once 'Session.class.php'; 27require_once 'Session.class.php';
28require_once 'store/store.class.php';
29require_once 'store/sqlite.class.php';
30require_once 'store/file.class.php';
29 31
30$db = new db(DB_PATH); 32$store = new $storage_type();
31 33
32# initialisation de RainTPL 34# initialisation de RainTPL
33raintpl::$tpl_dir = './tpl/'; 35raintpl::$tpl_dir = './tpl/';
diff --git a/inc/functions.php b/inc/functions.php
index ef1fc0e2..df7e9b17 100644
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -228,20 +228,20 @@ function remove_directory($directory)
228 228
229function display_view($view, $id = 0, $full_head = 'yes') 229function display_view($view, $id = 0, $full_head = 'yes')
230{ 230{
231 global $tpl; 231 global $tpl, $store;
232 232
233 switch ($view) 233 switch ($view)
234 { 234 {
235 case 'view': 235 case 'view':
236 $entry = get_article($id); 236 $entry = $store->retrieveOneById($id);
237 237
238 if ($entry != NULL) { 238 if ($entry != NULL) {
239 $tpl->assign('id', $entry[0]['id']); 239 $tpl->assign('id', $entry['id']);
240 $tpl->assign('url', $entry[0]['url']); 240 $tpl->assign('url', $entry['url']);
241 $tpl->assign('title', $entry[0]['title']); 241 $tpl->assign('title', $entry['title']);
242 $tpl->assign('content', $entry[0]['content']); 242 $tpl->assign('content', $entry['content']);
243 $tpl->assign('is_fav', $entry[0]['is_fav']); 243 $tpl->assign('is_fav', $entry['is_fav']);
244 $tpl->assign('is_read', $entry[0]['is_read']); 244 $tpl->assign('is_read', $entry['is_read']);
245 $tpl->assign('load_all_js', 0); 245 $tpl->assign('load_all_js', 0);
246 $tpl->draw('view'); 246 $tpl->draw('view');
247 } 247 }
@@ -252,7 +252,7 @@ function display_view($view, $id = 0, $full_head = 'yes')
252 logm('view link #' . $id); 252 logm('view link #' . $id);
253 break; 253 break;
254 default: # home view 254 default: # home view
255 $entries = get_entries($view); 255 $entries = $store->getEntriesByView($view);
256 256
257 $tpl->assign('entries', $entries); 257 $tpl->assign('entries', $entries);
258 258
@@ -277,7 +277,7 @@ function display_view($view, $id = 0, $full_head = 'yes')
277 */ 277 */
278function action_to_do($action, $url, $id = 0) 278function action_to_do($action, $url, $id = 0)
279{ 279{
280 global $db; 280 global $store;
281 281
282 switch ($action) 282 switch ($action)
283 { 283 {
@@ -286,139 +286,31 @@ function action_to_do($action, $url, $id = 0)
286 continue; 286 continue;
287 287
288 if($parametres_url = prepare_url($url)) { 288 if($parametres_url = prepare_url($url)) {
289 $sql_action = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)'; 289 $store->add($url, $parametres_url['title'], $parametres_url['content']);
290 $params_action = array($url, $parametres_url['title'], $parametres_url['content']); 290 $last_id = $store->getLastId();
291 if (DOWNLOAD_PICTURES) {
292 $content = filtre_picture($parametres_url['content'], $url, $last_id);
293 }
291 } 294 }
292 295
293 logm('add link ' . $url); 296 logm('add link ' . $url);
294 break; 297 break;
295 case 'delete': 298 case 'delete':
296 remove_directory(ABS_PATH . $id); 299 remove_directory(ABS_PATH . $id);
297 $sql_action = "DELETE FROM entries WHERE id=?"; 300 $store->deleteById($id);
298 $params_action = array($id);
299 logm('delete link #' . $id); 301 logm('delete link #' . $id);
300 break; 302 break;
301 case 'toggle_fav' : 303 case 'toggle_fav' :
302 $sql_action = "UPDATE entries SET is_fav=~is_fav WHERE id=?"; 304 $store->favoriteById($id);
303 $params_action = array($id);
304 logm('mark as favorite link #' . $id); 305 logm('mark as favorite link #' . $id);
305 break; 306 break;
306 case 'toggle_archive' : 307 case 'toggle_archive' :
307 $sql_action = "UPDATE entries SET is_read=~is_read WHERE id=?"; 308 $store->archiveById($id);
308 $params_action = array($id);
309 logm('archive link #' . $id); 309 logm('archive link #' . $id);
310 break; 310 break;
311 default: 311 default:
312 break; 312 break;
313 } 313 }
314
315 try
316 {
317 # action query
318 if (isset($sql_action))
319 {
320 $query = $db->getHandle()->prepare($sql_action);
321 $query->execute($params_action);
322 # if we add a link, we have to download pictures
323 if ($action == 'add') {
324 $last_id = $db->getHandle()->lastInsertId();
325 if (DOWNLOAD_PICTURES) {
326 $content = filtre_picture($parametres_url['content'], $url, $last_id);
327 $sql_update = "UPDATE entries SET content=? WHERE id=?";
328 $params_update = array($content, $last_id);
329 $query_update = $db->getHandle()->prepare($sql_update);
330 $query_update->execute($params_update);
331 }
332 }
333 }
334 }
335 catch (Exception $e)
336 {
337 logm('action query error : '.$e->getMessage());
338 }
339}
340
341/**
342 * Détermine quels liens afficher : home, fav ou archives
343 */
344function get_entries($view)
345{
346 global $db;
347
348 switch ($_SESSION['sort'])
349 {
350 case 'ia':
351 $order = 'ORDER BY id';
352 break;
353 case 'id':
354 $order = 'ORDER BY id DESC';
355 break;
356 case 'ta':
357 $order = 'ORDER BY lower(title)';
358 break;
359 case 'td':
360 $order = 'ORDER BY lower(title) DESC';
361 break;
362 default:
363 $order = 'ORDER BY id';
364 break;
365 }
366
367 switch ($view)
368 {
369 case 'archive':
370 $sql = "SELECT * FROM entries WHERE is_read=? " . $order;
371 $params = array(-1);
372 break;
373 case 'fav' :
374 $sql = "SELECT * FROM entries WHERE is_fav=? " . $order;
375 $params = array(-1);
376 break;
377 default:
378 $sql = "SELECT * FROM entries WHERE is_read=? " . $order;
379 $params = array(0);
380 break;
381 }
382
383 # view query
384 try
385 {
386 $query = $db->getHandle()->prepare($sql);
387 $query->execute($params);
388 $entries = $query->fetchAll();
389 }
390 catch (Exception $e)
391 {
392 logm('view query error : '.$e->getMessage());
393 }
394
395 return $entries;
396}
397
398/**
399 * Récupère un article en fonction d'un ID
400 */
401function get_article($id)
402{
403 global $db;
404
405 $entry = NULL;
406 $sql = "SELECT * FROM entries WHERE id=?";
407 $params = array(intval($id));
408
409 # view article query
410 try
411 {
412 $query = $db->getHandle()->prepare($sql);
413 $query->execute($params);
414 $entry = $query->fetchAll();
415 }
416 catch (Exception $e)
417 {
418 logm('get article query error : '.$e->getMessage());
419 }
420
421 return $entry;
422} 314}
423 315
424function logm($message) 316function logm($message)
diff --git a/inc/store/file.class.php b/inc/store/file.class.php
new file mode 100644
index 00000000..ad20937d
--- /dev/null
+++ b/inc/store/file.class.php
@@ -0,0 +1,51 @@
1<?php
2/**
3 * poche, a read it later open source system
4 *
5 * @category poche
6 * @author Nicolas Lœuillet <support@inthepoche.com>
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
10
11class File extends Store {
12 function __construct() {
13
14 }
15
16 public function add() {
17
18 }
19
20 public function retrieveOneById($id) {
21
22 }
23
24 public function retrieveOneByURL($url) {
25
26 }
27
28 public function deleteById($id) {
29
30 }
31
32 public function favoriteById($id) {
33
34 }
35
36 public function archiveById($id) {
37
38 }
39
40 public function getEntriesByView($view) {
41
42 }
43
44 public function getLastId() {
45
46 }
47
48 public function updateContentById($id) {
49
50 }
51}
diff --git a/inc/store/sqlite.class.php b/inc/store/sqlite.class.php
new file mode 100644
index 00000000..51054bc5
--- /dev/null
+++ b/inc/store/sqlite.class.php
@@ -0,0 +1,136 @@
1<?php
2/**
3 * poche, a read it later open source system
4 *
5 * @category poche
6 * @author Nicolas Lœuillet <support@inthepoche.com>
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
10
11class Sqlite extends Store {
12
13 public static $db_path = 'sqlite:./db/poche.sqlite';
14 var $handle;
15
16 function __construct() {
17 parent::__construct();
18
19 $this->handle = new PDO(self::$db_path);
20 $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)');
21 $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
22 }
23
24 private function getHandle() {
25 return $this->handle;
26 }
27
28 private function executeQuery($sql, $params) {
29 try
30 {
31 $query = $this->getHandle()->prepare($sql);
32 $query->execute($params);
33 return $query;
34 }
35 catch (Exception $e)
36 {
37 logm('execute query error : '.$e->getMessage());
38 }
39 }
40
41 public function retrieveOneById($id) {
42 parent::__construct();
43
44 $entry = NULL;
45 $sql = "SELECT * FROM entries WHERE id=?";
46 $params = array(intval($id));
47 $query = $this->executeQuery($sql, $params);
48 $entry = $query->fetchAll();
49
50 return $entry[0];
51 }
52
53 public function getEntriesByView($view) {
54 parent::__construct();
55
56 switch ($_SESSION['sort'])
57 {
58 case 'ia':
59 $order = 'ORDER BY id';
60 break;
61 case 'id':
62 $order = 'ORDER BY id DESC';
63 break;
64 case 'ta':
65 $order = 'ORDER BY lower(title)';
66 break;
67 case 'td':
68 $order = 'ORDER BY lower(title) DESC';
69 break;
70 default:
71 $order = 'ORDER BY id';
72 break;
73 }
74
75 switch ($view)
76 {
77 case 'archive':
78 $sql = "SELECT * FROM entries WHERE is_read=? " . $order;
79 $params = array(-1);
80 break;
81 case 'fav' :
82 $sql = "SELECT * FROM entries WHERE is_fav=? " . $order;
83 $params = array(-1);
84 break;
85 default:
86 $sql = "SELECT * FROM entries WHERE is_read=? " . $order;
87 $params = array(0);
88 break;
89 }
90
91 $query = $this->executeQuery($sql, $params);
92 $entries = $query->fetchAll();
93
94 return $entries;
95 }
96
97 public function add() {
98 parent::__construct();
99 $sql_action = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)';
100 $params_action = array($url, $parametres_url['title'], $parametres_url['content']);
101 $query = $this->executeQuery($sql_action, $params_action);
102 }
103
104 public function deleteById($id) {
105 parent::__construct();
106 $sql_action = "DELETE FROM entries WHERE id=?";
107 $params_action = array($id);
108 $query = $this->executeQuery($sql_action, $params_action);
109 }
110
111 public function favoriteById($id) {
112 parent::__construct();
113 $sql_action = "UPDATE entries SET is_fav=~is_fav WHERE id=?";
114 $params_action = array($id);
115 $query = $this->executeQuery($sql_action, $params_action);
116 }
117
118 public function archiveById($id) {
119 parent::__construct();
120 $sql_action = "UPDATE entries SET is_read=~is_read WHERE id=?";
121 $params_action = array($id);
122 $query = $this->executeQuery($sql_action, $params_action);
123 }
124
125 public function getLastId() {
126 parent::__construct();
127 return $this->getHandle()->lastInsertId();
128 }
129
130 public function updateContentById($id) {
131 parent::__construct();
132 $sql_update = "UPDATE entries SET content=? WHERE id=?";
133 $params_update = array($content, $id);
134 $query = $this->executeQuery($sql_update, $params_update);
135 }
136} \ 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 00000000..ae3cb341
--- /dev/null
+++ b/inc/store/store.class.php
@@ -0,0 +1,51 @@
1<?php
2/**
3 * poche, a read it later open source system
4 *
5 * @category poche
6 * @author Nicolas Lœuillet <support@inthepoche.com>
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
10
11class Store {
12 function __construct() {
13
14 }
15
16 public function add() {
17
18 }
19
20 public function retrieveOneById($id) {
21
22 }
23
24 public function retrieveOneByURL($url) {
25
26 }
27
28 public function deleteById($id) {
29
30 }
31
32 public function favoriteById($id) {
33
34 }
35
36 public function archiveById($id) {
37
38 }
39
40 public function getEntriesByView($view) {
41
42 }
43
44 public function getLastId() {
45
46 }
47
48 public function updateContentById($id) {
49
50 }
51}