+
+ /**
+ * 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' => [],
+ ];
+
+ $searchpath = System::baseUrl() . "/search?tag=";
+
+ $taglist = DBA::select(
+ 'term',
+ ['type', 'term', 'url'],
+ ["`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION],
+ ['order' => ['tid']]
+ );
+
+ while ($tag = DBA::fetch($taglist)) {
+ if ($tag['url'] == '') {
+ $tag['url'] = $searchpath . rawurlencode($tag['term']);
+ }
+
+ $orig_tag = $tag['url'];
+
+ $author = ['uid' => 0, 'id' => $item['author-id'],
+ 'network' => $item['author-network'], 'url' => $item['author-link']];
+ $tag['url'] = Contact::magicLinkByContact($author, $tag['url']);
+
+ $prefix = '';
+ if ($tag['type'] == TERM_HASHTAG) {
+ if ($orig_tag != $tag['url']) {
+ $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
+ }
+
+ $return['hashtags'][] = '#<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
+ $prefix = '#';
+ } elseif ($tag['type'] == TERM_MENTION) {
+ $return['mentions'][] = '@<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
+ $prefix = '@';
+ }
+
+ $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
+ }
+ DBA::close($taglist);
+
+ return $return;
+ }
+
+ /**
+ * Delete all tags from an item
+ *
+ * @param int itemid - choose from which item the tags will be removed
+ * @param array $type
+ * @throws \Exception
+ */
+ public static function deleteByItemId($itemid, $type = [TERM_HASHTAG, TERM_MENTION])
+ {
+ if (empty($itemid)) {
+ return;
+ }
+
+ // Clean up all tags
+ DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => $type]);
+
+ }