+
+ /**
+ * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
+ * provided item's body with them.
+ *
+ * @param array $item
+ * @return array
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ * @throws \ImagickException
+ */
+ public static function populateTagsFromItem(&$item)
+ {
+ $return = [
+ 'tags' => [],
+ 'hashtags' => [],
+ 'mentions' => [],
+ 'implicit_mentions' => [],
+ ];
+
+ $searchpath = DI::baseUrl() . "/search?tag=";
+
+ $taglist = DBA::select('tag-view', ['type', 'name', 'url'],
+ ['uri-id' => $item['uri-id'], 'type' => [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]);
+ while ($tag = DBA::fetch($taglist)) {
+ if ($tag['url'] == '') {
+ $tag['url'] = $searchpath . rawurlencode($tag['name']);
+ }
+
+ $orig_tag = $tag['url'];
+
+ $prefix = self::TAG_CHARACTER[$tag['type']];
+ switch($tag['type']) {
+ case self::HASHTAG:
+ if ($orig_tag != $tag['url']) {
+ $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
+ }
+
+ $return['hashtags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
+ $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
+ break;
+ case self::MENTION:
+ case self::EXCLUSIVE_MENTION:
+ $tag['url'] = Contact::magicLink($tag['url']);
+ $return['mentions'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
+ $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
+ break;
+ case self::IMPLICIT_MENTION:
+ $return['implicit_mentions'][] = $prefix . $tag['name'];
+ break;
+ }
+ }
+ DBA::close($taglist);
+
+ return $return;
+ }
+