]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Tag.php
Merge pull request #10093 from urbalazs/copyright-2021
[friendica.git] / src / Model / Tag.php
index c63ba0a83940e281b20066424f2082858972474a..796cc13f8872a6653b0c925252862b7e43b6e683 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -125,7 +125,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]);
+                               }
                        }
                }
 
@@ -455,12 +457,11 @@ class Tag
         */
        public static function countByTag(string $search, int $uid = 0)
        {
-               $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']];
+               $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`))
+                       AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
+                       $search, 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0];
 
-               return DBA::count('tag-search-view', $condition, $params);
+               return DBA::count('tag-search-view', $condition);
        }
 
        /**
@@ -475,9 +476,9 @@ class Tag
         */
        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` = ?))
-                       AND `uri-id` IN (SELECT `uri-id` FROM `post-view` WHERE `network` IN (?, ?, ?, ?))",
-                       $search, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
+               $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`))
+                       AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
+                       $search, 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0];
 
                if (!empty($last_uriid)) {
                        $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $last_uriid]);
@@ -485,7 +486,6 @@ class Tag
 
                $params = [
                        'order' => ['uri-id' => true],
-                       'group_by' => ['uri-id'],
                        'limit' => [$start, $limit]
                ];
 
@@ -518,6 +518,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
         *
@@ -528,10 +545,12 @@ class Tag
         */
        public static function setGlobalTrendingHashtags(int $period, int $limit = 10)
        {
-               $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
+               $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 ?",
+                       WHERE `private` = ? AND `uid` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR) $block_sql
+                       GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
                        Item::PUBLIC, 0, $period, $limit);
 
                if (DBA::isResult($tagsStmt)) {
@@ -571,10 +590,12 @@ class Tag
         */
        public static function setLocalTrendingHashtags(int $period, int $limit = 10)
        {
-               $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
+               $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 ?",
+                       WHERE `private` = ? AND `wall` AND `origin` AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR) $block_sql
+                       GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
                        Item::PUBLIC, $period, $limit);
 
                if (DBA::isResult($tagsStmt)) {