]> git.mxchange.org Git - friendica.git/blob - include/tags.php
fb3315441c1336807e40f4e58fb6b22a1f7930e6
[friendica.git] / include / tags.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\System;
5 use Friendica\Database\DBM;
6 use Friendica\Object\Contact;
7
8 function create_tags_from_item($itemid) {
9         $profile_base = System::baseUrl();
10         $profile_data = parse_url($profile_base);
11         $profile_base_friendica = $profile_data['host'].$profile_data['path']."/profile/";
12         $profile_base_diaspora = $profile_data['host'].$profile_data['path']."/u/";
13
14         $messages = q("SELECT `guid`, `uid`, `id`, `edited`, `deleted`, `created`, `received`, `title`, `body`, `tag`, `parent` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
15
16         if (!$messages)
17                 return;
18
19         $message = $messages[0];
20
21         // Clean up all tags
22         q("DELETE FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` IN (%d, %d)",
23                 intval(TERM_OBJ_POST),
24                 intval($itemid),
25                 intval(TERM_HASHTAG),
26                 intval(TERM_MENTION));
27
28         if ($message["deleted"])
29                 return;
30
31         $taglist = explode(",", $message["tag"]);
32
33         $tags = "";
34         foreach ($taglist as $tag)
35                 if ((substr(trim($tag), 0, 1) == "#") || (substr(trim($tag), 0, 1) == "@"))
36                         $tags .= " ".trim($tag);
37                 else
38                         $tags .= " #".trim($tag);
39
40         $data = " ".$message["title"]." ".$message["body"]." ".$tags." ";
41
42         // ignore anything in a code block
43         $data = preg_replace('/\[code\](.*?)\[\/code\]/sm','',$data);
44
45         $tags = array();
46
47         $pattern = "/\W\#([^\[].*?)[\s'\".,:;\?!\[\]\/]/ism";
48         if (preg_match_all($pattern, $data, $matches))
49                 foreach ($matches[1] as $match)
50                         $tags["#".strtolower($match)] = "";
51
52         $pattern = "/\W([\#@])\[url\=(.*?)\](.*?)\[\/url\]/ism";
53         if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
54                 foreach ($matches as $match)
55                         $tags[$match[1].strtolower(trim($match[3], ',.:;[]/\"?!'))] = $match[2];
56         }
57
58         foreach ($tags as $tag=>$link) {
59
60                 if (substr(trim($tag), 0, 1) == "#") {
61                         // try to ignore #039 or #1 or anything like that
62                         if (ctype_digit(substr(trim($tag),1)))
63                                 continue;
64                         // try to ignore html hex escapes, e.g. #x2317
65                         if ((substr(trim($tag),1,1) == 'x' || substr(trim($tag),1,1) == 'X') && ctype_digit(substr(trim($tag),2)))
66                                 continue;
67                         $type = TERM_HASHTAG;
68                         $term = substr($tag, 1);
69                 } elseif (substr(trim($tag), 0, 1) == "@") {
70                         $type = TERM_MENTION;
71                         $term = substr($tag, 1);
72                 } else { // This shouldn't happen
73                         $type = TERM_HASHTAG;
74                         $term = $tag;
75                 }
76
77                 if ($message["uid"] == 0) {
78                         $global = true;
79
80                         q("UPDATE `term` SET `global` = 1 WHERE `otype` = %d AND `guid` = '%s'",
81                                 intval(TERM_OBJ_POST), dbesc($message["guid"]));
82                 } else {
83                         $isglobal = q("SELECT `global` FROM `term` WHERE `uid` = 0 AND `otype` = %d AND `guid` = '%s'",
84                                 intval(TERM_OBJ_POST), dbesc($message["guid"]));
85
86                         $global = (count($isglobal) > 0);
87                 }
88
89                 $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`, `guid`, `created`, `received`, `global`)
90                                 VALUES (%d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s', %d)",
91                         intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval($type), dbesc($term),
92                         dbesc($link), dbesc($message["guid"]), dbesc($message["created"]), dbesc($message["received"]), intval($global));
93
94                 // Search for mentions
95                 if ((substr($tag, 0, 1) == '@') && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) {
96                         $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
97                         foreach ($users AS $user) {
98                                 if ($user["uid"] == $message["uid"]) {
99                                         q("UPDATE `item` SET `mention` = 1 WHERE `id` = %d", intval($itemid));
100
101                                         q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", intval($message["parent"]));
102                                 }
103                         }
104                 }
105         }
106 }
107
108 function create_tags_from_itemuri($itemuri, $uid) {
109         $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
110
111         if (count($messages)) {
112                 foreach ($messages as $message) {
113                         create_tags_from_item($message["id"]);
114                 }
115         }
116 }
117
118 function update_items() {
119
120         $messages = dba::p("SELECT `oid`,`item`.`guid`, `item`.`created`, `item`.`received` FROM `term` INNER JOIN `item` ON `item`.`id`=`term`.`oid` WHERE `term`.`otype` = 1 AND `term`.`guid` = ''");
121
122         logger("fetched messages: ".dba::num_rows($messages));
123         while ($message = dba::fetch($messages)) {
124
125                 if ($message["uid"] == 0) {
126                         $global = true;
127
128                         q("UPDATE `term` SET `global` = 1 WHERE `otype` = %d AND `guid` = '%s'",
129                                 intval(TERM_OBJ_POST), dbesc($message["guid"]));
130                 } else {
131                         $isglobal = q("SELECT `global` FROM `term` WHERE `uid` = 0 AND `otype` = %d AND `guid` = '%s'",
132                                 intval(TERM_OBJ_POST), dbesc($message["guid"]));
133
134                         $global = (count($isglobal) > 0);
135                 }
136
137                 q("UPDATE `term` SET `guid` = '%s', `created` = '%s', `received` = '%s', `global` = %d WHERE `otype` = %d AND `oid` = %d",
138                         dbesc($message["guid"]), dbesc($message["created"]), dbesc($message["received"]),
139                         intval($global), intval(TERM_OBJ_POST), intval($message["oid"]));
140         }
141
142         dba::close($messages);
143
144         $messages = dba::p("SELECT `guid` FROM `item` WHERE `uid` = 0");
145
146         logger("fetched messages: ".dba::num_rows($messages));
147         while ($message = dba::fetch(messages)) {
148                 q("UPDATE `item` SET `global` = 1 WHERE `guid` = '%s'", dbesc($message["guid"]));
149         }
150
151         dba::close($messages);
152 }
153
154 function tagadelic($uid, $count = 0, $owner = 0, $flags = 0, $type = TERM_HASHTAG) {
155         require_once('include/security.php');
156
157         $item_condition = item_condition();
158         $sql_options = item_permissions_sql($uid);
159         $count = intval($count);
160         if ($flags) {
161                 if ($flags === 'wall') {
162                         $sql_options .= " AND `item`.`wall` ";
163                 }
164         }
165
166         if ($owner) {
167                 $sql_options .= " AND `item`.`owner-id` = ".intval($owner)." ";
168         }
169
170         // Fetch tags
171         $r = q("SELECT `term`, COUNT(`term`) AS `total` FROM `term`
172                 LEFT JOIN `item` ON `term`.`oid` = `item`.`id`
173                 WHERE `term`.`uid` = %d AND `term`.`type` = %d
174                 AND `term`.`otype` = %d AND `item`.`private` = 0
175                 $sql_options AND $item_condition
176                 GROUP BY `term` ORDER BY `total` DESC %s",
177                 intval($uid),
178                 intval($type),
179                 intval(TERM_OBJ_POST),
180                 ((intval($count)) ? "LIMIT $count" : '')
181         );
182         if(!DBM::is_result($r)) {
183                 return array();
184         }
185                 
186         return tag_calc($r);
187 }
188
189 function wtagblock($uid, $count = 0,$owner = 0, $flags = 0, $type = TERM_HASHTAG) {
190         $o = '';
191         $r = tagadelic($uid, $count, $owner, $flags, $type);
192         if($r) {
193                 foreach ($r as $rr) {
194                         $tag['level'] = $rr[2];
195                         $tag['url'] = urlencode($rr[0]);
196                         $tag['name'] = $rr[0];
197
198                         $tags[] = $tag;
199                 }
200
201                 $tpl = get_markup_template("tagblock_widget.tpl");
202                 $o = replace_macros($tpl, array(
203                         '$title' => t('Tags'),
204                         '$tags'  => $tags
205                 ));
206
207         }
208         return $o;
209 }
210
211 function tag_calc($arr) {
212         $tags = array();
213         $min = 1e9;
214         $max = -1e9;
215         $x = 0;
216
217         if (!$arr) {
218                 return array();
219         }
220
221         foreach ($arr as $rr) {
222                 $tags[$x][0] = $rr['term'];
223                 $tags[$x][1] = log($rr['total']);
224                 $tags[$x][2] = 0;
225                 $min = min($min, $tags[$x][1]);
226                 $max = max($max, $tags[$x][1]);
227                 $x ++;
228         }
229
230         usort($tags, 'tags_sort');
231         $range = max(.01, $max - $min) * 1.0001;
232
233         for ($x = 0; $x < count($tags); $x ++) {
234                 $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
235         }
236
237         return $tags;
238 }
239
240 function tags_sort($a,$b) {
241         if (strtolower($a[0]) == strtolower($b[0])) {
242                 return 0;
243         }
244         return((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);
245 }
246
247
248 function tagcloud_wall_widget($arr = array()) {
249         $a = get_app();
250
251         if(!$a->profile['profile_uid'] || !$a->profile['url']) {
252                 return "";
253         }
254
255         $limit = ((array_key_exists('limit', $arr)) ? intval($arr['limit']) : 50);
256
257         if(feature_enabled($a->profile['profile_uid'], 'tagadelic')) {
258                 $owner_id = Contact::getIdForURL($a->profile['url']);
259
260                 if(!$owner_id) {
261                         return "";
262                 }
263                 return wtagblock($a->profile['profile_uid'], $limit, $owner_id, 'wall');
264         }
265
266         return "";
267 }