]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Entity/Entry.php
Hash the urls to check if they exist
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Entity / Entry.php
CommitLineData
9d50517c
NL
1<?php
2
ad4d1caa 3namespace Wallabag\CoreBundle\Entity;
9d50517c 4
0a018fe0 5use Doctrine\Common\Collections\ArrayCollection;
9d50517c 6use Doctrine\ORM\Mapping as ORM;
0f006880 7use Hateoas\Configuration\Annotation as Hateoas;
268e9e72 8use JMS\Serializer\Annotation\Groups;
0f006880 9use JMS\Serializer\Annotation\XmlRoot;
7d1fdab2
TC
10use JMS\Serializer\Annotation\Exclude;
11use JMS\Serializer\Annotation\VirtualProperty;
12use JMS\Serializer\Annotation\SerializedName;
619cc453 13use Symfony\Component\Validator\Constraints as Assert;
1210dae1 14use Wallabag\UserBundle\Entity\User;
4dc87223 15use Wallabag\AnnotationBundle\Entity\Annotation;
9d50517c
NL
16
17/**
4346a860 18 * Entry.
9d50517c 19 *
0f006880 20 * @XmlRoot("entry")
be463487 21 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository")
7e9c1d65
JB
22 * @ORM\Table(
23 * name="`entry`",
24 * options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"},
81375151
NL
25 * indexes={
26 * @ORM\Index(name="created_at", columns={"created_at"}),
d5fbb570
TC
27 * @ORM\Index(name="uid", columns={"uid"}),
28 * @ORM\Index(name="hashedurl", columns={"hashedurl"})
81375151 29 * }
7e9c1d65 30 * )
34d15eb4 31 * @ORM\HasLifecycleCallbacks()
0f006880 32 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
9d50517c 33 */
be463487 34class Entry
9d50517c 35{
0f006880 36 /** @Serializer\XmlAttribute */
9d50517c 37 /**
4346a860 38 * @var int
9d50517c 39 *
be463487 40 * @ORM\Column(name="id", type="integer")
9d50517c 41 * @ORM\Id
34d15eb4 42 * @ORM\GeneratedValue(strategy="AUTO")
5b7da076
TC
43 *
44 * @Groups({"entries_for_user", "export_all"})
9d50517c 45 */
be463487 46 private $id;
9d50517c 47
f3d0cb91 48 /**
78b3c31d 49 * @var string
f3d0cb91 50 *
7239082a 51 * @ORM\Column(name="uid", type="string", length=23, nullable=true)
f3d0cb91
NL
52 *
53 * @Groups({"entries_for_user", "export_all"})
54 */
7239082a 55 private $uid;
f3d0cb91 56
9d50517c
NL
57 /**
58 * @var string
59 *
60 * @ORM\Column(name="title", type="text", nullable=true)
5b7da076
TC
61 *
62 * @Groups({"entries_for_user", "export_all"})
9d50517c
NL
63 */
64 private $title;
65
66 /**
67 * @var string
68 *
b84a8055 69 * @Assert\NotBlank()
9d50517c 70 * @ORM\Column(name="url", type="text", nullable=true)
5b7da076
TC
71 *
72 * @Groups({"entries_for_user", "export_all"})
9d50517c
NL
73 */
74 private $url;
75
d5fbb570
TC
76 /**
77 * @var string
78 *
79 * @ORM\Column(name="hashedurl", type="text", nullable=true)
80 */
81 private $hashedUrl;
82
9d50517c 83 /**
4346a860 84 * @var bool
9d50517c 85 *
189ef634
TC
86 * @Exclude
87 *
be463487 88 * @ORM\Column(name="is_archived", type="boolean")
5b7da076
TC
89 *
90 * @Groups({"entries_for_user", "export_all"})
9d50517c 91 */
905ae369 92 private $isArchived = false;
9d50517c
NL
93
94 /**
4346a860 95 * @var bool
9d50517c 96 *
189ef634
TC
97 * @Exclude
98 *
be463487 99 * @ORM\Column(name="is_starred", type="boolean")
5b7da076
TC
100 *
101 * @Groups({"entries_for_user", "export_all"})
9d50517c 102 */
905ae369 103 private $isStarred = false;
9d50517c 104
9d50517c
NL
105 /**
106 * @var string
107 *
eacaf7f8 108 * @ORM\Column(name="content", type="text", nullable=true)
5b7da076
TC
109 *
110 * @Groups({"entries_for_user", "export_all"})
9d50517c 111 */
eacaf7f8 112 private $content;
9d50517c 113
34d15eb4 114 /**
8664069e 115 * @var \DateTime
34d15eb4 116 *
be463487 117 * @ORM\Column(name="created_at", type="datetime")
5b7da076 118 *
9401696f 119 * @Groups({"entries_for_user", "export_all"})
34d15eb4
NL
120 */
121 private $createdAt;
122
123 /**
8664069e 124 * @var \DateTime
34d15eb4 125 *
be463487 126 * @ORM\Column(name="updated_at", type="datetime")
5b7da076 127 *
9401696f 128 * @Groups({"entries_for_user", "export_all"})
34d15eb4
NL
129 */
130 private $updatedAt;
131
5e9009ce
NL
132 /**
133 * @var \DateTime
134 *
135 * @ORM\Column(name="published_at", type="datetime", nullable=true)
136 *
137 * @Groups({"entries_for_user", "export_all"})
138 */
139 private $publishedAt;
140
7b0b3622
NL
141 /**
142 * @var array
143 *
1517d577 144 * @ORM\Column(name="published_by", type="array", nullable=true)
7b0b3622
NL
145 *
146 * @Groups({"entries_for_user", "export_all"})
147 */
148 private $publishedBy;
149
34d15eb4 150 /**
4dc87223 151 * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"})
f38e03dc 152 * @ORM\JoinTable
5b7da076 153 *
f38e03dc 154 * @Groups({"entries_for_user", "export_all"})
34d15eb4 155 */
4dc87223 156 private $annotations;
34d15eb4
NL
157
158 /**
159 * @var string
160 *
161 * @ORM\Column(name="mimetype", type="text", nullable=true)
5b7da076
TC
162 *
163 * @Groups({"entries_for_user", "export_all"})
34d15eb4
NL
164 */
165 private $mimetype;
166
98f0929f
JB
167 /**
168 * @var string
169 *
170 * @ORM\Column(name="language", type="text", nullable=true)
5b7da076
TC
171 *
172 * @Groups({"entries_for_user", "export_all"})
98f0929f
JB
173 */
174 private $language;
175
34d15eb4 176 /**
4346a860 177 * @var int
34d15eb4 178 *
26864574 179 * @ORM\Column(name="reading_time", type="integer", nullable=true)
5b7da076
TC
180 *
181 * @Groups({"entries_for_user", "export_all"})
34d15eb4
NL
182 */
183 private $readingTime;
184
185 /**
186 * @var string
187 *
188 * @ORM\Column(name="domain_name", type="text", nullable=true)
5b7da076
TC
189 *
190 * @Groups({"entries_for_user", "export_all"})
34d15eb4
NL
191 */
192 private $domainName;
193
fad31615
JB
194 /**
195 * @var string
196 *
197 * @ORM\Column(name="preview_picture", type="text", nullable=true)
5b7da076
TC
198 *
199 * @Groups({"entries_for_user", "export_all"})
fad31615
JB
200 */
201 private $previewPicture;
202
10b35097 203 /**
e10e6ab3 204 * @var string
10b35097 205 *
3ef75cc4 206 * @ORM\Column(name="http_status", type="string", length=3, nullable=true)
10b35097
NL
207 *
208 * @Groups({"entries_for_user", "export_all"})
209 */
210 private $httpStatus;
211
dda6a6ad
NL
212 /**
213 * @var array
214 *
1517d577 215 * @ORM\Column(name="headers", type="array", nullable=true)
dda6a6ad
NL
216 *
217 * @Groups({"entries_for_user", "export_all"})
218 */
219 private $headers;
220
5f09650e 221 /**
7d1fdab2
TC
222 * @Exclude
223 *
1210dae1 224 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
5b7da076
TC
225 *
226 * @Groups({"export_all"})
5f09650e
NL
227 */
228 private $user;
229
0a018fe0 230 /**
af95c09c 231 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
e42b13bc
JB
232 * @ORM\JoinTable(
233 * name="entry_tag",
234 * joinColumns={
206bade5 235 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
e42b13bc
JB
236 * },
237 * inverseJoinColumns={
206bade5 238 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
e42b13bc
JB
239 * }
240 * )
0a018fe0
NL
241 */
242 private $tags;
243
5f09650e
NL
244 /*
245 * @param User $user
246 */
5d6f6f56 247 public function __construct(User $user)
5f09650e
NL
248 {
249 $this->user = $user;
0a018fe0 250 $this->tags = new ArrayCollection();
5f09650e
NL
251 }
252
9d50517c 253 /**
4346a860 254 * Get id.
9d50517c 255 *
4346a860 256 * @return int
9d50517c
NL
257 */
258 public function getId()
259 {
260 return $this->id;
261 }
262
263 /**
4346a860
JB
264 * Set title.
265 *
266 * @param string $title
9d50517c 267 *
be463487 268 * @return Entry
9d50517c
NL
269 */
270 public function setTitle($title)
271 {
272 $this->title = $title;
273
274 return $this;
275 }
276
277 /**
4346a860 278 * Get title.
9d50517c 279 *
7df80cb3 280 * @return string
9d50517c
NL
281 */
282 public function getTitle()
283 {
284 return $this->title;
285 }
286
287 /**
4346a860
JB
288 * Set url.
289 *
290 * @param string $url
9d50517c 291 *
be463487 292 * @return Entry
9d50517c
NL
293 */
294 public function setUrl($url)
295 {
296 $this->url = $url;
297
298 return $this;
299 }
300
301 /**
4346a860 302 * Get url.
9d50517c 303 *
7df80cb3 304 * @return string
9d50517c
NL
305 */
306 public function getUrl()
307 {
308 return $this->url;
309 }
310
311 /**
4346a860
JB
312 * Set isArchived.
313 *
8eedc8cf 314 * @param bool $isArchived
9d50517c 315 *
be463487 316 * @return Entry
9d50517c 317 */
905ae369 318 public function setArchived($isArchived)
9d50517c 319 {
905ae369 320 $this->isArchived = $isArchived;
9d50517c
NL
321
322 return $this;
323 }
324
325 /**
4346a860 326 * Get isArchived.
9d50517c 327 *
8eedc8cf 328 * @return bool
9d50517c 329 */
905ae369 330 public function isArchived()
9d50517c 331 {
905ae369 332 return $this->isArchived;
9d50517c
NL
333 }
334
189ef634
TC
335 /**
336 * @VirtualProperty
337 * @SerializedName("is_archived")
338 * @Groups({"entries_for_user", "export_all"})
339 */
340 public function is_Archived()
341 {
342 return (int) $this->isArchived();
343 }
344
163eae0b
NL
345 public function toggleArchive()
346 {
905ae369 347 $this->isArchived = $this->isArchived() ^ 1;
7df80cb3 348
163eae0b
NL
349 return $this;
350 }
351
9d50517c 352 /**
4346a860
JB
353 * Set isStarred.
354 *
8eedc8cf 355 * @param bool $isStarred
9d50517c 356 *
be463487 357 * @return Entry
9d50517c 358 */
905ae369 359 public function setStarred($isStarred)
9d50517c 360 {
905ae369 361 $this->isStarred = $isStarred;
9d50517c
NL
362
363 return $this;
364 }
365
366 /**
4346a860 367 * Get isStarred.
9d50517c 368 *
8eedc8cf 369 * @return bool
9d50517c 370 */
905ae369 371 public function isStarred()
9d50517c 372 {
905ae369 373 return $this->isStarred;
9d50517c
NL
374 }
375
189ef634
TC
376 /**
377 * @VirtualProperty
378 * @SerializedName("is_starred")
379 * @Groups({"entries_for_user", "export_all"})
380 */
381 public function is_Starred()
382 {
383 return (int) $this->isStarred();
384 }
385
163eae0b
NL
386 public function toggleStar()
387 {
905ae369 388 $this->isStarred = $this->isStarred() ^ 1;
163eae0b
NL
389
390 return $this;
391 }
392
9d50517c 393 /**
4346a860
JB
394 * Set content.
395 *
396 * @param string $content
9d50517c 397 *
be463487 398 * @return Entry
9d50517c
NL
399 */
400 public function setContent($content)
401 {
402 $this->content = $content;
403
404 return $this;
405 }
406
407 /**
4346a860 408 * Get content.
9d50517c 409 *
7df80cb3 410 * @return string
9d50517c
NL
411 */
412 public function getContent()
413 {
414 return $this->content;
415 }
416
417 /**
5f09650e 418 * @return User
9d50517c 419 */
5f09650e 420 public function getUser()
9d50517c 421 {
5f09650e 422 return $this->user;
9d50517c 423 }
42a90646 424
7d1fdab2
TC
425 /**
426 * @VirtualProperty
427 * @SerializedName("user_name")
428 */
429 public function getUserName()
430 {
431 return $this->user->getUserName();
432 }
433
434 /**
435 * @VirtualProperty
436 * @SerializedName("user_email")
437 */
438 public function getUserEmail()
439 {
440 return $this->user->getEmail();
441 }
442
443 /**
444 * @VirtualProperty
445 * @SerializedName("user_id")
446 */
447 public function getUserId()
448 {
449 return $this->user->getId();
450 }
451
34d15eb4 452 /**
6d65c0a8
JB
453 * Set created_at.
454 * Only used when importing data from an other service.
455 *
8664069e 456 * @param \DateTime $createdAt
6d65c0a8
JB
457 *
458 * @return Entry
459 */
460 public function setCreatedAt(\DateTime $createdAt)
461 {
462 $this->createdAt = $createdAt;
463
464 return $this;
465 }
466
467 /**
8664069e 468 * @return \DateTime
34d15eb4
NL
469 */
470 public function getCreatedAt()
471 {
472 return $this->createdAt;
473 }
474
34d15eb4 475 /**
8664069e 476 * @return \DateTime
34d15eb4
NL
477 */
478 public function getUpdatedAt()
479 {
480 return $this->updatedAt;
481 }
482
483 /**
be463487 484 * @ORM\PrePersist
34d15eb4
NL
485 * @ORM\PreUpdate
486 */
be463487 487 public function timestamps()
34d15eb4 488 {
be463487
NL
489 if (is_null($this->createdAt)) {
490 $this->createdAt = new \DateTime();
491 }
492
34d15eb4
NL
493 $this->updatedAt = new \DateTime();
494 }
495
496 /**
4dc87223 497 * @return ArrayCollection<Annotation>
34d15eb4 498 */
4dc87223 499 public function getAnnotations()
34d15eb4 500 {
4dc87223 501 return $this->annotations;
34d15eb4
NL
502 }
503
504 /**
4dc87223 505 * @param Annotation $annotation
34d15eb4 506 */
4dc87223 507 public function setAnnotation(Annotation $annotation)
34d15eb4 508 {
4dc87223 509 $this->annotations[] = $annotation;
34d15eb4
NL
510 }
511
512 /**
513 * @return string
514 */
515 public function getMimetype()
516 {
517 return $this->mimetype;
518 }
519
520 /**
521 * @param string $mimetype
522 */
523 public function setMimetype($mimetype)
524 {
525 $this->mimetype = $mimetype;
526 }
527
528 /**
529 * @return int
530 */
531 public function getReadingTime()
532 {
533 return $this->readingTime;
534 }
535
536 /**
537 * @param int $readingTime
538 */
539 public function setReadingTime($readingTime)
540 {
541 $this->readingTime = $readingTime;
542 }
543
544 /**
545 * @return string
546 */
547 public function getDomainName()
548 {
549 return $this->domainName;
550 }
551
552 /**
553 * @param string $domainName
554 */
555 public function setDomainName($domainName)
556 {
557 $this->domainName = $domainName;
558 }
559
0a018fe0
NL
560 /**
561 * @return ArrayCollection<Tag>
562 */
563 public function getTags()
564 {
565 return $this->tags;
566 }
567
b0458874
JB
568 /**
569 * @VirtualProperty
570 * @SerializedName("tags")
571 * @Groups({"entries_for_user", "export_all"})
572 */
573 public function getSerializedTags()
574 {
575 $data = [];
576 foreach ($this->tags as $tag) {
577 $data[] = $tag->getLabel();
578 }
579
580 return $data;
581 }
582
0a018fe0
NL
583 /**
584 * @param Tag $tag
585 */
586 public function addTag(Tag $tag)
587 {
625acf33
KG
588 if ($this->tags->contains($tag)) {
589 return;
590 }
591
fc031e57
JB
592 // check if tag already exist but has not yet be persisted
593 // it seems that the previous condition with `contains()` doesn't check that case
594 foreach ($this->tags as $existingTag) {
fc732227 595 if ($existingTag->getLabel() === $tag->getLabel()) {
fc031e57
JB
596 return;
597 }
598 }
599
e42b13bc 600 $this->tags->add($tag);
46bbd8d3 601 $tag->addEntry($this);
0a018fe0 602 }
092ca707
NL
603
604 public function removeTag(Tag $tag)
605 {
e42b13bc
JB
606 if (!$this->tags->contains($tag)) {
607 return;
608 }
609
092ca707 610 $this->tags->removeElement($tag);
e42b13bc 611 $tag->removeEntry($this);
092ca707 612 }
fad31615
JB
613
614 /**
a1413a3d 615 * Set previewPicture.
fad31615
JB
616 *
617 * @param string $previewPicture
618 *
619 * @return Entry
620 */
621 public function setPreviewPicture($previewPicture)
622 {
623 $this->previewPicture = $previewPicture;
624
625 return $this;
626 }
627
628 /**
a1413a3d 629 * Get previewPicture.
fad31615
JB
630 *
631 * @return string
632 */
633 public function getPreviewPicture()
634 {
635 return $this->previewPicture;
636 }
98f0929f
JB
637
638 /**
639 * Set language.
640 *
641 * @param string $language
642 *
643 * @return Entry
644 */
645 public function setLanguage($language)
646 {
647 $this->language = $language;
648
649 return $this;
650 }
651
652 /**
653 * Get language.
654 *
655 * @return string
656 */
657 public function getLanguage()
658 {
659 return $this->language;
660 }
f3d0cb91
NL
661
662 /**
78b3c31d 663 * @return string
f3d0cb91 664 */
7239082a 665 public function getUid()
f3d0cb91 666 {
7239082a 667 return $this->uid;
f3d0cb91
NL
668 }
669
670 /**
7239082a 671 * @param string $uid
f3d0cb91
NL
672 *
673 * @return Entry
674 */
7239082a 675 public function setUid($uid)
f3d0cb91 676 {
7239082a 677 $this->uid = $uid;
f3d0cb91
NL
678
679 return $this;
680 }
681
7239082a 682 public function generateUid()
f3d0cb91 683 {
7239082a 684 if (null === $this->uid) {
a7e2218e 685 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
7239082a 686 $this->uid = uniqid('', true);
f3d0cb91
NL
687 }
688 }
f1be7af4 689
7239082a 690 public function cleanUid()
f1be7af4 691 {
7239082a 692 $this->uid = null;
f1be7af4 693 }
10b35097
NL
694
695 /**
696 * @return int
697 */
698 public function getHttpStatus()
699 {
700 return $this->httpStatus;
701 }
702
703 /**
704 * @param int $httpStatus
705 *
706 * @return Entry
707 */
708 public function setHttpStatus($httpStatus)
709 {
710 $this->httpStatus = $httpStatus;
711
712 return $this;
713 }
5e9009ce
NL
714
715 /**
716 * @return \Datetime
717 */
718 public function getPublishedAt()
719 {
720 return $this->publishedAt;
721 }
722
723 /**
724 * @param \Datetime $publishedAt
725 *
726 * @return Entry
727 */
728 public function setPublishedAt(\Datetime $publishedAt)
729 {
730 $this->publishedAt = $publishedAt;
731
732 return $this;
733 }
7b0b3622
NL
734
735 /**
dda6a6ad 736 * @return array
7b0b3622
NL
737 */
738 public function getPublishedBy()
739 {
740 return $this->publishedBy;
741 }
742
743 /**
1517d577 744 * @param array $publishedBy
7b0b3622
NL
745 *
746 * @return Entry
747 */
748 public function setPublishedBy($publishedBy)
749 {
750 $this->publishedBy = $publishedBy;
751
752 return $this;
753 }
dda6a6ad
NL
754
755 /**
756 * @return array
757 */
758 public function getHeaders()
759 {
760 return $this->headers;
761 }
762
763 /**
1517d577 764 * @param array $headers
dda6a6ad
NL
765 *
766 * @return Entry
767 */
768 public function setHeaders($headers)
769 {
770 $this->headers = $headers;
771
772 return $this;
773 }
d5fbb570
TC
774
775 /**
776 * @return string
777 */
778 public function getHashedUrl()
779 {
780 return $this->hashedUrl;
781 }
782
783 /**
784 * @param mixed $hashedUrl
785 *
786 * @return Entry
787 */
788 public function setHashedUrl($hashedUrl)
789 {
790 $this->hashedUrl = $hashedUrl;
791
792 return $this;
793 }
9d50517c 794}