]> git.mxchange.org Git - friendica.git/blob - src/Model/Term.php
Merge pull request #4404 from annando/item-isolation
[friendica.git] / src / Model / Term.php
1 <?php
2 /**
3  * @file src/Model/Term
4  */
5 namespace Friendica\Model;
6
7 use Friendica\Core\System;
8 use Friendica\Database\DBM;
9 use dba;
10
11 require_once 'boot.php';
12 require_once 'include/dba.php';
13
14 class Term
15 {
16         public static function insertFromTagFieldByItemId($itemid)
17         {
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/';
23
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)) {
27                         return;
28                 }
29
30                 // Clean up all tags
31                 dba::e("DELETE FROM `term` WHERE `otype` = ? AND `oid` = ? AND `type` IN (?, ?)",
32                         TERM_OBJ_POST, $itemid, TERM_HASHTAG, TERM_MENTION);
33
34                 if ($message['deleted']) {
35                         return;
36                 }
37
38                 $taglist = explode(',', $message['tag']);
39
40                 $tags_string = '';
41                 foreach ($taglist as $tag) {
42                         if ((substr(trim($tag), 0, 1) == '#') || (substr(trim($tag), 0, 1) == '@')) {
43                                 $tags_string .= ' ' . trim($tag);
44                         } else {
45                                 $tags_string .= ' #' . trim($tag);
46                         }
47                 }
48
49                 $data = ' ' . $message['title'] . ' ' . $message['body'] . ' ' . $tags_string . ' ';
50
51                 // ignore anything in a code block
52                 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
53
54                 $tags = [];
55
56                 $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
57                 if (preg_match_all($pattern, $data, $matches)) {
58                         foreach ($matches[1] as $match) {
59                                 $tags['#' . strtolower($match)] = '';
60                         }
61                 }
62
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];
67                         }
68                 }
69
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))) {
74                                         continue;
75                                 }
76
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))) {
79                                         continue;
80                                 }
81
82                                 $type = TERM_HASHTAG;
83                                 $term = substr($tag, 1);
84                         } elseif (substr(trim($tag), 0, 1) == '@') {
85                                 $type = TERM_MENTION;
86                                 $term = substr($tag, 1);
87                         } else { // This shouldn't happen
88                                 $type = TERM_HASHTAG;
89                                 $term = $tag;
90                         }
91
92                         if ($message['uid'] == 0) {
93                                 $global = true;
94                                 dba::update('term', ['global' => true], ['otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
95                         } else {
96                                 $global = dba::exists('term', ['uid' => 0, 'otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
97                         }
98
99                         dba::insert('term', [
100                                 'uid'      => $message['uid'],
101                                 'oid'      => $itemid,
102                                 'otype'    => TERM_OBJ_POST,
103                                 'type'     => $type,
104                                 'term'     => $term,
105                                 'url'      => $link,
106                                 'guid'     => $message['guid'],
107                                 'created'  => $message['created'],
108                                 'received' => $message['received'],
109                                 'global'   => $global
110                         ]);
111
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']]);
119                                         }
120                                 }
121                         }
122                 }
123         }
124
125         /**
126          * @param integer $itemid item id
127          * @return void
128          */
129         public static function insertFromFileFieldByItemId($itemid)
130         {
131                 $message = dba::selectFirst('item', ['uid', 'deleted', 'file'], ['id' => $itemid]);
132                 if (!DBM::is_result($message)) {
133                         return;
134                 }
135
136                 // Clean up all tags
137                 q("DELETE FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` IN (%d, %d)",
138                         intval(TERM_OBJ_POST),
139                         intval($itemid),
140                         intval(TERM_FILE),
141                         intval(TERM_CATEGORY));
142
143                 if ($message["deleted"]) {
144                         return;
145                 }
146
147                 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
148                         foreach ($files[1] as $file) {
149                                 dba::insert('term', [
150                                         'uid' => $message["uid"],
151                                         'oid' => $itemid,
152                                         'otype' => TERM_OBJ_POST,
153                                         'type' => TERM_FILE,
154                                         'term' => $file
155                                 ]);
156                         }
157                 }
158
159                 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
160                         foreach ($files[1] as $file) {
161                                 dba::insert('term', [
162                                         'uid' => $message["uid"],
163                                         'oid' => $itemid,
164                                         'otype' => TERM_OBJ_POST,
165                                         'type' => TERM_CATEGORY,
166                                         'term' => $file
167                                 ]);
168                         }
169                 }
170         }
171 }