The tag table is now really used

This commit is contained in:
Michael 2020-04-26 15:24:58 +00:00
parent af97df96bd
commit 5df5e9521b
9 changed files with 121 additions and 93 deletions

View file

@ -33,7 +33,6 @@ use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Notify; use Friendica\Model\Notify;
use Friendica\Model\Term;
use Friendica\Util\BasePath; use Friendica\Util\BasePath;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;

View file

@ -22,7 +22,6 @@
use Friendica\App; use Friendica\App;
use Friendica\Content\ContactSelector; use Friendica\Content\ContactSelector;
use Friendica\Content\Feature; use Friendica\Content\Feature;
use Friendica\Content\Pager;
use Friendica\Content\Text\BBCode; use Friendica\Content\Text\BBCode;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\Core\Logger; use Friendica\Core\Logger;
@ -34,7 +33,7 @@ use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Profile; use Friendica\Model\Profile;
use Friendica\Model\Term; use Friendica\Model\Tag;
use Friendica\Object\Post; use Friendica\Object\Post;
use Friendica\Object\Thread; use Friendica\Object\Thread;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
@ -527,7 +526,7 @@ function conversation(App $a, array $items, $mode, $update, $preview = false, $o
$profile_name = $item['author-link']; $profile_name = $item['author-link'];
} }
$tags = Term::populateTagsFromItem($item); $tags = Tag::populateTagsFromItem($item);
$author = ['uid' => 0, 'id' => $item['author-id'], $author = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']]; 'network' => $item['author-network'], 'url' => $item['author-link']];

View file

@ -25,6 +25,7 @@ use Friendica\Content\Text\BBCode;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Util\Strings; use Friendica\Util\Strings;
/** /**
@ -221,6 +222,17 @@ class Tag
} }
} }
/**
* Checks for stored hashtags and mentions for the given post
*
* @param integer $uriid
* @return bool
*/
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]]);
}
/** /**
* Remove tag/mention * Remove tag/mention
* *
@ -282,6 +294,86 @@ class Tag
} else { } else {
return self::UNKNOWN; return self::UNKNOWN;
} }
} }
/**
* 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 ArrayFromURIId(int $uri_id, array $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION, self::EXCLUSIVE_MENTION])
{
$condition = ['uri-id' => $uri_id, 'type' => $type];
$tags = DBA::select('tag-view', ['type', 'name', 'url'], $condition);
if (!DBA::isResult($tags)) {
return [];
}
$tag_list = [];
while ($tag = DBA::fetch($tags)) {
$tag['term'] = $tag['name']; /// @todo Remove this line when all occurrences of "term" had been replaced with "name"
$tag_list[] = $tag;
}
return $tag_list;
}
/**
* 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
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function populateTagsFromItem(&$item)
{
$return = [
'tags' => [],
'hashtags' => [],
'mentions' => [],
'implicit_mentions' => [],
];
$searchpath = DI::baseUrl() . "/search?tag=";
$taglist = DBA::select('tag-view', ['type', 'name', 'url'],
['uri-id' => $item['uri-id'], 'type' => [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]);
while ($tag = DBA::fetch($taglist)) {
if ($tag['url'] == '') {
$tag['url'] = $searchpath . rawurlencode($tag['name']);
}
$orig_tag = $tag['url'];
$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'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
$return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
break;
case self::MENTION:
case self::EXCLUSIVE_MENTION:
$tag['url'] = Contact::magicLink($tag['url']);
$return['mentions'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
$return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
break;
case self::IMPLICIT_MENTION:
$return['implicit_mentions'][] = $prefix . $tag['name'];
break;
}
}
DBA::close($taglist);
return $return;
}
} }

View file

@ -89,8 +89,8 @@ class Term
ORDER BY `score` DESC ORDER BY `score` DESC
LIMIT ?", LIMIT ?",
Item::PUBLIC, Item::PUBLIC,
Term::OBJECT_TYPE_POST, self::OBJECT_TYPE_POST,
Term::HASHTAG, self::HASHTAG,
$period, $period,
$limit $limit
); );
@ -134,8 +134,8 @@ class Term
ORDER BY `score` DESC ORDER BY `score` DESC
LIMIT ?", LIMIT ?",
Item::PUBLIC, Item::PUBLIC,
Term::OBJECT_TYPE_POST, self::OBJECT_TYPE_POST,
Term::HASHTAG, self::HASHTAG,
$period, $period,
$limit $limit
); );
@ -424,64 +424,6 @@ class Term
} }
} }
/**
* 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
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function populateTagsFromItem(&$item)
{
$return = [
'tags' => [],
'hashtags' => [],
'mentions' => [],
'implicit_mentions' => [],
];
$searchpath = DI::baseUrl() . "/search?tag=";
$taglist = DBA::select(
'term',
['type', 'term', 'url'],
['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']);
}
$orig_tag = $tag['url'];
$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'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['term']) . '</a>';
$return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['term']) . '</a>';
break;
case self::MENTION:
$tag['url'] = Contact::magicLink($tag['url']);
$return['mentions'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['term']) . '</a>';
$return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['term']) . '</a>';
break;
case self::IMPLICIT_MENTION:
$return['implicit_mentions'][] = $prefix . $tag['term'];
break;
}
}
DBA::close($taglist);
return $return;
}
/** /**
* Delete tags of the specific type(s) from an item * Delete tags of the specific type(s) from an item
* *

View file

@ -35,7 +35,7 @@ use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Profile as ProfileModel; use Friendica\Model\Profile as ProfileModel;
use Friendica\Model\Term; use Friendica\Model\Tag;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Module\BaseProfile; use Friendica\Module\BaseProfile;
use Friendica\Module\Security\Login; use Friendica\Module\Security\Login;
@ -184,7 +184,7 @@ class Profile extends BaseProfile
foreach (explode(',', $a->profile['pub_keywords']) as $tag_label) { foreach (explode(',', $a->profile['pub_keywords']) as $tag_label) {
$tags[] = [ $tags[] = [
'url' => '/search?tag=' . $tag_label, 'url' => '/search?tag=' . $tag_label,
'label' => Term::TAG_CHARACTER[Term::HASHTAG] . $tag_label, 'label' => Tag::TAG_CHARACTER[Tag::HASHTAG] . $tag_label,
]; ];
} }

View file

@ -33,7 +33,7 @@ use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Term; use Friendica\Model\Tag;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
use Friendica\Util\Crypto; use Friendica\Util\Crypto;
@ -390,7 +390,7 @@ class Post
$buttons["like"] = false; $buttons["like"] = false;
} }
$tags = Term::populateTagsFromItem($item); $tags = Tag::populateTagsFromItem($item);
$ago = Temporal::getRelativeDate($item['created']); $ago = Temporal::getRelativeDate($item['created']);
$ago_received = Temporal::getRelativeDate($item['received']); $ago_received = Temporal::getRelativeDate($item['received']);
@ -860,7 +860,7 @@ class Post
return ''; return '';
} }
$item = Item::selectFirst(['author-addr'], ['id' => $this->getId()]); $item = Item::selectFirst(['author-addr', 'uri-id'], ['id' => $this->getId()]);
if (!DBA::isResult($item) || empty($item['author-addr'])) { if (!DBA::isResult($item) || empty($item['author-addr'])) {
// Should not happen // Should not happen
return ''; return '';
@ -872,7 +872,7 @@ class Post
$text = ''; $text = '';
} }
$terms = Term::tagArrayFromItemId($this->getId(), [Term::MENTION, Term::IMPLICIT_MENTION]); $terms = Tag::ArrayFromURIId($item['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
foreach ($terms as $term) { foreach ($terms as $term) {
$profile = Contact::getDetailsByURL($term['url']); $profile = Contact::getDetailsByURL($term['url']);
if (!empty($profile['addr']) && ((($profile['contact-type'] ?? '') ?: Contact::TYPE_UNKNOWN) != Contact::TYPE_COMMUNITY) && if (!empty($profile['addr']) && ((($profile['contact-type'] ?? '') ?: Contact::TYPE_UNKNOWN) != Contact::TYPE_COMMUNITY) &&

View file

@ -35,7 +35,6 @@ use Friendica\Model\Item;
use Friendica\Model\ItemURI; use Friendica\Model\ItemURI;
use Friendica\Model\Mail; use Friendica\Model\Mail;
use Friendica\Model\Tag; use Friendica\Model\Tag;
use Friendica\Model\Term;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub; use Friendica\Protocol\ActivityPub;
@ -1016,7 +1015,7 @@ class Processor
return []; return [];
} }
$parent_terms = Term::tagArrayFromItemId($parent['id'], [Term::MENTION, Term::IMPLICIT_MENTION]); $parent_terms = Tag::ArrayFromURIId($parent['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
$parent_author = Contact::getDetailsByURL($parent['author-link'], 0); $parent_author = Contact::getDetailsByURL($parent['author-link'], 0);
@ -1084,8 +1083,8 @@ class Processor
foreach ($activity_tags as $index => $tag) { foreach ($activity_tags as $index => $tag) {
if (in_array($tag['href'], $potential_mentions)) { if (in_array($tag['href'], $potential_mentions)) {
$activity_tags[$index]['name'] = preg_replace( $activity_tags[$index]['name'] = preg_replace(
'/' . preg_quote(Term::TAG_CHARACTER[Term::MENTION], '/') . '/', '/' . preg_quote(Tag::TAG_CHARACTER[Tag::MENTION], '/') . '/',
Term::TAG_CHARACTER[Term::IMPLICIT_MENTION], Tag::TAG_CHARACTER[Tag::IMPLICIT_MENTION],
$activity_tags[$index]['name'], $activity_tags[$index]['name'],
1 1
); );

View file

@ -36,7 +36,7 @@ use Friendica\Model\Conversation;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Profile; use Friendica\Model\Profile;
use Friendica\Model\Photo; use Friendica\Model\Photo;
use Friendica\Model\Term; use Friendica\Model\Tag;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub; use Friendica\Protocol\ActivityPub;
@ -405,7 +405,7 @@ class Transmitter
$actor_profile = APContact::getByURL($item['author-link']); $actor_profile = APContact::getByURL($item['author-link']);
} }
$terms = Term::tagArrayFromItemId($item['id'], [Term::MENTION, Term::IMPLICIT_MENTION]); $terms = Tag::ArrayFromURIId($item['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
if ($item['private'] != Item::PRIVATE) { if ($item['private'] != Item::PRIVATE) {
// Directly mention the original author upon a quoted reshare. // Directly mention the original author upon a quoted reshare.
@ -1007,12 +1007,12 @@ class Transmitter
{ {
$tags = []; $tags = [];
$terms = Term::tagArrayFromItemId($item['id'], [Term::HASHTAG, Term::MENTION, Term::IMPLICIT_MENTION]); $terms = Tag::ArrayFromURIId($item['uri-id'], [Tag::HASHTAG, Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
foreach ($terms as $term) { foreach ($terms as $term) {
if ($term['type'] == Term::HASHTAG) { if ($term['type'] == Tag::HASHTAG) {
$url = DI::baseUrl() . '/search?tag=' . urlencode($term['term']); $url = DI::baseUrl() . '/search?tag=' . urlencode($term['term']);
$tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['term']]; $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['term']];
} elseif ($term['type'] == Term::MENTION || $term['type'] == Term::IMPLICIT_MENTION) { } else {
$contact = Contact::getDetailsByURL($term['url']); $contact = Contact::getDetailsByURL($term['url']);
if (!empty($contact['addr'])) { if (!empty($contact['addr'])) {
$mention = '@' . $contact['addr']; $mention = '@' . $contact['addr'];
@ -1211,15 +1211,14 @@ class Transmitter
/** /**
* Returns if the post contains sensitive content ("nsfw") * Returns if the post contains sensitive content ("nsfw")
* *
* @param integer $item_id * @param integer $uri_id
* *
* @return boolean * @return boolean
* @throws \Exception * @throws \Exception
*/ */
private static function isSensitive($item_id) private static function isSensitive($uri_id)
{ {
$condition = ['otype' => Term::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => Term::HASHTAG, 'term' => 'nsfw']; return DBA::exists('tag-view', ['uri-id' => $uri_id, 'name' => 'nsfw']);
return DBA::exists('term', $condition);
} }
/** /**
@ -1301,7 +1300,7 @@ class Transmitter
$data['url'] = $item['plink']; $data['url'] = $item['plink'];
$data['attributedTo'] = $item['author-link']; $data['attributedTo'] = $item['author-link'];
$data['sensitive'] = self::isSensitive($item['id']); $data['sensitive'] = self::isSensitive($item['uri-id']);
$data['context'] = self::fetchContextURLForItem($item); $data['context'] = self::fetchContextURLForItem($item);
if (!empty($item['title'])) { if (!empty($item['title'])) {

View file

@ -38,9 +38,7 @@ use Friendica\Model\Item;
use Friendica\Model\ItemURI; use Friendica\Model\ItemURI;
use Friendica\Model\ItemDeliveryData; use Friendica\Model\ItemDeliveryData;
use Friendica\Model\Mail; use Friendica\Model\Mail;
use Friendica\Model\Profile;
use Friendica\Model\Tag; use Friendica\Model\Tag;
use Friendica\Model\Term;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Network\Probe; use Friendica\Network\Probe;
use Friendica\Util\Crypto; use Friendica\Util\Crypto;
@ -114,7 +112,7 @@ class Diaspora
if (DI::config()->get("system", "relay_directly", false)) { if (DI::config()->get("system", "relay_directly", false)) {
// We distribute our stuff based on the parent to ensure that the thread will be complete // We distribute our stuff based on the parent to ensure that the thread will be complete
$parent = Item::selectFirst(['parent'], ['id' => $item_id]); $parent = Item::selectFirst(['uri-id'], ['id' => $item_id]);
if (!DBA::isResult($parent)) { if (!DBA::isResult($parent)) {
return; return;
} }
@ -126,12 +124,12 @@ class Diaspora
} }
// All tags of the current post // All tags of the current post
$condition = ['otype' => Term::OBJECT_TYPE_POST, 'type' => Term::HASHTAG, 'oid' => $parent['parent']]; $tags = DBA::select('tag-view', ['term'], ['uri-id' => $parent['uri-id'], 'type' => Tag::HASHTAG]);
$tags = DBA::select('term', ['term'], $condition);
$taglist = []; $taglist = [];
while ($tag = DBA::fetch($tags)) { while ($tag = DBA::fetch($tags)) {
$taglist[] = $tag['term']; $taglist[] = $tag['name'];
} }
DBA::close($tags);
// All servers who wants content with this tag // All servers who wants content with this tag
$tagserverlist = []; $tagserverlist = [];