X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FTag.php;h=1cc48bd2f7dca5572e2cafa9517d42c335e60ae2;hb=72a3ab6382551811f22391b7e82e7398628b235b;hp=63a4ff492c662b32bf281db5614ba79235ef78c6;hpb=ee5207410c71492b2e771479ff1890d8ef4552da;p=friendica.git diff --git a/src/Model/Tag.php b/src/Model/Tag.php index 63a4ff492c..1cc48bd2f7 100644 --- a/src/Model/Tag.php +++ b/src/Model/Tag.php @@ -1,6 +1,6 @@ '#', self::MENTION => '@', - self::IMPLICIT_MENTION => '%', self::EXCLUSIVE_MENTION => '!', + self::IMPLICIT_MENTION => '%', ]; /** @@ -65,9 +71,8 @@ class Tag * @param integer $type * @param string $name * @param string $url - * @param boolean $probing */ - public static function store(int $uriid, int $type, string $name, string $url = '', $probing = true) + public static function store(int $uriid, int $type, string $name, string $url = '') { if ($type == self::HASHTAG) { // Trim Unicode non-word characters @@ -76,7 +81,7 @@ class Tag $tags = explode(self::TAG_CHARACTER[self::HASHTAG], $name); if (count($tags) > 1) { foreach ($tags as $tag) { - self::store($uriid, $type, $tag, $url, $probing); + self::store($uriid, $type, $tag, $url); } return; } @@ -89,7 +94,7 @@ class Tag $cid = 0; $tagid = 0; - if (in_array($type, [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION])) { + if (in_array($type, [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION, self::TO, self::CC, self::BTO, self::BCC])) { if (empty($url)) { // No mention without a contact url return; @@ -99,41 +104,26 @@ class Tag Logger::notice('Wrong scheme in url', ['url' => $url, 'callstack' => System::callstack(20)]); } - if (!$probing) { - $condition = ['nurl' => Strings::normaliseLink($url), 'uid' => 0, 'deleted' => false]; - $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]); - if (DBA::isResult($contact)) { - $cid = $contact['id']; - Logger::info('Got id for contact url', ['cid' => $cid, 'url' => $url]); - } - - if (empty($cid)) { - $ssl_url = str_replace('http://', 'https://', $url); - $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, 0]; - $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]); - if (DBA::isResult($contact)) { - $cid = $contact['id']; - Logger::info('Got id for contact alias', ['cid' => $cid, 'url' => $url]); - } - } - } else { - $cid = Contact::getIdForURL($url, 0, false); - Logger::info('Got id by probing', ['cid' => $cid, 'url' => $url]); - } + $cid = Contact::getIdForURL($url, 0, false); + Logger::debug('Got id for contact', ['cid' => $cid, 'url' => $url]); if (empty($cid)) { // The contact wasn't found in the system (most likely some dead account) // We ensure that we only store a single entry by overwriting the previous name - Logger::info('Contact not found, updating tag', ['url' => $url, 'name' => $name]); - DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url]); + Logger::info('URL is not a known contact, updating tag', ['url' => $url, 'name' => $name]); + if (!DBA::exists('tag', ['name' => substr($name, 0, 96), 'url' => $url])) { + DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url]); + } } } if (empty($cid)) { - if (($type != self::HASHTAG) && !empty($url) && ($url != $name)) { - $url = strtolower($url); - } else { - $url = ''; + if (!in_array($type, [self::TO, self::CC, self::BTO, self::BCC])) { + if (($type != self::HASHTAG) && !empty($url) && ($url != $name)) { + $url = strtolower($url); + } else { + $url = ''; + } } $tagid = self::getID($name, $url); @@ -204,22 +194,40 @@ class Tag } /** - * Store tags and mentions from the body + * Get tags and mentions from the body * - * @param integer $uriid URI-Id * @param string $body Body of the post * @param string $tags Accepted tags - * @param boolean $probing Perform a probing for contacts, adding them if needed + * + * @return array Tag list */ - public static function storeFromBody(int $uriid, string $body, string $tags = null, $probing = true) + public static function getFromBody(string $body, string $tags = null) { if (is_null($tags)) { $tags = self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]; } + if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) { + return []; + } + + return $result; + } + + /** + * Store tags and mentions from the body + * + * @param integer $uriid URI-Id + * @param string $body Body of the post + * @param string $tags Accepted tags + * @param boolean $probing Perform a probing for contacts, adding them if needed + */ + public static function storeFromBody(int $uriid, string $body, string $tags = null, $probing = true) + { Logger::info('Check for tags', ['uri-id' => $uriid, 'hash' => $tags, 'callstack' => System::callstack()]); - if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) { + $result = self::getFromBody($body, $tags); + if (empty($result)) { return; } @@ -265,7 +273,7 @@ class Tag */ public static function existsForPost(int $uriid) { - return DBA::exists('post-tag', ['uri-id' => $uriid, 'type' => [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION, self::EXCLUSIVE_MENTION]]); + return DBA::exists('post-tag', ['uri-id' => $uriid, 'type' => [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]); } /** @@ -347,7 +355,7 @@ class Tag return; } - $tags = DBA::select('tag-view', ['name', 'url'], ['uri-id' => $parent_uri_id]); + $tags = DBA::select('tag-view', ['name', 'url'], ['uri-id' => $parent_uri_id, 'type' => [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]); while ($tag = DBA::fetch($tags)) { self::store($uri_id, self::IMPLICIT_MENTION, $tag['name'], $tag['url']); } @@ -362,7 +370,7 @@ class Tag * @return array * @throws \Exception */ - public static function getByURIId(int $uri_id, array $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION, self::EXCLUSIVE_MENTION]) + public static function getByURIId(int $uri_id, array $type = [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]) { $condition = ['uri-id' => $uri_id, 'type' => $type]; return DBA::selectToArray('tag-view', ['type', 'name', 'url'], $condition); @@ -376,7 +384,7 @@ class Tag * @return string tags and mentions * @throws \Exception */ - public static function getCSVByURIId(int $uri_id, array $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION, self::EXCLUSIVE_MENTION]) + public static function getCSVByURIId(int $uri_id, array $type = [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]) { $tag_list = []; $tags = self::getByURIId($uri_id, $type); @@ -407,7 +415,7 @@ class Tag $searchpath = DI::baseUrl() . "/search?tag="; - $taglist = DBA::select('tag-view', ['type', 'name', 'url'], + $taglist = DBA::select('tag-view', ['type', 'name', 'url', 'cid'], ['uri-id' => $item['uri-id'], 'type' => [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]); while ($tag = DBA::fetch($taglist)) { if ($tag['url'] == '') { @@ -423,14 +431,18 @@ class Tag $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']); } - $return['hashtags'][] = $prefix . '' . htmlspecialchars($tag['name']) . ''; - $return['tags'][] = $prefix . '' . htmlspecialchars($tag['name']) . ''; + $return['hashtags'][] = '' . $prefix . '' . htmlspecialchars($tag['name']) . ''; + $return['tags'][] = '' . $prefix . '' . htmlspecialchars($tag['name']) . ''; break; case self::MENTION: case self::EXCLUSIVE_MENTION: + if (!empty($tag['cid'])) { + $tag['url'] = Contact::magicLinkById($tag['cid']); + } else { $tag['url'] = Contact::magicLink($tag['url']); - $return['mentions'][] = $prefix . '' . htmlspecialchars($tag['name']) . ''; - $return['tags'][] = $prefix . '' . htmlspecialchars($tag['name']) . ''; + } + $return['mentions'][] = '' . $prefix . '' . htmlspecialchars($tag['name']) . ''; + $return['tags'][] = '' . $prefix . '' . htmlspecialchars($tag['name']) . ''; break; case self::IMPLICIT_MENTION: $return['implicit_mentions'][] = $prefix . $tag['name']; @@ -451,12 +463,11 @@ class Tag */ public static function countByTag(string $search, int $uid = 0) { - $condition = ["`name` = ? AND (NOT `private` OR (`private` AND `uid` = ?)) - AND `uri-id` IN (SELECT `uri-id` FROM `post-view` WHERE `network` IN (?, ?, ?, ?))", - $search, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]; - $params = ['group_by' => ['uri-id']]; + $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`)) + AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))", + $search, 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0]; - return DBA::count('tag-search-view', $condition, $params); + return DBA::count('tag-search-view', $condition); } /** @@ -471,9 +482,9 @@ class Tag */ public static function getURIIdListByTag(string $search, int $uid = 0, int $start = 0, int $limit = 100, int $last_uriid = 0) { - $condition = ["`name` = ? AND (NOT `private` OR (`private` AND `uid` = ?)) - AND `uri-id` IN (SELECT `uri-id` FROM `post-view` WHERE `network` IN (?, ?, ?, ?))", - $search, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]; + $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`)) + AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))", + $search, 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0]; if (!empty($last_uriid)) { $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $last_uriid]); @@ -481,7 +492,6 @@ class Tag $params = [ 'order' => ['uri-id' => true], - 'group_by' => ['uri-id'], 'limit' => [$start, $limit] ]; @@ -514,6 +524,23 @@ class Tag } } + /** + * Fetch the blocked tags as SQL + * + * @return string + */ + private static function getBlockedSQL() + { + $blocked_txt = DI::config()->get('system', 'blocked_tags'); + if (empty($blocked_txt)) { + return ''; + } + + $blocked = explode(',', $blocked_txt); + array_walk($blocked, function(&$value) { $value = "'" . DBA::escape(trim($value)) . "'";}); + return " AND NOT `name` IN (" . implode(',', $blocked) . ")"; + } + /** * Creates a list of the most frequent global hashtags over the given period * @@ -524,11 +551,21 @@ class Tag */ public static function setGlobalTrendingHashtags(int $period, int $limit = 10) { - $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score` + // Get a uri-id that is at least X hours old. + // We use the uri-id in the query for the hash tags since this is much faster + $post = Post::selectFirstThread(['uri-id'], ["`uid` = ? AND `received` < ?", 0, DateTimeFormat::utc('now - ' . $period . ' hour')], + ['order' => ['received' => true]]); + if (empty($post['uri-id'])) { + return []; + } + + $block_sql = self::getBlockedSQL(); + + $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors` FROM `tag-search-view` - WHERE `private` = ? AND `uid` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR) - GROUP BY `term` ORDER BY `score` DESC LIMIT ?", - Item::PUBLIC, 0, $period, $limit); + WHERE `private` = ? AND `uid` = ? AND `uri-id` > ? $block_sql + GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?", + Item::PUBLIC, 0, $post['uri-id'], $limit); if (DBA::isResult($tagsStmt)) { $tags = DBA::toArray($tagsStmt); @@ -567,11 +604,21 @@ class Tag */ public static function setLocalTrendingHashtags(int $period, int $limit = 10) { - $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score` + // Get a uri-id that is at least X hours old. + // We use the uri-id in the query for the hash tags since this is much faster + $post = Post::selectFirstThread(['uri-id'], ["`uid` = ? AND `received` < ?", 0, DateTimeFormat::utc('now - ' . $period . ' hour')], + ['order' => ['received' => true]]); + if (empty($post['uri-id'])) { + return []; + } + + $block_sql = self::getBlockedSQL(); + + $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors` FROM `tag-search-view` - WHERE `private` = ? AND `wall` AND `origin` AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR) - GROUP BY `term` ORDER BY `score` DESC LIMIT ?", - Item::PUBLIC, $period, $limit); + WHERE `private` = ? AND `wall` AND `origin` AND `uri-id` > ? $block_sql + GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?", + Item::PUBLIC, $post['uri-id'], $limit); if (DBA::isResult($tagsStmt)) { $tags = DBA::toArray($tagsStmt);