linkeddata.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /**
  16. * Generates linked data URIs for various concepts including scrobble events,
  17. * artists, albums and tracks. General technique is to create a dbtune.org
  18. * URI if we have the appropriate MusicBrainz ID. Otherwise, create a URI
  19. * based on the scrobble time if known. Lastly, create one based on the libre.fm
  20. * artist page.
  21. */
  22. require_once($install_path . 'data/Server.php');
  23. function identifierScrobbleEvent ($username, $artist, $track, $album, $time, $mbid = null, $ambid = null, $lmbid = null) {
  24. if (!($username && $artist && $track && $time)) {
  25. return null;
  26. }
  27. $microhash = substr(md5($artist . '//' . $track), 0, 4);
  28. return sprintf('%s#%s.%s', Server::getUserURL($username), rawurlencode($time), rawurlencode($microhash));
  29. }
  30. function identifierArtist ($username, $artist, $track, $album, $time, $mbid = null, $ambid = null, $lmbid = null) {
  31. if (!empty($ambid)) {
  32. return sprintf('http://dbtune.org/musicbrainz/resource/artist/%s', strtolower($ambid));
  33. }
  34. $u = identifierScrobbleEvent($username, $artist, $track, $album, $time, $mbid, $ambid, $lmbid) . '.artist';
  35. if ($u != '.artist') {
  36. return $u;
  37. }
  38. return sprintf('%s#artist', Server::getArtistURL($artist));
  39. }
  40. function identifierAlbum ($username, $artist, $track, $album, $time, $mbid = null, $ambid = null, $lmbid = null) {
  41. if (!empty($lmbid)) {
  42. return sprintf('http://dbtune.org/musicbrainz/resource/record/%s', strtolower($lmbid));
  43. }
  44. $u = identifierScrobbleEvent($username, $artist, $track, $album, $time, $mbid, $ambid, $lmbid) . '.album';
  45. if ($u != '.album') {
  46. return $u;
  47. }
  48. return sprintf('%s#album', Server::getAlbumURL($artist, $album));
  49. }
  50. function identifierTrack ($username, $artist, $track, $album, $time, $mbid = null, $ambid = null, $lmbid = null) {
  51. if (!empty($mbid)) {
  52. return sprintf('http://dbtune.org/musicbrainz/resource/track/%s', strtolower($mbid));
  53. }
  54. $u = identifierScrobbleEvent($username, $artist, $track, $album, $time, $mbid, $ambid, $lmbid) . '.track';
  55. if ($u != '.track') {
  56. return $u;
  57. }
  58. return sprintf('%s#track', Server::getTrackURL($artist, $album, $track));
  59. }