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