3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Content\Widget;
24 use Friendica\Core\Renderer;
25 use Friendica\Database\DBA;
27 use Friendica\Model\Item;
37 * Construct a tag/term cloud block for an user.
39 * @param int $uid The user ID.
40 * @param int $count Max number of displayed tags/terms.
41 * @param int $owner_id The contact ID of the owner of the tagged items.
42 * @param string $flags Special item flags.
43 * @param int $type The tag/term type.
45 * @return string HTML formatted output.
46 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
48 public static function getHTML($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
51 $r = self::tagadelic($uid, $count, $owner_id, $flags, $type);
53 $contact = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
54 $url = DI::baseUrl()->remove($contact['url']);
58 $tag['level'] = $rr[2];
59 $tag['url'] = $url . '?tag=' . urlencode($rr[0]);
60 $tag['name'] = $rr[0];
65 $tpl = Renderer::getMarkupTemplate('widget/tagcloud.tpl');
66 $o = Renderer::replaceMacros($tpl, [
67 '$title' => DI::l10n()->t('Tags'),
75 * Get alphabetical sorted array of used tags/terms of an user including
76 * a weighting by frequency of use.
78 * @param int $uid The user ID.
79 * @param int $count Max number of displayed tags/terms.
80 * @param int $owner_id The contact id of the owner of the tagged items.
81 * @param string $flags Special item flags.
82 * @param int $type The tag/term type.
84 * @return array Alphabetical sorted array of used tags of an user.
87 private static function tagadelic($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
89 $sql_options = Item::getPermissionsSQLByUserId($uid);
90 $limit = $count ? sprintf('LIMIT %d', intval($count)) : '';
93 if ($flags === 'wall') {
94 $sql_options .= ' AND `item`.`wall` ';
99 $sql_options .= ' AND `item`.`owner-id` = ' . intval($owner_id) . ' ';
103 $tag_stmt = DBA::p("SELECT `term`, COUNT(`term`) AS `total` FROM `term`
104 LEFT JOIN `item` ON `term`.`oid` = `item`.`id`
105 WHERE `term`.`uid` = ? AND `term`.`type` = ?
106 AND `term`.`otype` = ?
107 AND `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
109 GROUP BY `term` ORDER BY `total` DESC $limit",
114 if (!DBA::isResult($tag_stmt)) {
118 $r = DBA::toArray($tag_stmt);
120 return self::tagCalc($r);
124 * Calculate weighting of tags according to the frequency of use.
126 * @param array $arr Array of tags/terms with tag/term name and total count of use.
127 * @return array Alphabetical sorted array of used tags/terms of an user.
129 private static function tagCalc(array $arr)
140 foreach ($arr as $rr) {
141 $tags[$x][0] = $rr['term'];
142 $tags[$x][1] = log($rr['total']);
144 $min = min($min, $tags[$x][1]);
145 $max = max($max, $tags[$x][1]);
149 usort($tags, 'self::tagsSort');
150 $range = max(.01, $max - $min) * 1.0001;
152 for ($x = 0; $x < count($tags); $x ++) {
153 $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
160 * Compare function to sort tags/terms alphabetically.
167 private static function tagsSort($a, $b)
169 if (strtolower($a[0]) == strtolower($b[0])) {
172 return ((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);