Merge pull request #7481 from MrPetovan/task/7473-trending-tags

Add trending tags widget to community page
This commit is contained in:
Philipp 2019-08-06 14:54:42 +02:00 committed by GitHub
commit c6845a1fe4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 666 additions and 399 deletions

View File

@ -4,15 +4,16 @@
*/ */
use Friendica\App; use Friendica\App;
use Friendica\Content\Feature;
use Friendica\Content\Nav; use Friendica\Content\Nav;
use Friendica\Content\Pager; use Friendica\Content\Pager;
use Friendica\Content\Widget\TrendingTags;
use Friendica\Core\ACL; use Friendica\Core\ACL;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\PConfig; use Friendica\Core\PConfig;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\User; use Friendica\Model\User;
@ -198,6 +199,14 @@ function community_content(App $a, $update = 0)
$o .= $pager->renderMinimal(count($r)); $o .= $pager->renderMinimal(count($r));
} }
if (empty($a->page['aside'])) {
$a->page['aside'] = '';
}
if (Feature::isEnabled(local_user(), 'trending_tags')) {
$a->page['aside'] .= TrendingTags::getHTML($content);
}
$t = Renderer::getMarkupTemplate("community.tpl"); $t = Renderer::getMarkupTemplate("community.tpl");
return Renderer::replaceMacros($t, [ return Renderer::replaceMacros($t, [
'$content' => $o, '$content' => $o,

View File

@ -84,6 +84,7 @@ class Feature
['multi_profiles', L10n::t('Multiple Profiles'), L10n::t('Ability to create multiple profiles'), false, Config::get('feature_lock', 'multi_profiles', false)], ['multi_profiles', L10n::t('Multiple Profiles'), L10n::t('Ability to create multiple profiles'), false, Config::get('feature_lock', 'multi_profiles', false)],
['photo_location', L10n::t('Photo Location'), L10n::t("Photo metadata is normally stripped. This extracts the location \x28if present\x29 prior to stripping metadata and links it to a map."), false, Config::get('feature_lock', 'photo_location', false)], ['photo_location', L10n::t('Photo Location'), L10n::t("Photo metadata is normally stripped. This extracts the location \x28if present\x29 prior to stripping metadata and links it to a map."), false, Config::get('feature_lock', 'photo_location', false)],
['export_calendar', L10n::t('Export Public Calendar'), L10n::t('Ability for visitors to download the public calendar'), false, Config::get('feature_lock', 'export_calendar', false)], ['export_calendar', L10n::t('Export Public Calendar'), L10n::t('Ability for visitors to download the public calendar'), false, Config::get('feature_lock', 'export_calendar', false)],
['trending_tags', L10n::t('Trending Tags'), L10n::t('Show a community page widget with a list of the most popular tags in recent public posts.'), false, Config::get('feature_lock', 'trending_tags', false)],
], ],
// Post composition // Post composition

View File

@ -0,0 +1,41 @@
<?php
namespace Friendica\Content\Widget;
use Friendica\Core\Cache;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Database\DBA;
use Friendica\Model\Term;
/**
* Trending tags aside widget for the community pages, handles both local and global scopes
*
* @package Friendica\Content\Widget
*/
class TrendingTags
{
/**
* @param string $content 'global' (all posts) or 'local' (this node's posts only)
* @param int $period Period in hours to consider posts
* @return string
* @throws \Exception
*/
public static function getHTML($content = 'global', int $period = 24)
{
if ($content == 'local') {
$tags = Term::getLocalTrendingHashtags($period, 20);
} else {
$tags = Term::getGlobalTrendingHashtags($period, 20);
}
$tpl = Renderer::getMarkupTemplate('widget/trending_tags.tpl');
$o = Renderer::replaceMacros($tpl, [
'$title' => L10n::tt('Trending Tags (last %d hour)', 'Trending Tags (last %d hours)', $period),
'$more' => L10n::t('More Trending Tags'),
'$tags' => $tags,
]);
return $o;
}
}

View File

@ -4,6 +4,7 @@
*/ */
namespace Friendica\Model; namespace Friendica\Model;
use Friendica\Core\Cache;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
@ -47,6 +48,94 @@ class Term
const OBJECT_TYPE_POST = 1; const OBJECT_TYPE_POST = 1;
const OBJECT_TYPE_PHOTO = 2; 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 = 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 NOT `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 ?",
Term::OBJECT_TYPE_POST,
Term::HASHTAG,
$period,
$limit
);
if (DBA::isResult($tags)) {
$tags = DBA::toArray($tagsStmt);
Cache::set('global_trending_tags', $tags, Cache::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 = 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`
JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
WHERE `thread`.`visible`
AND NOT `thread`.`deleted`
AND NOT `thread`.`moderated`
AND NOT `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 ?",
Term::OBJECT_TYPE_POST,
Term::HASHTAG,
$period,
$limit
);
if (DBA::isResult($tags)) {
$tags = DBA::toArray($tagsStmt);
Cache::set('local_trending_tags', $tags, Cache::HOUR);
}
}
return $tags ?: [];
}
/** /**
* Generates the legacy item.tag field comma-separated BBCode string from an item ID. * Generates the legacy item.tag field comma-separated BBCode string from an item ID.
* Includes only hashtags, implicit and explicit mentions. * Includes only hashtags, implicit and explicit mentions.

View File

@ -171,7 +171,7 @@ class Compose extends BaseModule
'$posttype' => $posttype, '$posttype' => $posttype,
'$type' => $type, '$type' => $type,
'$wall' => $wall, '$wall' => $wall,
'$default' => L10n::t(''), '$default' => '',
'$mylink' => $a->removeBaseURL($a->contact['url']), '$mylink' => $a->removeBaseURL($a->contact['url']),
'$mytitle' => L10n::t('This is you'), '$mytitle' => L10n::t('This is you'),
'$myphoto' => $a->removeBaseURL($a->contact['thumb']), '$myphoto' => $a->removeBaseURL($a->contact['thumb']),

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,18 @@
<div id="trending-tags-sidebar" class="widget">
<h3>{{$title}}</h3>
<ul>
{{section name=ol loop=$tags max=10}}
<li><a href="search?tag={{$tags[ol].term}}">{{$tags[ol].term}}</a></li>
{{/section}}
</ul>
{{if $tags|count > 10}}
<details>
<summary>{{$more}}</summary>
<ul>
{{section name=ul loop=$tags start=10}}
<li><a href="search?tag={{$tags[ul].term}}">{{$tags[ul].term}}</a></li>
{{/section}}
</ul>
</details>
{{/if}}
</div>