]> git.mxchange.org Git - friendica.git/commitdiff
Usage of the new tag tables
authorMichael <heluecht@pirati.ca>
Tue, 28 Apr 2020 11:52:51 +0000 (11:52 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 28 Apr 2020 11:52:51 +0000 (11:52 +0000)
src/Model/Tag.php
src/Module/Hashtag.php
src/Module/Search/Index.php

index 1186602f549b4277d472adb0af115b88aef3968b..829567fe5f9096f221ce81544d42bbbe0471d6b1 100644 (file)
@@ -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;
+       }
 }
index 8c1dad4afd3702767e904d9ae8aa1aff226ec96d..365e77572cfa50f1227534b273ee4dfd4652e1fc 100644 (file)
@@ -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);
 
index e5ad5091d9bde69bdecec883a159efb9afd9ef95..0aba7a203d5422c544ab97bc499ec0653c991938 100644 (file)
@@ -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 = [];