]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Tag.php
Merge pull request #9815 from annando/post-again
[friendica.git] / src / Model / Tag.php
index 0d6c9cfb5106b33a72599f3d0e22f331716b9c12..63a4ff492c662b32bf281db5614ba79235ef78c6 100644 (file)
@@ -24,7 +24,9 @@ namespace Friendica\Model;
 use Friendica\Content\Text\BBCode;
 use Friendica\Core\Cache\Duration;
 use Friendica\Core\Logger;
+use Friendica\Core\Protocol;
 use Friendica\Core\System;
+use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Util\Strings;
@@ -68,8 +70,8 @@ class Tag
        public static function store(int $uriid, int $type, string $name, string $url = '', $probing = true)
        {
                if ($type == self::HASHTAG) {
-                       // Remove some common "garbarge" from tags
-                       $name = trim($name, "\x00..\x20\xFF#!@,;.:'/?!^°$%".'"');
+                       // Trim Unicode non-word characters
+                       $name = preg_replace('/(^\W+)|(\W+$)/us', '', $name);
 
                        $tags = explode(self::TAG_CHARACTER[self::HASHTAG], $name);
                        if (count($tags) > 1) {
@@ -151,7 +153,7 @@ class Tag
                        }
                }
 
-               DBA::insert('post-tag', $fields, true);
+               DBA::insert('post-tag', $fields, Database::INSERT_IGNORE);
 
                Logger::info('Stored tag/mention', ['uri-id' => $uriid, 'tag-id' => $tagid, 'contact-id' => $cid, 'name' => $name, 'type' => $type, 'callstack' => System::callstack(8)]);
        }
@@ -172,7 +174,7 @@ class Tag
                        return $tag['id'];
                }
 
-               DBA::insert('tag', $fields, true);
+               DBA::insert('tag', $fields, Database::INSERT_IGNORE);
                $tid = DBA::lastInsertId();
                if (!empty($tid)) {
                        return $tid;
@@ -338,7 +340,7 @@ class Tag
        public static function createImplicitMentions(int $uri_id, int $parent_uri_id)
        {
                // Always mention the direct parent author
-               $parent = Item::selectFirst(['author-link', 'author-name'], ['uri-id' => $parent_uri_id]);
+               $parent = Post::selectFirst(['author-link', 'author-name'], ['uri-id' => $parent_uri_id]);
                self::store($uri_id, self::IMPLICIT_MENTION, $parent['author-name'], $parent['author-link']);
 
                if (DI::config()->get('system', 'disable_implicit_mentions')) {
@@ -449,7 +451,9 @@ class Tag
         */
        public static function countByTag(string $search, int $uid = 0)
        {
-               $condition = ["`name` = ? AND (NOT `private` OR (`private` AND `uid` = ?))", $search, $uid];
+               $condition = ["`name` = ? AND (NOT `private` OR (`private` AND `uid` = ?))
+                       AND `uri-id` IN (SELECT `uri-id` FROM `post-view` WHERE `network` IN (?, ?, ?, ?))",
+                       $search, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
                $params = ['group_by' => ['uri-id']];
 
                return DBA::count('tag-search-view', $condition, $params);
@@ -462,11 +466,19 @@ class Tag
         * @param integer $uid
         * @param integer $start
         * @param integer $limit
+        * @param integer $last_uriid
         * @return array with URI-ID
         */
-       public static function getURIIdListByTag(string $search, int $uid = 0, int $start = 0, int $limit = 100)
+       public static function getURIIdListByTag(string $search, int $uid = 0, int $start = 0, int $limit = 100, int $last_uriid = 0)
        {
-               $condition = ["`name` = ? AND (NOT `private` OR (`private` AND `uid` = ?))", $search, $uid];
+               $condition = ["`name` = ? AND (NOT `private` OR (`private` AND `uid` = ?))
+                       AND `uri-id` IN (SELECT `uri-id` FROM `post-view` WHERE `network` IN (?, ?, ?, ?))",
+                       $search, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
+
+               if (!empty($last_uriid)) {
+                       $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $last_uriid]);
+               }
+
                $params = [
                        'order' => ['uri-id' => true],
                        'group_by' => ['uri-id'],
@@ -510,17 +522,17 @@ class Tag
         * @return array
         * @throws \Exception
         */
-       public static function setGlobalTrendingHashtags(int $period, $limit = 10)
+       public static function setGlobalTrendingHashtags(int $period, int $limit = 10)
        {
                $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
                        FROM `tag-search-view`
-                       WHERE `private` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
+                       WHERE `private` = ? AND `uid` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
                        GROUP BY `term` ORDER BY `score` DESC LIMIT ?",
-                       Item::PUBLIC, $period, $limit);
+                       Item::PUBLIC, 0, $period, $limit);
 
                if (DBA::isResult($tagsStmt)) {
                        $tags = DBA::toArray($tagsStmt);
-                       DI::cache()->set('global_trending_tags-' . $period . '-' . $limit, $tags, Duration::HOUR);
+                       DI::cache()->set('global_trending_tags-' . $period . '-' . $limit, $tags, Duration::DAY);
                        return $tags;
                }
 
@@ -537,7 +549,7 @@ class Tag
         */
        public static function getLocalTrendingHashtags(int $period, $limit = 10)
        {
-               $tags = DI::cache()->get('local_trending_tags');
+               $tags = DI::cache()->get('local_trending_tags-' . $period . '-' . $limit);
                if (!empty($tags)) {
                        return $tags;
                } else {
@@ -553,7 +565,7 @@ class Tag
         * @return array
         * @throws \Exception
         */
-       public static function setLocalTrendingHashtags(int $period, $limit = 10)
+       public static function setLocalTrendingHashtags(int $period, int $limit = 10)
        {
                $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
                        FROM `tag-search-view`
@@ -563,7 +575,7 @@ class Tag
 
                if (DBA::isResult($tagsStmt)) {
                        $tags = DBA::toArray($tagsStmt);
-                       DI::cache()->set('local_trending_tags', $tags, Duration::HOUR);
+                       DI::cache()->set('local_trending_tags-' . $period . '-' . $limit, $tags, Duration::DAY);
                        return $tags;
                }