]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Entity/Entry.php
Use hash given url to avoid duplicate
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Entity / Entry.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Entity;
4
5 use Doctrine\Common\Collections\ArrayCollection;
6 use Doctrine\ORM\Mapping as ORM;
7 use Hateoas\Configuration\Annotation as Hateoas;
8 use JMS\Serializer\Annotation\Exclude;
9 use JMS\Serializer\Annotation\Groups;
10 use JMS\Serializer\Annotation\SerializedName;
11 use JMS\Serializer\Annotation\VirtualProperty;
12 use JMS\Serializer\Annotation\XmlRoot;
13 use Symfony\Component\Validator\Constraints as Assert;
14 use Wallabag\AnnotationBundle\Entity\Annotation;
15 use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
16 use Wallabag\CoreBundle\Helper\UrlHasher;
17 use Wallabag\UserBundle\Entity\User;
18
19 /**
20 * Entry.
21 *
22 * @XmlRoot("entry")
23 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository")
24 * @ORM\Table(
25 * name="`entry`",
26 * options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"},
27 * indexes={
28 * @ORM\Index(name="created_at", columns={"created_at"}),
29 * @ORM\Index(name="uid", columns={"uid"}),
30 * @ORM\Index(name="hashed_urls_user_id", columns={"user_id", "hashed_url", "hashed_given_url"}, options={"lengths"={null, 40, 40}})
31 * }
32 * )
33 * @ORM\HasLifecycleCallbacks()
34 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
35 */
36 class Entry
37 {
38 use EntityTimestampsTrait;
39
40 /** @Serializer\XmlAttribute */
41 /**
42 * @var int
43 *
44 * @ORM\Column(name="id", type="integer")
45 * @ORM\Id
46 * @ORM\GeneratedValue(strategy="AUTO")
47 *
48 * @Groups({"entries_for_user", "export_all"})
49 */
50 private $id;
51
52 /**
53 * @var string
54 *
55 * @ORM\Column(name="uid", type="string", length=23, nullable=true)
56 *
57 * @Groups({"entries_for_user", "export_all"})
58 */
59 private $uid;
60
61 /**
62 * @var string
63 *
64 * @ORM\Column(name="title", type="text", nullable=true)
65 *
66 * @Groups({"entries_for_user", "export_all"})
67 */
68 private $title;
69
70 /**
71 * Define the url fetched by wallabag (the final url after potential redirections).
72 *
73 * @var string
74 *
75 * @Assert\NotBlank()
76 * @ORM\Column(name="url", type="text", nullable=true)
77 *
78 * @Groups({"entries_for_user", "export_all"})
79 */
80 private $url;
81
82 /**
83 * @var string
84 *
85 * @ORM\Column(name="hashed_url", type="string", length=40, nullable=true)
86 */
87 private $hashedUrl;
88
89 /**
90 * From where user retrieved/found the url (an other article, a twitter, or the given_url if non are provided).
91 *
92 * @var string
93 *
94 * @ORM\Column(name="origin_url", type="text", nullable=true)
95 *
96 * @Groups({"entries_for_user", "export_all"})
97 */
98 private $originUrl;
99
100 /**
101 * Define the url entered by the user (without redirections).
102 *
103 * @var string
104 *
105 * @ORM\Column(name="given_url", type="text", nullable=true)
106 *
107 * @Groups({"entries_for_user", "export_all"})
108 */
109 private $givenUrl;
110
111 /**
112 * @var string
113 *
114 * @ORM\Column(name="hashed_given_url", type="string", length=40, nullable=true)
115 */
116 private $hashedGivenUrl;
117
118 /**
119 * @var bool
120 *
121 * @Exclude
122 *
123 * @ORM\Column(name="is_archived", type="boolean")
124 *
125 * @Groups({"entries_for_user", "export_all"})
126 */
127 private $isArchived = false;
128
129 /**
130 * @var \DateTime
131 *
132 * @ORM\Column(name="archived_at", type="datetime", nullable=true)
133 *
134 * @Groups({"entries_for_user", "export_all"})
135 */
136 private $archivedAt = null;
137
138 /**
139 * @var bool
140 *
141 * @Exclude
142 *
143 * @ORM\Column(name="is_starred", type="boolean")
144 *
145 * @Groups({"entries_for_user", "export_all"})
146 */
147 private $isStarred = false;
148
149 /**
150 * @var string
151 *
152 * @ORM\Column(name="content", type="text", nullable=true)
153 *
154 * @Groups({"entries_for_user", "export_all"})
155 */
156 private $content;
157
158 /**
159 * @var \DateTime
160 *
161 * @ORM\Column(name="created_at", type="datetime")
162 *
163 * @Groups({"entries_for_user", "export_all"})
164 */
165 private $createdAt;
166
167 /**
168 * @var \DateTime
169 *
170 * @ORM\Column(name="updated_at", type="datetime")
171 *
172 * @Groups({"entries_for_user", "export_all"})
173 */
174 private $updatedAt;
175
176 /**
177 * @var \DateTime
178 *
179 * @ORM\Column(name="published_at", type="datetime", nullable=true)
180 *
181 * @Groups({"entries_for_user", "export_all"})
182 */
183 private $publishedAt;
184
185 /**
186 * @var array
187 *
188 * @ORM\Column(name="published_by", type="array", nullable=true)
189 *
190 * @Groups({"entries_for_user", "export_all"})
191 */
192 private $publishedBy;
193
194 /**
195 * @var \DateTime
196 *
197 * @ORM\Column(name="starred_at", type="datetime", nullable=true)
198 *
199 * @Groups({"entries_for_user", "export_all"})
200 */
201 private $starredAt = null;
202
203 /**
204 * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"})
205 * @ORM\JoinTable
206 *
207 * @Groups({"entries_for_user", "export_all"})
208 */
209 private $annotations;
210
211 /**
212 * @var string
213 *
214 * @ORM\Column(name="mimetype", type="text", nullable=true)
215 *
216 * @Groups({"entries_for_user", "export_all"})
217 */
218 private $mimetype;
219
220 /**
221 * @var string
222 *
223 * @ORM\Column(name="language", type="text", nullable=true)
224 *
225 * @Groups({"entries_for_user", "export_all"})
226 */
227 private $language;
228
229 /**
230 * @var int
231 *
232 * @ORM\Column(name="reading_time", type="integer", nullable=false)
233 *
234 * @Groups({"entries_for_user", "export_all"})
235 */
236 private $readingTime = 0;
237
238 /**
239 * @var string
240 *
241 * @ORM\Column(name="domain_name", type="text", nullable=true)
242 *
243 * @Groups({"entries_for_user", "export_all"})
244 */
245 private $domainName;
246
247 /**
248 * @var string
249 *
250 * @ORM\Column(name="preview_picture", type="text", nullable=true)
251 *
252 * @Groups({"entries_for_user", "export_all"})
253 */
254 private $previewPicture;
255
256 /**
257 * @var string
258 *
259 * @ORM\Column(name="http_status", type="string", length=3, nullable=true)
260 *
261 * @Groups({"entries_for_user", "export_all"})
262 */
263 private $httpStatus;
264
265 /**
266 * @var array
267 *
268 * @ORM\Column(name="headers", type="array", nullable=true)
269 *
270 * @Groups({"entries_for_user", "export_all"})
271 */
272 private $headers;
273
274 /**
275 * @Exclude
276 *
277 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
278 *
279 * @Groups({"export_all"})
280 */
281 private $user;
282
283 /**
284 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
285 * @ORM\JoinTable(
286 * name="entry_tag",
287 * joinColumns={
288 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
289 * },
290 * inverseJoinColumns={
291 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
292 * }
293 * )
294 */
295 private $tags;
296
297 /*
298 * @param User $user
299 */
300 public function __construct(User $user)
301 {
302 $this->user = $user;
303 $this->tags = new ArrayCollection();
304 }
305
306 /**
307 * Get id.
308 *
309 * @return int
310 */
311 public function getId()
312 {
313 return $this->id;
314 }
315
316 /**
317 * Set title.
318 *
319 * @param string $title
320 *
321 * @return Entry
322 */
323 public function setTitle($title)
324 {
325 $this->title = $title;
326
327 return $this;
328 }
329
330 /**
331 * Get title.
332 *
333 * @return string
334 */
335 public function getTitle()
336 {
337 return $this->title;
338 }
339
340 /**
341 * Set url.
342 *
343 * @param string $url
344 *
345 * @return Entry
346 */
347 public function setUrl($url)
348 {
349 $this->url = $url;
350 $this->hashedUrl = UrlHasher::hashUrl($url);
351
352 return $this;
353 }
354
355 /**
356 * Get url.
357 *
358 * @return string
359 */
360 public function getUrl()
361 {
362 return $this->url;
363 }
364
365 /**
366 * Set isArchived.
367 *
368 * @param bool $isArchived
369 *
370 * @return Entry
371 */
372 public function setArchived($isArchived)
373 {
374 $this->isArchived = $isArchived;
375
376 return $this;
377 }
378
379 /**
380 * update isArchived and archive_at fields.
381 *
382 * @param bool $isArchived
383 *
384 * @return Entry
385 */
386 public function updateArchived($isArchived = false)
387 {
388 $this->setArchived($isArchived);
389 $this->setArchivedAt(null);
390 if ($this->isArchived()) {
391 $this->setArchivedAt(new \DateTime());
392 }
393
394 return $this;
395 }
396
397 /**
398 * @return \DateTime|null
399 */
400 public function getArchivedAt()
401 {
402 return $this->archivedAt;
403 }
404
405 /**
406 * @param \DateTime|null $archivedAt
407 *
408 * @return Entry
409 */
410 public function setArchivedAt($archivedAt = null)
411 {
412 $this->archivedAt = $archivedAt;
413
414 return $this;
415 }
416
417 /**
418 * Get isArchived.
419 *
420 * @return bool
421 */
422 public function isArchived()
423 {
424 return $this->isArchived;
425 }
426
427 /**
428 * @VirtualProperty
429 * @SerializedName("is_archived")
430 * @Groups({"entries_for_user", "export_all"})
431 */
432 public function is_Archived()
433 {
434 return (int) $this->isArchived();
435 }
436
437 public function toggleArchive()
438 {
439 $this->updateArchived($this->isArchived() ^ 1);
440
441 return $this;
442 }
443
444 /**
445 * Set isStarred.
446 *
447 * @param bool $isStarred
448 *
449 * @return Entry
450 */
451 public function setStarred($isStarred)
452 {
453 $this->isStarred = $isStarred;
454
455 return $this;
456 }
457
458 /**
459 * Get isStarred.
460 *
461 * @return bool
462 */
463 public function isStarred()
464 {
465 return $this->isStarred;
466 }
467
468 /**
469 * @VirtualProperty
470 * @SerializedName("is_starred")
471 * @Groups({"entries_for_user", "export_all"})
472 */
473 public function is_Starred()
474 {
475 return (int) $this->isStarred();
476 }
477
478 public function toggleStar()
479 {
480 $this->isStarred = $this->isStarred() ^ 1;
481
482 return $this;
483 }
484
485 /**
486 * Set content.
487 *
488 * @param string $content
489 *
490 * @return Entry
491 */
492 public function setContent($content)
493 {
494 $this->content = $content;
495
496 return $this;
497 }
498
499 /**
500 * Get content.
501 *
502 * @return string
503 */
504 public function getContent()
505 {
506 return $this->content;
507 }
508
509 /**
510 * @return User
511 */
512 public function getUser()
513 {
514 return $this->user;
515 }
516
517 /**
518 * @VirtualProperty
519 * @SerializedName("user_name")
520 */
521 public function getUserName()
522 {
523 return $this->user->getUserName();
524 }
525
526 /**
527 * @VirtualProperty
528 * @SerializedName("user_email")
529 */
530 public function getUserEmail()
531 {
532 return $this->user->getEmail();
533 }
534
535 /**
536 * @VirtualProperty
537 * @SerializedName("user_id")
538 */
539 public function getUserId()
540 {
541 return $this->user->getId();
542 }
543
544 /**
545 * Set created_at.
546 * Only used when importing data from an other service.
547 *
548 * @param \DateTime $createdAt
549 *
550 * @return Entry
551 */
552 public function setCreatedAt(\DateTime $createdAt)
553 {
554 $this->createdAt = $createdAt;
555
556 return $this;
557 }
558
559 /**
560 * @return \DateTime
561 */
562 public function getCreatedAt()
563 {
564 return $this->createdAt;
565 }
566
567 /**
568 * @return \DateTime
569 */
570 public function getUpdatedAt()
571 {
572 return $this->updatedAt;
573 }
574
575 /**
576 * @return \DateTime|null
577 */
578 public function getStarredAt()
579 {
580 return $this->starredAt;
581 }
582
583 /**
584 * @param \DateTime|null $starredAt
585 *
586 * @return Entry
587 */
588 public function setStarredAt($starredAt = null)
589 {
590 $this->starredAt = $starredAt;
591
592 return $this;
593 }
594
595 /**
596 * update isStarred and starred_at fields.
597 *
598 * @param bool $isStarred
599 *
600 * @return Entry
601 */
602 public function updateStar($isStarred = false)
603 {
604 $this->setStarred($isStarred);
605 $this->setStarredAt(null);
606 if ($this->isStarred()) {
607 $this->setStarredAt(new \DateTime());
608 }
609
610 return $this;
611 }
612
613 /**
614 * @return ArrayCollection<Annotation>
615 */
616 public function getAnnotations()
617 {
618 return $this->annotations;
619 }
620
621 /**
622 * @param Annotation $annotation
623 */
624 public function setAnnotation(Annotation $annotation)
625 {
626 $this->annotations[] = $annotation;
627 }
628
629 /**
630 * @return string
631 */
632 public function getMimetype()
633 {
634 return $this->mimetype;
635 }
636
637 /**
638 * @param string $mimetype
639 */
640 public function setMimetype($mimetype)
641 {
642 $this->mimetype = $mimetype;
643 }
644
645 /**
646 * @return int
647 */
648 public function getReadingTime()
649 {
650 return $this->readingTime;
651 }
652
653 /**
654 * @param int $readingTime
655 */
656 public function setReadingTime($readingTime)
657 {
658 $this->readingTime = $readingTime;
659 }
660
661 /**
662 * @return string
663 */
664 public function getDomainName()
665 {
666 return $this->domainName;
667 }
668
669 /**
670 * @param string $domainName
671 */
672 public function setDomainName($domainName)
673 {
674 $this->domainName = $domainName;
675 }
676
677 /**
678 * @return ArrayCollection
679 */
680 public function getTags()
681 {
682 return $this->tags;
683 }
684
685 /**
686 * @VirtualProperty
687 * @SerializedName("tags")
688 * @Groups({"entries_for_user", "export_all"})
689 */
690 public function getSerializedTags()
691 {
692 $data = [];
693 foreach ($this->tags as $tag) {
694 $data[] = $tag->getLabel();
695 }
696
697 return $data;
698 }
699
700 /**
701 * @param Tag $tag
702 */
703 public function addTag(Tag $tag)
704 {
705 if ($this->tags->contains($tag)) {
706 return;
707 }
708
709 // check if tag already exist but has not yet be persisted
710 // it seems that the previous condition with `contains()` doesn't check that case
711 foreach ($this->tags as $existingTag) {
712 if ($existingTag->getLabel() === $tag->getLabel()) {
713 return;
714 }
715 }
716
717 $this->tags->add($tag);
718 $tag->addEntry($this);
719 }
720
721 /**
722 * Remove the given tag from the entry (if the tag is associated).
723 *
724 * @param Tag $tag
725 */
726 public function removeTag(Tag $tag)
727 {
728 if (!$this->tags->contains($tag)) {
729 return;
730 }
731
732 $this->tags->removeElement($tag);
733 $tag->removeEntry($this);
734 }
735
736 /**
737 * Remove all assigned tags from the entry.
738 */
739 public function removeAllTags()
740 {
741 foreach ($this->tags as $tag) {
742 $this->tags->removeElement($tag);
743 $tag->removeEntry($this);
744 }
745 }
746
747 /**
748 * Set previewPicture.
749 *
750 * @param string $previewPicture
751 *
752 * @return Entry
753 */
754 public function setPreviewPicture($previewPicture)
755 {
756 $this->previewPicture = $previewPicture;
757
758 return $this;
759 }
760
761 /**
762 * Get previewPicture.
763 *
764 * @return string
765 */
766 public function getPreviewPicture()
767 {
768 return $this->previewPicture;
769 }
770
771 /**
772 * Set language.
773 *
774 * @param string $language
775 *
776 * @return Entry
777 */
778 public function setLanguage($language)
779 {
780 $this->language = $language;
781
782 return $this;
783 }
784
785 /**
786 * Get language.
787 *
788 * @return string
789 */
790 public function getLanguage()
791 {
792 return $this->language;
793 }
794
795 /**
796 * @return string
797 */
798 public function getUid()
799 {
800 return $this->uid;
801 }
802
803 /**
804 * @param string $uid
805 *
806 * @return Entry
807 */
808 public function setUid($uid)
809 {
810 $this->uid = $uid;
811
812 return $this;
813 }
814
815 public function generateUid()
816 {
817 if (null === $this->uid) {
818 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
819 $this->uid = uniqid('', true);
820 }
821 }
822
823 public function cleanUid()
824 {
825 $this->uid = null;
826 }
827
828 /**
829 * Used in the entries filter so it's more explicit for the end user than the uid.
830 * Also used in the API.
831 *
832 * @VirtualProperty
833 * @SerializedName("is_public")
834 * @Groups({"entries_for_user"})
835 *
836 * @return bool
837 */
838 public function isPublic()
839 {
840 return null !== $this->uid;
841 }
842
843 /**
844 * @return string
845 */
846 public function getHttpStatus()
847 {
848 return $this->httpStatus;
849 }
850
851 /**
852 * @param string $httpStatus
853 *
854 * @return Entry
855 */
856 public function setHttpStatus($httpStatus)
857 {
858 $this->httpStatus = $httpStatus;
859
860 return $this;
861 }
862
863 /**
864 * @return \Datetime
865 */
866 public function getPublishedAt()
867 {
868 return $this->publishedAt;
869 }
870
871 /**
872 * @param \Datetime $publishedAt
873 *
874 * @return Entry
875 */
876 public function setPublishedAt(\Datetime $publishedAt)
877 {
878 $this->publishedAt = $publishedAt;
879
880 return $this;
881 }
882
883 /**
884 * @return array
885 */
886 public function getPublishedBy()
887 {
888 return $this->publishedBy;
889 }
890
891 /**
892 * @param array $publishedBy
893 *
894 * @return Entry
895 */
896 public function setPublishedBy($publishedBy)
897 {
898 $this->publishedBy = $publishedBy;
899
900 return $this;
901 }
902
903 /**
904 * @return array
905 */
906 public function getHeaders()
907 {
908 return $this->headers;
909 }
910
911 /**
912 * @param array $headers
913 *
914 * @return Entry
915 */
916 public function setHeaders($headers)
917 {
918 $this->headers = $headers;
919
920 return $this;
921 }
922
923 /**
924 * Set origin url.
925 *
926 * @param string $originUrl
927 *
928 * @return Entry
929 */
930 public function setOriginUrl($originUrl)
931 {
932 $this->originUrl = $originUrl;
933
934 return $this;
935 }
936
937 /**
938 * Get origin url.
939 *
940 * @return string
941 */
942 public function getOriginUrl()
943 {
944 return $this->originUrl;
945 }
946
947 /**
948 * Set origin url.
949 *
950 * @param string $givenUrl
951 *
952 * @return Entry
953 */
954 public function setGivenUrl($givenUrl)
955 {
956 $this->givenUrl = $givenUrl;
957 $this->hashedGivenUrl = UrlHasher::hashUrl($givenUrl);
958
959 return $this;
960 }
961
962 /**
963 * Get origin url.
964 *
965 * @return string
966 */
967 public function getGivenUrl()
968 {
969 return $this->givenUrl;
970 }
971
972 /**
973 * @return string
974 */
975 public function getHashedUrl()
976 {
977 return $this->hashedUrl;
978 }
979
980 /**
981 * @param mixed $hashedUrl
982 *
983 * @return Entry
984 */
985 public function setHashedUrl($hashedUrl)
986 {
987 $this->hashedUrl = $hashedUrl;
988
989 return $this;
990 }
991 }