X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FTerm.php;h=868f2bf0532380ee8bef06655f8e5e8bd6a594df;hb=7b7132971a64a92a685a5fd860fe4709dce1765a;hp=3718887122436ad35784b0f7b1f3b7cc8fc91620;hpb=3f4636d490f11aafe354061fdcf2188c242ea4d3;p=friendica.git
diff --git a/src/Model/Term.php b/src/Model/Term.php
index 3718887122..868f2bf053 100644
--- a/src/Model/Term.php
+++ b/src/Model/Term.php
@@ -1,41 +1,187 @@
.
+ *
*/
+
namespace Friendica\Model;
-use Friendica\Core\System;
+use Friendica\Core\Cache\Duration;
+use Friendica\Core\Logger;
use Friendica\Database\DBA;
+use Friendica\DI;
+use Friendica\Util\Strings;
-require_once 'boot.php';
-require_once 'include/conversation.php';
-require_once 'include/dba.php';
-
+/**
+ * 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.
+ */
class Term
{
- public static function tagTextFromItemId($itemid)
+ const UNKNOWN = 0;
+ const HASHTAG = 1;
+ const MENTION = 2;
+ const CATEGORY = 3;
+ const PCATEGORY = 4;
+ const FILE = 5;
+ const SAVEDSEARCH = 6;
+ const CONVERSATION = 7;
+ /**
+ * An implicit mention is a mention in a comment body that is redundant with the threading information.
+ */
+ const IMPLICIT_MENTION = 8;
+ /**
+ * An exclusive mention transfers the ownership of the post to the target account, usually a forum.
+ */
+ 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;
+
+ /**
+ * 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)
{
- $tag_text = '';
- $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]];
- $tags = DBA::select('term', [], $condition);
- while ($tag = DBA::fetch($tags)) {
- if ($tag_text != '') {
- $tag_text .= ',';
+ $tags = DI::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 `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 ?",
+ Item::PUBLIC,
+ Term::OBJECT_TYPE_POST,
+ Term::HASHTAG,
+ $period,
+ $limit
+ );
+
+ if (DBA::isResult($tagsStmt)) {
+ $tags = DBA::toArray($tagsStmt);
+ DI::cache()->set('global_trending_tags', $tags, Duration::HOUR);
}
+ }
- if ($tag['type'] == 1) {
- $tag_text .= '#';
- } else {
- $tag_text .= '@';
+ 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 = DI::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`
+ WHERE `thread`.`visible`
+ AND NOT `thread`.`deleted`
+ AND NOT `thread`.`moderated`
+ AND `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 ?",
+ Item::PUBLIC,
+ Term::OBJECT_TYPE_POST,
+ Term::HASHTAG,
+ $period,
+ $limit
+ );
+
+ if (DBA::isResult($tagsStmt)) {
+ $tags = DBA::toArray($tagsStmt);
+ DI::cache()->set('local_trending_tags', $tags, Duration::HOUR);
}
- $tag_text .= '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]';
}
- return $tag_text;
+
+ return $tags ?: [];
}
- public static function tagArrayFromItemId($itemid, $type = [TERM_HASHTAG, TERM_MENTION])
+ /**
+ * 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)
{
- $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => $type];
+ $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]';
+ }
+
+ return implode(',', $tag_list);
+ }
+
+ /**
+ * 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])
+ {
+ $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => $type];
$tags = DBA::select('term', ['type', 'term', 'url'], $condition);
if (!DBA::isResult($tags)) {
return [];
@@ -44,56 +190,73 @@ class Term
return DBA::toArray($tags);
}
- public static function fileTextFromItemId($itemid)
+ /**
+ * 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)
{
$file_text = '';
- $condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]];
- $tags = DBA::select('term', [], $condition);
- while ($tag = DBA::fetch($tags)) {
- if ($tag['type'] == TERM_CATEGORY) {
+ $tags = self::tagArrayFromItemId($item_id, [self::FILE, self::CATEGORY]);
+ foreach ($tags as $tag) {
+ if ($tag['type'] == self::CATEGORY) {
$file_text .= '<' . $tag['term'] . '>';
} else {
$file_text .= '[' . $tag['term'] . ']';
}
}
+
return $file_text;
}
- public static function insertFromTagFieldByItemId($itemid, $tags)
+ /**
+ * 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)
{
- $profile_base = System::baseUrl();
+ $profile_base = DI::baseUrl();
$profile_data = parse_url($profile_base);
- $profile_path = defaults($profile_data, 'path', '');
+ $profile_path = $profile_data['path'] ?? '';
$profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
$profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
$fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'parent'];
- $message = Item::selectFirst($fields, ['id' => $itemid]);
- if (!DBA::isResult($message)) {
+ $item = Item::selectFirst($fields, ['id' => $item_id]);
+ if (!DBA::isResult($item)) {
return;
}
- $message['tag'] = $tags;
+ $item['tag'] = $tag_str;
// Clean up all tags
- self::deleteByItemId($itemid);
+ self::deleteByItemId($item_id);
- if ($message['deleted']) {
+ if ($item['deleted']) {
return;
}
- $taglist = explode(',', $message['tag']);
+ $taglist = explode(',', $item['tag']);
$tags_string = '';
foreach ($taglist as $tag) {
- if ((substr(trim($tag), 0, 1) == '#') || (substr(trim($tag), 0, 1) == '@') || (substr(trim($tag), 0, 1) == '!')) {
+ if (Strings::startsWith($tag, self::TAG_CHARACTER)) {
$tags_string .= ' ' . trim($tag);
} else {
$tags_string .= ' #' . trim($tag);
}
}
- $data = ' ' . $message['title'] . ' ' . $message['body'] . ' ' . $tags_string . ' ';
+ $data = ' ' . $item['title'] . ' ' . $item['body'] . ' ' . $tags_string . ' ';
// ignore anything in a code block
$data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
@@ -107,11 +270,15 @@ class Term
}
}
- $pattern = '/\W([\#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism';
+ $pattern = '/\W([\#@!%])\[url\=(.*?)\](.*?)\[\/url\]/ism';
if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
- if (($match[1] == '@') || ($match[1] == '!')) {
+ if (in_array($match[1], [
+ self::TAG_CHARACTER[self::MENTION],
+ self::TAG_CHARACTER[self::IMPLICIT_MENTION],
+ self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]
+ ])) {
$contact = Contact::getDetailsByURL($match[2], 0);
if (!empty($contact['addr'])) {
$match[3] = $contact['addr'];
@@ -122,12 +289,12 @@ class Term
}
}
- $tags[$match[1] . trim($match[3], ',.:;[]/\"?!')] = $match[2];
+ $tags[$match[2]] = $match[1] . trim($match[3], ',.:;[]/\"?!');
}
}
- foreach ($tags as $tag => $link) {
- if (substr(trim($tag), 0, 1) == '#') {
+ foreach ($tags as $link => $tag) {
+ if (self::isType($tag, self::HASHTAG)) {
// try to ignore #039 or #1 or anything like that
if (ctype_digit(substr(trim($tag), 1))) {
continue;
@@ -138,11 +305,15 @@ class Term
continue;
}
- $type = TERM_HASHTAG;
+ $type = self::HASHTAG;
$term = substr($tag, 1);
$link = '';
- } elseif ((substr(trim($tag), 0, 1) == '@') || (substr(trim($tag), 0, 1) == '!')) {
- $type = TERM_MENTION;
+ } 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;
+ }
$contact = Contact::getDetailsByURL($link, 0);
if (!empty($contact['name'])) {
@@ -151,43 +322,51 @@ class Term
$term = substr($tag, 1);
}
} else { // This shouldn't happen
- $type = TERM_HASHTAG;
+ $type = self::HASHTAG;
$term = $tag;
$link = '';
+
+ Logger::notice('Unknown term type', ['tag' => $tag]);
}
- if (DBA::exists('term', ['uid' => $message['uid'], 'otype' => TERM_OBJ_POST, 'oid' => $itemid, 'url' => $link])) {
+ if (DBA::exists('term', ['uid' => $item['uid'], 'otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'term' => $term, 'type' => $type])) {
continue;
}
- if ($message['uid'] == 0) {
+ if ($item['uid'] == 0) {
$global = true;
- DBA::update('term', ['global' => true], ['otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
+ DBA::update('term', ['global' => true], ['otype' => self::OBJECT_TYPE_POST, 'guid' => $item['guid']]);
} else {
- $global = DBA::exists('term', ['uid' => 0, 'otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
+ $global = DBA::exists('term', ['uid' => 0, 'otype' => self::OBJECT_TYPE_POST, 'guid' => $item['guid']]);
}
DBA::insert('term', [
- 'uid' => $message['uid'],
- 'oid' => $itemid,
- 'otype' => TERM_OBJ_POST,
+ 'uid' => $item['uid'],
+ 'oid' => $item_id,
+ 'otype' => self::OBJECT_TYPE_POST,
'type' => $type,
'term' => $term,
'url' => $link,
- 'guid' => $message['guid'],
- 'created' => $message['created'],
- 'received' => $message['received'],
+ 'guid' => $item['guid'],
+ 'created' => $item['created'],
+ 'received' => $item['received'],
'global' => $global
]);
// Search for mentions
- if (((substr($tag, 0, 1) == '@') || (substr($tag, 0, 1) == '!')) && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) {
- $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
+ 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);
foreach ($users AS $user) {
- if ($user['uid'] == $message['uid']) {
- /// @todo This function is called frim Item::update - so we mustn't call that function here
- DBA::update('item', ['mention' => true], ['id' => $itemid]);
- DBA::update('thread', ['mention' => true], ['iid' => $message['parent']]);
+ 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']]);
}
}
}
@@ -195,18 +374,23 @@ class Term
}
/**
- * @param integer $itemid item id
+ * 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
+ * @param $files
* @return void
+ * @throws \Exception
*/
- public static function insertFromFileFieldByItemId($itemid, $files)
+ public static function insertFromFileFieldByItemId($item_id, $files)
{
- $message = Item::selectFirst(['uid', 'deleted'], ['id' => $itemid]);
+ $message = Item::selectFirst(['uid', 'deleted'], ['id' => $item_id]);
if (!DBA::isResult($message)) {
return;
}
// Clean up all tags
- DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]]);
+ DBA::delete('term', ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [self::FILE, self::CATEGORY]]);
if ($message["deleted"]) {
return;
@@ -218,9 +402,9 @@ class Term
foreach ($files[1] as $file) {
DBA::insert('term', [
'uid' => $message["uid"],
- 'oid' => $itemid,
- 'otype' => TERM_OBJ_POST,
- 'type' => TERM_FILE,
+ 'oid' => $item_id,
+ 'otype' => self::OBJECT_TYPE_POST,
+ 'type' => self::FILE,
'term' => $file
]);
}
@@ -230,9 +414,9 @@ class Term
foreach ($files[1] as $file) {
DBA::insert('term', [
'uid' => $message["uid"],
- 'oid' => $itemid,
- 'otype' => TERM_OBJ_POST,
- 'type' => TERM_CATEGORY,
+ 'oid' => $item_id,
+ 'otype' => self::OBJECT_TYPE_POST,
+ 'type' => self::CATEGORY,
'term' => $file
]);
}
@@ -245,6 +429,8 @@ class Term
*
* @param array $item
* @return array
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ * @throws \ImagickException
*/
public static function populateTagsFromItem(&$item)
{
@@ -252,17 +438,17 @@ class Term
'tags' => [],
'hashtags' => [],
'mentions' => [],
+ 'implicit_mentions' => [],
];
- $searchpath = System::baseUrl() . "/search?tag=";
+ $searchpath = DI::baseUrl() . "/search?tag=";
$taglist = DBA::select(
'term',
['type', 'term', 'url'],
- ["`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION],
+ ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item['id'], 'type' => [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION]],
['order' => ['tid']]
);
-
while ($tag = DBA::fetch($taglist)) {
if ($tag['url'] == '') {
$tag['url'] = $searchpath . rawurlencode($tag['term']);
@@ -270,23 +456,25 @@ class Term
$orig_tag = $tag['url'];
- $author = ['uid' => 0, 'id' => $item['author-id'],
- 'network' => $item['author-network'], 'url' => $item['author-link']];
- $tag['url'] = Contact::magicLinkByContact($author, $tag['url']);
-
- if ($tag['type'] == TERM_HASHTAG) {
- if ($orig_tag != $tag['url']) {
- $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
- }
+ $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']);
+ }
- $return['hashtags'][] = '#' . $tag['term'] . '';
- $prefix = '#';
- } elseif ($tag['type'] == TERM_MENTION) {
- $return['mentions'][] = '@' . $tag['term'] . '';
- $prefix = '@';
+ $return['hashtags'][] = $prefix . '' . htmlspecialchars($tag['term']) . '';
+ $return['tags'][] = $prefix . '' . htmlspecialchars($tag['term']) . '';
+ break;
+ case self::MENTION:
+ $tag['url'] = Contact::magicLink($tag['url']);
+ $return['mentions'][] = $prefix . '' . htmlspecialchars($tag['term']) . '';
+ $return['tags'][] = $prefix . '' . htmlspecialchars($tag['term']) . '';
+ break;
+ case self::IMPLICIT_MENTION:
+ $return['implicit_mentions'][] = $prefix . $tag['term'];
+ break;
}
-
- $return['tags'][] = $prefix . '' . $tag['term'] . '';
}
DBA::close($taglist);
@@ -294,18 +482,38 @@ class Term
}
/**
- * Delete all tags from an item
- * @param int itemid - choose from which item the tags will be removed
- * @param array type - items type. default is [TERM_HASHTAG, TERM_MENTION]
+ * Delete tags of the specific type(s) from an item
+ *
+ * @param int $item_id
+ * @param int|array $type
+ * @throws \Exception
*/
- public static function deleteByItemId($itemid, $type = [TERM_HASHTAG, TERM_MENTION])
+ public static function deleteByItemId($item_id, $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION])
{
- if (empty($itemid)) {
+ if (empty($item_id)) {
return;
}
// Clean up all tags
- DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => $type]);
+ 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) {
+ if (array_key_exists($type, self::TAG_CHARACTER)) {
+ $tag_chars[] = self::TAG_CHARACTER[$type];
+ }
+ }
+ return Strings::startsWith($tag, $tag_chars);
}
}