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

Artist.php 4.31 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 53 54 55 56 57 58 59
		if($mbid) {
			$row = $adodb->CacheGetRow(1200, 'SELECT name, mbid, streamable, bio_published, bio_content, bio_summary, image_small, image_medium, image_large FROM Artist WHERE '
				. 'mbid = ' . $adodb->qstr($mbid) . ' OR '
				. 'name = ' . $adodb->qstr($name));
		} else {
			$row = $adodb->CacheGetRow(1200, 'SELECT name, mbid, streamable, bio_published, bio_content, bio_summary, image_small, image_medium, image_large FROM Artist WHERE '
				. 'name = ' . $adodb->qstr($name));
		}
clint's avatar
clint committed
60
		if(!$row) {
61
			throw new Exception('No such artist' . $name);
62
		} else {
63 64 65 66 67 68 69 70 71
			$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
72 73

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

	/**
	 * Retrieves all an artist's albums
	 *
	 * @return An array of Album objects
	 */
Matt Lee's avatar
Matt Lee committed
82
	function getAlbums() {
clint's avatar
clint committed
83 84 85
		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
86
			. $adodb->qstr($this->name));
clint's avatar
clint committed
87
		foreach($res as &$row) {
88
			$albums[] = new Album($row['name'], $this->name);
89
		}
clint's avatar
clint committed
90

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

	/**
	 * Retrieves all an artist's tracks
	 *
	 * @return An array of Track objects
	 */
	function getTracks() {
clint's avatar
clint committed
100 101 102 103 104
		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) {
105
			$tracks[] = new Track($row['name'], $this->name);
106 107 108 109 110
		}

		return $tracks;
	}

111 112 113 114 115 116 117
	/**
	 * 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
118 119 120
		global $adodb;
		$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
		$res = $adodb->CacheGetAll(600,
121
			'SELECT track, COUNT(track) AS freq, COUNT(DISTINCT userid) AS listeners FROM Scrobbles WHERE'
clint's avatar
clint committed
122 123 124
			. ' artist = ' . $adodb->qstr($this->name)
			. ' GROUP BY track ORDER BY freq DESC LIMIT ' . (int)($number));
		foreach($res as &$row) {
125 126 127
			$track = new Track($row['track'], $this->name);
			$track->setPlayCount($row['freq']);
			$track->setListenerCount($row['listeners']);
128 129
			$tracks[] = $track;
		}
130

131 132
		return $tracks;
	}
133

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

143
}