upstream V3 development https://www.gnusocial.rocks/v3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

282 lines
11 KiB

  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;
  20. use App\Core\Cache;
  21. use App\Core\DB;
  22. use App\Core\Event;
  23. use function App\Core\I18n\_m;
  24. use App\Core\Modules\Component;
  25. use App\Core\Router;
  26. use App\Entity\Actor;
  27. use App\Entity\Note;
  28. use App\Util\Common;
  29. use App\Util\Exception\ClientException;
  30. use App\Util\Formatting;
  31. use App\Util\Functional as GSF;
  32. use App\Util\HTML;
  33. use Component\Circle\Entity\ActorTag;
  34. use Component\Language\Entity\Language;
  35. use Component\Tag\Entity\NoteTag;
  36. use Doctrine\Common\Collections\ExpressionBuilder;
  37. use Doctrine\ORM\Query\Expr;
  38. use Doctrine\ORM\QueryBuilder;
  39. use EventResult;
  40. use Functional as F;
  41. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  42. use Symfony\Component\HttpFoundation\Request;
  43. /**
  44. * Component responsible for extracting tags from posted notes, as well as normalizing them
  45. *
  46. * @author Hugo Sales <hugo@hsal.es>
  47. * @author Diogo Peralta Cordeiro <@diogo.site>
  48. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  49. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  50. */
  51. class Tag extends Component
  52. {
  53. public const MAX_TAG_LENGTH = 64;
  54. public const TAG_REGEX = '/(^|\\s)(#[\\pL\\pN_\\-]{1,64})/u'; // Brion Vibber 2011-02-23 v2:classes/Notice.php:367 function saveTags
  55. public const TAG_SLUG_REGEX = '[A-Za-z0-9]{1,64}';
  56. public function onAddRoute($r): EventResult
  57. {
  58. $r->connect('single_note_tag', '/note-tag/{tag<' . self::TAG_SLUG_REGEX . '>}', [Controller\Tag::class, 'single_note_tag']);
  59. $r->connect('multi_note_tags', '/note-tags/{tags<(' . self::TAG_SLUG_REGEX . ',)+' . self::TAG_SLUG_REGEX . '>}', [Controller\Tag::class, 'multi_note_tags']);
  60. return Event::next;
  61. }
  62. public static function maybeCreateTag(string $tag, int $note_id, ?int $lang_id): ?NoteTag
  63. {
  64. if (!self::validate($tag)) {
  65. return null; // Ignore invalid tag candidates
  66. }
  67. $canonical_tag = self::canonicalTag($tag, \is_null($lang_id) ? null : Language::getById($lang_id)->getLocale());
  68. DB::persist($note_tag = NoteTag::create([
  69. 'tag' => $tag,
  70. 'canonical' => $canonical_tag,
  71. 'note_id' => $note_id,
  72. 'use_canonical' => $extra_args['tag_use_canonical'] ?? false,
  73. 'language_id' => $lang_id,
  74. ]));
  75. foreach (self::cacheKeys($canonical_tag) as $key) {
  76. Cache::delete($key);
  77. }
  78. return $note_tag;
  79. }
  80. /**
  81. * @return NoteTag[]
  82. */
  83. public static function getNoteTags(int $actor_id, ?string $note_type): array
  84. {
  85. $query = <<<'EOF'
  86. select nt from \App\Entity\Note n
  87. join \Component\Tag\Entity\NoteTag nt with n.id = nt.note_id
  88. where n.actor_id = :id
  89. EOF;
  90. if (\is_null($note_type)) {
  91. return Cache::getList(
  92. Actor::cacheKeys($actor_id, 'any')['note-tags'],
  93. fn () => DB::dql(
  94. $query,
  95. ['id' => $actor_id],
  96. ),
  97. );
  98. } else {
  99. return Cache::getList(
  100. Actor::cacheKeys($actor_id, $note_type)['note-tags'],
  101. fn () => DB::dql(
  102. $query . ' and n.type = :type',
  103. ['id' => $actor_id, 'type' => $note_type],
  104. ),
  105. );
  106. }
  107. }
  108. /**
  109. * Process note by extracting any tags present
  110. */
  111. public function onProcessNoteContent(Note $note, string $content, string $content_type, array $extra_args): EventResult
  112. {
  113. if ($extra_args['TagProcessed'] ?? false) {
  114. return Event::next;
  115. }
  116. // XXX: We remove <span> because when content is in html the tag comes as #<span>hashtag</span>
  117. $content = str_replace('<span>', '', $content);
  118. $matched_tags = [];
  119. preg_match_all(self::TAG_REGEX, $content, $matched_tags, \PREG_SET_ORDER);
  120. $matched_tags = array_unique(F\map($matched_tags, fn ($m) => $m[2]));
  121. foreach ($matched_tags as $match) {
  122. $tag = self::extract($match);
  123. self::maybeCreateTag(tag: $tag, note_id: $note->getId(), lang_id: $note->getLanguageId());
  124. }
  125. return Event::next;
  126. }
  127. public function onRenderPlainTextNoteContent(string &$text, ?string $locale = null): EventResult
  128. {
  129. $text = preg_replace_callback(self::TAG_REGEX, fn ($m) => $m[1] . self::tagLink($m[2], $locale), $text);
  130. return Event::next;
  131. }
  132. public static function cacheKeys(string $tag_single_or_multi): array
  133. {
  134. return [
  135. 'note_single' => "note-tag-feed-{$tag_single_or_multi}",
  136. 'note_multi' => "note-tags-feed-{$tag_single_or_multi}",
  137. 'actor_single' => "actor-tag-feed-{$tag_single_or_multi}",
  138. 'actor_multi' => "actor-tags-feed-{$tag_single_or_multi}",
  139. ];
  140. }
  141. private static function tagLink(string $tag, ?string $locale): string
  142. {
  143. $tag = self::extract($tag);
  144. $url = Router::url('single_note_tag', !\is_null($locale) ? ['tag' => $tag, 'locale' => $locale] : ['tag' => $tag]);
  145. return HTML::html(['span' => ['attrs' => ['class' => 'tag'],
  146. '#' . HTML::html(['a' => [
  147. 'attrs' => [
  148. 'href' => $url,
  149. 'rel' => 'tag', // https://microformats.org/wiki/rel-tag
  150. ],
  151. $tag,
  152. ]], options: ['indent' => false]),
  153. ]], options: ['indent' => false, 'raw' => true]);
  154. }
  155. public static function extract(string $tag): string
  156. {
  157. return self::ensureLength(Formatting::removePrefix($tag, '#'));
  158. }
  159. public static function validate(string $tag): bool
  160. {
  161. return preg_match(self::TAG_REGEX, '#' . $tag) === 1;
  162. }
  163. public static function sanitize(string $tag): string
  164. {
  165. $tag = self::extract($tag);
  166. if (!self::validate($tag)) {
  167. throw new ClientException(_m('Invalid tag given: {tag}', ['{tag}' => $tag]));
  168. }
  169. return $tag;
  170. }
  171. public static function ensureLength(string $tag): string
  172. {
  173. return mb_substr($tag, 0, self::MAX_TAG_LENGTH);
  174. }
  175. /**
  176. * Convert a tag to its canonical representation, by splitting it
  177. * into words, stemming it in the given language (if enabled) and
  178. * sluggifying it (turning it into an ASCII representation)
  179. */
  180. public static function canonicalTag(string $tag, ?string $language = null): string
  181. {
  182. $result = '';
  183. foreach (Formatting::splitWords(str_replace('#', '', $tag)) as $word) {
  184. $temp_res = null;
  185. if (\is_null($language) || Event::handle('StemWord', [$language, $word, &$temp_res]) !== Event::stop) {
  186. $temp_res = $word;
  187. }
  188. $result .= Formatting::slugify($temp_res);
  189. }
  190. return self::ensureLength($result);
  191. }
  192. /**
  193. * Populate $note_expr with an expression to match a tag, if the term looks like a tag
  194. *
  195. * $term /^(note|tag|people|actor)/ means we want to match only either a note or an actor
  196. */
  197. public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $locale, ?Actor $actor, &$note_expr, &$actor_expr): EventResult
  198. {
  199. if (!str_contains($term, ':')) {
  200. return Event::next;
  201. }
  202. if (\is_null($locale)) {
  203. $locale = Common::currentLanguage();
  204. }
  205. [$search_type, $search_term] = explode(':', $term);
  206. if (str_starts_with($search_term, '#')) {
  207. $search_term = self::sanitize($search_term);
  208. $canonical_search_term = self::canonicalTag($search_term, $locale);
  209. $temp_note_expr = $eb->eq('note_tag.canonical', $canonical_search_term);
  210. $temp_actor_expr = $eb->eq('actor_tag.canonical', $canonical_search_term);
  211. if (Formatting::startsWith($term, ['note:', 'tag:', 'people:'])) {
  212. $note_expr = $temp_note_expr;
  213. } elseif (Formatting::startsWith($term, ['people:', 'actor:'])) {
  214. $actor_expr = $temp_actor_expr;
  215. } elseif (Formatting::startsWith($term, GSF::cartesianProduct([['people', 'actor'], ['circle', 'list'], [':']], separator: ['-', '_']))) {
  216. $null_tagger_expr = $eb->isNull('actor_circle.tagger');
  217. $tagger_expr = \is_null($actor_expr) ? $null_tagger_expr : $eb->orX($null_tagger_expr, $eb->eq('actor_circle.tagger', $actor->getId()));
  218. $tags = array_unique([$search_term, $canonical_search_term]);
  219. $tag_expr = \count($tags) === 1 ? $eb->eq('actor_circle.tag', $tags[0]) : $eb->in('actor_circle.tag', $tags);
  220. $search_expr = $eb->andX(
  221. $tagger_expr,
  222. $tag_expr,
  223. );
  224. $note_expr = $search_expr;
  225. $actor_expr = $search_expr;
  226. } else {
  227. $note_expr = $temp_note_expr;
  228. $actor_expr = $temp_actor_expr;
  229. return Event::next;
  230. }
  231. return Event::stop;
  232. }
  233. return Event::next;
  234. }
  235. public function onCollectionQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb): EventResult
  236. {
  237. if (!\in_array('note_tag', $note_qb->getAllAliases())) {
  238. $note_qb->leftJoin(NoteTag::class, 'note_tag', Expr\Join::WITH, 'note_tag.note_id = note.id');
  239. }
  240. if (!\in_array('actor_tag', $actor_qb->getAllAliases())) {
  241. $actor_qb->leftJoin(ActorTag::class, 'actor_tag', Expr\Join::WITH, 'actor_tag.tagger = actor.id');
  242. }
  243. return Event::next;
  244. }
  245. public function onPostingAddFormEntries(Request $request, Actor $actor, array &$form_params): EventResult
  246. {
  247. $form_params[] = ['tag_use_canonical', CheckboxType::class, ['required' => false, 'data' => true, 'label' => _m('Make note tags canonical'), 'help' => _m('Canonical tags will be treated as a version of an existing tag with the same root/stem (e.g. \'#great_tag\' will be considered as a version of \'#great\', if it already exists)')]];
  248. return Event::next;
  249. }
  250. public function onAddExtraArgsToNoteContent(Request $request, Actor $actor, array $data, array &$extra_args): EventResult
  251. {
  252. if (!isset($data['tag_use_canonical'])) {
  253. throw new ClientException(_m('Missing Use Canonical preference for Tags.'));
  254. }
  255. $extra_args['tag_use_canonical'] = $data['tag_use_canonical'];
  256. return Event::next;
  257. }
  258. }