From c2a9b3b9e95260a73c82704620e59f2a08114911 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 28 Apr 2020 11:52:51 +0000 Subject: [PATCH] Usage of the new tag tables --- src/Model/Tag.php | 50 +++++++++++++++++++++++++++++++++---- src/Module/Hashtag.php | 10 ++------ src/Module/Search/Index.php | 26 +++---------------- 3 files changed, 51 insertions(+), 35 deletions(-) diff --git a/src/Model/Tag.php b/src/Model/Tag.php index 1186602f54..829567fe5f 100644 --- a/src/Model/Tag.php +++ b/src/Model/Tag.php @@ -68,7 +68,19 @@ class Tag */ public static function store(int $uriid, int $type, string $name, string $url = '', $probing = true) { - $name = trim($name, "\x00..\x20\xFF#!@"); + if ($type = self::HASHTAG) { + // Remove some common "garbarge" from tags + $name = trim($name, "\x00..\x20\xFF#!@,;.:'/?!^°$%".'"'); + + $tags = explode(self::TAG_CHARACTER[self::HASHTAG], $name); + if (count($tags) > 1) { + foreach ($tags as $tag) { + self::store($uriid, $type, $tag, $url, $probing); + } + return; + } + } + if (empty($name)) { return; } @@ -76,7 +88,7 @@ class Tag $cid = 0; $tagid = 0; - if (in_array($type, [Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION])) { + if (in_array($type, [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION])) { if (empty($url)) { // No mention without a contact url return; @@ -115,7 +127,7 @@ class Tag if (empty($cid)) { $fields = ['name' => substr($name, 0, 96), 'url' => '']; - if (($type != Tag::HASHTAG) && !empty($url) && ($url != $name)) { + if (($type != self::HASHTAG) && !empty($url) && ($url != $name)) { $fields['url'] = strtolower($url); } @@ -135,9 +147,9 @@ class Tag $fields = ['uri-id' => $uriid, 'type' => $type, 'tid' => $tagid, 'cid' => $cid]; - if (in_array($type, [Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION])) { + if (in_array($type, [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION])) { $condition = $fields; - $condition['type'] = [Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION]; + $condition['type'] = [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]; if (DBA::exists('post-tag', $condition)) { Logger::info('Tag already exists', $fields); return; @@ -376,4 +388,32 @@ class Tag return $return; } + /** + * Search posts for given tag + * + * @param string $search + * @param integer $uid + * @param integer $start + * @param integer $limit + * @return array with URI-ID + */ + public static function getURIIdListForTag(string $search, int $uid = 0, int $start = 0, int $limit = 100) + { + $condition = ["`name` = ? AND (NOT `private` OR (`private` AND `uid` = ?))", $search, $uid]; + $params = [ + 'order' => ['uri-id' => true], + 'group_by' => ['uri-id'], + 'limit' => [$start, $limit] + ]; + + $tags = DBA::select('tag-search-view', ['uri-id'], $condition, $params); + + $uriids = []; + while ($tag = DBA::fetch($tags)) { + $uriids[] = $tag['uri-id']; + } + DBA::close($tags); + + return $uriids; + } } diff --git a/src/Module/Hashtag.php b/src/Module/Hashtag.php index 8c1dad4afd..365e77572c 100644 --- a/src/Module/Hashtag.php +++ b/src/Module/Hashtag.php @@ -25,15 +25,12 @@ use Friendica\BaseModule; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Util\Strings; -use Friendica\Model\Tag; -use Friendica\Model\Term; /** * Hashtag module. */ class Hashtag extends BaseModule { - public static function content(array $parameters = []) { $result = []; @@ -43,12 +40,9 @@ class Hashtag extends BaseModule System::jsonExit($result); } - $taglist = DBA::p("SELECT DISTINCT(`term`) FROM `term` WHERE `term` LIKE ? AND `type` = ? ORDER BY `term`", - $t . '%', - intval(Tag::HASHTAG) - ); + $taglist = DBA::select('tag', ['name'], ["`name` LIKE ?", $t . "%"], ['order' => ['name'], 'limit' => 100]); while ($tag = DBA::fetch($taglist)) { - $result[] = ['text' => $tag['term']]; + $result[] = ['text' => $tag['name']]; } DBA::close($taglist); diff --git a/src/Module/Search/Index.php b/src/Module/Search/Index.php index e5ad5091d9..0aba7a203d 100644 --- a/src/Module/Search/Index.php +++ b/src/Module/Search/Index.php @@ -34,7 +34,6 @@ use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\Item; use Friendica\Model\Tag; -use Friendica\Model\Term; use Friendica\Module\BaseSearch; use Friendica\Network\HTTPException; use Friendica\Util\Strings; @@ -150,28 +149,11 @@ class Index extends BaseSearch if ($tag) { Logger::info('Start tag search.', ['q' => $search]); + $uriids = Tag::getURIIdListForTag($search, local_user(), $pager->getStart(), $pager->getItemsPerPage()); - $condition = [ - "(`uid` = 0 OR (`uid` = ? AND NOT `global`)) - AND `otype` = ? AND `type` = ? AND `term` = ?", - local_user(), Term::OBJECT_TYPE_POST, Tag::HASHTAG, $search - ]; - $params = [ - 'order' => ['received' => true], - 'limit' => [$pager->getStart(), $pager->getItemsPerPage()] - ]; - $terms = DBA::select('term', ['oid'], $condition, $params); - - $itemids = []; - while ($term = DBA::fetch($terms)) { - $itemids[] = $term['oid']; - } - - DBA::close($terms); - - if (!empty($itemids)) { - $params = ['order' => ['id' => true]]; - $items = Item::selectForUser(local_user(), [], ['id' => $itemids], $params); + if (!empty($uriids)) { + $params = ['order' => ['id' => true], 'group_by' => ['uri-id']]; + $items = Item::selectForUser(local_user(), [], ['uri-id' => $uriids], $params); $r = Item::inArray($items); } else { $r = [];