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 fileTextFromItemId($itemid)
41 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]];
42 $tags = dba::select('term', [], $condition);
43 while ($tag = dba::fetch($tags)) {
44 $file_text .= '[' . $tag['term'] . ']';
49 public static function insertFromTagFieldByItemId($itemid, $tags)
51 $profile_base = System::baseUrl();
52 $profile_data = parse_url($profile_base);
53 $profile_path = defaults($profile_data, 'path', '');
54 $profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
55 $profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
57 $fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'parent'];
58 $message = Item::selectFirst($fields, ['id' => $itemid]);
59 if (!DBM::is_result($message)) {
63 $message['tag'] = $tags;
66 dba::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]]);
68 if ($message['deleted']) {
72 $taglist = explode(',', $message['tag']);
75 foreach ($taglist as $tag) {
76 if ((substr(trim($tag), 0, 1) == '#') || (substr(trim($tag), 0, 1) == '@')) {
77 $tags_string .= ' ' . trim($tag);
79 $tags_string .= ' #' . trim($tag);
83 $data = ' ' . $message['title'] . ' ' . $message['body'] . ' ' . $tags_string . ' ';
85 // ignore anything in a code block
86 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
90 $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
91 if (preg_match_all($pattern, $data, $matches)) {
92 foreach ($matches[1] as $match) {
93 $tags['#' . $match] = '';
97 $pattern = '/\W([\#@])\[url\=(.*?)\](.*?)\[\/url\]/ism';
98 if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
99 foreach ($matches as $match) {
100 $tags[$match[1] . trim($match[3], ',.:;[]/\"?!')] = $match[2];
104 foreach ($tags as $tag => $link) {
105 if (substr(trim($tag), 0, 1) == '#') {
106 // try to ignore #039 or #1 or anything like that
107 if (ctype_digit(substr(trim($tag), 1))) {
111 // try to ignore html hex escapes, e.g. #x2317
112 if ((substr(trim($tag), 1, 1) == 'x' || substr(trim($tag), 1, 1) == 'X') && ctype_digit(substr(trim($tag), 2))) {
116 $type = TERM_HASHTAG;
117 $term = substr($tag, 1);
118 } elseif (substr(trim($tag), 0, 1) == '@') {
119 $type = TERM_MENTION;
120 $term = substr($tag, 1);
121 } else { // This shouldn't happen
122 $type = TERM_HASHTAG;
126 if ($message['uid'] == 0) {
128 dba::update('term', ['global' => true], ['otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
130 $global = dba::exists('term', ['uid' => 0, 'otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
133 dba::insert('term', [
134 'uid' => $message['uid'],
136 'otype' => TERM_OBJ_POST,
140 'guid' => $message['guid'],
141 'created' => $message['created'],
142 'received' => $message['received'],
146 // Search for mentions
147 if ((substr($tag, 0, 1) == '@') && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) {
148 $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
149 foreach ($users AS $user) {
150 if ($user['uid'] == $message['uid']) {
151 /// @todo This function is called frim Item::update - so we mustn't call that function here
152 dba::update('item', ['mention' => true], ['id' => $itemid]);
153 dba::update('thread', ['mention' => true], ['iid' => $message['parent']]);
161 * @param integer $itemid item id
164 public static function insertFromFileFieldByItemId($itemid, $files)
166 $message = Item::selectFirst(['uid', 'deleted'], ['id' => $itemid]);
167 if (!DBM::is_result($message)) {
172 dba::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]]);
174 if ($message["deleted"]) {
178 $message['file'] = $files;
180 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
181 foreach ($files[1] as $file) {
182 dba::insert('term', [
183 'uid' => $message["uid"],
185 'otype' => TERM_OBJ_POST,
192 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
193 foreach ($files[1] as $file) {
194 dba::insert('term', [
195 'uid' => $message["uid"],
197 'otype' => TERM_OBJ_POST,
198 'type' => TERM_CATEGORY,
206 * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
207 * provided item's body with them.
212 public static function populateTagsFromItem(&$item)
220 $searchpath = System::baseUrl() . "/search?tag=";
222 $taglist = dba::select(
224 ['type', 'term', 'url'],
225 ["`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION],
229 while ($tag = dba::fetch($taglist)) {
230 if ($tag["url"] == "") {
231 $tag["url"] = $searchpath . $tag["term"];
234 $orig_tag = $tag["url"];
236 $tag["url"] = Contact::magicLinkById($item['author-id'], $tag['url']);
238 if ($tag["type"] == TERM_HASHTAG) {
239 if ($orig_tag != $tag["url"]) {
240 $item['body'] = str_replace($orig_tag, $tag["url"], $item['body']);
243 $return['hashtags'][] = "#<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
245 } elseif ($tag["type"] == TERM_MENTION) {
246 $return['mentions'][] = "@<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
250 $return['tags'][] = $prefix . "<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
252 dba::close($taglist);