]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget/TagCloud.php
Add license info at Friendica PHP files
[friendica.git] / src / Content / Widget / TagCloud.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Content\Widget;
23
24 use Friendica\Core\Renderer;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Item;
28
29 /**
30  * TagCloud widget
31  *
32  * @author Rabuzarus
33  */
34 class TagCloud
35 {
36         /**
37          * Construct a tag/term cloud block for an user.
38          *
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.
44          *
45          * @return string       HTML formatted output.
46          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
47          */
48         public static function getHTML($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
49         {
50                 $o = '';
51                 $r = self::tagadelic($uid, $count, $owner_id, $flags, $type);
52                 if (count($r)) {
53                         $contact = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
54                         $url = DI::baseUrl()->remove($contact['url']);
55
56                         $tags = [];
57                         foreach ($r as $rr) {
58                                 $tag['level'] = $rr[2];
59                                 $tag['url'] = $url . '?tag=' . urlencode($rr[0]);
60                                 $tag['name'] = $rr[0];
61
62                                 $tags[] = $tag;
63                         }
64
65                         $tpl = Renderer::getMarkupTemplate('widget/tagcloud.tpl');
66                         $o = Renderer::replaceMacros($tpl, [
67                                 '$title' => DI::l10n()->t('Tags'),
68                                 '$tags' => $tags
69                         ]);
70                 }
71                 return $o;
72         }
73
74         /**
75          * Get alphabetical sorted array of used tags/terms of an user including
76          * a weighting by frequency of use.
77          *
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.
83          *
84          * @return array        Alphabetical sorted array of used tags of an user.
85          * @throws \Exception
86          */
87         private static function tagadelic($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
88         {
89                 $sql_options = Item::getPermissionsSQLByUserId($uid);
90                 $limit = $count ? sprintf('LIMIT %d', intval($count)) : '';
91
92                 if ($flags) {
93                         if ($flags === 'wall') {
94                                 $sql_options .= ' AND `item`.`wall` ';
95                         }
96                 }
97
98                 if ($owner_id) {
99                         $sql_options .= ' AND `item`.`owner-id` = ' . intval($owner_id) . ' ';
100                 }
101
102                 // Fetch tags
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`
108                         $sql_options
109                         GROUP BY `term` ORDER BY `total` DESC $limit",
110                         $uid,
111                         $type,
112                         TERM_OBJ_POST
113                 );
114                 if (!DBA::isResult($tag_stmt)) {
115                         return [];
116                 }
117
118                 $r = DBA::toArray($tag_stmt);
119
120                 return self::tagCalc($r);
121         }
122
123         /**
124          * Calculate weighting of tags according to the frequency of use.
125          *
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.
128          */
129         private static function tagCalc(array $arr)
130         {
131                 $tags = [];
132                 $min = 1e9;
133                 $max = -1e9;
134                 $x = 0;
135
136                 if (!$arr) {
137                         return [];
138                 }
139
140                 foreach ($arr as $rr) {
141                         $tags[$x][0] = $rr['term'];
142                         $tags[$x][1] = log($rr['total']);
143                         $tags[$x][2] = 0;
144                         $min = min($min, $tags[$x][1]);
145                         $max = max($max, $tags[$x][1]);
146                         $x ++;
147                 }
148
149                 usort($tags, 'self::tagsSort');
150                 $range = max(.01, $max - $min) * 1.0001;
151
152                 for ($x = 0; $x < count($tags); $x ++) {
153                         $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
154                 }
155
156                 return $tags;
157         }
158
159         /**
160          * Compare function to sort tags/terms alphabetically.
161          *
162          * @param string $a
163          * @param string $b
164          *
165          * @return int
166          */
167         private static function tagsSort($a, $b)
168         {
169                 if (strtolower($a[0]) == strtolower($b[0])) {
170                         return 0;
171                 }
172                 return ((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);
173         }
174 }