5 namespace Friendica\Model;
7 use Friendica\Core\System;
8 use Friendica\Database\DBA;
10 require_once 'boot.php';
11 require_once 'include/conversation.php';
12 require_once 'include/dba.php';
16 public static function tagTextFromItemId($itemid)
19 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]];
20 $tags = DBA::select('term', [], $condition);
21 while ($tag = DBA::fetch($tags)) {
22 if ($tag_text != '') {
26 if ($tag['type'] == 1) {
31 $tag_text .= '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]';
36 public static function fileTextFromItemId($itemid)
39 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]];
40 $tags = DBA::select('term', [], $condition);
41 while ($tag = DBA::fetch($tags)) {
42 if ($tag['type'] == TERM_CATEGORY) {
43 $file_text .= '<' . $tag['term'] . '>';
45 $file_text .= '[' . $tag['term'] . ']';
51 public static function insertFromTagFieldByItemId($itemid, $tags)
53 $profile_base = System::baseUrl();
54 $profile_data = parse_url($profile_base);
55 $profile_path = defaults($profile_data, 'path', '');
56 $profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
57 $profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
59 $fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'parent'];
60 $message = Item::selectFirst($fields, ['id' => $itemid]);
61 if (!DBA::isResult($message)) {
65 $message['tag'] = $tags;
68 DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]]);
70 if ($message['deleted']) {
74 $taglist = explode(',', $message['tag']);
77 foreach ($taglist as $tag) {
78 if ((substr(trim($tag), 0, 1) == '#') || (substr(trim($tag), 0, 1) == '@')) {
79 $tags_string .= ' ' . trim($tag);
81 $tags_string .= ' #' . trim($tag);
85 $data = ' ' . $message['title'] . ' ' . $message['body'] . ' ' . $tags_string . ' ';
87 // ignore anything in a code block
88 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
92 $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
93 if (preg_match_all($pattern, $data, $matches)) {
94 foreach ($matches[1] as $match) {
95 $tags['#' . $match] = '';
99 $pattern = '/\W([\#@])\[url\=(.*?)\](.*?)\[\/url\]/ism';
100 if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
101 foreach ($matches as $match) {
102 $tags[$match[1] . trim($match[3], ',.:;[]/\"?!')] = $match[2];
106 foreach ($tags as $tag => $link) {
107 if (substr(trim($tag), 0, 1) == '#') {
108 // try to ignore #039 or #1 or anything like that
109 if (ctype_digit(substr(trim($tag), 1))) {
113 // try to ignore html hex escapes, e.g. #x2317
114 if ((substr(trim($tag), 1, 1) == 'x' || substr(trim($tag), 1, 1) == 'X') && ctype_digit(substr(trim($tag), 2))) {
118 $type = TERM_HASHTAG;
119 $term = substr($tag, 1);
120 } elseif (substr(trim($tag), 0, 1) == '@') {
121 $type = TERM_MENTION;
122 $term = substr($tag, 1);
123 } else { // This shouldn't happen
124 $type = TERM_HASHTAG;
128 if ($message['uid'] == 0) {
130 DBA::update('term', ['global' => true], ['otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
132 $global = DBA::exists('term', ['uid' => 0, 'otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
135 DBA::insert('term', [
136 'uid' => $message['uid'],
138 'otype' => TERM_OBJ_POST,
142 'guid' => $message['guid'],
143 'created' => $message['created'],
144 'received' => $message['received'],
148 // Search for mentions
149 if ((substr($tag, 0, 1) == '@') && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) {
150 $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
151 foreach ($users AS $user) {
152 if ($user['uid'] == $message['uid']) {
153 /// @todo This function is called frim Item::update - so we mustn't call that function here
154 DBA::update('item', ['mention' => true], ['id' => $itemid]);
155 DBA::update('thread', ['mention' => true], ['iid' => $message['parent']]);
163 * @param integer $itemid item id
166 public static function insertFromFileFieldByItemId($itemid, $files)
168 $message = Item::selectFirst(['uid', 'deleted'], ['id' => $itemid]);
169 if (!DBA::isResult($message)) {
174 DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]]);
176 if ($message["deleted"]) {
180 $message['file'] = $files;
182 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
183 foreach ($files[1] as $file) {
184 DBA::insert('term', [
185 'uid' => $message["uid"],
187 'otype' => TERM_OBJ_POST,
194 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
195 foreach ($files[1] as $file) {
196 DBA::insert('term', [
197 'uid' => $message["uid"],
199 'otype' => TERM_OBJ_POST,
200 'type' => TERM_CATEGORY,
208 * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
209 * provided item's body with them.
214 public static function populateTagsFromItem(&$item)
222 $searchpath = System::baseUrl() . "/search?tag=";
224 $taglist = DBA::select(
226 ['type', 'term', 'url'],
227 ["`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION],
231 while ($tag = DBA::fetch($taglist)) {
232 if ($tag["url"] == "") {
233 $tag["url"] = $searchpath . $tag["term"];
236 $orig_tag = $tag["url"];
238 $author = ['uid' => 0, 'id' => $item['author-id'],
239 'network' => $item['author-network'], 'url' => $item['author-link']];
240 $tag["url"] = Contact::magicLinkByContact($author, $tag['url']);
242 if ($tag["type"] == TERM_HASHTAG) {
243 if ($orig_tag != $tag["url"]) {
244 $item['body'] = str_replace($orig_tag, $tag["url"], $item['body']);
247 $return['hashtags'][] = "#<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
249 } elseif ($tag["type"] == TERM_MENTION) {
250 $return['mentions'][] = "@<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
254 $return['tags'][] = $prefix . "<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
256 DBA::close($taglist);