]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Tag.php
Use rawContent for Special Options to avoid a protected options() method
[friendica.git] / src / Model / Tag.php
index 8f132e82f3614168b91812480bc21c16f6a17173..3020a2f2934839e0108779bda564314752e09bcb 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 namespace Friendica\Model;
 
 use Friendica\Content\Text\BBCode;
-use Friendica\Core\Cache\Duration;
+use Friendica\Core\Cache\Enum\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\DateTimeFormat;
 use Friendica\Util\Strings;
 
 /**
@@ -125,7 +126,9 @@ class Tag
                                // The contact wasn't found in the system (most likely some dead account)
                                // We ensure that we only store a single entry by overwriting the previous name
                                Logger::info('Contact not found, updating tag', ['url' => $url, 'name' => $name]);
-                               DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url]);
+                               if (!DBA::exists('tag', ['name' => substr($name, 0, 96), 'url' => $url])) {
+                                       DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url]);
+                               }
                        }
                }
 
@@ -423,8 +426,8 @@ class Tag
                                                $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>';
+                                       $return['hashtags'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
+                                       $return['tags'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
                                        break;
                                case self::MENTION:
                                case self::EXCLUSIVE_MENTION:
@@ -433,8 +436,8 @@ class Tag
                                        } else {
                                                $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>';
+                                       $return['mentions'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
+                                       $return['tags'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
                                        break;
                                case self::IMPLICIT_MENTION:
                                        $return['implicit_mentions'][] = $prefix . $tag['name'];
@@ -516,6 +519,23 @@ class Tag
                }
        }
 
+       /**
+        * Fetch the blocked tags as SQL
+        *
+        * @return string 
+        */
+       private static function getBlockedSQL()
+       {
+               $blocked_txt = DI::config()->get('system', 'blocked_tags');
+               if (empty($blocked_txt)) {
+                       return '';
+               }
+
+               $blocked = explode(',', $blocked_txt);
+               array_walk($blocked, function(&$value) { $value = "'" . DBA::escape(trim($value)) . "'";});
+               return " AND NOT `name` IN (" . implode(',', $blocked) . ")";
+       }
+
        /**
         * Creates a list of the most frequent global hashtags over the given period
         *
@@ -526,11 +546,21 @@ class Tag
         */
        public static function setGlobalTrendingHashtags(int $period, int $limit = 10)
        {
-               $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
+               // Get a uri-id that is at least X hours old.
+               // We use the uri-id in the query for the hash tags since this is much faster
+               $post = Post::selectFirstThread(['uri-id'], ["`uid` = ? AND `received` < ?", 0, DateTimeFormat::utc('now - ' . $period . ' hour')],
+                       ['order' => ['received' => true]]);
+               if (empty($post['uri-id'])) {
+                       return [];
+               }
+
+               $block_sql = self::getBlockedSQL();
+
+               $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors`
                        FROM `tag-search-view`
-                       WHERE `private` = ? AND `uid` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
-                       GROUP BY `term` ORDER BY `score` DESC LIMIT ?",
-                       Item::PUBLIC, 0, $period, $limit);
+                       WHERE `private` = ? AND `uid` = ? AND `uri-id` > ? $block_sql
+                       GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
+                       Item::PUBLIC, 0, $post['uri-id'], $limit);
 
                if (DBA::isResult($tagsStmt)) {
                        $tags = DBA::toArray($tagsStmt);
@@ -569,11 +599,21 @@ class Tag
         */
        public static function setLocalTrendingHashtags(int $period, int $limit = 10)
        {
-               $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
+               // Get a uri-id that is at least X hours old.
+               // We use the uri-id in the query for the hash tags since this is much faster
+               $post = Post::selectFirstThread(['uri-id'], ["`uid` = ? AND `received` < ?", 0, DateTimeFormat::utc('now - ' . $period . ' hour')],
+                       ['order' => ['received' => true]]);
+               if (empty($post['uri-id'])) {
+                       return [];
+               }
+
+               $block_sql = self::getBlockedSQL();
+
+               $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors`
                        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);
+                       WHERE `private` = ? AND `wall` AND `origin` AND `uri-id` > ? $block_sql
+                       GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
+                       Item::PUBLIC, $post['uri-id'], $limit);
 
                if (DBA::isResult($tagsStmt)) {
                        $tags = DBA::toArray($tagsStmt);