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