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"})
35 * @ORM\HasLifecycleCallbacks()
36 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
40 use EntityTimestampsTrait
;
42 /** @Serializer\XmlAttribute */
46 * @ORM\Column(name="id", type="integer")
48 * @ORM\GeneratedValue(strategy="AUTO")
50 * @Groups({"entries_for_user", "export_all"})
57 * @ORM\Column(name="uid", type="string", length=23, nullable=true)
59 * @Groups({"entries_for_user", "export_all"})
66 * @ORM\Column(name="title", type="text", nullable=true)
68 * @Groups({"entries_for_user", "export_all"})
73 * Define the url fetched by wallabag (the final url after potential redirections).
78 * @ORM\Column(name="url", type="text", nullable=true)
80 * @Groups({"entries_for_user", "export_all"})
87 * @ORM\Column(name="hashed_url", type="string", length=40, nullable=true)
92 * From where user retrieved/found the url (an other article, a twitter, or the given_url if non are provided).
96 * @ORM\Column(name="origin_url", type="text", nullable=true)
98 * @Groups({"entries_for_user", "export_all"})
103 * Define the url entered by the user (without redirections).
107 * @ORM\Column(name="given_url", type="text", nullable=true)
109 * @Groups({"entries_for_user", "export_all"})
116 * @ORM\Column(name="hashed_given_url", type="string", length=40, nullable=true)
118 private $hashedGivenUrl;
125 * @ORM\Column(name="is_archived", type="boolean")
127 * @Groups({"entries_for_user", "export_all"})
129 private $isArchived = false;
134 * @ORM\Column(name="archived_at", type="datetime", nullable=true)
136 * @Groups({"entries_for_user", "export_all"})
138 private $archivedAt = null;
145 * @ORM\Column(name="is_starred", type="boolean")
147 * @Groups({"entries_for_user", "export_all"})
149 private $isStarred = false;
154 * @ORM\Column(name="content", type="text", nullable=true)
156 * @Groups({"entries_for_user", "export_all"})
163 * @ORM\Column(name="created_at", type="datetime")
165 * @Groups({"entries_for_user", "export_all"})
172 * @ORM\Column(name="updated_at", type="datetime")
174 * @Groups({"entries_for_user", "export_all"})
181 * @ORM\Column(name="published_at", type="datetime", nullable=true)
183 * @Groups({"entries_for_user", "export_all"})
185 private $publishedAt;
190 * @ORM\Column(name="published_by", type="array", nullable=true)
192 * @Groups({"entries_for_user", "export_all"})
194 private $publishedBy;
199 * @ORM\Column(name="starred_at", type="datetime", nullable=true)
201 * @Groups({"entries_for_user", "export_all"})
203 private $starredAt = null;
206 * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"})
209 * @Groups({"entries_for_user", "export_all"})
211 private $annotations;
216 * @ORM\Column(name="mimetype", type="text", nullable=true)
218 * @Groups({"entries_for_user", "export_all"})
225 * @ORM\Column(name="language", type="string", length=20, nullable=true)
227 * @Groups({"entries_for_user", "export_all"})
234 * @ORM\Column(name="reading_time", type="integer", nullable=false)
236 * @Groups({"entries_for_user", "export_all"})
238 private $readingTime = 0;
243 * @ORM\Column(name="domain_name", type="text", nullable=true)
245 * @Groups({"entries_for_user", "export_all"})
252 * @ORM\Column(name="preview_picture", type="text", nullable=true)
254 * @Groups({"entries_for_user", "export_all"})
256 private $previewPicture;
261 * @ORM\Column(name="http_status", type="string", length=3, nullable=true)
263 * @Groups({"entries_for_user", "export_all"})
270 * @ORM\Column(name="headers", type="array", nullable=true)
272 * @Groups({"entries_for_user", "export_all"})
279 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
281 * @Groups({"export_all"})
286 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
290 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
292 * inverseJoinColumns={
293 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
302 public function __construct(User
$user)
305 $this->tags
= new ArrayCollection();
313 public function getId()
321 * @param string $title
325 public function setTitle($title)
327 $this->title
= $title;
337 public function getTitle()
349 public function setUrl($url)
352 $this->hashedUrl
= UrlHasher
::hashUrl($url);
362 public function getUrl()
370 * @param bool $isArchived
374 public function setArchived($isArchived)
376 $this->isArchived
= $isArchived;
382 * update isArchived and archive_at fields.
384 * @param bool $isArchived
388 public function updateArchived($isArchived = false)
390 $this->setArchived($isArchived);
391 $this->setArchivedAt(null);
392 if ($this->isArchived()) {
393 $this->setArchivedAt(new \
DateTime());
400 * @return \DateTime|null
402 public function getArchivedAt()
404 return $this->archivedAt
;
408 * @param \DateTime|null $archivedAt
412 public function setArchivedAt($archivedAt = null)
414 $this->archivedAt
= $archivedAt;
424 public function isArchived()
426 return $this->isArchived
;
431 * @SerializedName("is_archived")
432 * @Groups({"entries_for_user", "export_all"})
434 public function is_Archived()
436 return (int) $this->isArchived();
439 public function toggleArchive()
441 $this->updateArchived($this->isArchived() ^
1);
449 * @param bool $isStarred
453 public function setStarred($isStarred)
455 $this->isStarred
= $isStarred;
465 public function isStarred()
467 return $this->isStarred
;
472 * @SerializedName("is_starred")
473 * @Groups({"entries_for_user", "export_all"})
475 public function is_Starred()
477 return (int) $this->isStarred();
480 public function toggleStar()
482 $this->isStarred
= $this->isStarred() ^
1;
490 * @param string $content
494 public function setContent($content)
496 $this->content
= $content;
506 public function getContent()
508 return $this->content
;
514 public function getUser()
521 * @SerializedName("user_name")
523 public function getUserName()
525 return $this->user
->getUserName();
530 * @SerializedName("user_email")
532 public function getUserEmail()
534 return $this->user
->getEmail();
539 * @SerializedName("user_id")
541 public function getUserId()
543 return $this->user
->getId();
548 * Only used when importing data from an other service.
550 * @param \DateTime $createdAt
554 public function setCreatedAt(\DateTime
$createdAt)
556 $this->createdAt
= $createdAt;
564 public function getCreatedAt()
566 return $this->createdAt
;
572 public function getUpdatedAt()
574 return $this->updatedAt
;
578 * @return \DateTime|null
580 public function getStarredAt()
582 return $this->starredAt
;
586 * @param \DateTime|null $starredAt
590 public function setStarredAt($starredAt = null)
592 $this->starredAt
= $starredAt;
598 * update isStarred and starred_at fields.
600 * @param bool $isStarred
604 public function updateStar($isStarred = false)
606 $this->setStarred($isStarred);
607 $this->setStarredAt(null);
608 if ($this->isStarred()) {
609 $this->setStarredAt(new \
DateTime());
616 * @return ArrayCollection<Annotation>
618 public function getAnnotations()
620 return $this->annotations
;
624 * @param Annotation $annotation
626 public function setAnnotation(Annotation
$annotation)
628 $this->annotations
[] = $annotation;
634 public function getMimetype()
636 return $this->mimetype
;
640 * @param string $mimetype
642 public function setMimetype($mimetype)
644 $this->mimetype
= $mimetype;
650 public function getReadingTime()
652 return $this->readingTime
;
656 * @param int $readingTime
658 public function setReadingTime($readingTime)
660 $this->readingTime
= $readingTime;
666 public function getDomainName()
668 return $this->domainName
;
672 * @param string $domainName
674 public function setDomainName($domainName)
676 $this->domainName
= $domainName;
680 * @return ArrayCollection
682 public function getTags()
689 * @SerializedName("tags")
690 * @Groups({"entries_for_user", "export_all"})
692 public function getSerializedTags()
695 foreach ($this->tags
as $tag) {
696 $data[] = $tag->getLabel();
705 public function addTag(Tag
$tag)
707 if ($this->tags
->contains($tag)) {
711 // check if tag already exist but has not yet be persisted
712 // it seems that the previous condition with `contains()` doesn't check that case
713 foreach ($this->tags
as $existingTag) {
714 if ($existingTag->getLabel() === $tag->getLabel()) {
719 $this->tags
->add($tag);
720 $tag->addEntry($this);
724 * Remove the given tag from the entry (if the tag is associated).
728 public function removeTag(Tag
$tag)
730 if (!$this->tags
->contains($tag)) {
734 $this->tags
->removeElement($tag);
735 $tag->removeEntry($this);
739 * Remove all assigned tags from the entry.
741 public function removeAllTags()
743 foreach ($this->tags
as $tag) {
744 $this->tags
->removeElement($tag);
745 $tag->removeEntry($this);
750 * Set previewPicture.
752 * @param string $previewPicture
756 public function setPreviewPicture($previewPicture)
758 $this->previewPicture
= $previewPicture;
764 * Get previewPicture.
768 public function getPreviewPicture()
770 return $this->previewPicture
;
776 * @param string $language
780 public function setLanguage($language)
782 $this->language
= $language;
792 public function getLanguage()
794 return $this->language
;
798 * @return string|null
800 public function getUid()
810 public function setUid($uid)
817 public function generateUid()
819 if (null === $this->uid
) {
820 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
821 $this->uid
= uniqid('', true);
825 public function cleanUid()
831 * Used in the entries filter so it's more explicit for the end user than the uid.
832 * Also used in the API.
835 * @SerializedName("is_public")
836 * @Groups({"entries_for_user"})
840 public function isPublic()
842 return null !== $this->uid
;
848 public function getHttpStatus()
850 return $this->httpStatus
;
854 * @param string $httpStatus
858 public function setHttpStatus($httpStatus)
860 $this->httpStatus
= $httpStatus;
868 public function getPublishedAt()
870 return $this->publishedAt
;
874 * @param \Datetime $publishedAt
878 public function setPublishedAt(\Datetime
$publishedAt)
880 $this->publishedAt
= $publishedAt;
888 public function getPublishedBy()
890 return $this->publishedBy
;
894 * @param array $publishedBy
898 public function setPublishedBy($publishedBy)
900 $this->publishedBy
= $publishedBy;
908 public function getHeaders()
910 return $this->headers
;
914 * @param array $headers
918 public function setHeaders($headers)
920 $this->headers
= $headers;
928 * @param string $originUrl
932 public function setOriginUrl($originUrl)
934 $this->originUrl
= $originUrl;
944 public function getOriginUrl()
946 return $this->originUrl
;
952 * @param string $givenUrl
956 public function setGivenUrl($givenUrl)
958 $this->givenUrl
= $givenUrl;
959 $this->hashedGivenUrl
= UrlHasher
::hashUrl($givenUrl);
969 public function getGivenUrl()
971 return $this->givenUrl
;
977 public function getHashedUrl()
979 return $this->hashedUrl
;
983 * @param mixed $hashedUrl
987 public function setHashedUrl($hashedUrl)
989 $this->hashedUrl
= $hashedUrl;