]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Entity/Entry.php
Use lang attribute
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Entity / Entry.php
index 9a7dd4e7b77e43613e7a1048a847fda7afb724d4..190457983f92eb9d5bc77a25c24b08646ddb918f 100644 (file)
@@ -5,14 +5,16 @@ namespace Wallabag\CoreBundle\Entity;
 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\Mapping as ORM;
 use Hateoas\Configuration\Annotation as Hateoas;
-use JMS\Serializer\Annotation\Groups;
-use JMS\Serializer\Annotation\XmlRoot;
 use JMS\Serializer\Annotation\Exclude;
-use JMS\Serializer\Annotation\VirtualProperty;
+use JMS\Serializer\Annotation\Groups;
 use JMS\Serializer\Annotation\SerializedName;
+use JMS\Serializer\Annotation\VirtualProperty;
+use JMS\Serializer\Annotation\XmlRoot;
 use Symfony\Component\Validator\Constraints as Assert;
-use Wallabag\UserBundle\Entity\User;
 use Wallabag\AnnotationBundle\Entity\Annotation;
+use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
+use Wallabag\CoreBundle\Helper\UrlHasher;
+use Wallabag\UserBundle\Entity\User;
 
 /**
  * Entry.
@@ -24,7 +26,13 @@ use Wallabag\AnnotationBundle\Entity\Annotation;
  *     options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"},
  *     indexes={
  *         @ORM\Index(name="created_at", columns={"created_at"}),
- *         @ORM\Index(name="uid", columns={"uid"})
+ *         @ORM\Index(name="uid", columns={"uid"}),
+ *         @ORM\Index(name="hashed_url_user_id", columns={"user_id", "hashed_url"}, options={"lengths"={null, 40}}),
+ *         @ORM\Index(name="hashed_given_url_user_id", columns={"user_id", "hashed_given_url"}, options={"lengths"={null, 40}}),
+ *         @ORM\Index(name="user_language", columns={"language", "user_id"}),
+ *         @ORM\Index(name="user_archived", columns={"user_id", "is_archived", "archived_at"}),
+ *         @ORM\Index(name="user_created", columns={"user_id", "created_at"}),
+ *         @ORM\Index(name="user_starred", columns={"user_id", "is_starred", "starred_at"})
  *     }
  * )
  * @ORM\HasLifecycleCallbacks()
@@ -32,6 +40,8 @@ use Wallabag\AnnotationBundle\Entity\Annotation;
  */
 class Entry
 {
+    use EntityTimestampsTrait;
+
     /** @Serializer\XmlAttribute */
     /**
      * @var int
@@ -63,6 +73,8 @@ class Entry
     private $title;
 
     /**
+     * Define the url fetched by wallabag (the final url after potential redirections).
+     *
      * @var string
      *
      * @Assert\NotBlank()
@@ -72,6 +84,42 @@ class Entry
      */
     private $url;
 
+    /**
+     * @var string
+     *
+     * @ORM\Column(name="hashed_url", type="string", length=40, nullable=true)
+     */
+    private $hashedUrl;
+
+    /**
+     * From where user retrieved/found the url (an other article, a twitter, or the given_url if non are provided).
+     *
+     * @var string
+     *
+     * @ORM\Column(name="origin_url", type="text", nullable=true)
+     *
+     * @Groups({"entries_for_user", "export_all"})
+     */
+    private $originUrl;
+
+    /**
+     * Define the url entered by the user (without redirections).
+     *
+     * @var string
+     *
+     * @ORM\Column(name="given_url", type="text", nullable=true)
+     *
+     * @Groups({"entries_for_user", "export_all"})
+     */
+    private $givenUrl;
+
+    /**
+     * @var string
+     *
+     * @ORM\Column(name="hashed_given_url", type="string", length=40, nullable=true)
+     */
+    private $hashedGivenUrl;
+
     /**
      * @var bool
      *
@@ -83,6 +131,15 @@ class Entry
      */
     private $isArchived = false;
 
+    /**
+     * @var \DateTime
+     *
+     * @ORM\Column(name="archived_at", type="datetime", nullable=true)
+     *
+     * @Groups({"entries_for_user", "export_all"})
+     */
+    private $archivedAt = null;
+
     /**
      * @var bool
      *
@@ -139,6 +196,15 @@ class Entry
      */
     private $publishedBy;
 
+    /**
+     * @var \DateTime
+     *
+     * @ORM\Column(name="starred_at", type="datetime", nullable=true)
+     *
+     * @Groups({"entries_for_user", "export_all"})
+     */
+    private $starredAt = null;
+
     /**
      * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"})
      * @ORM\JoinTable
@@ -159,7 +225,7 @@ class Entry
     /**
      * @var string
      *
-     * @ORM\Column(name="language", type="text", nullable=true)
+     * @ORM\Column(name="language", type="string", length=20, nullable=true)
      *
      * @Groups({"entries_for_user", "export_all"})
      */
@@ -168,11 +234,11 @@ class Entry
     /**
      * @var int
      *
-     * @ORM\Column(name="reading_time", type="integer", nullable=true)
+     * @ORM\Column(name="reading_time", type="integer", nullable=false)
      *
      * @Groups({"entries_for_user", "export_all"})
      */
-    private $readingTime;
+    private $readingTime = 0;
 
     /**
      * @var string
@@ -286,6 +352,7 @@ class Entry
     public function setUrl($url)
     {
         $this->url = $url;
+        $this->hashedUrl = UrlHasher::hashUrl($url);
 
         return $this;
     }
@@ -314,6 +381,44 @@ class Entry
         return $this;
     }
 
+    /**
+     * update isArchived and archive_at fields.
+     *
+     * @param bool $isArchived
+     *
+     * @return Entry
+     */
+    public function updateArchived($isArchived = false)
+    {
+        $this->setArchived($isArchived);
+        $this->setArchivedAt(null);
+        if ($this->isArchived()) {
+            $this->setArchivedAt(new \DateTime());
+        }
+
+        return $this;
+    }
+
+    /**
+     * @return \DateTime|null
+     */
+    public function getArchivedAt()
+    {
+        return $this->archivedAt;
+    }
+
+    /**
+     * @param \DateTime|null $archivedAt
+     *
+     * @return Entry
+     */
+    public function setArchivedAt($archivedAt = null)
+    {
+        $this->archivedAt = $archivedAt;
+
+        return $this;
+    }
+
     /**
      * Get isArchived.
      *
@@ -336,7 +441,7 @@ class Entry
 
     public function toggleArchive()
     {
-        $this->isArchived = $this->isArchived() ^ 1;
+        $this->updateArchived($this->isArchived() ^ 1);
 
         return $this;
     }
@@ -445,8 +550,6 @@ class Entry
      * Set created_at.
      * Only used when importing data from an other service.
      *
-     * @param \DateTime $createdAt
-     *
      * @return Entry
      */
     public function setCreatedAt(\DateTime $createdAt)
@@ -473,16 +576,41 @@ class Entry
     }
 
     /**
-     * @ORM\PrePersist
-     * @ORM\PreUpdate
+     * @return \DateTime|null
      */
