]>
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
);
37 die(STORAGE
. ' is not a recognised database system !');
40 $this->handle
->setAttribute(PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_EXCEPTION
);
41 Tools
::logm('storage type ' . STORAGE
);
44 private function getHandle() {
48 public function isInstalled() {
49 $sql = "SELECT username FROM users";
50 $query = $this->executeQuery($sql, array());
51 if ($query == false) {
52 die(STORAGE
. ' database looks empty. You have to create it (you can find database structure in install folder).');
54 $hasAdmin = count($query->fetchAll());
62 public function checkTags() {
64 if (STORAGE
== 'sqlite') {
66 CREATE TABLE IF NOT EXISTS tags (
67 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
71 elseif(STORAGE
== 'mysql') {
73 CREATE TABLE IF NOT EXISTS `tags` (
74 `id` int(11) NOT NULL AUTO_INCREMENT,
75 `value` varchar(255) NOT NULL,
77 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
82 CREATE TABLE IF NOT EXISTS tags (
83 id bigserial primary key,
84 value varchar(255) NOT NULL
89 $query = $this->executeQuery($sql, array());
91 if (STORAGE
== 'sqlite') {
93 CREATE TABLE IF NOT EXISTS tags_entries (
94 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
97 FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE,
98 FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE
101 elseif(STORAGE
== 'mysql') {
103 CREATE TABLE IF NOT EXISTS `tags_entries` (
104 `id` int(11) NOT NULL AUTO_INCREMENT,
105 `entry_id` int(11) NOT NULL,
106 `tag_id` int(11) NOT NULL,
107 FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE,
108 FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE,
110 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
115 CREATE TABLE IF NOT EXISTS tags_entries (
116 id bigserial primary key,
117 entry_id integer NOT NULL,
118 tag_id integer NOT NULL
123 $query = $this->executeQuery($sql, array());
126 public function install($login, $password) {
127 $sql = 'INSERT INTO users ( username, password, name, email) VALUES (?, ?, ?, ?)';
128 $params = array($login, $password, $login, ' ');
129 $query = $this->executeQuery($sql, $params);
132 if (STORAGE
== 'postgres') {
133 $sequence = 'users_id_seq';
136 $id_user = intval($this->getLastId($sequence));
138 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
139 $params = array($id_user, 'pager', PAGINATION
);
140 $query = $this->executeQuery($sql, $params);
142 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
143 $params = array($id_user, 'language', LANG
);
144 $query = $this->executeQuery($sql, $params);
146 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
147 $params = array($id_user, 'theme', DEFAULT_THEME
);
148 $query = $this->executeQuery($sql, $params);
153 public function getConfigUser($id) {
154 $sql = "SELECT * FROM users_config WHERE user_id = ?";
155 $query = $this->executeQuery($sql, array($id));
156 $result = $query->fetchAll();
157 $user_config = array();
159 foreach ($result as $key => $value) {
160 $user_config[$value['name']] = $value['value'];
166 public function userExists($username) {
167 $sql = "SELECT * FROM users WHERE username=?";
168 $query = $this->executeQuery($sql, array($username));
169 $login = $query->fetchAll();
170 if (isset($login[0])) {
177 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) {
208 $config = $this->getConfigUser($userId);
210 if (! isset($config[$key])) {
211 $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)";
214 $sql = "UPDATE users_config SET value=? WHERE user_id=? AND name=?";
217 $params = array($value, $userId, $key);
218 $query = $this->executeQuery($sql, $params);
221 private function executeQuery($sql, $params) {
224 $query = $this->getHandle()->prepare($sql);
225 $query->execute($params);
230 Tools
::logm('execute query error : '.$e->getMessage());
235 public function listUsers($username=null) {
236 $sql = 'SELECT count(*) FROM users'.( $username ? ' WHERE username=?' : '');
237 $query = $this->executeQuery($sql, ( $username ? array($username) : array()));
238 list($count) = $query->fetch();
242 public function getUserPassword($userID) {
243 $sql = "SELECT * FROM users WHERE id=?";
244 $query = $this->executeQuery($sql, array($userID));
245 $password = $query->fetchAll();
246 return isset($password[0]['password']) ? $password[0]['password'] : null;
249 public function deleteUserConfig($userID) {
250 $sql_action = 'DELETE from users_config WHERE user_id=?';
251 $params_action = array($userID);
252 $query = $this->executeQuery($sql_action, $params_action);
256 public function deleteTagsEntriesAndEntries($userID) {
257 $entries = $this->retrieveAll($userID);
258 foreach($entries as $entryid) {
259 $tags = $this->retrieveTagsByEntry($entryid);
260 foreach($tags as $tag) {
261 $this->removeTagForEntry($entryid,$tags);
263 $this->deleteById($entryid,$userID);
267 public function deleteUser($userID) {
268 $sql_action = 'DELETE from users WHERE id=?';
269 $params_action = array($userID);
270 $query = $this->executeQuery($sql_action, $params_action);
273 public function updateContentAndTitle($id, $title, $body, $user_id) {
274 $sql_action = 'UPDATE entries SET content = ?, title = ? WHERE id=? AND user_id=?';
275 $params_action = array($body, $title, $id, $user_id);
276 $query = $this->executeQuery($sql_action, $params_action);
280 public function retrieveUnfetchedEntries($user_id, $limit) {
282 $sql_limit = "LIMIT 0,".$limit;
283 if (STORAGE
== 'postgres') {
284 $sql_limit = "LIMIT ".$limit." OFFSET 0";
287 $sql = "SELECT * FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE 'Untitled - Import%' AND user_id=? ORDER BY id " . $sql_limit;
288 $query = $this->executeQuery($sql, array($user_id));
289 $entries = $query->fetchAll();
294 public function retrieveUnfetchedEntriesCount($user_id) {
295 $sql = "SELECT count(*) FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE 'Untitled - Import%' AND user_id=?";
296 $query = $this->executeQuery($sql, array($user_id));
297 list($count) = $query->fetch();
302 public function retrieveAll($user_id) {
303 $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id";
304 $query = $this->executeQuery($sql, array($user_id));
305 $entries = $query->fetchAll();
310 public function retrieveOneById($id, $user_id) {
312 $sql = "SELECT * FROM entries WHERE id=? AND user_id=?";
313 $params = array(intval($id), $user_id);
314 $query = $this->executeQuery($sql, $params);
315 $entry = $query->fetchAll();
317 return isset($entry[0]) ? $entry[0] : null;
320 public function retrieveOneByURL($url, $user_id) {
322 $sql = "SELECT * FROM entries WHERE url=? AND user_id=?";
323 $params = array($url, $user_id);
324 $query = $this->executeQuery($sql, $params);
325 $entry = $query->fetchAll();
327 return isset($entry[0]) ? $entry[0] : null;
330 public function reassignTags($old_entry_id, $new_entry_id) {
331 $sql = "UPDATE tags_entries SET entry_id=? WHERE entry_id=?";
332 $params = array($new_entry_id, $old_entry_id);
333 $query = $this->executeQuery($sql, $params);
336 public function getEntriesByView($view, $user_id, $limit = '', $tag_id = 0) {
339 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? ";
340 $params = array($user_id, 1);
343 $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? ";
344 $params = array($user_id, 1);
347 $sql = "SELECT entries.* FROM entries
348 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
349 WHERE entries.user_id=? AND tags_entries.tag_id = ? ";
350 $params = array($user_id, $tag_id);
353 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? ";
354 $params = array($user_id, 0);
358 $sql .= $this->getEntriesOrder().' ' . $limit;
360 $query = $this->executeQuery($sql, $params);
361 $entries = $query->fetchAll();
366 public function getEntriesByViewCount($view, $user_id, $tag_id = 0) {
369 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? ";
370 $params = array($user_id, 1);
373 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_fav=? ";
374 $params = array($user_id, 1);
377 $sql = "SELECT count(*) FROM entries
378 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
379 WHERE entries.user_id=? AND tags_entries.tag_id = ? ";
380 $params = array($user_id, $tag_id);
383 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? ";
384 $params = array($user_id, 0);
388 $query = $this->executeQuery($sql, $params);
389 list($count) = $query->fetch();
394 public function updateContent($id, $content, $user_id) {
395 $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?';
396 $params_action = array($content, $id, $user_id);
397 $query = $this->executeQuery($sql_action, $params_action);
404 * @param string $title
405 * @param string $content
406 * @param integer $user_id
407 * @return integer $id of inserted record
409 public function add($url, $title, $content, $user_id, $isFavorite=0, $isRead=0) {
410 $sql_action = 'INSERT INTO entries ( url, title, content, user_id, is_fav, is_read ) VALUES (?, ?, ?, ?, ?, ?)';
411 $params_action = array($url, $title, $content, $user_id, $isFavorite, $isRead);
412 if ( !$this->executeQuery($sql_action, $params_action) ) {
416 $id = intval($this->getLastId( (STORAGE
== 'postgres') ? 'entries_id_seq' : '') );
421 public function deleteById($id, $user_id) {
422 $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?";
423 $params_action = array($id, $user_id);
424 $query = $this->executeQuery($sql_action, $params_action);
428 public function favoriteById($id, $user_id) {
429 $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?";
430 $params_action = array($id, $user_id);
431 $query = $this->executeQuery($sql_action, $params_action);
434 public function archiveById($id, $user_id) {
435 $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?";
436 $params_action = array($id, $user_id);
437 $query = $this->executeQuery($sql_action, $params_action);
440 public function archiveAll($user_id) {
441 $sql_action = "UPDATE entries SET is_read=? WHERE user_id=? AND is_read=?";
442 $params_action = array($user_id, 1, 0);
443 $query = $this->executeQuery($sql_action, $params_action);
446 public function getLastId($column = '') {
447 return $this->getHandle()->lastInsertId($column);
450 public function search($term, $user_id, $limit = '') {
451 $search = '%'.$term.'%';
452 $sql_action = "SELECT * FROM entries WHERE user_id=? AND (content LIKE ? OR title LIKE ? OR url LIKE ?) "; //searches in content, title and URL
453 $sql_action .= $this->getEntriesOrder().' ' . $limit;
454 $params_action = array($user_id, $search, $search, $search);
455 $query = $this->executeQuery($sql_action, $params_action);
456 return $query->fetchAll();
459 public function retrieveAllTags($user_id, $term = null) {
460 $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags
461 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
462 LEFT JOIN entries ON tags_entries.entry_id=entries.id
463 WHERE entries.user_id=?
464 ". (($term) ? "AND lower(tags.value) LIKE ?" : '') ."
465 GROUP BY tags.id, tags.value
466 ORDER BY tags.value";
467 $query = $this->executeQuery($sql, (($term)? array($user_id, strtolower('%'.$term.'%')) : array($user_id) ));
468 $tags = $query->fetchAll();
473 public function retrieveTag($id, $user_id) {
475 $sql = "SELECT DISTINCT tags.* FROM tags
476 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
477 LEFT JOIN entries ON tags_entries.entry_id=entries.id
478 WHERE tags.id=? AND entries.user_id=?";
479 $params = array(intval($id), $user_id);
480 $query = $this->executeQuery($sql, $params);
481 $tag = $query->fetchAll();
483 return isset($tag[0]) ? $tag[0] : null;
486 public function retrieveEntriesByTag($tag_id, $user_id) {
488 "SELECT entries.* FROM entries
489 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
490 WHERE tags_entries.tag_id = ? AND entries.user_id=?";
491 $query = $this->executeQuery($sql, array($tag_id, $user_id));
492 $entries = $query->fetchAll();
497 public function retrieveTagsByEntry($entry_id) {
499 "SELECT tags.* FROM tags
500 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
501 WHERE tags_entries.entry_id = ?";
502 $query = $this->executeQuery($sql, array($entry_id));
503 $tags = $query->fetchAll();
508 public function removeTagForEntry($entry_id, $tag_id) {
509 $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?";
510 $params_action = array($tag_id, $entry_id);
511 $query = $this->executeQuery($sql_action, $params_action);
515 public function cleanUnusedTag($tag_id) {
516 $sql_action = "SELECT tags.* FROM tags JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
517 $query = $this->executeQuery($sql_action,array($tag_id));
518 $tagstokeep = $query->fetchAll();
519 $sql_action = "SELECT tags.* FROM tags LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
520 $query = $this->executeQuery($sql_action,array($tag_id));
521 $alltags = $query->fetchAll();
523 foreach ($alltags as $tag) {
524 if ($tag && !in_array($tag,$tagstokeep)) {
525 $sql_action = "DELETE FROM tags WHERE id=?";
526 $params_action = array($tag[0]);
527 $this->executeQuery($sql_action, $params_action);
534 public function retrieveTagByValue($value) {
536 $sql = "SELECT * FROM tags WHERE value=?";
537 $params = array($value);
538 $query = $this->executeQuery($sql, $params);
539 $tag = $query->fetchAll();
541 return isset($tag[0]) ? $tag[0] : null;
544 public function createTag($value) {
545 $sql_action = 'INSERT INTO tags ( value ) VALUES (?)';
546 $params_action = array($value);
547 $query = $this->executeQuery($sql_action, $params_action);
551 public function setTagToEntry($tag_id, $entry_id) {
552 $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)';
553 $params_action = array($tag_id, $entry_id);
554 $query = $this->executeQuery($sql_action, $params_action);
558 private function getEntriesOrder() {
559 if (isset($_SESSION['sort']) and array_key_exists($_SESSION['sort'], $this->order
)) {
560 return $this->order
[$_SESSION['sort']];
563 return $this->order
['default'];