aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/poche/Database.class.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/poche/Database.class.php')
-rwxr-xr-xinc/poche/Database.class.php182
1 files changed, 148 insertions, 34 deletions
diff --git a/inc/poche/Database.class.php b/inc/poche/Database.class.php
index c998fe14..11cccb72 100755
--- a/inc/poche/Database.class.php
+++ b/inc/poche/Database.class.php
@@ -18,7 +18,7 @@ class Database {
18 'default' => 'ORDER BY entries.id' 18 'default' => 'ORDER BY entries.id'
19 ); 19 );
20 20
21 function __construct() 21 function __construct()
22 { 22 {
23 switch (STORAGE) { 23 switch (STORAGE) {
24 case 'sqlite': 24 case 'sqlite':
@@ -27,12 +27,14 @@ class Database {
27 break; 27 break;
28 case 'mysql': 28 case 'mysql':
29 $db_path = 'mysql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB; 29 $db_path = 'mysql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB;
30 $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); 30 $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD);
31 break; 31 break;
32 case 'postgres': 32 case 'postgres':
33 $db_path = 'pgsql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB; 33 $db_path = 'pgsql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB;
34 $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); 34 $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD);
35 break; 35 break;
36 default:
37 die(STORAGE . ' is not a recognised database system !');
36 } 38 }
37 39
38 $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 40 $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
@@ -51,7 +53,7 @@ class Database {
51 } 53 }
52 $hasAdmin = count($query->fetchAll()); 54 $hasAdmin = count($query->fetchAll());
53 55
54 if ($hasAdmin == 0) 56 if ($hasAdmin == 0)
55 return false; 57 return false;
56 58
57 return true; 59 return true;
@@ -77,7 +79,7 @@ class Database {
77 } 79 }
78 else { 80 else {
79 $sql = ' 81 $sql = '
80 CREATE TABLE tags ( 82 CREATE TABLE IF NOT EXISTS tags (
81 id bigserial primary key, 83 id bigserial primary key,
82 value varchar(255) NOT NULL 84 value varchar(255) NOT NULL
83 ); 85 );
@@ -110,7 +112,7 @@ class Database {
110 } 112 }
111 else { 113 else {
112 $sql = ' 114 $sql = '
113 CREATE TABLE tags_entries ( 115 CREATE TABLE IF NOT EXISTS tags_entries (
114 id bigserial primary key, 116 id bigserial primary key,
115 entry_id integer NOT NULL, 117 entry_id integer NOT NULL,
116 tag_id integer NOT NULL 118 tag_id integer NOT NULL
@@ -140,7 +142,7 @@ class Database {
140 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; 142 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
141 $params = array($id_user, 'language', LANG); 143 $params = array($id_user, 'language', LANG);
142 $query = $this->executeQuery($sql, $params); 144 $query = $this->executeQuery($sql, $params);
143 145
144 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; 146 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
145 $params = array($id_user, 'theme', DEFAULT_THEME); 147 $params = array($id_user, 'theme', DEFAULT_THEME);
146 $query = $this->executeQuery($sql, $params); 148 $query = $this->executeQuery($sql, $params);
@@ -153,7 +155,7 @@ class Database {
153 $query = $this->executeQuery($sql, array($id)); 155 $query = $this->executeQuery($sql, array($id));
154 $result = $query->fetchAll(); 156 $result = $query->fetchAll();
155 $user_config = array(); 157 $user_config = array();
156 158
157 foreach ($result as $key => $value) { 159 foreach ($result as $key => $value) {
158 $user_config[$value['name']] = $value['value']; 160 $user_config[$value['name']] = $value['value'];
159 } 161 }
@@ -201,10 +203,10 @@ class Database {
201 $params_update = array($password, $userId); 203 $params_update = array($password, $userId);
202 $query = $this->executeQuery($sql_update, $params_update); 204 $query = $this->executeQuery($sql_update, $params_update);
203 } 205 }
204 206
205 public function updateUserConfig($userId, $key, $value) { 207 public function updateUserConfig($userId, $key, $value) {
206 $config = $this->getConfigUser($userId); 208 $config = $this->getConfigUser($userId);
207 209
208 if (! isset($config[$key])) { 210 if (! isset($config[$key])) {
209 $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)"; 211 $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)";
210 } 212 }
@@ -229,6 +231,73 @@ class Database {
229 return FALSE; 231 return FALSE;
230 } 232 }
231 } 233 }
234
235 public function listUsers($username=null) {
236 $sql = 'SELECT count(*) FROM users'.( $username ? ' WHERE username=?' : '');
237 $query = $this->executeQuery($sql, ( $username ? array($username) : array()));
238 list($count) = $query->fetch();
239 return $count;
240 }
241
242 public function getUserPassword($userID) {
243 $sql = "SELECT * FROM users WHERE id=?";
244 $query = $this->executeQuery($sql, array($userID));
245 $password = $query->fetchAll();
246 return isset($password[0]['password']) ? $password[0]['password'] : null;
247 }
248
249 public function deleteUserConfig($userID) {
250 $sql_action = 'DELETE from users_config WHERE user_id=?';
251 $params_action = array($userID);
252 $query = $this->executeQuery($sql_action, $params_action);
253 return $query;
254 }
255
256 public function deleteTagsEntriesAndEntries($userID) {
257 $entries = $this->retrieveAll($userID);
258 foreach($entries as $entryid) {
259 $tags = $this->retrieveTagsByEntry($entryid);
260 foreach($tags as $tag) {
261 $this->removeTagForEntry($entryid,$tags);
262 }
263 $this->deleteById($entryid,$userID);
264 }
265 }
266
267 public function deleteUser($userID) {
268 $sql_action = 'DELETE from users WHERE id=?';
269 $params_action = array($userID);
270 $query = $this->executeQuery($sql_action, $params_action);
271 }
272
273 public function updateContentAndTitle($id, $title, $body, $user_id) {
274 $sql_action = 'UPDATE entries SET content = ?, title = ? WHERE id=? AND user_id=?';
275 $params_action = array($body, $title, $id, $user_id);
276 $query = $this->executeQuery($sql_action, $params_action);
277 return $query;
278 }
279
280 public function retrieveUnfetchedEntries($user_id, $limit) {
281
282 $sql_limit = "LIMIT 0,".$limit;
283 if (STORAGE == 'postgres') {
284 $sql_limit = "LIMIT ".$limit." OFFSET 0";
285 }
286
287 $sql = "SELECT * FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE 'Untitled - Import%' AND user_id=? ORDER BY id " . $sql_limit;
288 $query = $this->executeQuery($sql, array($user_id));
289 $entries = $query->fetchAll();
290
291 return $entries;
292 }
293
294 public function retrieveUnfetchedEntriesCount($user_id) {
295 $sql = "SELECT count(*) FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE 'Untitled - Import%' AND user_id=?";
296 $query = $this->executeQuery($sql, array($user_id));
297 list($count) = $query->fetch();
298
299 return $count;
300 }
232 301
233 public function retrieveAll($user_id) { 302 public function retrieveAll($user_id) {
234 $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id"; 303 $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id";
@@ -294,24 +363,24 @@ class Database {
294 return $entries; 363 return $entries;
295 } 364 }
296 365
297 public function getEntriesByViewCount($view, $user_id, $tag_id = 0) { 366 public function getEntriesByViewCount($view, $user_id, $tag_id = 0) {
298 switch ($view) { 367 switch ($view) {
299 case 'archive': 368 case 'archive':
300 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? "; 369 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? ";
301 $params = array($user_id, 1); 370 $params = array($user_id, 1);
302 break; 371 break;
303 case 'fav' : 372 case 'fav' :
304 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_fav=? "; 373 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_fav=? ";
305 $params = array($user_id, 1); 374 $params = array($user_id, 1);
306 break; 375 break;
307 case 'tag' : 376 case 'tag' :
308 $sql = "SELECT count(*) FROM entries 377 $sql = "SELECT count(*) FROM entries
309 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id 378 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
310 WHERE entries.user_id=? AND tags_entries.tag_id = ? "; 379 WHERE entries.user_id=? AND tags_entries.tag_id = ? ";
311 $params = array($user_id, $tag_id); 380 $params = array($user_id, $tag_id);
312 break; 381 break;
313 default: 382 default:
314 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? "; 383 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? ";
315 $params = array($user_id, 0); 384 $params = array($user_id, 0);
316 break; 385 break;
317 } 386 }
@@ -319,7 +388,7 @@ class Database {
319 $query = $this->executeQuery($sql, $params); 388 $query = $this->executeQuery($sql, $params);
320 list($count) = $query->fetch(); 389 list($count) = $query->fetch();
321 390
322 return $count; 391 return $count;
323 } 392 }
324 393
325 public function updateContent($id, $content, $user_id) { 394 public function updateContent($id, $content, $user_id) {
@@ -329,11 +398,25 @@ class Database {
329 return $query; 398 return $query;
330 } 399 }
331 400
332 public function add($url, $title, $content, $user_id) { 401 /**
333 $sql_action = 'INSERT INTO entries ( url, title, content, user_id ) VALUES (?, ?, ?, ?)'; 402 *
334 $params_action = array($url, $title, $content, $user_id); 403 * @param string $url
335 $query = $this->executeQuery($sql_action, $params_action); 404 * @param string $title
336 return $query; 405 * @param string $content
406 * @param integer $user_id
407 * @return integer $id of inserted record
408 */
409 public function add($url, $title, $content, $user_id, $isFavorite=0, $isRead=0) {
410 $sql_action = 'INSERT INTO entries ( url, title, content, user_id, is_fav, is_read ) VALUES (?, ?, ?, ?, ?, ?)';
411 $params_action = array($url, $title, $content, $user_id, $isFavorite, $isRead);
412
413 if ( !$this->executeQuery($sql_action, $params_action) ) {
414 $id = null;
415 }
416 else {
417 $id = intval($this->getLastId( (STORAGE == 'postgres') ? 'entries_id_seq' : '') );
418 }
419 return $id;
337 } 420 }
338 421
339 public function deleteById($id, $user_id) { 422 public function deleteById($id, $user_id) {
@@ -365,12 +448,24 @@ class Database {
365 return $this->getHandle()->lastInsertId($column); 448 return $this->getHandle()->lastInsertId($column);
366 } 449 }
367 450
368 public function retrieveAllTags($user_id) { 451 public function search($term, $user_id, $limit = '') {
369 $sql = "SELECT DISTINCT tags.* FROM tags 452 $search = '%'.$term.'%';
453 $sql_action = "SELECT * FROM entries WHERE user_id=? AND (content LIKE ? OR title LIKE ? OR url LIKE ?) "; //searches in content, title and URL
454 $sql_action .= $this->getEntriesOrder().' ' . $limit;
455 $params_action = array($user_id, $search, $search, $search);
456 $query = $this->executeQuery($sql_action, $params_action);
457 return $query->fetchAll();
458 }
459
460 public function retrieveAllTags($user_id, $term = null) {
461 $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags
370 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id 462 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
371 LEFT JOIN entries ON tags_entries.entry_id=entries.id 463 LEFT JOIN entries ON tags_entries.entry_id=entries.id
372 WHERE entries.user_id=?"; 464 WHERE entries.user_id=?
373 $query = $this->executeQuery($sql, array($user_id)); 465 ". (($term) ? "AND lower(tags.value) LIKE ?" : '') ."
466 GROUP BY tags.id, tags.value
467 ORDER BY tags.value";
468 $query = $this->executeQuery($sql, (($term)? array($user_id, strtolower('%'.$term.'%')) : array($user_id) ));
374 $tags = $query->fetchAll(); 469 $tags = $query->fetchAll();
375 470
376 return $tags; 471 return $tags;
@@ -390,10 +485,10 @@ class Database {
390 } 485 }
391 486
392 public function retrieveEntriesByTag($tag_id, $user_id) { 487 public function retrieveEntriesByTag($tag_id, $user_id) {
393 $sql = 488 $sql =
394 "SELECT entries.* FROM entries 489 "SELECT entries.* FROM entries
395 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id 490 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
396 WHERE tags_entries.tag_id = ? AND entries.user_id=?"; 491 WHERE tags_entries.tag_id = ? AND entries.user_id=? ORDER by entries.id DESC";
397 $query = $this->executeQuery($sql, array($tag_id, $user_id)); 492 $query = $this->executeQuery($sql, array($tag_id, $user_id));
398 $entries = $query->fetchAll(); 493 $entries = $query->fetchAll();
399 494
@@ -401,7 +496,7 @@ class Database {
401 } 496 }
402 497
403 public function retrieveTagsByEntry($entry_id) { 498 public function retrieveTagsByEntry($entry_id) {
404 $sql = 499 $sql =
405 "SELECT tags.* FROM tags 500 "SELECT tags.* FROM tags
406 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id 501 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
407 WHERE tags_entries.entry_id = ?"; 502 WHERE tags_entries.entry_id = ?";
@@ -417,6 +512,25 @@ class Database {
417 $query = $this->executeQuery($sql_action, $params_action); 512 $query = $this->executeQuery($sql_action, $params_action);
418 return $query; 513 return $query;
419 } 514 }
515
516 public function cleanUnusedTag($tag_id) {
517 $sql_action = "SELECT tags.* FROM tags JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
518 $query = $this->executeQuery($sql_action,array($tag_id));
519 $tagstokeep = $query->fetchAll();
520 $sql_action = "SELECT tags.* FROM tags LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
521 $query = $this->executeQuery($sql_action,array($tag_id));
522 $alltags = $query->fetchAll();
523
524 foreach ($alltags as $tag) {
525 if ($tag && !in_array($tag,$tagstokeep)) {
526 $sql_action = "DELETE FROM tags WHERE id=?";
527 $params_action = array($tag[0]);
528 $this->executeQuery($sql_action, $params_action);
529 return true;
530 }
531 }
532
533 }
420 534
421 public function retrieveTagByValue($value) { 535 public function retrieveTagByValue($value) {
422 $tag = NULL; 536 $tag = NULL;