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