2018-02-09 03:49:49 +00:00
|
|
|
<?php
|
2020-02-09 15:34:23 +00:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
2018-02-09 03:49:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Content\Widget;
|
|
|
|
|
2018-10-31 14:35:50 +00:00
|
|
|
use Friendica\Core\Renderer;
|
2018-07-20 12:19:26 +00:00
|
|
|
use Friendica\Database\DBA;
|
2019-12-30 22:02:20 +00:00
|
|
|
use Friendica\DI;
|
2018-10-17 19:30:41 +00:00
|
|
|
use Friendica\Model\Item;
|
2020-04-26 16:15:39 +00:00
|
|
|
use Friendica\Model\Tag;
|
2018-02-09 03:49:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TagCloud widget
|
|
|
|
*
|
|
|
|
* @author Rabuzarus
|
|
|
|
*/
|
|
|
|
class TagCloud
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Construct a tag/term cloud block for an user.
|
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param int $uid The user ID.
|
|
|
|
* @param int $count Max number of displayed tags/terms.
|
|
|
|
* @param int $owner_id The contact ID of the owner of the tagged items.
|
|
|
|
* @param string $flags Special item flags.
|
|
|
|
* @param int $type The tag/term type.
|
2018-02-09 03:49:49 +00:00
|
|
|
*
|
|
|
|
* @return string HTML formatted output.
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-02-09 03:49:49 +00:00
|
|
|
*/
|
2020-04-26 16:15:39 +00:00
|
|
|
public static function getHTML($uid, $count = 0, $owner_id = 0, $flags = '', $type = Tag::HASHTAG)
|
2018-02-09 03:49:49 +00:00
|
|
|
{
|
|
|
|
$o = '';
|
|
|
|
$r = self::tagadelic($uid, $count, $owner_id, $flags, $type);
|
|
|
|
if (count($r)) {
|
2018-07-20 12:19:26 +00:00
|
|
|
$contact = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
|
2019-12-30 22:02:20 +00:00
|
|
|
$url = DI::baseUrl()->remove($contact['url']);
|
2018-02-09 03:49:49 +00:00
|
|
|
|
2019-01-21 21:53:03 +00:00
|
|
|
$tags = [];
|
2018-02-09 03:49:49 +00:00
|
|
|
foreach ($r as $rr) {
|
|
|
|
$tag['level'] = $rr[2];
|
|
|
|
$tag['url'] = $url . '?tag=' . urlencode($rr[0]);
|
|
|
|
$tag['name'] = $rr[0];
|
|
|
|
|
|
|
|
$tags[] = $tag;
|
|
|
|
}
|
|
|
|
|
2019-05-18 15:33:35 +00:00
|
|
|
$tpl = Renderer::getMarkupTemplate('widget/tagcloud.tpl');
|
2018-10-31 14:35:50 +00:00
|
|
|
$o = Renderer::replaceMacros($tpl, [
|
2020-01-18 19:52:34 +00:00
|
|
|
'$title' => DI::l10n()->t('Tags'),
|
2018-02-09 03:49:49 +00:00
|
|
|
'$tags' => $tags
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
return $o;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get alphabetical sorted array of used tags/terms of an user including
|
|
|
|
* a weighting by frequency of use.
|
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param int $uid The user ID.
|
|
|
|
* @param int $count Max number of displayed tags/terms.
|
|
|
|
* @param int $owner_id The contact id of the owner of the tagged items.
|
|
|
|
* @param string $flags Special item flags.
|
|
|
|
* @param int $type The tag/term type.
|
2018-02-09 03:49:49 +00:00
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @return array Alphabetical sorted array of used tags of an user.
|
|
|
|
* @throws \Exception
|
2018-02-09 03:49:49 +00:00
|
|
|
*/
|
2020-04-26 16:15:39 +00:00
|
|
|
private static function tagadelic($uid, $count = 0, $owner_id = 0, $flags = '', $type = Tag::HASHTAG)
|
2018-02-09 03:49:49 +00:00
|
|
|
{
|
2021-01-18 20:19:13 +00:00
|
|
|
$sql_options = Item::getPermissionsSQLByUserId($uid, 'post-view');
|
2018-02-09 03:49:49 +00:00
|
|
|
$limit = $count ? sprintf('LIMIT %d', intval($count)) : '';
|
|
|
|
|
|
|
|
if ($flags) {
|
|
|
|
if ($flags === 'wall') {
|
2021-01-17 20:32:13 +00:00
|
|
|
$sql_options .= ' AND `post-view`.`wall` ';
|
2018-02-09 03:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($owner_id) {
|
2021-01-17 20:32:13 +00:00
|
|
|
$sql_options .= ' AND `post-view`.`owner-id` = ' . intval($owner_id) . ' ';
|
2018-02-09 03:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch tags
|
2020-05-01 08:02:21 +00:00
|
|
|
$tag_stmt = DBA::p("SELECT `name`, COUNT(`name`) AS `total` FROM `tag-search-view`
|
2021-01-17 20:32:13 +00:00
|
|
|
LEFT JOIN `post-view` ON `tag-search-view`.`uri-id` = `post-view`.`uri-id`
|
2020-05-01 08:02:21 +00:00
|
|
|
WHERE `tag-search-view`.`uid` = ?
|
2021-02-13 19:56:03 +00:00
|
|
|
AND `post-view`.`visible` AND NOT `post-view`.`deleted`
|
2018-06-10 15:19:52 +00:00
|
|
|
$sql_options
|
2020-05-01 08:02:21 +00:00
|
|
|
GROUP BY `name` ORDER BY `total` DESC $limit",
|
|
|
|
$uid
|
2018-02-09 03:49:49 +00:00
|
|
|
);
|
2019-01-21 21:55:35 +00:00
|
|
|
if (!DBA::isResult($tag_stmt)) {
|
2018-02-09 03:49:49 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-01-21 21:55:35 +00:00
|
|
|
$r = DBA::toArray($tag_stmt);
|
|
|
|
|
2018-02-09 03:49:49 +00:00
|
|
|
return self::tagCalc($r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate weighting of tags according to the frequency of use.
|
|
|
|
*
|
|
|
|
* @param array $arr Array of tags/terms with tag/term name and total count of use.
|
|
|
|
* @return array Alphabetical sorted array of used tags/terms of an user.
|
|
|
|
*/
|
2019-01-21 21:51:59 +00:00
|
|
|
private static function tagCalc(array $arr)
|
2018-02-09 03:49:49 +00:00
|
|
|
{
|
|
|
|
$tags = [];
|
|
|
|
$min = 1e9;
|
|
|
|
$max = -1e9;
|
|
|
|
$x = 0;
|
|
|
|
|
|
|
|
if (!$arr) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($arr as $rr) {
|
2020-05-01 08:02:21 +00:00
|
|
|
$tags[$x][0] = $rr['name'];
|
2018-02-09 03:49:49 +00:00
|
|
|
$tags[$x][1] = log($rr['total']);
|
|
|
|
$tags[$x][2] = 0;
|
|
|
|
$min = min($min, $tags[$x][1]);
|
|
|
|
$max = max($max, $tags[$x][1]);
|
|
|
|
$x ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
usort($tags, 'self::tagsSort');
|
|
|
|
$range = max(.01, $max - $min) * 1.0001;
|
|
|
|
|
|
|
|
for ($x = 0; $x < count($tags); $x ++) {
|
|
|
|
$tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare function to sort tags/terms alphabetically.
|
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param string $a
|
|
|
|
* @param string $b
|
2018-02-09 03:49:49 +00:00
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private static function tagsSort($a, $b)
|
|
|
|
{
|
|
|
|
if (strtolower($a[0]) == strtolower($b[0])) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);
|
|
|
|
}
|
|
|
|
}
|