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 insertFromItemId($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']]);
125 public static function insertFromItemUri($itemuri, $uid)
127 $messages = dba::select('item', ['id'], ['uri' => $itemuri, 'uid' => $uid]);
129 if (DBM::is_result($messages)) {
130 while ($message = dba::fetch($messages)) {
131 self::insertFromItemId($message['id']);
133 dba::close($messages);
139 * @param integer $itemid item id
142 public static function insertFromItemFileById($itemid)
144 $message = dba::selectFirst('item', ['uid', 'deleted', 'file'], ['id' => $itemid]);
145 if (!DBM::is_result($message)) {
150 q("DELETE FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` IN (%d, %d)",
151 intval(TERM_OBJ_POST),
154 intval(TERM_CATEGORY));
156 if ($message["deleted"]) {
160 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
161 foreach ($files[1] as $file) {
162 dba::insert('term', [
163 'uid' => $message["uid"],
165 'otype' => TERM_OBJ_POST,
172 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
173 foreach ($files[1] as $file) {
174 dba::insert('term', [
175 'uid' => $message["uid"],
177 'otype' => TERM_OBJ_POST,
178 'type' => TERM_CATEGORY,
186 * @param string $itemuri item uri
187 * @param integer $uid uid
190 public static function insertFromItemFileByUri($itemuri, $uid)
192 $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
194 if (count($messages)) {
195 foreach ($messages as $message) {
196 self::insertFromItemFileById($message["id"]);