]> git.mxchange.org Git - friendica.git/blob - src/Model/Term.php
bed44d6857a0f66580ddb4e8d3ee20aa0a0c9d28
[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 Friendica\Model\Item;
10 use dba;
11
12 require_once 'boot.php';
13 require_once 'include/conversation.php';
14 require_once 'include/dba.php';
15
16 class Term
17 {
18         public static function tagTextFromItemId($itemid)
19         {
20                 $tag_text = '';
21                 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]];
22                 $tags = dba::select('term', [], $condition);
23                 while ($tag = dba::fetch($tags)) {
24                         if ($tag_text != '') {
25                                 $tag_text .= ',';
26                         }
27
28                         if ($tag['type'] == 1) {
29                                 $tag_text .= '#';
30                         } else {
31                                 $tag_text .= '@';
32                         }
33                         $tag_text .= '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]';
34                 }
35                 return $tag_text;
36         }
37
38         public static function fileTextFromItemId($itemid)
39         {
40                 $file_text = '';
41                 $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]];
42                 $tags = dba::select('term', [], $condition);
43                 while ($tag = dba::fetch($tags)) {
44                         $file_text .= '[' . $tag['term'] . ']';
45                 }
46                 return $file_text;
47         }
48
49         public static function insertFromTagFieldByItemId($itemid, $tags)
50         {
51                 $profile_base = System::baseUrl();
52                 $profile_data = parse_url($profile_base);
53                 $profile_path = defaults($profile_data, 'path', '');
54                 $profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
55                 $profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
56
57                 $fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'parent'];
58                 $message = Item::selectFirst($fields, ['id' => $itemid]);
59                 if (!DBM::is_result($message)) {
60                         return;
61                 }
62
63                 $message['tag'] = $tags;
64
65                 // Clean up all tags
66                 dba::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]]);
67
68                 if ($message['deleted']) {
69                         return;
70                 }
71
72                 $taglist = explode(',', $message['tag']);
73
74                 $tags_string = '';
75                 foreach ($taglist as $tag) {
76                         if ((substr(trim($tag), 0, 1) == '#') || (substr(trim($tag), 0, 1) == '@')) {
77                                 $tags_string .= ' ' . trim($tag);
78                         } else {
79                                 $tags_string .= ' #' . trim($tag);
80                         }
81                 }
82
83                 $data = ' ' . $message['title'] . ' ' . $message['body'] . ' ' . $tags_string . ' ';
84
85                 // ignore anything in a code block
86                 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
87
88                 $tags = [];
89
90                 $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
91                 if (preg_match_all($pattern, $data, $matches)) {
92                         foreach ($matches[1] as $match) {
93                                 $tags['#' . $match] = '';
94                         }
95                 }
96
97                 $pattern = '/\W([\#@])\[url\=(.*?)\](.*?)\[\/url\]/ism';
98                 if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
99                         foreach ($matches as $match) {
100                                 $tags[$match[1] . trim($match[3], ',.:;[]/\"?!')] = $match[2];
101                         }
102                 }
103
104                 foreach ($tags as $tag => $link) {
105                         if (substr(trim($tag), 0, 1) == '#') {
106                                 // try to ignore #039 or #1 or anything like that
107                                 if (ctype_digit(substr(trim($tag), 1))) {
108                                         continue;
109                                 }
110
111                                 // try to ignore html hex escapes, e.g. #x2317
112                                 if ((substr(trim($tag), 1, 1) == 'x' || substr(trim($tag), 1, 1) == 'X') && ctype_digit(substr(trim($tag), 2))) {
113                                         continue;
114                                 }
115
116                                 $type = TERM_HASHTAG;
117                                 $term = substr($tag, 1);
118                         } elseif (substr(trim($tag), 0, 1) == '@') {
119                                 $type = TERM_MENTION;
120                                 $term = substr($tag, 1);
121                         } else { // This shouldn't happen
122                                 $type = TERM_HASHTAG;
123                                 $term = $tag;
124                         }
125
126                         if ($message['uid'] == 0) {
127                                 $global = true;
128                                 dba::update('term', ['global' => true], ['otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
129                         } else {
130                                 $global = dba::exists('term', ['uid' => 0, 'otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
131                         }
132
133                         dba::insert('term', [
134                                 'uid'      => $message['uid'],
135                                 'oid'      => $itemid,
136                                 'otype'    => TERM_OBJ_POST,
137                                 'type'     => $type,
138                                 'term'     => $term,
139                                 'url'      => $link,
140                                 'guid'     => $message['guid'],
141                                 'created'  => $message['created'],
142                                 'received' => $message['received'],
143                                 'global'   => $global
144                         ]);
145
146                         // Search for mentions
147                         if ((substr($tag, 0, 1) == '@') && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) {
148                                 $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
149                                 foreach ($users AS $user) {
150                                         if ($user['uid'] == $message['uid']) {
151                                                 /// @todo This function is called frim Item::update - so we mustn't call that function here
152                                                 dba::update('item', ['mention' => true], ['id' => $itemid]);
153                                                 dba::update('thread', ['mention' => true], ['iid' => $message['parent']]);
154                                         }
155                                 }
156                         }
157                 }
158         }
159
160         /**
161          * @param integer $itemid item id
162          * @return void
163          */
164         public static function insertFromFileFieldByItemId($itemid, $files)
165         {
166                 $message = Item::selectFirst(['uid', 'deleted'], ['id' => $itemid]);
167                 if (!DBM::is_result($message)) {
168                         return;
169                 }
170
171                 // Clean up all tags
172                 dba::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]]);
173
174                 if ($message["deleted"]) {
175                         return;
176                 }
177
178                 $message['file'] = $files;
179
180                 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
181                         foreach ($files[1] as $file) {
182                                 dba::insert('term', [
183                                         'uid' => $message["uid"],
184                                         'oid' => $itemid,
185                                         'otype' => TERM_OBJ_POST,
186                                         'type' => TERM_FILE,
187                                         'term' => $file
188                                 ]);
189                         }
190                 }
191
192                 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
193                         foreach ($files[1] as $file) {
194                                 dba::insert('term', [
195                                         'uid' => $message["uid"],
196                                         'oid' => $itemid,
197                                         'otype' => TERM_OBJ_POST,
198                                         'type' => TERM_CATEGORY,
199                                         'term' => $file
200                                 ]);
201                         }
202                 }
203         }
204
205         /**
206          * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
207          * provided item's body with them.
208          *
209          * @param array $item
210          * @return array
211          */
212         public static function populateTagsFromItem(&$item)
213         {
214                 $return = [
215                         'tags' => [],
216                         'hashtags' => [],
217                         'mentions' => [],
218                 ];
219
220                 $searchpath = System::baseUrl() . "/search?tag=";
221
222                 $taglist = dba::select(
223                         'term',
224                         ['type', 'term', 'url'],
225                         ["`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION],
226                         ['order' => ['tid']]
227                 );
228
229                 while ($tag = dba::fetch($taglist)) {
230                         if ($tag["url"] == "") {
231                                 $tag["url"] = $searchpath . $tag["term"];
232                         }
233
234                         $orig_tag = $tag["url"];
235
236                         $tag["url"] = Contact::magicLinkById($item['author-id'], $tag['url']);
237
238                         if ($tag["type"] == TERM_HASHTAG) {
239                                 if ($orig_tag != $tag["url"]) {
240                                         $item['body'] = str_replace($orig_tag, $tag["url"], $item['body']);
241                                 }
242
243                                 $return['hashtags'][] = "#<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
244                                 $prefix = "#";
245                         } elseif ($tag["type"] == TERM_MENTION) {
246                                 $return['mentions'][] = "@<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
247                                 $prefix = "@";
248                         }
249
250                         $return['tags'][] = $prefix . "<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
251                 }
252                 dba::close($taglist);
253
254                 return $return;
255         }
256 }