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