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

Artist.php 4.14 KB
Newer Older
1 2
<?php

3
/* GNU FM -- a free network service for sharing your music listening habits
4

5
   Copyright (C) 2009 Free Software Foundation, Inc
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

   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/>.

*/


23
require_once($install_path . '/database.php');
24 25 26 27
require_once($install_path . '/data/sanitize.php');
require_once($install_path . '/data/Album.php');
require_once($install_path . '/data/Track.php');
require_once($install_path . '/data/Server.php');
tobyink's avatar
tobyink committed
28
require_once($install_path . '/utils/linkeddata.php');
29 30 31 32

/**
 * Represents artist data
 *
clint's avatar
clint committed
33
 * General artist attributes are accessible as public variables.
34 35 36 37 38 39
 * Lists of tracks and albums are only generated when requested.
 */
class Artist {


	public $name, $mbid, $streamable, $bio_content, $bio_published, $bio_summary, $image_small, $image_medium, $image_large;
tobyink's avatar
tobyink committed
40
	public $id;
41 42 43 44

	/**
	 * Artist constructor
	 *
45 46
	 * @param string $name The name of the artist to load
	 * @param string $mbid The mbid of the artist (optional)
47
	 */
48
	function __construct($name, $mbid=false) {
clint's avatar
clint committed
49
		global $adodb;
50

clint's avatar
clint committed
51
		$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
52
		$mbidquery = "";
53
		if($mbid) {
54
			$mbidquery = 'mbid = ' . $adodb->qstr($mbid) . ' OR ';
55
		}
56 57 58
		$row = $adodb->CacheGetRow(1200, 'SELECT name, mbid, streamable, bio_published, bio_content, bio_summary, image_small, image_medium, image_large FROM Artist WHERE '
			. $mbidquery
			. 'name = ' . $adodb->qstr($name));
clint's avatar
clint committed
59
		if(!$row) {
60
			throw new Exception('No such artist' . $name);
61
		} else {
62 63 64 65 66 67 68 69 70
			$this->name = $row['name'];
			$this->mbid = $row['mbid'];
			$this->streamable = $row['streamable'];
			$this->bio_published = $row['bio_published'];
			$this->bio_content = $row['bio_content'];
			$this->bio_summary = $row['bio_summary'];
			$this->image_small = $row['image_small'];
			$this->image_medium = $row['image_medium'];
			$this->image_large = $row['image_large'];
tobyink's avatar
tobyink committed
71 72

			$this->id = identifierArtist(null, $this->name, null, null, null, null, $this->mbid, null);
73 74 75 76 77 78 79 80
		}
	}

	/**
	 * Retrieves all an artist's albums
	 *
	 * @return An array of Album objects
	 */
Matt Lee's avatar
Matt Lee committed
81
	function getAlbums() {
clint's avatar
clint committed
82 83 84
		global $adodb;
		$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
		$res = $adodb->CacheGetAll(600, 'SELECT name, image FROM Album WHERE artist_name = '
Matt Lee's avatar
Matt Lee committed
85
			. $adodb->qstr($this->name));
clint's avatar
clint committed
86
		foreach($res as &$row) {
87
			$albums[] = new Album($row['name'], $this->name);
88
		}
clint's avatar
clint committed
89

90 91 92 93 94 95 96 97 98
		return $albums;
	}

	/**
	 * Retrieves all an artist's tracks
	 *
	 * @return An array of Track objects
	 */
	function getTracks() {
clint's avatar
clint committed
99 100 101 102 103
		global $adodb;
		$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
		$res = $adodb->CacheGetAll(600, 'SELECT name FROM Track WHERE artist_name = '
			. $adodb->qstr($this->name));
		foreach($res as &$row) {
104
			$tracks[] = new Track($row['name'], $this->name);
105 106 107 108 109
		}

		return $tracks;
	}

110 111 112 113 114 115 116
	/**
	 * Retrieves an artist's most popular tracks
	 *
	 * @param int $tracks the number of tracks to return
	 * @return An array of Track objects
	 */
	function getTopTracks($number) {
clint's avatar
clint committed
117 118 119
		global $adodb;
		$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
		$res = $adodb->CacheGetAll(600,
120
			'SELECT track, COUNT(track) AS freq, COUNT(DISTINCT userid) AS listeners FROM Scrobbles WHERE'
clint's avatar
clint committed
121 122 123
			. ' artist = ' . $adodb->qstr($this->name)
			. ' GROUP BY track ORDER BY freq DESC LIMIT ' . (int)($number));
		foreach($res as &$row) {
124 125 126
			$track = new Track($row['track'], $this->name);
			$track->setPlayCount($row['freq']);
			$track->setListenerCount($row['listeners']);
127 128
			$tracks[] = $track;
		}
129

130 131
		return $tracks;
	}
132

133 134 135 136 137 138
	/**
	 * Gives the URL for this artist
	 *
	 * @return A string containing the URL of this artist
	 */
	function getURL() {
elleo's avatar
elleo committed
139
		return Server::getArtistURL($this->name);
140 141
	}

142
}