5 namespace Friendica\Model;
7 use Friendica\Core\System;
8 use Friendica\Database\DBM;
9 use Friendica\Model\Item;
12 require_once 'boot.php';
13 require_once 'include/conversation.php';
14 require_once 'include/dba.php';
18 public static function tagTextFromItemId($itemid)
21 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]];
22 $tags = dba::select('term', [], $condition);
23 while ($tag = dba::fetch($tags)) {
24 if ($tag_text != '') {
28 if ($tag['type'] == 1) {
33 $tag_text .= '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]';
38 public static function insertFromTagFieldByItemId($itemid, $tags)
40 $profile_base = System::baseUrl();
41 $profile_data = parse_url($profile_base);
42 $profile_path = defaults($profile_data, 'path', '');
43 $profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
44 $profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
46 $fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'parent'];
47 $message = Item::selectFirst($fields, ['id' => $itemid]);
48 if (!DBM::is_result($message)) {
52 $message['tag'] = $tags;
55 dba::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]]);
57 if ($message['deleted']) {
61 $taglist = explode(',', $message['tag']);
64 foreach ($taglist as $tag) {
65 if ((substr(trim($tag), 0, 1) == '#') || (substr(trim($tag), 0, 1) == '@')) {
66 $tags_string .= ' ' . trim($tag);
68 $tags_string .= ' #' . trim($tag);
72 $data = ' ' . $message['title'] . ' ' . $message['body'] . ' ' . $tags_string . ' ';
74 // ignore anything in a code block
75 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
79 $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
80 if (preg_match_all($pattern, $data, $matches)) {
81 foreach ($matches[1] as $match) {
82 $tags['#' . $match] = '';
86 $pattern = '/\W([\#@])\[url\=(.*?)\](.*?)\[\/url\]/ism';
87 if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
88 foreach ($matches as $match) {
89 $tags[$match[1] . trim($match[3], ',.:;[]/\"?!')] = $match[2];
93 foreach ($tags as $tag => $link) {
94 if (substr(trim($tag), 0, 1) == '#') {
95 // try to ignore #039 or #1 or anything like that
96 if (ctype_digit(substr(trim($tag), 1))) {
100 // try to ignore html hex escapes, e.g. #x2317
101 if ((substr(trim($tag), 1, 1) == 'x' || substr(trim($tag), 1, 1) == 'X') && ctype_digit(substr(trim($tag), 2))) {
105 $type = TERM_HASHTAG;
106 $term = substr($tag, 1);
107 } elseif (substr(trim($tag), 0, 1) == '@') {
108 $type = TERM_MENTION;
109 $term = substr($tag, 1);
110 } else { // This shouldn't happen
111 $type = TERM_HASHTAG;
115 if ($message['uid'] == 0) {
117 dba::update('term', ['global' => true], ['otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
119 $global = dba::exists('term', ['uid' => 0, 'otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
122 dba::insert('term', [
123 'uid' => $message['uid'],
125 'otype' => TERM_OBJ_POST,
129 'guid' => $message['guid'],
130 'created' => $message['created'],
131 'received' => $message['received'],
135 // Search for mentions
136 if ((substr($tag, 0, 1) == '@') && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) {
137 $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
138 foreach ($users AS $user) {
139 if ($user['uid'] == $message['uid']) {
140 /// @todo This function is called frim Item::update - so we mustn't call that function here
141 dba::update('item', ['mention' => true], ['id' => $itemid]);
142 dba::update('thread', ['mention' => true], ['iid' => $message['parent']]);
150 * @param integer $itemid item id
153 public static function insertFromFileFieldByItemId($itemid)
155 $message = Item::selectFirst(['uid', 'deleted', 'file'], ['id' => $itemid]);
156 if (!DBM::is_result($message)) {
161 dba::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]]);
163 if ($message["deleted"]) {
167 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
168 foreach ($files[1] as $file) {
169 dba::insert('term', [
170 'uid' => $message["uid"],
172 'otype' => TERM_OBJ_POST,
179 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
180 foreach ($files[1] as $file) {
181 dba::insert('term', [
182 'uid' => $message["uid"],
184 'otype' => TERM_OBJ_POST,
185 'type' => TERM_CATEGORY,
193 * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
194 * provided item's body with them.
199 public static function populateTagsFromItem(&$item)
207 $searchpath = System::baseUrl() . "/search?tag=";
209 $taglist = dba::select(
211 ['type', 'term', 'url'],
212 ["`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION],
216 while ($tag = dba::fetch($taglist)) {
217 if ($tag["url"] == "") {
218 $tag["url"] = $searchpath . $tag["term"];
221 $orig_tag = $tag["url"];
223 $tag["url"] = Contact::magicLinkById($item['author-id'], $tag['url']);
225 if ($tag["type"] == TERM_HASHTAG) {
226 if ($orig_tag != $tag["url"]) {
227 $item['body'] = str_replace($orig_tag, $tag["url"], $item['body']);
230 $return['hashtags'][] = "#<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
232 } elseif ($tag["type"] == TERM_MENTION) {
233 $return['mentions'][] = "@<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
237 $return['tags'][] = $prefix . "<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
239 dba::close($taglist);