Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.0KB

  1. <?php
  2. /*
  3. * @file src/Content/Widget/TagCloud.php
  4. */
  5. namespace Friendica\Content\Widget;
  6. use Friendica\Core\L10n;
  7. use Friendica\Core\Renderer;
  8. use Friendica\Core\System;
  9. use Friendica\Database\DBA;
  10. use Friendica\Model\Item;
  11. use Friendica\Util\Security;
  12. /**
  13. * TagCloud widget
  14. *
  15. * @author Rabuzarus
  16. */
  17. class TagCloud
  18. {
  19. /**
  20. * Construct a tag/term cloud block for an user.
  21. *
  22. * @brief Construct a tag/term cloud block for an user.
  23. * @param int $uid The user ID.
  24. * @param int $count Max number of displayed tags/terms.
  25. * @param int $owner_id The contact ID of the owner of the tagged items.
  26. * @param string $flags Special item flags.
  27. * @param int $type The tag/term type.
  28. *
  29. * @return string HTML formatted output.
  30. */
  31. public static function getHTML($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
  32. {
  33. $o = '';
  34. $r = self::tagadelic($uid, $count, $owner_id, $flags, $type);
  35. if (count($r)) {
  36. $contact = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
  37. $url = System::removedBaseUrl($contact['url']);
  38. foreach ($r as $rr) {
  39. $tag['level'] = $rr[2];
  40. $tag['url'] = $url . '?tag=' . urlencode($rr[0]);
  41. $tag['name'] = $rr[0];
  42. $tags[] = $tag;
  43. }
  44. $tpl = Renderer::getMarkupTemplate('tagblock_widget.tpl');
  45. $o = Renderer::replaceMacros($tpl, [
  46. '$title' => L10n::t('Tags'),
  47. '$tags' => $tags
  48. ]);
  49. }
  50. return $o;
  51. }
  52. /**
  53. * Get alphabetical sorted array of used tags/terms of an user including
  54. * a weighting by frequency of use.
  55. *
  56. * @brief Get alphabetical sorted array of used tags/terms of an user including
  57. * a weighting by frequency of use.
  58. * @param int $uid The user ID.
  59. * @param int $count Max number of displayed tags/terms.
  60. * @param int $owner_id The contact id of the owner of the tagged items.
  61. * @param string $flags Special item flags.
  62. * @param int $type The tag/term type.
  63. *
  64. * @return arr Alphabetical sorted array of used tags of an user.
  65. */
  66. private static function tagadelic($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
  67. {
  68. $sql_options = Item::getPermissionsSQLByUserId($uid);
  69. $limit = $count ? sprintf('LIMIT %d', intval($count)) : '';
  70. if ($flags) {
  71. if ($flags === 'wall') {
  72. $sql_options .= ' AND `item`.`wall` ';
  73. }
  74. }
  75. if ($owner_id) {
  76. $sql_options .= ' AND `item`.`owner-id` = ' . intval($owner_id) . ' ';
  77. }
  78. // Fetch tags
  79. $r = DBA::p("SELECT `term`, COUNT(`term`) AS `total` FROM `term`
  80. LEFT JOIN `item` ON `term`.`oid` = `item`.`id`
  81. WHERE `term`.`uid` = ? AND `term`.`type` = ?
  82. AND `term`.`otype` = ?
  83. AND `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
  84. $sql_options
  85. GROUP BY `term` ORDER BY `total` DESC $limit",
  86. $uid,
  87. $type,
  88. TERM_OBJ_POST
  89. );
  90. if (!DBA::isResult($r)) {
  91. return [];
  92. }
  93. return self::tagCalc($r);
  94. }
  95. /**
  96. * Calculate weighting of tags according to the frequency of use.
  97. *
  98. * @brief Calculate weighting of tags according to the frequency of use.
  99. * @param array $arr Array of tags/terms with tag/term name and total count of use.
  100. * @return array Alphabetical sorted array of used tags/terms of an user.
  101. */
  102. private static function tagCalc($arr)
  103. {
  104. $tags = [];
  105. $min = 1e9;
  106. $max = -1e9;
  107. $x = 0;
  108. if (!$arr) {
  109. return [];
  110. }
  111. foreach ($arr as $rr) {
  112. $tags[$x][0] = $rr['term'];
  113. $tags[$x][1] = log($rr['total']);
  114. $tags[$x][2] = 0;
  115. $min = min($min, $tags[$x][1]);
  116. $max = max($max, $tags[$x][1]);
  117. $x ++;
  118. }
  119. usort($tags, 'self::tagsSort');
  120. $range = max(.01, $max - $min) * 1.0001;
  121. for ($x = 0; $x < count($tags); $x ++) {
  122. $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
  123. }
  124. return $tags;
  125. }
  126. /**
  127. * Compare function to sort tags/terms alphabetically.
  128. *
  129. * @brief Compare function to sort tags/terms alphabetically.
  130. * @param type $a
  131. * @param type $b
  132. *
  133. * @return int
  134. */
  135. private static function tagsSort($a, $b)
  136. {
  137. if (strtolower($a[0]) == strtolower($b[0])) {
  138. return 0;
  139. }
  140. return ((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);
  141. }
  142. }