5 namespace Friendica\Model;
7 use Friendica\Core\System;
8 use Friendica\Database\DBA;
12 public static function tagTextFromItemId($itemid)
15 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]];
16 $tags = DBA::select('term', [], $condition);
17 while ($tag = DBA::fetch($tags)) {
18 if ($tag_text != '') {
22 if ($tag['type'] == 1) {
27 $tag_text .= '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]';
32 public static function tagArrayFromItemId($itemid, $type = [TERM_HASHTAG, TERM_MENTION])
34 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => $type];
35 $tags = DBA::select('term', ['type', 'term', 'url'], $condition);
36 if (!DBA::isResult($tags)) {
40 return DBA::toArray($tags);
43 public static function fileTextFromItemId($itemid)
46 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]];
47 $tags = DBA::select('term', [], $condition);
48 while ($tag = DBA::fetch($tags)) {
49 if ($tag['type'] == TERM_CATEGORY) {
50 $file_text .= '<' . $tag['term'] . '>';
52 $file_text .= '[' . $tag['term'] . ']';
58 public static function insertFromTagFieldByItemId($itemid, $tags)
60 $profile_base = System::baseUrl();
61 $profile_data = parse_url($profile_base);
62 $profile_path = defaults($profile_data, 'path', '');
63 $profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
64 $profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
66 $fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'parent'];
67 $message = Item::selectFirst($fields, ['id' => $itemid]);
68 if (!DBA::isResult($message)) {
72 $message['tag'] = $tags;
75 self::deleteByItemId($itemid);
77 if ($message['deleted']) {
81 $taglist = explode(',', $message['tag']);
84 foreach ($taglist as $tag) {
85 if ((substr(trim($tag), 0, 1) == '#') || (substr(trim($tag), 0, 1) == '@') || (substr(trim($tag), 0, 1) == '!')) {
86 $tags_string .= ' ' . trim($tag);
88 $tags_string .= ' #' . trim($tag);
92 $data = ' ' . $message['title'] . ' ' . $message['body'] . ' ' . $tags_string . ' ';
94 // ignore anything in a code block
95 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
99 $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
100 if (preg_match_all($pattern, $data, $matches)) {
101 foreach ($matches[1] as $match) {
102 $tags['#' . $match] = '';
106 $pattern = '/\W([\#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism';
107 if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
108 foreach ($matches as $match) {
110 if (($match[1] == '@') || ($match[1] == '!')) {
111 $contact = Contact::getDetailsByURL($match[2], 0);
112 if (!empty($contact['addr'])) {
113 $match[3] = $contact['addr'];
116 if (!empty($contact['url'])) {
117 $match[2] = $contact['url'];
121 $tags[$match[1] . trim($match[3], ',.:;[]/\"?!')] = $match[2];
125 foreach ($tags as $tag => $link) {
126 if (substr(trim($tag), 0, 1) == '#') {
127 // try to ignore #039 or #1 or anything like that
128 if (ctype_digit(substr(trim($tag), 1))) {
132 // try to ignore html hex escapes, e.g. #x2317
133 if ((substr(trim($tag), 1, 1) == 'x' || substr(trim($tag), 1, 1) == 'X') && ctype_digit(substr(trim($tag), 2))) {
137 $type = TERM_HASHTAG;
138 $term = substr($tag, 1);
140 } elseif ((substr(trim($tag), 0, 1) == '@') || (substr(trim($tag), 0, 1) == '!')) {
141 $type = TERM_MENTION;
143 $contact = Contact::getDetailsByURL($link, 0);
144 if (!empty($contact['name'])) {
145 $term = $contact['name'];
147 $term = substr($tag, 1);
149 } else { // This shouldn't happen
150 $type = TERM_HASHTAG;
155 if (DBA::exists('term', ['uid' => $message['uid'], 'otype' => TERM_OBJ_POST, 'oid' => $itemid, 'term' => $term])) {
159 if ($message['uid'] == 0) {
161 DBA::update('term', ['global' => true], ['otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
163 $global = DBA::exists('term', ['uid' => 0, 'otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
166 DBA::insert('term', [
167 'uid' => $message['uid'],
169 'otype' => TERM_OBJ_POST,
173 'guid' => $message['guid'],
174 'created' => $message['created'],
175 'received' => $message['received'],
179 // Search for mentions
180 if (((substr($tag, 0, 1) == '@') || (substr($tag, 0, 1) == '!')) && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) {
181 $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
182 foreach ($users AS $user) {
183 if ($user['uid'] == $message['uid']) {
184 /// @todo This function is called frim Item::update - so we mustn't call that function here
185 DBA::update('item', ['mention' => true], ['id' => $itemid]);
186 DBA::update('thread', ['mention' => true], ['iid' => $message['parent']]);
194 * @param integer $itemid item id
199 public static function insertFromFileFieldByItemId($itemid, $files)
201 $message = Item::selectFirst(['uid', 'deleted'], ['id' => $itemid]);
202 if (!DBA::isResult($message)) {
207 DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]]);
209 if ($message["deleted"]) {
213 $message['file'] = $files;
215 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
216 foreach ($files[1] as $file) {
217 DBA::insert('term', [
218 'uid' => $message["uid"],
220 'otype' => TERM_OBJ_POST,
227 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
228 foreach ($files[1] as $file) {
229 DBA::insert('term', [
230 'uid' => $message["uid"],
232 'otype' => TERM_OBJ_POST,
233 'type' => TERM_CATEGORY,
241 * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
242 * provided item's body with them.
246 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
247 * @throws \ImagickException
249 public static function populateTagsFromItem(&$item)
257 $searchpath = System::baseUrl() . "/search?tag=";
259 $taglist = DBA::select(
261 ['type', 'term', 'url'],
262 ["`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION],
266 while ($tag = DBA::fetch($taglist)) {
267 if ($tag['url'] == '') {
268 $tag['url'] = $searchpath . rawurlencode($tag['term']);
271 $orig_tag = $tag['url'];
273 $author = ['uid' => 0, 'id' => $item['author-id'],
274 'network' => $item['author-network'], 'url' => $item['author-link']];
275 $tag['url'] = Contact::magicLinkByContact($author, $tag['url']);
278 if ($tag['type'] == TERM_HASHTAG) {
279 if ($orig_tag != $tag['url']) {
280 $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
283 $return['hashtags'][] = '#<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
285 } elseif ($tag['type'] == TERM_MENTION) {
286 $return['mentions'][] = '@<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
290 $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
292 DBA::close($taglist);
298 * Delete all tags from an item
300 * @param int itemid - choose from which item the tags will be removed
304 public static function deleteByItemId($itemid, $type = [TERM_HASHTAG, TERM_MENTION])
306 if (empty($itemid)) {
311 DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => $type]);