3 * @file include/tags.php
6 use Friendica\Content\Feature;
7 use Friendica\Core\L10n;
8 use Friendica\Core\System;
9 use Friendica\Database\DBM;
10 use Friendica\Model\Contact;
12 function create_tags_from_item($itemid)
14 $profile_base = System::baseUrl();
15 $profile_data = parse_url($profile_base);
16 $profile_path = defaults($profile_data, 'path', '');
17 $profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
18 $profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
20 $messages = q("SELECT `guid`, `uid`, `id`, `edited`, `deleted`, `created`, `received`, `title`, `body`, `tag`, `parent` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
25 $message = $messages[0];
28 q("DELETE FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` IN (%d, %d)",
29 intval(TERM_OBJ_POST),
32 intval(TERM_MENTION));
34 if ($message['deleted']) {
38 $taglist = explode(',', $message['tag']);
41 foreach ($taglist as $tag) {
42 if ((substr(trim($tag), 0, 1) == '#') || (substr(trim($tag), 0, 1) == '@')) {
43 $tags .= ' ' . trim($tag);
45 $tags .= ' #' . trim($tag);
49 $data = ' ' . $message['title'] . ' ' . $message['body'] . ' ' . $tags . ' ';
51 // ignore anything in a code block
52 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
56 $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
57 if (preg_match_all($pattern, $data, $matches)) {
58 foreach ($matches[1] as $match) {
59 $tags['#' . strtolower($match)] = '';
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];
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)))
75 // try to ignore html hex escapes, e.g. #x2317
76 if ((substr(trim($tag), 1, 1) == 'x' || substr(trim($tag), 1, 1) == 'X') && ctype_digit(substr(trim($tag), 2)))
79 $term = substr($tag, 1);
80 } elseif (substr(trim($tag), 0, 1) == '@') {
82 $term = substr($tag, 1);
83 } else { // This shouldn't happen
88 if ($message['uid'] == 0) {
91 q("UPDATE `term` SET `global` = 1 WHERE `otype` = %d AND `guid` = '%s'",
92 intval(TERM_OBJ_POST), dbesc($message['guid']));
94 $isglobal = q("SELECT `global` FROM `term` WHERE `uid` = 0 AND `otype` = %d AND `guid` = '%s'",
95 intval(TERM_OBJ_POST), dbesc($message['guid']));
97 $global = (count($isglobal) > 0);
100 q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`, `guid`, `created`, `received`, `global`)
101 VALUES (%d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s', %d)",
102 intval($message['uid']), intval($itemid), intval(TERM_OBJ_POST), intval($type), dbesc($term),
103 dbesc($link), dbesc($message['guid']), dbesc($message['created']), dbesc($message['received']), intval($global));
105 // Search for mentions
106 if ((substr($tag, 0, 1) == '@') && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) {
107 $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
108 foreach ($users AS $user) {
109 if ($user['uid'] == $message['uid']) {
110 q("UPDATE `item` SET `mention` = 1 WHERE `id` = %d", intval($itemid));
112 q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", intval($message['parent']));
119 function create_tags_from_itemuri($itemuri, $uid)
121 $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
123 if (count($messages)) {
124 foreach ($messages as $message) {
125 create_tags_from_item($message['id']);
130 function update_items()
132 $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` = ''");
134 logger('fetched messages: ' . dba::num_rows($messages));
135 while ($message = dba::fetch($messages)) {
136 if ($message['uid'] == 0) {
139 q("UPDATE `term` SET `global` = 1 WHERE `otype` = %d AND `guid` = '%s'",
140 intval(TERM_OBJ_POST), dbesc($message['guid']));
142 $isglobal = q("SELECT `global` FROM `term` WHERE `uid` = 0 AND `otype` = %d AND `guid` = '%s'",
143 intval(TERM_OBJ_POST), dbesc($message['guid']));
145 $global = (count($isglobal) > 0);
148 q("UPDATE `term` SET `guid` = '%s', `created` = '%s', `received` = '%s', `global` = %d WHERE `otype` = %d AND `oid` = %d",
149 dbesc($message['guid']), dbesc($message['created']), dbesc($message['received']),
150 intval($global), intval(TERM_OBJ_POST), intval($message['oid']));
153 dba::close($messages);
155 $messages = dba::p("SELECT `guid` FROM `item` WHERE `uid` = 0");
157 logger('fetched messages: ' . dba::num_rows($messages));
158 while ($message = dba::fetch(messages)) {
159 q("UPDATE `item` SET `global` = 1 WHERE `guid` = '%s'", dbesc($message['guid']));
162 dba::close($messages);
166 * @brief Get alphabetical sorted array of used tags/terms of an user including
167 * a weighting by frequency of use.
169 * @param int $uid The user ID.
170 * @param int $count Max number of displayed tags/terms.
171 * @param int $owner_id The contact id of the owner of the tagged items.
172 * @param string $flags Special item flags.
173 * @param int $type The tag/term type.
175 * @return arr Alphabetical sorted array of used tags of an user.
177 function tagadelic($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
179 require_once 'include/security.php';
181 $item_condition = item_condition();
182 $sql_options = item_permissions_sql($uid);
183 $limit = $count ? sprintf('LIMIT %d', intval($count)) : '';
186 if ($flags === 'wall') {
187 $sql_options .= ' AND `item`.`wall` ';
192 $sql_options .= ' AND `item`.`owner-id` = ' . intval($owner_id) . ' ';
196 $r = dba::p("SELECT `term`, COUNT(`term`) AS `total` FROM `term`
197 LEFT JOIN `item` ON `term`.`oid` = `item`.`id`
198 WHERE `term`.`uid` = ? AND `term`.`type` = ?
199 AND `term`.`otype` = ?
200 AND $item_condition $sql_options
201 GROUP BY `term` ORDER BY `total` DESC $limit",
206 if (!DBM::is_result($r)) {
214 * @brief Construct a tag/term cloud block for an user.
216 * @param int $uid The user ID.
217 * @param int $count Max number of displayed tags/terms.
218 * @param int $owner_id The contact ID of the owner of the tagged items.
219 * @param string $flags Special item flags.
220 * @param int $type The tag/term type.
222 * @return string HTML formatted output.
224 function wtagblock($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
227 $r = tagadelic($uid, $count, $owner_id, $flags, $type);
229 $contact = dba::selectFirst('contact', ['url'], ['id' => $uid]);
230 $url = System::removedBaseUrl($contact['url']);
232 foreach ($r as $rr) {
233 $tag['level'] = $rr[2];
234 $tag['url'] = $url . '?tag=' . urlencode($rr[0]);
235 $tag['name'] = $rr[0];
240 $tpl = get_markup_template('tagblock_widget.tpl');
241 $o = replace_macros($tpl, [
242 '$title' => L10n::t('Tags'),
250 * @brief Calculate weighting of tags according to the frequency of use.
252 * @param array $arr Array of tags/terms with tag/term name and total count of use.
253 * @return array Alphabetical sorted array of used tags/terms of an user.
255 function tag_calc($arr)
266 foreach ($arr as $rr) {
267 $tags[$x][0] = $rr['term'];
268 $tags[$x][1] = log($rr['total']);
270 $min = min($min, $tags[$x][1]);
271 $max = max($max, $tags[$x][1]);
275 usort($tags, 'tags_sort');
276 $range = max(.01, $max - $min) * 1.0001;
278 for ($x = 0; $x < count($tags); $x ++) {
279 $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
286 * @brief Compare function to sort tags/terms alphabetically.
293 function tags_sort($a, $b)
295 if (strtolower($a[0]) == strtolower($b[0])) {
298 return ((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);
302 * @brief Insert a tag cloud widget for the present profile.
304 * @param int $limit Max number of displayed tags.
305 * @return string HTML formattat output.
307 function tagcloud_wall_widget($limit = 50)
311 if (!$a->profile['profile_uid'] || !$a->profile['url']) {
315 if (Feature::isEnabled($a->profile['profile_uid'], 'tagadelic')) {
316 $owner_id = Contact::getIdForURL($a->profile['url']);
321 return wtagblock($a->profile['profile_uid'], $limit, $owner_id, 'wall');