]>
Commit | Line | Data |
---|---|---|
14890de3 | 1 | <?php |
2 | /** | |
c95b78a8 | 3 | * wallabag, self hostable application allowing you to not miss any content anymore |
14890de3 | 4 | * |
c95b78a8 NL |
5 | * @category wallabag |
6 | * @author Nicolas Lœuillet <nicolas@loeuillet.org> | |
14890de3 | 7 | * @copyright 2013 |
3602405e | 8 | * @license http://opensource.org/licenses/MIT see COPYING file |
14890de3 | 9 | */ |
10 | ||
bc1ee852 | 11 | class Database { |
b3cda72e | 12 | |
14890de3 | 13 | var $handle; |
4e067cea NL |
14 | private $order = array ( |
15 | 'ia' => 'ORDER BY entries.id', | |
16 | 'id' => 'ORDER BY entries.id DESC', | |
17 | 'ta' => 'ORDER BY lower(entries.title)', | |
18 | 'td' => 'ORDER BY lower(entries.title) DESC', | |
19 | 'default' => 'ORDER BY entries.id' | |
032e0ca1 MR |
20 | ); |
21 | ||
182faf26 | 22 | function __construct() |
68857cea NL |
23 | { |
24 | switch (STORAGE) { | |
25 | case 'sqlite': | |
211068ce | 26 | // Check if /db is writeable |
b668db24 | 27 | if ( !is_writable(STORAGE_SQLITE) || !is_writable(dirname(STORAGE_SQLITE))) { |
211068ce MR |
28 | die('An error occured: "db" directory must be writeable for your web server user!'); |
29 | } | |
68857cea NL |
30 | $db_path = 'sqlite:' . STORAGE_SQLITE; |
31 | $this->handle = new PDO($db_path); | |
32 | break; | |
33 | case 'mysql': | |
054c9d88 TC |
34 | if (MYSQL_USE_UTF8MB4) { |
35 | $db_path = 'mysql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB . ';charset=utf8mb4'; | |
36 | $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD, array( | |
37 | PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4', | |
38 | )); | |
39 | } else { | |
40 | $db_path = 'mysql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB; | |
41 | $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); | |
42 | } | |
68857cea NL |
43 | break; |
44 | case 'postgres': | |
45 | $db_path = 'pgsql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB; | |
182faf26 | 46 | $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); |
68857cea | 47 | break; |
8af31ae0 | 48 | default: |
49 | die(STORAGE . ' is not a recognised database system !'); | |
68857cea NL |
50 | } |
51 | ||
14890de3 | 52 | $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
3602405e | 53 | $this->_checkTags(); |
580d60b9 | 54 | Tools::logm('storage type ' . STORAGE); |
14890de3 | 55 | } |
56 | ||
b3cda72e NL |
57 | private function getHandle() |
58 | { | |
14890de3 | 59 | return $this->handle; |
60 | } | |
61 | ||
b3cda72e NL |
62 | private function _checkTags() |
63 | { | |
5cfafc61 NL |
64 | |
65 | if (STORAGE == 'sqlite') { | |
66 | $sql = ' | |
67 | CREATE TABLE IF NOT EXISTS tags ( | |
68 | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, | |
69 | value TEXT | |
70 | )'; | |
71 | } | |
72 | elseif(STORAGE == 'mysql') { | |
73 | $sql = ' | |
74 | CREATE TABLE IF NOT EXISTS `tags` ( | |
75 | `id` int(11) NOT NULL AUTO_INCREMENT, | |
76 | `value` varchar(255) NOT NULL, | |
77 | PRIMARY KEY (`id`) | |
78 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
79 | '; | |
80 | } | |
81 | else { | |
82 | $sql = ' | |
5ce39784 | 83 | CREATE TABLE IF NOT EXISTS tags ( |
5cfafc61 NL |
84 | id bigserial primary key, |
85 | value varchar(255) NOT NULL | |
86 | ); | |
87 | '; | |
88 | } | |
aa8c9f2a | 89 | |
5cfafc61 NL |
90 | $query = $this->executeQuery($sql, array()); |
91 | ||
92 | if (STORAGE == 'sqlite') { | |
93 | $sql = ' | |
a562e390 | 94 | CREATE TABLE IF NOT EXISTS tags_entries ( |
5cfafc61 NL |
95 | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, |
96 | entry_id INTEGER, | |
97 | tag_id INTEGER, | |
98 | FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE, | |
99 | FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE | |
100 | )'; | |
101 | } | |
102 | elseif(STORAGE == 'mysql') { | |
103 | $sql = ' | |
104 | CREATE TABLE IF NOT EXISTS `tags_entries` ( | |
105 | `id` int(11) NOT NULL AUTO_INCREMENT, | |
106 | `entry_id` int(11) NOT NULL, | |
107 | `tag_id` int(11) NOT NULL, | |
108 | FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE, | |
109 | FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE, | |
110 | PRIMARY KEY (`id`) | |
111 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
112 | '; | |
113 | } | |
114 | else { | |
115 | $sql = ' | |
5ce39784 | 116 | CREATE TABLE IF NOT EXISTS tags_entries ( |
5cfafc61 NL |
117 | id bigserial primary key, |
118 | entry_id integer NOT NULL, | |
119 | tag_id integer NOT NULL | |
120 | ) | |
121 | '; | |
122 | } | |
123 | ||
124 | $query = $this->executeQuery($sql, array()); | |
aa8c9f2a NL |
125 | } |
126 | ||
046b9316 | 127 | public function install($login, $password, $email = '') |
b3cda72e | 128 | { |
b916bcfc | 129 | $sql = 'INSERT INTO users ( username, password, name, email) VALUES (?, ?, ?, ?)'; |
046b9316 | 130 | $params = array($login, $password, $login, $email); |
b916bcfc NL |
131 | $query = $this->executeQuery($sql, $params); |
132 | ||
133 | $sequence = ''; | |
134 | if (STORAGE == 'postgres') { | |
135 | $sequence = 'users_id_seq'; | |
136 | } | |
137 | ||
138 | $id_user = intval($this->getLastId($sequence)); | |
139 | ||
140 | $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; | |
00dbaf90 | 141 | $params = array($id_user, 'pager', PAGINATION); |
b916bcfc NL |
142 | $query = $this->executeQuery($sql, $params); |
143 | ||
144 | $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; | |
00dbaf90 NL |
145 | $params = array($id_user, 'language', LANG); |
146 | $query = $this->executeQuery($sql, $params); | |
182faf26 | 147 | |
00dbaf90 NL |
148 | $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; |
149 | $params = array($id_user, 'theme', DEFAULT_THEME); | |
7ce7ec4c | 150 | $query = $this->executeQuery($sql, $params); |
aa8c9f2a | 151 | |
7ce7ec4c NL |
152 | return TRUE; |
153 | } | |
aa8c9f2a | 154 | |
b3cda72e NL |
155 | public function getConfigUser($id) |
156 | { | |
7ce7ec4c NL |
157 | $sql = "SELECT * FROM users_config WHERE user_id = ?"; |
158 | $query = $this->executeQuery($sql, array($id)); | |
159 | $result = $query->fetchAll(); | |
160 | $user_config = array(); | |
182faf26 | 161 | |
7ce7ec4c NL |
162 | foreach ($result as $key => $value) { |
163 | $user_config[$value['name']] = $value['value']; | |
aa8c9f2a NL |
164 | } |
165 | ||
7ce7ec4c | 166 | return $user_config; |
aa8c9f2a NL |
167 | } |
168 | ||
b3cda72e NL |
169 | public function userExists($username) |
170 | { | |
027b4e15 DS |
171 | $sql = "SELECT * FROM users WHERE username=?"; |
172 | $query = $this->executeQuery($sql, array($username)); | |
173 | $login = $query->fetchAll(); | |
174 | if (isset($login[0])) { | |
175 | return true; | |
176 | } else { | |
177 | return false; | |
178 | } | |
179 | } | |
180 | ||
b3cda72e NL |
181 | public function login($username, $password, $isauthenticated = FALSE) |
182 | { | |
6af66b11 | 183 | if ($isauthenticated) { |
4e067cea NL |
184 | $sql = "SELECT * FROM users WHERE username=?"; |
185 | $query = $this->executeQuery($sql, array($username)); | |
6af66b11 | 186 | } else { |
4e067cea NL |
187 | $sql = "SELECT * FROM users WHERE username=? AND password=?"; |
188 | $query = $this->executeQuery($sql, array($username, $password)); | |
6af66b11 | 189 | } |
8d3275be | 190 | $login = $query->fetchAll(); |
aa8c9f2a | 191 | |
7ce7ec4c NL |
192 | $user = array(); |
193 | if (isset($login[0])) { | |
194 | $user['id'] = $login[0]['id']; | |
195 | $user['username'] = $login[0]['username']; | |
196 | $user['password'] = $login[0]['password']; | |
197 | $user['name'] = $login[0]['name']; | |
198 | $user['email'] = $login[0]['email']; | |
199 | $user['config'] = $this->getConfigUser($login[0]['id']); | |
200 | } | |
aa8c9f2a | 201 | |
7ce7ec4c | 202 | return $user; |
aa8c9f2a NL |
203 | } |
204 | ||
00dbaf90 | 205 | public function updatePassword($userId, $password) |
6499b26a | 206 | { |
8d3275be | 207 | $sql_update = "UPDATE users SET password=? WHERE id=?"; |
1e98ee1d NL |
208 | $params_update = array($password, $userId); |
209 | $query = $this->executeQuery($sql_update, $params_update); | |
00dbaf90 | 210 | } |
182faf26 | 211 | |
b3cda72e NL |
212 | public function updateUserConfig($userId, $key, $value) |
213 | { | |
58ace494 | 214 | $config = $this->getConfigUser($userId); |
182faf26 | 215 | |
d1d3498b | 216 | if (! isset($config[$key])) { |
da5fc42f | 217 | $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)"; |
58ace494 NL |
218 | } |
219 | else { | |
da5fc42f | 220 | $sql = "UPDATE users_config SET value=? WHERE user_id=? AND name=?"; |
58ace494 NL |
221 | } |
222 | ||
223 | $params = array($value, $userId, $key); | |
224 | $query = $this->executeQuery($sql, $params); | |
6499b26a NL |
225 | } |
226 | ||
b3cda72e NL |
227 | private function executeQuery($sql, $params) |
228 | { | |
14890de3 | 229 | try |
230 | { | |
231 | $query = $this->getHandle()->prepare($sql); | |
232 | $query->execute($params); | |
233 | return $query; | |
234 | } | |
235 | catch (Exception $e) | |
236 | { | |
eb1af592 | 237 | Tools::logm('execute query error : '.$e->getMessage()); |
bc1ee852 | 238 | return FALSE; |
14890de3 | 239 | } |
240 | } | |
4d99bae8 | 241 | |
b3cda72e NL |
242 | public function listUsers($username = NULL) |
243 | { | |
4d99bae8 | 244 | $sql = 'SELECT count(*) FROM users'.( $username ? ' WHERE username=?' : ''); |
245 | $query = $this->executeQuery($sql, ( $username ? array($username) : array())); | |
246 | list($count) = $query->fetch(); | |
247 | return $count; | |
248 | } | |
249 | ||
b3cda72e NL |
250 | public function getUserPassword($userID) |
251 | { | |
4d99bae8 | 252 | $sql = "SELECT * FROM users WHERE id=?"; |
253 | $query = $this->executeQuery($sql, array($userID)); | |
254 | $password = $query->fetchAll(); | |
255 | return isset($password[0]['password']) ? $password[0]['password'] : null; | |
256 | } | |
257 | ||
b3cda72e NL |
258 | public function deleteUserConfig($userID) |
259 | { | |
4d99bae8 | 260 | $sql_action = 'DELETE from users_config WHERE user_id=?'; |
261 | $params_action = array($userID); | |
262 | $query = $this->executeQuery($sql_action, $params_action); | |
263 | return $query; | |
264 | } | |
265 | ||
b3cda72e NL |
266 | public function deleteTagsEntriesAndEntries($userID) |
267 | { | |
4d99bae8 | 268 | $entries = $this->retrieveAll($userID); |
269 | foreach($entries as $entryid) { | |
270 | $tags = $this->retrieveTagsByEntry($entryid); | |
271 | foreach($tags as $tag) { | |
272 | $this->removeTagForEntry($entryid,$tags); | |
273 | } | |
274 | $this->deleteById($entryid,$userID); | |
275 | } | |
276 | } | |
277 | ||
b3cda72e NL |
278 | public function deleteUser($userID) |
279 | { | |
4d99bae8 | 280 | $sql_action = 'DELETE from users WHERE id=?'; |
281 | $params_action = array($userID); | |
282 | $query = $this->executeQuery($sql_action, $params_action); | |
283 | } | |
14890de3 | 284 | |
b3cda72e NL |
285 | public function updateContentAndTitle($id, $title, $body, $user_id) |
286 | { | |
53e3158d NL |
287 | $sql_action = 'UPDATE entries SET content = ?, title = ? WHERE id=? AND user_id=?'; |
288 | $params_action = array($body, $title, $id, $user_id); | |
289 | $query = $this->executeQuery($sql_action, $params_action); | |
53e3158d NL |
290 | return $query; |
291 | } | |
292 | ||
b3cda72e NL |
293 | public function retrieveUnfetchedEntries($user_id, $limit) |
294 | { | |
53e3158d NL |
295 | |
296 | $sql_limit = "LIMIT 0,".$limit; | |
297 | if (STORAGE == 'postgres') { | |
298 | $sql_limit = "LIMIT ".$limit." OFFSET 0"; | |
299 | } | |
300 | ||
fde4cf06 | 301 | $sql = "SELECT * FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE '%Import%' AND user_id=? ORDER BY id " . $sql_limit; |
53e3158d NL |
302 | $query = $this->executeQuery($sql, array($user_id)); |
303 | $entries = $query->fetchAll(); | |
304 | ||
305 | return $entries; | |
306 | } | |
307 | ||
b3cda72e NL |
308 | public function retrieveUnfetchedEntriesCount($user_id) |
309 | { | |
fde4cf06 | 310 | $sql = "SELECT count(*) FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE '%Import%' AND user_id=?"; |
182faf26 MR |
311 | $query = $this->executeQuery($sql, array($user_id)); |
312 | list($count) = $query->fetch(); | |
313 | ||
314 | return $count; | |
315 | } | |
316 | ||
b3cda72e NL |
317 | public function retrieveAll($user_id) |
318 | { | |
182faf26 | 319 | $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id"; |
8d3275be | 320 | $query = $this->executeQuery($sql, array($user_id)); |
44e77bfa | 321 | $entries = $query->fetchAll(); |
322 | ||
323 | return $entries; | |
324 | } | |
325 | ||
b3cda72e NL |
326 | public function retrieveOneById($id, $user_id) |
327 | { | |
14890de3 | 328 | $entry = NULL; |
8d3275be NL |
329 | $sql = "SELECT * FROM entries WHERE id=? AND user_id=?"; |
330 | $params = array(intval($id), $user_id); | |
14890de3 | 331 | $query = $this->executeQuery($sql, $params); |
332 | $entry = $query->fetchAll(); | |
333 | ||
0c2f4537 | 334 | return isset($entry[0]) ? $entry[0] : null; |
14890de3 | 335 | } |
336 | ||
b3cda72e NL |
337 | public function retrieveOneByURL($url, $user_id) |
338 | { | |
488fc63b | 339 | $entry = NULL; |
182faf26 | 340 | $sql = "SELECT * FROM entries WHERE url=? AND user_id=?"; |
488fc63b MR |
341 | $params = array($url, $user_id); |
342 | $query = $this->executeQuery($sql, $params); | |
343 | $entry = $query->fetchAll(); | |
344 | ||
345 | return isset($entry[0]) ? $entry[0] : null; | |
346 | } | |
347 | ||
b3cda72e NL |
348 | public function reassignTags($old_entry_id, $new_entry_id) |
349 | { | |
488fc63b MR |
350 | $sql = "UPDATE tags_entries SET entry_id=? WHERE entry_id=?"; |
351 | $params = array($new_entry_id, $old_entry_id); | |
352 | $query = $this->executeQuery($sql, $params); | |
353 | } | |
354 | ||
b3cda72e NL |
355 | public function getEntriesByView($view, $user_id, $limit = '', $tag_id = 0) |
356 | { | |
032e0ca1 MR |
357 | switch ($view) { |
358 | case 'archive': | |
182faf26 | 359 | $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? "; |
032e0ca1 | 360 | $params = array($user_id, 1); |
14890de3 | 361 | break; |
032e0ca1 | 362 | case 'fav' : |
182faf26 | 363 | $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? "; |
032e0ca1 | 364 | $params = array($user_id, 1); |
14890de3 | 365 | break; |
032e0ca1 MR |
366 | case 'tag' : |
367 | $sql = "SELECT entries.* FROM entries | |
368 | LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id | |
182faf26 | 369 | WHERE entries.user_id=? AND tags_entries.tag_id = ? "; |
032e0ca1 | 370 | $params = array($user_id, $tag_id); |
14890de3 | 371 | break; |
372 | default: | |
182faf26 | 373 | $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? "; |
032e0ca1 | 374 | $params = array($user_id, 0); |
14890de3 | 375 | break; |
376 | } | |
377 | ||
032e0ca1 MR |
378 | $sql .= $this->getEntriesOrder().' ' . $limit; |
379 | ||
380 | $query = $this->executeQuery($sql, $params); | |
381 | $entries = $query->fetchAll(); | |
382 | ||
383 | return $entries; | |
b3cda72e | 384 | } |
032e0ca1 | 385 | |
b3cda72e NL |
386 | public function getEntriesByViewCount($view, $user_id, $tag_id = 0) |
387 | { | |
53e3158d | 388 | switch ($view) { |
14890de3 | 389 | case 'archive': |
182faf26 | 390 | $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? "; |
bc1ee852 | 391 | $params = array($user_id, 1); |
14890de3 | 392 | break; |
393 | case 'fav' : | |
182faf26 | 394 | $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_fav=? "; |
bc1ee852 | 395 | $params = array($user_id, 1); |
14890de3 | 396 | break; |
53e3158d NL |
397 | case 'tag' : |
398 | $sql = "SELECT count(*) FROM entries | |
399 | LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id | |
182faf26 | 400 | WHERE entries.user_id=? AND tags_entries.tag_id = ? "; |
53e3158d NL |
401 | $params = array($user_id, $tag_id); |
402 | break; | |
14890de3 | 403 | default: |
182faf26 | 404 | $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? "; |
8d3275be | 405 | $params = array($user_id, 0); |
14890de3 | 406 | break; |
407 | } | |
408 | ||
8d3275be | 409 | $query = $this->executeQuery($sql, $params); |
032e0ca1 | 410 | list($count) = $query->fetch(); |
14890de3 | 411 | |
53e3158d | 412 | return $count; |
14890de3 | 413 | } |
f76dab12 | 414 | public function getRandomId($user_id, $view) { |
cdee5e65 | 415 | $random = (STORAGE == 'mysql') ? 'RAND()' : 'RANDOM()'; |
f76dab12 TC |
416 | switch ($view) { |
417 | case 'archive': | |
418 | $sql = "SELECT id FROM entries WHERE user_id=? AND is_read=? ORDER BY ". $random . " LIMIT 1"; | |
419 | $params = array($user_id,1); | |
420 | break; | |
421 | case 'fav': | |
422 | $sql = "SELECT id FROM entries WHERE user_id=? AND is_fav=? ORDER BY ". $random . " LIMIT 1"; | |
423 | $params = array($user_id,1); | |
424 | break; | |
425 | default: | |
426 | $sql = "SELECT id FROM entries WHERE user_id=? AND is_read=? ORDER BY ". $random . " LIMIT 1"; | |
427 | $params = array($user_id,0); | |
428 | break; | |
429 | } | |
cdee5e65 TC |
430 | $query = $this->executeQuery($sql, $params); |
431 | $id = $query->fetchAll(); | |
6b607416 | 432 | |
cdee5e65 | 433 | return $id; |
6b607416 TC |
434 | } |
435 | ||
059a3380 TC |
436 | public function getPreviousArticle($id, $user_id) |
437 | { | |
7f8f8271 | 438 | $sql = "SELECT id FROM entries WHERE id = (SELECT max(id) FROM entries WHERE id < ? AND is_read=0) AND user_id=? AND is_read=0"; |
059a3380 TC |
439 | $params = array($id, $user_id); |
440 | $query = $this->executeQuery($sql, $params); | |
441 | $id_entry = $query->fetchAll(); | |
442 | $id = $id_entry[0][0]; | |
443 | return $id; | |
444 | } | |
445 | ||
446 | public function getNextArticle($id, $user_id) | |
447 | { | |
7f8f8271 | 448 | $sql = "SELECT id FROM entries WHERE id = (SELECT min(id) FROM entries WHERE id > ? AND is_read=0) AND user_id=? AND is_read=0"; |
059a3380 TC |
449 | $params = array($id, $user_id); |
450 | $query = $this->executeQuery($sql, $params); | |
451 | $id_entry = $query->fetchAll(); | |
452 | $id = $id_entry[0][0]; | |
453 | return $id; | |
454 | } | |
455 | ||
14890de3 | 456 | |
b3cda72e NL |
457 | public function updateContent($id, $content, $user_id) |
458 | { | |
b58e261d NL |
459 | $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?'; |
460 | $params_action = array($content, $id, $user_id); | |
461 | $query = $this->executeQuery($sql_action, $params_action); | |
462 | return $query; | |
463 | } | |
464 | ||
182faf26 MR |
465 | /** |
466 | * | |
467 | * @param string $url | |
468 | * @param string $title | |
469 | * @param string $content | |
470 | * @param integer $user_id | |
471 | * @return integer $id of inserted record | |
472 | */ | |
b3cda72e NL |
473 | public function add($url, $title, $content, $user_id, $isFavorite=0, $isRead=0) |
474 | { | |
6400371f NL |
475 | $sql_action = 'INSERT INTO entries ( url, title, content, user_id, is_fav, is_read ) VALUES (?, ?, ?, ?, ?, ?)'; |
476 | $params_action = array($url, $title, $content, $user_id, $isFavorite, $isRead); | |
5425b0dd | 477 | |
182faf26 MR |
478 | if ( !$this->executeQuery($sql_action, $params_action) ) { |
479 | $id = null; | |
480 | } | |
481 | else { | |
1bcbe8be | 482 | $id = intval($this->getLastId( (STORAGE == 'postgres') ? 'entries_id_seq' : '') ); |
182faf26 MR |
483 | } |
484 | return $id; | |
14890de3 | 485 | } |
486 | ||
b3cda72e NL |
487 | public function deleteById($id, $user_id) |
488 | { | |
8d3275be NL |
489 | $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?"; |
490 | $params_action = array($id, $user_id); | |
14890de3 | 491 | $query = $this->executeQuery($sql_action, $params_action); |
29c6fd46 | 492 | return $query; |
14890de3 | 493 | } |
494 | ||
b3cda72e NL |
495 | public function favoriteById($id, $user_id) |
496 | { | |
bc1ee852 | 497 | $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?"; |
8d3275be | 498 | $params_action = array($id, $user_id); |
14890de3 | 499 | $query = $this->executeQuery($sql_action, $params_action); |
500 | } | |
501 | ||
b3cda72e NL |
502 | public function archiveById($id, $user_id) |
503 | { | |
bc1ee852 | 504 | $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?"; |
8d3275be | 505 | $params_action = array($id, $user_id); |
14890de3 | 506 | $query = $this->executeQuery($sql_action, $params_action); |
507 | } | |
508 | ||
b3cda72e NL |
509 | public function archiveAll($user_id) |
510 | { | |
f14807de NL |
511 | $sql_action = "UPDATE entries SET is_read=? WHERE user_id=? AND is_read=?"; |
512 | $params_action = array($user_id, 1, 0); | |
513 | $query = $this->executeQuery($sql_action, $params_action); | |
514 | } | |
515 | ||
b3cda72e NL |
516 | public function getLastId($column = '') |
517 | { | |
b916bcfc | 518 | return $this->getHandle()->lastInsertId($column); |
14890de3 | 519 | } |
5ce39784 | 520 | |
b3cda72e NL |
521 | public function search($term, $user_id, $limit = '') |
522 | { | |
a4585f7e MR |
523 | $search = '%'.$term.'%'; |
524 | $sql_action = "SELECT * FROM entries WHERE user_id=? AND (content LIKE ? OR title LIKE ? OR url LIKE ?) "; //searches in content, title and URL | |
525 | $sql_action .= $this->getEntriesOrder().' ' . $limit; | |
526 | $params_action = array($user_id, $search, $search, $search); | |
527 | $query = $this->executeQuery($sql_action, $params_action); | |
528 | return $query->fetchAll(); | |
529 | } | |
5bea1a4d | 530 | |
b3cda72e NL |
531 | public function retrieveAllTags($user_id, $term = NULL) |
532 | { | |
fb26cc93 | 533 | $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags |
b89d5a2b NL |
534 | LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id |
535 | LEFT JOIN entries ON tags_entries.entry_id=entries.id | |
182faf26 | 536 | WHERE entries.user_id=? |
fb26cc93 MR |
537 | ". (($term) ? "AND lower(tags.value) LIKE ?" : '') ." |
538 | GROUP BY tags.id, tags.value | |
539 | ORDER BY tags.value"; | |
540 | $query = $this->executeQuery($sql, (($term)? array($user_id, strtolower('%'.$term.'%')) : array($user_id) )); | |
2e2ebe5e NL |
541 | $tags = $query->fetchAll(); |
542 | ||
543 | return $tags; | |
544 | } | |
545 | ||
b3cda72e NL |
546 | public function retrieveTag($id, $user_id) |
547 | { | |
4886ed6d | 548 | $tag = NULL; |
e83cf5a7 | 549 | $sql = "SELECT DISTINCT tags.* FROM tags |
b89d5a2b NL |
550 | LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id |
551 | LEFT JOIN entries ON tags_entries.entry_id=entries.id | |
182faf26 | 552 | WHERE tags.id=? AND entries.user_id=?"; |
b89d5a2b | 553 | $params = array(intval($id), $user_id); |
4886ed6d NL |
554 | $query = $this->executeQuery($sql, $params); |
555 | $tag = $query->fetchAll(); | |
556 | ||
b3cda72e | 557 | return isset($tag[0]) ? $tag[0] : NULL; |
4886ed6d NL |
558 | } |
559 | ||
b3cda72e NL |
560 | public function retrieveEntriesByTag($tag_id, $user_id) |
561 | { | |
182faf26 | 562 | $sql = |
9de34d4e | 563 | "SELECT entries.* FROM entries |
4886ed6d | 564 | LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id |
25052a76 | 565 | WHERE tags_entries.tag_id = ? AND entries.user_id=? ORDER by entries.id DESC"; |
b89d5a2b | 566 | $query = $this->executeQuery($sql, array($tag_id, $user_id)); |
4886ed6d NL |
567 | $entries = $query->fetchAll(); |
568 | ||
569 | return $entries; | |
570 | } | |
571 | ||
b3cda72e NL |
572 | public function retrieveTagsByEntry($entry_id) |
573 | { | |
182faf26 | 574 | $sql = |
9de34d4e | 575 | "SELECT tags.* FROM tags |
5bea1a4d NL |
576 | LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id |
577 | WHERE tags_entries.entry_id = ?"; | |
2e2ebe5e NL |
578 | $query = $this->executeQuery($sql, array($entry_id)); |
579 | $tags = $query->fetchAll(); | |
5bea1a4d NL |
580 | |
581 | return $tags; | |
582 | } | |
c432fa16 | 583 | |
b3cda72e NL |
584 | public function removeTagForEntry($entry_id, $tag_id) |
585 | { | |
c432fa16 NL |
586 | $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?"; |
587 | $params_action = array($tag_id, $entry_id); | |
588 | $query = $this->executeQuery($sql_action, $params_action); | |
589 | return $query; | |
590 | } | |
9c743ab9 | 591 | |
b3cda72e NL |
592 | public function cleanUnusedTag($tag_id) |
593 | { | |
24696800 | 594 | $sql_action = "SELECT tags.* FROM tags JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?"; |
595 | $query = $this->executeQuery($sql_action,array($tag_id)); | |
9c743ab9 | 596 | $tagstokeep = $query->fetchAll(); |
24696800 | 597 | $sql_action = "SELECT tags.* FROM tags LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?"; |
598 | $query = $this->executeQuery($sql_action,array($tag_id)); | |
9c743ab9 | 599 | $alltags = $query->fetchAll(); |
24696800 | 600 | |
9c743ab9 | 601 | foreach ($alltags as $tag) { |
602 | if ($tag && !in_array($tag,$tagstokeep)) { | |
9c743ab9 | 603 | $sql_action = "DELETE FROM tags WHERE id=?"; |
604 | $params_action = array($tag[0]); | |
24696800 | 605 | $this->executeQuery($sql_action, $params_action); |
606 | return true; | |
9c743ab9 | 607 | } |
608 | } | |
24696800 | 609 | |
9c743ab9 | 610 | } |
c432fa16 | 611 | |
b3cda72e NL |
612 | public function retrieveTagByValue($value) |
613 | { | |
c432fa16 NL |
614 | $tag = NULL; |
615 | $sql = "SELECT * FROM tags WHERE value=?"; | |
616 | $params = array($value); | |
617 | $query = $this->executeQuery($sql, $params); | |
618 | $tag = $query->fetchAll(); | |
619 | ||
620 | return isset($tag[0]) ? $tag[0] : null; | |
621 | } | |
622 | ||
b3cda72e NL |
623 | public function createTag($value) |
624 | { | |
c432fa16 NL |
625 | $sql_action = 'INSERT INTO tags ( value ) VALUES (?)'; |
626 | $params_action = array($value); | |
627 | $query = $this->executeQuery($sql_action, $params_action); | |
628 | return $query; | |
629 | } | |
630 | ||
b3cda72e NL |
631 | public function setTagToEntry($tag_id, $entry_id) |
632 | { | |
c432fa16 NL |
633 | $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)'; |
634 | $params_action = array($tag_id, $entry_id); | |
635 | $query = $this->executeQuery($sql_action, $params_action); | |
636 | return $query; | |
637 | } | |
032e0ca1 | 638 | |
b3cda72e NL |
639 | private function getEntriesOrder() |
640 | { | |
641 | if (isset($_SESSION['sort']) and array_key_exists($_SESSION['sort'], $this->order)) { | |
642 | return $this->order[$_SESSION['sort']]; | |
032e0ca1 | 643 | } |
b3cda72e NL |
644 | else { |
645 | return $this->order['default']; | |
646 | } | |
647 | } | |
2cd263e0 | 648 | } |