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 if ($tag['type'] == TERM_CATEGORY) {
45 $file_text .= '<' . $tag['term'] . '>';
47 $file_text .= '[' . $tag['term'] . ']';
53 public static function insertFromTagFieldByItemId($itemid, $tags)
55 $profile_base = System::baseUrl();
56 $profile_data = parse_url($profile_base);
57 $profile_path = defaults($profile_data, 'path', '');
58 $profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
59 $profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
61 $fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'parent'];
62 $message = Item::selectFirst($fields, ['id' => $itemid]);
63 if (!DBM::is_result($message)) {
67 $message['tag'] = $tags;
70 dba::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]]);
72 if ($message['deleted']) {
76 $taglist = explode(',', $message['tag']);
79 foreach ($taglist as $tag) {
80 if ((substr(trim($tag), 0, 1) == '#') || (substr(trim($tag), 0, 1) == '@')) {
81 $tags_string .= ' ' . trim($tag);
83 $tags_string .= ' #' . trim($tag);
87 $data = ' ' . $message['title'] . ' ' . $message['body'] . ' ' . $tags_string . ' ';
89 // ignore anything in a code block
90 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
94 $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
95 if (preg_match_all($pattern, $data, $matches)) {
96 foreach ($matches[1] as $match) {
97 $tags['#' . $match] = '';
101 $pattern = '/\W([\#@])\[url\=(.*?)\](.*?)\[\/url\]/ism';
102 if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
103 foreach ($matches as $match) {
104 $tags[$match[1] . trim($match[3], ',.:;[]/\"?!')] = $match[2];
108 foreach ($tags as $tag => $link) {
109 if (substr(trim($tag), 0, 1) == '#') {
110 // try to ignore #039 or #1 or anything like that
111 if (ctype_digit(substr(trim($tag), 1))) {
115 // try to ignore html hex escapes, e.g. #x2317
116 if ((substr(trim($tag), 1, 1) == 'x' || substr(trim($tag), 1, 1) == 'X') && ctype_digit(substr(trim($tag), 2))) {
120 $type = TERM_HASHTAG;
121 $term = substr($tag, 1);
122 } elseif (substr(trim($tag), 0, 1) == '@') {
123 $type = TERM_MENTION;
124 $term = substr($tag, 1);
125 } else { // This shouldn't happen
126 $type = TERM_HASHTAG;
130 if ($message['uid'] == 0) {
132 dba::update('term', ['global' => true], ['otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
134 $global = dba::exists('term', ['uid' => 0, 'otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
137 dba::insert('term', [
138 'uid' => $message['uid'],
140 'otype' => TERM_OBJ_POST,
144 'guid' => $message['guid'],
145 'created' => $message['created'],
146 'received' => $message['received'],
150 // Search for mentions
151 if ((substr($tag, 0, 1) == '@') && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) {
152 $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
153 foreach ($users AS $user) {
154 if ($user['uid'] == $message['uid']) {
155 /// @todo This function is called frim Item::update - so we mustn't call that function here
156 dba::update('item', ['mention' => true], ['id' => $itemid]);
157 dba::update('thread', ['mention' => true], ['iid' => $message['parent']]);
165 * @param integer $itemid item id
168 public static function insertFromFileFieldByItemId($itemid, $files)
170 $message = Item::selectFirst(['uid', 'deleted'], ['id' => $itemid]);
171 if (!DBM::is_result($message)) {
176 dba::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]]);
178 if ($message["deleted"]) {
182 $message['file'] = $files;
184 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
185 foreach ($files[1] as $file) {
186 dba::insert('term', [
187 'uid' => $message["uid"],
189 'otype' => TERM_OBJ_POST,
196 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
197 foreach ($files[1] as $file) {
198 dba::insert('term', [
199 'uid' => $message["uid"],
201 'otype' => TERM_OBJ_POST,
202 'type' => TERM_CATEGORY,
210 * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
211 * provided item's body with them.
216 public static function populateTagsFromItem(&$item)
224 $searchpath = System::baseUrl() . "/search?tag=";
226 $taglist = dba::select(
228 ['type', 'term', 'url'],
229 ["`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION],
233 while ($tag = dba::fetch($taglist)) {
234 if ($tag["url"] == "") {
235 $tag["url"] = $searchpath . $tag["term"];
238 $orig_tag = $tag["url"];
240 $author = ['uid' => 0, 'id' => $item['author-id'],
241 'network' => $item['author-network'], 'url' => $item['author-link']];
242 $tag["url"] = Contact::magicLinkByContact($author, $tag['url']);
244 if ($tag["type"] == TERM_HASHTAG) {
245 if ($orig_tag != $tag["url"]) {
246 $item['body'] = str_replace($orig_tag, $tag["url"], $item['body']);
249 $return['hashtags'][] = "#<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
251 } elseif ($tag["type"] == TERM_MENTION) {
252 $return['mentions'][] = "@<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
256 $return['tags'][] = $prefix . "<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
258 dba::close($taglist);