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

Commit 20883577 authored by tobyink's avatar tobyink

Add Group support to database classes.

parent 76b333e1
......@@ -56,7 +56,6 @@ if (isset($_POST['install'])) {
country_name varchar(200),
wikipedia_en varchar(120));")
$mdb2->query("CREATE TABLE Users (
username VARCHAR(64) PRIMARY KEY,
password VARCHAR(32) NOT NULL,
......@@ -85,7 +84,7 @@ if (isset($_POST['install'])) {
grouptype INTEGER)");
$res = $mdb2->query("CREATE TABLE Group_Members (
group VARCHAR(64) REFERENCES Groups(groupname),
groupname VARCHAR(64) REFERENCES Groups(groupname),
member VARCHAR(64) REFERENCES Users(username),
joined int NOT NULL");
......
<?php
/* Libre.fm -- a free network service for sharing your music listening habits
Copyright (C) 2009 Libre.fm Project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
require_once($install_path . '/database.php');
require_once($install_path . '/data/sanitize.php');
require_once($install_path . '/utils/human-time.php');
require_once($install_path . '/data/Server.php');
require_once($install_path . '/data/TagCloud.php');
require_once($install_path . '/data/User.php');
/**
* Represents Group data
*
* General attributes are accessible as public variables.
*
*/
class Group {
public $name, $owner, $fullname, $bio, $homepage, $count, $grouptype, $id, $avatar_uri, $users;
/**
* User constructor
*
* @param string $name The name of the user to load
*/
function __construct($name, $data=null) {
global $base_url;
$base = preg_replace('#/$#', '', $base_url);
if (is_array($data)) {
$row = $data;
}
else {
global $mdb2;
$res = $mdb2->query('SELECT * FROM Groups WHERE lower(groupname) = ' . $mdb2->quote(strtolower($name), 'text'));
if($res->numRows()) {
$row = $res->fetchRow(MDB2_FETCHMODE_ASSOC);
}
}
if (is_array($row)) {
$this->name = $row['groupname'];
$this->fullname = $row['fullname'];
$this->homepage = $row['homepage'];
$this->bio = $row['bio'];
$this->avatar_uri = $row["avatar_uri"];
$this->owner = new User($row['owner']);
$this->count = -1;
$this->users = array();
if (! preg_match('/\:/', $this->id))
$this->id = $base.'/group/' . urlencode($this->name) . '#group';
}
}
function save ()
{
global $mdb2;
$q = sprintf("UPDATE Groups SET "
. "owner=%s, "
. "fullname=%s, "
. "homepage=%s, "
. "bio=%s, "
. "avatar_uri=%s, "
. "modified=%d "
. "WHERE groupname=%s"
, $mdb2->quote($this->owner->name, 'text')
, $mdb2->quote($this->fullname, 'text')
, $mdb2->quote($this->homepage, 'text')
, $mdb2->quote($this->bio, 'text')
, $mdb2->quote($this->avatar_uri, 'text')
, time()
, $mdb2->quote($this->name, 'text'));
$res = $mdb2->query($q);
if(PEAR::isError($res)) {
header("Content-Type: text/plain");
print_r($res);
exit;
}
return 1;
}
/**
* Retrieve a user's avatar via the gravatar service
*
* @param int $size The desired size of the avatar (between 1 and 512 pixels)
* @return A URL to the user's avatar image
*/
function getAvatar($size=64) {
global $base_uri;
if (!empty($this->avatar_uri))
return $this->avatar_uri;
return $base_uri . "/i/qm50.png";
}
function getURL() {
return Server::getGroupURL($this->name);
}
function tagCloudData () {
return TagCloud::generateTagCloud(
'Scrobbles INNER JOIN Group_Members ON Scrobbles.username=Group_Members.member',
'artist',
40,
$this->name,
'groupname');
}
}
......@@ -21,6 +21,7 @@
require_once($install_path . '/database.php');
require_once($install_path . '/data/Artist.php');
require_once($install_path . '/data/Group.php');
require_once($install_path . '/data/Track.php');
require_once($install_path . '/data/User.php');
require_once($install_path . "/data/sanitize.php");
......@@ -57,7 +58,7 @@ class Server {
a.mbid AS artist_mbid,
l.mbid AS album_mbid,
l.image AS album_image,
l.artwork_license,
l.artwork_license,
t.license
FROM Scrobbles s
LEFT JOIN Artist a
......@@ -86,7 +87,7 @@ class Server {
a.mbid AS artist_mbid,
l.mbid AS album_mbid,
l.image AS album_image,
l.artwork_license,
l.artwork_license,
t.license
FROM Scrobbles s
LEFT JOIN Artist a
......@@ -274,6 +275,15 @@ class Server {
}
}
static function getGroupURL($groupname) {
global $friendly_urls, $base_url;
if($friendly_urls) {
return $base_url . "/group/" . urlencode(stripslashes($groupname));
} else {
return $base_url . "/group.php?group=" . urlencode(stripslashes($groupname));
}
}
static function getArtistURL($artist) {
global $friendly_urls, $base_url;
if($friendly_urls) {
......
......@@ -32,41 +32,40 @@ class TagCloud {
* inaccurate @param float $max_font_size maximum font size (px, em, %, etc)
* @return array tagcloud
*/
static function generateTagCloud($table, $field, $limit = 40, $constraint = null) {
static function generateTagCloud($table, $field, $limit = 40, $constraint = null, $contrained_field = false) {
global $mdb2;
if (!is_string($field)) return false;
if (!is_string($table)) return false;
if (!is_integer($limit)) return false;
$sizes = array('xx-large', 'x-large', 'large', 'medium', 'small', 'x-small', 'xx-small');
$sizes = array('xx-large', 'x-large', 'large', 'medium', 'small', 'x-small', 'xx-small');
$query = "SELECT $field, count(*) AS count FROM $table";
$query .= (!is_null($constraint) || ($table == "Scrobbles")) ? ' WHERE ' : null;
if ($field == "track") {
$query .= (!is_null($constraint)) ? ' artist = ' . $mdb2->quote($constraint, 'text') : null;
} else {
$query .= (!is_null($constraint)) ? ' username = ' . $mdb2->quote($constraint, 'text') : null;
}
if ($contrained_field) {
$query .= (!is_null($constraint)) ? " $constrained_field = " . $mdb2->quote($constraint, 'text') : null;
} elseif ($field == "track") {
$query .= (!is_null($constraint)) ? ' artist = ' . $mdb2->quote($constraint, 'text') : null;
} else {
$query .= (!is_null($constraint)) ? ' username = ' . $mdb2->quote($constraint, 'text') : null;
}
$query .= (!is_null($constraint) && ($table == "Scrobbles")) ? ' AND ' : null;
$query .= ($table == "Scrobbles") ? " rating <> 'S' " : null;
$query .= " GROUP BY $field ORDER BY count DESC LIMIT $limit";
$res = $mdb2->query($query);
if (PEAR::isError($res)) {
echo("ERROR" . $res->getMessage());
}
if (PEAR::isError($res)) {
echo("ERROR" . $res->getMessage());
}
if (!$res->numRows()) {
return false;
return false;
} else {
$data = $res->fetchAll(MDB2_FETCHMODE_ASSOC);
foreach($data as $count => &$i) {
$i['size'] = $sizes[(int) ($count/(count($data)/7))];
}
foreach($data as &$i){
$i['pageurl'] = Server::getArtistURL($i['artist']);
}
sort($data);
return $data;
$data = $res->fetchAll(MDB2_FETCHMODE_ASSOC);
foreach($data as $count => &$i) {
$i['size'] = $sizes[(int) ($count/(count($data)/7))];
}
foreach($data as &$i){
$i['pageurl'] = Server::getArtistURL($i['artist']);
}
sort($data);
return $data;
}
}
}
......
......@@ -97,7 +97,8 @@ class User {
. "userlevel=%d, "
. "webid_uri=%s, "
. "location_uri=%s, "
. "avatar_uri=%s "
. "avatar_uri=%s, "
. "modified=%d "
. "WHERE username=%s"
, $mdb2->quote($this->email, 'text')
, $mdb2->quote($this->password, 'text')
......@@ -109,6 +110,7 @@ class User {
, $mdb2->quote($this->id, 'text')
, (empty($this->location_uri) ? 'NULL' : $mdb2->quote($this->location_uri, 'text'))
, $mdb2->quote($this->avatar_uri, 'text')
, time()
, $mdb2->quote($this->name, 'text'));
$res = $mdb2->query($q);
......
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