|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ownCloud - bookmarks plugin |
|
5
|
|
|
* |
|
6
|
|
|
* @author Arthur Schiwon |
|
7
|
|
|
* @copyright 2011 Arthur Schiwon [email protected] |
|
8
|
|
|
* |
|
9
|
|
|
* This library is free software; you can redistribute it and/or |
|
10
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
|
11
|
|
|
* License as published by the Free Software Foundation; either |
|
12
|
|
|
* version 3 of the License, or any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This library is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public |
|
20
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
/** |
|
24
|
|
|
* This class manages bookmarks |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace OCA\Bookmarks\Controller\Lib; |
|
28
|
|
|
|
|
29
|
|
|
use \OCP\IDb; |
|
30
|
|
|
|
|
31
|
|
|
class Bookmarks { |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @brief Finds all tags for bookmarks |
|
35
|
|
|
* @param $userId UserId |
|
36
|
|
|
* @param IDb $db Database Interface |
|
37
|
|
|
* @param filterTags array of tag to look for if empty then every tag |
|
38
|
|
|
* @param offset integer offset |
|
39
|
|
|
* @param limit integer of item to return |
|
40
|
|
|
* @return Found Tags |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public static function findTags($userId, IDb $db, $filterTags = array(), $offset = 0, $limit = -1) { |
|
43
|
1 |
|
$params = array_merge($filterTags, $filterTags); |
|
44
|
1 |
|
array_unshift($params, $userId); |
|
45
|
1 |
|
$not_in = ''; |
|
46
|
1 |
|
if (!empty($filterTags)) { |
|
47
|
|
|
$exist_clause = " AND exists (select 1 from `*PREFIX*bookmarks_tags` |
|
48
|
|
|
`t2` where `t2`.`bookmark_id` = `t`.`bookmark_id` and `tag` = ?) "; |
|
49
|
|
|
|
|
50
|
|
|
$not_in = ' AND `tag` not in (' . implode(',', array_fill(0, count($filterTags), '?')) . ')' . |
|
51
|
|
|
str_repeat($exist_clause, count($filterTags)); |
|
52
|
|
|
} |
|
53
|
|
|
$sql = 'SELECT tag, count(*) as nbr from *PREFIX*bookmarks_tags t ' . |
|
54
|
1 |
|
' WHERE EXISTS( SELECT 1 from *PREFIX*bookmarks bm where t.bookmark_id = bm.id and user_id = ?) ' . |
|
55
|
1 |
|
$not_in . |
|
56
|
1 |
|
' GROUP BY `tag` ORDER BY `nbr` DESC '; |
|
57
|
|
|
|
|
58
|
1 |
|
$query = $db->prepareQuery($sql, $limit, $offset); |
|
59
|
1 |
|
$tags = $query->execute($params)->fetchAll(); |
|
60
|
1 |
|
return $tags; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @brief Finds Bookmark with certain ID |
|
65
|
|
|
* @param $id BookmarkId |
|
66
|
|
|
* @param $userId UserId |
|
67
|
|
|
* @param IDb $db Database Interface |
|
68
|
|
|
* @return Specific Bookmark |
|
69
|
|
|
*/ |
|
70
|
2 |
|
public static function findUniqueBookmark($id, $userId, IDb $db) { |
|
71
|
2 |
|
$CONFIG_DBTYPE = \OCP\Config::getSystemValue('dbtype', 'sqlite'); |
|
72
|
2 |
|
if ($CONFIG_DBTYPE == 'pgsql') { |
|
73
|
|
|
$group_fct = 'array_agg(`tag`)'; |
|
74
|
|
|
} else { |
|
75
|
2 |
|
$group_fct = 'GROUP_CONCAT(`tag`)'; |
|
76
|
|
|
} |
|
77
|
|
|
$sql = "SELECT *, (select $group_fct from `*PREFIX*bookmarks_tags` where `bookmark_id` = `b`.`id`) as `tags` |
|
78
|
|
|
FROM `*PREFIX*bookmarks` `b` |
|
79
|
2 |
|
WHERE `user_id` = ? AND `id` = ?"; |
|
80
|
2 |
|
$query = $db->prepareQuery($sql); |
|
81
|
2 |
|
$result = $query->execute(array($userId, $id))->fetchRow(); |
|
82
|
2 |
|
$result['tags'] = explode(',', $result['tags']); |
|
83
|
2 |
|
return $result; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @brief Check if an URL is bookmarked |
|
88
|
|
|
* @param $url Url of a possible bookmark |
|
89
|
|
|
* @param $userId UserId |
|
90
|
|
|
* @param IDb $db Database Interface |
|
91
|
|
|
* @return boolean if the url is already bookmarked |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public static function bookmarkExists($url, $userId, IDb $db) { |
|
94
|
1 |
|
$enc_url = htmlspecialchars_decode($url); |
|
95
|
1 |
|
$sql = "SELECT id from `*PREFIX*bookmarks` where `url` = ? and `user_id` = ?"; |
|
96
|
1 |
|
$query = $db->prepareQuery($sql); |
|
97
|
1 |
|
$result = $query->execute(array($enc_url, $userId))->fetchRow(); |
|
98
|
1 |
|
if ($result) { |
|
99
|
1 |
|
return $result['id']; |
|
100
|
|
|
} else { |
|
101
|
1 |
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @brief Finds all bookmarks, matching the filter |
|
107
|
|
|
* @param $userid UserId |
|
108
|
|
|
* @param IDb $db Database Interface |
|
109
|
|
|
* @param int $offset offset |
|
110
|
|
|
* @param string $sqlSortColumn result with this column |
|
111
|
|
|
* @param string|array $filters filters can be: empty -> no filter, a string -> filter this, a string array -> filter for all strings |
|
112
|
|
|
* @param bool $filterTagOnly true, filter affects only tags, else filter affects url, title and tags |
|
113
|
|
|
* @param int $limit limit of items to return (default 10) if -1 or false then all items are returned |
|
114
|
|
|
* @param bool $public check if only public bookmarks should be returned |
|
115
|
|
|
* @param array $requestedAttributes select all the attributes that should be returned. default is * + tags |
|
116
|
|
|
* @param string $tagFilterConjunction select wether the filterTagOnly should filter with an AND or an OR conjunction |
|
117
|
|
|
* @return Collection of specified bookmarks |
|
118
|
|
|
*/ |
|
119
|
3 |
|
public static function findBookmarks( |
|
120
|
|
|
$userid, IDb $db, $offset, $sqlSortColumn, $filters, $filterTagOnly, $limit = 10, $public = false, $requestedAttributes = null, $tagFilterConjunction = "and") { |
|
121
|
|
|
|
|
122
|
3 |
|
$CONFIG_DBTYPE = \OCP\Config::getSystemValue('dbtype', 'sqlite'); |
|
123
|
3 |
|
if (is_string($filters)) { |
|
124
|
|
|
$filters = array($filters); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
3 |
|
$toSelect = '*'; |
|
128
|
3 |
|
$tableAttributes = array('id', 'url', 'title', 'user_id', 'description', |
|
129
|
3 |
|
'public', 'added', 'lastmodified', 'clickcount',); |
|
130
|
|
|
|
|
131
|
3 |
|
$returnTags = true; |
|
132
|
|
|
|
|
133
|
3 |
|
if ($requestedAttributes != null) { |
|
134
|
|
|
|
|
135
|
1 |
|
$key = array_search('tags', $requestedAttributes); |
|
136
|
1 |
|
if ($key == false) { |
|
137
|
1 |
|
$returnTags = false; |
|
138
|
1 |
|
} else { |
|
139
|
|
|
unset($requestedAttributes[$key]); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
1 |
|
$toSelect = implode(",", array_intersect($tableAttributes, $requestedAttributes)); |
|
143
|
1 |
|
} |
|
144
|
|
|
|
|
145
|
3 |
|
if ($CONFIG_DBTYPE == 'pgsql') { |
|
146
|
|
|
$sql = "SELECT " . $toSelect . " FROM (SELECT *, (select array_to_string(array_agg(`tag`),',') |
|
147
|
|
|
from `*PREFIX*bookmarks_tags` where `bookmark_id` = `b2`.`id`) as `tags` |
|
148
|
|
|
FROM `*PREFIX*bookmarks` `b2` |
|
149
|
|
|
WHERE `user_id` = ? ) as `b` WHERE true "; |
|
150
|
|
|
} else { |
|
151
|
3 |
|
$sql = "SELECT " . $toSelect . ", (SELECT GROUP_CONCAT(`tag`) from `*PREFIX*bookmarks_tags` |
|
152
|
|
|
WHERE `bookmark_id` = `b`.`id`) as `tags` |
|
153
|
|
|
FROM `*PREFIX*bookmarks` `b` |
|
154
|
3 |
|
WHERE `user_id` = ? "; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
3 |
|
$params = array($userid); |
|
158
|
|
|
|
|
159
|
3 |
|
if ($public) { |
|
160
|
1 |
|
$sql .= ' AND public = 1 '; |
|
161
|
1 |
|
} |
|
162
|
|
|
|
|
163
|
3 |
|
if (count($filters) > 0) { |
|
164
|
2 |
|
Bookmarks::findBookmarksBuildFilter($sql, $params, $filters, $filterTagOnly, $tagFilterConjunction, $CONFIG_DBTYPE); |
|
165
|
2 |
|
} |
|
166
|
|
|
|
|
167
|
3 |
|
if (!in_array($sqlSortColumn, $tableAttributes)) { |
|
168
|
1 |
|
$sqlSortColumn = 'lastmodified'; |
|
169
|
1 |
|
} |
|
170
|
3 |
|
$sql .= " ORDER BY " . $sqlSortColumn . " DESC "; |
|
171
|
3 |
|
if ($limit == -1 || $limit === false) { |
|
172
|
3 |
|
$limit = null; |
|
173
|
3 |
|
$offset = null; |
|
174
|
3 |
|
} |
|
175
|
|
|
|
|
176
|
3 |
|
$query = $db->prepareQuery($sql, $limit, $offset); |
|
177
|
3 |
|
$results = $query->execute($params)->fetchAll(); |
|
178
|
3 |
|
$bookmarks = array(); |
|
179
|
3 |
|
foreach ($results as $result) { |
|
180
|
3 |
|
if ($returnTags) { |
|
181
|
2 |
|
$result['tags'] = explode(',', $result['tags']); |
|
182
|
2 |
|
} else { |
|
183
|
1 |
|
unset($result['tags']); |
|
184
|
|
|
} |
|
185
|
3 |
|
$bookmarks[] = $result; |
|
186
|
3 |
|
} |
|
187
|
3 |
|
return $bookmarks; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
2 |
|
private static function findBookmarksBuildFilter(&$sql, &$params, $filters, $filterTagOnly, $tagFilterConjunction, $CONFIG_DBTYPE) { |
|
191
|
2 |
|
$tagOrSearch = false; |
|
192
|
2 |
|
$connectWord = 'AND'; |
|
193
|
|
|
|
|
194
|
2 |
|
if ($tagFilterConjunction == 'or') { |
|
195
|
1 |
|
$tagOrSearch = true; |
|
196
|
1 |
|
$connectWord = 'OR'; |
|
197
|
1 |
|
} |
|
198
|
|
|
|
|
199
|
2 |
|
if ($filterTagOnly) { |
|
200
|
2 |
|
if ($tagOrSearch) { |
|
201
|
1 |
|
$sql .= 'AND ('; |
|
202
|
1 |
|
} else { |
|
203
|
1 |
|
$sql .= 'AND'; |
|
204
|
|
|
} |
|
205
|
|
|
$exist_clause = " exists (SELECT `id` FROM `*PREFIX*bookmarks_tags` |
|
206
|
2 |
|
`t2` WHERE `t2`.`bookmark_id` = `b`.`id` AND `tag` = ?) "; |
|
207
|
2 |
|
$sql .= str_repeat($exist_clause . $connectWord, count($filters)); |
|
208
|
2 |
|
if ($tagOrSearch) { |
|
209
|
1 |
|
$sql = rtrim($sql, 'OR'); |
|
210
|
1 |
|
$sql .= ')'; |
|
211
|
1 |
|
} else { |
|
212
|
1 |
|
$sql = rtrim($sql, 'AND'); |
|
213
|
|
|
} |
|
214
|
2 |
|
$params = array_merge($params, $filters); |
|
215
|
2 |
|
} else { |
|
216
|
|
|
if ($CONFIG_DBTYPE == 'mysql') { //Dirty hack to allow usage of alias in where |
|
217
|
|
|
$sql .= ' HAVING true '; |
|
218
|
|
|
} |
|
219
|
|
|
foreach ($filters as $filter) { |
|
220
|
|
|
if ($CONFIG_DBTYPE == 'mysql') { |
|
221
|
|
|
$sql .= ' AND lower( concat(url,title,description,IFNULL(tags,\'\') )) like ? '; |
|
222
|
|
|
} else { |
|
223
|
|
|
$sql .= ' AND lower(url || title || description || ifnull(tags,\'\') ) like ? '; |
|
224
|
|
|
} |
|
225
|
|
|
$params[] = '%' . strtolower($filter) . '%'; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
2 |
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @brief Delete bookmark with specific id |
|
232
|
|
|
* @param $userId UserId |
|
233
|
|
|
* @param IDb $db Database Interface |
|
234
|
|
|
* @param $id Bookmark ID to delete |
|
235
|
|
|
* @return boolean Success of operation |
|
236
|
|
|
*/ |
|
237
|
1 |
|
public static function deleteUrl($userId, IDb $db, $id) { |
|
238
|
1 |
|
$user = $userId; |
|
239
|
|
|
|
|
240
|
1 |
|
$query = $db->prepareQuery(" |
|
241
|
|
|
SELECT `id` FROM `*PREFIX*bookmarks` |
|
242
|
|
|
WHERE `id` = ? |
|
243
|
|
|
AND `user_id` = ? |
|
244
|
1 |
|
"); |
|
245
|
|
|
|
|
246
|
1 |
|
$result = $query->execute(array($id, $user)); |
|
247
|
1 |
|
$id = $result->fetchOne(); |
|
248
|
1 |
|
if ($id === false) { |
|
249
|
|
|
return false; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
1 |
|
$query = $db->prepareQuery(" |
|
253
|
|
|
DELETE FROM `*PREFIX*bookmarks` |
|
254
|
|
|
WHERE `id` = ? |
|
255
|
1 |
|
"); |
|
256
|
|
|
|
|
257
|
1 |
|
$query->execute(array($id)); |
|
258
|
|
|
|
|
259
|
1 |
|
$query = $db->prepareQuery(" |
|
260
|
|
|
DELETE FROM `*PREFIX*bookmarks_tags` |
|
261
|
|
|
WHERE `bookmark_id` = ? |
|
262
|
1 |
|
"); |
|
263
|
|
|
|
|
264
|
1 |
|
$query->execute(array($id)); |
|
265
|
1 |
|
return true; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @brief Rename a tag |
|
270
|
|
|
* @param $userId UserId |
|
271
|
|
|
* @param IDb $db Database Interface |
|
272
|
|
|
* @param string $old Old Tag Name |
|
273
|
|
|
* @param string $new New Tag Name |
|
274
|
|
|
* @return boolean Success of operation |
|
275
|
|
|
*/ |
|
276
|
|
|
public static function renameTag($userId, IDb $db, $old, $new) { |
|
277
|
|
|
$user_id = $userId; |
|
278
|
|
|
$CONFIG_DBTYPE = \OCP\Config::getSystemValue('dbtype', 'sqlite'); |
|
279
|
|
|
|
|
280
|
|
|
|
|
281
|
|
|
if ($CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3') { |
|
282
|
|
|
// Update tags to the new label unless it already exists a tag like this |
|
283
|
|
|
$query = $db->prepareQuery(" |
|
284
|
|
|
UPDATE OR REPLACE `*PREFIX*bookmarks_tags` |
|
285
|
|
|
SET `tag` = ? |
|
286
|
|
|
WHERE `tag` = ? |
|
287
|
|
|
AND exists( select `b`.`id` from `*PREFIX*bookmarks` `b` |
|
288
|
|
|
WHERE `b`.`user_id` = ? AND `bookmark_id` = `b`.`id`) |
|
289
|
|
|
"); |
|
290
|
|
|
|
|
291
|
|
|
$params = array( |
|
292
|
|
|
$new, |
|
293
|
|
|
$old, |
|
294
|
|
|
$user_id, |
|
295
|
|
|
); |
|
296
|
|
|
|
|
297
|
|
|
$query->execute($params); |
|
298
|
|
|
} else { |
|
299
|
|
|
|
|
300
|
|
|
// Remove potentialy duplicated tags |
|
301
|
|
|
$query = $db->prepareQuery(" |
|
302
|
|
|
DELETE FROM `*PREFIX*bookmarks_tags` as `tgs` WHERE `tgs`.`tag` = ? |
|
303
|
|
|
AND exists( SELECT `id` FROM `*PREFIX*bookmarks` WHERE `user_id` = ? |
|
304
|
|
|
AND `tgs`.`bookmark_id` = `id`) |
|
305
|
|
|
AND exists( SELECT `t`.`tag` FROM `*PREFIX*bookmarks_tags` `t` where `t`.`tag` = ? |
|
306
|
|
|
AND `tgs`.`bookmark_id` = `t`.`bookmark_id`)"); |
|
307
|
|
|
|
|
308
|
|
|
$params = array( |
|
309
|
|
|
$new, |
|
310
|
|
|
$user_id, |
|
311
|
|
|
$new |
|
312
|
|
|
); |
|
313
|
|
|
|
|
314
|
|
|
$query->execute($params); |
|
315
|
|
|
|
|
316
|
|
|
|
|
317
|
|
|
// Update tags to the new label unless it already exists a tag like this |
|
318
|
|
|
$query = $db->prepareQuery(" |
|
319
|
|
|
UPDATE `*PREFIX*bookmarks_tags` |
|
320
|
|
|
SET `tag` = ? |
|
321
|
|
|
WHERE `tag` = ? |
|
322
|
|
|
AND exists( SELECT `b`.`id` FROM `*PREFIX*bookmarks` `b` |
|
323
|
|
|
WHERE `b`.`user_id` = ? AND `bookmark_id` = `b`.`id`) |
|
324
|
|
|
"); |
|
325
|
|
|
|
|
326
|
|
|
$params = array( |
|
327
|
|
|
$new, |
|
328
|
|
|
$old, |
|
329
|
|
|
$user_id |
|
330
|
|
|
); |
|
331
|
|
|
|
|
332
|
|
|
$query->execute($params); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
|
|
336
|
|
|
return true; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* @brief Delete a tag |
|
341
|
|
|
* @param $userid UserId |
|
342
|
|
|
* @param IDb $db Database Interface |
|
343
|
|
|
* @param string $old Tag Name to delete |
|
344
|
|
|
* @return boolean Success of operation |
|
345
|
|
|
*/ |
|
346
|
|
|
public static function deleteTag($userid, IDb $db, $old) { |
|
347
|
|
|
|
|
348
|
|
|
// Update the record |
|
349
|
|
|
$query = $db->prepareQuery(" |
|
350
|
|
|
DELETE FROM `*PREFIX*bookmarks_tags` |
|
351
|
|
|
WHERE `tag` = ? |
|
352
|
|
|
AND exists( SELECT `id` FROM `*PREFIX*bookmarks` WHERE `user_id` = ? AND `bookmark_id` = `id`) |
|
353
|
|
|
"); |
|
354
|
|
|
|
|
355
|
|
|
$params = array( |
|
356
|
|
|
$old, |
|
357
|
|
|
$userid, |
|
358
|
|
|
); |
|
359
|
|
|
|
|
360
|
|
|
$result = $query->execute($params); |
|
361
|
|
|
return $result; |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* Edit a bookmark |
|
366
|
|
|
* @param $userid UserId |
|
367
|
|
|
* @param IDb $db Database Interface |
|
368
|
|
|
* @param int $id The id of the bookmark to edit |
|
369
|
|
|
* @param string $url The url to set |
|
370
|
|
|
* @param string $title Name of the bookmark |
|
371
|
|
|
* @param array $tags Simple array of tags to qualify the bookmark (different tags are taken from values) |
|
372
|
|
|
* @param string $description A longer description about the bookmark |
|
373
|
|
|
* @param boolean $is_public True if the bookmark is publishable to not registered users |
|
374
|
|
|
* @return null |
|
375
|
|
|
*/ |
|
376
|
1 |
|
public static function editBookmark($userid, IDb $db, $id, $url, $title, $tags = array(), $description = '', $is_public = false) { |
|
377
|
|
|
|
|
378
|
1 |
|
$is_public = $is_public ? 1 : 0; |
|
379
|
1 |
|
$user_id = $userid; |
|
380
|
|
|
|
|
381
|
|
|
// Update the record |
|
382
|
1 |
|
$query = $db->prepareQuery(" |
|
383
|
|
|
UPDATE `*PREFIX*bookmarks` SET |
|
384
|
|
|
`url` = ?, `title` = ?, `public` = ?, `description` = ?, |
|
385
|
|
|
`lastmodified` = UNIX_TIMESTAMP() |
|
386
|
|
|
WHERE `id` = ? |
|
387
|
|
|
AND `user_id` = ? |
|
388
|
1 |
|
"); |
|
389
|
|
|
|
|
390
|
|
|
$params = array( |
|
391
|
1 |
|
htmlspecialchars_decode($url), |
|
392
|
1 |
|
htmlspecialchars_decode($title), |
|
393
|
1 |
|
$is_public, |
|
394
|
1 |
|
htmlspecialchars_decode($description), |
|
395
|
1 |
|
$id, |
|
396
|
1 |
|
$user_id, |
|
397
|
1 |
|
); |
|
398
|
|
|
|
|
399
|
1 |
|
$result = $query->execute($params); |
|
400
|
|
|
|
|
401
|
|
|
// Abort the operation if bookmark couldn't be set |
|
402
|
|
|
// (probably because the user is not allowed to edit this bookmark) |
|
403
|
1 |
|
if ($result == 0) |
|
404
|
1 |
|
exit(); |
|
405
|
|
|
|
|
406
|
|
|
|
|
407
|
|
|
// Remove old tags |
|
408
|
1 |
|
$sql = "DELETE FROM `*PREFIX*bookmarks_tags` WHERE `bookmark_id` = ?"; |
|
409
|
1 |
|
$query = $db->prepareQuery($sql); |
|
410
|
1 |
|
$query->execute(array($id)); |
|
411
|
|
|
|
|
412
|
|
|
// Add New Tags |
|
413
|
1 |
|
self::addTags($db, $id, $tags); |
|
414
|
|
|
|
|
415
|
1 |
|
return $id; |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* Add a bookmark |
|
420
|
|
|
* @param $userid UserId |
|
421
|
|
|
* @param IDb $db Database Interface |
|
422
|
|
|
* @param string $url |
|
423
|
|
|
* @param string $title Name of the bookmark |
|
424
|
|
|
* @param array $tags Simple array of tags to qualify the bookmark (different tags are taken from values) |
|
425
|
|
|
* @param string $description A longer description about the bookmark |
|
426
|
|
|
* @param boolean $public True if the bookmark is publishable to not registered users |
|
|
|
|
|
|
427
|
|
|
* @return int The id of the bookmark created |
|
428
|
|
|
*/ |
|
429
|
8 |
|
public static function addBookmark($userid, IDb $db, $url, $title, $tags = array(), $description = '', $is_public = false) { |
|
430
|
8 |
|
$public = $is_public ? 1 : 0; |
|
431
|
8 |
|
$url_without_prefix = substr($url, strpos($url, "://") + 3); // Removes everything from the url before the "://" pattern (included) |
|
432
|
8 |
|
$enc_url_noprefix = htmlspecialchars_decode($url_without_prefix); |
|
433
|
8 |
|
$enc_url = htmlspecialchars_decode($url); |
|
434
|
|
|
// Change lastmodified date if the record if already exists |
|
435
|
8 |
|
$sql = "SELECT * from `*PREFIX*bookmarks` WHERE `url` like ? AND `user_id` = ?"; |
|
436
|
8 |
|
$query = $db->prepareQuery($sql, 1); |
|
437
|
8 |
|
$result = $query->execute(array('%'.$enc_url_noprefix, $userid)); // Find url in the db independantly from its protocol |
|
438
|
8 |
|
if ($row = $result->fetchRow()) { |
|
439
|
|
|
$params = array(); |
|
440
|
|
|
$title_str = ''; |
|
441
|
|
|
if (trim($title) != '') { // Do we replace the old title |
|
442
|
|
|
$title_str = ' , title = ?'; |
|
443
|
|
|
$params[] = $title; |
|
444
|
|
|
} |
|
445
|
|
|
$desc_str = ''; |
|
446
|
|
|
if (trim($description) != '') { // Do we replace the old description |
|
447
|
|
|
$desc_str = ' , description = ?'; |
|
448
|
|
|
$params[] = $description; |
|
449
|
|
|
} |
|
450
|
|
|
$sql = "UPDATE `*PREFIX*bookmarks` SET `lastmodified` = " |
|
451
|
|
|
. "UNIX_TIMESTAMP() $title_str $desc_str , `url` = ? WHERE `url` like ? and `user_id` = ?"; |
|
452
|
|
|
$params[] = $enc_url; |
|
453
|
|
|
$params[] = '%'.$enc_url_noprefix; |
|
454
|
|
|
$params[] = $userid; |
|
455
|
|
|
$query = $db->prepareQuery($sql); |
|
456
|
|
|
$query->execute($params); |
|
457
|
|
|
return $row['id']; |
|
458
|
|
|
} else { |
|
459
|
8 |
|
$query = $db->prepareQuery(" |
|
460
|
|
|
INSERT INTO `*PREFIX*bookmarks` |
|
461
|
|
|
(`url`, `title`, `user_id`, `public`, `added`, `lastmodified`, `description`) |
|
462
|
|
|
VALUES (?, ?, ?, ?, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), ?) |
|
463
|
8 |
|
"); |
|
464
|
|
|
|
|
465
|
|
|
$params = array( |
|
466
|
8 |
|
$enc_url, |
|
467
|
8 |
|
htmlspecialchars_decode($title), |
|
468
|
8 |
|
$userid, |
|
469
|
8 |
|
$public, |
|
470
|
8 |
|
$description, |
|
471
|
8 |
|
); |
|
472
|
8 |
|
$query->execute($params); |
|
473
|
|
|
|
|
474
|
8 |
|
$b_id = $db->getInsertId('*PREFIX*bookmarks'); |
|
475
|
|
|
|
|
476
|
8 |
|
if ($b_id !== false) { |
|
477
|
8 |
|
self::addTags($db, $b_id, $tags); |
|
478
|
8 |
|
return $b_id; |
|
479
|
|
|
} |
|
480
|
|
|
} |
|
481
|
|
|
} |
|
482
|
|
|
|
|
483
|
|
|
/** |
|
484
|
|
|
* @brief Add a set of tags for a bookmark |
|
485
|
|
|
* @param IDb $db Database Interface |
|
486
|
|
|
* @param int $bookmarkID The bookmark reference |
|
487
|
|
|
* @param array $tags Set of tags to add to the bookmark |
|
488
|
|
|
* @return null |
|
489
|
|
|
* */ |
|
490
|
8 |
|
private static function addTags(IDb $db, $bookmarkID, $tags) { |
|
491
|
8 |
|
$sql = 'INSERT INTO `*PREFIX*bookmarks_tags` (`bookmark_id`, `tag`) select ?, ? '; |
|
492
|
8 |
|
$dbtype = \OCP\Config::getSystemValue('dbtype', 'sqlite'); |
|
493
|
|
|
|
|
494
|
8 |
|
if ($dbtype === 'mysql') { |
|
495
|
|
|
$sql .= 'from dual '; |
|
496
|
|
|
} |
|
497
|
8 |
|
$sql .= 'where not exists(select * from `*PREFIX*bookmarks_tags` where `bookmark_id` = ? and `tag` = ?)'; |
|
498
|
|
|
|
|
499
|
8 |
|
$query = $db->prepareQuery($sql); |
|
500
|
8 |
|
foreach ($tags as $tag) { |
|
501
|
8 |
|
$tag = trim($tag); |
|
502
|
8 |
|
if (empty($tag)) { |
|
503
|
|
|
//avoid saving white spaces |
|
504
|
|
|
continue; |
|
505
|
|
|
} |
|
506
|
8 |
|
$params = array($bookmarkID, $tag, $bookmarkID, $tag); |
|
507
|
8 |
|
$query->execute($params); |
|
508
|
8 |
|
} |
|
509
|
8 |
|
} |
|
510
|
|
|
|
|
511
|
|
|
/** |
|
512
|
|
|
* @brief Import Bookmarks from html formatted file |
|
513
|
|
|
* @param $user User imported Bookmarks should belong to |
|
514
|
|
|
* @param IDb $db Database Interface |
|
515
|
|
|
* @param $file Content to import |
|
516
|
|
|
* @return null |
|
517
|
|
|
* */ |
|
518
|
|
|
public static function importFile($user, IDb $db, $file) { |
|
519
|
|
|
libxml_use_internal_errors(true); |
|
520
|
|
|
$dom = new \domDocument(); |
|
521
|
|
|
|
|
522
|
|
|
$dom->loadHTMLFile($file); |
|
523
|
|
|
$links = $dom->getElementsByTagName('a'); |
|
524
|
|
|
|
|
525
|
|
|
// Reintroduce transaction here!? |
|
526
|
|
|
foreach ($links as $link) { |
|
527
|
|
|
$title = $link->nodeValue; |
|
528
|
|
|
$ref = $link->getAttribute("href"); |
|
529
|
|
|
$tag_str = ''; |
|
530
|
|
|
if ($link->hasAttribute("tags")) |
|
531
|
|
|
$tag_str = $link->getAttribute("tags"); |
|
532
|
|
|
$tags = explode(',', $tag_str); |
|
533
|
|
|
|
|
534
|
|
|
$desc_str = ''; |
|
535
|
|
|
if ($link->hasAttribute("description")) |
|
536
|
|
|
$desc_str = $link->getAttribute("description"); |
|
537
|
|
|
|
|
538
|
|
|
self::addBookmark($user, $db, $ref, $title, $tags, $desc_str); |
|
539
|
|
|
} |
|
540
|
|
|
|
|
541
|
|
|
return array(); |
|
542
|
|
|
} |
|
543
|
|
|
|
|
544
|
|
|
/** |
|
545
|
|
|
* @brief Load Url and receive Metadata (Title) |
|
546
|
|
|
* @param $url Url to load and analyze |
|
547
|
|
|
* @return array Metadata for url; |
|
548
|
|
|
* */ |
|
549
|
1 |
|
public static function getURLMetadata($url) { |
|
550
|
|
|
|
|
551
|
1 |
|
$metadata = array(); |
|
552
|
1 |
|
$metadata['url'] = $url; |
|
553
|
1 |
|
$page = ""; |
|
554
|
|
|
|
|
555
|
|
|
try { |
|
556
|
1 |
|
$request = \OC::$server->getHTTPClientService()->newClient()->get($url); |
|
557
|
1 |
|
$page = $request->getBody(); |
|
558
|
1 |
|
$contentType = $request->getHeader('Content-Type'); |
|
559
|
1 |
|
} catch (\Exception $e) { |
|
560
|
|
|
throw $e; |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
//Check for encoding of site. |
|
564
|
|
|
//If not UTF-8 convert it. |
|
565
|
1 |
|
$encoding = array(); |
|
566
|
1 |
|
preg_match('#.+?/.+?;\\s?charset\\s?=\\s?(.+)#i', $contentType, $encoding); |
|
567
|
1 |
|
if(empty($encoding)) { |
|
568
|
1 |
|
preg_match('/charset="?(.*?)["|;]/i', $page, $encoding); |
|
569
|
1 |
|
} |
|
570
|
|
|
|
|
571
|
1 |
|
if (isset($encoding[1])) { |
|
572
|
1 |
|
$decodeFrom = strtoupper($encoding[1]); |
|
573
|
1 |
|
} else { |
|
574
|
|
|
$decodeFrom = 'UTF-8'; |
|
575
|
|
|
} |
|
576
|
|
|
|
|
577
|
1 |
|
if ($page) { |
|
578
|
|
|
|
|
579
|
1 |
|
if ($decodeFrom != 'UTF-8') { |
|
580
|
1 |
|
$page = iconv($decodeFrom, "UTF-8", $page); |
|
581
|
1 |
|
} |
|
582
|
|
|
|
|
583
|
1 |
|
preg_match("/<title>(.*)<\/title>/si", $page, $match); |
|
584
|
|
|
|
|
585
|
1 |
|
if (isset($match[1])) { |
|
586
|
1 |
|
$metadata['title'] = html_entity_decode($match[1]); |
|
587
|
1 |
|
} |
|
588
|
1 |
|
} |
|
589
|
|
|
|
|
590
|
1 |
|
return $metadata; |
|
591
|
|
|
} |
|
592
|
|
|
|
|
593
|
|
|
/** |
|
594
|
|
|
* @brief Seperate Url String at comma charachter |
|
595
|
|
|
* @param $line String of Tags |
|
596
|
|
|
* @return array Array of Tags |
|
597
|
|
|
* */ |
|
598
|
|
|
public static function analyzeTagRequest($line) { |
|
599
|
|
|
$tags = explode(',', $line); |
|
600
|
|
|
$filterTag = array(); |
|
601
|
|
|
foreach ($tags as $tag) { |
|
602
|
|
|
if (trim($tag) != '') |
|
603
|
|
|
$filterTag[] = trim($tag); |
|
604
|
|
|
} |
|
605
|
|
|
return $filterTag; |
|
606
|
|
|
} |
|
607
|
|
|
|
|
608
|
|
|
} |
|
609
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.