NoteTag.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License/
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\Tag\Entity;
  20. use App\Core\Cache;
  21. use App\Core\DB\DB;
  22. use App\Core\Entity;
  23. use App\Core\Router\Router;
  24. use App\Entity\Actor;
  25. use App\Entity\Note;
  26. use Component\Language\Entity\Language;
  27. use Component\Tag\Tag;
  28. use DateTimeInterface;
  29. /**
  30. * Entity for Notice Tag
  31. *
  32. * @category DB
  33. * @package GNUsocial
  34. *
  35. * @author Zach Copley <zach@status.net>
  36. * @copyright 2010 StatusNet Inc.
  37. * @author Mikael Nordfeldth <mmn@hethane.se>
  38. * @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
  39. * @author Hugo Sales <hugo@hsal.es>
  40. * @author Diogo Peralta Cordeiro <@diogo.site>
  41. * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
  42. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  43. */
  44. class NoteTag extends Entity
  45. {
  46. // {{{ Autocode
  47. // @codeCoverageIgnoreStart
  48. private string $tag;
  49. private string $canonical;
  50. private int $note_id;
  51. private bool $use_canonical;
  52. private ?int $language_id = null;
  53. private DateTimeInterface $created;
  54. public function setTag(string $tag): self
  55. {
  56. $this->tag = mb_substr($tag, 0, 64);
  57. return $this;
  58. }
  59. public function getTag(): string
  60. {
  61. return $this->tag;
  62. }
  63. public function setCanonical(string $canonical): self
  64. {
  65. $this->canonical = mb_substr($canonical, 0, 64);
  66. return $this;
  67. }
  68. public function getCanonical(): string
  69. {
  70. return $this->canonical;
  71. }
  72. public function setNoteId(int $note_id): self
  73. {
  74. $this->note_id = $note_id;
  75. return $this;
  76. }
  77. public function getNoteId(): int
  78. {
  79. return $this->note_id;
  80. }
  81. public function setUseCanonical(bool $use_canonical): self
  82. {
  83. $this->use_canonical = $use_canonical;
  84. return $this;
  85. }
  86. public function getUseCanonical(): bool
  87. {
  88. return $this->use_canonical;
  89. }
  90. public function setLanguageId(?int $language_id): self
  91. {
  92. $this->language_id = $language_id;
  93. return $this;
  94. }
  95. public function getLanguageId(): ?int
  96. {
  97. return $this->language_id;
  98. }
  99. public function setCreated(DateTimeInterface $created): self
  100. {
  101. $this->created = $created;
  102. return $this;
  103. }
  104. public function getCreated(): DateTimeInterface
  105. {
  106. return $this->created;
  107. }
  108. // @codeCoverageIgnoreEnd
  109. // }}} Autocode
  110. public static function cacheKey(int|Note $note_id)
  111. {
  112. if (!\is_int($note_id)) {
  113. $note_id = $note_id->getId();
  114. }
  115. return "note-tags-{$note_id}";
  116. }
  117. public static function getByNoteId(int $note_id): array
  118. {
  119. return Cache::getList(self::cacheKey($note_id), fn () => DB::dql('SELECT nt FROM note_tag AS nt JOIN note AS n WITH n.id = nt.note_id WHERE n.id = :id', ['id' => $note_id]));
  120. }
  121. public function getUrl(?Actor $actor = null, int $type = Router::ABSOLUTE_PATH): string
  122. {
  123. $params['tag'] = $this->getTag();
  124. if (\is_null($this->getLanguageId())) {
  125. if (!\is_null($actor)) {
  126. $params['locale'] = $actor->getTopLanguage()->getLocale();
  127. }
  128. } else {
  129. $params['locale'] = Language::getById($this->getLanguageId())->getLocale();
  130. }
  131. if ($this->getUseCanonical()) {
  132. $params['canonical'] = $this->getCanonical();
  133. }
  134. return Router::url(id: 'single_note_tag', args: $params, type: $type);
  135. }
  136. public static function schemaDef(): array
  137. {
  138. return [
  139. 'name' => 'note_tag',
  140. 'description' => 'Hash tags on notes',
  141. 'fields' => [
  142. 'note_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to tagged note'],
  143. 'tag' => ['type' => 'varchar', 'length' => Tag::MAX_TAG_LENGTH, 'not null' => true, 'description' => 'hash tag associated with this note'],
  144. 'canonical' => ['type' => 'varchar', 'length' => Tag::MAX_TAG_LENGTH, 'not null' => true, 'description' => 'ascii slug of tag'],
  145. 'use_canonical' => ['type' => 'bool', 'not null' => true, 'description' => 'whether the user wanted to use canonical tags in this note. Separate for blocks'],
  146. 'language_id' => ['type' => 'int', 'not null' => false, 'foreign key' => true, 'target' => 'Language.id', 'multiplicity' => 'many to many', 'description' => 'the language this entry refers to'],
  147. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  148. ],
  149. 'primary key' => ['note_id', 'tag'], // No need to require language in this association because all the related tags will be in the note's language already
  150. 'indexes' => [
  151. 'note_tag_created_idx' => ['created'],
  152. 'note_tag_note_id_idx' => ['note_id'],
  153. 'note_tag_tag_language_id_idx' => ['tag', 'language_id'],
  154. 'note_tag_tag_created_note_id_idx' => ['tag', 'created', 'note_id'],
  155. ],
  156. ];
  157. }
  158. }