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}})
34 * @ORM\HasLifecycleCallbacks()
35 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
39 use EntityTimestampsTrait
;
41 /** @Serializer\XmlAttribute */
45 * @ORM\Column(name="id", type="integer")
47 * @ORM\GeneratedValue(strategy="AUTO")
49 * @Groups({"entries_for_user", "export_all"})
56 * @ORM\Column(name="uid", type="string", length=23, nullable=true)
58 * @Groups({"entries_for_user", "export_all"})
65 * @ORM\Column(name="title", type="text", nullable=true)
67 * @Groups({"entries_for_user", "export_all"})
72 * Define the url fetched by wallabag (the final url after potential redirections).
77 * @ORM\Column(name="url", type="text", nullable=true)
79 * @Groups({"entries_for_user", "export_all"})
86 * @ORM\Column(name="hashed_url", type="string", length=40, nullable=true)
91 * From where user retrieved/found the url (an other article, a twitter, or the given_url if non are provided).
95 * @ORM\Column(name="origin_url", type="text", nullable=true)
97 * @Groups({"entries_for_user", "export_all"})
102 * Define the url entered by the user (without redirections).
106 * @ORM\Column(name="given_url", type="text", nullable=true)
108 * @Groups({"entries_for_user", "export_all"})
115 * @ORM\Column(name="hashed_given_url", type="string", length=40, nullable=true)
117 private $hashedGivenUrl;
124 * @ORM\Column(name="is_archived", type="boolean")
126 * @Groups({"entries_for_user", "export_all"})
128 private $isArchived = false;
133 * @ORM\Column(name="archived_at", type="datetime", nullable=true)
135 * @Groups({"entries_for_user", "export_all"})
137 private $archivedAt = null;
144 * @ORM\Column(name="is_starred", type="boolean")
146 * @Groups({"entries_for_user", "export_all"})
148 private $isStarred = false;
153 * @ORM\Column(name="content", type="text", nullable=true)
155 * @Groups({"entries_for_user", "export_all"})
162 * @ORM\Column(name="created_at", type="datetime")
164 * @Groups({"entries_for_user", "export_all"})
171 * @ORM\Column(name="updated_at", type="datetime")
173 * @Groups({"entries_for_user", "export_all"})
180 * @ORM\Column(name="published_at", type="datetime", nullable=true)
182 * @Groups({"entries_for_user", "export_all"})
184 private $publishedAt;
189 * @ORM\Column(name="published_by", type="array", nullable=true)
191 * @Groups({"entries_for_user", "export_all"})
193 private $publishedBy;
198 * @ORM\Column(name="starred_at", type="datetime", nullable=true)
200 * @Groups({"entries_for_user", "export_all"})
202 private $starredAt = null;
205 * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"})
208 * @Groups({"entries_for_user", "export_all"})
210 private $annotations;
215 * @ORM\Column(name="mimetype", type="text", nullable=true)
217 * @Groups({"entries_for_user", "export_all"})
224 * @ORM\Column(name="language", type="text", nullable=true)
226 * @Groups({"entries_for_user", "export_all"})
233 * @ORM\Column(name="reading_time", type="integer", nullable=false)
235 * @Groups({"entries_for_user", "export_all"})
237 private $readingTime = 0;
242 * @ORM\Column(name="domain_name", type="text", nullable=true)
244 * @Groups({"entries_for_user", "export_all"})
251 * @ORM\Column(name="preview_picture", type="text", nullable=true)
253 * @Groups({"entries_for_user", "export_all"})
255 private $previewPicture;
260 * @ORM\Column(name="http_status", type="string", length=3, nullable=true)
262 * @Groups({"entries_for_user", "export_all"})
269 * @ORM\Column(name="headers", type="array", nullable=true)
271 * @Groups({"entries_for_user", "export_all"})
278 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
280 * @Groups({"export_all"})
285 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
289 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
291 * inverseJoinColumns={
292 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
301 public function __construct(User
$user)
304 $this->tags
= new ArrayCollection();
312 public function getId()
320 * @param string $title
324 public function setTitle($title)
326 $this->title
= $title;
336 public function getTitle()
348 public function setUrl($url)
351 $this->hashedUrl
= UrlHasher
::hashUrl($url);
361 public function getUrl()
369 * @param bool $isArchived
373 public function setArchived($isArchived)
375 $this->isArchived
= $isArchived;
381 * update isArchived and archive_at fields.
383 * @param bool $isArchived
387 public function updateArchived($isArchived = false)
389 $this->setArchived($isArchived);
390 $this->setArchivedAt(null);
391 if ($this->isArchived()) {
392 $this->setArchivedAt(new \
DateTime());
399 * @return \DateTime|null
401 public function getArchivedAt()
403 return $this->archivedAt
;
407 * @param \DateTime|null $archivedAt
411 public function setArchivedAt($archivedAt = null)
413 $this->archivedAt
= $archivedAt;
423 public function isArchived()
425 return $this->isArchived
;
430 * @SerializedName("is_archived")
431 * @Groups({"entries_for_user", "export_all"})
433 public function is_Archived()
435 return (int) $this->isArchived();
438 public function toggleArchive()
440 $this->updateArchived($this->isArchived() ^
1);
448 * @param bool $isStarred
452 public function setStarred($isStarred)
454 $this->isStarred
= $isStarred;
464 public function isStarred()
466 return $this->isStarred
;
471 * @SerializedName("is_starred")
472 * @Groups({"entries_for_user", "export_all"})
474 public function is_Starred()
476 return (int) $this->isStarred();
479 public function toggleStar()
481 $this->isStarred
= $this->isStarred() ^
1;
489 * @param string $content
493 public function setContent($content)
495 $this->content
= $content;
505 public function getContent()
507 return $this->content
;
513 public function getUser()
520 * @SerializedName("user_name")
522 public function getUserName()
524 return $this->user
->getUserName();
529 * @SerializedName("user_email")
531 public function getUserEmail()
533 return $this->user
->getEmail();
538 * @SerializedName("user_id")
540 public function getUserId()
542 return $this->user
->getId();
547 * Only used when importing data from an other service.
549 * @param \DateTime $createdAt
553 public function setCreatedAt(\DateTime
$createdAt)
555 $this->createdAt
= $createdAt;
563 public function getCreatedAt()
565 return $this->createdAt
;
571 public function getUpdatedAt()
573 return $this->updatedAt
;
577 * @return \DateTime|null
579 public function getStarredAt()
581 return $this->starredAt
;
585 * @param \DateTime|null $starredAt
589 public function setStarredAt($starredAt = null)
591 $this->starredAt
= $starredAt;
597 * update isStarred and starred_at fields.
599 * @param bool $isStarred
603 public function updateStar($isStarred = false)
605 $this->setStarred($isStarred);
606 $this->setStarredAt(null);
607 if ($this->isStarred()) {
608 $this->setStarredAt(new \
DateTime());
615 * @return ArrayCollection<Annotation>
617 public function getAnnotations()
619 return $this->annotations
;
623 * @param Annotation $annotation
625 public function setAnnotation(Annotation
$annotation)
627 $this->annotations
[] = $annotation;
633 public function getMimetype()
635 return $this->mimetype
;
639 * @param string $mimetype
641 public function setMimetype($mimetype)
643 $this->mimetype
= $mimetype;
649 public function getReadingTime()
651 return $this->readingTime
;
655 * @param int $readingTime
657 public function setReadingTime($readingTime)
659 $this->readingTime
= $readingTime;
665 public function getDomainName()
667 return $this->domainName
;
671 * @param string $domainName
673 public function setDomainName($domainName)
675 $this->domainName
= $domainName;
679 * @return ArrayCollection
681 public function getTags()
688 * @SerializedName("tags")
689 * @Groups({"entries_for_user", "export_all"})
691 public function getSerializedTags()
694 foreach ($this->tags
as $tag) {
695 $data[] = $tag->getLabel();
704 public function addTag(Tag
$tag)
706 if ($this->tags
->contains($tag)) {
710 // check if tag already exist but has not yet be persisted
711 // it seems that the previous condition with `contains()` doesn't check that case
712 foreach ($this->tags
as $existingTag) {
713 if ($existingTag->getLabel() === $tag->getLabel()) {
718 $this->tags
->add($tag);
719 $tag->addEntry($this);
723 * Remove the given tag from the entry (if the tag is associated).
727 public function removeTag(Tag
$tag)
729 if (!$this->tags
->contains($tag)) {
733 $this->tags
->removeElement($tag);
734 $tag->removeEntry($this);
738 * Remove all assigned tags from the entry.
740 public function removeAllTags()
742 foreach ($this->tags
as $tag) {
743 $this->tags
->removeElement($tag);
744 $tag->removeEntry($this);
749 * Set previewPicture.
751 * @param string $previewPicture
755 public function setPreviewPicture($previewPicture)
757 $this->previewPicture
= $previewPicture;
763 * Get previewPicture.
767 public function getPreviewPicture()
769 return $this->previewPicture
;
775 * @param string $language
779 public function setLanguage($language)
781 $this->language
= $language;
791 public function getLanguage()
793 return $this->language
;
799 public function getUid()
809 public function setUid($uid)
816 public function generateUid()
818 if (null === $this->uid
) {
819 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
820 $this->uid
= uniqid('', true);
824 public function cleanUid()
830 * Used in the entries filter so it's more explicit for the end user than the uid.
831 * Also used in the API.
834 * @SerializedName("is_public")
835 * @Groups({"entries_for_user"})
839 public function isPublic()
841 return null !== $this->uid
;
847 public function getHttpStatus()
849 return $this->httpStatus
;
853 * @param string $httpStatus
857 public function setHttpStatus($httpStatus)
859 $this->httpStatus
= $httpStatus;
867 public function getPublishedAt()
869 return $this->publishedAt
;
873 * @param \Datetime $publishedAt
877 public function setPublishedAt(\Datetime
$publishedAt)
879 $this->publishedAt
= $publishedAt;
887 public function getPublishedBy()
889 return $this->publishedBy
;
893 * @param array $publishedBy
897 public function setPublishedBy($publishedBy)
899 $this->publishedBy
= $publishedBy;
907 public function getHeaders()
909 return $this->headers
;
913 * @param array $headers
917 public function setHeaders($headers)
919 $this->headers
= $headers;
927 * @param string $originUrl
931 public function setOriginUrl($originUrl)
933 $this->originUrl
= $originUrl;
943 public function getOriginUrl()
945 return $this->originUrl
;
951 * @param string $givenUrl
955 public function setGivenUrl($givenUrl)
957 $this->givenUrl
= $givenUrl;
958 $this->hashedGivenUrl
= UrlHasher
::hashUrl($givenUrl);
968 public function getGivenUrl()
970 return $this->givenUrl
;
976 public function getHashedUrl()
978 return $this->hashedUrl
;
982 * @param mixed $hashedUrl
986 public function setHashedUrl($hashedUrl)
988 $this->hashedUrl
= $hashedUrl;