2018-02-09 04:49:49 +01:00
|
|
|
<?php
|
2020-02-09 16:34:23 +01:00
|
|
|
/**
|
2021-03-29 08:40:20 +02:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 16:34:23 +01:00
|
|
|
*
|
|
|
|
* @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 04:49:49 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Content\Widget;
|
|
|
|
|
2018-10-31 15:35:50 +01:00
|
|
|
use Friendica\Core\Renderer;
|
2018-07-20 14:19:26 +02:00
|
|
|
use Friendica\Database\DBA;
|
2019-12-30 23:02:20 +01:00
|
|
|
use Friendica\DI;
|
2018-10-17 21:30:41 +02:00
|
|
|
use Friendica\Model\Item;
|
2020-04-26 18:15:39 +02:00
|
|
|
use Friendica\Model\Tag;
|
2018-02-09 04:49:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TagCloud widget
|
|
|
|
*
|
|
|
|
* @author Rabuzarus
|
|
|
|
*/
|
|
|
|
class TagCloud
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Construct a tag/term cloud block for an user.
|
|
|
|
*
|
2019-01-06 22:06:53 +01: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 04:49:49 +01:00
|
|
|
*
|
|
|
|
* @return string HTML formatted output.
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-02-09 04:49:49 +01:00
|
|
|
*/
|
2020-04-26 18:15:39 +02:00
|
|
|
public static function getHTML($uid, $count = 0, $owner_id = 0, $flags = '', $type = Tag::HASHTAG)
|
2018-02-09 04:49:49 +01:00
|
|
|
{
|
|
|
|
$o = '';
|
|
|
|
$r = self::tagadelic($uid, $count, $owner_id, $flags, $type);
|
|
|
|
if (count($r)) {
|
2018-07-20 14:19:26 +02:00
|
|
|
$contact = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
|
2019-12-30 23:02:20 +01:00
|
|
|
$url = DI::baseUrl()->remove($contact['url']);
|
2018-02-09 04:49:49 +01:00
|
|
|
|
2019-01-21 22:53:03 +01:00
|
|
|
$tags = [];
|
2018-02-09 04:49:49 +01: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 17:33:35 +02:00
|
|
|
$tpl = Renderer::getMarkupTemplate('widget/tagcloud.tpl');
|
2018-10-31 15:35:50 +01:00
|
|
|
$o = Renderer::replaceMacros($tpl, [
|
2020-01-18 20:52:34 +01:00
|
|
|
'$title' => DI::l10n()->t('Tags'),
|
2018-02-09 04:49:49 +01: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 22:06:53 +01: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 04:49:49 +01:00
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @return array Alphabetical sorted array of used tags of an user.
|
|
|
|
* @throws \Exception
|
2018-02-09 04:49:49 +01:00
|
|
|
*/
|
2020-04-26 18:15:39 +02:00
|
|
|
private static function tagadelic($uid, $count = 0, $owner_id = 0, $flags = '', $type = Tag::HASHTAG)
|
2018-02-09 04:49:49 +01:00
|
|
|
{
|
2021-02-22 20:47:08 +01:00
|
|
|
$sql_options = Item::getPermissionsSQLByUserId($uid, 'post-user-view');
|
2018-02-09 04:49:49 +01:00
|
|
|
$limit = $count ? sprintf('LIMIT %d', intval($count)) : '';
|
|
|
|
|
|
|
|
if ($flags) {
|
|
|
|
if ($flags === 'wall') {
|
2021-02-22 20:47:08 +01:00
|
|
|
$sql_options .= ' AND `post-user-view`.`wall` ';
|
2018-02-09 04:49:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($owner_id) {
|
2021-02-22 20:47:08 +01:00
|
|
|
$sql_options .= ' AND `post-user-view`.`owner-id` = ' . intval($owner_id) . ' ';
|
2018-02-09 04:49:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch tags
|
2020-05-01 10:02:21 +02:00
|
|
|
$tag_stmt = DBA::p("SELECT `name`, COUNT(`name`) AS `total` FROM `tag-search-view`
|
2021-02-22 20:47:08 +01:00
|
|
|
LEFT JOIN `post-user-view` ON `tag-search-view`.`uri-id` = `post-user-view`.`uri-id` AND `tag-search-view`.`uid` = `post-user-view`.`uid`
|
2020-05-01 10:02:21 +02:00
|
|
|
WHERE `tag-search-view`.`uid` = ?
|
2021-02-22 20:47:08 +01:00
|
|
|
AND `post-user-view`.`visible` AND NOT `post-user-view`.`deleted`
|
2018-06-10 17:19:52 +02:00
|
|
|
$sql_options
|
2020-05-01 10:02:21 +02:00
|
|
|
GROUP BY `name` ORDER BY `total` DESC $limit",
|
|
|
|
$uid
|
2018-02-09 04:49:49 +01:00
|
|
|
);
|
2019-01-21 22:55:35 +01:00
|
|
|
if (!DBA::isResult($tag_stmt)) {
|
2018-02-09 04:49:49 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-01-21 22:55:35 +01:00
|
|
|
$r = DBA::toArray($tag_stmt);
|
|
|
|
|
2018-02-09 04:49:49 +01: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 22:51:59 +01:00
|
|
|
private static function tagCalc(array $arr)
|
2018-02-09 04:49:49 +01:00
|
|
|
{
|
|
|
|
$tags = [];
|
2021-05-22 23:45:15 +02:00
|
|
|
$min = 1000000000.0;
|
|
|
|
$max = -1000000000.0;
|
2018-02-09 04:49:49 +01:00
|
|
|
$x = 0;
|
|
|
|
|
|
|
|
if (!$arr) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($arr as $rr) {
|
2020-05-01 10:02:21 +02:00
|
|
|
$tags[$x][0] = $rr['name'];
|
2018-02-09 04:49:49 +01: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');
|
2021-05-22 23:45:15 +02:00
|
|
|
$range = max(0.01, $max - $min) * 1.0001;
|
2018-02-09 04:49:49 +01:00
|
|
|
|
|
|
|
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 22:06:53 +01:00
|
|
|
* @param string $a
|
|
|
|
* @param string $b
|
2018-02-09 04:49:49 +01: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);
|
|
|
|
}
|
|
|
|
}
|