5 namespace Friendica\Model;
7 use Friendica\Core\System;
8 use Friendica\Database\DBM;
11 require_once 'boot.php';
12 require_once 'include/conversation.php';
13 require_once 'include/dba.php';
17 public static function insertFromTagFieldByItemId($itemid)
19 $profile_base = System::baseUrl();
20 $profile_data = parse_url($profile_base);
21 $profile_path = defaults($profile_data, 'path', '');
22 $profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
23 $profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
25 $fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'tag', 'parent'];
26 $message = dba::selectFirst('item', $fields, ['id' => $itemid]);
27 if (!DBM::is_result($message)) {
32 dba::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]]);
34 if ($message['deleted']) {
38 $taglist = explode(',', $message['tag']);
41 foreach ($taglist as $tag) {
42 if ((substr(trim($tag), 0, 1) == '#') || (substr(trim($tag), 0, 1) == '@')) {
43 $tags_string .= ' ' . trim($tag);
45 $tags_string .= ' #' . trim($tag);
49 $data = ' ' . $message['title'] . ' ' . $message['body'] . ' ' . $tags_string . ' ';
51 // ignore anything in a code block
52 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
56 $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
57 if (preg_match_all($pattern, $data, $matches)) {
58 foreach ($matches[1] as $match) {
59 $tags['#' . strtolower($match)] = '';
63 $pattern = '/\W([\#@])\[url\=(.*?)\](.*?)\[\/url\]/ism';
64 if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
65 foreach ($matches as $match) {
66 $tags[$match[1] . strtolower(trim($match[3], ',.:;[]/\"?!'))] = $match[2];
70 foreach ($tags as $tag => $link) {
71 if (substr(trim($tag), 0, 1) == '#') {
72 // try to ignore #039 or #1 or anything like that
73 if (ctype_digit(substr(trim($tag), 1))) {
77 // try to ignore html hex escapes, e.g. #x2317
78 if ((substr(trim($tag), 1, 1) == 'x' || substr(trim($tag), 1, 1) == 'X') && ctype_digit(substr(trim($tag), 2))) {
83 $term = substr($tag, 1);
84 } elseif (substr(trim($tag), 0, 1) == '@') {
86 $term = substr($tag, 1);
87 } else { // This shouldn't happen
92 if ($message['uid'] == 0) {
94 dba::update('term', ['global' => true], ['otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
96 $global = dba::exists('term', ['uid' => 0, 'otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
100 'uid' => $message['uid'],
102 'otype' => TERM_OBJ_POST,
106 'guid' => $message['guid'],
107 'created' => $message['created'],
108 'received' => $message['received'],
112 // Search for mentions
113 if ((substr($tag, 0, 1) == '@') && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) {
114 $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
115 foreach ($users AS $user) {
116 if ($user['uid'] == $message['uid']) {
117 dba::update('item', ['mention' => true], ['id' => $itemid]);
118 dba::update('thread', ['mention' => true], ['iid' => $message['parent']]);
126 * @param integer $itemid item id
129 public static function insertFromFileFieldByItemId($itemid)
131 $message = dba::selectFirst('item', ['uid', 'deleted', 'file'], ['id' => $itemid]);
132 if (!DBM::is_result($message)) {
137 dba::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]]);
139 if ($message["deleted"]) {
143 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
144 foreach ($files[1] as $file) {
145 dba::insert('term', [
146 'uid' => $message["uid"],
148 'otype' => TERM_OBJ_POST,
155 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
156 foreach ($files[1] as $file) {
157 dba::insert('term', [
158 'uid' => $message["uid"],
160 'otype' => TERM_OBJ_POST,
161 'type' => TERM_CATEGORY,
169 * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
170 * provided item's body with them.
175 public static function populateTagsFromItem(&$item)
183 $searchpath = System::baseUrl() . "/search?tag=";
185 $taglist = dba::select(
187 ['type', 'term', 'url'],
188 ["`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION],
192 while ($tag = dba::fetch($taglist)) {
193 if ($tag["url"] == "") {
194 $tag["url"] = $searchpath . strtolower($tag["term"]);
197 $orig_tag = $tag["url"];
199 $tag["url"] = Contact::magicLink($item['author-link'], $tag['url']);
201 if ($tag["type"] == TERM_HASHTAG) {
202 if ($orig_tag != $tag["url"]) {
203 $item['body'] = str_replace($orig_tag, $tag["url"], $item['body']);
206 $return['hashtags'][] = "#<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
208 } elseif ($tag["type"] == TERM_MENTION) {
209 $return['mentions'][] = "@<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
213 $return['tags'][] = $prefix . "<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
215 dba::close($taglist);