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