3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model;
24 use Friendica\Core\Cache\Duration;
25 use Friendica\Core\Logger;
26 use Friendica\Database\DBA;
28 use Friendica\Util\Strings;
33 * This Model class handles term table interactions.
34 * This tables stores relevant terms related to posts, photos and searches, like hashtags, mentions and
35 * user-applied categories.
45 const SAVEDSEARCH = 6;
46 const CONVERSATION = 7;
48 * An implicit mention is a mention in a comment body that is redundant with the threading information.
50 const IMPLICIT_MENTION = 8;
52 * An exclusive mention transfers the ownership of the post to the target account, usually a forum.
54 const EXCLUSIVE_MENTION = 9;
56 const TAG_CHARACTER = [
59 self::IMPLICIT_MENTION => '%',
60 self::EXCLUSIVE_MENTION => '!',
63 const OBJECT_TYPE_POST = 1;
64 const OBJECT_TYPE_PHOTO = 2;
67 * Returns a list of the most frequent global hashtags over the given period
69 * @param int $period Period in hours to consider posts
73 public static function getGlobalTrendingHashtags(int $period, $limit = 10)
75 $tags = DI::cache()->get('global_trending_tags');
78 $tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
80 JOIN `item` i ON i.`id` = t.`oid` AND i.`uid` = t.`uid`
81 JOIN `thread` ON `thread`.`iid` = i.`id`
82 WHERE `thread`.`visible`
83 AND NOT `thread`.`deleted`
84 AND NOT `thread`.`moderated`
85 AND NOT `thread`.`private`
90 AND i.`received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
94 Term::OBJECT_TYPE_POST,
100 if (DBA::isResult($tagsStmt)) {
101 $tags = DBA::toArray($tagsStmt);
102 DI::cache()->set('global_trending_tags', $tags, Duration::HOUR);
110 * Returns a list of the most frequent local hashtags over the given period
112 * @param int $period Period in hours to consider posts
116 public static function getLocalTrendingHashtags(int $period, $limit = 10)
118 $tags = DI::cache()->get('local_trending_tags');
121 $tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
123 JOIN `item` i ON i.`id` = t.`oid` AND i.`uid` = t.`uid`
124 JOIN `thread` ON `thread`.`iid` = i.`id`
125 JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
126 WHERE `thread`.`visible`
127 AND NOT `thread`.`deleted`
128 AND NOT `thread`.`moderated`
129 AND NOT `thread`.`private`
131 AND `thread`.`origin`
135 AND i.`received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
137 ORDER BY `score` DESC
139 Term::OBJECT_TYPE_POST,
145 if (DBA::isResult($tagsStmt)) {
146 $tags = DBA::toArray($tagsStmt);
147 DI::cache()->set('local_trending_tags', $tags, Duration::HOUR);
155 * Generates the legacy item.tag field comma-separated BBCode string from an item ID.
156 * Includes only hashtags, implicit and explicit mentions.
158 * @param int $item_id
162 public static function tagTextFromItemId($item_id)
165 $tags = self::tagArrayFromItemId($item_id, [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION]);
166 foreach ($tags as $tag) {
167 $tag_list[] = self::TAG_CHARACTER[$tag['type']] . '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]';
170 return implode(',', $tag_list);
174 * Retrieves the terms from the provided type(s) associated with the provided item ID.
176 * @param int $item_id
177 * @param int|array $type
181 public static function tagArrayFromItemId($item_id, $type = [self::HASHTAG, self::MENTION])
183 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => $type];
184 $tags = DBA::select('term', ['type', 'term', 'url'], $condition);
185 if (!DBA::isResult($tags)) {
189 return DBA::toArray($tags);
193 * Generates the legacy item.file field string from an item ID.
194 * Includes only file and category terms.
196 * @param int $item_id
200 public static function fileTextFromItemId($item_id)
203 $tags = self::tagArrayFromItemId($item_id, [self::FILE, self::CATEGORY]);
204 foreach ($tags as $tag) {
205 if ($tag['type'] == self::CATEGORY) {
206 $file_text .= '<' . $tag['term'] . '>';
208 $file_text .= '[' . $tag['term'] . ']';
216 * Inserts new terms for the provided item ID based on the legacy item.tag field BBCode content.
217 * Deletes all previous tag terms for the same item ID.
218 * Sets both the item.mention and thread.mentions field flags if a mention concerning the item UID is found.
220 * @param int $item_id
221 * @param string $tag_str
222 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
224 public static function insertFromTagFieldByItemId($item_id, $tag_str)
226 $profile_base = DI::baseUrl();
227 $profile_data = parse_url($profile_base);
228 $profile_path = $profile_data['path'] ?? '';
229 $profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
230 $profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
232 $fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'parent'];
233 $item = Item::selectFirst($fields, ['id' => $item_id]);
234 if (!DBA::isResult($item)) {
238 $item['tag'] = $tag_str;
241 self::deleteByItemId($item_id);
243 if ($item['deleted']) {
247 $taglist = explode(',', $item['tag']);
250 foreach ($taglist as $tag) {
251 if (Strings::startsWith($tag, self::TAG_CHARACTER)) {
252 $tags_string .= ' ' . trim($tag);
254 $tags_string .= ' #' . trim($tag);
258 $data = ' ' . $item['title'] . ' ' . $item['body'] . ' ' . $tags_string . ' ';
260 // ignore anything in a code block
261 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
265 $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
266 if (preg_match_all($pattern, $data, $matches)) {
267 foreach ($matches[1] as $match) {
268 $tags['#' . $match] = '';
272 $pattern = '/\W([\#@!%])\[url\=(.*?)\](.*?)\[\/url\]/ism';
273 if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
274 foreach ($matches as $match) {
276 if (in_array($match[1], [
277 self::TAG_CHARACTER[self::MENTION],
278 self::TAG_CHARACTER[self::IMPLICIT_MENTION],
279 self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]
281 $contact = Contact::getDetailsByURL($match[2], 0);
282 if (!empty($contact['addr'])) {
283 $match[3] = $contact['addr'];
286 if (!empty($contact['url'])) {
287 $match[2] = $contact['url'];
291 $tags[$match[2]] = $match[1] . trim($match[3], ',.:;[]/\"?!');
295 foreach ($tags as $link => $tag) {
296 if (self::isType($tag, self::HASHTAG)) {
297 // try to ignore #039 or #1 or anything like that
298 if (ctype_digit(substr(trim($tag), 1))) {
302 // try to ignore html hex escapes, e.g. #x2317
303 if ((substr(trim($tag), 1, 1) == 'x' || substr(trim($tag), 1, 1) == 'X') && ctype_digit(substr(trim($tag), 2))) {
307 $type = self::HASHTAG;
308 $term = substr($tag, 1);
310 } elseif (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION)) {
311 if (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION)) {
312 $type = self::MENTION;
314 $type = self::IMPLICIT_MENTION;
317 $contact = Contact::getDetailsByURL($link, 0);
318 if (!empty($contact['name'])) {
319 $term = $contact['name'];
321 $term = substr($tag, 1);
323 } else { // This shouldn't happen
324 $type = self::HASHTAG;
328 Logger::notice('Unknown term type', ['tag' => $tag]);
331 if (DBA::exists('term', ['uid' => $item['uid'], 'otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'term' => $term, 'type' => $type])) {
335 if ($item['uid'] == 0) {
337 DBA::update('term', ['global' => true], ['otype' => self::OBJECT_TYPE_POST, 'guid' => $item['guid']]);
339 $global = DBA::exists('term', ['uid' => 0, 'otype' => self::OBJECT_TYPE_POST, 'guid' => $item['guid']]);
342 DBA::insert('term', [
343 'uid' => $item['uid'],
345 'otype' => self::OBJECT_TYPE_POST,
349 'guid' => $item['guid'],
350 'created' => $item['created'],
351 'received' => $item['received'],
355 // Search for mentions
356 if (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION)
358 strpos($link, $profile_base_friendica) !== false
359 || strpos($link, $profile_base_diaspora) !== false
362 $users_stmt = DBA::p("SELECT `uid` FROM `contact` WHERE self AND (`url` = ? OR `nurl` = ?)", $link, $link);
363 $users = DBA::toArray($users_stmt);
364 foreach ($users AS $user) {
365 if ($user['uid'] == $item['uid']) {
366 /// @todo This function is called from Item::update - so we mustn't call that function here
367 DBA::update('item', ['mention' => true], ['id' => $item_id]);
368 DBA::update('thread', ['mention' => true], ['iid' => $item['parent']]);
376 * Inserts new terms for the provided item ID based on the legacy item.file field BBCode content.
377 * Deletes all previous file terms for the same item ID.
379 * @param integer $item_id item id
384 public static function insertFromFileFieldByItemId($item_id, $files)
386 $message = Item::selectFirst(['uid', 'deleted'], ['id' => $item_id]);
387 if (!DBA::isResult($message)) {
392 DBA::delete('term', ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [self::FILE, self::CATEGORY]]);
394 if ($message["deleted"]) {
398 $message['file'] = $files;
400 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
401 foreach ($files[1] as $file) {
402 DBA::insert('term', [
403 'uid' => $message["uid"],
405 'otype' => self::OBJECT_TYPE_POST,
406 'type' => self::FILE,
412 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
413 foreach ($files[1] as $file) {
414 DBA::insert('term', [
415 'uid' => $message["uid"],
417 'otype' => self::OBJECT_TYPE_POST,
418 'type' => self::CATEGORY,
426 * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
427 * provided item's body with them.
431 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
432 * @throws \ImagickException
434 public static function populateTagsFromItem(&$item)
440 'implicit_mentions' => [],
443 $searchpath = DI::baseUrl() . "/search?tag=";
445 $taglist = DBA::select(
447 ['type', 'term', 'url'],
448 ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item['id'], 'type' => [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION]],
451 while ($tag = DBA::fetch($taglist)) {
452 if ($tag['url'] == '') {
453 $tag['url'] = $searchpath . rawurlencode($tag['term']);
456 $orig_tag = $tag['url'];
458 $prefix = self::TAG_CHARACTER[$tag['type']];
459 switch($tag['type']) {
461 if ($orig_tag != $tag['url']) {
462 $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
465 $return['hashtags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . htmlspecialchars($tag['term']) . '</a>';
466 $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . htmlspecialchars($tag['term']) . '</a>';
469 $tag['url'] = Contact::magicLink($tag['url']);
470 $return['mentions'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . htmlspecialchars($tag['term']) . '</a>';
471 $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . htmlspecialchars($tag['term']) . '</a>';
473 case self::IMPLICIT_MENTION:
474 $return['implicit_mentions'][] = $prefix . $tag['term'];
478 DBA::close($taglist);
484 * Delete tags of the specific type(s) from an item
486 * @param int $item_id
487 * @param int|array $type
490 public static function deleteByItemId($item_id, $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION])
492 if (empty($item_id)) {
497 DBA::delete('term', ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => $type]);
501 * Check if the provided tag is of one of the provided term types.
504 * @param int ...$types
507 public static function isType($tag, ...$types)
510 foreach ($types as $type) {
511 if (array_key_exists($type, self::TAG_CHARACTER)) {
512 $tag_chars[] = self::TAG_CHARACTER[$type];
516 return Strings::startsWith($tag, $tag_chars);