3 namespace Wallabag\CoreBundle\Entity
;
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
;
23 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository")
26 * options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"},
28 * @ORM\Index(name="created_at", columns={"created_at"}),
29 * @ORM\Index(name="uid", columns={"uid"}),
30 * @ORM\Index(name="hashed_url_user_id", columns={"user_id", "hashed_url"}, options={"lengths"={null, 40}}),
31 * @ORM\Index(name="hashed_given_url_user_id", columns={"user_id", "hashed_given_url"}, options={"lengths"={null, 40}}),
32 * @ORM\Index(name="user_language", columns={"language", "user_id"}),
33 * @ORM\Index(name="user_starred", columns={"user_id", "is_starred", "starred_at"})
36 * @ORM\HasLifecycleCallbacks()
37 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
41 use EntityTimestampsTrait
;
43 /** @Serializer\XmlAttribute */
47 * @ORM\Column(name="id", type="integer")
49 * @ORM\GeneratedValue(strategy="AUTO")
51 * @Groups({"entries_for_user", "export_all"})
58 * @ORM\Column(name="uid", type="string", length=23, nullable=true)
60 * @Groups({"entries_for_user", "export_all"})
67 * @ORM\Column(name="title", type="text", nullable=true)
69 * @Groups({"entries_for_user", "export_all"})
74 * Define the url fetched by wallabag (the final url after potential redirections).
79 * @ORM\Column(name="url", type="text", nullable=true)
81 * @Groups({"entries_for_user", "export_all"})
88 * @ORM\Column(name="hashed_url", type="string", length=40, nullable=true)
93 * From where user retrieved/found the url (an other article, a twitter, or the given_url if non are provided).
97 * @ORM\Column(name="origin_url", type="text", nullable=true)
99 * @Groups({"entries_for_user", "export_all"})
104 * Define the url entered by the user (without redirections).
108 * @ORM\Column(name="given_url", type="text", nullable=true)
110 * @Groups({"entries_for_user", "export_all"})
117 * @ORM\Column(name="hashed_given_url", type="string", length=40, nullable=true)
119 private $hashedGivenUrl;
126 * @ORM\Column(name="is_archived", type="boolean")
128 * @Groups({"entries_for_user", "export_all"})
130 private $isArchived = false;
135 * @ORM\Column(name="archived_at", type="datetime", nullable=true)
137 * @Groups({"entries_for_user", "export_all"})
139 private $archivedAt = null;
146 * @ORM\Column(name="is_starred", type="boolean")
148 * @Groups({"entries_for_user", "export_all"})
150 private $isStarred = false;
155 * @ORM\Column(name="content", type="text", nullable=true)
157 * @Groups({"entries_for_user", "export_all"})
164 * @ORM\Column(name="created_at", type="datetime")
166 * @Groups({"entries_for_user", "export_all"})
173 * @ORM\Column(name="updated_at", type="datetime")
175 * @Groups({"entries_for_user", "export_all"})
182 * @ORM\Column(name="published_at", type="datetime", nullable=true)
184 * @Groups({"entries_for_user", "export_all"})
186 private $publishedAt;
191 * @ORM\Column(name="published_by", type="array", nullable=true)
193 * @Groups({"entries_for_user", "export_all"})
195 private $publishedBy;
200 * @ORM\Column(name="starred_at", type="datetime", nullable=true)
202 * @Groups({"entries_for_user", "export_all"})
204 private $starredAt = null;
207 * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"})
210 * @Groups({"entries_for_user", "export_all"})
212 private $annotations;
217 * @ORM\Column(name="mimetype", type="text", nullable=true)
219 * @Groups({"entries_for_user", "export_all"})
226 * @ORM\Column(name="language", type="string", length=20, nullable=true)
228 * @Groups({"entries_for_user", "export_all"})
235 * @ORM\Column(name="reading_time", type="integer", nullable=false)
237 * @Groups({"entries_for_user", "export_all"})
239 private $readingTime = 0;
244 * @ORM\Column(name="domain_name", type="text", nullable=true)
246 * @Groups({"entries_for_user", "export_all"})
253 * @ORM\Column(name="preview_picture", type="text", nullable=true)
255 * @Groups({"entries_for_user", "export_all"})
257 private $previewPicture;
262 * @ORM\Column(name="http_status", type="string", length=3, nullable=true)
264 * @Groups({"entries_for_user", "export_all"})
271 * @ORM\Column(name="headers", type="array", nullable=true)
273 * @Groups({"entries_for_user", "export_all"})
280 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
282 * @Groups({"export_all"})
287 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
291 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
293 * inverseJoinColumns={
294 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
303 public function __construct(User
$user)
306 $this->tags
= new ArrayCollection();
314 public function getId()
322 * @param string $title
326 public function setTitle($title)
328 $this->title
= $title;
338 public function getTitle()
350 public function setUrl($url)
353 $this->hashedUrl
= UrlHasher
::hashUrl($url);
363 public function getUrl()
371 * @param bool $isArchived
375 public function setArchived($isArchived)
377 $this->isArchived
= $isArchived;
383 * update isArchived and archive_at fields.
385 * @param bool $isArchived
389 public function updateArchived($isArchived = false)
391 $this->setArchived($isArchived);
392 $this->setArchivedAt(null);
393 if ($this->isArchived()) {
394 $this->setArchivedAt(new \
DateTime());
401 * @return \DateTime|null
403 public function getArchivedAt()
405 return $this->archivedAt
;
409 * @param \DateTime|null $archivedAt
413 public function setArchivedAt($archivedAt = null)
415 $this->archivedAt
= $archivedAt;
425 public function isArchived()
427 return $this->isArchived
;
432 * @SerializedName("is_archived")
433 * @Groups({"entries_for_user", "export_all"})
435 public function is_Archived()
437 return (int) $this->isArchived();
440 public function toggleArchive()
442 $this->updateArchived($this->isArchived() ^
1);
450 * @param bool $isStarred
454 public function setStarred($isStarred)
456 $this->isStarred
= $isStarred;
466 public function isStarred()
468 return $this->isStarred
;
473 * @SerializedName("is_starred")
474 * @Groups({"entries_for_user", "export_all"})
476 public function is_Starred()
478 return (int) $this->isStarred();
481 public function toggleStar()
483 $this->isStarred
= $this->isStarred() ^
1;
491 * @param string $content
495 public function setContent($content)
497 $this->content
= $content;
507 public function getContent()
509 return $this->content
;
515 public function getUser()
522 * @SerializedName("user_name")
524 public function getUserName()
526 return $this->user
->getUserName();
531 * @SerializedName("user_email")
533 public function getUserEmail()
535 return $this->user
->getEmail();
540 * @SerializedName("user_id")
542 public function getUserId()
544 return $this->user
->getId();
549 * Only used when importing data from an other service.
551 * @param \DateTime $createdAt
555 public function setCreatedAt(\DateTime
$createdAt)
557 $this->createdAt
= $createdAt;
565 public function getCreatedAt()
567 return $this->createdAt
;
573 public function getUpdatedAt()
575 return $this->updatedAt
;
579 * @return \DateTime|null
581 public function getStarredAt()
583 return $this->starredAt
;
587 * @param \DateTime|null $starredAt
591 public function setStarredAt($starredAt = null)
593 $this->starredAt
= $starredAt;
599 * update isStarred and starred_at fields.
601 * @param bool $isStarred
605 public function updateStar($isStarred = false)
607 $this->setStarred($isStarred);
608 $this->setStarredAt(null);
609 if ($this->isStarred()) {
610 $this->setStarredAt(new \
DateTime());
617 * @return ArrayCollection<Annotation>
619 public function getAnnotations()
621 return $this->annotations
;
625 * @param Annotation $annotation
627 public function setAnnotation(Annotation
$annotation)
629 $this->annotations
[] = $annotation;
635 public function getMimetype()
637 return $this->mimetype
;
641 * @param string $mimetype
643 public function setMimetype($mimetype)
645 $this->mimetype
= $mimetype;
651 public function getReadingTime()
653 return $this->readingTime
;
657 * @param int $readingTime
659 public function setReadingTime($readingTime)
661 $this->readingTime
= $readingTime;
667 public function getDomainName()
669 return $this->domainName
;
673 * @param string $domainName
675 public function setDomainName($domainName)
677 $this->domainName
= $domainName;
681 * @return ArrayCollection
683 public function getTags()
690 * @SerializedName("tags")
691 * @Groups({"entries_for_user", "export_all"})
693 public function getSerializedTags()
696 foreach ($this->tags
as $tag) {
697 $data[] = $tag->getLabel();
706 public function addTag(Tag
$tag)
708 if ($this->tags
->contains($tag)) {
712 // check if tag already exist but has not yet be persisted
713 // it seems that the previous condition with `contains()` doesn't check that case
714 foreach ($this->tags
as $existingTag) {
715 if ($existingTag->getLabel() === $tag->getLabel()) {
720 $this->tags
->add($tag);
721 $tag->addEntry($this);
725 * Remove the given tag from the entry (if the tag is associated).
729 public function removeTag(Tag
$tag)
731 if (!$this->tags
->contains($tag)) {
735 $this->tags
->removeElement($tag);
736 $tag->removeEntry($this);
740 * Remove all assigned tags from the entry.
742 public function removeAllTags()
744 foreach ($this->tags
as $tag) {
745 $this->tags
->removeElement($tag);
746 $tag->removeEntry($this);
751 * Set previewPicture.
753 * @param string $previewPicture
757 public function setPreviewPicture($previewPicture)
759 $this->previewPicture
= $previewPicture;
765 * Get previewPicture.
769 public function getPreviewPicture()
771 return $this->previewPicture
;
777 * @param string $language
781 public function setLanguage($language)
783 $this->language
= $language;
793 public function getLanguage()
795 return $this->language
;
799 * @return string|null
801 public function getUid()
811 public function setUid($uid)
818 public function generateUid()
820 if (null === $this->uid
) {
821 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
822 $this->uid
= uniqid('', true);
826 public function cleanUid()
832 * Used in the entries filter so it's more explicit for the end user than the uid.
833 * Also used in the API.
836 * @SerializedName("is_public")
837 * @Groups({"entries_for_user"})
841 public function isPublic()
843 return null !== $this->uid
;
849 public function getHttpStatus()
851 return $this->httpStatus
;
855 * @param string $httpStatus
859 public function setHttpStatus($httpStatus)
861 $this->httpStatus
= $httpStatus;
869 public function getPublishedAt()
871 return $this->publishedAt
;
875 * @param \Datetime $publishedAt
879 public function setPublishedAt(\Datetime
$publishedAt)
881 $this->publishedAt
= $publishedAt;
889 public function getPublishedBy()
891 return $this->publishedBy
;
895 * @param array $publishedBy
899 public function setPublishedBy($publishedBy)
901 $this->publishedBy
= $publishedBy;
909 public function getHeaders()
911 return $this->headers
;
915 * @param array $headers
919 public function setHeaders($headers)
921 $this->headers
= $headers;
929 * @param string $originUrl
933 public function setOriginUrl($originUrl)
935 $this->originUrl
= $originUrl;
945 public function getOriginUrl()
947 return $this->originUrl
;
953 * @param string $givenUrl
957 public function setGivenUrl($givenUrl)
959 $this->givenUrl
= $givenUrl;
960 $this->hashedGivenUrl
= UrlHasher
::hashUrl($givenUrl);
970 public function getGivenUrl()
972 return $this->givenUrl
;
978 public function getHashedUrl()
980 return $this->hashedUrl
;
984 * @param mixed $hashedUrl
988 public function setHashedUrl($hashedUrl)
990 $this->hashedUrl
= $hashedUrl;