AlbumXML.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/Album.php');
  17. require_once('xml.php');
  18. /**
  19. * Class with functions that returns XML-formatted data for albums.
  20. *
  21. * These functions are mainly used by web service methods.
  22. *
  23. * @package API
  24. */
  25. class AlbumXML {
  26. /**
  27. * Get an album's Top Tags
  28. *
  29. * @param string $artist Name of the album's artist
  30. * @param string $name Name of the album
  31. * @param int $limit How many tags to return
  32. * @param int $cache Caching period of request in seconds
  33. * @return string XML-formatted data
  34. */
  35. public static function getTopTags($artist, $name, $limit, $cache) {
  36. try {
  37. $album = new Album($name, $artist);
  38. $res = $album->getTopTags($limit, 0, $cache);
  39. } catch (Exception $e) {
  40. return(XML::error('failed', '7', 'Invalid resource specified'));
  41. }
  42. if(!$res) {
  43. return(XML::error('failed', '6', 'No tags for this album'));
  44. }
  45. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  46. $root = $xml->addChild('toptags', null);
  47. $root->addAttribute('artist', $album->artist_name);
  48. $root->addAttribute('album', $album->name);
  49. foreach ($res as &$row) {
  50. $tag_node = $root->addChild('tag', null);
  51. $tag_node->addChild('name', repamp($row['tag']));
  52. $tag_node->addChild('count', $row['freq']);
  53. $tag_node->addChild('url', Server::getTagURL($row['tag']));
  54. }
  55. return $xml;
  56. }
  57. public static function getTags($artist, $name, $userid, $limit, $cache) {
  58. try {
  59. $album = new Album($name, $artist);
  60. $res = $album->getTags($userid, $limit, 0, $cache);
  61. } catch (Exception $e) {
  62. return(XML::error('failed', '7', 'Invalid resource specified'));
  63. }
  64. if(!$res) {
  65. return(XML::error('failed', '6', 'No tags for this album'));
  66. }
  67. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  68. $root = $xml->addChild('tags', null);
  69. $root->addAttribute('artist', $artist);
  70. $root->addAttribute('album', $name);
  71. foreach ($res as &$row) {
  72. $tag_node = $root->addChild('tag', null);
  73. $tag_node->addChild('name', repamp($row['tag']));
  74. $tag_node->addChild('url', Server::getTagURL($row['tag']));
  75. }
  76. return $xml;
  77. }
  78. }