diff options
author | yude <yudesleepy@gmail.com> | 2021-01-04 18:51:10 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-04 18:51:10 +0900 |
commit | e6754f2154a79abd8e5e64bd923f6984aa9ad44b (patch) | |
tree | f074119530bb59ef155938ea367f719f1e4b70f1 /application/bookmark/Bookmark.php | |
parent | 5256b4287021342a9f8868967b2a77e481314331 (diff) | |
parent | ed4ee8f0297941ac83300389b7de6a293312d20e (diff) | |
download | Shaarli-e6754f2154a79abd8e5e64bd923f6984aa9ad44b.tar.gz Shaarli-e6754f2154a79abd8e5e64bd923f6984aa9ad44b.tar.zst Shaarli-e6754f2154a79abd8e5e64bd923f6984aa9ad44b.zip |
Merge pull request #2 from shaarli/master
Merge fork source
Diffstat (limited to 'application/bookmark/Bookmark.php')
-rw-r--r-- | application/bookmark/Bookmark.php | 64 |
1 files changed, 43 insertions, 21 deletions
diff --git a/application/bookmark/Bookmark.php b/application/bookmark/Bookmark.php index ea565d1f..4238ef25 100644 --- a/application/bookmark/Bookmark.php +++ b/application/bookmark/Bookmark.php | |||
@@ -19,7 +19,7 @@ use Shaarli\Bookmark\Exception\InvalidBookmarkException; | |||
19 | class Bookmark | 19 | class Bookmark |
20 | { | 20 | { |
21 | /** @var string Date format used in string (former ID format) */ | 21 | /** @var string Date format used in string (former ID format) */ |
22 | const LINK_DATE_FORMAT = 'Ymd_His'; | 22 | public const LINK_DATE_FORMAT = 'Ymd_His'; |
23 | 23 | ||
24 | /** @var int Bookmark ID */ | 24 | /** @var int Bookmark ID */ |
25 | protected $id; | 25 | protected $id; |
@@ -60,11 +60,13 @@ class Bookmark | |||
60 | /** | 60 | /** |
61 | * Initialize a link from array data. Especially useful to create a Bookmark from former link storage format. | 61 | * Initialize a link from array data. Especially useful to create a Bookmark from former link storage format. |
62 | * | 62 | * |
63 | * @param array $data | 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. | ||
64 | * | 66 | * |
65 | * @return $this | 67 | * @return $this |
66 | */ | 68 | */ |
67 | public function fromArray(array $data): Bookmark | 69 | public function fromArray(array $data, string $tagsSeparator = ' '): Bookmark |
68 | { | 70 | { |
69 | $this->id = $data['id'] ?? null; | 71 | $this->id = $data['id'] ?? null; |
70 | $this->shortUrl = $data['shorturl'] ?? null; | 72 | $this->shortUrl = $data['shorturl'] ?? null; |
@@ -77,7 +79,7 @@ class Bookmark | |||
77 | if (is_array($data['tags'])) { | 79 | if (is_array($data['tags'])) { |
78 | $this->tags = $data['tags']; | 80 | $this->tags = $data['tags']; |
79 | } else { | 81 | } else { |
80 | $this->tags = preg_split('/\s+/', $data['tags'] ?? '', -1, PREG_SPLIT_NO_EMPTY); | 82 | $this->tags = tags_str2array($data['tags'] ?? '', $tagsSeparator); |
81 | } | 83 | } |
82 | if (! empty($data['updated'])) { | 84 | if (! empty($data['updated'])) { |
83 | $this->updated = $data['updated']; | 85 | $this->updated = $data['updated']; |
@@ -104,7 +106,8 @@ class Bookmark | |||
104 | */ | 106 | */ |
105 | public function validate(): void | 107 | public function validate(): void |
106 | { | 108 | { |
107 | if ($this->id === null | 109 | if ( |
110 | $this->id === null | ||
108 | || ! is_int($this->id) | 111 | || ! is_int($this->id) |
109 | || empty($this->shortUrl) | 112 | || empty($this->shortUrl) |
110 | || empty($this->created) | 113 | || empty($this->created) |
@@ -112,7 +115,7 @@ class Bookmark | |||
112 | throw new InvalidBookmarkException($this); | 115 | throw new InvalidBookmarkException($this); |
113 | } | 116 | } |
114 | if (empty($this->url)) { | 117 | if (empty($this->url)) { |
115 | $this->url = '/shaare/'. $this->shortUrl; | 118 | $this->url = '/shaare/' . $this->shortUrl; |
116 | } | 119 | } |
117 | if (empty($this->title)) { | 120 | if (empty($this->title)) { |
118 | $this->title = $this->url; | 121 | $this->title = $this->url; |
@@ -348,7 +351,12 @@ class Bookmark | |||
348 | */ | 351 | */ |
349 | public function setTags(?array $tags): Bookmark | 352 | public function setTags(?array $tags): Bookmark |
350 | { | 353 | { |
351 | $this->setTagsString(implode(' ', $tags ?? [])); | 354 | $this->tags = array_map( |
355 | function (string $tag): string { | ||
356 | return $tag[0] === '-' ? substr($tag, 1) : $tag; | ||
357 | }, | ||
358 | tags_filter($tags, ' ') | ||
359 | ); | ||
352 | 360 | ||
353 | return $this; | 361 | return $this; |
354 | } | 362 | } |
@@ -378,6 +386,24 @@ class Bookmark | |||
378 | } | 386 | } |
379 | 387 | ||
380 | /** | 388 | /** |
389 | * Return true if: | ||
390 | * - the bookmark's thumbnail is not already set to false (= not found) | ||
391 | * - it's not a note | ||
392 | * - it's an HTTP(S) link | ||
393 | * - the thumbnail has not yet be retrieved (null) or its associated cache file doesn't exist anymore | ||
394 | * | ||
395 | * @return bool True if the bookmark's thumbnail needs to be retrieved. | ||
396 | */ | ||
397 | public function shouldUpdateThumbnail(): bool | ||
398 | { | ||
399 | return $this->thumbnail !== false | ||
400 | && !$this->isNote() | ||
401 | && startsWith(strtolower($this->url), 'http') | ||
402 | && (null === $this->thumbnail || !is_file($this->thumbnail)) | ||
403 | ; | ||
404 | } | ||
405 | |||
406 | /** | ||
381 | * Get the Sticky. | 407 | * Get the Sticky. |
382 | * | 408 | * |
383 | * @return bool | 409 | * @return bool |
@@ -402,11 +428,13 @@ class Bookmark | |||
402 | } | 428 | } |
403 | 429 | ||
404 | /** | 430 | /** |
405 | * @return string Bookmark's tags as a string, separated by a space | 431 | * @param string $separator Tags separator loaded from the config file. |
432 | * | ||
433 | * @return string Bookmark's tags as a string, separated by a separator | ||
406 | */ | 434 | */ |
407 | public function getTagsString(): string | 435 | public function getTagsString(string $separator = ' '): string |
408 | { | 436 | { |
409 | return implode(' ', $this->getTags()); | 437 | return tags_array2str($this->getTags(), $separator); |
410 | } | 438 | } |
411 | 439 | ||
412 | /** | 440 | /** |
@@ -426,19 +454,13 @@ class Bookmark | |||
426 | * - trailing dash in tags will be removed | 454 | * - trailing dash in tags will be removed |
427 | * | 455 | * |
428 | * @param string|null $tags | 456 | * @param string|null $tags |
457 | * @param string $separator Tags separator loaded from the config file. | ||
429 | * | 458 | * |
430 | * @return $this | 459 | * @return $this |
431 | */ | 460 | */ |
432 | public function setTagsString(?string $tags): Bookmark | 461 | public function setTagsString(?string $tags, string $separator = ' '): Bookmark |
433 | { | 462 | { |
434 | // Remove first '-' char in tags. | 463 | $this->setTags(tags_str2array($tags, $separator)); |
435 | $tags = preg_replace('/(^| )\-/', '$1', $tags ?? ''); | ||
436 | // Explode all tags separted by spaces or commas | ||
437 | $tags = preg_split('/[\s,]+/', $tags); | ||
438 | // Remove eventual empty values | ||
439 | $tags = array_values(array_filter($tags)); | ||
440 | |||
441 | $this->tags = $tags; | ||
442 | 464 | ||
443 | return $this; | 465 | return $this; |
444 | } | 466 | } |
@@ -489,7 +511,7 @@ class Bookmark | |||
489 | */ | 511 | */ |
490 | public function renameTag(string $fromTag, string $toTag): void | 512 | public function renameTag(string $fromTag, string $toTag): void |
491 | { | 513 | { |
492 | if (($pos = array_search($fromTag, $this->tags)) !== false) { | 514 | if (($pos = array_search($fromTag, $this->tags ?? [])) !== false) { |
493 | $this->tags[$pos] = trim($toTag); | 515 | $this->tags[$pos] = trim($toTag); |
494 | } | 516 | } |
495 | } | 517 | } |
@@ -501,7 +523,7 @@ class Bookmark | |||
501 | */ | 523 | */ |
502 | public function deleteTag(string $tag): void | 524 | public function deleteTag(string $tag): void |
503 | { | 525 | { |
504 | if (($pos = array_search($tag, $this->tags)) !== false) { | 526 | if (($pos = array_search($tag, $this->tags ?? [])) !== false) { |
505 | unset($this->tags[$pos]); | 527 | unset($this->tags[$pos]); |
506 | $this->tags = array_values($this->tags); | 528 | $this->tags = array_values($this->tags); |
507 | } | 529 | } |