5 namespace Friendica\Model;
7 use Friendica\Core\System;
8 use Friendica\Database\DBA;
10 require_once 'boot.php';
11 require_once 'include/conversation.php';
12 require_once 'include/dba.php';
16 public static function tagTextFromItemId($itemid)
19 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]];
20 $tags = DBA::select('term', [], $condition);
21 while ($tag = DBA::fetch($tags)) {
22 if ($tag_text != '') {
26 if ($tag['type'] == 1) {
31 $tag_text .= '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]';
36 public static function tagArrayFromItemId($itemid, $type = [TERM_HASHTAG, TERM_MENTION])
38 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => $type];
39 $tags = DBA::select('term', ['type', 'term', 'url'], $condition);
40 if (!DBA::isResult($tags)) {
44 return DBA::toArray($tags);
47 public static function fileTextFromItemId($itemid)
50 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]];
51 $tags = DBA::select('term', [], $condition);
52 while ($tag = DBA::fetch($tags)) {
53 if ($tag['type'] == TERM_CATEGORY) {
54 $file_text .= '<' . $tag['term'] . '>';
56 $file_text .= '[' . $tag['term'] . ']';
62 public static function insertFromTagFieldByItemId($itemid, $tags)
64 $profile_base = System::baseUrl();
65 $profile_data = parse_url($profile_base);
66 $profile_path = defaults($profile_data, 'path', '');
67 $profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
68 $profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
70 $fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'parent'];
71 $message = Item::selectFirst($fields, ['id' => $itemid]);
72 if (!DBA::isResult($message)) {
76 $message['tag'] = $tags;
79 self::deleteByItemId($itemid);
81 if ($message['deleted']) {
85 $taglist = explode(',', $message['tag']);
88 foreach ($taglist as $tag) {
89 if ((substr(trim($tag), 0, 1) == '#') || (substr(trim($tag), 0, 1) == '@') || (substr(trim($tag), 0, 1) == '!')) {
90 $tags_string .= ' ' . trim($tag);
92 $tags_string .= ' #' . trim($tag);
96 $data = ' ' . $message['title'] . ' ' . $message['body'] . ' ' . $tags_string . ' ';
98 // ignore anything in a code block
99 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
103 $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
104 if (preg_match_all($pattern, $data, $matches)) {
105 foreach ($matches[1] as $match) {
106 $tags['#' . $match] = '';
110 $pattern = '/\W([\#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism';
111 if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
112 foreach ($matches as $match) {
114 if (($match[1] == '@') || ($match[1] == '!')) {
115 $contact = Contact::getDetailsByURL($match[2], 0);
116 if (!empty($contact['addr'])) {
117 $match[3] = $contact['addr'];
120 if (!empty($contact['url'])) {
121 $match[2] = $contact['url'];
125 $tags[$match[1] . trim($match[3], ',.:;[]/\"?!')] = $match[2];
129 foreach ($tags as $tag => $link) {
130 if (substr(trim($tag), 0, 1) == '#') {
131 // try to ignore #039 or #1 or anything like that
132 if (ctype_digit(substr(trim($tag), 1))) {
136 // try to ignore html hex escapes, e.g. #x2317
137 if ((substr(trim($tag), 1, 1) == 'x' || substr(trim($tag), 1, 1) == 'X') && ctype_digit(substr(trim($tag), 2))) {
141 $type = TERM_HASHTAG;
142 $term = substr($tag, 1);
144 } elseif ((substr(trim($tag), 0, 1) == '@') || (substr(trim($tag), 0, 1) == '!')) {
145 $type = TERM_MENTION;
147 $contact = Contact::getDetailsByURL($link, 0);
148 if (!empty($contact['name'])) {
149 $term = $contact['name'];
151 $term = substr($tag, 1);
153 } else { // This shouldn't happen
154 $type = TERM_HASHTAG;
159 if (DBA::exists('term', ['uid' => $message['uid'], 'otype' => TERM_OBJ_POST, 'oid' => $itemid, 'term' => $term])) {
163 if ($message['uid'] == 0) {
165 DBA::update('term', ['global' => true], ['otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
167 $global = DBA::exists('term', ['uid' => 0, 'otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
170 DBA::insert('term', [
171 'uid' => $message['uid'],
173 'otype' => TERM_OBJ_POST,
177 'guid' => $message['guid'],
178 'created' => $message['created'],
179 'received' => $message['received'],
183 // Search for mentions
184 if (((substr($tag, 0, 1) == '@') || (substr($tag, 0, 1) == '!')) && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) {
185 $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
186 foreach ($users AS $user) {
187 if ($user['uid'] == $message['uid']) {
188 /// @todo This function is called frim Item::update - so we mustn't call that function here
189 DBA::update('item', ['mention' => true], ['id' => $itemid]);
190 DBA::update('thread', ['mention' => true], ['iid' => $message['parent']]);
198 * @param integer $itemid item id
201 public static function insertFromFileFieldByItemId($itemid, $files)
203 $message = Item::selectFirst(['uid', 'deleted'], ['id' => $itemid]);
204 if (!DBA::isResult($message)) {
209 DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]]);
211 if ($message["deleted"]) {
215 $message['file'] = $files;
217 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
218 foreach ($files[1] as $file) {
219 DBA::insert('term', [
220 'uid' => $message["uid"],
222 'otype' => TERM_OBJ_POST,
229 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
230 foreach ($files[1] as $file) {
231 DBA::insert('term', [
232 'uid' => $message["uid"],
234 'otype' => TERM_OBJ_POST,
235 'type' => TERM_CATEGORY,
243 * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
244 * provided item's body with them.
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']);
277 if ($tag['type'] == TERM_HASHTAG) {
278 if ($orig_tag != $tag['url']) {
279 $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
282 $return['hashtags'][] = '#<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
284 } elseif ($tag['type'] == TERM_MENTION) {
285 $return['mentions'][] = '@<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
289 $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
291 DBA::close($taglist);
297 * Delete all tags from an item
298 * @param int itemid - choose from which item the tags will be removed
299 * @param array type - items type. default is [TERM_HASHTAG, TERM_MENTION]
301 public static function deleteByItemId($itemid, $type = [TERM_HASHTAG, TERM_MENTION])
303 if (empty($itemid)) {
308 DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => $type]);