]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Database.class.php
3 * wallabag, self hostable application allowing you to not miss any content anymore
6 * @author Nicolas LÅ“uillet <nicolas@loeuillet.org>
8 * @license http://opensource.org/licenses/MIT see COPYING file
14 private $order = array (
15 'ia' => 'ORDER BY entries.id',
16 'id' => 'ORDER BY entries.id DESC',
17 'ta' => 'ORDER BY lower(entries.title)',
18 'td' => 'ORDER BY lower(entries.title) DESC',
19 'default' => 'ORDER BY entries.id'
22 function __construct()
26 // Check if /db is writeable
27 if ( !is_writable(STORAGE_SQLITE
) || !is_writable(dirname(STORAGE_SQLITE
))) {
28 die('An error occured: "db" directory must be writeable for your web server user!');
30 $db_path = 'sqlite:' . STORAGE_SQLITE
;
31 $this->handle
= new PDO($db_path);
34 if (MYSQL_USE_UTF8MB4
) {
35 $db_path = 'mysql:host=' . STORAGE_SERVER
. ';dbname=' . STORAGE_DB
. ';charset=utf8mb4';
36 $this->handle
= new PDO($db_path, STORAGE_USER
, STORAGE_PASSWORD
, array(
37 PDO
::MYSQL_ATTR_INIT_COMMAND
=> 'SET NAMES utf8mb4',
40 $db_path = 'mysql:host=' . STORAGE_SERVER
. ';dbname=' . STORAGE_DB
;
41 $this->handle
= new PDO($db_path, STORAGE_USER
, STORAGE_PASSWORD
);
45 $db_path = 'pgsql:host=' . STORAGE_SERVER
. ';dbname=' . STORAGE_DB
;
46 $this->handle
= new PDO($db_path, STORAGE_USER
, STORAGE_PASSWORD
);
49 die(STORAGE
. ' is not a recognised database system !');
52 $this->handle
->setAttribute(PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_EXCEPTION
);
54 Tools
::logm('storage type ' . STORAGE
);
57 private function getHandle()
62 private function _checkTags()
65 if (STORAGE
== 'sqlite') {
67 CREATE TABLE IF NOT EXISTS tags (
68 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
72 elseif(STORAGE
== 'mysql') {
74 CREATE TABLE IF NOT EXISTS `tags` (
75 `id` int(11) NOT NULL AUTO_INCREMENT,
76 `value` varchar(255) NOT NULL,
78 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
83 CREATE TABLE IF NOT EXISTS tags (
84 id bigserial primary key,
85 value varchar(255) NOT NULL
90 $query = $this->executeQuery($sql, array());
92 if (STORAGE
== 'sqlite') {
94 CREATE TABLE IF NOT EXISTS tags_entries (
95 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
98 FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE,
99 FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE
102 elseif(STORAGE
== 'mysql') {
104 CREATE TABLE IF NOT EXISTS `tags_entries` (
105 `id` int(11) NOT NULL AUTO_INCREMENT,
106 `entry_id` int(11) NOT NULL,
107 `tag_id` int(11) NOT NULL,
108 FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE,
109 FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE,
111 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
116 CREATE TABLE IF NOT EXISTS tags_entries (
117 id bigserial primary key,
118 entry_id integer NOT NULL,
119 tag_id integer NOT NULL
124 $query = $this->executeQuery($sql, array());
127 public function install($login, $password, $email = '')
129 $sql = 'INSERT INTO users ( username, password, name, email) VALUES (?, ?, ?, ?)';
130 $params = array($login, $password, $login, $email);
131 $query = $this->executeQuery($sql, $params);
134 if (STORAGE
== 'postgres') {
135 $sequence = 'users_id_seq';
138 $id_user = intval($this->getLastId($sequence));
140 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
141 $params = array($id_user, 'pager', PAGINATION
);
142 $query = $this->executeQuery($sql, $params);
144 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
145 $params = array($id_user, 'language', LANG
);
146 $query = $this->executeQuery($sql, $params);
148 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
149 $params = array($id_user, 'theme', DEFAULT_THEME
);
150 $query = $this->executeQuery($sql, $params);
155 public function getConfigUser($id)
157 $sql = "SELECT * FROM users_config WHERE user_id = ?";
158 $query = $this->executeQuery($sql, array($id));
159 $result = ($query) ? $query->fetchAll() : false;
160 $user_config = false;
162 $user_config = array();
164 foreach ($result as $key => $value) {
165 $user_config[$value['name']] = $value['value'];
172 public function userExists($username)
174 $sql = "SELECT * FROM users WHERE username=?";
175 $query = $this->executeQuery($sql, array($username));
176 $login = $query->fetchAll();
177 return (isset($login[0]) && $query) ? true : false;
180 public function login($username, $password, $isauthenticated = FALSE)
182 if ($isauthenticated) {
183 $sql = "SELECT * FROM users WHERE username=?";
184 $query = $this->executeQuery($sql, array($username));
186 $sql = "SELECT * FROM users WHERE username=? AND password=?";
187 $query = $this->executeQuery($sql, array($username, $password));
189 $login = ($query) ? $query->fetchAll() : false;
193 $user['id'] = $login[0]['id'];
194 $user['username'] = $login[0]['username'];
195 $user['password'] = $login[0]['password'];
196 $user['name'] = $login[0]['name'];
197 $user['email'] = $login[0]['email'];
198 $user['config'] = $this->getConfigUser($login[0]['id']);
204 public function updatePassword($userId, $password)
206 $sql_update = "UPDATE users SET password=? WHERE id=?";
207 $params_update = array($password, $userId);
208 $query = $this->executeQuery($sql_update, $params_update);
211 public function updateUserConfig($userId, $key, $value)
213 $config = $this->getConfigUser($userId);
215 if (! isset($config[$key])) {
216 $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)";
219 $sql = "UPDATE users_config SET value=? WHERE user_id=? AND name=?";
222 $params = array($value, $userId, $key);
223 $query = $this->executeQuery($sql, $params);
226 private function executeQuery($sql, $params)
230 $query = $this->getHandle()->prepare($sql);
231 $query->execute($params);
236 Tools
::logm('execute query error : '.$e->getMessage());
241 public function listUsers($username = NULL)
243 $sql = 'SELECT count(*) FROM users'.( $username ? ' WHERE username=?' : '');
244 $query = $this->executeQuery($sql, ( $username ? array($username) : array()));
245 list($count) = ($query) ? $query->fetch() : false;
249 public function getUserPassword($userID)
251 $sql = "SELECT * FROM users WHERE id=?";
252 $query = $this->executeQuery($sql, array($userID));
253 $password = $query->fetchAll();
254 return ($query) ? $password[0]['password'] : false;
257 public function deleteUserConfig($userID)
259 $sql_action = 'DELETE from users_config WHERE user_id=?';
260 $params_action = array($userID);
261 $query = $this->executeQuery($sql_action, $params_action);
262 return ($query) ? $query : false;
265 public function deleteTagsEntriesAndEntries($userID)
267 $entries = $this->retrieveAll($userID);
269 foreach($entries as $entry) {
270 $tags = $this->retrieveTagsByEntry($entry['id']);
271 foreach($tags as $tag) {
272 $this->removeTagForEntry($entry['id'], $tags);
274 $this->deleteById($entryid,$userID);
281 public function deleteUser($userID)
283 $sql_action = 'DELETE from users WHERE id=?';
284 $params_action = array($userID);
285 $query = $this->executeQuery($sql_action, $params_action);
288 public function updateContentAndTitle($id, $title, $body, $user_id)
290 $sql_action = 'UPDATE entries SET content = ?, title = ? WHERE id=? AND user_id=?';
291 $params_action = array($body, $title, $id, $user_id);
292 $query = $this->executeQuery($sql_action, $params_action);
296 public function retrieveUnfetchedEntries($user_id, $limit)
299 $sql_limit = "LIMIT 0,".$limit;
300 if (STORAGE
== 'postgres') {
301 $sql_limit = "LIMIT ".$limit." OFFSET 0";
304 $sql = "SELECT * FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE '%Import%' AND user_id=? ORDER BY id " . $sql_limit;
305 $query = $this->executeQuery($sql, array($user_id));
306 $entries = $query->fetchAll();
308 return ($query) ? $entries : false;
311 public function retrieveUnfetchedEntriesCount($user_id)
313 $sql = "SELECT count(*) FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE '%Import%' AND user_id=?";
314 $query = $this->executeQuery($sql, array($user_id));
315 list($count) = $query->fetch();
320 public function retrieveAll($user_id)
322 $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id";
323 $query = $this->executeQuery($sql, array($user_id));
324 $entries = $query->fetchAll();
326 return ($query) ? $entries : false;
329 public function retrieveAllWithTags($user_id)
331 $entries = $this->retrieveAll($user_id);
333 $count = count($entries);
334 for ($i = 0; $i < $count; $i++
) {
335 $tag_entries = $this->retrieveTagsByEntry($entries[$i]['id']);
337 foreach ($tag_entries as $tag) {
340 $entries[$i]['tags'] = implode(',', $tags);
346 public function retrieveOneById($id, $user_id)
348 $sql = "SELECT * FROM entries WHERE id=? AND user_id=?";
349 $params = array(intval($id), $user_id);
350 $query = $this->executeQuery($sql, $params);
351 $entry = $query->fetchAll();
353 return ($query) ? $entry[0] : false;
356 public function retrieveOneByURL($url, $user_id)
358 $sql = "SELECT * FROM entries WHERE url=? AND user_id=?";
359 $params = array($url, $user_id);
360 $query = $this->executeQuery($sql, $params);
361 $entry = $query->fetchAll();
363 return ($query) ? $entry[0] : false;
366 public function reassignTags($old_entry_id, $new_entry_id)
368 $sql = "UPDATE tags_entries SET entry_id=? WHERE entry_id=?";
369 $params = array($new_entry_id, $old_entry_id);
370 $query = $this->executeQuery($sql, $params);
373 public function getEntriesByView($view, $user_id, $limit = '', $tag_id = 0)
377 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? ";
378 $params = array($user_id, 1);
381 $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? ";
382 $params = array($user_id, 1);
385 $sql = "SELECT entries.* FROM entries
386 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
387 WHERE entries.user_id=? AND tags_entries.tag_id = ? ";
388 $params = array($user_id, $tag_id);
391 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? ";
392 $params = array($user_id, 0);
396 $sql .= $this->getEntriesOrder().' ' . $limit;
398 $query = $this->executeQuery($sql, $params);
399 $entries = $query->fetchAll();
401 return ($query) ? $entries : false;
405 public function getEntriesByViewCount($view, $user_id, $tag_id = 0)
409 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? ";
410 $params = array($user_id, 1);
413 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_fav=? ";
414 $params = array($user_id, 1);
417 $sql = "SELECT count(*) FROM entries
418 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
419 WHERE entries.user_id=? AND tags_entries.tag_id = ? ";
420 $params = array($user_id, $tag_id);
423 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? ";
424 $params = array($user_id, 0);
428 $query = $this->executeQuery($sql, $params);
429 list($count) = ($query) ? $query->fetch() : array(false);
433 public function getRandomId($user_id, $view) {
434 $random = (STORAGE
== 'mysql') ? 'RAND()' : 'RANDOM()';
437 $sql = "SELECT id FROM entries WHERE user_id=? AND is_read=? ORDER BY ". $random . " LIMIT 1";
438 $params = array($user_id,1);
441 $sql = "SELECT id FROM entries WHERE user_id=? AND is_fav=? ORDER BY ". $random . " LIMIT 1";
442 $params = array($user_id,1);
445 $sql = "SELECT id FROM entries WHERE user_id=? AND is_read=? ORDER BY ". $random . " LIMIT 1";
446 $params = array($user_id,0);
449 $query = $this->executeQuery($sql, $params);
450 $id = $query->fetchAll();
452 return ($query) ? $id : false;
455 public function getPreviousArticle($id, $user_id)
457 $sqlcondition = "is_read=0";
458 if (STORAGE
== 'postgres') {
459 $sqlcondition = "is_read=false";
461 $sql = "SELECT id FROM entries WHERE id = (SELECT max(id) FROM entries WHERE id < ? AND " . $sqlcondition . ") AND user_id=? AND " . $sqlcondition;
462 $params = array($id, $user_id);
463 $query = $this->executeQuery($sql, $params);
464 $id_entry = ($query) ? $query->fetchAll() : false;
465 $id = ($query) ? $id_entry[0][0] : false;
469 public function getNextArticle($id, $user_id)
471 $sqlcondition = "is_read=0";
472 if (STORAGE
== 'postgres') {
473 $sqlcondition = "is_read=false";
475 $sql = "SELECT id FROM entries WHERE id = (SELECT min(id) FROM entries WHERE id > ? AND " . $sqlcondition . ") AND user_id=? AND " . $sqlcondition;
476 $params = array($id, $user_id);
477 $query = $this->executeQuery($sql, $params);
478 $id_entry = ($query) ? $query->fetchAll() : false;
479 $id = ($query) ? $id_entry[0][0] : false;
484 public function updateContent($id, $content, $user_id)
486 $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?';
487 $params_action = array($content, $id, $user_id);
488 $query = $this->executeQuery($sql_action, $params_action);
495 * @param string $title
496 * @param string $content
497 * @param integer $user_id
498 * @return integer $id of inserted record
500 public function add($url, $title, $content, $user_id, $isFavorite=0, $isRead=0)
502 $sql_action = 'INSERT INTO entries ( url, title, content, user_id, is_fav, is_read ) VALUES (?, ?, ?, ?, ?, ?)';
503 $params_action = array($url, $title, $content, $user_id, $isFavorite, $isRead);
505 if ( !$this->executeQuery($sql_action, $params_action) ) {
509 $id = intval($this->getLastId( (STORAGE
== 'postgres') ? 'entries_id_seq' : '') );
514 public function deleteById($id, $user_id)
516 $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?";
517 $params_action = array($id, $user_id);
518 $query = $this->executeQuery($sql_action, $params_action);
522 public function favoriteById($id, $user_id)
524 $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?";
525 $params_action = array($id, $user_id);
526 $query = $this->executeQuery($sql_action, $params_action);
529 public function archiveById($id, $user_id)
531 $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?";
532 $params_action = array($id, $user_id);
533 $query = $this->executeQuery($sql_action, $params_action);
536 public function archiveAll($user_id)
538 $sql_action = "UPDATE entries SET is_read=? WHERE user_id=? AND is_read=?";
539 $params_action = array($user_id, 1, 0);
540 $query = $this->executeQuery($sql_action, $params_action);
543 public function getLastId($column = '')
545 return $this->getHandle()->lastInsertId($column);
548 public function search($term, $user_id, $limit = '')
550 $search = '%'.$term.'%';
551 $sql_action = "SELECT * FROM entries WHERE user_id=? AND (content LIKE ? OR title LIKE ? OR url LIKE ?) "; //searches in content, title and URL
552 $sql_action .= $this->getEntriesOrder().' ' . $limit;
553 $params_action = array($user_id, $search, $search, $search);
554 $query = $this->executeQuery($sql_action, $params_action);
555 return ($query) ? $query->fetchAll() : false;
558 public function retrieveAllTags($user_id, $term = NULL)
560 $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags
561 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
562 LEFT JOIN entries ON tags_entries.entry_id=entries.id
563 WHERE entries.user_id=?
564 ". (($term) ? "AND lower(tags.value) LIKE ?" : '') ."
565 GROUP BY tags.id, tags.value
566 ORDER BY tags.value";
567 $query = $this->executeQuery($sql, (($term)? array($user_id, strtolower('%'.$term.'%')) : array($user_id) ));
568 $tags = ($query) ? $query->fetchAll() : false;
573 public function retrieveTag($id, $user_id)
575 $sql = "SELECT DISTINCT tags.* FROM tags
576 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
577 LEFT JOIN entries ON tags_entries.entry_id=entries.id
578 WHERE tags.id=? AND entries.user_id=?";
579 $params = array(intval($id), $user_id);
580 $query = $this->executeQuery($sql, $params);
581 $tags = ($query) ? $query->fetchAll() : false;
582 $tag = ($query) ? $tags[0] : false;
587 public function retrieveEntriesByTag($tag_id, $user_id)
590 "SELECT entries.* FROM entries
591 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
592 WHERE tags_entries.tag_id = ? AND entries.user_id=? ORDER by entries.id DESC";
593 $query = $this->executeQuery($sql, array($tag_id, $user_id));
594 $entries = ($query) ? $query->fetchAll() : false;
599 public function retrieveTagsByEntry($entry_id)
602 "SELECT tags.* FROM tags
603 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
604 WHERE tags_entries.entry_id = ?";
605 $query = $this->executeQuery($sql, array($entry_id));
606 $tags = ($query) ? $query->fetchAll() : false;
611 public function removeTagForEntry($entry_id, $tag_id)
613 $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?";
614 $params_action = array($tag_id, $entry_id);
615 $query = $this->executeQuery($sql_action, $params_action);
616 return ($query) ? $query : false;
619 public function cleanUnusedTag($tag_id)
621 $sql_action = "SELECT tags.* FROM tags JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
622 $query = $this->executeQuery($sql_action,array($tag_id));
623 $tagstokeep = ($query) ? $query->fetchAll() : false;
624 $sql_action = "SELECT tags.* FROM tags LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
625 $query = $this->executeQuery($sql_action,array($tag_id));
626 $alltags = ($query) ? $query->fetchAll() : false;
628 if ($tagstokeep && $alltags) {
629 foreach ($alltags as $tag) {
630 if ($tag && !in_array($tag,$tagstokeep)) {
631 $sql_action = "DELETE FROM tags WHERE id=?";
632 $params_action = array($tag[0]);
633 $this->executeQuery($sql_action, $params_action);
642 public function retrieveTagByValue($value)
644 $sql = "SELECT * FROM tags WHERE value=?";
645 $params = array($value);
646 $query = $this->executeQuery($sql, $params);
647 $tag = ($query) ? $query->fetchAll() : false;
649 return ($query) ? $tag[0] : false;
652 public function createTag($value)
654 $sql_action = 'INSERT INTO tags ( value ) VALUES (?)';
655 $params_action = array($value);
656 $query = $this->executeQuery($sql_action, $params_action);
657 return ($query) ? $query : false;
660 public function setTagToEntry($tag_id, $entry_id)
662 $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)';
663 $params_action = array($tag_id, $entry_id);
664 $query = $this->executeQuery($sql_action, $params_action);
665 return ($query) ? $query : false;
668 private function getEntriesOrder()
670 if (isset($_SESSION['sort']) and array_key_exists($_SESSION['sort'], $this->order
)) {
671 return $this->order
[$_SESSION['sort']];
674 return $this->order
['default'];