]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Database.class.php
f6ba47089e8b1cb5b5a87d08787cf0b30397aefc
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 $db_path = 'mysql:host=' . STORAGE_SERVER
. ';dbname=' . STORAGE_DB
. ';charset=utf8mb4';
35 $this->handle
= new PDO($db_path, STORAGE_USER
, STORAGE_PASSWORD
, array(
36 PDO
::MYSQL_ATTR_INIT_COMMAND
=> 'SET NAMES utf8mb4',
40 $db_path = 'pgsql:host=' . STORAGE_SERVER
. ';dbname=' . STORAGE_DB
;
41 $this->handle
= new PDO($db_path, STORAGE_USER
, STORAGE_PASSWORD
);
44 die(STORAGE
. ' is not a recognised database system !');
47 $this->handle
->setAttribute(PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_EXCEPTION
);
49 Tools
::logm('storage type ' . STORAGE
);
52 private function getHandle()
57 private function _checkTags()
60 if (STORAGE
== 'sqlite') {
62 CREATE TABLE IF NOT EXISTS tags (
63 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
67 elseif(STORAGE
== 'mysql') {
69 CREATE TABLE IF NOT EXISTS `tags` (
70 `id` int(11) NOT NULL AUTO_INCREMENT,
71 `value` varchar(255) NOT NULL,
73 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
78 CREATE TABLE IF NOT EXISTS tags (
79 id bigserial primary key,
80 value varchar(255) NOT NULL
85 $query = $this->executeQuery($sql, array());
87 if (STORAGE
== 'sqlite') {
89 CREATE TABLE IF NOT EXISTS tags_entries (
90 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
93 FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE,
94 FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE
97 elseif(STORAGE
== 'mysql') {
99 CREATE TABLE IF NOT EXISTS `tags_entries` (
100 `id` int(11) NOT NULL AUTO_INCREMENT,
101 `entry_id` int(11) NOT NULL,
102 `tag_id` int(11) NOT NULL,
103 FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE,
104 FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE,
106 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
111 CREATE TABLE IF NOT EXISTS tags_entries (
112 id bigserial primary key,
113 entry_id integer NOT NULL,
114 tag_id integer NOT NULL
119 $query = $this->executeQuery($sql, array());
122 public function install($login, $password, $email = '')
124 $sql = 'INSERT INTO users ( username, password, name, email) VALUES (?, ?, ?, ?)';
125 $params = array($login, $password, $login, $email);
126 $query = $this->executeQuery($sql, $params);
129 if (STORAGE
== 'postgres') {
130 $sequence = 'users_id_seq';
133 $id_user = intval($this->getLastId($sequence));
135 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
136 $params = array($id_user, 'pager', PAGINATION
);
137 $query = $this->executeQuery($sql, $params);
139 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
140 $params = array($id_user, 'language', LANG
);
141 $query = $this->executeQuery($sql, $params);
143 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
144 $params = array($id_user, 'theme', DEFAULT_THEME
);
145 $query = $this->executeQuery($sql, $params);
150 public function getConfigUser($id)
152 $sql = "SELECT * FROM users_config WHERE user_id = ?";
153 $query = $this->executeQuery($sql, array($id));
154 $result = $query->fetchAll();
155 $user_config = array();
157 foreach ($result as $key => $value) {
158 $user_config[$value['name']] = $value['value'];
164 public function userExists($username)
166 $sql = "SELECT * FROM users WHERE username=?";
167 $query = $this->executeQuery($sql, array($username));
168 $login = $query->fetchAll();
169 if (isset($login[0])) {
176 public function login($username, $password, $isauthenticated = FALSE)
178 if ($isauthenticated) {
179 $sql = "SELECT * FROM users WHERE username=?";
180 $query = $this->executeQuery($sql, array($username));
182 $sql = "SELECT * FROM users WHERE username=? AND password=?";
183 $query = $this->executeQuery($sql, array($username, $password));
185 $login = $query->fetchAll();
188 if (isset($login[0])) {
189 $user['id'] = $login[0]['id'];
190 $user['username'] = $login[0]['username'];
191 $user['password'] = $login[0]['password'];
192 $user['name'] = $login[0]['name'];
193 $user['email'] = $login[0]['email'];
194 $user['config'] = $this->getConfigUser($login[0]['id']);
200 public function updatePassword($userId, $password)
202 $sql_update = "UPDATE users SET password=? WHERE id=?";
203 $params_update = array($password, $userId);
204 $query = $this->executeQuery($sql_update, $params_update);
207 public function updateUserConfig($userId, $key, $value)
209 $config = $this->getConfigUser($userId);
211 if (! isset($config[$key])) {
212 $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)";
215 $sql = "UPDATE users_config SET value=? WHERE user_id=? AND name=?";
218 $params = array($value, $userId, $key);
219 $query = $this->executeQuery($sql, $params);
222 private function executeQuery($sql, $params)
226 $query = $this->getHandle()->prepare($sql);
227 $query->execute($params);
232 Tools
::logm('execute query error : '.$e->getMessage());
237 public function listUsers($username = NULL)
239 $sql = 'SELECT count(*) FROM users'.( $username ? ' WHERE username=?' : '');
240 $query = $this->executeQuery($sql, ( $username ? array($username) : array()));
241 list($count) = $query->fetch();
245 public function getUserPassword($userID)
247 $sql = "SELECT * FROM users WHERE id=?";
248 $query = $this->executeQuery($sql, array($userID));
249 $password = $query->fetchAll();
250 return isset($password[0]['password']) ? $password[0]['password'] : null;
253 public function deleteUserConfig($userID)
255 $sql_action = 'DELETE from users_config WHERE user_id=?';
256 $params_action = array($userID);
257 $query = $this->executeQuery($sql_action, $params_action);
261 public function deleteTagsEntriesAndEntries($userID)
263 $entries = $this->retrieveAll($userID);
264 foreach($entries as $entryid) {
265 $tags = $this->retrieveTagsByEntry($entryid);
266 foreach($tags as $tag) {
267 $this->removeTagForEntry($entryid,$tags);
269 $this->deleteById($entryid,$userID);
273 public function deleteUser($userID)
275 $sql_action = 'DELETE from users WHERE id=?';
276 $params_action = array($userID);
277 $query = $this->executeQuery($sql_action, $params_action);
280 public function updateContentAndTitle($id, $title, $body, $user_id)
282 $sql_action = 'UPDATE entries SET content = ?, title = ? WHERE id=? AND user_id=?';
283 $params_action = array($body, $title, $id, $user_id);
284 $query = $this->executeQuery($sql_action, $params_action);
288 public function retrieveUnfetchedEntries($user_id, $limit)
291 $sql_limit = "LIMIT 0,".$limit;
292 if (STORAGE
== 'postgres') {
293 $sql_limit = "LIMIT ".$limit." OFFSET 0";
296 $sql = "SELECT * FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE 'Untitled - Import%' AND user_id=? ORDER BY id " . $sql_limit;
297 $query = $this->executeQuery($sql, array($user_id));
298 $entries = $query->fetchAll();
303 public function retrieveUnfetchedEntriesCount($user_id)
305 $sql = "SELECT count(*) FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE 'Untitled - Import%' AND user_id=?";
306 $query = $this->executeQuery($sql, array($user_id));
307 list($count) = $query->fetch();
312 public function retrieveAll($user_id)
314 $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id";
315 $query = $this->executeQuery($sql, array($user_id));
316 $entries = $query->fetchAll();
321 public function retrieveOneById($id, $user_id)
324 $sql = "SELECT * FROM entries WHERE id=? AND user_id=?";
325 $params = array(intval($id), $user_id);
326 $query = $this->executeQuery($sql, $params);
327 $entry = $query->fetchAll();
329 return isset($entry[0]) ? $entry[0] : null;
332 public function retrieveOneByURL($url, $user_id)
335 $sql = "SELECT * FROM entries WHERE url=? AND user_id=?";
336 $params = array($url, $user_id);
337 $query = $this->executeQuery($sql, $params);
338 $entry = $query->fetchAll();
340 return isset($entry[0]) ? $entry[0] : null;
343 public function reassignTags($old_entry_id, $new_entry_id)
345 $sql = "UPDATE tags_entries SET entry_id=? WHERE entry_id=?";
346 $params = array($new_entry_id, $old_entry_id);
347 $query = $this->executeQuery($sql, $params);
350 public function getEntriesByView($view, $user_id, $limit = '', $tag_id = 0)
354 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? ";
355 $params = array($user_id, 1);
358 $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? ";
359 $params = array($user_id, 1);
362 $sql = "SELECT entries.* FROM entries
363 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
364 WHERE entries.user_id=? AND tags_entries.tag_id = ? ";
365 $params = array($user_id, $tag_id);
368 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? ";
369 $params = array($user_id, 0);
373 $sql .= $this->getEntriesOrder().' ' . $limit;
375 $query = $this->executeQuery($sql, $params);
376 $entries = $query->fetchAll();
381 public function getEntriesByViewCount($view, $user_id, $tag_id = 0)
385 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? ";
386 $params = array($user_id, 1);
389 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_fav=? ";
390 $params = array($user_id, 1);
393 $sql = "SELECT count(*) FROM entries
394 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
395 WHERE entries.user_id=? AND tags_entries.tag_id = ? ";
396 $params = array($user_id, $tag_id);
399 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? ";
400 $params = array($user_id, 0);
404 $query = $this->executeQuery($sql, $params);
405 list($count) = $query->fetch();
410 public function updateContent($id, $content, $user_id)
412 $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?';
413 $params_action = array($content, $id, $user_id);
414 $query = $this->executeQuery($sql_action, $params_action);
421 * @param string $title
422 * @param string $content
423 * @param integer $user_id
424 * @return integer $id of inserted record
426 public function add($url, $title, $content, $user_id, $isFavorite=0, $isRead=0)
428 $sql_action = 'INSERT INTO entries ( url, title, content, user_id, is_fav, is_read ) VALUES (?, ?, ?, ?, ?, ?)';
429 $params_action = array($url, $title, $content, $user_id, $isFavorite, $isRead);
431 if ( !$this->executeQuery($sql_action, $params_action) ) {
435 $id = intval($this->getLastId( (STORAGE
== 'postgres') ? 'entries_id_seq' : '') );
440 public function deleteById($id, $user_id)
442 $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?";
443 $params_action = array($id, $user_id);
444 $query = $this->executeQuery($sql_action, $params_action);
448 public function favoriteById($id, $user_id)
450 $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?";
451 $params_action = array($id, $user_id);
452 $query = $this->executeQuery($sql_action, $params_action);
455 public function archiveById($id, $user_id)
457 $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?";
458 $params_action = array($id, $user_id);
459 $query = $this->executeQuery($sql_action, $params_action);
462 public function archiveAll($user_id)
464 $sql_action = "UPDATE entries SET is_read=? WHERE user_id=? AND is_read=?";
465 $params_action = array($user_id, 1, 0);
466 $query = $this->executeQuery($sql_action, $params_action);
469 public function getLastId($column = '')
471 return $this->getHandle()->lastInsertId($column);
474 public function search($term, $user_id, $limit = '')
476 $search = '%'.$term.'%';
477 $sql_action = "SELECT * FROM entries WHERE user_id=? AND (content LIKE ? OR title LIKE ? OR url LIKE ?) "; //searches in content, title and URL
478 $sql_action .= $this->getEntriesOrder().' ' . $limit;
479 $params_action = array($user_id, $search, $search, $search);
480 $query = $this->executeQuery($sql_action, $params_action);
481 return $query->fetchAll();
484 public function retrieveAllTags($user_id, $term = NULL)
486 $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags
487 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
488 LEFT JOIN entries ON tags_entries.entry_id=entries.id
489 WHERE entries.user_id=?
490 ". (($term) ? "AND lower(tags.value) LIKE ?" : '') ."
491 GROUP BY tags.id, tags.value
492 ORDER BY tags.value";
493 $query = $this->executeQuery($sql, (($term)? array($user_id, strtolower('%'.$term.'%')) : array($user_id) ));
494 $tags = $query->fetchAll();
499 public function retrieveTag($id, $user_id)
502 $sql = "SELECT DISTINCT tags.* FROM tags
503 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
504 LEFT JOIN entries ON tags_entries.entry_id=entries.id
505 WHERE tags.id=? AND entries.user_id=?";
506 $params = array(intval($id), $user_id);
507 $query = $this->executeQuery($sql, $params);
508 $tag = $query->fetchAll();
510 return isset($tag[0]) ? $tag[0] : NULL;
513 public function retrieveEntriesByTag($tag_id, $user_id)
516 "SELECT entries.* FROM entries
517 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
518 WHERE tags_entries.tag_id = ? AND entries.user_id=? ORDER by entries.id DESC";
519 $query = $this->executeQuery($sql, array($tag_id, $user_id));
520 $entries = $query->fetchAll();
525 public function retrieveTagsByEntry($entry_id)
528 "SELECT tags.* FROM tags
529 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
530 WHERE tags_entries.entry_id = ?";
531 $query = $this->executeQuery($sql, array($entry_id));
532 $tags = $query->fetchAll();
537 public function removeTagForEntry($entry_id, $tag_id)
539 $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?";
540 $params_action = array($tag_id, $entry_id);
541 $query = $this->executeQuery($sql_action, $params_action);
545 public function cleanUnusedTag($tag_id)
547 $sql_action = "SELECT tags.* FROM tags JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
548 $query = $this->executeQuery($sql_action,array($tag_id));
549 $tagstokeep = $query->fetchAll();
550 $sql_action = "SELECT tags.* FROM tags LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
551 $query = $this->executeQuery($sql_action,array($tag_id));
552 $alltags = $query->fetchAll();
554 foreach ($alltags as $tag) {
555 if ($tag && !in_array($tag,$tagstokeep)) {
556 $sql_action = "DELETE FROM tags WHERE id=?";
557 $params_action = array($tag[0]);
558 $this->executeQuery($sql_action, $params_action);
565 public function retrieveTagByValue($value)
568 $sql = "SELECT * FROM tags WHERE value=?";
569 $params = array($value);
570 $query = $this->executeQuery($sql, $params);
571 $tag = $query->fetchAll();
573 return isset($tag[0]) ? $tag[0] : null;
576 public function createTag($value)
578 $sql_action = 'INSERT INTO tags ( value ) VALUES (?)';
579 $params_action = array($value);
580 $query = $this->executeQuery($sql_action, $params_action);
584 public function setTagToEntry($tag_id, $entry_id)
586 $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)';
587 $params_action = array($tag_id, $entry_id);
588 $query = $this->executeQuery($sql_action, $params_action);
592 private function getEntriesOrder()
594 if (isset($_SESSION['sort']) and array_key_exists($_SESSION['sort'], $this->order
)) {
595 return $this->order
[$_SESSION['sort']];
598 return $this->order
['default'];