Tag.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 . '/database.php');
  16. require_once($install_path . '/data/sanitize.php');
  17. /**
  18. * Provides access to functions used for retrieving data involving tags.
  19. */
  20. class Tag {
  21. /**
  22. * Get various data involving tags, used by other tag related functions.
  23. *
  24. * This function is not meant to be used directly,
  25. * it is easier to use higher-level functions such as Tag::getTopTags()
  26. * to get the data needed.
  27. *
  28. * @param int $cache Caching period of sql query in seconds
  29. * @param int $limit The number of results to return
  30. * @param int $offset The position of the first result to return
  31. * @param int $user The userid to return results for
  32. * @param string $artist The artist to return results for
  33. * @param string $album The album to return results for
  34. * @param string $track The track to return results for
  35. * @param string $tag The tag to return results for
  36. * @param string $taggingtype The type of tag to return results for (artist|album|track)
  37. * @param boolean $streamable Only return streamable results if True
  38. * @return array An array of results
  39. */
  40. function _getTagData($cache=600, $limit=null, $offset=null, $user=null, $artist=null, $album=null,
  41. $track=null, $tag=null, $taggingtype=null, $streamable=False) {
  42. global $adodb;
  43. $whereuser = ' WHERE userid=' . (int)$user;
  44. $anduser = ' AND userid=' . (int)$user;
  45. $whereartist = ' WHERE LOWER(artist) = LOWER(' . $adodb->qstr((string)$artist) . ')';
  46. $wherestream = '';
  47. $andartist = ' AND LOWER(artist) = LOWER(' . $adodb->qstr((string)$artist) . ')';
  48. $wheretag = ' WHERE LOWER(tag) = LOWER(' . $adodb->qstr((string)$tag) . ')';
  49. $andtag = ' AND LOWER(tag) = LOWER(' . $adodb->qstr((string)$tag) . ')';
  50. $andalbum = ' AND LOWER(album) = LOWER(' . $adodb->qstr((string)$album) . ')';
  51. $andtrack = ' AND LOWER(track) = LOWER(' . $adodb->qstr((string)$track) . ')';
  52. $hasartist = ' AND artist IS NOT NULL AND artist <> \'\'';
  53. $hasalbum = ' AND album IS NOT NULL AND album <> \'\'';
  54. $hastrack = ' AND track IS NOT NULL AND track <> \'\'';
  55. $orderfreq = ' ORDER BY freq DESC';
  56. if($streamable) {
  57. $wherestream = ' INNER JOIN Artist ON Tags.artist=Artist.name WHERE Artist.streamable=1';
  58. $whereuser = $anduser;
  59. $whereartist = $andartist;
  60. $wheretag = $andtag;
  61. }
  62. if($user) {
  63. if($artist) {
  64. if($album) {
  65. //Album->getTags
  66. $query = 'SELECT tag, COUNT(tag) AS freq FROM Tags' . $whereuser . $andartist . $andalbum . ' GROUP BY tag';
  67. } elseif($track) {
  68. //Track->getTags
  69. $query = 'SELECT tag, COUNT(tag) AS freq FROM Tags' . $whereuser . $andartist . $andtrack . ' GROUP BY tag';
  70. } else {
  71. //Artist->getTags
  72. $query = 'SELECT tag, COUNT(tag) AS freq FROM Tags' . $whereuser . $andartist . ' GROUP BY tag';
  73. }
  74. } else {
  75. if($tag) {
  76. if(strtolower($taggingtype)=='artist') {
  77. //User->getPersonalTags (artist)
  78. $query = 'SELECT artist FROM Tags' . $wherestream . $whereuser . $andtag . $hasartist;
  79. } elseif(strtolower($taggingtype)=='album') {
  80. //User->getPersonalTags (album)
  81. $query = 'SELECT artist, album FROM Tags' . $wherestream . $whereuser . $andtag . $hasalbum;
  82. } elseif(strtolower($taggingtype)=='track') {
  83. //User->getPersonalTags (track)
  84. $query = 'SELECT artist, track FROM Tags' . $wherestream . $whereuser . $andtag . $hastrack;
  85. } elseif($taggingtype) {
  86. //Invalid taggingtype
  87. throw new Exception("Invalid taggingtype: " . $taggingtype);
  88. } else {
  89. //User->getTagInfo
  90. $query = 'SELECT tag, COUNT(tag) AS freq FROM Tags' . $whereuser . $andtag . ' GROUP BY tag';
  91. }
  92. } else {
  93. //User->getTopTags
  94. $query = 'SELECT tag, COUNT(tag) AS freq FROM Tags' . $whereuser . ' GROUP BY tag' . $orderfreq;
  95. }
  96. }
  97. } else {
  98. if($artist) {
  99. if($album) {
  100. //Album->getTopTags
  101. $query = 'SELECT tag, COUNT(tag) AS freq FROM Tags' .$whereartist . $andalbum . ' GROUP BY tag' . $orderfreq;
  102. } elseif($track) {
  103. //Track->getTopTags
  104. $query = 'SELECT tag, COUNT(tag) AS freq FROM Tags' . $whereartist . $andtrack . ' GROUP BY tag' . $orderfreq;
  105. } else {
  106. //Artist->getTopTags
  107. $query = 'SELECT tag, COUNT(tag) AS freq FROM Tags' . $whereartist . ' GROUP BY tag' . $orderfreq;
  108. }
  109. } elseif($tag) {
  110. if(strtolower($taggingtype)=='artist') {
  111. //Tag::getTopArtists
  112. $query = 'SELECT artist, COUNT(artist) AS freq FROM Tags' . $wherestream . $wheretag . $hasartist . ' GROUP BY artist' . $orderfreq;
  113. } elseif(strtolower($taggingtype)=='album') {
  114. //Tag::getTopAlbums
  115. $query = 'SELECT artist, album, COUNT(album) AS freq FROM Tags' . $wherestream . $wheretag . $hasalbum . ' GROUP BY album, artist' . $orderfreq;
  116. } elseif(strtolower($taggingtype)=='track') {
  117. //Tag::getTopTracks
  118. $query = 'SELECT artist, track, COUNT(track) AS freq FROM Tags' . $wherestream . $wheretag . $hastrack . ' GROUP BY track, artist' . $orderfreq;
  119. } elseif($taggingtype) {
  120. //Invalid taggingtype
  121. throw new Exception("Invalid taggingtype: " . $taggingtype);
  122. } else {
  123. //Tag::getInfo
  124. $query = 'SELECT tag, COUNT(tag) AS freq FROM Tags' . $wheretag . ' GROUP BY tag';
  125. }
  126. } else {
  127. //Tag::getTopTags
  128. $query = 'SELECT tag, COUNT(tag) AS freq FROM Tags GROUP BY tag' . $orderfreq;
  129. }
  130. }
  131. if($limit) {
  132. $query .= ' LIMIT ' . (int)$limit;
  133. }
  134. if($offset) {
  135. $query .= ' OFFSET ' . (int)$offset;
  136. }
  137. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  138. $res = $adodb->CacheGetAll($cache, $query);
  139. if(!$res) {
  140. return null;
  141. }
  142. foreach($res as &$i) {
  143. $row = sanitize($i);
  144. $result[] = $row;
  145. }
  146. return $result;
  147. }
  148. /**
  149. * Get top tags, ordered by tag count
  150. *
  151. * @param int $limit The number of tags to return (default is 10)
  152. * @param int $offset The position of the first tag to return (default is 0)
  153. * @param int $cache Caching period of query in seconds (default is 600)
  154. * @return array Tag details ((tag, freq) .. )
  155. */
  156. function getTopTags($limit=10, $offset=0, $cache=600) {
  157. return Tag::_getTagData($cache, $limit, $offset);
  158. }
  159. /**
  160. * Get top artists tagged with tag, ordered by tag count
  161. *
  162. * @param string $tag The tag to return artists for
  163. * @param int $limit The number of artists to return (default is 10)
  164. * @param int $offset The position of the first artist to return (default is 0)
  165. * @param boolean $streamable Only return streamable artists if True (default is True)
  166. * @param int $cache Caching period of query in seconds (default is 600)
  167. * @return array Artist details ((artist, freq) .. )
  168. */
  169. function getTopArtists($tag, $limit=10, $offset=0, $streamable=True, $cache=600) {
  170. if(isset($tag)) {
  171. return Tag::_getTagData($cache, $limit, $offset, null, null, null, null, $tag, 'artist', $streamable);
  172. }
  173. }
  174. /**
  175. * Get top albums tagged with tag, ordered by tag count
  176. *
  177. * @param string $tag The tag to return albums for
  178. * @param int $limit The number of albums to return (default is 10)
  179. * @param int $offset The position of the first album to return (default is 0)
  180. * @param boolean $streamable Only return albums by streamable artists if True (default is True)
  181. * @param int $cache Caching period of query in seconds (default is 600)
  182. * @return array Album details ((artist, album, freq) .. )
  183. */
  184. function getTopAlbums($tag, $limit=10, $offset=0, $streamable=True, $cache=600) {
  185. if(isset($tag)) {
  186. return Tag::_getTagData($cache, $limit, $offset, null, null, null, null, $tag, 'album', $streamable);
  187. }
  188. }
  189. /**
  190. * Get top tracks tagged with tag, ordered by tag count
  191. *
  192. * @param string $tag The tag to return tracks for
  193. * @param int $limit The number of tracks to return (default is 10)
  194. * @param int $offset The position of the first track to return (default is 0)
  195. * @param boolean $streamable Only return tracks by streamable artists if True (default is True)
  196. * @param int $cache Caching period of query in seconds (default is 600)
  197. * @return array Track details ((artist, track, freq) .. )
  198. */
  199. function getTopTracks($tag, $limit=10, $offset=0, $streamable=True, $cache=600) {
  200. if(isset($tag)) {
  201. return Tag::_getTagData($cache, $limit, $offset, null, null, null, null, $tag, 'track', $streamable);
  202. }
  203. }
  204. /**
  205. * Get tag count for tag
  206. *
  207. * @param string $tag The tag to return tag count for
  208. * @param int $cache Caching period of query in seconds (default is 600)
  209. * @return array Tag details ((tag, freq) .. )
  210. */
  211. function getInfo($tag, $cache=600) {
  212. if(isset($tag)) {
  213. return Tag::_getTagData($cache, 1, 0, null, null, null, null, $tag);
  214. }
  215. }
  216. }