]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Database.class.php
3 * poche, a read it later open source system
6 * @author Nicolas LÅ“uillet <support@inthepoche.com>
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) {
169 $sql = "SELECT * FROM users WHERE username=? AND password=?";
170 $query = $this->executeQuery($sql, array($username, $password));
171 $login = $query->fetchAll();
174 if (isset($login[0])) {
175 $user['id'] = $login[0]['id'];
176 $user['username'] = $login[0]['username'];
177 $user['password'] = $login[0]['password'];
178 $user['name'] = $login[0]['name'];
179 $user['email'] = $login[0]['email'];
180 $user['config'] = $this->getConfigUser($login[0]['id']);
186 public function updatePassword($userId, $password)
188 $sql_update = "UPDATE users SET password=? WHERE id=?";
189 $params_update = array($password, $userId);
190 $query = $this->executeQuery($sql_update, $params_update);
193 public function updateUserConfig($userId, $key, $value) {
194 $config = $this->getConfigUser($userId);
196 if (!isset ($user_config[$key])) {
197 $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)";
200 $sql = "UPDATE users_config SET value=? WHERE user_id=? AND name=?";
203 $params = array($value, $userId, $key);
204 $query = $this->executeQuery($sql, $params);
207 private function executeQuery($sql, $params) {
210 $query = $this->getHandle()->prepare($sql);
211 $query->execute($params);
216 Tools
::logm('execute query error : '.$e->getMessage());
221 public function retrieveAll($user_id) {
222 $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id";
223 $query = $this->executeQuery($sql, array($user_id));
224 $entries = $query->fetchAll();
229 public function retrieveOneById($id, $user_id) {
231 $sql = "SELECT * FROM entries WHERE id=? AND user_id=?";
232 $params = array(intval($id), $user_id);
233 $query = $this->executeQuery($sql, $params);
234 $entry = $query->fetchAll();
236 return isset($entry[0]) ? $entry[0] : null;
239 public function getEntriesByView($view, $user_id, $limit = '') {
240 switch ($_SESSION['sort'])
243 $order = 'ORDER BY id';
246 $order = 'ORDER BY id DESC';
249 $order = 'ORDER BY lower(title)';
252 $order = 'ORDER BY lower(title) DESC';
255 $order = 'ORDER BY id';
262 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
263 $params = array($user_id, 1);
266 $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? " . $order;
267 $params = array($user_id, 1);
270 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
271 $params = array($user_id, 0);
275 $sql .= ' ' . $limit;
277 $query = $this->executeQuery($sql, $params);
278 $entries = $query->fetchAll();
283 public function updateContent($id, $content, $user_id) {
284 $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?';
285 $params_action = array($content, $id, $user_id);
286 $query = $this->executeQuery($sql_action, $params_action);
290 public function add($url, $title, $content, $user_id) {
291 $sql_action = 'INSERT INTO entries ( url, title, content, user_id ) VALUES (?, ?, ?, ?)';
292 $params_action = array($url, $title, $content, $user_id);
293 $query = $this->executeQuery($sql_action, $params_action);
297 public function deleteById($id, $user_id) {
298 $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?";
299 $params_action = array($id, $user_id);
300 $query = $this->executeQuery($sql_action, $params_action);
304 public function favoriteById($id, $user_id) {
305 $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?";
306 $params_action = array($id, $user_id);
307 $query = $this->executeQuery($sql_action, $params_action);
310 public function archiveById($id, $user_id) {
311 $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?";
312 $params_action = array($id, $user_id);
313 $query = $this->executeQuery($sql_action, $params_action);
316 public function getLastId($column = '') {
317 return $this->getHandle()->lastInsertId($column);
320 public function retrieveAllTags() {
321 $sql = "SELECT * FROM tags";
322 $query = $this->executeQuery($sql, array());
323 $tags = $query->fetchAll();
328 public function retrieveTag($id) {
330 $sql = "SELECT * FROM tags WHERE id=?";
331 $params = array(intval($id));
332 $query = $this->executeQuery($sql, $params);
333 $tag = $query->fetchAll();
335 return isset($tag[0]) ? $tag[0] : null;
338 public function retrieveEntriesByTag($tag_id) {
340 "SELECT entries.* FROM entries
341 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
342 WHERE tags_entries.tag_id = ?";
343 $query = $this->executeQuery($sql, array($tag_id));
344 $entries = $query->fetchAll();
349 public function retrieveTagsByEntry($entry_id) {
351 "SELECT tags.* FROM tags
352 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
353 WHERE tags_entries.entry_id = ?";
354 $query = $this->executeQuery($sql, array($entry_id));
355 $tags = $query->fetchAll();
360 public function removeTagForEntry($entry_id, $tag_id) {
361 $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?";
362 $params_action = array($tag_id, $entry_id);
363 $query = $this->executeQuery($sql_action, $params_action);
367 public function retrieveTagByValue($value) {
369 $sql = "SELECT * FROM tags WHERE value=?";
370 $params = array($value);
371 $query = $this->executeQuery($sql, $params);
372 $tag = $query->fetchAll();
374 return isset($tag[0]) ? $tag[0] : null;
377 public function createTag($value) {
378 $sql_action = 'INSERT INTO tags ( value ) VALUES (?)';
379 $params_action = array($value);
380 $query = $this->executeQuery($sql_action, $params_action);
384 public function setTagToEntry($tag_id, $entry_id) {
385 $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)';
386 $params_action = array($tag_id, $entry_id);
387 $query = $this->executeQuery($sql_action, $params_action);