2018-01-10 17:46:05 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2019-02-23 15:25:37 +01:00
|
|
|
* @file src/Model/Term.php
|
2018-01-10 17:46:05 +01:00
|
|
|
*/
|
|
|
|
namespace Friendica\Model;
|
|
|
|
|
2019-08-06 13:36:51 +02:00
|
|
|
use Friendica\Core\Cache;
|
2019-02-24 19:33:11 +01:00
|
|
|
use Friendica\Core\Logger;
|
2018-07-20 14:19:26 +02:00
|
|
|
use Friendica\Database\DBA;
|
2019-12-30 23:00:08 +01:00
|
|
|
use Friendica\DI;
|
2019-02-23 01:15:35 +01:00
|
|
|
use Friendica\Util\Strings;
|
2018-01-10 18:05:20 +01:00
|
|
|
|
2019-02-23 15:25:37 +01:00
|
|
|
/**
|
|
|
|
* Class Term
|
|
|
|
*
|
|
|
|
* This Model class handles term table interactions.
|
|
|
|
* This tables stores relevant terms related to posts, photos and searches, like hashtags, mentions and
|
|
|
|
* user-applied categories.
|
|
|
|
*
|
|
|
|
* @package Friendica\Model
|
|
|
|
*/
|
2018-01-10 17:46:05 +01:00
|
|
|
class Term
|
|
|
|
{
|
2019-02-23 01:15:35 +01:00
|
|
|
const UNKNOWN = 0;
|
|
|
|
const HASHTAG = 1;
|
|
|
|
const MENTION = 2;
|
|
|
|
const CATEGORY = 3;
|
|
|
|
const PCATEGORY = 4;
|
|
|
|
const FILE = 5;
|
|
|
|
const SAVEDSEARCH = 6;
|
|
|
|
const CONVERSATION = 7;
|
2019-02-23 15:25:37 +01:00
|
|
|
/**
|
|
|
|
* An implicit mention is a mention in a comment body that is redundant with the threading information.
|
|
|
|
*/
|
2019-02-23 01:15:35 +01:00
|
|
|
const IMPLICIT_MENTION = 8;
|
2019-02-23 15:25:37 +01:00
|
|
|
/**
|
|
|
|
* An exclusive mention transfers the ownership of the post to the target account, usually a forum.
|
|
|
|
*/
|
2019-02-23 01:15:35 +01:00
|
|
|
const EXCLUSIVE_MENTION = 9;
|
|
|
|
|
|
|
|
const TAG_CHARACTER = [
|
|
|
|
self::HASHTAG => '#',
|
|
|
|
self::MENTION => '@',
|
|
|
|
self::IMPLICIT_MENTION => '%',
|
|
|
|
self::EXCLUSIVE_MENTION => '!',
|
|
|
|
];
|
|
|
|
|
|
|
|
const OBJECT_TYPE_POST = 1;
|
|
|
|
const OBJECT_TYPE_PHOTO = 2;
|
2018-06-30 15:54:01 +02:00
|
|
|
|
2019-08-06 13:36:51 +02:00
|
|
|
/**
|
|
|
|
* Returns a list of the most frequent global hashtags over the given period
|
|
|
|
*
|
|
|
|
* @param int $period Period in hours to consider posts
|
|
|
|
* @return array
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function getGlobalTrendingHashtags(int $period, $limit = 10)
|
|
|
|
{
|
|
|
|
$tags = Cache::get('global_trending_tags');
|
|
|
|
|
|
|
|
if (!$tags) {
|
|
|
|
$tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
|
|
|
|
FROM `term` t
|
|
|
|
JOIN `item` i ON i.`id` = t.`oid` AND i.`uid` = t.`uid`
|
|
|
|
JOIN `thread` ON `thread`.`iid` = i.`id`
|
|
|
|
WHERE `thread`.`visible`
|
|
|
|
AND NOT `thread`.`deleted`
|
|
|
|
AND NOT `thread`.`moderated`
|
|
|
|
AND NOT `thread`.`private`
|
|
|
|
AND t.`uid` = 0
|
|
|
|
AND t.`otype` = ?
|
|
|
|
AND t.`type` = ?
|
|
|
|
AND t.`term` != ''
|
|
|
|
AND i.`received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
|
|
|
|
GROUP BY `term`
|
|
|
|
ORDER BY `score` DESC
|
|
|
|
LIMIT ?",
|
|
|
|
Term::OBJECT_TYPE_POST,
|
|
|
|
Term::HASHTAG,
|
|
|
|
$period,
|
|
|
|
$limit
|
|
|
|
);
|
|
|
|
|
2019-08-07 02:52:54 +02:00
|
|
|
if (DBA::isResult($tagsStmt)) {
|
2019-08-06 13:36:51 +02:00
|
|
|
$tags = DBA::toArray($tagsStmt);
|
|
|
|
Cache::set('global_trending_tags', $tags, Cache::HOUR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tags ?: [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of the most frequent local hashtags over the given period
|
|
|
|
*
|
|
|
|
* @param int $period Period in hours to consider posts
|
|
|
|
* @return array
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function getLocalTrendingHashtags(int $period, $limit = 10)
|
|
|
|
{
|
|
|
|
$tags = Cache::get('local_trending_tags');
|
|
|
|
|
|
|
|
if (!$tags) {
|
|
|
|
$tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
|
|
|
|
FROM `term` t
|
|
|
|
JOIN `item` i ON i.`id` = t.`oid` AND i.`uid` = t.`uid`
|
|
|
|
JOIN `thread` ON `thread`.`iid` = i.`id`
|
|
|
|
JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
|
|
|
|
WHERE `thread`.`visible`
|
|
|
|
AND NOT `thread`.`deleted`
|
|
|
|
AND NOT `thread`.`moderated`
|
|
|
|
AND NOT `thread`.`private`
|
|
|
|
AND `thread`.`wall`
|
|
|
|
AND `thread`.`origin`
|
|
|
|
AND t.`otype` = ?
|
|
|
|
AND t.`type` = ?
|
|
|
|
AND t.`term` != ''
|
|
|
|
AND i.`received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
|
|
|
|
GROUP BY `term`
|
|
|
|
ORDER BY `score` DESC
|
|
|
|
LIMIT ?",
|
|
|
|
Term::OBJECT_TYPE_POST,
|
|
|
|
Term::HASHTAG,
|
|
|
|
$period,
|
|
|
|
$limit
|
|
|
|
);
|
|
|
|
|
2019-08-07 02:52:54 +02:00
|
|
|
if (DBA::isResult($tagsStmt)) {
|
2019-08-06 13:36:51 +02:00
|
|
|
$tags = DBA::toArray($tagsStmt);
|
|
|
|
Cache::set('local_trending_tags', $tags, Cache::HOUR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tags ?: [];
|
|
|
|
}
|
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
/**
|
|
|
|
* Generates the legacy item.tag field comma-separated BBCode string from an item ID.
|
|
|
|
* Includes only hashtags, implicit and explicit mentions.
|
|
|
|
*
|
|
|
|
* @param int $item_id
|
|
|
|
* @return string
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function tagTextFromItemId($item_id)
|
|
|
|
{
|
|
|
|
$tag_list = [];
|
|
|
|
$tags = self::tagArrayFromItemId($item_id, [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION]);
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
$tag_list[] = self::TAG_CHARACTER[$tag['type']] . '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]';
|
2018-06-30 15:54:01 +02:00
|
|
|
}
|
2019-02-23 01:15:35 +01:00
|
|
|
|
|
|
|
return implode(',', $tag_list);
|
2018-06-30 15:54:01 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
/**
|
|
|
|
* Retrieves the terms from the provided type(s) associated with the provided item ID.
|
|
|
|
*
|
|
|
|
* @param int $item_id
|
|
|
|
* @param int|array $type
|
|
|
|
* @return array
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function tagArrayFromItemId($item_id, $type = [self::HASHTAG, self::MENTION])
|
2018-09-17 23:13:08 +02:00
|
|
|
{
|
2019-02-23 01:15:35 +01:00
|
|
|
$condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => $type];
|
2018-09-17 23:13:08 +02:00
|
|
|
$tags = DBA::select('term', ['type', 'term', 'url'], $condition);
|
|
|
|
if (!DBA::isResult($tags)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return DBA::toArray($tags);
|
|
|
|
}
|
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
/**
|
|
|
|
* Generates the legacy item.file field string from an item ID.
|
|
|
|
* Includes only file and category terms.
|
|
|
|
*
|
|
|
|
* @param int $item_id
|
|
|
|
* @return string
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function fileTextFromItemId($item_id)
|
2018-06-30 23:15:24 +02:00
|
|
|
{
|
|
|
|
$file_text = '';
|
2019-02-23 01:15:35 +01:00
|
|
|
$tags = self::tagArrayFromItemId($item_id, [self::FILE, self::CATEGORY]);
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
if ($tag['type'] == self::CATEGORY) {
|
2018-07-01 09:57:59 +02:00
|
|
|
$file_text .= '<' . $tag['term'] . '>';
|
|
|
|
} else {
|
|
|
|
$file_text .= '[' . $tag['term'] . ']';
|
|
|
|
}
|
2018-06-30 23:15:24 +02:00
|
|
|
}
|
2019-02-23 01:15:35 +01:00
|
|
|
|
2018-06-30 23:15:24 +02:00
|
|
|
return $file_text;
|
|
|
|
}
|
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
/**
|
|
|
|
* Inserts new terms for the provided item ID based on the legacy item.tag field BBCode content.
|
|
|
|
* Deletes all previous tag terms for the same item ID.
|
|
|
|
* Sets both the item.mention and thread.mentions field flags if a mention concerning the item UID is found.
|
|
|
|
*
|
|
|
|
* @param int $item_id
|
|
|
|
* @param string $tag_str
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
*/
|
|
|
|
public static function insertFromTagFieldByItemId($item_id, $tag_str)
|
2018-02-04 05:49:48 +01:00
|
|
|
{
|
2019-12-30 23:00:08 +01:00
|
|
|
$profile_base = DI::baseUrl();
|
2018-02-04 05:49:48 +01:00
|
|
|
$profile_data = parse_url($profile_base);
|
2019-10-16 14:35:14 +02:00
|
|
|
$profile_path = $profile_data['path'] ?? '';
|
2018-02-04 05:49:48 +01:00
|
|
|
$profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
|
|
|
|
$profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
|
|
|
|
|
2018-06-30 17:21:32 +02:00
|
|
|
$fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'parent'];
|
2019-02-23 01:15:35 +01:00
|
|
|
$item = Item::selectFirst($fields, ['id' => $item_id]);
|
|
|
|
if (!DBA::isResult($item)) {
|
2018-02-04 05:49:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
$item['tag'] = $tag_str;
|
2018-06-30 17:21:32 +02:00
|
|
|
|
2018-02-04 05:49:48 +01:00
|
|
|
// Clean up all tags
|
2019-02-23 01:15:35 +01:00
|
|
|
self::deleteByItemId($item_id);
|
2018-02-04 05:49:48 +01:00
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
if ($item['deleted']) {
|
2018-02-04 05:49:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
$taglist = explode(',', $item['tag']);
|
2018-02-04 05:49:48 +01:00
|
|
|
|
|
|
|
$tags_string = '';
|
|
|
|
foreach ($taglist as $tag) {
|
2019-02-23 01:15:35 +01:00
|
|
|
if (Strings::startsWith($tag, self::TAG_CHARACTER)) {
|
2018-02-04 05:49:48 +01:00
|
|
|
$tags_string .= ' ' . trim($tag);
|
|
|
|
} else {
|
|
|
|
$tags_string .= ' #' . trim($tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
$data = ' ' . $item['title'] . ' ' . $item['body'] . ' ' . $tags_string . ' ';
|
2018-02-04 05:49:48 +01:00
|
|
|
|
|
|
|
// ignore anything in a code block
|
|
|
|
$data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
|
|
|
|
|
|
|
|
$tags = [];
|
|
|
|
|
|
|
|
$pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
|
|
|
|
if (preg_match_all($pattern, $data, $matches)) {
|
|
|
|
foreach ($matches[1] as $match) {
|
2018-06-30 15:54:01 +02:00
|
|
|
$tags['#' . $match] = '';
|
2018-02-04 05:49:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
$pattern = '/\W([\#@!%])\[url\=(.*?)\](.*?)\[\/url\]/ism';
|
2018-02-04 05:49:48 +01:00
|
|
|
if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
|
|
|
|
foreach ($matches as $match) {
|
2018-10-02 16:48:57 +02:00
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
if (in_array($match[1], [
|
|
|
|
self::TAG_CHARACTER[self::MENTION],
|
|
|
|
self::TAG_CHARACTER[self::IMPLICIT_MENTION],
|
|
|
|
self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]
|
|
|
|
])) {
|
2018-10-02 16:48:57 +02:00
|
|
|
$contact = Contact::getDetailsByURL($match[2], 0);
|
|
|
|
if (!empty($contact['addr'])) {
|
|
|
|
$match[3] = $contact['addr'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($contact['url'])) {
|
|
|
|
$match[2] = $contact['url'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
$tags[$match[2]] = $match[1] . trim($match[3], ',.:;[]/\"?!');
|
2018-02-04 05:49:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
foreach ($tags as $link => $tag) {
|
|
|
|
if (self::isType($tag, self::HASHTAG)) {
|
2018-02-04 05:49:48 +01:00
|
|
|
// try to ignore #039 or #1 or anything like that
|
|
|
|
if (ctype_digit(substr(trim($tag), 1))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to ignore html hex escapes, e.g. #x2317
|
|
|
|
if ((substr(trim($tag), 1, 1) == 'x' || substr(trim($tag), 1, 1) == 'X') && ctype_digit(substr(trim($tag), 2))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
$type = self::HASHTAG;
|
2018-02-04 05:49:48 +01:00
|
|
|
$term = substr($tag, 1);
|
2018-11-22 15:33:42 +01:00
|
|
|
$link = '';
|
2019-02-23 01:15:35 +01:00
|
|
|
} elseif (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION)) {
|
|
|
|
if (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION)) {
|
|
|
|
$type = self::MENTION;
|
|
|
|
} else {
|
|
|
|
$type = self::IMPLICIT_MENTION;
|
|
|
|
}
|
2018-10-02 11:04:32 +02:00
|
|
|
|
|
|
|
$contact = Contact::getDetailsByURL($link, 0);
|
|
|
|
if (!empty($contact['name'])) {
|
|
|
|
$term = $contact['name'];
|
|
|
|
} else {
|
|
|
|
$term = substr($tag, 1);
|
|
|
|
}
|
2018-02-04 05:49:48 +01:00
|
|
|
} else { // This shouldn't happen
|
2019-02-23 01:15:35 +01:00
|
|
|
$type = self::HASHTAG;
|
2018-02-04 05:49:48 +01:00
|
|
|
$term = $tag;
|
2018-11-22 15:33:42 +01:00
|
|
|
$link = '';
|
2019-02-23 15:25:21 +01:00
|
|
|
|
|
|
|
Logger::notice('Unknown term type', ['tag' => $tag]);
|
2018-02-04 05:49:48 +01:00
|
|
|
}
|
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
if (DBA::exists('term', ['uid' => $item['uid'], 'otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'term' => $term, 'type' => $type])) {
|
2018-10-02 11:04:32 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
if ($item['uid'] == 0) {
|
2018-02-04 05:49:48 +01:00
|
|
|
$global = true;
|
2019-02-23 01:15:35 +01:00
|
|
|
DBA::update('term', ['global' => true], ['otype' => self::OBJECT_TYPE_POST, 'guid' => $item['guid']]);
|
2018-02-04 05:49:48 +01:00
|
|
|
} else {
|
2019-02-23 01:15:35 +01:00
|
|
|
$global = DBA::exists('term', ['uid' => 0, 'otype' => self::OBJECT_TYPE_POST, 'guid' => $item['guid']]);
|
2018-02-04 05:49:48 +01:00
|
|
|
}
|
|
|
|
|
2018-07-20 14:19:26 +02:00
|
|
|
DBA::insert('term', [
|
2019-02-23 01:15:35 +01:00
|
|
|
'uid' => $item['uid'],
|
|
|
|
'oid' => $item_id,
|
|
|
|
'otype' => self::OBJECT_TYPE_POST,
|
2018-02-04 05:49:48 +01:00
|
|
|
'type' => $type,
|
|
|
|
'term' => $term,
|
|
|
|
'url' => $link,
|
2019-02-23 01:15:35 +01:00
|
|
|
'guid' => $item['guid'],
|
|
|
|
'created' => $item['created'],
|
|
|
|
'received' => $item['received'],
|
2018-02-04 05:49:48 +01:00
|
|
|
'global' => $global
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Search for mentions
|
2019-02-23 01:15:35 +01:00
|
|
|
if (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION)
|
|
|
|
&& (
|
|
|
|
strpos($link, $profile_base_friendica) !== false
|
|
|
|
|| strpos($link, $profile_base_diaspora) !== false
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$users_stmt = DBA::p("SELECT `uid` FROM `contact` WHERE self AND (`url` = ? OR `nurl` = ?)", $link, $link);
|
|
|
|
$users = DBA::toArray($users_stmt);
|
2018-02-04 05:49:48 +01:00
|
|
|
foreach ($users AS $user) {
|
2019-02-23 01:15:35 +01:00
|
|
|
if ($user['uid'] == $item['uid']) {
|
|
|
|
/// @todo This function is called from Item::update - so we mustn't call that function here
|
|
|
|
DBA::update('item', ['mention' => true], ['id' => $item_id]);
|
|
|
|
DBA::update('thread', ['mention' => true], ['iid' => $item['parent']]);
|
2018-02-04 05:49:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:46:05 +01:00
|
|
|
/**
|
2019-02-23 01:15:35 +01:00
|
|
|
* Inserts new terms for the provided item ID based on the legacy item.file field BBCode content.
|
|
|
|
* Deletes all previous file terms for the same item ID.
|
|
|
|
*
|
|
|
|
* @param integer $item_id item id
|
2019-01-06 22:06:53 +01:00
|
|
|
* @param $files
|
2018-01-10 17:46:05 +01:00
|
|
|
* @return void
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Exception
|
2018-01-10 17:46:05 +01:00
|
|
|
*/
|
2019-02-23 01:15:35 +01:00
|
|
|
public static function insertFromFileFieldByItemId($item_id, $files)
|
2018-01-10 17:46:05 +01:00
|
|
|
{
|
2019-02-23 01:15:35 +01:00
|
|
|
$message = Item::selectFirst(['uid', 'deleted'], ['id' => $item_id]);
|
2018-07-21 14:46:04 +02:00
|
|
|
if (!DBA::isResult($message)) {
|
2018-01-10 17:46:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up all tags
|
2019-02-23 01:15:35 +01:00
|
|
|
DBA::delete('term', ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [self::FILE, self::CATEGORY]]);
|
2018-01-10 17:46:05 +01:00
|
|
|
|
2018-01-10 19:04:00 +01:00
|
|
|
if ($message["deleted"]) {
|
2018-01-10 17:46:05 +01:00
|
|
|
return;
|
2018-01-10 19:04:00 +01:00
|
|
|
}
|
2018-01-10 17:46:05 +01:00
|
|
|
|
2018-06-30 23:15:24 +02:00
|
|
|
$message['file'] = $files;
|
|
|
|
|
2018-01-10 17:46:05 +01:00
|
|
|
if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
|
|
|
|
foreach ($files[1] as $file) {
|
2018-07-20 14:19:26 +02:00
|
|
|
DBA::insert('term', [
|
2018-01-10 19:04:00 +01:00
|
|
|
'uid' => $message["uid"],
|
2019-02-23 01:15:35 +01:00
|
|
|
'oid' => $item_id,
|
|
|
|
'otype' => self::OBJECT_TYPE_POST,
|
|
|
|
'type' => self::FILE,
|
2018-01-10 19:04:00 +01:00
|
|
|
'term' => $file
|
|
|
|
]);
|
2018-01-10 17:46:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
|
|
|
|
foreach ($files[1] as $file) {
|
2018-07-20 14:19:26 +02:00
|
|
|
DBA::insert('term', [
|
2018-01-10 19:04:00 +01:00
|
|
|
'uid' => $message["uid"],
|
2019-02-23 01:15:35 +01:00
|
|
|
'oid' => $item_id,
|
|
|
|
'otype' => self::OBJECT_TYPE_POST,
|
|
|
|
'type' => self::CATEGORY,
|
2018-01-10 19:04:00 +01:00
|
|
|
'term' => $file
|
|
|
|
]);
|
2018-01-10 17:46:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-14 23:54:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
|
|
|
|
* provided item's body with them.
|
|
|
|
*
|
|
|
|
* @param array $item
|
|
|
|
* @return array
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
2018-04-14 23:54:16 +02:00
|
|
|
*/
|
|
|
|
public static function populateTagsFromItem(&$item)
|
|
|
|
{
|
|
|
|
$return = [
|
|
|
|
'tags' => [],
|
|
|
|
'hashtags' => [],
|
|
|
|
'mentions' => [],
|
2019-02-23 01:15:35 +01:00
|
|
|
'implicit_mentions' => [],
|
2018-04-14 23:54:16 +02:00
|
|
|
];
|
|
|
|
|
2019-12-30 23:00:08 +01:00
|
|
|
$searchpath = DI::baseUrl() . "/search?tag=";
|
2018-04-14 23:54:16 +02:00
|
|
|
|
2018-07-20 14:19:26 +02:00
|
|
|
$taglist = DBA::select(
|
2018-04-14 23:54:16 +02:00
|
|
|
'term',
|
|
|
|
['type', 'term', 'url'],
|
2019-02-23 01:15:35 +01:00
|
|
|
['otype' => self::OBJECT_TYPE_POST, 'oid' => $item['id'], 'type' => [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION]],
|
2018-04-14 23:54:16 +02:00
|
|
|
['order' => ['tid']]
|
|
|
|
);
|
2018-07-20 14:19:26 +02:00
|
|
|
while ($tag = DBA::fetch($taglist)) {
|
2018-11-22 15:33:42 +01:00
|
|
|
if ($tag['url'] == '') {
|
|
|
|
$tag['url'] = $searchpath . rawurlencode($tag['term']);
|
2018-04-14 23:54:16 +02:00
|
|
|
}
|
|
|
|
|
2018-11-22 15:23:42 +01:00
|
|
|
$orig_tag = $tag['url'];
|
2018-04-14 23:54:16 +02:00
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
$prefix = self::TAG_CHARACTER[$tag['type']];
|
|
|
|
switch($tag['type']) {
|
|
|
|
case self::HASHTAG:
|
|
|
|
if ($orig_tag != $tag['url']) {
|
|
|
|
$item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
|
|
|
|
}
|
2018-04-14 23:54:16 +02:00
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
$return['hashtags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
|
|
|
|
$return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
|
|
|
|
break;
|
|
|
|
case self::MENTION:
|
|
|
|
$tag['url'] = Contact::magicLink($tag['url']);
|
|
|
|
$return['mentions'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
|
|
|
|
$return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
|
|
|
|
break;
|
|
|
|
case self::IMPLICIT_MENTION:
|
|
|
|
$return['implicit_mentions'][] = $prefix . $tag['term'];
|
|
|
|
break;
|
2018-04-14 23:54:16 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-20 14:19:26 +02:00
|
|
|
DBA::close($taglist);
|
2018-04-14 23:54:16 +02:00
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
2018-10-23 11:40:20 +02:00
|
|
|
|
2018-10-23 13:32:32 +02:00
|
|
|
/**
|
2019-02-23 01:15:35 +01:00
|
|
|
* Delete tags of the specific type(s) from an item
|
2019-01-06 22:06:53 +01:00
|
|
|
*
|
2019-02-23 01:15:35 +01:00
|
|
|
* @param int $item_id
|
|
|
|
* @param int|array $type
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Exception
|
2018-10-23 11:40:20 +02:00
|
|
|
*/
|
2019-02-23 01:15:35 +01:00
|
|
|
public static function deleteByItemId($item_id, $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION])
|
2018-10-23 11:40:20 +02:00
|
|
|
{
|
2019-02-23 01:15:35 +01:00
|
|
|
if (empty($item_id)) {
|
2018-10-23 11:40:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up all tags
|
2019-02-23 01:15:35 +01:00
|
|
|
DBA::delete('term', ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => $type]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the provided tag is of one of the provided term types.
|
|
|
|
*
|
|
|
|
* @param string $tag
|
|
|
|
* @param int ...$types
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isType($tag, ...$types)
|
|
|
|
{
|
|
|
|
$tag_chars = [];
|
|
|
|
foreach ($types as $type) {
|
2019-02-27 13:59:43 +01:00
|
|
|
if (array_key_exists($type, self::TAG_CHARACTER)) {
|
2019-02-23 01:15:35 +01:00
|
|
|
$tag_chars[] = self::TAG_CHARACTER[$type];
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 11:40:20 +02:00
|
|
|
|
2019-02-23 01:15:35 +01:00
|
|
|
return Strings::startsWith($tag, $tag_chars);
|
2018-10-23 11:40:20 +02:00
|
|
|
}
|
2018-01-10 18:51:51 +01:00
|
|
|
}
|