-    public function timestamps()
+    public function getStarredAt()
     {
-        if (is_null($this->createdAt)) {
-            $this->createdAt = new \DateTime();
+        return $this->starredAt;
+    }
+
+    /**
+     * @param \DateTime|null $starredAt
+     *
+     * @return Entry
+     */
+    public function setStarredAt($starredAt = null)
+    {
+        $this->starredAt = $starredAt;
+
+        return $this;
+    }
+
+    /**
+     * update isStarred and starred_at fields.
+     *
+     * @param bool $isStarred
+     *
+     * @return Entry
+     */
+    public function updateStar($isStarred = false)
+    {
+        $this->setStarred($isStarred);
+        $this->setStarredAt(null);
+        if ($this->isStarred()) {
+            $this->setStarredAt(new \DateTime());
         }
 
-        $this->updatedAt = new \DateTime();
+        return $this;
     }
 
     /**
@@ -493,9 +621,6 @@ class Entry
         return $this->annotations;
     }
 
-    /**
-     * @param Annotation $annotation
-     */
     public function setAnnotation(Annotation $annotation)
     {
         $this->annotations[] = $annotation;
@@ -572,9 +697,6 @@ class Entry
         return $data;
     }
 
-    /**
-     * @param Tag $tag
-     */
     public function addTag(Tag $tag)
     {
         if ($this->tags->contains($tag)) {
@@ -593,6 +715,9 @@ class Entry
         $tag->addEntry($this);
     }
 
+    /**
+     * Remove the given tag from the entry (if the tag is associated).
+     */
     public function removeTag(Tag $tag)
     {
         if (!$this->tags->contains($tag)) {
@@ -603,6 +728,17 @@ class Entry
         $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);
+        }
+    }
+
     /**
      * Set previewPicture.
      *
@@ -652,7 +788,20 @@ class Entry
     }
 
     /**
-     * @return string
+     * Format the entry language to a valid html lang attribute.
+     */
+    public function getHTMLLanguage()
+    {
+        $parsedLocale = \Locale::parseLocale($this->getLanguage());
+        $lang = '';
+        $lang .= $parsedLocale['language'] ?? '';
+        $lang .= isset($parsedLocale['region']) ? '-' . $parsedLocale['region'] : '';
+
+        return $lang;
+    }
+
+    /**
+     * @return string|null
      */
     public function getUid()
     {
@@ -684,6 +833,21 @@ class Entry
         $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
      */
@@ -713,8 +877,6 @@ class Entry
     }
 
     /**
-     * @param \Datetime $publishedAt
-     *
      * @return Entry
      */
     public function setPublishedAt(\Datetime $publishedAt)
@@ -763,4 +925,73 @@ class Entry
 
         return $this;
     }
+
+    /**
+     * Set origin url.
+     *
+     * @param string $originUrl
+     *
+     * @return Entry
+     */
+    public function setOriginUrl($originUrl)
+    {
+        $this->originUrl = $originUrl;
+
+        return $this;
+    }
+
+    /**
+     * Get origin url.
+     *
+     * @return string
+     */
+    public function getOriginUrl()
+    {
+        return $this->originUrl;
+    }
+
+    /**
+     * Set given url.
+     *
+     * @param string $givenUrl
+     *
+     * @return Entry
+     */
+    public function setGivenUrl($givenUrl)
+    {
+        $this->givenUrl = $givenUrl;
+        $this->hashedGivenUrl = UrlHasher::hashUrl($givenUrl);
+
+        return $this;
+    }
+
+    /**
+     * Get given url.
+     *
+     * @return string
+     */
+    public function getGivenUrl()
+    {
+        return $this->givenUrl;
+    }
+
+    /**
+     * @return string
+     */
+    public function getHashedUrl()
+    {
+        return $this->hashedUrl;
+    }
+
+    /**
+     * @param mixed $hashedUrl
+     *
+     * @return Entry
+     */
+    public function setHashedUrl($hashedUrl)
+    {
+        $this->hashedUrl = $hashedUrl;
+
+        return $this;
+    }
 }