5 namespace Friendica\Model;
7 use Friendica\Core\System;
8 use Friendica\Database\DBM;
11 require_once 'boot.php';
12 require_once 'include/dba.php';
16 public static function insertFromTagFieldByItemId($itemid)
18 $profile_base = System::baseUrl();
19 $profile_data = parse_url($profile_base);
20 $profile_path = defaults($profile_data, 'path', '');
21 $profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
22 $profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
24 $fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'tag', 'parent'];
25 $message = dba::selectFirst('item', $fields, ['id' => $itemid]);
26 if (!DBM::is_result($message)) {
31 dba::e("DELETE FROM `term` WHERE `otype` = ? AND `oid` = ? AND `type` IN (?, ?)",
32 TERM_OBJ_POST, $itemid, 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 q("DELETE FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` IN (%d, %d)",
138 intval(TERM_OBJ_POST),
141 intval(TERM_CATEGORY));
143 if ($message["deleted"]) {
147 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
148 foreach ($files[1] as $file) {
149 dba::insert('term', [
150 'uid' => $message["uid"],
152 'otype' => TERM_OBJ_POST,
159 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
160 foreach ($files[1] as $file) {
161 dba::insert('term', [
162 'uid' => $message["uid"],
164 'otype' => TERM_OBJ_POST,
165 'type' => TERM_CATEGORY,