TagCloud.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /* GNU FM -- a free network service for sharing your music listening habits
  3. Copyright (C) 2009 Free Software Foundation, Inc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. require_once($install_path . '/data/Server.php');
  16. require_once($install_path . '/data/Artist.php');
  17. require_once($install_path . '/data/Album.php');
  18. require_once($install_path . '/data/Track.php');
  19. require_once($install_path . '/data/Tag.php');
  20. /**
  21. * Provides access to functions for generating tag clouds
  22. */
  23. class TagCloud {
  24. /**
  25. * Generate array for use when building tag clouds
  26. *
  27. * The *size* value in the resulting array corresponds to a CSS font-size in the range of xx-small to xx-large.
  28. *
  29. * Examples
  30. * --------
  31. * Get an array with a track's top 10 tags: ((name, count, size, pageurl) .. )
  32. *
  33. * `$trackTags = generateTagCloud('tags', 'tag', 10, 'track', array($trackname, $artistname));`
  34. *
  35. * Get an array with a user's top 10 streamable artists: ((name, count, size, pageurl) .. )
  36. *
  37. * `$userArtists = generateTagCloud('artists', 'artist', 10, 'userid', $userid)`
  38. *
  39. * Get an array with the top 10 of all loved tracks: ((name, count, size, artist_name, pageurl) .. )
  40. *
  41. * `$lovedTracks = generateTagCloud('loved', 'track', 10, null, null, False);`
  42. *
  43. * @param string $set The set to return data from (artists|tracks|tags|loved)
  44. * @param string $item The item to count in the set (artist|track|tag)
  45. * @param int $limit Max amount of items to return
  46. * @param string $constraint_type The type of constraint to filter by (artist|track|tag|userid|null)
  47. * @param string|array $constraint The constraint value to filter by
  48. * @param bool $streamable Only return streamable artists/albums/tracks
  49. * @param int $cache int The caching period in seconds
  50. * @return array Items ((name, count, size, pageurl) .. )
  51. */
  52. static function generateTagCloud($set, $item, $limit=40, $constraint_type=null, $constraint=null, $streamable = True, $cache=7200) {
  53. $sizes = array('xx-large', 'x-large', 'large', 'medium', 'small', 'x-small', 'xx-small');
  54. if ($set == 'artists') {
  55. if ($item == 'artist') {
  56. if ($constraint_type == 'userid') {
  57. $res = Server::getTopArtists($limit, 0, $streamable, null, null, $constraint, $cache);
  58. } else if (is_null($constraint_type)) {
  59. $res = Server::getTopArtists($limit, 0, $streamable, null, null, null, $cache);
  60. }
  61. } else {
  62. throw new Exception("Not a valid tagcloud item: " . $item);
  63. }
  64. } else if ($set == 'loved') {
  65. if ($item == 'artist') {
  66. if ($constraint_type == 'userid') {
  67. $res = Server::getLovedArtists($limit, 0, $streamable, $constraint, $cache);
  68. } else if (is_null($constraint_type)) {
  69. $res = Server::getLovedArtists($limit, 0, $streamable, null, $cache);
  70. }
  71. } else if ($item == 'track') {
  72. if ($constraint_type == 'userid') {
  73. $res = Server::getLovedTracks($limit, 0, $streamable, null, $constraint, $cache);
  74. } else if ($constraint_type == 'artist') {
  75. $res = Server::getLovedTracks($limit, 0, $streamable, $constraint, null, $cache);
  76. } else if (is_null($constraint_type)) {
  77. $res = Server::getLovedTracks($limit, 0, $streamable, null, null, $cache);
  78. }
  79. } else {
  80. throw new Exception("Not a valid tagcloud item: " . $item);
  81. }
  82. } else if ($set == 'tracks') {
  83. if ($item == 'track') {
  84. if ($constraint_type == 'userid') {
  85. $res = Server::getTopTracks($limit, 0, $streamable, null, null, null, $constraint, $cache);
  86. } else if ($constraint_type == 'artist') {
  87. $res = Server::getTopTracks($limit, 0, $streamable, null, null, $constraint, null, $cache);
  88. } else if (is_null($constraint_type)) {
  89. $res = Server::getTopTracks($limit, 0, $streamable, null, null, null, null, $cache);
  90. }
  91. } else {
  92. throw new Exception("Not a valid tagcloud item: " . $item);
  93. }
  94. } else if ($set == 'tags') {
  95. if ($item == 'tag') {
  96. if ($constraint_type == 'artist') {
  97. $artist = new Artist($constraint);
  98. $res = $artist->getTopTags($limit, 0, $cache);
  99. } else if ($constraint_type == 'album') {
  100. // $constraint needs to be an array of (album_name, artist_name)
  101. $album = new Album($constraint[0], $constraint[1]);
  102. $res = $album->getTopTags($limit, 0, $cache);
  103. } else if ($constraint_type == 'track') {
  104. // $constraint needs to be an array of (track_name, artist_name)
  105. $track = new Track($constraint[0], $constraint[1]);
  106. $res = $track->getTopTags($limit, 0, $cache);
  107. }
  108. } else if ($item == 'artist') {
  109. if ($constraint_type == 'tag') {
  110. $res = Tag::getTopArtists($constraint, $limit, 0, $streamable, $cache);
  111. }
  112. } else if ($item == 'track') {
  113. if ($constraint_type == 'tag') {
  114. $res = Tag::getTopTracks($constraint, $limit, 0, $streamable, $cache);
  115. }
  116. } else {
  117. throw new Exception("Not a valid tagcloud item: " . $item);
  118. }
  119. } else {
  120. throw new Exception("Not a valid tagcloud set: " . $set);
  121. }
  122. if(!$res) {
  123. return array();
  124. }
  125. $tagcloud = array();
  126. $i=0;
  127. foreach ($res as &$row) {
  128. $tagcloud[$i]['name'] = $row[$item];
  129. $tagcloud[$i]['count'] = $row['freq'];
  130. $tagcloud[$i]['size'] = $sizes[(int) ($i/(count($res)/7))];
  131. if ($item == 'artist') {
  132. $tagcloud[$i]['pageurl'] = Server::getArtistURL($row[$item]);
  133. } else if ($item == 'tag') {
  134. $tagcloud[$i]['pageurl'] = Server::getTagURL($row[$item]);
  135. } else if ($item == 'track') {
  136. $tagcloud[$i]['artist_name'] = $row['artist'];
  137. $tagcloud[$i]['pageurl'] = Server::getTrackURL($row['artist'], null, $row[$item]);
  138. }
  139. $i++;
  140. }
  141. sort($tagcloud);
  142. return $tagcloud;
  143. }
  144. }