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)
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', 'tag', 'parent'];
47 $message = Item::selectFirst($fields, ['id' => $itemid]);
48 if (!DBM::is_result($message)) {
53 dba::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]]);
55 if ($message['deleted']) {
59 $taglist = explode(',', $message['tag']);
62 foreach ($taglist as $tag) {
63 if ((substr(trim($tag), 0, 1) == '#') || (substr(trim($tag), 0, 1) == '@')) {
64 $tags_string .= ' ' . trim($tag);
66 $tags_string .= ' #' . trim($tag);
70 $data = ' ' . $message['title'] . ' ' . $message['body'] . ' ' . $tags_string . ' ';
72 // ignore anything in a code block
73 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
77 $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
78 if (preg_match_all($pattern, $data, $matches)) {
79 foreach ($matches[1] as $match) {
80 $tags['#' . $match] = '';
84 $pattern = '/\W([\#@])\[url\=(.*?)\](.*?)\[\/url\]/ism';
85 if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
86 foreach ($matches as $match) {
87 $tags[$match[1] . trim($match[3], ',.:;[]/\"?!')] = $match[2];
91 foreach ($tags as $tag => $link) {
92 if (substr(trim($tag), 0, 1) == '#') {
93 // try to ignore #039 or #1 or anything like that
94 if (ctype_digit(substr(trim($tag), 1))) {
98 // try to ignore html hex escapes, e.g. #x2317
99 if ((substr(trim($tag), 1, 1) == 'x' || substr(trim($tag), 1, 1) == 'X') && ctype_digit(substr(trim($tag), 2))) {
103 $type = TERM_HASHTAG;
104 $term = substr($tag, 1);
105 } elseif (substr(trim($tag), 0, 1) == '@') {
106 $type = TERM_MENTION;
107 $term = substr($tag, 1);
108 } else { // This shouldn't happen
109 $type = TERM_HASHTAG;
113 if ($message['uid'] == 0) {
115 dba::update('term', ['global' => true], ['otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
117 $global = dba::exists('term', ['uid' => 0, 'otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
120 dba::insert('term', [
121 'uid' => $message['uid'],
123 'otype' => TERM_OBJ_POST,
127 'guid' => $message['guid'],
128 'created' => $message['created'],
129 'received' => $message['received'],
133 // Search for mentions
134 if ((substr($tag, 0, 1) == '@') && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) {
135 $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
136 foreach ($users AS $user) {
137 if ($user['uid'] == $message['uid']) {
138 /// @todo This function is called frim Item::update - so we mustn't call that function here
139 dba::update('item', ['mention' => true], ['id' => $itemid]);
140 dba::update('thread', ['mention' => true], ['iid' => $message['parent']]);
148 * @param integer $itemid item id
151 public static function insertFromFileFieldByItemId($itemid)
153 $message = Item::selectFirst(['uid', 'deleted', 'file'], ['id' => $itemid]);
154 if (!DBM::is_result($message)) {
159 dba::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]]);
161 if ($message["deleted"]) {
165 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
166 foreach ($files[1] as $file) {
167 dba::insert('term', [
168 'uid' => $message["uid"],
170 'otype' => TERM_OBJ_POST,
177 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
178 foreach ($files[1] as $file) {
179 dba::insert('term', [
180 'uid' => $message["uid"],
182 'otype' => TERM_OBJ_POST,
183 'type' => TERM_CATEGORY,
191 * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
192 * provided item's body with them.
197 public static function populateTagsFromItem(&$item)
205 $searchpath = System::baseUrl() . "/search?tag=";
207 $taglist = dba::select(
209 ['type', 'term', 'url'],
210 ["`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION],
214 while ($tag = dba::fetch($taglist)) {
215 if ($tag["url"] == "") {
216 $tag["url"] = $searchpath . $tag["term"];
219 $orig_tag = $tag["url"];
221 $tag["url"] = Contact::magicLinkById($item['author-id'], $tag['url']);
223 if ($tag["type"] == TERM_HASHTAG) {
224 if ($orig_tag != $tag["url"]) {
225 $item['body'] = str_replace($orig_tag, $tag["url"], $item['body']);
228 $return['hashtags'][] = "#<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
230 } elseif ($tag["type"] == TERM_MENTION) {
231 $return['mentions'][] = "@<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
235 $return['tags'][] = $prefix . "<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
237 dba::close($taglist);