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

Commit ea52122b authored by elleo's avatar elleo

Make User's getScrobbles/NowPlaying use the general Server method to reduce code duplication

Make the front page scrobble/nowplaying artist links use the new URL system
parent 0fc9da5a
......@@ -38,10 +38,14 @@ class Server {
* @param int $number The number of scrobbles to return
* @return An array of scrobbles or a PEAR_Error in case of failure
*/
static function getRecentScrobbles($number) {
static function getRecentScrobbles($number=10, $username=false) {
global $mdb2;
$res = $mdb2->query('SELECT username, artist, track, time FROM Scrobbles ORDER BY time DESC LIMIT 10');
if($username) {
$res = $mdb2->query('SELECT username, artist, track, time FROM Scrobbles WHERE username = ' . $mdb2->quote($username, "text") . ' ORDER BY time DESC LIMIT ' . $mdb2->quote($number, "integer"));
} else {
$res = $mdb2->query('SELECT username, artist, track, time FROM Scrobbles ORDER BY time DESC LIMIT ' . $mdb2->quote($number, "integer"));
}
if(PEAR::isError($res)) {
return $res;
......@@ -50,7 +54,8 @@ class Server {
$data = $res->fetchAll(MDB2_FETCHMODE_ASSOC);
foreach($data as $i) {
$row = sanitize($i);
$row["userurl"] = Server::getUserURL($i["username"]);
$row["userurl"] = Server::getUserURL($row["username"]);
$row["artisturl"] = Server::getArtistURL($row["artist"]);
$result[] = $row;
}
......@@ -63,10 +68,14 @@ class Server {
* @param int $number The maximum number of tracks to return
* @return An array of now playing data or a PEAR_Error in case of failure
*/
static function getNowPlaying($number) {
static function getNowPlaying($number, $username=false) {
global $mdb2;
$res = $mdb2->query('SELECT username, artist, track, client, ClientCodes.name, ClientCodes.url from Now_Playing LEFT OUTER JOIN Scrobble_Sessions ON Now_Playing.sessionid=Scrobble_Sessions.sessionid LEFT OUTER JOIN ClientCodes ON Scrobble_Sessions.client=ClientCodes.code ORDER BY Now_Playing.expires DESC LIMIT ' . $mdb2->quote($number, "integer"));
if($username) {
$res = $mdb2->query('SELECT username, artist, track, client, ClientCodes.name, ClientCodes.url from Now_Playing LEFT OUTER JOIN Scrobble_Sessions ON Now_Playing.sessionid=Scrobble_Sessions.sessionid LEFT OUTER JOIN ClientCodes ON Scrobble_Sessions.client=ClientCodes.code WHERE username = ' . $mdb2->quote($username, "text") . ' ORDER BY Now_Playing.expires DESC LIMIT ' . $mdb2->quote($number, "integer"));
} else {
$res = $mdb2->query('SELECT username, artist, track, client, ClientCodes.name, ClientCodes.url from Now_Playing LEFT OUTER JOIN Scrobble_Sessions ON Now_Playing.sessionid=Scrobble_Sessions.sessionid LEFT OUTER JOIN ClientCodes ON Scrobble_Sessions.client=ClientCodes.code ORDER BY Now_Playing.expires DESC LIMIT ' . $mdb2->quote($number, "integer"));
}
if(PEAR::isError($res)) {
return $res;
......@@ -82,6 +91,7 @@ class Server {
}
$row["clientstr"] = $clientstr;
$row["userurl"] = Server::getUserURL($row["username"]);
$row["artisturl"] = Server::getArtistURL($row["artist"]);
$result[] = $row;
}
......
......@@ -64,16 +64,7 @@ class User {
* @return An array of scrobbles
*/
function getScrobbles($number) {
global $mdb2;
$res = $mdb2->query('SELECT * FROM Scrobbles WHERE username = ' .$mdb2->quote($this->name, 'text') . ' ORDER BY time DESC LIMIT '.$mdb2->quote($number, 'integer'));
$data = $res->fetchAll(MDB2_FETCHMODE_ASSOC);
foreach($data as &$i) {
$row = sanitize($i);
$row['timehuman'] = human_timestamp($row['time']);
$row['artisturl'] = Server::getArtistURL($row['artist']);
$result[] = $row;
}
return $result;
return Server::getRecentScrobbles($number, $this->name);
}
/**
......@@ -95,25 +86,8 @@ class User {
*
* @return An array of nowplaying data
*/
function getNP() {
global $mdb2;
$res = $mdb2->query('SELECT username, artist, track, client, ClientCodes.name, ClientCodes.url FROM Now_Playing LEFT OUTER JOIN Scrobble_Sessions ON Now_Playing.sessionid=Scrobble_Sessions.sessionid LEFT OUTER JOIN ClientCodes ON Scrobble_Sessions.client=ClientCodes.code WHERE username=' . $mdb2->quote($this->name, 'text'));
$data = $res->fetchAll(MDB2_FETCHMODE_ASSOC);
foreach($data as &$i) {
$row = sanitize($i);
if($row["name"] == "") {
$clientstr = strip_tags(stripslashes($row["client"])) . " (unknown, please tell us what this is)";
} else {
$clientstr = "<a href=\"" . strip_tags(stripslashes($row["url"])) . "\">" . strip_tags(stripslashes($row["name"])) . "</a>";
}
$row["clientstr"] = $clientstr;
$row["artisturl"] = Server::getArtistURL($row["artist"]);
$result[] = $row;
}
return $result;
function getNowPlaying($number) {
return Server::getNowPlaying($number, $this->name);
}
}
......
......@@ -41,7 +41,7 @@ if(isset($user->name)) {
$smarty->assign('scrobbles', $user->getScrobbles(20));
$smarty->assign('userlevel', $user->userlevel);
$smarty->assign('avatar', $user->getAvatar());
$smarty->assign('nowplaying', $user->getNP());
$smarty->assign('nowplaying', $user->getNowPlaying(10));
$smarty->display('profile.tpl');
} else {
$smarty->assign('error', 'User not found');
......
......@@ -10,7 +10,7 @@ musical tastes with other people.</p>
{section name=np loop=$nowplaying}
<li>
<dl>
<dt><a href="artist.php?artist={$nowplaying[np].artist|stripslashes|urlencode}">
<dt><a href="{$nowplaying[np].artisturl}">
{$nowplaying[np].artist|stripslashes|htmlspecialchars}</a></dt>
<dd>{$nowplaying[np].track|stripslashes|htmlspecialchars}</dd>
<dd><a href="{$nowplaying[np].userurl}">{$nowplaying[np].username|stripslashes|htmlspecialchars}</a></dd>
......@@ -26,7 +26,7 @@ musical tastes with other people.</p>
{section name=recent loop=$recenttracks}
<li>
<dl>
<dt><a href="artist.php?artist={$recenttracks[recent].artist|stripslashes|urlencode}">
<dt><a href="{$recenttracks[recent].artisturl}">
{$recenttracks[recent].artist|stripslashes|htmlspecialchars}</a></dt>
<dd>{$recenttracks[recent].track|stripslashes|htmlspecialchars}</dd>
<dd><a href="{$recenttracks[recent].userurl}">{$recenttracks[recent].username|stripslashes|htmlspecialchars}</a></dd>
......
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