]>
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 getEntriesByView($view, $user_id, $limit = '') {
245 switch ($_SESSION['sort'])
248 $order = 'ORDER BY id';
251 $order = 'ORDER BY id DESC';
254 $order = 'ORDER BY lower(title)';
257 $order = 'ORDER BY lower(title) DESC';
260 $order = 'ORDER BY id';
267 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
268 $params = array($user_id, 1);
271 $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? " . $order;
272 $params = array($user_id, 1);
275 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
276 $params = array($user_id, 0);
280 $sql .= ' ' . $limit;
282 $query = $this->executeQuery($sql, $params);
283 $entries = $query->fetchAll();
288 public function updateContent($id, $content, $user_id) {
289 $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?';
290 $params_action = array($content, $id, $user_id);
291 $query = $this->executeQuery($sql_action, $params_action);
295 public function add($url, $title, $content, $user_id) {
296 $sql_action = 'INSERT INTO entries ( url, title, content, user_id ) VALUES (?, ?, ?, ?)';
297 $params_action = array($url, $title, $content, $user_id);
298 $query = $this->executeQuery($sql_action, $params_action);
302 public function deleteById($id, $user_id) {
303 $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?";
304 $params_action = array($id, $user_id);
305 $query = $this->executeQuery($sql_action, $params_action);
309 public function favoriteById($id, $user_id) {
310 $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?";
311 $params_action = array($id, $user_id);
312 $query = $this->executeQuery($sql_action, $params_action);
315 public function archiveById($id, $user_id) {
316 $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?";
317 $params_action = array($id, $user_id);
318 $query = $this->executeQuery($sql_action, $params_action);
321 public function getLastId($column = '') {
322 return $this->getHandle()->lastInsertId($column);
325 public function retrieveAllTags() {
326 $sql = "SELECT * FROM tags";
327 $query = $this->executeQuery($sql, array());
328 $tags = $query->fetchAll();
333 public function retrieveTag($id) {
335 $sql = "SELECT * FROM tags WHERE id=?";
336 $params = array(intval($id));
337 $query = $this->executeQuery($sql, $params);
338 $tag = $query->fetchAll();
340 return isset($tag[0]) ? $tag[0] : null;
343 public function retrieveEntriesByTag($tag_id) {
345 "SELECT entries.* FROM entries
346 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
347 WHERE tags_entries.tag_id = ?";
348 $query = $this->executeQuery($sql, array($tag_id));
349 $entries = $query->fetchAll();
354 public function retrieveTagsByEntry($entry_id) {
356 "SELECT tags.* FROM tags
357 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
358 WHERE tags_entries.entry_id = ?";
359 $query = $this->executeQuery($sql, array($entry_id));
360 $tags = $query->fetchAll();
365 public function removeTagForEntry($entry_id, $tag_id) {
366 $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?";
367 $params_action = array($tag_id, $entry_id);
368 $query = $this->executeQuery($sql_action, $params_action);
372 public function retrieveTagByValue($value) {
374 $sql = "SELECT * FROM tags WHERE value=?";
375 $params = array($value);
376 $query = $this->executeQuery($sql, $params);
377 $tag = $query->fetchAll();
379 return isset($tag[0]) ? $tag[0] : null;
382 public function createTag($value) {
383 $sql_action = 'INSERT INTO tags ( value ) VALUES (?)';
384 $params_action = array($value);
385 $query = $this->executeQuery($sql_action, $params_action);
389 public function setTagToEntry($tag_id, $entry_id) {
390 $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)';
391 $params_action = array($tag_id, $entry_id);
392 $query = $this->executeQuery($sql_action, $params_action);