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

Track.php 4.34 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
require_once($install_path . '/data/Artist.php');
require_once($install_path . '/data/Album.php');
require_once($install_path . '/data/Server.php');
27 28
require_once($install_path . '/utils/resolve-external.php');
require_once($install_path . '/utils/licenses.php');
29
require_once($install_path . '/utils/linkeddata.php');
30 31 32 33

/**
 * Represents track data
 *
clint's avatar
clint committed
34
 * All track attributes are accessible as public variables.
35 36 37
 */
class Track {

38
	public $name, $artist_name, $album_name, $mbid, $duration, $streamable, $license, $downloadurl, $streamurl;
39
	public $id;
40

41 42
	private $_playcount = false, $_listenercount = false;

43 44 45
	/**
	 * Track constructor
	 *
46 47
	 * @param string $name The name of the track to load
	 * @param string $artist The name of the artist who recorded this track
48 49
	 */
	function __construct($name, $artist) {
clint's avatar
clint committed
50 51 52 53
		global $adodb;
		$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
		$res = $adodb->CacheGetRow(600, 'SELECT name, artist_name, album_name, duration, streamable, license, downloadurl, streamurl, mbid FROM Track WHERE '
			. 'name = ' . $adodb->qstr($name) . ' AND '
clint's avatar
clint committed
54
			. 'artist_name = ' . $adodb->qstr($artist));
clint's avatar
clint committed
55
		if(!$res) {
56
			$this->name = 'No such track: ' . $name;
elleo's avatar
elleo committed
57
		} else {
clint's avatar
clint committed
58
			$row = $res;
59 60 61 62 63 64 65 66 67 68
			$this->name = $row['name'];
			$this->mbid = $row['mbid'];
			$this->artist_name = $row['artist_name'];
			$this->album_name = $row['album_name'];
			$this->duration = $row['duration'];
			$this->streamable = $row['streamable'];
			$this->license = simplify_license($row['license']);
			$this->licenseurl = $row['license'];
			$this->downloadurl = resolve_external_url($row['downloadurl']);
			$this->streamurl = resolve_external_url($row['streamurl']);
clint's avatar
clint committed
69

70
			$this->id = identifierTrack(null, $this->artist_name, $this->name, $this->album_name, null, $this->mbid, null, null);
elleo's avatar
elleo committed
71 72
		}

73 74
	}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	/**
	 * Sets the playcount
	 *
	 * @param int $playcount The number of plays this track has received
	 */
	function setPlayCount($playcount) {
		$this->_playcount = $playcount;
	}

	/**
	 * Sets the number of listeners
	 *
	 * @param int $listeners The number of people who've listened to this track
	 */
	function setListenerCount($listeners) {
		$this->_listenercount = $listeners;
	}

	/**
	 *
	 * @return An int indicating the number of times this track has been played
	 */
	function getPlayCount() {
		if($this->_playcount) {
			// If we've been given a cached value from another SQL call use that
			return $this->_playcount;
		}

		$this->_getPlayCountAndListenerCount();
		return $this->_playcount;
	}

	/**
	 *
	 * @return An int indicating the number of listeners this track has
	 */
	function getListenerCount() {
		if($this->_listeners) {
			return $this->_listenercount;
		}

		$this->_getPlayCountAndListenerCount();
		return $this->_listenercount;
	}


	private function _getPlayCountAndListenerCount() {
clint's avatar
clint committed
122
		global $adodb;
123

clint's avatar
clint committed
124
		$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
125
		$row = $adodb->CacheGetRow(300, 'SELECT COUNT(track) AS freq, COUNT(DISTINCT userid) AS listeners FROM Scrobbles WHERE'
clint's avatar
clint committed
126 127
			. ' artist = ' . $adodb->qstr($this->artist_name)
			. ' AND track = ' . $adodb->qstr($this->name)
128
			. ' GROUP BY track ORDER BY freq DESC');
129

130 131 132 133 134 135 136
		if (!isset($row)) {
		        $this->setPlaycount(0);
			$this->setListenerCount(0);
		} else {
			$this->setPlaycount($row['freq']);
			$this->setListenerCount($row['listeners']);
		}
137
	}
138

139 140 141 142 143 144 145 146 147
	/**
	 * Retrieve the artist for this track.
	 *
	 * @return An artist object
	 */
	function getArtist() {
		return new Artist($this->artist_name);
	}

elleo's avatar
elleo committed
148
	function getURL() {
jurgbohn's avatar
jurgbohn committed
149
		return Server::getTrackURL($this->artist_name, $this->album_name, $this->name);
elleo's avatar
elleo committed
150 151
	}

152
}