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 * An implicit mention is a mention in a comment body that is redundant with the threading information.
47 const IMPLICIT_MENTION = 8;
49 * An exclusive mention transfers the ownership of the post to the target account, usually a forum.
51 const EXCLUSIVE_MENTION = 9;
53 const TAG_CHARACTER = [
56 self::IMPLICIT_MENTION => '%',
57 self::EXCLUSIVE_MENTION => '!',
60 const OBJECT_TYPE_POST = 1;
61 const OBJECT_TYPE_PHOTO = 2;
64 * Returns a list of the most frequent global hashtags over the given period
66 * @param int $period Period in hours to consider posts
70 public static function getGlobalTrendingHashtags(int $period, $limit = 10)
72 $tags = DI::cache()->get('global_trending_tags');
75 $tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
77 JOIN `item` i ON i.`id` = t.`oid` AND i.`uid` = t.`uid`
78 JOIN `thread` ON `thread`.`iid` = i.`id`
79 WHERE `thread`.`visible`
80 AND NOT `thread`.`deleted`
81 AND NOT `thread`.`moderated`
82 AND `thread`.`private` = ?
87 AND i.`received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
92 Term::OBJECT_TYPE_POST,
98 if (DBA::isResult($tagsStmt)) {
99 $tags = DBA::toArray($tagsStmt);
100 DI::cache()->set('global_trending_tags', $tags, Duration::HOUR);
108 * Returns a list of the most frequent local hashtags over the given period
110 * @param int $period Period in hours to consider posts
114 public static function getLocalTrendingHashtags(int $period, $limit = 10)
116 $tags = DI::cache()->get('local_trending_tags');
119 $tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
121 JOIN `item` i ON i.`id` = t.`oid` AND i.`uid` = t.`uid`
122 JOIN `thread` ON `thread`.`iid` = i.`id`
123 WHERE `thread`.`visible`
124 AND NOT `thread`.`deleted`
125 AND NOT `thread`.`moderated`
126 AND `thread`.`private` = ?
128 AND `thread`.`origin`
132 AND i.`received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
134 ORDER BY `score` DESC
137 Term::OBJECT_TYPE_POST,
143 if (DBA::isResult($tagsStmt)) {
144 $tags = DBA::toArray($tagsStmt);
145 DI::cache()->set('local_trending_tags', $tags, Duration::HOUR);
153 * Generates the legacy item.tag field comma-separated BBCode string from an item ID.
154 * Includes only hashtags, implicit and explicit mentions.
156 * @param int $item_id
160 public static function tagTextFromItemId($item_id)
163 $tags = self::tagArrayFromItemId($item_id, [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION]);
164 foreach ($tags as $tag) {
165 $tag_list[] = self::TAG_CHARACTER[$tag['type']] . '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]';
168 return implode(',', $tag_list);
172 * Retrieves the terms from the provided type(s) associated with the provided item ID.
174 * @param int $item_id
175 * @param int|array $type
179 public static function tagArrayFromItemId($item_id, $type = [self::HASHTAG, self::MENTION])
181 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => $type];
182 $tags = DBA::select('term', ['type', 'term', 'url'], $condition);
183 if (!DBA::isResult($tags)) {
187 return DBA::toArray($tags);
191 * Generates the legacy item.file field string from an item ID.
192 * Includes only file and category terms.
194 * @param int $item_id
198 public static function fileTextFromItemId($item_id)
201 $tags = self::tagArrayFromItemId($item_id, [self::FILE, self::CATEGORY]);
202 foreach ($tags as $tag) {
203 if ($tag['type'] == self::CATEGORY) {
204 $file_text .= '<' . $tag['term'] . '>';
206 $file_text .= '[' . $tag['term'] . ']';
214 * Inserts new terms for the provided item ID based on the legacy item.tag field BBCode content.
215 * Deletes all previous tag terms for the same item ID.
216 * Sets both the item.mention and thread.mentions field flags if a mention concerning the item UID is found.
218 * @param int $item_id
219 * @param string $tag_str
220 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
222 public static function insertFromTagFieldByItemId($item_id, $tag_str)
224 $profile_base = DI::baseUrl();
225 $profile_data = parse_url($profile_base);
226 $profile_path = $profile_data['path'] ?? '';
227 $profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
228 $profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
230 $fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'parent'];
231 $item = Item::selectFirst($fields, ['id' => $item_id]);
232 if (!DBA::isResult($item)) {
236 $item['tag'] = $tag_str;
239 self::deleteByItemId($item_id);
241 if ($item['deleted']) {
245 $taglist = explode(',', $item['tag']);
248 foreach ($taglist as $tag) {
249 if (Strings::startsWith($tag, self::TAG_CHARACTER)) {
250 $tags_string .= ' ' . trim($tag);
252 $tags_string .= ' #' . trim($tag);
256 $data = ' ' . $item['title'] . ' ' . $item['body'] . ' ' . $tags_string . ' ';
258 // ignore anything in a code block
259 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
263 $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
264 if (preg_match_all($pattern, $data, $matches)) {
265 foreach ($matches[1] as $match) {
266 $tags['#' . $match] = '';
270 $pattern = '/\W([\#@!%])\[url\=(.*?)\](.*?)\[\/url\]/ism';
271 if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
272 foreach ($matches as $match) {
274 if (in_array($match[1], [
275 self::TAG_CHARACTER[self::MENTION],
276 self::TAG_CHARACTER[self::IMPLICIT_MENTION],
277 self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]
279 $contact = Contact::getDetailsByURL($match[2], 0);
280 if (!empty($contact['addr'])) {
281 $match[3] = $contact['addr'];
284 if (!empty($contact['url'])) {
285 $match[2] = $contact['url'];
289 $tags[$match[2]] = $match[1] . trim($match[3], ',.:;[]/\"?!');
293 foreach ($tags as $link => $tag) {
294 if (self::isType($tag, self::HASHTAG)) {
295 // try to ignore #039 or #1 or anything like that
296 if (ctype_digit(substr(trim($tag), 1))) {
300 // try to ignore html hex escapes, e.g. #x2317
301 if ((substr(trim($tag), 1, 1) == 'x' || substr(trim($tag), 1, 1) == 'X') && ctype_digit(substr(trim($tag), 2))) {
305 $type = self::HASHTAG;
306 $term = substr($tag, 1);
308 } elseif (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION)) {
309 if (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION)) {
310 $type = self::MENTION;
312 $type = self::IMPLICIT_MENTION;
315 $contact = Contact::getDetailsByURL($link, 0);
316 if (!empty($contact['name'])) {
317 $term = $contact['name'];
319 $term = substr($tag, 1);
321 } else { // This shouldn't happen
322 $type = self::HASHTAG;
326 Logger::notice('Unknown term type', ['tag' => $tag]);
329 if (DBA::exists('term', ['uid' => $item['uid'], 'otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'term' => $term, 'type' => $type])) {
337 if ($item['uid'] == 0) {
339 DBA::update('term', ['global' => true], ['otype' => self::OBJECT_TYPE_POST, 'guid' => $item['guid']]);
341 $global = DBA::exists('term', ['uid' => 0, 'otype' => self::OBJECT_TYPE_POST, 'guid' => $item['guid']]);
344 DBA::insert('term', [
345 'uid' => $item['uid'],
347 'otype' => self::OBJECT_TYPE_POST,
349 'term' => substr($term, 0, 255),
351 'guid' => $item['guid'],
352 'created' => $item['created'],
353 'received' => $item['received'],
357 // Search for mentions
358 if (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION)
360 strpos($link, $profile_base_friendica) !== false
361 || strpos($link, $profile_base_diaspora) !== false
364 $users_stmt = DBA::p("SELECT `uid` FROM `contact` WHERE self AND (`url` = ? OR `nurl` = ?)", $link, $link);
365 $users = DBA::toArray($users_stmt);
366 foreach ($users AS $user) {
367 if ($user['uid'] == $item['uid']) {
368 /// @todo This function is called from Item::update - so we mustn't call that function here
369 DBA::update('item', ['mention' => true], ['id' => $item_id]);
370 DBA::update('thread', ['mention' => true], ['iid' => $item['parent']]);
378 * Inserts new terms for the provided item ID based on the legacy item.file field BBCode content.
379 * Deletes all previous file terms for the same item ID.
381 * @param integer $item_id item id
386 public static function insertFromFileFieldByItemId($item_id, $files)
388 $message = Item::selectFirst(['uid', 'deleted'], ['id' => $item_id]);
389 if (!DBA::isResult($message)) {
394 DBA::delete('term', ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [self::FILE, self::CATEGORY]]);
396 if ($message["deleted"]) {
400 $message['file'] = $files;
402 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
403 foreach ($files[1] as $file) {
404 DBA::insert('term', [
405 'uid' => $message["uid"],
407 'otype' => self::OBJECT_TYPE_POST,
408 'type' => self::FILE,
414 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
415 foreach ($files[1] as $file) {
416 DBA::insert('term', [
417 'uid' => $message["uid"],
419 'otype' => self::OBJECT_TYPE_POST,
420 'type' => self::CATEGORY,
428 * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
429 * provided item's body with them.
433 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
434 * @throws \ImagickException
436 public static function populateTagsFromItem(&$item)
442 'implicit_mentions' => [],
445 $searchpath = DI::baseUrl() . "/search?tag=";
447 $taglist = DBA::select(
449 ['type', 'term', 'url'],
450 ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item['id'], 'type' => [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION]],
453 while ($tag = DBA::fetch($taglist)) {
454 if ($tag['url'] == '') {
455 $tag['url'] = $searchpath . rawurlencode($tag['term']);
458 $orig_tag = $tag['url'];
460 $prefix = self::TAG_CHARACTER[$tag['type']];
461 switch($tag['type']) {
463 if ($orig_tag != $tag['url']) {
464 $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
467 $return['hashtags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['term']) . '</a>';
468 $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['term']) . '</a>';
471 $tag['url'] = Contact::magicLink($tag['url']);
472 $return['mentions'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['term']) . '</a>';
473 $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['term']) . '</a>';
475 case self::IMPLICIT_MENTION:
476 $return['implicit_mentions'][] = $prefix . $tag['term'];
480 DBA::close($taglist);
486 * Delete tags of the specific type(s) from an item
488 * @param int $item_id
489 * @param int|array $type
492 public static function deleteByItemId($item_id, $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION])
494 if (empty($item_id)) {
499 DBA::delete('term', ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => $type]);
503 * Check if the provided tag is of one of the provided term types.
506 * @param int ...$types
509 public static function isType($tag, ...$types)
512 foreach ($types as $type) {
513 if (array_key_exists($type, self::TAG_CHARACTER)) {
514 $tag_chars[] = self::TAG_CHARACTER[$type];
518 return Strings::startsWith($tag, $tag_chars);