X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FTerm.php;h=868f2bf0532380ee8bef06655f8e5e8bd6a594df;hb=7b7132971a64a92a685a5fd860fe4709dce1765a;hp=58c322365958f4871d6865889a92171257af2241;hpb=fa5525d0848af855ff770b206d9ff85911128a5d;p=friendica.git
diff --git a/src/Model/Term.php b/src/Model/Term.php
index 58c3223659..868f2bf053 100644
--- a/src/Model/Term.php
+++ b/src/Model/Term.php
@@ -1,11 +1,30 @@
.
+ *
*/
+
namespace Friendica\Model;
-use Friendica\Core\System;
+use Friendica\Core\Cache\Duration;
+use Friendica\Core\Logger;
use Friendica\Database\DBA;
+use Friendica\DI;
use Friendica\Util\Strings;
/**
@@ -14,8 +33,6 @@ use Friendica\Util\Strings;
* This Model class handles term table interactions.
* This tables stores relevant terms related to posts, photos and searches, like hashtags, mentions and
* user-applied categories.
- *
- * @package Friendica\Model
*/
class Term
{
@@ -46,6 +63,95 @@ class Term
const OBJECT_TYPE_POST = 1;
const OBJECT_TYPE_PHOTO = 2;
+ /**
+ * Returns a list of the most frequent global hashtags over the given period
+ *
+ * @param int $period Period in hours to consider posts
+ * @return array
+ * @throws \Exception
+ */
+ public static function getGlobalTrendingHashtags(int $period, $limit = 10)
+ {
+ $tags = DI::cache()->get('global_trending_tags');
+
+ if (!$tags) {
+ $tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
+ FROM `term` t
+ JOIN `item` i ON i.`id` = t.`oid` AND i.`uid` = t.`uid`
+ JOIN `thread` ON `thread`.`iid` = i.`id`
+ WHERE `thread`.`visible`
+ AND NOT `thread`.`deleted`
+ AND NOT `thread`.`moderated`
+ AND `thread`.`private` = ?
+ AND t.`uid` = 0
+ AND t.`otype` = ?
+ AND t.`type` = ?
+ AND t.`term` != ''
+ AND i.`received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
+ GROUP BY `term`
+ ORDER BY `score` DESC
+ LIMIT ?",
+ Item::PUBLIC,
+ Term::OBJECT_TYPE_POST,
+ Term::HASHTAG,
+ $period,
+ $limit
+ );
+
+ if (DBA::isResult($tagsStmt)) {
+ $tags = DBA::toArray($tagsStmt);
+ DI::cache()->set('global_trending_tags', $tags, Duration::HOUR);
+ }
+ }
+
+ return $tags ?: [];
+ }
+
+ /**
+ * Returns a list of the most frequent local hashtags over the given period
+ *
+ * @param int $period Period in hours to consider posts
+ * @return array
+ * @throws \Exception
+ */
+ public static function getLocalTrendingHashtags(int $period, $limit = 10)
+ {
+ $tags = DI::cache()->get('local_trending_tags');
+
+ if (!$tags) {
+ $tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
+ FROM `term` t
+ JOIN `item` i ON i.`id` = t.`oid` AND i.`uid` = t.`uid`
+ JOIN `thread` ON `thread`.`iid` = i.`id`
+ WHERE `thread`.`visible`
+ AND NOT `thread`.`deleted`
+ AND NOT `thread`.`moderated`
+ AND `thread`.`private` = ?
+ AND `thread`.`wall`
+ AND `thread`.`origin`
+ AND t.`otype` = ?
+ AND t.`type` = ?
+ AND t.`term` != ''
+ AND i.`received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
+ GROUP BY `term`
+ ORDER BY `score` DESC
+ LIMIT ?",
+ Item::PUBLIC,
+ Term::OBJECT_TYPE_POST,
+ Term::HASHTAG,
+ $period,
+ $limit
+ );
+
+ if (DBA::isResult($tagsStmt)) {
+ $tags = DBA::toArray($tagsStmt);
+ DI::cache()->set('local_trending_tags', $tags, Duration::HOUR);
+ }
+ }
+
+ return $tags ?: [];
+ }
+
/**
* Generates the legacy item.tag field comma-separated BBCode string from an item ID.
* Includes only hashtags, implicit and explicit mentions.
@@ -118,9 +224,9 @@ class Term
*/
public static function insertFromTagFieldByItemId($item_id, $tag_str)
{
- $profile_base = System::baseUrl();
+ $profile_base = DI::baseUrl();
$profile_data = parse_url($profile_base);
- $profile_path = defaults($profile_data, 'path', '');
+ $profile_path = $profile_data['path'] ?? '';
$profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
$profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
@@ -335,7 +441,7 @@ class Term
'implicit_mentions' => [],
];
- $searchpath = System::baseUrl() . "/search?tag=";
+ $searchpath = DI::baseUrl() . "/search?tag=";
$taglist = DBA::select(
'term',
@@ -357,13 +463,13 @@ class Term
$item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
}
- $return['hashtags'][] = $prefix . '' . $tag['term'] . '';
- $return['tags'][] = $prefix . '' . $tag['term'] . '';
+ $return['hashtags'][] = $prefix . '' . htmlspecialchars($tag['term']) . '';
+ $return['tags'][] = $prefix . '' . htmlspecialchars($tag['term']) . '';
break;
case self::MENTION:
$tag['url'] = Contact::magicLink($tag['url']);
- $return['mentions'][] = $prefix . '' . $tag['term'] . '';
- $return['tags'][] = $prefix . '' . $tag['term'] . '';
+ $return['mentions'][] = $prefix . '' . htmlspecialchars($tag['term']) . '';
+ $return['tags'][] = $prefix . '' . htmlspecialchars($tag['term']) . '';
break;
case self::IMPLICIT_MENTION:
$return['implicit_mentions'][] = $prefix . $tag['term'];
@@ -403,7 +509,7 @@ class Term
{
$tag_chars = [];
foreach ($types as $type) {
- if (isset(self::TAG_CHARACTER[$type])) {
+ if (array_key_exists($type, self::TAG_CHARACTER)) {
$tag_chars[] = self::TAG_CHARACTER[$type];
}
}