summaryrefslogtreecommitdiffstats
path: root/modules/php_musicbrainz/mbQuery.php
blob: 07afd771ad1d9259732ab22eed1f72140c2e66b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
122
123
124
125
126
127
128
129
130
131
132
<?php
/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */
	define ( 'NS_MMD_1', "http://musicbrainz.org/ns/mmd-1.0#" );
	
	require_once( 'xml/xmlParser.php' );
	
	require_once( 'mbUtil.php' );
	require_once( 'mbRelation.php' );
	require_once( 'mbEntity.php' );
	require_once( 'mbArtistAlias.php' );
	require_once( 'mbArtist.php' );
	require_once( 'mbReleaseEvent.php' );
	require_once( 'mbRelease.php' );
	require_once( 'mbTrack.php' );
	require_once( 'mbDisc.php' );
	require_once( 'mbLabel.php' );
	require_once( 'mbLabelAlias.php' );
	require_once( 'mbTag.php' );
	require_once( 'mbResults.php' );
	require_once( 'mbMetadata.php' );
	require_once( 'mbFilter.php' );
	require_once( 'mbInclude.php' );
	require_once( 'mbWebService.php' );
	require_once( 'mbXmlParser.php' );
	require_once( 'mbFactory.php' );

	class mbRequestError extends Exception { }
	class mbResponseError extends Exception { }

	class MusicBrainzQuery {
		private $ws;
		private $ownWs = false;
		private $clientId;

		function MusicBrainzQuery( IWebService $ws=null, $clientId = '' ) {
			if ( $ws != null )
			  $this->ws = $ws;
			else {
			  $this->ws = new mbWebService();
			  $this->ownWs = true;
			}
			
			$this->clientId = $clientId;
		}

		function getUserByName( $name ) {
			$metadata = $this->getFromWebService( "user", "", null, mbUserFilter().name($name) );
			$list = $metadata->getUserList(true);
			
			if ( count($list) > 0 ) {
				return $list[0];
			}
			
			throw mbResponseError("response didn't contain user data");
		}
		
		function getArtists( mbArtistFilter $artist_filters ) {
			$metadata = $this->getFromWebService( "artist", "", null, $artist_filters );
			return $metadata->getArtistResults2(true);
		}

		function getReleases( mbReleaseFilter $release_filters ) {
			$metadata = $this->getFromWebService( "release", "", null, $release_filters );
			return $metadata->getReleaseResults2(true);
		}
		
		function getTracks( mbTrackFilter $track_filters ) {
			$metadata = $this->getFromWebService( "track", "", null, $track_filters );
			return $metadata->getTrackResults2(true);
		}
		
		function getArtistById( $aID, mbArtistIncludes $artist_includes ) {
			try {
				$id = extractUuid($aID);
			} catch ( mbValueError $e ) {
				throw new mbRequestError($e->getMessage(),$e->getCode());
			}
			$metadata = $this->getFromWebService( "artist", $id, $artist_includes );
			$artist = $metadata->getArtist(true);
			return $artist;
		}

		function getReleaseById( $rID, mbReleaseIncludes $release_includes ) {
			try {
				$id = extractUuid($rID);
			} catch ( mbValueError $e ) {
				throw new mbRequestError($e->getMessage(),$e->getCode());
			}
			$metadata = $this->getFromWebService( "release", $id, $release_includes );
			$release = $metadata->getRelease(true);
			return $release;
		}

		function getTrackById( $tID, mbTrackIncludes $track_includes ) {
			try {
				$id = extractUuid($tID);
			} catch ( mbValueError $e ) {
				throw new mbRequestError($e->getMessage(),$e->getCode());
			}
			$metadata = $this->getFromWebService( "track", $id, $track_includes );
			$track = $metadata->getTrack(true);
			return $track;
		}

		protected function getFromWebService( $entity, $id, $includes=null, $filters=null ) {
			$includeList = $includes ? $includes->createIncludeTags() : null;
			$filterList  = $filters  ? $filters->createParameters()  : null;
			$content = $this->ws->get( $entity, $id, $includeList, $filterList );
			
			try {
				$parser = new mbXmlParser();
				$parsed_content = $parser->parse($content);
				return $parsed_content;
			} catch ( mbParseError $e ) {
				throw new mbResponseError( $e->getMessage(), $e->getCode() );
			}
		}

		function submitPuids( array $tracks2puids ) {
			if ( empty($this->clientId) ) {
				throw WebServiceError("Please supply a client ID");
			}
			$params = array(
				array( 'client', $this->clientId )
			);
			foreach ( $tracks2puids as $puid => $track ) {
				$params[] = array( 'puid', extractUuid($puid).' '.$track );
			}
			$this->ws->post("track", "", urlencode($params) );
		}
	}
?>