We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit 5a9c6ebd authored by Mike Sheldon's avatar Mike Sheldon

Implement real track.getTags method to fetch a user's tags for a specific track

parent 3d5d69b5
......@@ -216,7 +216,7 @@ if (isset($_POST['install'])) {
PRIMARY KEY(code))");
$adodb->Execute("CREATE TABLE Tags(
username VARCHAR(64) REFERENCES Users(username),
userid INTEGER REFERENCES Users(uniqueid),
tag VARCHAR(64),
artist VARCHAR(255) REFERENCES Artist(name),
album VARCHAR(255),
......
......@@ -77,6 +77,7 @@ $method_map = array(
'radio.tune' => method_radio_tune,
'radio.getplaylist' => method_radio_getPlaylist,
'track.gettoptags' => method_track_getTopTags,
'track.gettags' => method_track_getTags,
);
function method_user_getrecenttracks() {
......@@ -391,6 +392,28 @@ function method_track_getTopTags() {
print(XML::prettyXML(TrackXML::getTopTags($_GET['artist'], $_GET['track'])));
}
function method_track_getTags() {
global $adodb;
if (!isset($_GET['artist']) || !isset($_GET['track']) || !isset($_GET['sk']) || !isset($_GET['token'])) {
report_failure(LFM_INVALID_PARAMS);
}
$username = $adodb->GetOne('SELECT username FROM Auth WHERE '
. 'token = ' . $adodb->qstr($_GET['token']) . ' AND '
. 'username IS NOT NULL AND sk = '.$adodb->qstr($_GET['sk']));
if (!$username) {
report_failure(LFM_INVALID_SESSION);
}
$userid = $adodb->GetOne('SELECT uniqueid FROM Users WHERE '
. 'username = ' . $adodb->qstr($username));
header('Content-Type: text/xml');
print(XML::prettyXML(TrackXML::getTags($_GET['artist'], $_GET['track'], $userid)));
}
function valid_api_key($key) {
return strlen($key) == 32;
......
......@@ -45,5 +45,26 @@ class TrackXML {
}
public static function getTags($artist, $name, $userid) {
$track = new Track($name, $artist);
$tags = $track->getTags($userid);
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
$root = $xml->addChild('tags', null);
$root->addAttribute('artist', $artist);
$root->addAttribute('track', $name);
foreach($tags as $tag) {
$tag_node = $root->addChild('tag', null);
$tag_node->addChild('name', repamp($tag));
}
return($xml);
}
}
?>
......@@ -158,7 +158,7 @@ class Track {
global $adodb;
$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
$res = $adodb->CacheGetAll(600, 'SELECT COUNT(track) AS freq, tag FROM tags WHERE'
$res = $adodb->CacheGetAll(600, 'SELECT COUNT(tag) AS freq, tag FROM tags WHERE'
. ' artist = ' . $adodb->qstr($this->artist_name)
. ' AND track = ' . $adodb->qstr($this->name)
. ' GROUP BY tag ORDER BY freq DESC');
......@@ -166,4 +166,25 @@ class Track {
return $res;
}
/**
* Retrieve a specific user's tags for this track.
*
* @return An array of tag names.
*/
function getTags($userid) {
global $adodb;
$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
$res = $adodb->GetAll('SELECT tag FROM tags WHERE'
. ' artist = ' . $adodb->qstr($this->artist_name)
. ' AND track = ' . $adodb->qstr($this->name)
. ' AND userid = ' . $userid);
foreach($res as &$row) {
$tags[] = $row['tag'];
}
return $tags;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment