]>
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->fetchAll();
160 $user_config = array();
162 foreach ($result as $key => $value) {
163 $user_config[$value['name']] = $value['value'];
169 public function userExists($username)
171 $sql = "SELECT * FROM users WHERE username=?";
172 $query = $this->executeQuery($sql, array($username));
173 $login = $query->fetchAll();
174 if (isset($login[0])) {
181 public function login($username, $password, $isauthenticated = FALSE)
183 if ($isauthenticated) {
184 $sql = "SELECT * FROM users WHERE username=?";
185 $query = $this->executeQuery($sql, array($username));
187 $sql = "SELECT * FROM users WHERE username=? AND password=?";
188 $query = $this->executeQuery($sql, array($username, $password));
190 $login = $query->fetchAll();
193 if (isset($login[0])) {
194 $user['id'] = $login[0]['id'];
195 $user['username'] = $login[0]['username'];
196 $user['password'] = $login[0]['password'];
197 $user['name'] = $login[0]['name'];
198 $user['email'] = $login[0]['email'];
199 $user['config'] = $this->getConfigUser($login[0]['id']);
205 public function updatePassword($userId, $password)
207 $sql_update = "UPDATE users SET password=? WHERE id=?";
208 $params_update = array($password, $userId);
209 $query = $this->executeQuery($sql_update, $params_update);
212 public function updateUserConfig($userId, $key, $value)
214 $config = $this->getConfigUser($userId);
216 if (! isset($config[$key])) {
217 $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)";
220 $sql = "UPDATE users_config SET value=? WHERE user_id=? AND name=?";
223 $params = array($value, $userId, $key);
224 $query = $this->executeQuery($sql, $params);
227 private function executeQuery($sql, $params)
231 $query = $this->getHandle()->prepare($sql);
232 $query->execute($params);
237 Tools
::logm('execute query error : '.$e->getMessage());
242 public function listUsers($username = NULL)
244 $sql = 'SELECT count(*) FROM users'.( $username ? ' WHERE username=?' : '');
245 $query = $this->executeQuery($sql, ( $username ? array($username) : array()));
246 list($count) = $query->fetch();
250 public function getUserPassword($userID)
252 $sql = "SELECT * FROM users WHERE id=?";
253 $query = $this->executeQuery($sql, array($userID));
254 $password = $query->fetchAll();
255 return isset($password[0]['password']) ? $password[0]['password'] : null;
258 public function deleteUserConfig($userID)
260 $sql_action = 'DELETE from users_config WHERE user_id=?';
261 $params_action = array($userID);
262 $query = $this->executeQuery($sql_action, $params_action);
266 public function deleteTagsEntriesAndEntries($userID)
268 $entries = $this->retrieveAll($userID);
269 foreach($entries as $entryid) {
270 $tags = $this->retrieveTagsByEntry($entryid);
271 foreach($tags as $tag) {
272 $this->removeTagForEntry($entryid,$tags);
274 $this->deleteById($entryid,$userID);
278 public function deleteUser($userID)
280 $sql_action = 'DELETE from users WHERE id=?';
281 $params_action = array($userID);
282 $query = $this->executeQuery($sql_action, $params_action);
285 public function updateContentAndTitle($id, $title, $body, $user_id)
287 $sql_action = 'UPDATE entries SET content = ?, title = ? WHERE id=? AND user_id=?';
288 $params_action = array($body, $title, $id, $user_id);
289 $query = $this->executeQuery($sql_action, $params_action);
293 public function retrieveUnfetchedEntries($user_id, $limit)
296 $sql_limit = "LIMIT 0,".$limit;
297 if (STORAGE
== 'postgres') {
298 $sql_limit = "LIMIT ".$limit." OFFSET 0";
301 $sql = "SELECT * FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE '%Import%' AND user_id=? ORDER BY id " . $sql_limit;
302 $query = $this->executeQuery($sql, array($user_id));
303 $entries = $query->fetchAll();
308 public function retrieveUnfetchedEntriesCount($user_id)
310 $sql = "SELECT count(*) FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE '%Import%' AND user_id=?";
311 $query = $this->executeQuery($sql, array($user_id));
312 list($count) = $query->fetch();
317 public function retrieveAll($user_id)
319 $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id";
320 $query = $this->executeQuery($sql, array($user_id));
321 $entries = $query->fetchAll();
326 public function retrieveAllWithTags($user_id)
328 $entries = $this->retrieveAll($user_id);
329 $count = count($entries);
330 for ($i = 0; $i < $count; $i++
) {
331 $tag_entries = $this->retrieveTagsByEntry($entries[$i]['id']);
333 foreach ($tag_entries as $tag) {
336 $entries[$i]['tags'] = implode(',', $tags);
341 public function retrieveOneById($id, $user_id)
344 $sql = "SELECT * FROM entries WHERE id=? AND user_id=?";
345 $params = array(intval($id), $user_id);
346 $query = $this->executeQuery($sql, $params);
347 $entry = $query->fetchAll();
349 return isset($entry[0]) ? $entry[0] : null;
352 public function retrieveOneByURL($url, $user_id)
355 $sql = "SELECT * FROM entries WHERE url=? AND user_id=?";
356 $params = array($url, $user_id);
357 $query = $this->executeQuery($sql, $params);
358 $entry = $query->fetchAll();
360 return isset($entry[0]) ? $entry[0] : null;
363 public function reassignTags($old_entry_id, $new_entry_id)
365 $sql = "UPDATE tags_entries SET entry_id=? WHERE entry_id=?";
366 $params = array($new_entry_id, $old_entry_id);
367 $query = $this->executeQuery($sql, $params);
370 public function getEntriesByView($view, $user_id, $limit = '', $tag_id = 0)
374 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? ";
375 $params = array($user_id, 1);
378 $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? ";
379 $params = array($user_id, 1);
382 $sql = "SELECT entries.* FROM entries
383 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
384 WHERE entries.user_id=? AND tags_entries.tag_id = ? ";
385 $params = array($user_id, $tag_id);
388 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? ";
389 $params = array($user_id, 0);
393 $sql .= $this->getEntriesOrder().' ' . $limit;
395 $query = $this->executeQuery($sql, $params);
396 $entries = $query->fetchAll();
401 public function getEntriesByViewCount($view, $user_id, $tag_id = 0)
405 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? ";
406 $params = array($user_id, 1);
409 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_fav=? ";
410 $params = array($user_id, 1);
413 $sql = "SELECT count(*) FROM entries
414 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
415 WHERE entries.user_id=? AND tags_entries.tag_id = ? ";
416 $params = array($user_id, $tag_id);
419 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? ";
420 $params = array($user_id, 0);
424 $query = $this->executeQuery($sql, $params);
425 list($count) = $query->fetch();
429 public function getRandomId($user_id, $view) {
430 $random = (STORAGE
== 'mysql') ? 'RAND()' : 'RANDOM()';
433 $sql = "SELECT id FROM entries WHERE user_id=? AND is_read=? ORDER BY ". $random . " LIMIT 1";
434 $params = array($user_id,1);
437 $sql = "SELECT id FROM entries WHERE user_id=? AND is_fav=? ORDER BY ". $random . " LIMIT 1";
438 $params = array($user_id,1);
441 $sql = "SELECT id FROM entries WHERE user_id=? AND is_read=? ORDER BY ". $random . " LIMIT 1";
442 $params = array($user_id,0);
445 $query = $this->executeQuery($sql, $params);
446 $id = $query->fetchAll();
451 public function getPreviousArticle($id, $user_id)
453 $sql = "SELECT id FROM entries WHERE id = (SELECT max(id) FROM entries WHERE id < ? AND is_read=0) AND user_id=? AND is_read=0";
454 $params = array($id, $user_id);
455 $query = $this->executeQuery($sql, $params);
456 $id_entry = $query->fetchAll();
457 $id = $id_entry[0][0];
461 public function getNextArticle($id, $user_id)
463 $sql = "SELECT id FROM entries WHERE id = (SELECT min(id) FROM entries WHERE id > ? AND is_read=0) AND user_id=? AND is_read=0";
464 $params = array($id, $user_id);
465 $query = $this->executeQuery($sql, $params);
466 $id_entry = $query->fetchAll();
467 $id = $id_entry[0][0];
472 public function updateContent($id, $content, $user_id)
474 $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?';
475 $params_action = array($content, $id, $user_id);
476 $query = $this->executeQuery($sql_action, $params_action);
483 * @param string $title
484 * @param string $content
485 * @param integer $user_id
486 * @return integer $id of inserted record
488 public function add($url, $title, $content, $user_id, $isFavorite=0, $isRead=0)
490 $sql_action = 'INSERT INTO entries ( url, title, content, user_id, is_fav, is_read ) VALUES (?, ?, ?, ?, ?, ?)';
491 $params_action = array($url, $title, $content, $user_id, $isFavorite, $isRead);
493 if ( !$this->executeQuery($sql_action, $params_action) ) {
497 $id = intval($this->getLastId( (STORAGE
== 'postgres') ? 'entries_id_seq' : '') );
502 public function deleteById($id, $user_id)
504 $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?";
505 $params_action = array($id, $user_id);
506 $query = $this->executeQuery($sql_action, $params_action);
510 public function favoriteById($id, $user_id)
512 $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?";
513 $params_action = array($id, $user_id);
514 $query = $this->executeQuery($sql_action, $params_action);
517 public function archiveById($id, $user_id)
519 $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?";
520 $params_action = array($id, $user_id);
521 $query = $this->executeQuery($sql_action, $params_action);
524 public function archiveAll($user_id)
526 $sql_action = "UPDATE entries SET is_read=? WHERE user_id=? AND is_read=?";
527 $params_action = array($user_id, 1, 0);
528 $query = $this->executeQuery($sql_action, $params_action);
531 public function getLastId($column = '')
533 return $this->getHandle()->lastInsertId($column);
536 public function search($term, $user_id, $limit = '')
538 $search = '%'.$term.'%';
539 $sql_action = "SELECT * FROM entries WHERE user_id=? AND (content LIKE ? OR title LIKE ? OR url LIKE ?) "; //searches in content, title and URL
540 $sql_action .= $this->getEntriesOrder().' ' . $limit;
541 $params_action = array($user_id, $search, $search, $search);
542 $query = $this->executeQuery($sql_action, $params_action);
543 return $query->fetchAll();
546 public function retrieveAllTags($user_id, $term = NULL)
548 $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags
549 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
550 LEFT JOIN entries ON tags_entries.entry_id=entries.id
551 WHERE entries.user_id=?
552 ". (($term) ? "AND lower(tags.value) LIKE ?" : '') ."
553 GROUP BY tags.id, tags.value
554 ORDER BY tags.value";
555 $query = $this->executeQuery($sql, (($term)? array($user_id, strtolower('%'.$term.'%')) : array($user_id) ));
556 $tags = $query->fetchAll();
561 public function retrieveTag($id, $user_id)
564 $sql = "SELECT DISTINCT tags.* FROM tags
565 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
566 LEFT JOIN entries ON tags_entries.entry_id=entries.id
567 WHERE tags.id=? AND entries.user_id=?";
568 $params = array(intval($id), $user_id);
569 $query = $this->executeQuery($sql, $params);
570 $tag = $query->fetchAll();
572 return isset($tag[0]) ? $tag[0] : NULL;
575 public function retrieveEntriesByTag($tag_id, $user_id)
578 "SELECT entries.* FROM entries
579 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
580 WHERE tags_entries.tag_id = ? AND entries.user_id=? ORDER by entries.id DESC";
581 $query = $this->executeQuery($sql, array($tag_id, $user_id));
582 $entries = $query->fetchAll();
587 public function retrieveTagsByEntry($entry_id)
590 "SELECT tags.* FROM tags
591 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
592 WHERE tags_entries.entry_id = ?";
593 $query = $this->executeQuery($sql, array($entry_id));
594 $tags = $query->fetchAll();
599 public function removeTagForEntry($entry_id, $tag_id)
601 $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?";
602 $params_action = array($tag_id, $entry_id);
603 $query = $this->executeQuery($sql_action, $params_action);
607 public function cleanUnusedTag($tag_id)
609 $sql_action = "SELECT tags.* FROM tags JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
610 $query = $this->executeQuery($sql_action,array($tag_id));
611 $tagstokeep = $query->fetchAll();
612 $sql_action = "SELECT tags.* FROM tags LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
613 $query = $this->executeQuery($sql_action,array($tag_id));
614 $alltags = $query->fetchAll();
616 foreach ($alltags as $tag) {
617 if ($tag && !in_array($tag,$tagstokeep)) {
618 $sql_action = "DELETE FROM tags WHERE id=?";
619 $params_action = array($tag[0]);
620 $this->executeQuery($sql_action, $params_action);
627 public function retrieveTagByValue($value)
630 $sql = "SELECT * FROM tags WHERE value=?";
631 $params = array($value);
632 $query = $this->executeQuery($sql, $params);
633 $tag = $query->fetchAll();
635 return isset($tag[0]) ? $tag[0] : null;
638 public function createTag($value)
640 $sql_action = 'INSERT INTO tags ( value ) VALUES (?)';
641 $params_action = array($value);
642 $query = $this->executeQuery($sql_action, $params_action);
646 public function setTagToEntry($tag_id, $entry_id)
648 $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)';
649 $params_action = array($tag_id, $entry_id);
650 $query = $this->executeQuery($sql_action, $params_action);
654 private function getEntriesOrder()
656 if (isset($_SESSION['sort']) and array_key_exists($_SESSION['sort'], $this->order
)) {
657 return $this->order
[$_SESSION['sort']];
660 return $this->order
['default'];