4 use Friendica\Core\System;
5 use Friendica\Database\DBM;
6 use Friendica\Object\Contact;
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/";
14 $messages = q("SELECT `guid`, `uid`, `id`, `edited`, `deleted`, `created`, `received`, `title`, `body`, `tag`, `parent` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
19 $message = $messages[0];
22 q("DELETE FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` IN (%d, %d)",
23 intval(TERM_OBJ_POST),
26 intval(TERM_MENTION));
28 if ($message["deleted"])
31 $taglist = explode(",", $message["tag"]);
34 foreach ($taglist as $tag)
35 if ((substr(trim($tag), 0, 1) == "#") || (substr(trim($tag), 0, 1) == "@"))
36 $tags .= " ".trim($tag);
38 $tags .= " #".trim($tag);
40 $data = " ".$message["title"]." ".$message["body"]." ".$tags." ";
42 // ignore anything in a code block
43 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm','',$data);
47 $pattern = "/\W\#([^\[].*?)[\s'\".,:;\?!\[\]\/]/ism";
48 if (preg_match_all($pattern, $data, $matches))
49 foreach ($matches[1] as $match)
50 $tags["#".strtolower($match)] = "";
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];
58 foreach ($tags as $tag=>$link) {
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)))
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)))
68 $term = substr($tag, 1);
69 } elseif (substr(trim($tag), 0, 1) == "@") {
71 $term = substr($tag, 1);
72 } else { // This shouldn't happen
77 if ($message["uid"] == 0) {
80 q("UPDATE `term` SET `global` = 1 WHERE `otype` = %d AND `guid` = '%s'",
81 intval(TERM_OBJ_POST), dbesc($message["guid"]));
83 $isglobal = q("SELECT `global` FROM `term` WHERE `uid` = 0 AND `otype` = %d AND `guid` = '%s'",
84 intval(TERM_OBJ_POST), dbesc($message["guid"]));
86 $global = (count($isglobal) > 0);
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));
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));
101 q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", intval($message["parent"]));
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));
111 if (count($messages)) {
112 foreach ($messages as $message) {
113 create_tags_from_item($message["id"]);
118 function update_items() {
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` = ''");
122 logger("fetched messages: ".dba::num_rows($messages));
123 while ($message = dba::fetch($messages)) {
125 if ($message["uid"] == 0) {
128 q("UPDATE `term` SET `global` = 1 WHERE `otype` = %d AND `guid` = '%s'",
129 intval(TERM_OBJ_POST), dbesc($message["guid"]));
131 $isglobal = q("SELECT `global` FROM `term` WHERE `uid` = 0 AND `otype` = %d AND `guid` = '%s'",
132 intval(TERM_OBJ_POST), dbesc($message["guid"]));
134 $global = (count($isglobal) > 0);
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"]));
142 dba::close($messages);
144 $messages = dba::p("SELECT `guid` FROM `item` WHERE `uid` = 0");
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"]));
151 dba::close($messages);
154 function tagadelic($uid, $count = 0, $owner = 0, $flags = 0, $type = TERM_HASHTAG) {
155 require_once('include/security.php');
157 $item_condition = item_condition();
158 $sql_options = item_permissions_sql($uid);
159 $count = intval($count);
161 if ($flags === 'wall') {
162 $sql_options .= " AND `item`.`wall` ";
167 $sql_options .= " AND `item`.`owner-id` = ".intval($owner)." ";
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",
179 intval(TERM_OBJ_POST),
180 ((intval($count)) ? "LIMIT $count" : '')
182 if(!DBM::is_result($r)) {
189 function wtagblock($uid, $count = 0,$owner = 0, $flags = 0, $type = TERM_HASHTAG) {
191 $r = tagadelic($uid, $count, $owner, $flags, $type);
193 foreach ($r as $rr) {
194 $tag['level'] = $rr[2];
195 $tag['url'] = urlencode($rr[0]);
196 $tag['name'] = $rr[0];
201 $tpl = get_markup_template("tagblock_widget.tpl");
202 $o = replace_macros($tpl, array(
203 '$title' => t('Tags'),
211 function tag_calc($arr) {
221 foreach ($arr as $rr) {
222 $tags[$x][0] = $rr['term'];
223 $tags[$x][1] = log($rr['total']);
225 $min = min($min, $tags[$x][1]);
226 $max = max($max, $tags[$x][1]);
230 usort($tags, 'tags_sort');
231 $range = max(.01, $max - $min) * 1.0001;
233 for ($x = 0; $x < count($tags); $x ++) {
234 $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
240 function tags_sort($a,$b) {
241 if (strtolower($a[0]) == strtolower($b[0])) {
244 return((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);
248 function tagcloud_wall_widget($arr = array()) {
251 if(!$a->profile['profile_uid'] || !$a->profile['url']) {
255 $limit = ((array_key_exists('limit', $arr)) ? intval($arr['limit']) : 50);
257 if(feature_enabled($a->profile['profile_uid'], 'tagadelic')) {
258 $owner_id = Contact::getIdForURL($a->profile['url']);
263 return wtagblock($a->profile['profile_uid'], $limit, $owner_id, 'wall');