1
1
Fork 0

Some more "term" to "tag" conversion

This commit is contained in:
Michael 2020-05-01 10:57:32 +00:00
parent 2b8dabcd95
commit ac2957c4dd
6 changed files with 70 additions and 101 deletions

View file

@ -101,7 +101,7 @@ function item_post(App $a) {
$toplevel_item_id = intval($_REQUEST['parent'] ?? 0);
$thr_parent_uri = trim($_REQUEST['parent_uri'] ?? '');
$thread_parent_id = 0;
$thread_parent_uriid = 0;
$thread_parent_contact = null;
$toplevel_item = null;
@ -123,7 +123,7 @@ function item_post(App $a) {
// if this isn't the top-level parent of the conversation, find it
if (DBA::isResult($toplevel_item)) {
// The URI and the contact is taken from the direct parent which needn't to be the top parent
$thread_parent_id = $toplevel_item['id'];
$thread_parent_uriid = $toplevel_item['uri-id'];
$thr_parent_uri = $toplevel_item['uri'];
$thread_parent_contact = Contact::getDetailsByURL($toplevel_item["author-link"]);
@ -381,8 +381,8 @@ function item_post(App $a) {
$tags = BBCode::getTags($body);
if ($thread_parent_id && !\Friendica\Content\Feature::isEnabled($uid, 'explicit_mentions')) {
$tags = item_add_implicit_mentions($tags, $thread_parent_contact, $thread_parent_id);
if ($thread_parent_uriid && !\Friendica\Content\Feature::isEnabled($uid, 'explicit_mentions')) {
$tags = item_add_implicit_mentions($tags, $thread_parent_contact, $thread_parent_uriid);
}
$tagged = [];
@ -1044,7 +1044,7 @@ function handle_tag(&$body, &$inform, &$str_tags, $profile_uid, $tag, $network =
return ['replaced' => $replaced, 'contact' => $contact];
}
function item_add_implicit_mentions(array $tags, array $thread_parent_contact, $thread_parent_id)
function item_add_implicit_mentions(array $tags, array $thread_parent_contact, $thread_parent_uriid)
{
if (DI::config()->get('system', 'disable_implicit_mentions')) {
// Add a tag if the parent contact is from ActivityPub or OStatus (This will notify them)
@ -1059,7 +1059,7 @@ function item_add_implicit_mentions(array $tags, array $thread_parent_contact, $
$thread_parent_contact['url'] => $thread_parent_contact['nick']
];
$parent_terms = Term::tagArrayFromItemId($thread_parent_id, [Tag::MENTION, Tag::IMPLICIT_MENTION]);
$parent_terms = Tag::getByURIId($thread_parent_uriid, [Tag::MENTION, Tag::IMPLICIT_MENTION]);
foreach ($parent_terms as $parent_term) {
$implicit_mentions[$parent_term['url']] = $parent_term['term'];

View file

@ -24,7 +24,6 @@ namespace Friendica\Content\Widget;
use Friendica\Core\Renderer;
use Friendica\DI;
use Friendica\Model\Tag;
use Friendica\Model\Term;
/**
* Trending tags aside widget for the community pages, handles both local and global scopes
@ -42,9 +41,9 @@ class TrendingTags
public static function getHTML($content = 'global', int $period = 24)
{
if ($content == 'local') {
$tags = Term::getLocalTrendingHashtags($period, 20);
$tags = Tag::getLocalTrendingHashtags($period, 20);
} else {
$tags = Term::getGlobalTrendingHashtags($period, 20);
$tags = Tag::getGlobalTrendingHashtags($period, 20);
}
$tpl = Renderer::getMarkupTemplate('widget/trending_tags.tpl');

View file

@ -22,6 +22,7 @@
namespace Friendica\Model;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Cache\Duration;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Database\DBA;
@ -416,4 +417,58 @@ class Tag
return $uriids;
}
/**
* 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 = DI::cache()->get('global_trending_tags');
if (empty($tags)) {
$tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
FROM `tag-search-view`
WHERE `private` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
GROUP BY `term` ORDER BY `score` DESC LIMIT ?",
Item::PUBLIC, $period, $limit);
if (DBA::isResult($tagsStmt)) {
$tags = DBA::toArray($tagsStmt);
DI::cache()->set('global_trending_tags', $tags, Duration::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 = DI::cache()->get('local_trending_tags');
if (empty($tags)) {
$tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
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);
if (DBA::isResult($tagsStmt)) {
$tags = DBA::toArray($tagsStmt);
DI::cache()->set('local_trending_tags', $tags, Duration::HOUR);
}
}
return $tags ?: [];
}
}

View file

@ -60,95 +60,6 @@ class Term
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)
{
$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,
self::OBJECT_TYPE_POST,
self::HASHTAG,
$period,
$limit
);
if (DBA::isResult($tagsStmt)) {
$tags = DBA::toArray($tagsStmt);
DI::cache()->set('global_trending_tags', $tags, Duration::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 = 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,
self::OBJECT_TYPE_POST,
self::HASHTAG,
$period,
$limit
);
if (DBA::isResult($tagsStmt)) {
$tags = DBA::toArray($tagsStmt);
DI::cache()->set('local_trending_tags', $tags, Duration::HOUR);
}
}
return $tags ?: [];
}
/**
* Generates the legacy item.tag field comma-separated BBCode string from an item ID.
* Includes only hashtags, implicit and explicit mentions.
@ -176,7 +87,7 @@ class Term
* @return array
* @throws \Exception
*/
public static function tagArrayFromItemId($item_id, $type = [self::HASHTAG, self::MENTION])
private 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);

View file

@ -48,14 +48,14 @@ class Source extends BaseAdmin
$item_id = '';
$terms = [];
if (!empty($guid)) {
$item = Model\Item::selectFirst(['id', 'guid', 'uri'], ['guid' => $guid]);
$item = Model\Item::selectFirst(['id', 'uri-id', 'guid', 'uri'], ['guid' => $guid]);
$conversation = Model\Conversation::getByItemUri($item['uri']);
$item_id = $item['id'];
$item_uri = $item['uri'];
$source = $conversation['source'];
$terms = Model\Term::tagArrayFromItemId($item['id'], [Model\Term::HASHTAG, Model\Term::MENTION, Model\Term::IMPLICIT_MENTION]);
$terms = Model\Tag::getByURIId($item['uri-id'], [Model\Tag::HASHTAG, Model\Tag::MENTION, Model\Tag::IMPLICIT_MENTION]);
}
$tpl = Renderer::getMarkupTemplate('admin/item/source.tpl');

View file

@ -228,6 +228,10 @@ return [
"guid" => ["item", "guid"],
"uid" => ["item", "uid"],
"private" => ["item", "private"],
"wall" => ["item", "wall"],
"origin" => ["item", "origin"],
"gravity" => ["item", "gravity"],
"received" => ["item", "received"],
"name" => ["tag", "name"],
],
"query" => "FROM `post-tag`