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_archived", columns={"user_id", "is_archived", "archived_at"}),
34 * @ORM\Index(name="user_starred", columns={"user_id", "is_starred", "starred_at"})
37 * @ORM\HasLifecycleCallbacks()
38 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
42 use EntityTimestampsTrait
;
44 /** @Serializer\XmlAttribute */
48 * @ORM\Column(name="id", type="integer")
50 * @ORM\GeneratedValue(strategy="AUTO")
52 * @Groups({"entries_for_user", "export_all"})
59 * @ORM\Column(name="uid", type="string", length=23, nullable=true)
61 * @Groups({"entries_for_user", "export_all"})
68 * @ORM\Column(name="title", type="text", nullable=true)
70 * @Groups({"entries_for_user", "export_all"})
75 * Define the url fetched by wallabag (the final url after potential redirections).
80 * @ORM\Column(name="url", type="text", nullable=true)
82 * @Groups({"entries_for_user", "export_all"})
89 * @ORM\Column(name="hashed_url", type="string", length=40, nullable=true)
94 * From where user retrieved/found the url (an other article, a twitter, or the given_url if non are provided).
98 * @ORM\Column(name="origin_url", type="text", nullable=true)
100 * @Groups({"entries_for_user", "export_all"})
105 * Define the url entered by the user (without redirections).
109 * @ORM\Column(name="given_url", type="text", nullable=true)
111 * @Groups({"entries_for_user", "export_all"})
118 * @ORM\Column(name="hashed_given_url", type="string", length=40, nullable=true)
120 private $hashedGivenUrl;
127 * @ORM\Column(name="is_archived", type="boolean")
129 * @Groups({"entries_for_user", "export_all"})
131 private $isArchived = false;
136 * @ORM\Column(name="archived_at", type="datetime", nullable=true)
138 * @Groups({"entries_for_user", "export_all"})
140 private $archivedAt = null;
147 * @ORM\Column(name="is_starred", type="boolean")
149 * @Groups({"entries_for_user", "export_all"})
151 private $isStarred = false;
156 * @ORM\Column(name="content", type="text", nullable=true)
158 * @Groups({"entries_for_user", "export_all"})
165 * @ORM\Column(name="created_at", type="datetime")
167 * @Groups({"entries_for_user", "export_all"})
174 * @ORM\Column(name="updated_at", type="datetime")
176 * @Groups({"entries_for_user", "export_all"})
183 * @ORM\Column(name="published_at", type="datetime", nullable=true)
185 * @Groups({"entries_for_user", "export_all"})
187 private $publishedAt;
192 * @ORM\Column(name="published_by", type="array", nullable=true)
194 * @Groups({"entries_for_user", "export_all"})
196 private $publishedBy;
201 * @ORM\Column(name="starred_at", type="datetime", nullable=true)
203 * @Groups({"entries_for_user", "export_all"})
205 private $starredAt = null;
208 * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"})
211 * @Groups({"entries_for_user", "export_all"})
213 private $annotations;
218 * @ORM\Column(name="mimetype", type="text", nullable=true)
220 * @Groups({"entries_for_user", "export_all"})
227 * @ORM\Column(name="language", type="string", length=20, nullable=true)
229 * @Groups({"entries_for_user", "export_all"})
236 * @ORM\Column(name="reading_time", type="integer", nullable=false)
238 * @Groups({"entries_for_user", "export_all"})
240 private $readingTime = 0;
245 * @ORM\Column(name="domain_name", type="text", nullable=true)
247 * @Groups({"entries_for_user", "export_all"})
254 * @ORM\Column(name="preview_picture", type="text", nullable=true)
256 * @Groups({"entries_for_user", "export_all"})
258 private $previewPicture;
263 * @ORM\Column(name="http_status", type="string", length=3, nullable=true)
265 * @Groups({"entries_for_user", "export_all"})
272 * @ORM\Column(name="headers", type="array", nullable=true)
274 * @Groups({"entries_for_user", "export_all"})
281 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
283 * @Groups({"export_all"})
288 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
292 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
294 * inverseJoinColumns={
295 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
304 public function __construct(User
$user)
307 $this->tags
= new ArrayCollection();
315 public function getId()
323 * @param string $title
327 public function setTitle($title)
329 $this->title
= $title;
339 public function getTitle()
351 public function setUrl($url)
354 $this->hashedUrl
= UrlHasher
::hashUrl($url);
364 public function getUrl()
372 * @param bool $isArchived
376 public function setArchived($isArchived)
378 $this->isArchived
= $isArchived;
384 * update isArchived and archive_at fields.
386 * @param bool $isArchived
390 public function updateArchived($isArchived = false)
392 $this->setArchived($isArchived);
393 $this->setArchivedAt(null);
394 if ($this->isArchived()) {
395 $this->setArchivedAt(new \
DateTime());
402 * @return \DateTime|null
404 public function getArchivedAt()
406 return $this->archivedAt
;
410 * @param \DateTime|null $archivedAt
414 public function setArchivedAt($archivedAt = null)
416 $this->archivedAt
= $archivedAt;
426 public function isArchived()
428 return $this->isArchived
;
433 * @SerializedName("is_archived")
434 * @Groups({"entries_for_user", "export_all"})
436 public function is_Archived()
438 return (int) $this->isArchived();
441 public function toggleArchive()
443 $this->updateArchived($this->isArchived() ^
1);
451 * @param bool $isStarred
455 public function setStarred($isStarred)
457 $this->isStarred
= $isStarred;
467 public function isStarred()
469 return $this->isStarred
;
474 * @SerializedName("is_starred")
475 * @Groups({"entries_for_user", "export_all"})
477 public function is_Starred()
479 return (int) $this->isStarred();
482 public function toggleStar()
484 $this->isStarred
= $this->isStarred() ^
1;
492 * @param string $content
496 public function setContent($content)
498 $this->content
= $content;
508 public function getContent()
510 return $this->content
;
516 public function getUser()
523 * @SerializedName("user_name")
525 public function getUserName()
527 return $this->user
->getUserName();
532 * @SerializedName("user_email")
534 public function getUserEmail()
536 return $this->user
->getEmail();
541 * @SerializedName("user_id")
543 public function getUserId()
545 return $this->user
->getId();
550 * Only used when importing data from an other service.
552 * @param \DateTime $createdAt
556 public function setCreatedAt(\DateTime
$createdAt)
558 $this->createdAt
= $createdAt;
566 public function getCreatedAt()
568 return $this->createdAt
;
574 public function getUpdatedAt()
576 return $this->updatedAt
;
580 * @return \DateTime|null
582 public function getStarredAt()
584 return $this->starredAt
;
588 * @param \DateTime|null $starredAt
592 public function setStarredAt($starredAt = null)
594 $this->starredAt
= $starredAt;
600 * update isStarred and starred_at fields.
602 * @param bool $isStarred
606 public function updateStar($isStarred = false)
608 $this->setStarred($isStarred);
609 $this->setStarredAt(null);
610 if ($this->isStarred()) {
611 $this->setStarredAt(new \
DateTime());
618 * @return ArrayCollection<Annotation>
620 public function getAnnotations()
622 return $this->annotations
;
626 * @param Annotation $annotation
628 public function setAnnotation(Annotation
$annotation)
630 $this->annotations
[] = $annotation;
636 public function getMimetype()
638 return $this->mimetype
;
642 * @param string $mimetype
644 public function setMimetype($mimetype)
646 $this->mimetype
= $mimetype;
652 public function getReadingTime()
654 return $this->readingTime
;
658 * @param int $readingTime
660 public function setReadingTime($readingTime)
662 $this->readingTime
= $readingTime;
668 public function getDomainName()
670 return $this->domainName
;
674 * @param string $domainName
676 public function setDomainName($domainName)
678 $this->domainName
= $domainName;
682 * @return ArrayCollection
684 public function getTags()
691 * @SerializedName("tags")
692 * @Groups({"entries_for_user", "export_all"})
694 public function getSerializedTags()
697 foreach ($this->tags
as $tag) {
698 $data[] = $tag->getLabel();
707 public function addTag(Tag
$tag)
709 if ($this->tags
->contains($tag)) {
713 // check if tag already exist but has not yet be persisted
714 // it seems that the previous condition with `contains()` doesn't check that case
715 foreach ($this->tags
as $existingTag) {
716 if ($existingTag->getLabel() === $tag->getLabel()) {
721 $this->tags
->add($tag);
722 $tag->addEntry($this);
726 * Remove the given tag from the entry (if the tag is associated).
730 public function removeTag(Tag
$tag)
732 if (!$this->tags
->contains($tag)) {
736 $this->tags
->removeElement($tag);
737 $tag->removeEntry($this);
741 * Remove all assigned tags from the entry.
743 public function removeAllTags()
745 foreach ($this->tags
as $tag) {
746 $this->tags
->removeElement($tag);
747 $tag->removeEntry($this);
752 * Set previewPicture.
754 * @param string $previewPicture
758 public function setPreviewPicture($previewPicture)
760 $this->previewPicture
= $previewPicture;
766 * Get previewPicture.
770 public function getPreviewPicture()
772 return $this->previewPicture
;
778 * @param string $language
782 public function setLanguage($language)
784 $this->language
= $language;
794 public function getLanguage()
796 return $this->language
;
800 * @return string|null
802 public function getUid()
812 public function setUid($uid)
819 public function generateUid()
821 if (null === $this->uid
) {
822 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
823 $this->uid
= uniqid('', true);
827 public function cleanUid()
833 * Used in the entries filter so it's more explicit for the end user than the uid.
834 * Also used in the API.
837 * @SerializedName("is_public")
838 * @Groups({"entries_for_user"})
842 public function isPublic()
844 return null !== $this->uid
;
850 public function getHttpStatus()
852 return $this->httpStatus
;
856 * @param string $httpStatus
860 public function setHttpStatus($httpStatus)
862 $this->httpStatus
= $httpStatus;
870 public function getPublishedAt()
872 return $this->publishedAt
;
876 * @param \Datetime $publishedAt
880 public function setPublishedAt(\Datetime
$publishedAt)
882 $this->publishedAt
= $publishedAt;
890 public function getPublishedBy()
892 return $this->publishedBy
;
896 * @param array $publishedBy
900 public function setPublishedBy($publishedBy)
902 $this->publishedBy
= $publishedBy;
910 public function getHeaders()
912 return $this->headers
;
916 * @param array $headers
920 public function setHeaders($headers)
922 $this->headers
= $headers;
930 * @param string $originUrl
934 public function setOriginUrl($originUrl)
936 $this->originUrl
= $originUrl;
946 public function getOriginUrl()
948 return $this->originUrl
;
954 * @param string $givenUrl
958 public function setGivenUrl($givenUrl)
960 $this->givenUrl
= $givenUrl;
961 $this->hashedGivenUrl
= UrlHasher
::hashUrl($givenUrl);
971 public function getGivenUrl()
973 return $this->givenUrl
;
979 public function getHashedUrl()
981 return $this->hashedUrl
;
985 * @param mixed $hashedUrl
989 public function setHashedUrl($hashedUrl)
991 $this->hashedUrl
= $hashedUrl;