3 * @file include/tags.php
6 use Friendica\Content\Feature;
7 use Friendica\Core\System;
8 use Friendica\Database\DBM;
9 use Friendica\Model\Contact;
11 function create_tags_from_item($itemid) {
12 $profile_base = System::baseUrl();
13 $profile_data = parse_url($profile_base);
14 $profile_base_friendica = $profile_data['host'].$profile_data['path']."/profile/";
15 $profile_base_diaspora = $profile_data['host'].$profile_data['path']."/u/";
17 $messages = q("SELECT `guid`, `uid`, `id`, `edited`, `deleted`, `created`, `received`, `title`, `body`, `tag`, `parent` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
22 $message = $messages[0];
25 q("DELETE FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` IN (%d, %d)",
26 intval(TERM_OBJ_POST),
29 intval(TERM_MENTION));
31 if ($message["deleted"])
34 $taglist = explode(",", $message["tag"]);
37 foreach ($taglist as $tag)
38 if ((substr(trim($tag), 0, 1) == "#") || (substr(trim($tag), 0, 1) == "@"))
39 $tags .= " ".trim($tag);
41 $tags .= " #".trim($tag);
43 $data = " ".$message["title"]." ".$message["body"]." ".$tags." ";
45 // ignore anything in a code block
46 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm','',$data);
50 $pattern = "/\W\#([^\[].*?)[\s'\".,:;\?!\[\]\/]/ism";
51 if (preg_match_all($pattern, $data, $matches))
52 foreach ($matches[1] as $match)
53 $tags["#".strtolower($match)] = "";
55 $pattern = "/\W([\#@])\[url\=(.*?)\](.*?)\[\/url\]/ism";
56 if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
57 foreach ($matches as $match)
58 $tags[$match[1].strtolower(trim($match[3], ',.:;[]/\"?!'))] = $match[2];
61 foreach ($tags as $tag=>$link) {
63 if (substr(trim($tag), 0, 1) == "#") {
64 // try to ignore #039 or #1 or anything like that
65 if (ctype_digit(substr(trim($tag),1)))
67 // try to ignore html hex escapes, e.g. #x2317
68 if ((substr(trim($tag),1,1) == 'x' || substr(trim($tag),1,1) == 'X') && ctype_digit(substr(trim($tag),2)))
71 $term = substr($tag, 1);
72 } elseif (substr(trim($tag), 0, 1) == "@") {
74 $term = substr($tag, 1);
75 } else { // This shouldn't happen
80 if ($message["uid"] == 0) {
83 q("UPDATE `term` SET `global` = 1 WHERE `otype` = %d AND `guid` = '%s'",
84 intval(TERM_OBJ_POST), dbesc($message["guid"]));
86 $isglobal = q("SELECT `global` FROM `term` WHERE `uid` = 0 AND `otype` = %d AND `guid` = '%s'",
87 intval(TERM_OBJ_POST), dbesc($message["guid"]));
89 $global = (count($isglobal) > 0);
92 $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`, `guid`, `created`, `received`, `global`)
93 VALUES (%d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s', %d)",
94 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval($type), dbesc($term),
95 dbesc($link), dbesc($message["guid"]), dbesc($message["created"]), dbesc($message["received"]), intval($global));
97 // Search for mentions
98 if ((substr($tag, 0, 1) == '@') && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) {
99 $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
100 foreach ($users AS $user) {
101 if ($user["uid"] == $message["uid"]) {
102 q("UPDATE `item` SET `mention` = 1 WHERE `id` = %d", intval($itemid));
104 q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", intval($message["parent"]));
111 function create_tags_from_itemuri($itemuri, $uid) {
112 $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
114 if (count($messages)) {
115 foreach ($messages as $message) {
116 create_tags_from_item($message["id"]);
121 function update_items() {
123 $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` = ''");
125 logger("fetched messages: ".dba::num_rows($messages));
126 while ($message = dba::fetch($messages)) {
128 if ($message["uid"] == 0) {
131 q("UPDATE `term` SET `global` = 1 WHERE `otype` = %d AND `guid` = '%s'",
132 intval(TERM_OBJ_POST), dbesc($message["guid"]));
134 $isglobal = q("SELECT `global` FROM `term` WHERE `uid` = 0 AND `otype` = %d AND `guid` = '%s'",
135 intval(TERM_OBJ_POST), dbesc($message["guid"]));
137 $global = (count($isglobal) > 0);
140 q("UPDATE `term` SET `guid` = '%s', `created` = '%s', `received` = '%s', `global` = %d WHERE `otype` = %d AND `oid` = %d",
141 dbesc($message["guid"]), dbesc($message["created"]), dbesc($message["received"]),
142 intval($global), intval(TERM_OBJ_POST), intval($message["oid"]));
145 dba::close($messages);
147 $messages = dba::p("SELECT `guid` FROM `item` WHERE `uid` = 0");
149 logger("fetched messages: ".dba::num_rows($messages));
150 while ($message = dba::fetch(messages)) {
151 q("UPDATE `item` SET `global` = 1 WHERE `guid` = '%s'", dbesc($message["guid"]));
154 dba::close($messages);
158 * @brief Get alphabetical sorted array of used tags/terms of an user including
159 * a weighting by frequency of use.
161 * @param int $uid The user ID.
162 * @param int $count Max number of displayed tags/terms.
163 * @param int $owner_id The contact id of the owner of the tagged items.
164 * @param string $flags Special item flags.
165 * @param int $type The tag/term type.
167 * @return arr Alphabetical sorted array of used tags of an user.
169 function tagadelic($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG) {
170 require_once('include/security.php');
172 $item_condition = item_condition();
173 $sql_options = item_permissions_sql($uid);
174 $limit = $count ? sprintf("LIMIT %d", intval($count)) : "";
177 if ($flags === 'wall') {
178 $sql_options .= " AND `item`.`wall` ";
183 $sql_options .= " AND `item`.`owner-id` = ".intval($owner_id)." ";
187 $r = dba::p("SELECT `term`, COUNT(`term`) AS `total` FROM `term`
188 LEFT JOIN `item` ON `term`.`oid` = `item`.`id`
189 WHERE `term`.`uid` = ? AND `term`.`type` = ?
190 AND `term`.`otype` = ?
191 AND $item_condition $sql_options
192 GROUP BY `term` ORDER BY `total` DESC $limit",
197 if(!DBM::is_result($r)) {
205 * @brief Construct a tag/term cloud block for an user.
207 * @param int $uid The user ID.
208 * @param int $count Max number of displayed tags/terms.
209 * @param int $owner_id The contact ID of the owner of the tagged items.
210 * @param string $flags Special item flags.
211 * @param int $type The tag/term type.
213 * @return string HTML formatted output.
215 function wtagblock($uid, $count = 0,$owner_id = 0, $flags = '', $type = TERM_HASHTAG) {
217 $r = tagadelic($uid, $count, $owner_id, $flags, $type);
219 $contact = dba::select(
225 $url = System::removedBaseUrl($contact['url']);
227 foreach ($r as $rr) {
228 $tag['level'] = $rr[2];
229 $tag['url'] = $url."?tag=".urlencode($rr[0]);
230 $tag['name'] = $rr[0];
235 $tpl = get_markup_template("tagblock_widget.tpl");
236 $o = replace_macros($tpl, array(
237 '$title' => t('Tags'),
246 * @brief Calculate weighting of tags according to the frequency of use.
248 * @param array $arr Array of tags/terms with tag/term name and total count of use.
249 * @return array Alphabetical sorted array of used tags/terms of an user.
251 function tag_calc($arr) {
261 foreach ($arr as $rr) {
262 $tags[$x][0] = $rr['term'];
263 $tags[$x][1] = log($rr['total']);
265 $min = min($min, $tags[$x][1]);
266 $max = max($max, $tags[$x][1]);
270 usort($tags, 'tags_sort');
271 $range = max(.01, $max - $min) * 1.0001;
273 for ($x = 0; $x < count($tags); $x ++) {
274 $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
281 * @brief Compare function to sort tags/terms alphabetically.
288 function tags_sort($a, $b) {
289 if (strtolower($a[0]) == strtolower($b[0])) {
292 return ((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);
296 * @brief Insert a tag cloud widget for the present profile.
298 * @param int $limit Max number of displayed tags.
299 * @return string HTML formattat output.
301 function tagcloud_wall_widget($limit = 50) {
304 if(!$a->profile['profile_uid'] || !$a->profile['url']) {
308 if(Feature::isEnabled($a->profile['profile_uid'], 'tagadelic')) {
309 $owner_id = Contact::getIdForURL($a->profile['url']);
314 return wtagblock($a->profile['profile_uid'], $limit, $owner_id, 'wall');