]>
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://www.wtfpl.net/ see COPYING file
13 private $order = array(
14 'ia' => 'ORDER BY entries.id',
15 'id' => 'ORDER BY entries.id DESC',
16 'ta' => 'ORDER BY lower(entries.title)',
17 'td' => 'ORDER BY lower(entries.title) DESC',
18 'default' => 'ORDER BY entries.id'
21 function __construct()
25 $db_path = 'sqlite:' . STORAGE_SQLITE
;
26 $this->handle
= new PDO($db_path);
29 $db_path = 'mysql:host=' . STORAGE_SERVER
. ';dbname=' . STORAGE_DB
;
30 $this->handle
= new PDO($db_path, STORAGE_USER
, STORAGE_PASSWORD
);
33 $db_path = 'pgsql:host=' . STORAGE_SERVER
. ';dbname=' . STORAGE_DB
;
34 $this->handle
= new PDO($db_path, STORAGE_USER
, STORAGE_PASSWORD
);
38 $this->handle
->setAttribute(PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_EXCEPTION
);
39 Tools
::logm('storage type ' . STORAGE
);
42 private function getHandle() {
46 public function isInstalled() {
47 $sql = "SELECT username FROM users";
48 $query = $this->executeQuery($sql, array());
49 if ($query == false) {
50 die(STORAGE
. ' database looks empty. You have to create it (you can find database structure in install folder).');
52 $hasAdmin = count($query->fetchAll());
60 public function checkTags() {
62 if (STORAGE
== 'sqlite') {
64 CREATE TABLE IF NOT EXISTS tags (
65 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
69 elseif(STORAGE
== 'mysql') {
71 CREATE TABLE IF NOT EXISTS `tags` (
72 `id` int(11) NOT NULL AUTO_INCREMENT,
73 `value` varchar(255) NOT NULL,
75 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
81 id bigserial primary key,
82 value varchar(255) NOT NULL
87 $query = $this->executeQuery($sql, array());
89 if (STORAGE
== 'sqlite') {
91 CREATE TABLE IF NOT EXISTS tags_entries (
92 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
95 FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE,
96 FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE
99 elseif(STORAGE
== 'mysql') {
101 CREATE TABLE IF NOT EXISTS `tags_entries` (
102 `id` int(11) NOT NULL AUTO_INCREMENT,
103 `entry_id` int(11) NOT NULL,
104 `tag_id` int(11) NOT NULL,
105 FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE,
106 FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE,
108 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
113 CREATE TABLE tags_entries (
114 id bigserial primary key,
115 entry_id integer NOT NULL,
116 tag_id integer NOT NULL
121 $query = $this->executeQuery($sql, array());
124 public function install($login, $password) {
125 $sql = 'INSERT INTO users ( username, password, name, email) VALUES (?, ?, ?, ?)';
126 $params = array($login, $password, $login, ' ');
127 $query = $this->executeQuery($sql, $params);
130 if (STORAGE
== 'postgres') {
131 $sequence = 'users_id_seq';
134 $id_user = intval($this->getLastId($sequence));
136 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
137 $params = array($id_user, 'pager', PAGINATION
);
138 $query = $this->executeQuery($sql, $params);
140 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
141 $params = array($id_user, 'language', LANG
);
142 $query = $this->executeQuery($sql, $params);
144 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
145 $params = array($id_user, 'theme', DEFAULT_THEME
);
146 $query = $this->executeQuery($sql, $params);
151 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) {
165 $sql = "SELECT * FROM users WHERE username=?";
166 $query = $this->executeQuery($sql, array($username));
167 $login = $query->fetchAll();
168 if (isset($login[0])) {
175 public function login($username, $password, $isauthenticated=false) {
176 if ($isauthenticated) {
177 $sql = "SELECT * FROM users WHERE username=?";
178 $query = $this->executeQuery($sql, array($username));
180 $sql = "SELECT * FROM users WHERE username=? AND password=?";
181 $query = $this->executeQuery($sql, array($username, $password));
183 $login = $query->fetchAll();
186 if (isset($login[0])) {
187 $user['id'] = $login[0]['id'];
188 $user['username'] = $login[0]['username'];
189 $user['password'] = $login[0]['password'];
190 $user['name'] = $login[0]['name'];
191 $user['email'] = $login[0]['email'];
192 $user['config'] = $this->getConfigUser($login[0]['id']);
198 public function updatePassword($userId, $password)
200 $sql_update = "UPDATE users SET password=? WHERE id=?";
201 $params_update = array($password, $userId);
202 $query = $this->executeQuery($sql_update, $params_update);
205 public function updateUserConfig($userId, $key, $value) {
206 $config = $this->getConfigUser($userId);
208 if (! isset($config[$key])) {
209 $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)";
212 $sql = "UPDATE users_config SET value=? WHERE user_id=? AND name=?";
215 $params = array($value, $userId, $key);
216 $query = $this->executeQuery($sql, $params);
219 private function executeQuery($sql, $params) {
222 $query = $this->getHandle()->prepare($sql);
223 $query->execute($params);
228 Tools
::logm('execute query error : '.$e->getMessage());
233 public function updateContentAndTitle($id, $title, $body, $user_id) {
234 $sql_action = 'UPDATE entries SET content = ?, title = ? WHERE id=? AND user_id=?';
235 $params_action = array($body, $title, $id, $user_id);
236 $query = $this->executeQuery($sql_action, $params_action);
241 public function retrieveUnfetchedEntries($user_id, $limit) {
243 $sql_limit = "LIMIT 0,".$limit;
244 if (STORAGE
== 'postgres') {
245 $sql_limit = "LIMIT ".$limit." OFFSET 0";
248 $sql = "SELECT * FROM entries WHERE (content = '' OR content IS NULL) AND user_id=? ORDER BY id " . $sql_limit;
249 $query = $this->executeQuery($sql, array($user_id));
250 $entries = $query->fetchAll();
255 public function retrieveAll($user_id) {
256 $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? ORDER BY id";
257 $query = $this->executeQuery($sql, array($user_id));
258 $entries = $query->fetchAll();
263 public function retrieveOneById($id, $user_id) {
265 $sql = "SELECT * FROM entries WHERE id=? AND user_id=?";
266 $params = array(intval($id), $user_id);
267 $query = $this->executeQuery($sql, $params);
268 $entry = $query->fetchAll();
270 return isset($entry[0]) ? $entry[0] : null;
273 public function retrieveOneByURL($url, $user_id) {
275 $sql = "SELECT * FROM entries WHERE content <> '' AND url=? AND user_id=?";
276 $params = array($url, $user_id);
277 $query = $this->executeQuery($sql, $params);
278 $entry = $query->fetchAll();
280 return isset($entry[0]) ? $entry[0] : null;
283 public function reassignTags($old_entry_id, $new_entry_id) {
284 $sql = "UPDATE tags_entries SET entry_id=? WHERE entry_id=?";
285 $params = array($new_entry_id, $old_entry_id);
286 $query = $this->executeQuery($sql, $params);
289 public function getEntriesByView($view, $user_id, $limit = '', $tag_id = 0) {
292 $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? AND is_read=? ";
293 $params = array($user_id, 1);
296 $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? AND is_fav=? ";
297 $params = array($user_id, 1);
300 $sql = "SELECT entries.* FROM entries
301 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
302 WHERE entries.content <> '' AND
303 entries.user_id=? AND tags_entries.tag_id = ? ";
304 $params = array($user_id, $tag_id);
307 $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? AND is_read=? ";
308 $params = array($user_id, 0);
312 $sql .= $this->getEntriesOrder().' ' . $limit;
314 $query = $this->executeQuery($sql, $params);
315 $entries = $query->fetchAll();
320 public function getEntriesByViewCount($view, $user_id, $tag_id = 0) {
323 $sql = "SELECT count(*) FROM entries WHERE content <> '' AND user_id=? AND is_read=? ";
324 $params = array($user_id, 1);
327 $sql = "SELECT count(*) FROM entries WHERE content <> '' AND user_id=? AND is_fav=? ";
328 $params = array($user_id, 1);
331 $sql = "SELECT count(*) FROM entries
332 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
333 WHERE entries.content <> '' AND
334 entries.user_id=? AND tags_entries.tag_id = ? ";
335 $params = array($user_id, $tag_id);
338 $sql = "SELECT count(*) FROM entries WHERE content <> '' AND user_id=? AND is_read=? ";
339 $params = array($user_id, 0);
343 $query = $this->executeQuery($sql, $params);
344 list($count) = $query->fetch();
349 public function updateContent($id, $content, $user_id) {
350 $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?';
351 $params_action = array($content, $id, $user_id);
352 $query = $this->executeQuery($sql_action, $params_action);
356 public function add($url, $title, $content, $user_id) {
357 $sql_action = 'INSERT INTO entries ( url, title, content, user_id ) VALUES (?, ?, ?, ?)';
358 $params_action = array($url, $title, $content, $user_id);
359 $query = $this->executeQuery($sql_action, $params_action);
363 public function deleteById($id, $user_id) {
364 $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?";
365 $params_action = array($id, $user_id);
366 $query = $this->executeQuery($sql_action, $params_action);
370 public function favoriteById($id, $user_id) {
371 $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?";
372 $params_action = array($id, $user_id);
373 $query = $this->executeQuery($sql_action, $params_action);
376 public function archiveById($id, $user_id) {
377 $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?";
378 $params_action = array($id, $user_id);
379 $query = $this->executeQuery($sql_action, $params_action);
382 public function archiveAll($user_id) {
383 $sql_action = "UPDATE entries SET is_read=? WHERE user_id=? AND is_read=?";
384 $params_action = array($user_id, 1, 0);
385 $query = $this->executeQuery($sql_action, $params_action);
388 public function getLastId($column = '') {
389 return $this->getHandle()->lastInsertId($column);
392 public function retrieveAllTags($user_id, $term = null) {
393 $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags
394 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
395 LEFT JOIN entries ON tags_entries.entry_id=entries.id
396 WHERE entries.content <> '' AND entries.user_id=?
397 ". (($term) ? "AND lower(tags.value) LIKE ?" : '') ."
398 GROUP BY tags.id, tags.value
399 ORDER BY tags.value";
400 $query = $this->executeQuery($sql, (($term)? array($user_id, strtolower('%'.$term.'%')) : array($user_id) ));
401 $tags = $query->fetchAll();
406 public function retrieveTag($id, $user_id) {
408 $sql = "SELECT DISTINCT tags.* FROM tags
409 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
410 LEFT JOIN entries ON tags_entries.entry_id=entries.id
411 WHERE entries.content <> '' AND tags.id=? AND entries.user_id=?";
412 $params = array(intval($id), $user_id);
413 $query = $this->executeQuery($sql, $params);
414 $tag = $query->fetchAll();
416 return isset($tag[0]) ? $tag[0] : null;
419 public function retrieveEntriesByTag($tag_id, $user_id) {
421 "SELECT entries.* FROM entries
422 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
423 WHERE entries.content <> '' AND
424 tags_entries.tag_id = ? AND entries.user_id=?";
425 $query = $this->executeQuery($sql, array($tag_id, $user_id));
426 $entries = $query->fetchAll();
431 public function retrieveTagsByEntry($entry_id) {
433 "SELECT tags.* FROM tags
434 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
435 WHERE tags_entries.entry_id = ?";
436 $query = $this->executeQuery($sql, array($entry_id));
437 $tags = $query->fetchAll();
442 public function removeTagForEntry($entry_id, $tag_id) {
443 $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?";
444 $params_action = array($tag_id, $entry_id);
445 $query = $this->executeQuery($sql_action, $params_action);
449 public function retrieveTagByValue($value) {
451 $sql = "SELECT * FROM tags WHERE value=?";
452 $params = array($value);
453 $query = $this->executeQuery($sql, $params);
454 $tag = $query->fetchAll();
456 return isset($tag[0]) ? $tag[0] : null;
459 public function createTag($value) {
460 $sql_action = 'INSERT INTO tags ( value ) VALUES (?)';
461 $params_action = array($value);
462 $query = $this->executeQuery($sql_action, $params_action);
466 public function setTagToEntry($tag_id, $entry_id) {
467 $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)';
468 $params_action = array($tag_id, $entry_id);
469 $query = $this->executeQuery($sql_action, $params_action);
473 private function getEntriesOrder() {
474 if (isset($_SESSION['sort']) and array_key_exists($_SESSION['sort'], $this->order
)) {
475 return $this->order
[$_SESSION['sort']];
478 return $this->order
['default'];