X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FEntity%2FEntry.php;h=8f4ddf613212d96fc640589f95646fce805ec87b;hb=927c9e796ff6fad2bf82a965234f52932cdee657;hp=e684c9b103f77189b6d799549c2fabd33561fe9e;hpb=a1413a3da9e845a0f2e00e73069f5363864367bd;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index e684c9b1..61d01bdc 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php @@ -4,22 +4,37 @@ namespace Wallabag\CoreBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; -use Symfony\Component\Validator\Constraints as Assert; use Hateoas\Configuration\Annotation as Hateoas; +use JMS\Serializer\Annotation\Exclude; +use JMS\Serializer\Annotation\Groups; +use JMS\Serializer\Annotation\SerializedName; +use JMS\Serializer\Annotation\VirtualProperty; use JMS\Serializer\Annotation\XmlRoot; -use Wallabag\CoreBundle\Tools\Utils; +use Symfony\Component\Validator\Constraints as Assert; +use Wallabag\AnnotationBundle\Entity\Annotation; +use Wallabag\CoreBundle\Helper\EntityTimestampsTrait; +use Wallabag\UserBundle\Entity\User; /** * Entry. * * @XmlRoot("entry") * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository") - * @ORM\Table + * @ORM\Table( + * name="`entry`", + * options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"}, + * indexes={ + * @ORM\Index(name="created_at", columns={"created_at"}), + * @ORM\Index(name="uid", columns={"uid"}) + * } + * ) * @ORM\HasLifecycleCallbacks() * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())") */ class Entry { + use EntityTimestampsTrait; + /** @Serializer\XmlAttribute */ /** * @var int @@ -27,13 +42,26 @@ class Entry * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") + * + * @Groups({"entries_for_user", "export_all"}) */ private $id; + /** + * @var string + * + * @ORM\Column(name="uid", type="string", length=23, nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) + */ + private $uid; + /** * @var string * * @ORM\Column(name="title", type="text", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) */ private $title; @@ -42,20 +70,30 @@ class Entry * * @Assert\NotBlank() * @ORM\Column(name="url", type="text", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) */ private $url; /** * @var bool * + * @Exclude + * * @ORM\Column(name="is_archived", type="boolean") + * + * @Groups({"entries_for_user", "export_all"}) */ private $isArchived = false; /** * @var bool * + * @Exclude + * * @ORM\Column(name="is_starred", type="boolean") + * + * @Groups({"entries_for_user", "export_all"}) */ private $isStarred = false; @@ -63,41 +101,79 @@ class Entry * @var string * * @ORM\Column(name="content", type="text", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) */ private $content; /** - * @var date + * @var \DateTime * * @ORM\Column(name="created_at", type="datetime") + * + * @Groups({"entries_for_user", "export_all"}) */ private $createdAt; /** - * @var date + * @var \DateTime * * @ORM\Column(name="updated_at", type="datetime") + * + * @Groups({"entries_for_user", "export_all"}) */ private $updatedAt; /** - * @var string + * @var \DateTime + * + * @ORM\Column(name="published_at", type="datetime", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) + */ + private $publishedAt; + + /** + * @var array + * + * @ORM\Column(name="published_by", type="array", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) + */ + private $publishedBy; + + /** + * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"}) + * @ORM\JoinTable * - * @ORM\Column(name="comments", type="text", nullable=true) + * @Groups({"entries_for_user", "export_all"}) */ - private $comments; + private $annotations; /** * @var string * * @ORM\Column(name="mimetype", type="text", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) */ private $mimetype; + /** + * @var string + * + * @ORM\Column(name="language", type="text", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) + */ + private $language; + /** * @var int * * @ORM\Column(name="reading_time", type="integer", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) */ private $readingTime; @@ -105,6 +181,8 @@ class Entry * @var string * * @ORM\Column(name="domain_name", type="text", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) */ private $domainName; @@ -112,24 +190,49 @@ class Entry * @var string * * @ORM\Column(name="preview_picture", type="text", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) */ private $previewPicture; /** - * @var bool + * @var string + * + * @ORM\Column(name="http_status", type="string", length=3, nullable=true) * - * @ORM\Column(name="is_public", type="boolean", nullable=true, options={"default" = false}) + * @Groups({"entries_for_user", "export_all"}) */ - private $isPublic; + private $httpStatus; /** - * @ORM\ManyToOne(targetEntity="User", inversedBy="entries") + * @var array + * + * @ORM\Column(name="headers", type="array", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) + */ + private $headers; + + /** + * @Exclude + * + * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries") + * + * @Groups({"export_all"}) */ private $user; /** * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"}) - * @ORM\JoinTable + * @ORM\JoinTable( + * name="entry_tag", + * joinColumns={ + * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade") + * }, + * inverseJoinColumns={ + * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade") + * } + * ) */ private $tags; @@ -203,7 +306,7 @@ class Entry /** * Set isArchived. * - * @param string $isArchived + * @param bool $isArchived * * @return Entry */ @@ -217,13 +320,23 @@ class Entry /** * Get isArchived. * - * @return string + * @return bool */ public function isArchived() { return $this->isArchived; } + /** + * @VirtualProperty + * @SerializedName("is_archived") + * @Groups({"entries_for_user", "export_all"}) + */ + public function is_Archived() + { + return (int) $this->isArchived(); + } + public function toggleArchive() { $this->isArchived = $this->isArchived() ^ 1; @@ -234,7 +347,7 @@ class Entry /** * Set isStarred. * - * @param string $isStarred + * @param bool $isStarred * * @return Entry */ @@ -248,13 +361,23 @@ class Entry /** * Get isStarred. * - * @return string + * @return bool */ public function isStarred() { return $this->isStarred; } + /** + * @VirtualProperty + * @SerializedName("is_starred") + * @Groups({"entries_for_user", "export_all"}) + */ + public function is_Starred() + { + return (int) $this->isStarred(); + } + public function toggleStar() { $this->isStarred = $this->isStarred() ^ 1; @@ -272,8 +395,6 @@ class Entry public function setContent($content) { $this->content = $content; - $this->readingTime = Utils::getReadingTime($content); - $this->domainName = parse_url($this->url, PHP_URL_HOST); return $this; } @@ -297,48 +418,77 @@ class Entry } /** - * @return string + * @VirtualProperty + * @SerializedName("user_name") */ - public function getCreatedAt() + public function getUserName() { - return $this->createdAt; + return $this->user->getUserName(); } /** - * @return string + * @VirtualProperty + * @SerializedName("user_email") */ - public function getUpdatedAt() + public function getUserEmail() { - return $this->updatedAt; + return $this->user->getEmail(); } /** - * @ORM\PrePersist - * @ORM\PreUpdate + * @VirtualProperty + * @SerializedName("user_id") */ - public function timestamps() + public function getUserId() { - if (is_null($this->createdAt)) { - $this->createdAt = new \DateTime(); - } + return $this->user->getId(); + } + + /** + * Set created_at. + * Only used when importing data from an other service. + * + * @param \DateTime $createdAt + * + * @return Entry + */ + public function setCreatedAt(\DateTime $createdAt) + { + $this->createdAt = $createdAt; - $this->updatedAt = new \DateTime(); + return $this; } /** - * @return string + * @return \DateTime */ - public function getComments() + public function getCreatedAt() { - return $this->comments; + return $this->createdAt; } /** - * @param string $comments + * @return \DateTime */ - public function setComments($comments) + public function getUpdatedAt() { - $this->comments = $comments; + return $this->updatedAt; + } + + /** + * @return ArrayCollection + */ + public function getAnnotations() + { + return $this->annotations; + } + + /** + * @param Annotation $annotation + */ + public function setAnnotation(Annotation $annotation) + { + $this->annotations[] = $annotation; } /** @@ -390,27 +540,26 @@ class Entry } /** - * @return bool + * @return ArrayCollection */ - public function isPublic() + public function getTags() { - return $this->isPublic; + return $this->tags; } /** - * @param bool $isPublic + * @VirtualProperty + * @SerializedName("tags") + * @Groups({"entries_for_user", "export_all"}) */ - public function setIsPublic($isPublic) + public function getSerializedTags() { - $this->isPublic = $isPublic; - } + $data = []; + foreach ($this->tags as $tag) { + $data[] = $tag->getLabel(); + } - /** - * @return ArrayCollection - */ - public function getTags() - { - return $this->tags; + return $data; } /** @@ -418,13 +567,46 @@ class Entry */ public function addTag(Tag $tag) { - $this->tags[] = $tag; + if ($this->tags->contains($tag)) { + return; + } + + // check if tag already exist but has not yet be persisted + // it seems that the previous condition with `contains()` doesn't check that case + foreach ($this->tags as $existingTag) { + if ($existingTag->getLabel() === $tag->getLabel()) { + return; + } + } + + $this->tags->add($tag); $tag->addEntry($this); } + /** + * Remove the given tag from the entry (if the tag is associated). + * + * @param Tag $tag + */ public function removeTag(Tag $tag) { + if (!$this->tags->contains($tag)) { + return; + } + $this->tags->removeElement($tag); + $tag->removeEntry($this); + } + + /** + * Remove all assigned tags from the entry. + */ + public function removeAllTags() + { + foreach ($this->tags as $tag) { + $this->tags->removeElement($tag); + $tag->removeEntry($this); + } } /** @@ -450,4 +632,156 @@ class Entry { return $this->previewPicture; } + + /** + * Set language. + * + * @param string $language + * + * @return Entry + */ + public function setLanguage($language) + { + $this->language = $language; + + return $this; + } + + /** + * Get language. + * + * @return string + */ + public function getLanguage() + { + return $this->language; + } + + /** + * @return string + */ + public function getUid() + { + return $this->uid; + } + + /** + * @param string $uid + * + * @return Entry + */ + public function setUid($uid) + { + $this->uid = $uid; + + return $this; + } + + public function generateUid() + { + if (null === $this->uid) { + // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter + $this->uid = uniqid('', true); + } + } + + public function cleanUid() + { + $this->uid = null; + } + + /** + * Used in the entries filter so it's more explicit for the end user than the uid. + * Also used in the API. + * + * @VirtualProperty + * @SerializedName("is_public") + * @Groups({"entries_for_user"}) + * + * @return bool + */ + public function isPublic() + { + return null !== $this->uid; + } + + /** + * @return string + */ + public function getHttpStatus() + { + return $this->httpStatus; + } + + /** + * @param string $httpStatus + * + * @return Entry + */ + public function setHttpStatus($httpStatus) + { + $this->httpStatus = $httpStatus; + + return $this; + } + + /** + * @return \Datetime + */ + public function getPublishedAt() + { + return $this->publishedAt; + } + + /** + * @param \Datetime $publishedAt + * + * @return Entry + */ + public function setPublishedAt(\Datetime $publishedAt) + { + $this->publishedAt = $publishedAt; + + return $this; + } + + /** + * @return array + */ + public function getPublishedBy() + { + return $this->publishedBy; + } + + /** + * @param array $publishedBy + * + * @return Entry + */ + public function setPublishedBy($publishedBy) + { + $this->publishedBy = $publishedBy; + + return $this; + } + + /** + * @return array + */ + public function getHeaders() + { + return $this->headers; + } + + /** + * @param array $headers + * + * @return Entry + */ + public function setHeaders($headers) + { + $this->headers = $headers; + + return $this; + } }