]>
Commit | Line | Data |
---|---|---|
336a28fa A |
1 | <?php |
2 | ||
efb7d21b A |
3 | declare(strict_types=1); |
4 | ||
336a28fa A |
5 | namespace Shaarli\Bookmark; |
6 | ||
7 | use DateTime; | |
c4d5be53 | 8 | use DateTimeInterface; |
336a28fa A |
9 | use Shaarli\Bookmark\Exception\InvalidBookmarkException; |
10 | ||
11 | /** | |
12 | * Class Bookmark | |
13 | * | |
14 | * This class represent a single Bookmark with all its attributes. | |
15 | * Every bookmark should manipulated using this, before being formatted. | |
16 | * | |
17 | * @package Shaarli\Bookmark | |
18 | */ | |
19 | class Bookmark | |
20 | { | |
21 | /** @var string Date format used in string (former ID format) */ | |
22 | const LINK_DATE_FORMAT = 'Ymd_His'; | |
23 | ||
24 | /** @var int Bookmark ID */ | |
25 | protected $id; | |
26 | ||
27 | /** @var string Permalink identifier */ | |
28 | protected $shortUrl; | |
29 | ||
30 | /** @var string Bookmark's URL - $shortUrl prefixed with `?` for notes */ | |
31 | protected $url; | |
32 | ||
33 | /** @var string Bookmark's title */ | |
34 | protected $title; | |
35 | ||
36 | /** @var string Raw bookmark's description */ | |
37 | protected $description; | |
38 | ||
39 | /** @var array List of bookmark's tags */ | |
40 | protected $tags; | |
41 | ||
1a68ae5a | 42 | /** @var string|bool|null Thumbnail's URL - initialized at null, false if no thumbnail could be found */ |
336a28fa A |
43 | protected $thumbnail; |
44 | ||
45 | /** @var bool Set to true if the bookmark is set as sticky */ | |
46 | protected $sticky; | |
47 | ||
c4d5be53 | 48 | /** @var DateTimeInterface Creation datetime */ |
336a28fa A |
49 | protected $created; |
50 | ||
c4d5be53 | 51 | /** @var DateTimeInterface datetime */ |
336a28fa A |
52 | protected $updated; |
53 | ||
54 | /** @var bool True if the bookmark can only be seen while logged in */ | |
55 | protected $private; | |
56 | ||
4e3875c0 A |
57 | /** @var mixed[] Available to store any additional content for a bookmark. Currently used for search highlight. */ |
58 | protected $additionalContent = []; | |
59 | ||
336a28fa A |
60 | /** |
61 | * Initialize a link from array data. Especially useful to create a Bookmark from former link storage format. | |
62 | * | |
b3bd8c3e A |
63 | * @param array $data |
64 | * @param string $tagsSeparator Tags separator loaded from the config file. | |
65 | * This is a context data, and it should *never* be stored in the Bookmark object. | |
336a28fa A |
66 | * |
67 | * @return $this | |
68 | */ | |
b3bd8c3e | 69 | public function fromArray(array $data, string $tagsSeparator = ' '): Bookmark |
336a28fa | 70 | { |
efb7d21b A |
71 | $this->id = $data['id'] ?? null; |
72 | $this->shortUrl = $data['shorturl'] ?? null; | |
73 | $this->url = $data['url'] ?? null; | |
74 | $this->title = $data['title'] ?? null; | |
75 | $this->description = $data['description'] ?? null; | |
76 | $this->thumbnail = $data['thumbnail'] ?? null; | |
77 | $this->sticky = $data['sticky'] ?? false; | |
78 | $this->created = $data['created'] ?? null; | |
336a28fa A |
79 | if (is_array($data['tags'])) { |
80 | $this->tags = $data['tags']; | |
81 | } else { | |
b3bd8c3e | 82 | $this->tags = tags_str2array($data['tags'] ?? '', $tagsSeparator); |
336a28fa A |
83 | } |
84 | if (! empty($data['updated'])) { | |
85 | $this->updated = $data['updated']; | |
86 | } | |
efb7d21b | 87 | $this->private = ($data['private'] ?? false) ? true : false; |
336a28fa A |
88 | |
89 | return $this; | |
90 | } | |
91 | ||
92 | /** | |
93 | * Make sure that the current instance of Bookmark is valid and can be saved into the data store. | |
94 | * A valid link requires: | |
95 | * - an integer ID | |
96 | * - a short URL (for permalinks) | |
97 | * - a creation date | |
98 | * | |
99 | * This function also initialize optional empty fields: | |
100 | * - the URL with the permalink | |
101 | * - the title with the URL | |
102 | * | |
4e3875c0 A |
103 | * Also make sure that we do not save search highlights in the datastore. |
104 | * | |
336a28fa A |
105 | * @throws InvalidBookmarkException |
106 | */ | |
efb7d21b | 107 | public function validate(): void |
336a28fa A |
108 | { |
109 | if ($this->id === null | |
110 | || ! is_int($this->id) | |
111 | || empty($this->shortUrl) | |
112 | || empty($this->created) | |
336a28fa A |
113 | ) { |
114 | throw new InvalidBookmarkException($this); | |
115 | } | |
116 | if (empty($this->url)) { | |
301c7ab1 | 117 | $this->url = '/shaare/'. $this->shortUrl; |
336a28fa A |
118 | } |
119 | if (empty($this->title)) { | |
120 | $this->title = $this->url; | |
121 | } | |
4e3875c0 A |
122 | if (array_key_exists('search_highlight', $this->additionalContent)) { |
123 | unset($this->additionalContent['search_highlight']); | |
124 | } | |
336a28fa A |
125 | } |
126 | ||
127 | /** | |
128 | * Set the Id. | |
129 | * If they're not already initialized, this function also set: | |
130 | * - created: with the current datetime | |
131 | * - shortUrl: with a generated small hash from the date and the given ID | |
132 | * | |
efb7d21b | 133 | * @param int|null $id |
336a28fa A |
134 | * |
135 | * @return Bookmark | |
136 | */ | |
efb7d21b | 137 | public function setId(?int $id): Bookmark |
336a28fa A |
138 | { |
139 | $this->id = $id; | |
140 | if (empty($this->created)) { | |
141 | $this->created = new DateTime(); | |
142 | } | |
143 | if (empty($this->shortUrl)) { | |
144 | $this->shortUrl = link_small_hash($this->created, $this->id); | |
145 | } | |
146 | ||
147 | return $this; | |
148 | } | |
149 | ||
150 | /** | |
151 | * Get the Id. | |
152 | * | |
efb7d21b | 153 | * @return int|null |
336a28fa | 154 | */ |
efb7d21b | 155 | public function getId(): ?int |
336a28fa A |
156 | { |
157 | return $this->id; | |
158 | } | |
159 | ||
160 | /** | |
161 | * Get the ShortUrl. | |
162 | * | |
efb7d21b | 163 | * @return string|null |
336a28fa | 164 | */ |
efb7d21b | 165 | public function getShortUrl(): ?string |
336a28fa A |
166 | { |
167 | return $this->shortUrl; | |
168 | } | |
169 | ||
170 | /** | |
171 | * Get the Url. | |
172 | * | |
efb7d21b | 173 | * @return string|null |
336a28fa | 174 | */ |
efb7d21b | 175 | public function getUrl(): ?string |
336a28fa A |
176 | { |
177 | return $this->url; | |
178 | } | |
179 | ||
180 | /** | |
181 | * Get the Title. | |
182 | * | |
183 | * @return string | |
184 | */ | |
efb7d21b | 185 | public function getTitle(): ?string |
336a28fa A |
186 | { |
187 | return $this->title; | |
188 | } | |
189 | ||
190 | /** | |
191 | * Get the Description. | |
192 | * | |
193 | * @return string | |
194 | */ | |
efb7d21b | 195 | public function getDescription(): string |
336a28fa A |
196 | { |
197 | return ! empty($this->description) ? $this->description : ''; | |
198 | } | |
199 | ||
200 | /** | |
201 | * Get the Created. | |
202 | * | |
c4d5be53 | 203 | * @return DateTimeInterface |
336a28fa | 204 | */ |
efb7d21b | 205 | public function getCreated(): ?DateTimeInterface |
336a28fa A |
206 | { |
207 | return $this->created; | |
208 | } | |
209 | ||
210 | /** | |
211 | * Get the Updated. | |
212 | * | |
c4d5be53 | 213 | * @return DateTimeInterface |
336a28fa | 214 | */ |
efb7d21b | 215 | public function getUpdated(): ?DateTimeInterface |
336a28fa A |
216 | { |
217 | return $this->updated; | |
218 | } | |
219 | ||
220 | /** | |
221 | * Set the ShortUrl. | |
222 | * | |
efb7d21b | 223 | * @param string|null $shortUrl |
336a28fa A |
224 | * |
225 | * @return Bookmark | |
226 | */ | |
efb7d21b | 227 | public function setShortUrl(?string $shortUrl): Bookmark |
336a28fa A |
228 | { |
229 | $this->shortUrl = $shortUrl; | |
230 | ||
231 | return $this; | |
232 | } | |
233 | ||
234 | /** | |
235 | * Set the Url. | |
236 | * | |
efb7d21b A |
237 | * @param string|null $url |
238 | * @param string[] $allowedProtocols | |
336a28fa A |
239 | * |
240 | * @return Bookmark | |
241 | */ | |
efb7d21b | 242 | public function setUrl(?string $url, array $allowedProtocols = []): Bookmark |
336a28fa | 243 | { |
efb7d21b | 244 | $url = $url !== null ? trim($url) : ''; |
336a28fa A |
245 | if (! empty($url)) { |
246 | $url = whitelist_protocols($url, $allowedProtocols); | |
247 | } | |
248 | $this->url = $url; | |
249 | ||
250 | return $this; | |
251 | } | |
252 | ||
253 | /** | |
254 | * Set the Title. | |
255 | * | |
efb7d21b | 256 | * @param string|null $title |
336a28fa A |
257 | * |
258 | * @return Bookmark | |
259 | */ | |
efb7d21b | 260 | public function setTitle(?string $title): Bookmark |
336a28fa | 261 | { |
efb7d21b | 262 | $this->title = $title !== null ? trim($title) : ''; |
336a28fa A |
263 | |
264 | return $this; | |
265 | } | |
266 | ||
267 | /** | |
268 | * Set the Description. | |
269 | * | |
efb7d21b | 270 | * @param string|null $description |
336a28fa A |
271 | * |
272 | * @return Bookmark | |
273 | */ | |
efb7d21b | 274 | public function setDescription(?string $description): Bookmark |
336a28fa A |
275 | { |
276 | $this->description = $description; | |
277 | ||
278 | return $this; | |
279 | } | |
280 | ||
281 | /** | |
282 | * Set the Created. | |
283 | * Note: you shouldn't set this manually except for special cases (like bookmark import) | |
284 | * | |
efb7d21b | 285 | * @param DateTimeInterface|null $created |
336a28fa A |
286 | * |
287 | * @return Bookmark | |
288 | */ | |
efb7d21b | 289 | public function setCreated(?DateTimeInterface $created): Bookmark |
336a28fa A |
290 | { |
291 | $this->created = $created; | |
292 | ||
293 | return $this; | |
294 | } | |
295 | ||
296 | /** | |
297 | * Set the Updated. | |
298 | * | |
efb7d21b | 299 | * @param DateTimeInterface|null $updated |
336a28fa A |
300 | * |
301 | * @return Bookmark | |
302 | */ | |
efb7d21b | 303 | public function setUpdated(?DateTimeInterface $updated): Bookmark |
336a28fa A |
304 | { |
305 | $this->updated = $updated; | |
306 | ||
307 | return $this; | |
308 | } | |
309 | ||
310 | /** | |
311 | * Get the Private. | |
312 | * | |
313 | * @return bool | |
314 | */ | |
efb7d21b | 315 | public function isPrivate(): bool |
336a28fa A |
316 | { |
317 | return $this->private ? true : false; | |
318 | } | |
319 | ||
320 | /** | |
321 | * Set the Private. | |
322 | * | |
efb7d21b | 323 | * @param bool|null $private |
336a28fa A |
324 | * |
325 | * @return Bookmark | |
326 | */ | |
efb7d21b | 327 | public function setPrivate(?bool $private): Bookmark |
336a28fa A |
328 | { |
329 | $this->private = $private ? true : false; | |
330 | ||
331 | return $this; | |
332 | } | |
333 | ||
334 | /** | |
335 | * Get the Tags. | |
336 | * | |
efb7d21b | 337 | * @return string[] |
336a28fa | 338 | */ |
efb7d21b | 339 | public function getTags(): array |
336a28fa A |
340 | { |
341 | return is_array($this->tags) ? $this->tags : []; | |
342 | } | |
343 | ||
344 | /** | |
345 | * Set the Tags. | |
346 | * | |
efb7d21b | 347 | * @param string[]|null $tags |
336a28fa A |
348 | * |
349 | * @return Bookmark | |
350 | */ | |
efb7d21b | 351 | public function setTags(?array $tags): Bookmark |
336a28fa | 352 | { |
b3bd8c3e A |
353 | $this->tags = array_map( |
354 | function (string $tag): string { | |
355 | return $tag[0] === '-' ? substr($tag, 1) : $tag; | |
356 | }, | |
357 | tags_filter($tags, ' ') | |
358 | ); | |
336a28fa A |
359 | |
360 | return $this; | |
361 | } | |
362 | ||
363 | /** | |
364 | * Get the Thumbnail. | |
365 | * | |
1a68ae5a | 366 | * @return string|bool|null Thumbnail's URL - initialized at null, false if no thumbnail could be found |
336a28fa A |
367 | */ |
368 | public function getThumbnail() | |
369 | { | |
370 | return !$this->isNote() ? $this->thumbnail : false; | |
371 | } | |
372 | ||
373 | /** | |
374 | * Set the Thumbnail. | |
375 | * | |
efb7d21b | 376 | * @param string|bool|null $thumbnail Thumbnail's URL - false if no thumbnail could be found |
336a28fa A |
377 | * |
378 | * @return Bookmark | |
379 | */ | |
efb7d21b | 380 | public function setThumbnail($thumbnail): Bookmark |
336a28fa A |
381 | { |
382 | $this->thumbnail = $thumbnail; | |
383 | ||
384 | return $this; | |
385 | } | |
386 | ||
21e72da9 A |
387 | /** |
388 | * Return true if: | |
389 | * - the bookmark's thumbnail is not already set to false (= not found) | |
390 | * - it's not a note | |
391 | * - it's an HTTP(S) link | |
392 | * - the thumbnail has not yet be retrieved (null) or its associated cache file doesn't exist anymore | |
393 | * | |
394 | * @return bool True if the bookmark's thumbnail needs to be retrieved. | |
395 | */ | |
396 | public function shouldUpdateThumbnail(): bool | |
397 | { | |
398 | return $this->thumbnail !== false | |
399 | && !$this->isNote() | |
400 | && startsWith(strtolower($this->url), 'http') | |
401 | && (null === $this->thumbnail || !is_file($this->thumbnail)) | |
402 | ; | |
403 | } | |
404 | ||
336a28fa A |
405 | /** |
406 | * Get the Sticky. | |
407 | * | |
408 | * @return bool | |
409 | */ | |
efb7d21b | 410 | public function isSticky(): bool |
336a28fa A |
411 | { |
412 | return $this->sticky ? true : false; | |
413 | } | |
414 | ||
415 | /** | |
416 | * Set the Sticky. | |
417 | * | |
efb7d21b | 418 | * @param bool|null $sticky |
336a28fa A |
419 | * |
420 | * @return Bookmark | |
421 | */ | |
efb7d21b | 422 | public function setSticky(?bool $sticky): Bookmark |
336a28fa A |
423 | { |
424 | $this->sticky = $sticky ? true : false; | |
425 | ||
426 | return $this; | |
427 | } | |
428 | ||
429 | /** | |
b3bd8c3e A |
430 | * @param string $separator Tags separator loaded from the config file. |
431 | * | |
432 | * @return string Bookmark's tags as a string, separated by a separator | |
336a28fa | 433 | */ |
b3bd8c3e | 434 | public function getTagsString(string $separator = ' '): string |
336a28fa | 435 | { |
b3bd8c3e | 436 | return tags_array2str($this->getTags(), $separator); |
336a28fa A |
437 | } |
438 | ||
439 | /** | |
440 | * @return bool | |
441 | */ | |
efb7d21b | 442 | public function isNote(): bool |
336a28fa A |
443 | { |
444 | // We check empty value to get a valid result if the link has not been saved yet | |
301c7ab1 | 445 | return empty($this->url) || startsWith($this->url, '/shaare/') || $this->url[0] === '?'; |
336a28fa A |
446 | } |
447 | ||
448 | /** | |
449 | * Set tags from a string. | |
450 | * Note: | |
451 | * - tags must be separated whether by a space or a comma | |
452 | * - multiple spaces will be removed | |
453 | * - trailing dash in tags will be removed | |
454 | * | |
efb7d21b | 455 | * @param string|null $tags |
b3bd8c3e | 456 | * @param string $separator Tags separator loaded from the config file. |
336a28fa A |
457 | * |
458 | * @return $this | |
459 | */ | |
b3bd8c3e | 460 | public function setTagsString(?string $tags, string $separator = ' '): Bookmark |
336a28fa | 461 | { |
b3bd8c3e | 462 | $this->setTags(tags_str2array($tags, $separator)); |
336a28fa A |
463 | |
464 | return $this; | |
465 | } | |
466 | ||
4e3875c0 A |
467 | /** |
468 | * Get entire additionalContent array. | |
469 | * | |
470 | * @return mixed[] | |
471 | */ | |
472 | public function getAdditionalContent(): array | |
473 | { | |
474 | return $this->additionalContent; | |
475 | } | |
476 | ||
477 | /** | |
478 | * Set a single entry in additionalContent, by key. | |
479 | * | |
480 | * @param string $key | |
481 | * @param mixed|null $value Any type of value can be set. | |
482 | * | |
483 | * @return $this | |
484 | */ | |
485 | public function addAdditionalContentEntry(string $key, $value): self | |
486 | { | |
487 | $this->additionalContent[$key] = $value; | |
488 | ||
489 | return $this; | |
490 | } | |
491 | ||
492 | /** | |
493 | * Get a single entry in additionalContent, by key. | |
494 | * | |
495 | * @param string $key | |
496 | * @param mixed|null $default | |
497 | * | |
498 | * @return mixed|null can be any type or even null. | |
499 | */ | |
500 | public function getAdditionalContentEntry(string $key, $default = null) | |
501 | { | |
502 | return array_key_exists($key, $this->additionalContent) ? $this->additionalContent[$key] : $default; | |
503 | } | |
504 | ||
336a28fa A |
505 | /** |
506 | * Rename a tag in tags list. | |
507 | * | |
508 | * @param string $fromTag | |
509 | * @param string $toTag | |
510 | */ | |
efb7d21b | 511 | public function renameTag(string $fromTag, string $toTag): void |
336a28fa | 512 | { |
b3bd8c3e | 513 | if (($pos = array_search($fromTag, $this->tags ?? [])) !== false) { |
336a28fa A |
514 | $this->tags[$pos] = trim($toTag); |
515 | } | |
516 | } | |
517 | ||
518 | /** | |
519 | * Delete a tag from tags list. | |
520 | * | |
521 | * @param string $tag | |
522 | */ | |
efb7d21b | 523 | public function deleteTag(string $tag): void |
336a28fa | 524 | { |
b3bd8c3e | 525 | if (($pos = array_search($tag, $this->tags ?? [])) !== false) { |
336a28fa A |
526 | unset($this->tags[$pos]); |
527 | $this->tags = array_values($this->tags); | |
528 | } | |
529 | } | |
530 | } |