Track.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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/Artist.php');
  17. require_once($install_path . '/data/Album.php');
  18. require_once($install_path . '/data/Tag.php');
  19. require_once($install_path . '/data/Server.php');
  20. require_once($install_path . '/utils/resolve-external.php');
  21. require_once($install_path . '/utils/licenses.php');
  22. require_once($install_path . '/utils/linkeddata.php');
  23. /**
  24. * Represents track data
  25. *
  26. * All track attributes are accessible as public variables.
  27. */
  28. class Track {
  29. public $name, $artist_name, $album_name, $mbid, $duration, $streamable, $license, $downloadurl, $streamurl;
  30. public $id;
  31. private $_playcount = false, $_listenercount = false;
  32. /**
  33. * Track constructor
  34. *
  35. * @param string $name The name of the track to load
  36. * @param string $artist The name of the artist who recorded this track
  37. *
  38. * @todo Should we call Track::create() instead of throwing "No such track" exception?
  39. */
  40. function __construct($name, $artist) {
  41. global $adodb;
  42. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  43. $this->query = 'SELECT name, artist_name, album_name, duration, streamable, license, downloadurl, streamurl, mbid FROM Track WHERE '
  44. . 'lower(name) = lower(' . $adodb->qstr($name) . ') AND '
  45. . 'lower(artist_name) = lower(' . $adodb->qstr($artist) . ')'
  46. . 'ORDER BY streamable DESC';
  47. $res = $adodb->CacheGetRow(600, $this->query);
  48. if (!$res) {
  49. throw new Exception('No such track: ' . $name);
  50. } else {
  51. $row = $res;
  52. $this->name = $row['name'];
  53. $this->mbid = $row['mbid'];
  54. $this->artist_name = $row['artist_name'];
  55. $this->album_name = $row['album_name'];
  56. $this->duration = $row['duration'];
  57. $this->streamable = $row['streamable'];
  58. $this->license = simplify_license($row['license']);
  59. $this->licenseurl = $row['license'];
  60. $this->downloadurl = resolve_external_url($row['downloadurl']);
  61. $this->streamurl = resolve_external_url($row['streamurl']);
  62. $this->id = identifierTrack(null, $this->artist_name, $this->name, $this->album_name, null, $this->mbid, null, null);
  63. }
  64. }
  65. /**
  66. * Add a new track to the database.
  67. *
  68. * @param string $name Track name
  69. * @param string $artist_name Artist name
  70. * @param string $album_name Album name
  71. * @param string $streamurl The URL pointing to a streamable file
  72. * @param string $downloadurl The URL pointing to a downloadable file
  73. * @param string $license A license URL
  74. * @return Track A newly created track object
  75. */
  76. public static function create($name, $artist_name, $album_name, $streamurl, $downloadurl, $license) {
  77. global $adodb;
  78. $streamable = (is_free_license($license) && !empty($streamurl)) ? 1 : 0;
  79. $adodb->Execute('INSERT INTO Track (name, artist_name, album_name, streamurl, downloadurl, license, streamable) VALUES ('
  80. . $adodb->qstr($name) . ', '
  81. . $adodb->qstr($artist_name) . ', '
  82. . $adodb->qstr($album_name) . ', '
  83. . $adodb->qstr($streamurl) . ', '
  84. . $adodb->qstr($downloadurl) . ', '
  85. . $adodb->qstr($license) . ', '
  86. . $streamable . ')');
  87. $album = new Album($album_name, $artist_name);
  88. $album->clearTrackCache();
  89. $artist = new Artist($artist_name);
  90. if (!$artist->isStreamable() && $streamable == 1) {
  91. // This artist has just had a streamable track added, so are now streamable
  92. $adodb->Execute('UPDATE Artist SET streamable = 1 WHERE name = ' . $adodb->qstr($artist->name));
  93. }
  94. return new Track($name, $artist_name);
  95. }
  96. /**
  97. * Clear cached database query for this track
  98. */
  99. function clearCache() {
  100. global $adodb;
  101. $adodb->CacheFlush($this->query);
  102. }
  103. /**
  104. * Sets the playcount
  105. *
  106. * @param int $playcount The number of plays this track has received
  107. */
  108. function setPlayCount($playcount) {
  109. $this->_playcount = $playcount;
  110. }
  111. /**
  112. * Sets the number of listeners
  113. *
  114. * @param int $listeners The number of people who've listened to this track
  115. */
  116. function setListenerCount($listeners) {
  117. $this->_listenercount = $listeners;
  118. }
  119. /**
  120. * Sets the streaming URL
  121. *
  122. * @param string $streamurl The URL pointing to a streamable file
  123. */
  124. function setStreamURL($streamurl) {
  125. global $adodb;
  126. $adodb->Execute('UPDATE Track SET streamurl=' . $adodb->qstr($streamurl) .
  127. ' WHERE artist_name=' . $adodb->qstr($this->artist_name) . ' AND ' .
  128. ' name=' . $adodb->qstr($this->name));
  129. $this->clearCache();
  130. }
  131. /**
  132. * Sets the download URL
  133. *
  134. * @param string $downloadurl The URL pointing to a downloadable file
  135. */
  136. function setDownloadURL($downloadurl) {
  137. global $adodb;
  138. $adodb->Execute('UPDATE Track SET downloadurl=' . $adodb->qstr($downloadurl) .
  139. ' WHERE artist_name=' . $adodb->qstr($this->artist_name) . ' AND ' .
  140. ' name=' . $adodb->qstr($this->name));
  141. $this->clearCache();
  142. }
  143. /**
  144. * Sets the license
  145. *
  146. * @param string $license A license URL
  147. */
  148. function setLicense($license) {
  149. global $adodb;
  150. $streamable = 0;
  151. if (is_free_license($license)) {
  152. $streamable = 1;
  153. }
  154. $adodb->Execute('UPDATE Track SET license=' . $adodb->qstr($license) . ', streamable=' . $streamable .
  155. ' WHERE artist_name=' . $adodb->qstr($this->artist_name) . ' AND ' .
  156. ' name=' . $adodb->qstr($this->name));
  157. if ($streamable) {
  158. $adodb->Execute('UPDATE Artist SET streamable=1 WHERE name=' . $adodb->qstr($this->artist_name));
  159. try {
  160. $artist = new Artist($this->artist_name);
  161. $artist->clearCache();
  162. } catch (Exception $e) {
  163. // No such artist.
  164. }
  165. }
  166. $this->clearCache();
  167. }
  168. /**
  169. * Gets the play count for this track
  170. *
  171. * @return int Number of times this track has been played
  172. */
  173. function getPlayCount() {
  174. if ($this->_playcount) {
  175. // If we've been given a cached value from another SQL call use that
  176. return $this->_playcount;
  177. }
  178. $this->_getPlayCountAndListenerCount();
  179. return $this->_playcount;
  180. }
  181. /**
  182. * Gets the listener count for this track
  183. *
  184. * @return int Number of listeners this track has
  185. */
  186. function getListenerCount() {
  187. if ($this->_listeners) {
  188. return $this->_listenercount;
  189. }
  190. $this->_getPlayCountAndListenerCount();
  191. return $this->_listenercount;
  192. }
  193. /**
  194. * Updates the play count and listener count for this track
  195. */
  196. private function _getPlayCountAndListenerCount() {
  197. global $adodb;
  198. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  199. $row = $adodb->CacheGetRow(300, 'SELECT COUNT(track) AS freq, COUNT(DISTINCT userid) AS listeners FROM Scrobbles WHERE'
  200. . ' lower(artist) = lower(' . $adodb->qstr($this->artist_name) . ')'
  201. . ' AND lower(track) = lower(' . $adodb->qstr($this->name) . ')'
  202. . ' GROUP BY track ORDER BY freq DESC');
  203. if (!isset($row)) {
  204. $this->setPlaycount(0);
  205. $this->setListenerCount(0);
  206. } else {
  207. $this->setPlaycount($row['freq']);
  208. $this->setListenerCount($row['listeners']);
  209. }
  210. }
  211. /**
  212. * Gets the artist for this track.
  213. *
  214. * @return Artist Artist object for this track
  215. */
  216. function getArtist() {
  217. try {
  218. return new Artist($this->artist_name);
  219. } catch (Exception $e) {
  220. throw $e;
  221. }
  222. }
  223. /**
  224. * Gets the URL for this track
  225. *
  226. * @param string $component Type of page, 'tags', ''
  227. * @return string URL for this track
  228. */
  229. function getURL($component = '') {
  230. return Server::getTrackURL($this->artist_name, $this->album_name, $this->name, $component);
  231. }
  232. /**
  233. * Gets the edit URL for this track
  234. *
  235. * @return string Edit URL for this track
  236. */
  237. function getEditURL() {
  238. return Server::getTrackEditURL($this->artist_name, $this->album_name, $this->name);
  239. }
  240. /**
  241. * Gets the top tags for this track, ordered by tag count
  242. *
  243. * @param int $limit The number of tags to return (default is 10)
  244. * @param int $offset The position of the first tag to return (default is 0)
  245. * @param int $cache Caching period of query in seconds (default is 600)
  246. * @return array Tag details ((tag, freq) .. )
  247. */
  248. function getTopTags($limit=10, $offset=0, $cache=600) {
  249. return Tag::_getTagData($cache, $limit, $offset, null, $this->artist_name, null, $this->name);
  250. }
  251. /**
  252. * Get this track's top listeners
  253. *
  254. * @param int $limit Amount of results to return
  255. * @param int $offset Skip this many items before returning results
  256. * @param int $streamable Only return results for streamable tracks
  257. * @param int $begin Only use scrobbles with time higher than this timestamp
  258. * @param int $end Only use scrobbles with time lower than this timestamp
  259. * @param int $cache Caching period in seconds
  260. * @return array ((userid, freq, username, userurl) ..)
  261. */
  262. function getTopListeners($limit = 20, $offset = 0, $streamable = False, $begin = null, $end = null, $cache = 600) {
  263. return Server::getTopListeners($limit, $offset, $streamable, $begin, $end, $this->artist_name, $this->name, $cache);
  264. }
  265. /**
  266. * Get a specific user's tags for this track.
  267. *
  268. * @param int $userid Get tags for this user
  269. * @param int $limit The number of tags to return (default is 10)
  270. * @param int $offset The position of the first tag to return (default is 0)
  271. * @param int $cache Caching period of query in seconds (default is 600)
  272. * @return array Tag details ((tag, freq) .. )
  273. */
  274. function getTags($userid, $limit=10, $offset=0, $cache=600) {
  275. if(isset($userid)) {
  276. return Tag::_getTagData($cache, $limit, $offset, $userid, $this->artist_name, null, $this->name);
  277. }
  278. }
  279. /**
  280. * Add a list of tags to a track
  281. *
  282. * @param string $tags A comma-separated list of tags.
  283. * @param int $userid The user adding these tags.
  284. * @return bool True if any tag was added, False if no tags were added.
  285. */
  286. function addTags($tags, $userid) {
  287. global $adodb;
  288. $tags = explode(',', strtolower($tags));
  289. $query = 'INSERT INTO Tags (tag, artist, album, track, userid) VALUES(?,?,?,?,?)';
  290. foreach($tags as $tag) {
  291. $tag = trim($tag);
  292. if(strlen($tag) == 0) {
  293. continue;
  294. }
  295. $params = array($tag, $this->artist_name, $this->album_name, $this->name, (int) $userid);
  296. try {
  297. $adodb->Execute($query, $params);
  298. if ($adodb->Affected_Rows()) {
  299. $res = $res + 1;
  300. }
  301. } catch (Exception $e) {
  302. reportError($e->GetMessage(), $e->GetTraceAsString());
  303. }
  304. }
  305. return (bool) $res;
  306. }
  307. /**
  308. * Love a track
  309. *
  310. * @param int $userid The user loving this track.
  311. * @return bool True on success, False on fail.
  312. */
  313. function love($userid) {
  314. global $adodb;
  315. $query = 'INSERT INTO Loved_Tracks (userid, track, artist, time) VALUES(?,?,?,?)';
  316. $params = array((int) $userid, $this->name, $this->artist_name, time());
  317. try {
  318. $adodb->Execute($query, $params);
  319. $res = $adodb->Affected_Rows();
  320. } catch (Exception $e) {
  321. reportError($e->GetMessage(), $e->GetTraceAsString());
  322. return False;
  323. }
  324. return (bool) $res;
  325. }
  326. /**
  327. * Unlove a track
  328. *
  329. * @param int $userid The user unloving this track.
  330. * @return bool True on success, False on fail.
  331. */
  332. function unlove($userid) {
  333. global $adodb;
  334. $query = 'DELETE FROM Loved_Tracks WHERE userid=? AND track=? AND artist=?';
  335. $params = array((int) $userid, $this->name, $this->artist_name);
  336. try {
  337. $adodb->Execute($query, $params);
  338. $res = $adodb->Affected_Rows();
  339. } catch (Exception $e) {
  340. reportError($e->GetMessage(), $e->GetTraceAsString());
  341. return False;
  342. }
  343. return (bool) $res;
  344. }
  345. /**
  346. * Check if track has been loved by user
  347. *
  348. * @param int $userid The user we are looking for
  349. * @return bool True if track has been loved by user
  350. */
  351. function isLoved($userid) {
  352. global $adodb;
  353. $query = 'SELECT * FROM Loved_Tracks WHERE userid=? AND track=? AND artist=?';
  354. $params = array((int) $userid, $this->name, $this->artist_name);
  355. try {
  356. $res = $adodb->GetRow($query, $params);
  357. } catch (Exception $e) {
  358. reportError($e->GetMessage(), $e->GetTraceAsString());
  359. return False;
  360. }
  361. if($res) {
  362. return True;
  363. }
  364. return False;
  365. }
  366. /**
  367. * Ban a track
  368. *
  369. * @param int $userid The user banning this track.
  370. * @return bool True on success, False on fail.
  371. *
  372. */
  373. function ban($userid) {
  374. global $adodb;
  375. $query = 'INSERT INTO Banned_Tracks (userid, track, artist, time) VALUES(?,?,?,?)';
  376. $params = array((int) $userid, $this->name, $this->artist_name, time());
  377. try {
  378. $adodb->Execute($query, $params);
  379. $res = $adodb->Affected_Rows();
  380. } catch (Exception $e) {
  381. reportError($e->GetMessage(), $e->GetTraceAsString());
  382. return False;
  383. }
  384. return (bool) $res;
  385. }
  386. /**
  387. * Unban a track
  388. *
  389. * @param int $userid The user unbanning this track.
  390. * @return bool True on success, False on fail.
  391. */
  392. function unban($userid) {
  393. global $adodb;
  394. $query = 'DELETE FROM Banned_Tracks WHERE userid=? AND track=? AND artist=?';
  395. $params = array((int) $userid, $this->name, $this->artist_name);
  396. try {
  397. $adodb->Execute($query, $params);
  398. $res = $adodb->Affected_Rows();
  399. } catch (Exception $e) {
  400. reportError($e->GetMessage(), $e->GetTraceAsString());
  401. return False;
  402. }
  403. return (bool) $res;
  404. }
  405. /**
  406. * Check if track has been banned by user
  407. *
  408. * @param int $userid The user we are looking for
  409. * @return bool True if track has been banned by user
  410. */
  411. function isBanned($userid) {
  412. global $adodb;
  413. $query = 'SELECT * FROM Banned_Tracks WHERE userid=? AND track=? AND artist=?';
  414. $params = array((int) $userid, $this->name, $this->artist_name);
  415. try {
  416. $res = $adodb->GetRow($query, $params);
  417. } catch (Exception $e) {
  418. reportError($e->GetMessage(), $e->GetTraceAsString());
  419. return False;
  420. }
  421. if($res) {
  422. return True;
  423. }
  424. return False;
  425. }
  426. /*
  427. * Remove a tag from a track
  428. *
  429. * @param string $tag The tag to be removed
  430. * @param int $userid The user removing the tag
  431. * @return bool True on success, False on fail.
  432. */
  433. function removeTag($tag, $userid) {
  434. global $adodb;
  435. $tag = trim($tag);
  436. if(strlen($tag) == 0) {
  437. return;
  438. }
  439. $query = 'DELETE FROM Tags WHERE tag=? AND artist=? AND track=? AND userid = ?';
  440. $params = array($tag, $this->artist_name, $this->name, (int) $userid);
  441. try {
  442. $adodb->Execute($query, $params);
  443. $res = $adodb->Affected_Rows();
  444. } catch (Exception $e) {
  445. reportError($e->getMessage(), $e->getTraceAsString());
  446. return False;
  447. }
  448. return (bool) $res;
  449. }
  450. }