]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Tag.php
Fallback to database lock if locking fails
[friendica.git] / src / Model / Tag.php
index 7eb475d727009e8642524895ed2b8a90597ead1c..f947b9f898429744b2f427e3dd1fa0fd4619499f 100644 (file)
@@ -440,6 +440,21 @@ class Tag
                return $return;
        }
 
+       /**
+        * Counts posts for given tag
+        *
+        * @param string $search
+        * @param integer $uid
+        * @return integer number of posts
+        */
+       public static function countByTag(string $search, int $uid = 0)
+       {
+               $condition = ["`name` = ? AND (NOT `private` OR (`private` AND `uid` = ?))", $search, $uid];
+               $params = ['group_by' => ['uri-id']];
+
+               return DBA::count('tag-search-view', $condition, $params);
+       }
+
        /**
         * Search posts for given tag
         *
@@ -447,11 +462,17 @@ 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];
+
+               if (!empty($last_uriid)) {
+                       $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $last_uriid]);
+               }
+
                $params = [
                        'order' => ['uri-id' => true],
                        'group_by' => ['uri-id'],
@@ -473,54 +494,86 @@ class Tag
         * Returns a list of the most frequent global hashtags over the given period
         *
         * @param int $period Period in hours to consider posts
+        * @param int $limit  Number of returned tags
         * @return array
         * @throws \Exception
         */
        public static function getGlobalTrendingHashtags(int $period, $limit = 10)
        {
-               $tags = DI::cache()->get('global_trending_tags');
-
-               if (empty($tags)) {
-                       $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
-                               FROM `tag-search-view`
-                               WHERE `private` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
-                               GROUP BY `term` ORDER BY `score` DESC LIMIT ?",
-                               Item::PUBLIC, $period, $limit);
-
-                       if (DBA::isResult($tagsStmt)) {
-                               $tags = DBA::toArray($tagsStmt);
-                               DI::cache()->set('global_trending_tags', $tags, Duration::HOUR);
-                       }
+               $tags = DI::cache()->get('global_trending_tags-' . $period . '-' . $limit);
+               if (!empty($tags)) {
+                       return $tags;
+               } else {
+                       return self::setGlobalTrendingHashtags($period, $limit);
+               }
+       }
+
+       /**
+        * Creates a list of the most frequent global hashtags over the given period
+        *
+        * @param int $period Period in hours to consider posts
+        * @param int $limit  Number of returned tags
+        * @return array
+        * @throws \Exception
+        */
+       public static function setGlobalTrendingHashtags(int $period, $limit = 10)
+       {
+               $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
+                       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);
+
+               if (DBA::isResult($tagsStmt)) {
+                       $tags = DBA::toArray($tagsStmt);
+                       DI::cache()->set('global_trending_tags-' . $period . '-' . $limit, $tags, Duration::DAY);
+                       return $tags;
                }
 
-               return $tags ?: [];
+               return [];
        }
 
        /**
         * Returns a list of the most frequent local hashtags over the given period
         *
         * @param int $period Period in hours to consider posts
+        * @param int $limit  Number of returned tags
         * @return array
         * @throws \Exception
         */
        public static function getLocalTrendingHashtags(int $period, $limit = 10)
        {
-               $tags = DI::cache()->get('local_trending_tags');
-
-               if (empty($tags)) {
-                       $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
-                               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);
-
-                       if (DBA::isResult($tagsStmt)) {
-                               $tags = DBA::toArray($tagsStmt);
-                               DI::cache()->set('local_trending_tags', $tags, Duration::HOUR);
-                       }
+               $tags = DI::cache()->get('local_trending_tags-' . $period . '-' . $limit);
+               if (!empty($tags)) {
+                       return $tags;
+               } else {
+                       return self::setLocalTrendingHashtags($period, $limit);
+               }
+       }
+
+       /**
+        * Returns a list of the most frequent local hashtags over the given period
+        *
+        * @param int $period Period in hours to consider posts
+        * @param int $limit  Number of returned tags
+        * @return array
+        * @throws \Exception
+        */
+       public static function setLocalTrendingHashtags(int $period, $limit = 10)
+       {
+               $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
+                       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);
+
+               if (DBA::isResult($tagsStmt)) {
+                       $tags = DBA::toArray($tagsStmt);
+                       DI::cache()->set('local_trending_tags-' . $period . '-' . $limit, $tags, Duration::DAY);
+                       return $tags;
                }
 
-               return $tags ?: [];
+               return [];
        }
 
        /**