5 namespace Friendica\Model;
7 use Friendica\Core\System;
8 use Friendica\Database\DBA;
9 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 tagTextFromItemId($itemid)
20 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]];
21 $tags = DBA::select('term', [], $condition);
22 while ($tag = DBA::fetch($tags)) {
23 if ($tag_text != '') {
27 if ($tag['type'] == 1) {
32 $tag_text .= '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]';
37 public static function fileTextFromItemId($itemid)
40 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]];
41 $tags = DBA::select('term', [], $condition);
42 while ($tag = DBA::fetch($tags)) {
43 if ($tag['type'] == TERM_CATEGORY) {
44 $file_text .= '<' . $tag['term'] . '>';
46 $file_text .= '[' . $tag['term'] . ']';
52 public static function insertFromTagFieldByItemId($itemid, $tags)
54 $profile_base = System::baseUrl();
55 $profile_data = parse_url($profile_base);
56 $profile_path = defaults($profile_data, 'path', '');
57 $profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
58 $profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
60 $fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'parent'];
61 $message = Item::selectFirst($fields, ['id' => $itemid]);
62 if (!DBM::is_result($message)) {
66 $message['tag'] = $tags;
69 DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]]);
71 if ($message['deleted']) {
75 $taglist = explode(',', $message['tag']);
78 foreach ($taglist as $tag) {
79 if ((substr(trim($tag), 0, 1) == '#') || (substr(trim($tag), 0, 1) == '@')) {
80 $tags_string .= ' ' . trim($tag);
82 $tags_string .= ' #' . trim($tag);
86 $data = ' ' . $message['title'] . ' ' . $message['body'] . ' ' . $tags_string . ' ';
88 // ignore anything in a code block
89 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
93 $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
94 if (preg_match_all($pattern, $data, $matches)) {
95 foreach ($matches[1] as $match) {
96 $tags['#' . $match] = '';
100 $pattern = '/\W([\#@])\[url\=(.*?)\](.*?)\[\/url\]/ism';
101 if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
102 foreach ($matches as $match) {
103 $tags[$match[1] . trim($match[3], ',.:;[]/\"?!')] = $match[2];
107 foreach ($tags as $tag => $link) {
108 if (substr(trim($tag), 0, 1) == '#') {
109 // try to ignore #039 or #1 or anything like that
110 if (ctype_digit(substr(trim($tag), 1))) {
114 // try to ignore html hex escapes, e.g. #x2317
115 if ((substr(trim($tag), 1, 1) == 'x' || substr(trim($tag), 1, 1) == 'X') && ctype_digit(substr(trim($tag), 2))) {
119 $type = TERM_HASHTAG;
120 $term = substr($tag, 1);
121 } elseif (substr(trim($tag), 0, 1) == '@') {
122 $type = TERM_MENTION;
123 $term = substr($tag, 1);
124 } else { // This shouldn't happen
125 $type = TERM_HASHTAG;
129 if ($message['uid'] == 0) {
131 DBA::update('term', ['global' => true], ['otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
133 $global = DBA::exists('term', ['uid' => 0, 'otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
136 DBA::insert('term', [
137 'uid' => $message['uid'],
139 'otype' => TERM_OBJ_POST,
143 'guid' => $message['guid'],
144 'created' => $message['created'],
145 'received' => $message['received'],
149 // Search for mentions
150 if ((substr($tag, 0, 1) == '@') && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) {
151 $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
152 foreach ($users AS $user) {
153 if ($user['uid'] == $message['uid']) {
154 /// @todo This function is called frim Item::update - so we mustn't call that function here
155 DBA::update('item', ['mention' => true], ['id' => $itemid]);
156 DBA::update('thread', ['mention' => true], ['iid' => $message['parent']]);
164 * @param integer $itemid item id
167 public static function insertFromFileFieldByItemId($itemid, $files)
169 $message = Item::selectFirst(['uid', 'deleted'], ['id' => $itemid]);
170 if (!DBM::is_result($message)) {
175 DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]]);
177 if ($message["deleted"]) {
181 $message['file'] = $files;
183 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
184 foreach ($files[1] as $file) {
185 DBA::insert('term', [
186 'uid' => $message["uid"],
188 'otype' => TERM_OBJ_POST,
195 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
196 foreach ($files[1] as $file) {
197 DBA::insert('term', [
198 'uid' => $message["uid"],
200 'otype' => TERM_OBJ_POST,
201 'type' => TERM_CATEGORY,
209 * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
210 * provided item's body with them.
215 public static function populateTagsFromItem(&$item)
223 $searchpath = System::baseUrl() . "/search?tag=";
225 $taglist = DBA::select(
227 ['type', 'term', 'url'],
228 ["`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION],
232 while ($tag = DBA::fetch($taglist)) {
233 if ($tag["url"] == "") {
234 $tag["url"] = $searchpath . $tag["term"];
237 $orig_tag = $tag["url"];
239 $author = ['uid' => 0, 'id' => $item['author-id'],
240 'network' => $item['author-network'], 'url' => $item['author-link']];
241 $tag["url"] = Contact::magicLinkByContact($author, $tag['url']);
243 if ($tag["type"] == TERM_HASHTAG) {
244 if ($orig_tag != $tag["url"]) {
245 $item['body'] = str_replace($orig_tag, $tag["url"], $item['body']);
248 $return['hashtags'][] = "#<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
250 } elseif ($tag["type"] == TERM_MENTION) {
251 $return['mentions'][] = "@<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
255 $return['tags'][] = $prefix . "<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
257 DBA::close($taglist);