publictagcloud.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Public tag cloud for notices
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Public
  23. * @package StatusNet
  24. * @author Mike Cochrane <mikec@mikenz.geek.nz>
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2008 Mike Cochrane
  27. * @copyright 2008-2009 StatusNet, Inc.
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  29. * @link http://status.net/
  30. */
  31. if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
  32. define('TAGS_PER_PAGE', 100);
  33. /**
  34. * Public tag cloud for notices
  35. *
  36. * @category Personal
  37. * @package StatusNet
  38. * @author Mike Cochrane <mikec@mikenz.geek.nz>
  39. * @author Evan Prodromou <evan@status.net>
  40. * @copyright 2008 Mike Cochrane
  41. * @copyright 2008-2009 StatusNet, Inc.
  42. * @link http://status.net/
  43. */
  44. class PublictagcloudAction extends Action
  45. {
  46. function isReadOnly($args)
  47. {
  48. return true;
  49. }
  50. function title()
  51. {
  52. // TRANS: Title for public tag cloud.
  53. return _('Public tag cloud');
  54. }
  55. function showPageNotice()
  56. {
  57. $this->element('p', 'instructions',
  58. // TRANS: Instructions (more used like an explanation/header).
  59. // TRANS: %s is the StatusNet sitename.
  60. sprintf(_('These are most popular recent tags on %s'),
  61. common_config('site', 'name')));
  62. }
  63. function showEmptyList()
  64. {
  65. // TRANS: This message contains a Markdown URL. The link description is between
  66. // TRANS: square brackets, and the link between parentheses. Do not separate "]("
  67. // TRANS: and do not change the URL part.
  68. $message = _('No one has posted a notice with a [hashtag](%%doc.tags%%) yet.') . ' ';
  69. if (common_logged_in()) {
  70. // TRANS: Message shown to a logged in user for the public tag cloud
  71. // TRANS: while no tags exist yet. "One" refers to the non-existing hashtag.
  72. $message .= _('Be the first to post one!');
  73. }
  74. else {
  75. // TRANS: Message shown to a anonymous user for the public tag cloud
  76. // TRANS: while no tags exist yet. "One" refers to the non-existing hashtag.
  77. // TRANS: This message contains a Markdown URL. The link description is between
  78. // TRANS: square brackets, and the link between parentheses. Do not separate "]("
  79. // TRANS: and do not change the URL part.
  80. $message .= _('Why not [register an account](%%action.register%%) and be the first to post one!');
  81. }
  82. $this->elementStart('div', 'guide');
  83. $this->raw(common_markup_to_html($message));
  84. $this->elementEnd('div');
  85. }
  86. function handle($args)
  87. {
  88. parent::handle($args);
  89. $this->showPage();
  90. }
  91. function showContent()
  92. {
  93. // This should probably be cached rather than recalculated
  94. $tags = new Notice_tag();
  95. #Need to clear the selection and then only re-add the field
  96. #we are grouping by, otherwise it's not a valid 'group by'
  97. #even though MySQL seems to let it slide...
  98. $tags->selectAdd();
  99. $tags->selectAdd('tag');
  100. #Add the aggregated columns...
  101. $tags->selectAdd('max(notice_id) as last_notice_id');
  102. $calc = common_sql_weight('created', common_config('tag', 'dropoff'));
  103. $cutoff = sprintf("notice_tag.created > '%s'",
  104. common_sql_date(time() - common_config('tag', 'cutoff')));
  105. $tags->selectAdd($calc . ' as weight');
  106. $tags->whereAdd($cutoff);
  107. $tags->groupBy('tag');
  108. $tags->orderBy('weight DESC');
  109. $tags->limit(TAGS_PER_PAGE);
  110. $cnt = $tags->find();
  111. if ($cnt > 0) {
  112. $this->elementStart('div', array('id' => 'tagcloud',
  113. 'class' => 'section'));
  114. $tw = array();
  115. $sum = 0;
  116. while ($tags->fetch()) {
  117. $tw[$tags->tag] = $tags->weight;
  118. $sum += $tags->weight;
  119. }
  120. ksort($tw);
  121. $this->elementStart('ul', 'tags xoxo tag-cloud');
  122. foreach ($tw as $tag => $weight) {
  123. if ($sum) {
  124. $weightedSum = $weight/$sum;
  125. } else {
  126. $weightedSum = 0.5;
  127. }
  128. $this->showTag($tag, $weight, $weightedSum);
  129. }
  130. $this->elementEnd('ul');
  131. $this->elementEnd('div');
  132. } else {
  133. $this->showEmptyList();
  134. }
  135. }
  136. function showTag($tag, $weight, $relative)
  137. {
  138. if ($relative > 0.1) {
  139. $rel = 'tag-cloud-7';
  140. } else if ($relative > 0.05) {
  141. $rel = 'tag-cloud-6';
  142. } else if ($relative > 0.02) {
  143. $rel = 'tag-cloud-5';
  144. } else if ($relative > 0.01) {
  145. $rel = 'tag-cloud-4';
  146. } else if ($relative > 0.005) {
  147. $rel = 'tag-cloud-3';
  148. } else if ($relative > 0.002) {
  149. $rel = 'tag-cloud-2';
  150. } else {
  151. $rel = 'tag-cloud-1';
  152. }
  153. $this->elementStart('li', $rel);
  154. $this->element('a', array('href' => common_local_url('tag', array('tag' => $tag))),
  155. $tag);
  156. $this->elementEnd('li');
  157. }
  158. }