]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget/TagCloud.php
Merge remote-tracking branch 'upstream/develop' into write-tags
[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 use Friendica\Model\Tag;
29 use Friendica\Model\Term;
30
31 /**
32  * TagCloud widget
33  *
34  * @author Rabuzarus
35  */
36 class TagCloud
37 {
38         /**
39          * Construct a tag/term cloud block for an user.
40          *
41          * @param int    $uid      The user ID.
42          * @param int    $count    Max number of displayed tags/terms.
43          * @param int    $owner_id The contact ID of the owner of the tagged items.
44          * @param string $flags    Special item flags.
45          * @param int    $type     The tag/term type.
46          *
47          * @return string       HTML formatted output.
48          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
49          */
50         public static function getHTML($uid, $count = 0, $owner_id = 0, $flags = '', $type = Tag::HASHTAG)
51         {
52                 $o = '';
53                 $r = self::tagadelic($uid, $count, $owner_id, $flags, $type);
54                 if (count($r)) {
55                         $contact = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
56                         $url = DI::baseUrl()->remove($contact['url']);
57
58                         $tags = [];
59                         foreach ($r as $rr) {
60                                 $tag['level'] = $rr[2];
61                                 $tag['url'] = $url . '?tag=' . urlencode($rr[0]);
62                                 $tag['name'] = $rr[0];
63
64                                 $tags[] = $tag;
65                         }
66
67                         $tpl = Renderer::getMarkupTemplate('widget/tagcloud.tpl');
68                         $o = Renderer::replaceMacros($tpl, [
69                                 '$title' => DI::l10n()->t('Tags'),
70                                 '$tags' => $tags
71                         ]);
72                 }
73                 return $o;
74         }
75
76         /**
77          * Get alphabetical sorted array of used tags/terms of an user including
78          * a weighting by frequency of use.
79          *
80          * @param int    $uid      The user ID.
81          * @param int    $count    Max number of displayed tags/terms.
82          * @param int    $owner_id The contact id of the owner of the tagged items.
83          * @param string $flags    Special item flags.
84          * @param int    $type     The tag/term type.
85          *
86          * @return array        Alphabetical sorted array of used tags of an user.
87          * @throws \Exception
88          */
89         private static function tagadelic($uid, $count = 0, $owner_id = 0, $flags = '', $type = Tag::HASHTAG)
90         {
91                 $sql_options = Item::getPermissionsSQLByUserId($uid);
92                 $limit = $count ? sprintf('LIMIT %d', intval($count)) : '';
93
94                 if ($flags) {
95                         if ($flags === 'wall') {
96                                 $sql_options .= ' AND `item`.`wall` ';
97                         }
98                 }
99
100                 if ($owner_id) {
101                         $sql_options .= ' AND `item`.`owner-id` = ' . intval($owner_id) . ' ';
102                 }
103
104                 // Fetch tags
105                 $tag_stmt = DBA::p("SELECT `term`, COUNT(`term`) AS `total` FROM `term`
106                         LEFT JOIN `item` ON `term`.`oid` = `item`.`id`
107                         WHERE `term`.`uid` = ? AND `term`.`type` = ?
108                         AND `term`.`otype` = ?
109                         AND `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
110                         $sql_options
111                         GROUP BY `term` ORDER BY `total` DESC $limit",
112                         $uid,
113                         $type,
114                         Term::OBJECT_TYPE_POST
115                 );
116                 if (!DBA::isResult($tag_stmt)) {
117                         return [];
118                 }
119
120                 $r = DBA::toArray($tag_stmt);
121
122                 return self::tagCalc($r);
123         }
124
125         /**
126          * Calculate weighting of tags according to the frequency of use.
127          *
128          * @param array $arr Array of tags/terms with tag/term name and total count of use.
129          * @return array     Alphabetical sorted array of used tags/terms of an user.
130          */
131         private static function tagCalc(array $arr)
132         {
133                 $tags = [];
134                 $min = 1e9;
135                 $max = -1e9;
136                 $x = 0;
137
138                 if (!$arr) {
139                         return [];
140                 }
141
142                 foreach ($arr as $rr) {
143                         $tags[$x][0] = $rr['term'];
144                         $tags[$x][1] = log($rr['total']);
145                         $tags[$x][2] = 0;
146                         $min = min($min, $tags[$x][1]);
147                         $max = max($max, $tags[$x][1]);
148                         $x ++;
149                 }
150
151                 usort($tags, 'self::tagsSort');
152                 $range = max(.01, $max - $min) * 1.0001;
153
154                 for ($x = 0; $x < count($tags); $x ++) {
155                         $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
156                 }
157
158                 return $tags;
159         }
160
161         /**
162          * Compare function to sort tags/terms alphabetically.
163          *
164          * @param string $a
165          * @param string $b
166          *
167          * @return int
168          */
169         private static function tagsSort($a, $b)
170         {
171                 if (strtolower($a[0]) == strtolower($b[0])) {
172                         return 0;
173                 }
174                 return ((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);
175         }
176 }