1
0
Fork 0
mirror of https://github.com/Yetangitu/ampache synced 2025-10-03 09:49:30 +02:00

Basic include support in the XML API

Includes are specified by the `include[]` array `GET` parameter.
Supported includes as of now are:

* For `artists` and `artist` actions, you can include `albums` and/or `songs`.
* For `albums` action, you can include `songs`.
This commit is contained in:
Phyks (Lucas Verney) 2016-07-25 11:11:11 +02:00
parent a0d76171de
commit 31ef95a3ed
2 changed files with 50 additions and 19 deletions

View file

@ -326,7 +326,7 @@ class Api
$artists = self::$browse->get_objects(); $artists = self::$browse->get_objects();
// echo out the resulting xml document // echo out the resulting xml document
ob_end_clean(); ob_end_clean();
echo XML_Data::artists($artists); echo XML_Data::artists($artists, $input['include']);
} // artists } // artists
/** /**
@ -338,7 +338,7 @@ class Api
public static function artist($input) public static function artist($input)
{ {
$uid = scrub_in($input['filter']); $uid = scrub_in($input['filter']);
echo XML_Data::artists(array($uid)); echo XML_Data::artists(array($uid), $input['include']);
} // artist } // artist
/** /**
@ -397,7 +397,7 @@ class Api
XML_Data::set_offset($input['offset']); XML_Data::set_offset($input['offset']);
XML_Data::set_limit($input['limit']); XML_Data::set_limit($input['limit']);
ob_end_clean(); ob_end_clean();
echo XML_Data::albums($albums); echo XML_Data::albums($albums, $input['include']);
} // albums } // albums
/** /**
@ -408,7 +408,7 @@ class Api
public static function album($input) public static function album($input)
{ {
$uid = scrub_in($input['filter']); $uid = scrub_in($input['filter']);
echo XML_Data::albums(array($uid)); echo XML_Data::albums(array($uid), $input['include']);
} // album } // album
/** /**
@ -739,7 +739,7 @@ class Api
if (isset($input['type'])) { if (isset($input['type'])) {
$type = $input['type']; $type = $input['type'];
} }
switch ($type) { switch ($type) {
case 'artist': case 'artist':
echo XML_Data::artists($results); echo XML_Data::artists($results);
@ -1061,7 +1061,7 @@ class Api
$type = $input['type']; $type = $input['type'];
$id = $input['id']; $id = $input['id'];
$rating = $input['rating']; $rating = $input['rating'];
if (!Core::is_library_item($type) || !$id) { if (!Core::is_library_item($type) || !$id) {
echo XML_Data::error('401', T_('Wrong library item type.')); echo XML_Data::error('401', T_('Wrong library item type.'));
} else { } else {
@ -1087,7 +1087,7 @@ class Api
$username = $input['username']; $username = $input['username'];
$limit = intval($input['limit']); $limit = intval($input['limit']);
$since = intval($input['since']); $since = intval($input['since']);
if (!empty($username)) { if (!empty($username)) {
$user = User::get_from_username($username); $user = User::get_from_username($username);
if ($user !== null) { if ($user !== null) {
@ -1115,7 +1115,7 @@ class Api
if (AmpConfig::get('sociable')) { if (AmpConfig::get('sociable')) {
$limit = intval($input['limit']); $limit = intval($input['limit']);
$since = intval($input['since']); $since = intval($input['since']);
if ($GLOBALS['user']->id > 0) { if ($GLOBALS['user']->id > 0) {
$activities = Useractivity::get_friends_activities($GLOBALS['user']->id, $limit, $since); $activities = Useractivity::get_friends_activities($GLOBALS['user']->id, $limit, $since);
ob_end_clean(); ob_end_clean();

View file

@ -287,9 +287,11 @@ class XML_Data
* we want * we want
* *
* @param array $artists (description here...) * @param array $artists (description here...)
* @param array $include Array of other items to include.
* @param bool $full_xml whether to return a full XML document or just the node.
* @return string return xml * @return string return xml
*/ */
public static function artists($artists) public static function artists($artists, $include=[], $full_xml=true)
{ {
if (count($artists) > self::$limit or self::$offset > 0) { if (count($artists) > self::$limit or self::$offset > 0) {
$artists = array_splice($artists,self::$offset,self::$limit); $artists = array_splice($artists,self::$offset,self::$limit);
@ -309,11 +311,23 @@ class XML_Data
// Build the Art URL, include session // Build the Art URL, include session
$art_url = AmpConfig::get('web_path') . '/image.php?object_id=' . $artist_id . '&object_type=artist&auth=' . scrub_out($_REQUEST['auth']); $art_url = AmpConfig::get('web_path') . '/image.php?object_id=' . $artist_id . '&object_type=artist&auth=' . scrub_out($_REQUEST['auth']);
// Handle includes
if (in_array("albums", $include)) {
$albums = self::albums($artist->get_albums(null, true), $include, false);
} else {
$albums = ($artist->albums ?: 0);
}
if (in_array("songs", $include)) {
$songs = self::songs($artist->get_songs(), '', false);
} else {
$songs = ($artist->songs ?: 0);
}
$string .= "<artist id=\"" . $artist->id . "\">\n" . $string .= "<artist id=\"" . $artist->id . "\">\n" .
"\t<name><![CDATA[" . $artist->f_full_name . "]]></name>\n" . "\t<name><![CDATA[" . $artist->f_full_name . "]]></name>\n" .
$tag_string . $tag_string .
"\t<albums>" . ($artist->albums ?: 0) . "</albums>\n" . "\t<albums>" . $albums . "</albums>\n" .
"\t<songs>" . ($artist->songs ?: 0) . "</songs>\n" . "\t<songs>" . $songs . "</songs>\n" .
"\t<art><![CDATA[$art_url]]></art>\n" . "\t<art><![CDATA[$art_url]]></art>\n" .
"\t<preciserating>" . ($rating->get_user_rating() ?: 0) . "</preciserating>\n" . "\t<preciserating>" . ($rating->get_user_rating() ?: 0) . "</preciserating>\n" .
"\t<rating>" . ($rating->get_user_rating() ?: 0) . "</rating>\n" . "\t<rating>" . ($rating->get_user_rating() ?: 0) . "</rating>\n" .
@ -325,7 +339,7 @@ class XML_Data
"</artist>\n"; "</artist>\n";
} // end foreach artists } // end foreach artists
return self::output_xml($string); return self::output_xml($string, $full_xml);
} // artists } // artists
/** /**
@ -334,9 +348,11 @@ class XML_Data
* This echos out a standard albums XML document, it pays attention to the limit * This echos out a standard albums XML document, it pays attention to the limit
* *
* @param array $albums (description here...) * @param array $albums (description here...)
* @param array $include Array of other items to include.
* @param bool $full_xml whether to return a full XML document or just the node.
* @return string return xml * @return string return xml
*/ */
public static function albums($albums) public static function albums($albums, $include=[], $full_xml=true)
{ {
if (count($albums) > self::$limit or self::$offset > 0) { if (count($albums) > self::$limit or self::$offset > 0) {
$albums = array_splice($albums,self::$offset,self::$limit); $albums = array_splice($albums,self::$offset,self::$limit);
@ -364,8 +380,15 @@ class XML_Data
$string .= "\t<artist id=\"$album->artist_id\"><![CDATA[$album->artist_name]]></artist>\n"; $string .= "\t<artist id=\"$album->artist_id\"><![CDATA[$album->artist_name]]></artist>\n";
} }
// Handle includes
if (in_array("songs", $include)) {
$songs = self::songs($album->get_songs(), '', false);
} else {
$songs = $album->song_count;
}
$string .= "\t<year>" . $album->year . "</year>\n" . $string .= "\t<year>" . $album->year . "</year>\n" .
"\t<tracks>" . $album->song_count . "</tracks>\n" . "\t<tracks>" . $songs . "</tracks>\n" .
"\t<disk>" . $album->disk . "</disk>\n" . "\t<disk>" . $album->disk . "</disk>\n" .
self::tags_string($album->tags) . self::tags_string($album->tags) .
"\t<art><![CDATA[$art_url]]></art>\n" . "\t<art><![CDATA[$art_url]]></art>\n" .
@ -376,7 +399,7 @@ class XML_Data
"</album>\n"; "</album>\n";
} // end foreach } // end foreach
return self::output_xml($string); return self::output_xml($string, $full_xml);
} // albums } // albums
/** /**
@ -419,7 +442,7 @@ class XML_Data
* This returns an xml document from an array of song ids. * This returns an xml document from an array of song ids.
* (Spiffy isn't it!) * (Spiffy isn't it!)
*/ */
public static function songs($songs, $playlist_data='') public static function songs($songs, $playlist_data='', $full_xml=true)
{ {
if (count($songs) > self::$limit or self::$offset > 0) { if (count($songs) > self::$limit or self::$offset > 0) {
$songs = array_slice($songs, self::$offset, self::$limit); $songs = array_slice($songs, self::$offset, self::$limit);
@ -495,7 +518,7 @@ class XML_Data
$string .= "</song>\n"; $string .= "</song>\n";
} // end foreach } // end foreach
return self::output_xml($string); return self::output_xml($string, $full_xml);
} // songs } // songs
/** /**
@ -665,9 +688,17 @@ class XML_Data
return self::output_xml($string); return self::output_xml($string);
} // shouts } // shouts
public static function output_xml($string) public static function output_xml($string, $full_xml=true)
{ {
return self::_header() . UI::clean_utf8($string) . self::_footer(); $xml = "";
if ($full_xml) {
$xml .= self::_header();
}
$xml .= UI::clean_utf8($string);
if ($full_xml) {
$xml .= self::_footer();
}
return $xml;
} }
/** /**