]>
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
14 function __construct()
18 $db_path = 'sqlite:' . STORAGE_SQLITE
;
19 $this->handle
= new PDO($db_path);
22 $db_path = 'mysql:host=' . STORAGE_SERVER
. ';dbname=' . STORAGE_DB
;
23 $this->handle
= new PDO($db_path, STORAGE_USER
, STORAGE_PASSWORD
);
26 $db_path = 'pgsql:host=' . STORAGE_SERVER
. ';dbname=' . STORAGE_DB
;
27 $this->handle
= new PDO($db_path, STORAGE_USER
, STORAGE_PASSWORD
);
31 $this->handle
->setAttribute(PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_EXCEPTION
);
32 Tools
::logm('storage type ' . STORAGE
);
35 private function getHandle() {
39 public function isInstalled() {
40 $sql = "SELECT username FROM users";
41 $query = $this->executeQuery($sql, array());
42 if ($query == false) {
43 die(STORAGE
. ' database looks empty. You have to create it (you can find database structure in install folder).');
45 $hasAdmin = count($query->fetchAll());
53 public function checkTags() {
55 if (STORAGE
== 'sqlite') {
57 CREATE TABLE IF NOT EXISTS tags (
58 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
62 elseif(STORAGE
== 'mysql') {
64 CREATE TABLE IF NOT EXISTS `tags` (
65 `id` int(11) NOT NULL AUTO_INCREMENT,
66 `value` varchar(255) NOT NULL,
68 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
74 id bigserial primary key,
75 value varchar(255) NOT NULL
80 $query = $this->executeQuery($sql, array());
82 if (STORAGE
== 'sqlite') {
84 CREATE TABLE IF NOT EXISTS tags_entries (
85 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
88 FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE,
89 FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE
92 elseif(STORAGE
== 'mysql') {
94 CREATE TABLE IF NOT EXISTS `tags_entries` (
95 `id` int(11) NOT NULL AUTO_INCREMENT,
96 `entry_id` int(11) NOT NULL,
97 `tag_id` int(11) NOT NULL,
98 FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE,
99 FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE,
101 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
106 CREATE TABLE tags_entries (
107 id bigserial primary key,
108 entry_id integer NOT NULL,
109 tag_id integer NOT NULL
114 $query = $this->executeQuery($sql, array());
117 public function install($login, $password) {
118 $sql = 'INSERT INTO users ( username, password, name, email) VALUES (?, ?, ?, ?)';
119 $params = array($login, $password, $login, ' ');
120 $query = $this->executeQuery($sql, $params);
123 if (STORAGE
== 'postgres') {
124 $sequence = 'users_id_seq';
127 $id_user = intval($this->getLastId($sequence));
129 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
130 $params = array($id_user, 'pager', PAGINATION
);
131 $query = $this->executeQuery($sql, $params);
133 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
134 $params = array($id_user, 'language', LANG
);
135 $query = $this->executeQuery($sql, $params);
137 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
138 $params = array($id_user, 'theme', DEFAULT_THEME
);
139 $query = $this->executeQuery($sql, $params);
144 public function getConfigUser($id) {
145 $sql = "SELECT * FROM users_config WHERE user_id = ?";
146 $query = $this->executeQuery($sql, array($id));
147 $result = $query->fetchAll();
148 $user_config = array();
150 foreach ($result as $key => $value) {
151 $user_config[$value['name']] = $value['value'];
157 public function userExists($username) {
158 $sql = "SELECT * FROM users WHERE username=?";
159 $query = $this->executeQuery($sql, array($username));
160 $login = $query->fetchAll();
161 if (isset($login[0])) {
168 public function login($username, $password, $isauthenticated=false) {
169 if ($isauthenticated) {
170 $sql = "SELECT * FROM users WHERE username=?";
171 $query = $this->executeQuery($sql, array($username));
173 $sql = "SELECT * FROM users WHERE username=? AND password=?";
174 $query = $this->executeQuery($sql, array($username, $password));
176 $login = $query->fetchAll();
179 if (isset($login[0])) {
180 $user['id'] = $login[0]['id'];
181 $user['username'] = $login[0]['username'];
182 $user['password'] = $login[0]['password'];
183 $user['name'] = $login[0]['name'];
184 $user['email'] = $login[0]['email'];
185 $user['config'] = $this->getConfigUser($login[0]['id']);
191 public function updatePassword($userId, $password)
193 $sql_update = "UPDATE users SET password=? WHERE id=?";
194 $params_update = array($password, $userId);
195 $query = $this->executeQuery($sql_update, $params_update);
198 public function updateUserConfig($userId, $key, $value) {
199 $config = $this->getConfigUser($userId);
201 if (! isset($config[$key])) {
202 $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)";
205 $sql = "UPDATE users_config SET value=? WHERE user_id=? AND name=?";
208 $params = array($value, $userId, $key);
209 $query = $this->executeQuery($sql, $params);
212 private function executeQuery($sql, $params) {
215 $query = $this->getHandle()->prepare($sql);
216 $query->execute($params);
221 Tools
::logm('execute query error : '.$e->getMessage());
226 public function retrieveAll($user_id) {
227 $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id";
228 $query = $this->executeQuery($sql, array($user_id));
229 $entries = $query->fetchAll();
234 public function retrieveOneById($id, $user_id) {
236 $sql = "SELECT * FROM entries WHERE id=? AND user_id=?";
237 $params = array(intval($id), $user_id);
238 $query = $this->executeQuery($sql, $params);
239 $entry = $query->fetchAll();
241 return isset($entry[0]) ? $entry[0] : null;
244 public function retrieveOneByURL($url, $user_id) {
246 $sql = "SELECT * FROM entries WHERE url=? AND user_id=?";
247 $params = array($url, $user_id);
248 $query = $this->executeQuery($sql, $params);
249 $entry = $query->fetchAll();
251 return isset($entry[0]) ? $entry[0] : null;
254 public function reassignTags($old_entry_id, $new_entry_id) {
255 $sql = "UPDATE tags_entries SET entry_id=? WHERE entry_id=?";
256 $params = array($new_entry_id, $old_entry_id);
257 $query = $this->executeQuery($sql, $params);
260 public function getEntriesByView($view, $user_id, $limit = '') {
261 switch ($_SESSION['sort'])
264 $order = 'ORDER BY id';
267 $order = 'ORDER BY id DESC';
270 $order = 'ORDER BY lower(title)';
273 $order = 'ORDER BY lower(title) DESC';
276 $order = 'ORDER BY id';
283 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
284 $params = array($user_id, 1);
287 $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? " . $order;
288 $params = array($user_id, 1);
291 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
292 $params = array($user_id, 0);
296 $sql .= ' ' . $limit;
298 $query = $this->executeQuery($sql, $params);
299 $entries = $query->fetchAll();
304 public function updateContent($id, $content, $user_id) {
305 $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?';
306 $params_action = array($content, $id, $user_id);
307 $query = $this->executeQuery($sql_action, $params_action);
311 public function add($url, $title, $content, $user_id) {
312 $sql_action = 'INSERT INTO entries ( url, title, content, user_id ) VALUES (?, ?, ?, ?)';
313 $params_action = array($url, $title, $content, $user_id);
314 $query = $this->executeQuery($sql_action, $params_action);
318 public function deleteById($id, $user_id) {
319 $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?";
320 $params_action = array($id, $user_id);
321 $query = $this->executeQuery($sql_action, $params_action);
325 public function favoriteById($id, $user_id) {
326 $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?";
327 $params_action = array($id, $user_id);
328 $query = $this->executeQuery($sql_action, $params_action);
331 public function archiveById($id, $user_id) {
332 $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?";
333 $params_action = array($id, $user_id);
334 $query = $this->executeQuery($sql_action, $params_action);
337 public function archiveAll($user_id) {
338 $sql_action = "UPDATE entries SET is_read=? WHERE user_id=? AND is_read=?";
339 $params_action = array($user_id, 1, 0);
340 $query = $this->executeQuery($sql_action, $params_action);
343 public function getLastId($column = '') {
344 return $this->getHandle()->lastInsertId($column);
347 public function retrieveAllTags() {
348 $sql = "SELECT * FROM tags";
349 $query = $this->executeQuery($sql, array());
350 $tags = $query->fetchAll();
355 public function retrieveTag($id) {
357 $sql = "SELECT * FROM tags WHERE id=?";
358 $params = array(intval($id));
359 $query = $this->executeQuery($sql, $params);
360 $tag = $query->fetchAll();
362 return isset($tag[0]) ? $tag[0] : null;
365 public function retrieveEntriesByTag($tag_id) {
367 "SELECT entries.* FROM entries
368 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
369 WHERE tags_entries.tag_id = ?";
370 $query = $this->executeQuery($sql, array($tag_id));
371 $entries = $query->fetchAll();
376 public function retrieveTagsByEntry($entry_id) {
378 "SELECT tags.* FROM tags
379 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
380 WHERE tags_entries.entry_id = ?";
381 $query = $this->executeQuery($sql, array($entry_id));
382 $tags = $query->fetchAll();
387 public function removeTagForEntry($entry_id, $tag_id) {
388 $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?";
389 $params_action = array($tag_id, $entry_id);
390 $query = $this->executeQuery($sql_action, $params_action);
394 public function retrieveTagByValue($value) {
396 $sql = "SELECT * FROM tags WHERE value=?";
397 $params = array($value);
398 $query = $this->executeQuery($sql, $params);
399 $tag = $query->fetchAll();
401 return isset($tag[0]) ? $tag[0] : null;
404 public function createTag($value) {
405 $sql_action = 'INSERT INTO tags ( value ) VALUES (?)';
406 $params_action = array($value);
407 $query = $this->executeQuery($sql_action, $params_action);
411 public function setTagToEntry($tag_id, $entry_id) {
412 $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)';
413 $params_action = array($tag_id, $entry_id);
414 $query = $this->executeQuery($sql_action, $params_action);