diff options
author | Paul 'flowerysong' Arthur <flowerysong00@yahoo.com> | 2010-05-07 03:52:53 +0000 |
---|---|---|
committer | Paul 'flowerysong' Arthur <flowerysong00@yahoo.com> | 2010-05-07 03:52:53 +0000 |
commit | 3a13166f1bed31640985ae7913f1a2608384ed8e (patch) | |
tree | dbc20249314679ccd6d2cc4f2b9b19fc1f3895d6 /modules | |
parent | 20d7fa5b085e196a1fcdda88c560bbb39881152a (diff) | |
download | ampache-3a13166f1bed31640985ae7913f1a2608384ed8e.tar.gz ampache-3a13166f1bed31640985ae7913f1a2608384ed8e.tar.bz2 ampache-3a13166f1bed31640985ae7913f1a2608384ed8e.zip |
php_musicbrainz: support for ratings, some code cleanup.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/php_musicbrainz/mbArtist.php | 240 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbArtistAlias.php | 34 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbDisc.php | 102 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbEntity.php | 124 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbFactory.php | 56 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbFilter.php | 311 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbInclude.php | 296 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbLabel.php | 178 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbMetadata.php | 142 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbQuery.php | 267 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbRating.php | 13 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbRelation.php | 220 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbRelease.php | 250 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbResults.php | 108 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbTag.php | 26 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbTrack.php | 118 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbWebService.php | 421 | ||||
-rw-r--r-- | modules/php_musicbrainz/mbXmlParser.php | 702 |
18 files changed, 1858 insertions, 1750 deletions
diff --git a/modules/php_musicbrainz/mbArtist.php b/modules/php_musicbrainz/mbArtist.php index 972ca311..7787463f 100644 --- a/modules/php_musicbrainz/mbArtist.php +++ b/modules/php_musicbrainz/mbArtist.php @@ -1,122 +1,122 @@ <?php -/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - class mbArtist extends MusicBrainzEntity { - const TYPE_GROUP = "http://musicbrainz.org/ns/mmd-1.0#Group"; - const TYPE_PERSON = "http://musicbrainz.org/ns/mmd-1.0#Person"; - - private $type; - private $name; - private $sortName; - private $disambiguation; - private $beginDate; - private $endDate; - private $releases; - private $releasesCount = 0; - private $releasesOffset = 0; - private $aliases = array(); - - function mbArtist( $id = '', $type = '', $name = '', $sortName = '' ) { - parent::MusicBrainzEntity($id); - $this->type = $type; - $this->name = $name; - $this->sortName = $sortName; - $this->releases = array(); - } - - function getType() { return $this->type; } - function setType( $type ) { $this->type = $type; } - function getName() { return $this->name; } - function setName( $name ) { $this->name = $name; } - function getSortName() { return $this->sortName; } - function setSortName( $sortName ) { $this->sortName = $sortName; } - function getDisambiguation() { return $this->disambiguation; } - function setDisambiguation( $disambiguation ) { $this->disambiguation = $disambiguation; } - function getBeginDate() { return $this->beginDate; } - function setBeginDate( $beginDate ) { $this->beginDate = $beginDate; } - function getEndDate() { return $this->endDate; } - function setEndDate( $endDate ) { $this->endDate = $endDate; } - - function getUniqueName() { - return empty($this->disambiguation) ? $this->name : - $this->name . ' (' . $this->disambiguation . ')'; - } - - function &getReleases() { - return $this->releases; - } - - function addRelease( Release $release ) { - $this->releases[] = $release; - } - - function &getAliases() { - return $this->aliases; - } - - function addAlias( AristAlias $alias ) { - $this->aliases[] = $alias; - } - - function getNumReleases() { - return count($this->releases); - } - - function getRelease( $i ) { - return $this->releases[$i]; - } - - function getReleasesOffset() { - return $this->releasesOffset; - } - - function setReleasesOffset( $relOffset ) { - $this->releasesOffset = $relOffset; - } - - function getReleasesCount() { - return $this->releasesCount; - } - - function setReleasesCount( $relCount ) { - $this->releasesCount = $relCount; - } - - function getNumAliases() { - return count($this->aliases); - } - - function getAlias( $i ) { - return $this->aliases[$i]; - } - - function imageURL( MusicBrainzQuery $q ) { - $rf = new ReleaseFilter(); - $rf->artistId( $this->getId() )->limit(5)->asin('')->releaseType(Release::TYPE_ALBUM); - - try { - $rresults = $q->getReleases( $rf ); - } catch ( ResponseError $e ) { - echo $e->getMessage() . " "; - return ""; - } - - if ( empty( $rresults ) ) - return ""; - - $keys = array(); - foreach ( $rresults as $key => $rr ) { - $rr = $rr->getRelease(); - if ( $rr->getAsin() && $rr->getAsin() != "" ) - $keys[] = $key; - } - - if ( sizeof($keys) > 0 ) { - $rand = rand(0,sizeof($keys)-1); - return "http://images.amazon.com/images/P/" . $rresults[$keys[$rand]]->getRelease()->getAsin() . - ".01._SCLZZZZZZZ_PU_PU-5_.jpg!,.-''-,.!" . $rresults[$keys[$rand]]->getRelease()->getTitle(); - } - - return ""; - } - } +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ +class mbArtist extends MusicBrainzEntity { + const TYPE_GROUP = "http://musicbrainz.org/ns/mmd-1.0#Group"; + const TYPE_PERSON = "http://musicbrainz.org/ns/mmd-1.0#Person"; + + private $type; + private $name; + private $sortName; + private $disambiguation; + private $beginDate; + private $endDate; + private $releases; + private $releasesCount = 0; + private $releasesOffset = 0; + private $aliases = array(); + + public function __construct($id = '', $type = '', $name = '', $sortName = '') { + parent::__construct($id); + $this->type = $type; + $this->name = $name; + $this->sortName = $sortName; + $this->releases = array(); + } + + public function getType() { return $this->type; } + public function setType($type) { $this->type = $type; } + public function getName() { return $this->name; } + public function setName($name) { $this->name = $name; } + public function getSortName() { return $this->sortName; } + public function setSortName($sortName) { $this->sortName = $sortName; } + public function getDisambiguation() { return $this->disambiguation; } + public function setDisambiguation($disambiguation) { $this->disambiguation = $disambiguation; } + public function getBeginDate() { return $this->beginDate; } + public function setBeginDate($beginDate) { $this->beginDate = $beginDate; } + public function getEndDate() { return $this->endDate; } + public function setEndDate($endDate) { $this->endDate = $endDate; } + + public function getUniqueName() { + return empty($this->disambiguation) ? $this->name : + $this->name . ' (' . $this->disambiguation . ')'; + } + + public function getReleases() { + return $this->releases; + } + + public function addRelease(Release $release) { + $this->releases[] = $release; + } + + public function getAliases() { + return $this->aliases; + } + + public function addAlias(AristAlias $alias) { + $this->aliases[] = $alias; + } + + public function getNumReleases() { + return count($this->releases); + } + + public function getRelease($i) { + return $this->releases[$i]; + } + + public function getReleasesOffset() { + return $this->releasesOffset; + } + + public function setReleasesOffset($relOffset) { + $this->releasesOffset = $relOffset; + } + + public function getReleasesCount() { + return $this->releasesCount; + } + + public function setReleasesCount($relCount) { + $this->releasesCount = $relCount; + } + + public function getNumAliases() { + return count($this->aliases); + } + + public function getAlias($i) { + return $this->aliases[$i]; + } + + public function imageURL(MusicBrainzQuery $q) { + $rf = new ReleaseFilter(); + $rf->artistId($this->getId())->limit(5)->asin('')->releaseType(Release::TYPE_ALBUM); + + try { + $rresults = $q->getReleases($rf); + } catch (ResponseError $e) { + echo $e->getMessage() . " "; + return ""; + } + + if (empty( $rresults) ) + return ""; + + $keys = array(); + foreach ($rresults as $key => $rr) { + $rr = $rr->getRelease(); + if ($rr->getAsin() && $rr->getAsin() != "") + $keys[] = $key; + } + + if (sizeof($keys) > 0) { + $rand = rand(0,sizeof($keys)-1); + return "http://images.amazon.com/images/P/" . $rresults[$keys[$rand]]->getRelease()->getAsin() . + ".01._SCLZZZZZZZ_PU_PU-5_.jpg!,.-''-,.!" . $rresults[$keys[$rand]]->getRelease()->getTitle(); + } + + return ""; + } +} ?> diff --git a/modules/php_musicbrainz/mbArtistAlias.php b/modules/php_musicbrainz/mbArtistAlias.php index 3b65fcb6..842b7d06 100644 --- a/modules/php_musicbrainz/mbArtistAlias.php +++ b/modules/php_musicbrainz/mbArtistAlias.php @@ -1,21 +1,21 @@ <?php -/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - class mbArtistAlias { - private $value; - private $type; - private $script; +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ +class mbArtistAlias { + private $value; + private $type; + private $script; - function mbAristAlias( $value='', $type='', $script='' ) { - $this->value = $value; - $this->type = $type; - $this->script = $script; - } + public function __construct($value='', $type='', $script='') { + $this->value = $value; + $this->type = $type; + $this->script = $script; + } - function getType() { return $this->type; } - function setType( $type ) { $this->type = $type; } - function getValue() { return $this->value; } - function setValue( $value ) { $this->value = $value; } - function getScript() { return $this->script; } - function setScript( $script ) { $this->script = $script; } - } + public function getType() { return $this->type; } + public function setType($type) { $this->type = $type; } + public function getValue() { return $this->value; } + public function setValue($value) { $this->value = $value; } + public function getScript() { return $this->script; } + public function setScript($script) { $this->script = $script; } +} ?> diff --git a/modules/php_musicbrainz/mbDisc.php b/modules/php_musicbrainz/mbDisc.php index 90dacc06..185774ac 100644 --- a/modules/php_musicbrainz/mbDisc.php +++ b/modules/php_musicbrainz/mbDisc.php @@ -1,53 +1,53 @@ <?php -/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - class mbDiscError extends Exception { } - - class mbDisc { - private $id; - private $sectors = 0; - private $firstTrackNum = 0; - private $lastTrackNum = 0; - private $tracks; - - function mbDisc( $id = '' ) { - $this->id = $id; - $this->tracks = array(); - } - - function setId ( $id ) { $this->id = $id; } - function getId ( ) { return $this->id; } - function setSectors ( $sectr ) { $this->sectors = $sectr; } - function getSectors ( ) { return $this->sectors; } - function setLastTrackNum ( $track ) { $this->lastTrackNum = $track; } - function getLastTrackNum ( ) { return $this->lastTrackNum; } - function setFirstTrackNum( $track ) { $this->firstTrackNum = $track; } - function getFirstTrackNum( ) { return $this->firstTrackNum; } - - function &getTracks() { - return $this->tracks; - } - - function addTrack( array $track ) { - $this->tracks[] = $track; - } - - function readDisc( $deviceName = '' ) { - throw new mbDiscError( "Cannot readDisc()", 1 ); - } - - function getSubmissionUrl( Disc $disc, $host='mm.musicbrainz.org', $port=80 ) { - if ( $port == 80 ) - $netloc = $host; - else - $netloc = $host . ':' . $port; - - $toc = $disc->getFirstTrackNum() . '+' . $disc->getLastTrackNum() . '+' . $disc->getSectors(); - - foreach ( $disc->getTracks() as $track ) - $toc .= '+' . $track[0]; - - return "http://" . $netloc . "/bare/cdlookup.html?id=" . $disc->getId() . "&toc=" . $toc . - "&tracks=" . $disc->getLastTrackNum(); - } - } +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ +class mbDiscError extends Exception { } + +class mbDisc { + private $id; + private $sectors = 0; + private $firstTrackNum = 0; + private $lastTrackNum = 0; + private $tracks; + + public function mbDisc($id = '') { + $this->id = $id; + $this->tracks = array(); + } + + public function setId ($id ) { $this->id = $id; } + public function getId ( ) { return $this->id; } + public function setSectors ($sectr) { $this->sectors = $sectr; } + public function getSectors ( ) { return $this->sectors; } + public function setLastTrackNum ($track) { $this->lastTrackNum = $track; } + public function getLastTrackNum ( ) { return $this->lastTrackNum; } + public function setFirstTrackNum($track) { $this->firstTrackNum = $track; } + public function getFirstTrackNum( ) { return $this->firstTrackNum; } + + public function getTracks() { + return $this->tracks; + } + + public function addTrack(array $track) { + $this->tracks[] = $track; + } + + public function readDisc($deviceName = '') { + throw new mbDiscError("Cannot readDisc()", 1); + } + + public function getSubmissionUrl(Disc $disc, $host='mm.musicbrainz.org', $port=80) { + if ($port == 80) + $netloc = $host; + else + $netloc = $host . ':' . $port; + + $toc = $disc->getFirstTrackNum() . '+' . $disc->getLastTrackNum() . '+' . $disc->getSectors(); + + foreach ($disc->getTracks() as $track) + $toc .= '+' . $track[0]; + + return "http://" . $netloc . "/bare/cdlookup.html?id=" . $disc->getId() . "&toc=" . $toc . + "&tracks=" . $disc->getLastTrackNum(); + } +} ?> diff --git a/modules/php_musicbrainz/mbEntity.php b/modules/php_musicbrainz/mbEntity.php index 86cfdc07..211890f7 100644 --- a/modules/php_musicbrainz/mbEntity.php +++ b/modules/php_musicbrainz/mbEntity.php @@ -1,77 +1,77 @@ <?php -/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - class MusicBrainzEntity { - private $id; - private $relations; - private $tags; +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ +abstract class MusicBrainzEntity { + private $id; + private $relations; + private $tags; - protected function MusicBrainzEntity( $id ) { - $this->id = $id; - $this->relations = array(); - } + public function __construct($id) { + $this->id = $id; + $this->relations = array(); + } - function getId() { - return $this->id; - } + public function getId() { + return $this->id; + } - function setId( $id ) { - $this->id = $id; - } + public function setId($id) { + $this->id = $id; + } - function &getRelations( $target_type='', $relation_type='' ) { - if ( $target_type == '' && $relation_type == '' ) - return $this->relations; + public function getRelations($target_type='', $relation_type='') { + if ($target_type == '' && $relation_type == '') + return $this->relations; - $result = array(); + $result = array(); - if ( empty($target_type) ) { - foreach ( $this->relations as $relation ) { - if ( $relation->getType() == $relation_type ) { - $result[] = $relation; - } - } - } - else if ( empty($relation_type) ) { - foreach ( $this->relation_tracks as $relation ) { - if ( $relation->getTargetType() == $target_type ) { - $result[] = $relation; - } - } - } - else { - foreach ( $this->relations as $relation ) { - if ( $relation->getTargetType() == $target_type - && $relation->getType() == $relation_type ) { - $result[] = $relation; - } - } - } + if (empty($target_type)) { + foreach ($this->relations as $relation) { + if ($relation->getType() == $relation_type) { + $result[] = $relation; + } + } + } + else if (empty($relation_type)) { + foreach ($this->relation_tracks as $relation) { + if ($relation->getTargetType() == $target_type) { + $result[] = $relation; + } + } + } + else { + foreach ($this->relations as $relation) { + if ($relation->getTargetType() == $target_type + && $relation->getType() == $relation_type) { + $result[] = $relation; + } + } + } - return $result; - } + return $result; + } - function addRelation( mbRelation $relation ) { - $this->relations[] = $relation; - } + public function addRelation(mbRelation $relation) { + $this->relations[] = $relation; + } - function getNumRelations() { - return count($this->relations); - } + public function getNumRelations() { + return count($this->relations); + } - function &getRelation( $i ) { - return $this->relations[$i]; - } + public function getRelation($i) { + return $this->relations[$i]; + } - function &getTags() { - return $this->tags; - } + public function getTags() { + return $this->tags; + } - function getNumTags() { - return count($this->tags); - } + public function getNumTags() { + return count($this->tags); + } - function &getTag( $i ) { - return $this->tags[$i]; - } - } + public function getTag($i) { + return $this->tags[$i]; + } +} ?> diff --git a/modules/php_musicbrainz/mbFactory.php b/modules/php_musicbrainz/mbFactory.php index e68a7e32..c483aa99 100644 --- a/modules/php_musicbrainz/mbFactory.php +++ b/modules/php_musicbrainz/mbFactory.php @@ -1,30 +1,32 @@ <?php -/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - interface mbIFactory { - function newArtist(); - function newArtistAlias(); - function newDisc(); - function newTrack(); - function newRelation(); - function newRelease(); - function newReleaseEvent(); - function newTag(); - function newLabel(); - function newLabelAlias(); - function newUser(); - } +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ +interface mbIFactory { + function newArtist(); + function newArtistAlias(); + function newDisc(); + function newTrack(); + function newRating(); + function newRelation(); + function newRelease(); + function newReleaseEvent(); + function newTag(); + function newLabel(); + function newLabelAlias(); + function newUser(); +} - class mbDefaultFactory implements mbIFactory { - function newArtist() { return new mbArtist(); } - function newArtistAlias() { return new mbArtistAlias(); } - function newDisc() { return new mbDisc(); } - function newTrack() { return new mbTrack(); } - function newRelation() { return new mbRelation(); } - function newRelease() { return new mbRelease(); } - function newReleaseEvent() { return new mbReleaseEvent(); } - function newTag() { return new mbTag(); } - function newLabel() { return new mbLabel(); } - function newLabelAlias() { return new mbLabelAlias(); } - function newUser() { return new mbUser(); } - } +class mbDefaultFactory implements mbIFactory { + function newArtist() { return new mbArtist(); } + function newArtistAlias() { return new mbArtistAlias(); } + function newDisc() { return new mbDisc(); } + function newTrack() { return new mbTrack(); } + function newRating() { return new mbRating(); } + function newRelation() { return new mbRelation(); } + function newRelease() { return new mbRelease(); } + function newReleaseEvent() { return new mbReleaseEvent(); } + function newTag() { return new mbTag(); } + function newLabel() { return new mbLabel(); } + function newLabelAlias() { return new mbLabelAlias(); } + function newUser() { return new mbUser(); } +} ?> diff --git a/modules/php_musicbrainz/mbFilter.php b/modules/php_musicbrainz/mbFilter.php index a759fffa..bb99fde8 100644 --- a/modules/php_musicbrainz/mbFilter.php +++ b/modules/php_musicbrainz/mbFilter.php @@ -1,150 +1,167 @@ <?php -/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - interface MusicBrainzFilter { - function createParameters(); - } - - class mbArtistFilter implements MusicBrainzFilter { - private $parameterList = array(); - - function createParameters() { - return $this->parameterList; - } - - function name( $name ) { - $this->parameterList['name'] = $name; - return $this; - } - - function limit( $limit ) { - $this->parameterList['limit'] = $limit; - return $this; - } - - function offset( $offset ) { - $this->parameterList['offset'] = $offset; - return $this; - } - } - - class mbReleaseFilter implements MusicBrainzFilter { - private $parameters = array(); - - function createParameters() { - return $this->parameters; - } - - function title( $title ) { - $this->parameters['title'] = $title; - return $this; - } - - function discId( $discid ) { - $this->parameters['discid'] = $discid; - return $this; - } - - function releaseType( $rtype ) { - $type = extractFragment($rtype); - - if ( isset( $this->parameters['releasetypes'] ) ) { - $this->parameters['releasetypes'] .= ' ' . $type; - } - else { - $this->parameters['releasetypes'] = $type; - } - - return $this; - } - - function artistName( $name ) { - $this->parameters['artist'] = $name; - return $this; - } - - function artistId( $id ) { - $this->parameters['artistid'] = $id; - return $this; - } - - function asin( $asin ) { - $this->parameters['asin'] = $asin; - return $this; - } - - function limit( $limit ) { - $this->parameters['limit'] = $limit; - return $this; - } - - function offset( $offset ) { - $this->parameterList['offset'] = $offset; - return $this; - } - } - - class mbTrackFilter implements MusicBrainzFilter { - private $parameters = array(); - - function createParameters() { - return $this->parameters; - } - - function title( $title ) { - $this->parameters['title'] = $title; - return $this; - } - - function artistName( $name ) { - $this->parameters['artist'] = $name; - return $this; - } - - function artistId( $id ) { - $this->parameters['artistid'] = $id; - return $this; - } - - function releaseTitle( $title ) { - $this->parameters['release'] = $title; - return $this; - } - - function releaseId( $id ) { - $this->parameters['releaseid'] = $id; - return $this; - } - - function duration( $duration ) { - $this->parameters['duration'] = $duration; - return $this; - } - - function puid( $puid ) { - $this->parameters['puid'] = $puid; - return $this; - } - - function limit( $limit ) { - $this->parameters['limit'] = $limit; - return $this; - } - - function offset( $offset ) { - $this->parameterList['offset'] = $offset; - return $this; - } - } - - class mbUserFilter implements MusicBrainzFilter { - private $parameters = array(); - - function createParameters() { - return $this->parameters; - } - - function name( $value ) { - $this->parameters['name'] = $value; - } +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ + +interface MusicBrainzFilter { + function createParameters(); +} + +class mbArtistFilter implements MusicBrainzFilter { + private $parameters = array(); + + function createParameters() { + return $this->parameters; + } + + function name( $name ) { + $this->parameters['name'] = $name; + return $this; + } + + function limit( $limit ) { + $this->parameters['limit'] = $limit; + return $this; + } + + function offset( $offset ) { + $this->parameters['offset'] = $offset; + return $this; + } +} + +class mbReleaseFilter implements MusicBrainzFilter { + private $parameters = array(); + + function createParameters() { + return $this->parameters; + } + + function title( $title ) { + $this->parameters['title'] = $title; + return $this; + } + + function discId( $discid ) { + $this->parameters['discid'] = $discid; + return $this; + } + + function releaseType( $rtype ) { + $type = extractFragment($rtype); + + if ( isset( $this->parameters['releasetypes'] ) ) { + $this->parameters['releasetypes'] .= ' ' . $type; + } + else { + $this->parameters['releasetypes'] = $type; + } + + return $this; + } + + function artistName( $name ) { + $this->parameters['artist'] = $name; + return $this; + } + + function artistId( $id ) { + $this->parameters['artistid'] = $id; + return $this; + } + + function asin( $asin ) { + $this->parameters['asin'] = $asin; + return $this; + } + + function limit( $limit ) { + $this->parameters['limit'] = $limit; + return $this; + } + + function offset( $offset ) { + $this->parameters['offset'] = $offset; + return $this; + } +} + +class mbTrackFilter implements MusicBrainzFilter { + private $parameters = array(); + + function createParameters() { + return $this->parameters; + } + + function title( $title ) { + $this->parameters['title'] = $title; + return $this; + } + + function artistName( $name ) { + $this->parameters['artist'] = $name; + return $this; + } + + function artistId( $id ) { + $this->parameters['artistid'] = $id; + return $this; + } + + function releaseTitle( $title ) { + $this->parameters['release'] = $title; + return $this; + } + + function releaseId( $id ) { + $this->parameters['releaseid'] = $id; + return $this; + } + + function duration( $duration ) { + $this->parameters['duration'] = $duration; + return $this; + } + + function puid( $puid ) { + $this->parameters['puid'] = $puid; + return $this; + } + + function limit( $limit ) { + $this->parameters['limit'] = $limit; + return $this; + } + + function offset( $offset ) { + $this->parameters['offset'] = $offset; + return $this; + } +} + +class mbUserFilter implements MusicBrainzFilter { + private $parameters = array(); + + function createParameters() { + return $this->parameters; + } + + function name( $value ) { + $this->parameters['name'] = $value; + } +} + +class mbRatingFilter implements MusicBrainzFilter { + private $parameters = array(); + + public function createParameters() { + return $this->parameters; + } + + public function entity($value) { + $this->parameters['entity'] = $value; + } + + public function id($value) { + $this->parameters['id'] = $value; } +} ?> diff --git a/modules/php_musicbrainz/mbInclude.php b/modules/php_musicbrainz/mbInclude.php index f5cbe018..82bfedbc 100644 --- a/modules/php_musicbrainz/mbInclude.php +++ b/modules/php_musicbrainz/mbInclude.php @@ -1,150 +1,150 @@ <?php -/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - interface MusicBrainzInclude { - function createIncludeTags(); - } - - class mbArtistIncludes implements MusicBrainzInclude { - private $includes = array(); - - function createIncludeTags() { - return $this->includes; - } - - function releases( $type ) { - $this->includes[] = "sa-" . extractFragment( $type ); - return $this; - } - - function vaReleases( $type ) { - $this->includes[] = "va-" . extractFragment( $type ); - return $this; - } - - function aliases() { - $this->includes[] = 'aliases'; - return $this; - } - - function artistRelations() { - $this->includes[] = 'artist-rels'; - return $this; - } - - function releaseRelations() { - $this->includes[] = 'release-rels'; - return $this; - } - - function trackRelations() { - $this->includes[] = 'track-rels'; - return $this; - } - - function urlRelations() { - $this->includes[] = 'url-rels'; - return $this; - } - - function releaseEvents() { - $this->includes[] = 'release-events'; - return $this; - } - } - - class mbReleaseIncludes implements MusicBrainzInclude { - private $includes; - - function createIncludeTags() { - return $this->includes; - } - - function artist() { - $this->includes[] = 'artist'; - return $this; - } - - function counts() { - $this->includes[] = 'counts'; - return $this; - } - - function releaseEvents() { - $this->includes[] = 'release-events'; - return $this; - } - - function discs() { - $this->includes[] = 'discs'; - return $this; - } - - function tracks() { - $this->includes[] = 'tracks'; - return $this; - } - - function artistRelations() { - $this->includes[] = 'artist-rels'; - return $this; - } - - function releaseRelations() { - $this->includes[] = 'release-rels'; - return $this; - } - - function trackRelations() { - $this->includes[] = 'track-rels'; - return $this; - } - - function urlRelations() { - $this->includes[] = 'url-rels'; - return $this; - } - } - - class mbTrackIncludes implements MusicBrainzInclude { - private $includes; - - function createIncludeTags() { - return $this->includes; - } - - function artist() { - $this->includes[] = 'artist'; - return $this; - } - - function releases() { - $this->includes[] = 'releases'; - return $this; - } - - function puids() { - $this->includes[] = 'puids'; - return $this; - } - - function artistRelations() { - $this->includes[] = 'artist-rels'; - return $this; - } - - function releaseRelations() { - $this->includes[] = 'release-rels'; - return $this; - } - - function trackRelations() { - $this->includes[] = 'track-rels'; - return $this; - } - - function urlRelations() { - $this->includes[] = 'url-rels'; - return $this; - } - } +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ +interface MusicBrainzInclude { + public function createIncludeTags(); +} + +class mbArtistIncludes implements MusicBrainzInclude { + private $includes = array(); + + public function createIncludeTags() { + return $this->includes; + } + + public function releases( $type ) { + $this->includes[] = "sa-" . extractFragment( $type ); + return $this; + } + + public function vaReleases( $type ) { + $this->includes[] = "va-" . extractFragment( $type ); + return $this; + } + + public function aliases() { + $this->includes[] = 'aliases'; + return $this; + } + + public function artistRelations() { + $this->includes[] = 'artist-rels'; + return $this; + } + + public function releaseRelations() { + $this->includes[] = 'release-rels'; + return $this; + } + + public function trackRelations() { + $this->includes[] = 'track-rels'; + return $this; + } + + public function urlRelations() { + $this->includes[] = 'url-rels'; + return $this; + } + + public function releaseEvents() { + $this->includes[] = 'release-events'; + return $this; + } +} + +class mbReleaseIncludes implements MusicBrainzInclude { + private $includes; + + public function createIncludeTags() { + return $this->includes; + } + + public function artist() { + $this->includes[] = 'artist'; + return $this; + } + + public function counts() { + $this->includes[] = 'counts'; + return $this; + } + + public function releaseEvents() { + $this->includes[] = 'release-events'; + return $this; + } + + public function discs() { + $this->includes[] = 'discs'; + return $this; + } + + public function tracks() { + $this->includes[] = 'tracks'; + return $this; + } + + public function artistRelations() { + $this->includes[] = 'artist-rels'; + return $this; + } + + public function releaseRelations() { + $this->includes[] = 'release-rels'; + return $this; + } + + public function trackRelations() { + $this->includes[] = 'track-rels'; + return $this; + } + + public function urlRelations() { + $this->includes[] = 'url-rels'; + return $this; + } +} + +class mbTrackIncludes implements MusicBrainzInclude { + private $includes; + + public function createIncludeTags() { + return $this->includes; + } + + public function artist() { + $this->includes[] = 'artist'; + return $this; + } + + public function releases() { + $this->includes[] = 'releases'; + return $this; + } + + public function puids() { + $this->includes[] = 'puids'; + return $this; + } + + public function artistRelations() { + $this->includes[] = 'artist-rels'; + return $this; + } + + public function releaseRelations() { + $this->includes[] = 'release-rels'; + return $this; + } + + public function trackRelations() { + $this->includes[] = 'track-rels'; + return $this; + } + + public function urlRelations() { + $this->includes[] = 'url-rels'; + return $this; + } +} ?> diff --git a/modules/php_musicbrainz/mbLabel.php b/modules/php_musicbrainz/mbLabel.php index 67e069ef..5786ddaa 100644 --- a/modules/php_musicbrainz/mbLabel.php +++ b/modules/php_musicbrainz/mbLabel.php @@ -1,91 +1,91 @@ <?php -/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - class mbLabel extends MusicBrainzEntity { - const TYPE_GROUP = "http://musicbrainz.org/ns/mmd-1.0#Group"; - const TYPE_PERSON = "http://musicbrainz.org/ns/mmd-1.0#Person"; - - private $type; - private $name; - private $sortName; - private $disambiguation; - private $beginDate; - private $endDate; - private $releases; - private $releasesCount; - private $releasesOffset; - private $aliases; - - function mbLabel( $id='', $type='', $name='', $sortName='' ) { - parent::MusicBrainzEntity($id); - $this->type = $type; - $this->name = $name; - $this->sortName = $sortName; - } - - function setType( $type ) { $this->type = $type; } - function getType() { return $this->type; } - function setName( $name ) { $this->name = $name; } - function getName() { return $this->name; } - function setSortName( $sortName ) { $this->sortName = $sortName; } - function getSortName() { return $this->sortName; } - function setDisambiguation( $disambiguation ) { $this->disambiguation = $disambiguation; } - function getDisambiguation() { return $this->disambiguation; } - function setBeginDate( $beginDate ) { $this->beginDate = $beginDate; } - function getBeginDate() { return $this->beginDate; } - function setEndDate( $endDate ) { $this->endDate = $endDate; } - function getEndDate() { return $this->endDate; } - - function getUniqueName() { - return empty($this->disambiguation) ? $this->name : - $this->name . ' (' . $this->disambiguation . ')'; - } - - function &getReleases() { - return $this->releases; - } - - function addRelease( mbRelease $release ) { - $this->releases[] = $release; - } - - function &getAliases() { - return $this->aliases; - } - - function addAlias( mbLabelAlias $alias ) { - $this->aliases[] = $alias; - } - - function getNumReleases() { - return count($this->releases); - } - - function &getRelease( $i ) { - return $this->releases[$i]; - } - - function getReleasesOffset() { - return $this->releasesOffset; - } - - function setReleasesOffset( $relOffset ) { - $this->releasesOffset = $relOffset; - } - - function getReleasesCount() { - return $this->releasesCount; - } - - function setReleasesCount( $relCount ) { - $this->releasesCount = $relCount; - } - - function getNumAliases() { - return count($this->aliases); - } - - function &getAlias( $i ) { - return $this->aliases[$i]; - } - } +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ +class mbLabel extends MusicBrainzEntity { + const TYPE_GROUP = "http://musicbrainz.org/ns/mmd-1.0#Group"; + const TYPE_PERSON = "http://musicbrainz.org/ns/mmd-1.0#Person"; + + private $type; + private $name; + private $sortName; + private $disambiguation; + private $beginDate; + private $endDate; + private $releases; + private $releasesCount; + private $releasesOffset; + private $aliases; + + public function __construct($id='', $type='', $name='', $sortName='') { + parent::__construct($id); + $this->type = $type; + $this->name = $name; + $this->sortName = $sortName; + } + + public function setType($type) { $this->type = $type; } + public function getType() { return $this->type; } + public function setName($name) { $this->name = $name; } + public function getName() { return $this->name; } + public function setSortName($sortName) { $this->sortName = $sortName; } + public function getSortName() { return $this->sortName; } + public function setDisambiguation($disambiguation) { $this->disambiguation = $disambiguation; } + public function getDisambiguation() { return $this->disambiguation; } + public function setBeginDate($beginDate) { $this->beginDate = $beginDate; } + public function getBeginDate() { return $this->beginDate; } + public function setEndDate($endDate) { $this->endDate = $endDate; } + public function getEndDate() { return $this->endDate; } + + public function getUniqueName() { + return empty($this->disambiguation) ? $this->name : + $this->name . ' (' . $this->disambiguation . ')'; + } + + public function getReleases() { + return $this->releases; + } + + public function addRelease(mbRelease $release) { + $this->releases[] = $release; + } + + public function getAliases() { + return $this->aliases; + } + + public function addAlias(mbLabelAlias $alias) { + $this->aliases[] = $alias; + } + + public function getNumReleases() { + return count($this->releases); + } + + public function getRelease($i) { + return $this->releases[$i]; + } + + public function getReleasesOffset() { + return $this->releasesOffset; + } + + public function setReleasesOffset($relOffset) { + $this->releasesOffset = $relOffset; + } + + public function getReleasesCount() { + return $this->releasesCount; + } + + public function setReleasesCount($relCount) { + $this->releasesCount = $relCount; + } + + public function getNumAliases() { + return count($this->aliases); + } + + public function getAlias($i) { + return $this->aliases[$i]; + } +} ?> diff --git a/modules/php_musicbrainz/mbMetadata.php b/modules/php_musicbrainz/mbMetadata.php index 45453d6e..9581d8f1 100644 --- a/modules/php_musicbrainz/mbMetadata.php +++ b/modules/php_musicbrainz/mbMetadata.php @@ -1,82 +1,92 @@ <?php /* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - class mbMetadata { - private $artist = null; - private $track = null; - private $release = null; - private $label = null; - private $artistList; - private $trackList; - private $releaseList; - private $userList; +class mbMetadata { + private $artist = null; + private $track = null; + private $release = null; + private $label = null; + private $rating = null; + private $artistList; + private $trackList; + private $releaseList; + private $userList; - function mbMetadata() { - $this->artistList = array(); - $this->trackList = array(); - $this->releaseList = array(); - $this->userList = array(); - } + function mbMetadata() { + $this->artistList = array(); + $this->trackList = array(); + $this->releaseList = array(); + $this->userList = array(); + } - function setArtist ( mbArtist $artist ) { $this->artist = $artist; } - function setTrack ( mbTrack $track ) { $this->track = $track; } - function setRelease( mbRelease $release ) { $this->release = $release; } - function setLabel ( mbLabel $label ) { $this->label = $label; } + function setArtist(mbArtist $artist) { $this->artist = $artist; } + function setTrack(mbTrack $track) { $this->track = $track; } + function setRating(mbRating $rating) { $this->rating = $rating; } + function setRelease(mbRelease $release) { $this->release = $release; } + function setLabel(mbLabel $label) { $this->label = $label; } - function getArtist( $remove=false ) { - $a = $this->artist; - if ( $remove ) - $this->artist = null; - return $a; - } + function getArtist($remove=false) { + $a = $this->artist; + if ($remove) + $this->artist = null; + return $a; + } - function getTrack( $remove=false ) { - $t = $this->track; - if ( $remove ) - $this->track = null; - return $t; - } + function getTrack($remove=false) { + $t = $this->track; + if ($remove) + $this->track = null; + return $t; + } - function getRelease( $remove=false ) { - $r = $this->release; - if ( $remove ) - $this->release = null; - return $r; + function getRating($remove=false) { + $r = $this->rating; + if ($remove) { + $this->rating = null; } + return $r; + } - function getLabel( $remove=false ) { - $l = $this->label; - if ( $remove ) - $this->label = null; - return $l; - } + function getRelease($remove=false) { + $r = $this->release; + if ($remove) + $this->release = null; + return $r; + } - function &getUserList () { return $this->userList; } - function &getArtistResults () { return $this->artistList; } - function &getTrackResults () { return $this->trackList; } - function &getReleaseResults() { return $this->releaseList; } + function getLabel($remove=false) { + $l = $this->label; + if ($remove) + $this->label = null; + return $l; + } - function getUserList2( $remove ) { - $ul = $this->userList; - $this->userList = array(); - return $ul; - } + function getUserList() { return $this->userList; } + function getArtistResults() { return $this->artistList; } + function getTrackResults() { return $this->trackList; } + function getReleaseResults() { return $this->releaseList; } - function getArtistResults2( $remove ) { - $al = $this->artistList; - $this->artistList = array(); - return $al; - } + function getUserList2($remove) { + $ul = $this->userList; + $this->userList = array(); + return $ul; + } - function getTrackResults2( $remove ) { - $tl = $this->trackList; - $this->trackList = array(); - return $tl; - } + function getArtistResults2($remove) { + $al = $this->artistList; + $this->artistList = array(); + return $al; + } - function getReleaseResults2( $remove ) { - $rl = $this->releaseList; - $this->releaseList = array(); - return $rl; - } + function getTrackResults2($remove) { + $tl = $this->trackList; + $this->trackList = array(); + return $tl; + } + + function getReleaseResults2($remove) { + $rl = $this->releaseList; + $this->releaseList = array(); + return $rl; } +} ?> diff --git a/modules/php_musicbrainz/mbQuery.php b/modules/php_musicbrainz/mbQuery.php index df8aa4aa..0c2ab3a6 100644 --- a/modules/php_musicbrainz/mbQuery.php +++ b/modules/php_musicbrainz/mbQuery.php @@ -1,132 +1,139 @@ <?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) ); - } - } +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 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('mbRating.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; + + public function __construct(IWebService $ws=null, $clientId = '') { + if ($ws != null) + $this->ws = $ws; + else { + $this->ws = new mbWebService(); + $this->ownWs = true; + } + + $this->clientId = $clientId; + } + + public 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"); + } + + public function getArtists(mbArtistFilter $artist_filters) { + $metadata = $this->getFromWebService("artist", "", null, $artist_filters); + return $metadata->getArtistResults2(true); + } + + public function getReleases(mbReleaseFilter $release_filters) { + $metadata = $this->getFromWebService("release", "", null, $release_filters); + return $metadata->getReleaseResults2(true); + } + + public function getTracks(mbTrackFilter $track_filters) { + $metadata = $this->getFromWebService("track", "", null, $track_filters); + return $metadata->getTrackResults2(true); + } + + public 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; + } + + public 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; + } + + public 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; + } + + public function getRating(mbRatingFilter $rating_filter) { + $metadata = $this->getFromWebService('rating', '', null, $rating_filter); + $rating = $metadata->getRating(true); + return $rating; + } + + 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()); + } + } + + public 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)); + } +} ?> diff --git a/modules/php_musicbrainz/mbRating.php b/modules/php_musicbrainz/mbRating.php new file mode 100644 index 00000000..a5249acc --- /dev/null +++ b/modules/php_musicbrainz/mbRating.php @@ -0,0 +1,13 @@ +<?php +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ +class mbRating { + private $rating; + + public function __construct($rating=0) { + $this->rating = $rating; + } + + public function setRating($rating) { $this->rating = $rating; } + public function getRating() { return $this->rating; } +} +?> diff --git a/modules/php_musicbrainz/mbRelation.php b/modules/php_musicbrainz/mbRelation.php index 398c950f..1482fc5f 100644 --- a/modules/php_musicbrainz/mbRelation.php +++ b/modules/php_musicbrainz/mbRelation.php @@ -1,112 +1,112 @@ <?php -/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - class mbRelation { - const DIR_BOTH = 0; - const DIR_FORWARD = 1; - const DIR_BACKWARD = 2; - - const TO_ARTIST = "http://musicbrainz.org/ns/mmd-1.0#Artist"; - const TO_TRACK = "http://musicbrainz.org/ns/mmd-1.0#Track"; - const TO_RELEASE = "http://musicbrainz.org/ns/mmd-1.0#Release"; - const TO_URL = "http://musicbrainz.org/ns/mmd-1.0#Url"; - - private $type; - private $targetType; - private $targetId; - private $direction; - private $attributes; - private $beginDate; - private $endDate; - private $target; - - function mbRelation( $relationType = '', - $targetType = '', - $targetId = '', - $direction = DIR_BOTH, - array $attributes = array(), - $beginDate = '', - $endDate = '', - MusicBrainzEntity $target = null ) { - $this->type = $relationType; - $this->targetType = $targetType; - $this->targetId = $targetId; - $this->direction = $direction; - $this->attributes = $attributes; - $this->beginDate = $beginDate; - $this->endDate = $endDate; - $this->target = $target; - } - - function setType( $type ) { - $this->type = $type; - } - - function getType() { - return $this->type; - } - - function setTargetType( $ttype ) { - $this->targetType = $ttype; - } - - function getTargetType() { - return $this->targetType; - } - - function setTargetId( $tid ) { - $this->targetId = $tid; - } - - function getTargetId() { - return $this->targetId; - } - - function setBeginDate( $bdate ) { - $this->beginDate = $bdate; - } - - function getBeginDate() { - return $this->beginDate; - } - - function setEndDate( $edate ) { - $this->endDate = $edate; - } - - function getEndDate() { - return $this->endDate; - } - - function getDirection() { - return $this->direction; - } - - function setDirection( $dir ) { - $this->direction = $dir; - } - - function getTarget() { - return $this->target; - } - - function setTarget( MusicBrainzEntity $entity=null ) { - $this->target = $entity; - } - - function &getAttributes() { - return $this->attributes; - } - - function addAttribute( $value ) { - $this->attributes[] = $value; - } - - function getNumAttributes() { - return count($this->attributes); - } - - function getAttribute( $i ) { - return $this->attributes( $i ); - } - } +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ +class mbRelation { + const DIR_BOTH = 0; + const DIR_FORWARD = 1; + const DIR_BACKWARD = 2; + + const TO_ARTIST = "http://musicbrainz.org/ns/mmd-1.0#Artist"; + const TO_TRACK = "http://musicbrainz.org/ns/mmd-1.0#Track"; + const TO_RELEASE = "http://musicbrainz.org/ns/mmd-1.0#Release"; + const TO_URL = "http://musicbrainz.org/ns/mmd-1.0#Url"; + + private $type; + private $targetType; + private $targetId; + private $direction; + private $attributes; + private $beginDate; + private $endDate; + private $target; + + public function __construct($relationType = '', + $targetType = '', + $targetId = '', + $direction = DIR_BOTH, + array $attributes = array(), + $beginDate = '', + $endDate = '', + MusicBrainzEntity $target = null) { + $this->type = $relationType; + $this->targetType = $targetType; + $this->targetId = $targetId; + $this->direction = $direction; + $this->attributes = $attributes; + $this->beginDate = $beginDate; + $this->endDate = $endDate; + $this->target = $target; + } + + public function setType($type) { + $this->type = $type; + } + + public function getType() { + return $this->type; + } + + public function setTargetType($ttype) { + $this->targetType = $ttype; + } + + public function getTargetType() { + return $this->targetType; + } + + public function setTargetId($tid) { + $this->targetId = $tid; + } + + public function getTargetId() { + return $this->targetId; + } + + public function setBeginDate($bdate) { + $this->beginDate = $bdate; + } + + public function getBeginDate() { + return $this->beginDate; + } + + public function setEndDate($edate) { + $this->endDate = $edate; + } + + public function getEndDate() { + return $this->endDate; + } + + public function getDirection() { + return $this->direction; + } + + public function setDirection($dir) { + $this->direction = $dir; + } + + public function getTarget() { + return $this->target; + } + + public function setTarget(MusicBrainzEntity $entity=null) { + $this->target = $entity; + } + + public function getAttributes() { + return $this->attributes; + } + + public function addAttribute($value) { + $this->attributes[] = $value; + } + + public function getNumAttributes() { + return count($this->attributes); + } + + public function getAttribute($i) { + return $this->attributes($i); + } +} ?> diff --git a/modules/php_musicbrainz/mbRelease.php b/modules/php_musicbrainz/mbRelease.php index 27afa4db..5362792b 100644 --- a/modules/php_musicbrainz/mbRelease.php +++ b/modules/php_musicbrainz/mbRelease.php @@ -1,127 +1,127 @@ <?php -/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - class mbRelease extends MusicBrainzEntity { - // Types - const TYPE_NONE = "http://musicbrainz.org/ns/mmd-1.0#None"; - - const TYPE_ALBUM = "http://musicbrainz.org/ns/mmd-1.0#Album"; - const TYPE_SINGLE = "http://musicbrainz.org/ns/mmd-1.0#Single"; - const TYPE_EP = "http://musicbrainz.org/ns/mmd-1.0#EP"; - const TYPE_COMPILATION = "http://musicbrainz.org/ns/mmd-1.0#Compilation"; - const TYPE_SOUNDTRACK = "http://musicbrainz.org/ns/mmd-1.0#Soundtrack"; - const TYPE_SPOKENWORD = "http://musicbrainz.org/ns/mmd-1.0#Spokenword"; - const TYPE_INTERVIEW = "http://musicbrainz.org/ns/mmd-1.0#Interview"; - const TYPE_AUDIOBOOK = "http://musicbrainz.org/ns/mmd-1.0#Audiobook"; - const TYPE_LIVE = "http://musicbrainz.org/ns/mmd-1.0#Live"; - const TYPE_REMIX = "http://musicbrainz.org/ns/mmd-1.0#Remix"; - const TYPE_OTHER = "http://musicbrainz.org/ns/mmd-1.0#Other"; - - // Statuses - const TYPE_OFFICIAL = "http://musicbrainz.org/ns/mmd-1.0#Official"; - const TYPE_PROMOTION = "http://musicbrainz.org/ns/mmd-1.0#Promotion"; - const TYPE_BOOTLEG = "http://musicbrainz.org/ns/mmd-1.0#Bootleg"; - const TYPE_PSEUDO_RELEASE = "http://musicbrainz.org/ns/mmd-1.0#Pseudo-Release"; - - private $title; - private $textLanguage; - private $textScript; - private $asin; - private $types = array(); - private $artist = null; - private $tracks = array(); - private $tracksOffset = 0; - private $tracksCount = 0; - private $discs = array(); - private $releaseEvents = array(); - - function mbRelease( $id = '', $title = '' ) { - parent::MusicBrainzEntity($id); - $this->title = $title; - } - - function getTitle() { return $this->title; } - function setTitle( $title ) { $this->title = $title; } - function getTextLanguage() { return $this->textLanguage; } - function setTextLanguage( $tlang ) { $this->textLanguage = $tlang; } - function getTextScript() { return $this->textScript; } - function setTextScript( $tscript ) { $this->textScript = $tscript; } - function getAsin() { return $this->asin; } - function setAsin( $asin ) { $this->asin = $asin; } - - function getArtist() { - return $this->artist; - } - - function setArtist( Artist $artist ) { - $this->artist = $artist; - } - - function &getTracks() { - return $this->tracks; - } - - function getTracksOffset() { - return $this->tracksOffset; - } - - function setTracksOffset( $value ) { - $this->tracksOffset = $value; - } - - function getTracksCount() { - return $this->tracksCount; - } - - function setTracksCount( $tracksCount ) { - $this->tracksCount = $tracksCount; - } - - function &getDiscs() { - return $this->discs; - } - - function &getReleaseEvents() { - return $this->releaseEvents; - } - - function getNumReleaseEvents() { - return count($this->releaseEvents); - } - - function getReleaseEvent( $i ) { - return $this->releaseEvents[$i]; - } - - function getNumDiscs() { - return count($this->discs); - } - - function getDisc( $i ) { - return $this->discs[$i]; - } - - function getNumTracks() { - return count($this->tracks); - } - - function getTrack( $i ) { - return $this->tracks[$i]; - } - - function setTypes( array $types ) { - $this->types = $types; - } - - function &getTypes() { - return $this->types; - } - - function getNumTypes() { - return count($this->types); - } - - function getType( $i ) { - return $this->types[$i]; - } - } +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ +class mbRelease extends MusicBrainzEntity { + // Types + const TYPE_NONE = "http://musicbrainz.org/ns/mmd-1.0#None"; + + const TYPE_ALBUM = "http://musicbrainz.org/ns/mmd-1.0#Album"; + const TYPE_SINGLE = "http://musicbrainz.org/ns/mmd-1.0#Single"; + const TYPE_EP = "http://musicbrainz.org/ns/mmd-1.0#EP"; + const TYPE_COMPILATION = "http://musicbrainz.org/ns/mmd-1.0#Compilation"; + const TYPE_SOUNDTRACK = "http://musicbrainz.org/ns/mmd-1.0#Soundtrack"; + const TYPE_SPOKENWORD = "http://musicbrainz.org/ns/mmd-1.0#Spokenword"; + const TYPE_INTERVIEW = "http://musicbrainz.org/ns/mmd-1.0#Interview"; + const TYPE_AUDIOBOOK = "http://musicbrainz.org/ns/mmd-1.0#Audiobook"; + const TYPE_LIVE = "http://musicbrainz.org/ns/mmd-1.0#Live"; + const TYPE_REMIX = "http://musicbrainz.org/ns/mmd-1.0#Remix"; + const TYPE_OTHER = "http://musicbrainz.org/ns/mmd-1.0#Other"; + + // Statuses + const TYPE_OFFICIAL = "http://musicbrainz.org/ns/mmd-1.0#Official"; + const TYPE_PROMOTION = "http://musicbrainz.org/ns/mmd-1.0#Promotion"; + const TYPE_BOOTLEG = "http://musicbrainz.org/ns/mmd-1.0#Bootleg"; + const TYPE_PSEUDO_RELEASE = "http://musicbrainz.org/ns/mmd-1.0#Pseudo-Release"; + + private $title; + private $textLanguage; + private $textScript; + private $asin; + private $types = array(); + private $artist = null; + private $tracks = array(); + private $tracksOffset = 0; + private $tracksCount = 0; + private $discs = array(); + private $releaseEvents = array(); + + function __construct($id = '', $title = '') { + parent::__construct($id); + $this->title = $title; + } + + function getTitle() { return $this->title; } + function setTitle($title) { $this->title = $title; } + function getTextLanguage() { return $this->textLanguage; } + function setTextLanguage($tlang) { $this->textLanguage = $tlang; } + function getTextScript() { return $this->textScript; } + function setTextScript($tscript) { $this->textScript = $tscript; } + function getAsin() { return $this->asin; } + function setAsin($asin) { $this->asin = $asin; } + + function getArtist() { + return $this->artist; + } + + function setArtist(Artist $artist) { + $this->artist = $artist; + } + + function getTracks() { + return $this->tracks; + } + + function getTracksOffset() { + return $this->tracksOffset; + } + + function setTracksOffset($value) { + $this->tracksOffset = $value; + } + + function getTracksCount() { + return $this->tracksCount; + } + + function setTracksCount($tracksCount) { + $this->tracksCount = $tracksCount; + } + + function getDiscs() { + return $this->discs; + } + + function getReleaseEvents() { + return $this->releaseEvents; + } + + function getNumReleaseEvents() { + return count($this->releaseEvents); + } + + function getReleaseEvent($i) { + return $this->releaseEvents[$i]; + } + + function getNumDiscs() { + return count($this->discs); + } + + function getDisc($i) { + return $this->discs[$i]; + } + + function getNumTracks() { + return count($this->tracks); + } + + function getTrack($i) { + return $this->tracks[$i]; + } + + function setTypes(array $types) { + $this->types = $types; + } + + function getTypes() { + return $this->types; + } + + function getNumTypes() { + return count($this->types); + } + + function getType($i) { + return $this->types[$i]; + } +} ?> diff --git a/modules/php_musicbrainz/mbResults.php b/modules/php_musicbrainz/mbResults.php index 36416792..50801514 100644 --- a/modules/php_musicbrainz/mbResults.php +++ b/modules/php_musicbrainz/mbResults.php @@ -1,70 +1,70 @@ <?php -/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - class mbResult { - private $score; - private $count; - private $offset; +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ +class mbResult { + private $score; + private $count; + private $offset; - function mbResult( $score ) { - $this->score = $score; - } + public function __construct($score) { + $this->score = $score; + } - function getScore( ) { return $this->score; } - function setScore( $score ) { $this->score = $score; } - function getCount( ) { return $this->count; } - function setCount( $count ) { $this->count = $count; } - function getOffset( ) { return $this->offset; } - function setOffset( $offset ) { $this->offset = $offset; } - } + public function getScore( ) { return $this->score; } + public function setScore($score) { $this->score = $score; } + public function getCount( ) { return $this->count; } + public function setCount($count) { $this->count = $count; } + public function getOffset( ) { return $this->offset; } + public function setOffset($offset) { $this->offset = $offset; } +} - class mbArtistResult extends mbResult { - private $artist; +class mbArtistResult extends mbResult { + private $artist; - function mbArtistResult( Artist $artist, $score ) { - parent::mbResult($score); - $this->artist = $artist; - } + public function __construct(Artist $artist, $score) { + parent::__construct($score); + $this->artist = $artist; + } - function setArtist( mbArtist $artist ) { - $this->artist = $artist; - } + public function setArtist(mbArtist $artist) { + $this->artist = $artist; + } - function getArtist() { - return $this->artist; - } - } + public function getArtist() { + return $this->artist; + } +} - class mbReleaseResult extends mbResult { - private $release; +class mbReleaseResult extends mbResult { + private $release; - function mbReleaseResult( mbRelease $release, $score ) { - parent::mbResult($score); - $this->release = $release; - } + public function __construct(mbRelease $release, $score) { + parent::__construct($score); + $this->release = $release; + } - function setRelease( Release $release ) { - $this->release = $release; - } + public function setRelease(Release $release) { + $this->release = $release; + } - function getRelease() { - return $this->release; - } - } + public function getRelease() { + return $this->release; + } +} - class mbTrackResult extends mbResult { - private $track; +class mbTrackResult extends mbResult { + private $track; - function mbTrackResult( mbTrack $track, $score ) { - parent::mbResult($score); - $this->track = $track; - } + public function __construct(mbTrack $track, $score) { + parent::__construct($score); + $this->track = $track; + } - function setTrack( mbTrack $track ) { - $this->track = $track; - } + public function setTrack(mbTrack $track) { + $this->track = $track; + } - function getTrack() { - return $this->track; - } - } + public function getTrack() { + return $this->track; + } +} ?> diff --git a/modules/php_musicbrainz/mbTag.php b/modules/php_musicbrainz/mbTag.php index 4f72d406..2c72c859 100644 --- a/modules/php_musicbrainz/mbTag.php +++ b/modules/php_musicbrainz/mbTag.php @@ -1,17 +1,17 @@ <?php -/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - class mbTag { - private $name; - private $count; +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ +class mbTag { + private $name; + private $count; - function mbTag( $name='', $count=0 ) { - $this->name = $name; - $this->count = $count; - } + public function __construct($name='', $count=0) { + $this->name = $name; + $this->count = $count; + } - function setName ( $name ) { $this->name = $name; } - function getName ( ) { return $this->name; } - function setCount( $count ) { $this->count = $count; } - function getCount( ) { return $this->count; } - } + public function setName($name) { $this->name = $name; } + public function getName() { return $this->name; } + public function setCount($count) { $this->count = $count; } + public function getCount() { return $this->count; } +} ?> diff --git a/modules/php_musicbrainz/mbTrack.php b/modules/php_musicbrainz/mbTrack.php index 9dc79419..70054c71 100644 --- a/modules/php_musicbrainz/mbTrack.php +++ b/modules/php_musicbrainz/mbTrack.php @@ -1,61 +1,61 @@ <?php -/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - class mbTrack extends MusicBrainzEntity { - private $title; - private $artist = null; - private $duration = 0; - private $releases; - private $releasesCount = 0; - private $releasesOffset = 0; - - function mbTrack( $id = '', $title = '' ) { - parent::MusicBrainzEntity($id); - $this->title = $title; - } - - function getTitle ( ) { return $this->title; } - function setTitle ( $title ) { $this->title = $title; } - function getDuration( ) { return $this->duration; } - function setDuration( $duration ) { $this->duration = $duration; } - - function getArtist() { - return $this->artist; - } - - function setArtist( mbArtist $artist ) { - $this->artist = $artist; - } - - function &getReleases() { - return $this->releases; - } - - function addRelease( mbRelease $release ) { - $this->releases[] = $release; - } - - function getNumReleases() { - return count($this->releases); - } - - function getRelease( $i ) { - return $this->releases[$i]; - } - - function getReleasesOffset() { - return $this->releasesOffset; - } - - function setReleasesOffset( $relOffset ) { - $this->releasesOffset = $relOffset; - } - - function getReleasesCount() { - return $this->releasesCount; - } - - function setReleasesCount( $relCount ) { - $this->releasesCount = $relCount; - } - } +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ +class mbTrack extends MusicBrainzEntity { + private $title; + private $artist = null; + private $duration = 0; + private $releases; + private $releasesCount = 0; + private $releasesOffset = 0; + + public function __construct($id = '', $title = '') { + parent::__construct($id); + $this->title = $title; + } + + public function getTitle ( ) { return $this->title; } + public function setTitle ($title ) { $this->title = $title; } + public function getDuration( ) { return $this->duration; } + public function setDuration($duration) { $this->duration = $duration; } + + public function getArtist() { + return $this->artist; + } + + public function setArtist(mbArtist $artist) { + $this->artist = $artist; + } + + public function getReleases() { + return $this->releases; + } + + public function addRelease(mbRelease $release) { + $this->releases[] = $release; + } + + public function getNumReleases() { + return count($this->releases); + } + + public function getRelease($i) { + return $this->releases[$i]; + } + + public function getReleasesOffset() { + return $this->releasesOffset; + } + + public function setReleasesOffset($relOffset) { + $this->releasesOffset = $relOffset; + } + + public function getReleasesCount() { + return $this->releasesCount; + } + + public function setReleasesCount($relCount) { + $this->releasesCount = $relCount; + } +} ?> diff --git a/modules/php_musicbrainz/mbWebService.php b/modules/php_musicbrainz/mbWebService.php index 76aa0939..a6377f64 100644 --- a/modules/php_musicbrainz/mbWebService.php +++ b/modules/php_musicbrainz/mbWebService.php @@ -1,190 +1,235 @@ <?php -/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - interface IWebService { - function get ( $entity, $id, $include, $filter, $version = '1' ); - function post( $entity, $id, $data, $version = '1' ); - } - - class mbWebService implements IWebService { - private $host; - private $port; - private $pathPrefix; - private $lastError; - private $fSock; - private $lastResponse = ""; - private $lastHeaders = array(); - - function mbWebService( $host="musicbrainz.org", $port=80, $pathPrefix="/ws" ) { - $this->host = $host; - $this->port = $port; - $this->pathPrefix = $pathPrefix; - $this->fSock = -1; - } - - function connect() { - $this->fSock = fsockopen( $this->host, $this->port, $errno, $this->lastError, 30 ); - - if ( $this->fSock == false ) { - $this->fSock = -1; - return false; - } - - return true; - } - - function close() { - if ( $this->fSock != -1 ) { - fclose($this->fSock); - $this->fSock = -1; - return true; - } - else { - $this->lastError = "Trying to close closed socket."; - return false; - } - } - - function parseHeaders( $string ) { - $lines = explode( "\n", $string ); - $this->lastHeaders = array(); - - foreach ( $lines as $key => $line ) { - // Status line - if ( $key == 0 ) { - if ( !preg_match( "/^HTTP\/(\d+)\.(\d+) (\d+) .+$/", $line, $matches ) ) { - $this->lastHeader = array(); - return false; - } - else { - $this->lastHeaders['HTTP_major_version'] = $matches[1]; - $this->lastHeaders['HTTP_minor_version'] = $matches[2]; - $this->lastHeaders['HTTP_status'] = $matches[3]; - } - } - // Empty line - else if ( $line == "\r" ) { - $new_string = ""; - for ( $i = $key+1; $i < sizeof($lines); $i++ ) - $new_string .= $lines[$i] . "\n"; - return $new_string; - } - // Not a header - else if ( !preg_match( "/^([^:]+): (.+)\r$/", $line, $matches ) ) { - $this->lastHeaders = array(); - return false; - } - // A header - else - $this->lastHeaders[$matches[1]] = $matches[2]; - } - - $this->lastHeaders = array(); - return false; - } - - function getHeaders() { - return $this->lastHeaders; - } - - function sendRequest( $string, $post_data='' ) { - if ( $this->fSock == -1 ) { - $this->lastError = "Trying to write to closed socket."; - return false; - } - - fwrite( $this->fSock, $string . "\r\n" ); - fwrite( $this->fSock, "Host: " . $this->host . "\r\n" ); - fwrite( $this->fSock, "Accept: */*\r\n" ); - fwrite( $this->fSock, "User-Agent: phpMbQuery\r\n" ); - //fwrite( $this->fSock, "Keep-Alive: 60\r\n" ); - //fwrite( $this->fSock, "Connection: keep-alive\r\n" ); - fwrite( $this->fSock, "Connection: close\r\n\r\n" ); - fwrite( $this->fSock, $post_data . "\r\n\r\n" ); - - return true; - } - - function getResponse() { - if ( $this->fSock == -1 ) { - $this->lastError = "Trying to read from closed socket."; - return false; - } - - $buffer = ""; - - while ( !feof($this->fSock) ) - $buffer .= fread( $this->fSock, 4096 ); - - if ( !$this->parseHeaders($buffer) ) - return $buffer; - - return $this->parseHeaders($buffer); - } - - function get( $entity, $uid, $includes, $filters, $version="1" ) { - $params = array(); - $params['type'] = "xml"; - - if ( is_array($includes) ) { - $inc_string = ""; - foreach ( $includes as $inc ) { - if ( $inc_string != "" ) - $inc_string .= " "; - $inc_string .= $inc; - } - if ( $inc_string != "" ) - $params['inc'] = $inc_string; - } - - if ( is_array($filters) ) { - foreach ( $filters as $filter => $value ) - $params[$filter] = $value; - } - - $URI = $this->pathPrefix . "/" . $version . "/" . $entity . "/" . $uid . "?" . $this->build_query( $params ); - - if ( $this->fSock == -1 && !$this->connect() ) - return false; - - $this->sendRequest( "GET $URI HTTP/1.1" ); - $this->lastResponse = $this->getResponse(); - $this->close(); - - if ( isset($this->lastHeaders['HTTP_status']) && $this->lastHeaders['HTTP_status'] != 200 ) - return false; - - return $this->lastResponse; - } - - function post( $entity, $id, $data, $version = '1' ) { - $URI = $this->pathPrefix . '/' . $version . '/' . $entity . '/' . $id; - if ( $this->fSock == -1 && !$this->connect() ) - return false; - - $this->sendRequest( "POST $URI HTTP/1.1", $data ); - $this->lastResponse = $this->getResponse(); - $this->close(); - - if ( isset($this->lastHeaders['HTTP_status']) && $this->lastHeaders['HTTP_status'] != 200 ) - return false; - - return $this->lastResponse; - } - - function build_query( $array ) { - $first = true; - $query_string = ""; - - if ( !is_array($array) || sizeof($array) == 0 ) - return ""; - - foreach ( $array as $key => $value ) { - $query_string .= ($first?"":"&") . "$key=" . urlencode($value); - if ( $first ) - $first = false; - } - - return $query_string; - } - } +/* vim:set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ +interface IWebService { + public function get ($entity, $id, $include, $filter, $version = '1'); + public function post($entity, $id, $data, $version = '1'); +} + +class mbWebService implements IWebService { + private $host; + private $port; + private $username; + private $password; + private $nonce; + private $realm; + private $pathPrefix; + private $lastError; + private $fSock; + private $lastResponse = ""; + private $lastHeaders = array(); + + /* + * Constructor + * + */ + public function __construct($host='musicbrainz.org', $port=80, $pathPrefix='/ws', $username='', $password='', $realm='musicbrainz.org') { + $this->host = $host; + $this->port = $port; + $this->pathPrefix = $pathPrefix; + $this->username = $username; + $this->password = $password; + $this->realm = $realm; + $this->fSock = -1; + } + + private function connect() { + if ($this->fSock != -1) { + return true; + } + + $this->fSock = fsockopen($this->host, $this->port, $errno, $this->lastError, 30); + + if ($this->fSock == false) { + $this->fSock = -1; + return false; + } + + return true; + } + + private function close() { + if ($this->fSock != -1) { + fclose($this->fSock); + $this->fSock = -1; + return true; + } + else { + $this->lastError = "Trying to close closed socket."; + return false; + } + } + + private function parseHeaders($string) { + $lines = explode("\n", $string); + $this->lastHeaders = array(); + + foreach ($lines as $key => $line) { + if ($key == 0) { // Status line + if (!preg_match("/^HTTP\/(\d+)\.(\d+) (\d+) .+$/", $line, $matches)) { + $this->lastHeaders = array(); + return false; + } + else { + $this->lastHeaders['HTTP_major_version'] = $matches[1]; + $this->lastHeaders['HTTP_minor_version'] = $matches[2]; + $this->lastHeaders['HTTP_status'] = $matches[3]; + } + } + else if ($line == "\r") { // Empty line + $new_string = ""; + for ($i = $key+1; $i < sizeof($lines); $i++) { + $new_string .= $lines[$i] . "\n"; + } + return $new_string; + } + else if (!preg_match("/^([^:]+): (.+)\r$/", $line, $matches)) { + // Not a header + $this->lastHeaders = array(); + return false; + } + else { // A header + $this->lastHeaders[$matches[1]] = $matches[2]; + } + } + + // Something failed (like having no body), so we clear and return false + $this->lastHeaders = array(); + return false; + } + + private function getHeaders() { + return $this->lastHeaders; + } + + private function runRequest($method, $uri, $post_data='') { + if ( ! ($this->connect() && + $this->sendRequest($method, $uri, $post_data))) { + return false; + } + + $this->lastResponse = $this->getResponse(); + + if ($this->lastHeaders['HTTP_status'] == 401) { + // We need to authenticate + $authrequest = $this->lastHeaders['WWW-Authenticate']; + preg_match('/nonce="(.+)"/', $authrequest, $matches); + $this->nonce = $matches[1]; + + $this->close(); + + if ( ! ($this->connect() && + $this->sendRequest($method, $uri, $post_data))) { + return false; + } + $this->lastResponse = $this->getResponse(); + } + + $this->close(); + + if (isset($this->lastHeaders['HTTP_status']) && $this->lastHeaders['HTTP_status'] != 200) { + return false; + } + + return $this->lastResponse; + } + + + private function sendRequest($method, $uri, $post_data='') { + if ($this->fSock == -1) { + $this->lastError = "Trying to write to closed socket."; + return false; + } + + fwrite($this->fSock, "$method $uri HTTP/1.1\r\n"); + fwrite($this->fSock, "Host: " . $this->host . "\r\n"); + fwrite($this->fSock, "Accept: */*\r\n"); + fwrite($this->fSock, "User-Agent: php_musicbrainz/1.0\r\n"); + if ($this->nonce) { + $h1 = md5($this->username . ':' . $this->realm . ':' . + $this->password); + $h2 = md5($method . ':' . $uri); + $response = md5($h1 . ':' . $this->nonce . ':' . $h2); + $authResponse = 'Authorization: Digest username="' . + $this->username . '", realm="' . $this->realm . '", nonce="' . + $this->nonce . '", uri="' . $uri . '", response="' . $response . + '"'; + fwrite($this->fSock, "$authResponse\r\n"); + } + fwrite( $this->fSock, "Connection: close\r\n\r\n"); + fwrite($this->fSock, $post_data . "\r\n\r\n"); + + return true; + } + + private function getResponse() { + if ($this->fSock == -1) { + $this->lastError = "Trying to read from closed socket."; + return false; + } + + $buffer = ""; + + while (!feof($this->fSock)) { + $buffer .= fread($this->fSock, 4096); + } + + if (!$this->parseHeaders($buffer)) { + return $buffer; + } + + return $this->parseHeaders($buffer); + } + + public function get($entity, $uid, $includes, $filters, $version="1") { + $params = array(); + $params['type'] = "xml"; + + if (is_array($includes)) { + $inc_string = ""; + foreach ($includes as $inc) { + if ($inc_string != "") + $inc_string .= " "; + $inc_string .= $inc; + } + if ($inc_string != "") { + $params['inc'] = $inc_string; + } + } + + if (is_array($filters)) { + foreach ($filters as $filter => $value) { + $params[$filter] = $value; + } + } + + $uri = $this->pathPrefix . "/" . $version . "/" . $entity . "/" . $uid . "?" . $this->build_query($params); + + + return $this->runRequest("GET", $uri); + } + + public function post($entity, $id, $data, $version = '1') { + $uri = $this->pathPrefix . '/' . $version . '/' . $entity . '/' . $id; + if ($this->fSock == -1 && !$this->connect()) { + return false; + } + + return $this->runRequest("POST", $uri, $data); + } + + private function build_query($array) { + $first = true; + $query_string = ""; + + if (!is_array($array) || sizeof($array) == 0) + return ""; + + foreach ($array as $key => $value) { + $query_string .= ($first ? "" : "&") . "$key=" . urlencode($value); + if ($first) { $first = false; } + } + + return $query_string; + } +} ?> diff --git a/modules/php_musicbrainz/mbXmlParser.php b/modules/php_musicbrainz/mbXmlParser.php index 46d39e7c..ebfcaab5 100644 --- a/modules/php_musicbrainz/mbXmlParser.php +++ b/modules/php_musicbrainz/mbXmlParser.php @@ -1,391 +1,405 @@ <?php /* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ - class mbParseError extends Exception { } +class mbParseError extends Exception { } - class mbXmlParser { - private $xml_parser; - private $factory; +class mbXmlParser { + private $xml_parser; + private $factory; - function mbXmlParser() { - $this->xml_parser = new xmlParser(); - $this->factory = new mbDefaultFactory(); + function mbXmlParser() { + $this->xml_parser = new xmlParser(); + $this->factory = new mbDefaultFactory(); + } + + private function parseList(XMLNode $node, array $list, $func) { + for ($i = 0; $i < $node->nChildNodes(); $i++) { + $cnode = $node->getChildNode($i); + $list[] = call_user_func(array($this, $func), $cnode); } + } - private function parseList( XMLNode $node, array &$list, $func ) { - for ( $i = 0; $i < $node->nChildNodes(); $i++ ) { - $cnode = $node->getChildNode($i); - $list[] = call_user_func( array( &$this, $func ), $cnode ); - } + private function parseResults(XMLNode $node, array $list, $func, $type) { + for ($i = 0; $i < $node->nChildNodes(); $i++) { + $cnode = $node->getChildNode($i); + $object = call_user_func(array($this, $func), $cnode); + $score = $cnode->getAttribute("ext:score"); + eval('$to_add = new ' . $type . 'Result($object,$score);'); + $to_add->setCount($node->getAttribute("count")); + $to_add->setOffset($node->getAttribute("offset")); + $list[] = $to_add; } + } - private function parseResults( XMLNode $node, array &$list, $func, $type ) { - for ( $i = 0; $i < $node->nChildNodes(); $i++ ) { - $cnode = $node->getChildNode($i); - $object = call_user_func( array( &$this, $func ), $cnode ); - $score = $cnode->getAttribute("ext:score"); - eval( '$to_add = new ' . $type . 'Result($object,$score);' ); - $to_add->setCount( $node->getAttribute("count") ); - $to_add->setOffset( $node->getAttribute("offset") ); - $list[] = $to_add; + private function parseRelations(XMLNode $node, MusicBrainzEntity $entity) { + $targetType = $node->getAttribute("target-type"); + if ($targetType == '') + return; + + for ($i = 0; $i < $node->nChildNodes(); $i++) { + $cnode = $node->getChildNode($i); + if (strtolower($cnode->getName()) == "relation") { + $relation = $this->createRelation($cnode, $targetType); + if ($relation) + $entity->addRelation($relation); } } + } - private function parseRelations( XMLNode $node, MusicBrainzEntity $entity ) { - $targetType = $node->getAttribute("target-type"); - if ( $targetType == '' ) - return; - - for ( $i = 0; $i < $node->nChildNodes(); $i++ ) { - $cnode = $node->getChildNode($i); - if ( strtolower($cnode->getName()) == "relation" ) { - $relation = $this->createRelation( $cnode, $targetType ); - if ( $relation ) - $entity->addRelation( $relation ); - } - } + private function parseUserResults(XMLNode $node, array $userList) { + for ($i = 0; $i < $node->nChildNodes(); $i++) { + $cnode = $node->getChildNode($i); + $userList[] = $this->createUser($cnode); } + } - private function parseUserResults( XMLNode $node, array &$userList ) { - for ( $i = 0; $i < $node->nChildNodes(); $i++ ) { - $cnode = $node->getChildNode($i); - $userList[] = $this->createUser( $cnode ); + private function createUser(XMLNode $node) { + $user = $this->factory->newUser(); + for ($i = 0; $i < $node->nChildNodes(); $i++) { + $cNode = $node->getChildNode($i); + $name = $cNode->getName(); + switch ($name) { + case "name": + $user->setName($cNode->getText()); + break; + case "ext:nag": + $user->setShowNag($node->getAttribute("show") == 'true' ? true : false); + break; } } + return $user; + } - private function createUser( XMLNode $node ) { - $user = $this->factory->newUser(); - for ( $i = 0; $i < $node->nChildNodes(); $i++ ) { - $cNode = $node->getChildNode($i); - $name = $cNode->getName(); - switch ( $name ) { - case "name": - $user->setName($cNode->getText()); - break; - case "ext:nag": - $user->setShowNag($node->getAttribute("show") == 'true' ? true : false); - break; - } + private function createArtistAlias(XMLNode $node) { + $aa = $this->factory->newArtistAlias(); + $aa->setType($node->getAttribute("type")); + $aa->setScript($node->getAttribute("script")); + $aa->setValue($node->getText()); + return $aa; + + } + + private function createTag(XMLNode $node) { + $tag = $this->factory->newTag(); + $tag->setCount($node->getAttribute("count")); + $tag->setName($node->getText()); + return $tag; + } + + private function createLabelAlias(XML $node) { + $la = $this->factory->newLabelAlias(); + $la->setType($node->getAttribute("type")); + $la->setScript($node->getAttribute("script")); + $la->setValue($node->getText()); + return $la; + } + + private function createDisc(XMLNode $node) { + $disc = $this->factory->newDisc(); + $disc->setId($node->getAttribute("id")); + return $disc; + } + + private function createReleaseEvent(XMLNode $node) { + $relEvent = $this->factory->newReleaseEvent(); + $relEvent->setCountry($node->getAttribute("country")); + $relEvent->setDate($node->getAttribute("date")); + $relEvent->setCatalogNumber($node->getAttribute("catalog-number")); + $relEvent->setBarcode($node->getAttribute("barcode")); + for ($i = 0; $i < $node->nChildNodes(); $i++) { + $cnode = $node->getChildNode($i); + switch (strtolower($cnode->getName())) { + case 'label': + $relEvent->setLabel($this->createLabel($cnode)); + break; } - return $user; } + return $relEvent; + } - private function createArtistAlias( XMLNode $node ) { - $aa = $this->factory->newArtistAlias(); - $aa->setType($node->getAttribute("type")); - $aa->setScript($node->getAttribute("script")); - $aa->setValue($node->getText()); - return $aa; + private function createRating(XMLNode $node) { + $rating = $this->factory->newRating(); + $rating->setRating($node->getText()); + return $rating; + } + private function createLabel(XMLNode $node) { + $label = $this->factory->newLabel(); + $label->setId($node->getAttribute("id")); + $label->setType($node->getAttribute("type")); + for ($i = 0; $i < $node->nChildNodes(); $i++) { + $cnode = $node->getChildNode($i); + switch (strtolower($cnode->getName())) { + case "name": + case "sort-name": + $label->setSortName($cnode->getText()); + break; + case "disambiguation": + $label->setDisambiguation($cnode->getText()); + break; + case "life-span": + $begin = $cnode->getAttribute("begin"); + $end = $cnode->getAttribute("end"); + if ($begin != "") { + $label->setBeginDate($begin); + } + if ($end != "") { + $label->setEndDate($end); + } + break; + case "alias-list": + $pAlias = $label->getAliases(); + $this->parseList($cnode, $pAlias, 'createAlias'); + break; + case "release-list": + $pRel = $label->getReleases(); + $label->setReleasesCount($cnode->getAttribute("count")); + $label->setReleasesOffset($cnode->getAttribute("offset")); + $this->parseList($cnode, $pRel, 'createRelease'); + break; + case "relation-list": + $this->parseRelations($cnode, $label); + break; + case "tag-list": + $pTag = $label->getTags(); + $this->parseList($cnode, $pTag, 'createTag'); + break; + } } - private function createTag( XMLNode $node ) { - $tag = $this->factory->newTag(); - $tag->setCount($node->getAttribute("count")); - $tag->setName($node->getText()); - return $tag; - } + return $label; + } - private function createLabelAlias( XML $node ) { - $la = $this->factory->newLabelAlias(); - $la->setType($node->getAttribute("type")); - $la->setScript($node->getAttribute("script")); - $la->setValue($node->getText()); - return $la; + private function createArtist(XMLNode $node) { + $artist = $this->factory->newArtist(); + $artist->setId($node->getAttribute("id")); + $artist->setType($node->getAttribute("type")); + for ($i = 0; $i < $node->nChildNodes(); $i++) { + $cnode = $node->getChildNode($i); + switch (strtolower($cnode->getName())) { + case "name": + $artist->setName($cnode->getText()); + break; + case "sort-name": + $artist->setSortName($cnode->getText()); + break; + case "disambiguation": + $artist->setDisambiguation($cnode->getText()); + break; + case "life-span": + $begin = $cnode->getAttribute("begin"); + $end = $cnode->getAttribute("end"); + if ($begin != "") { + $artist->setBeginDate($begin); + } + if ($end != "") { + $artist->setEndDate($end); + } + break; + case "alias-list": + $pAli = $artist->getAliases(); + $this->parseList($cnode, $pAli, 'createArtistAlias'); + break; + case "release-list": + $pRel = $artist->getReleases(); + $artist->setReleasesCount($cnode->getAttribute("count")); + $artist->setReleasesOffset($cnode->getAttribute("offset")); + $this->parseList($cnode, $pRel, 'createRelease'); + break; + case "relation-list": + $this->parseRelations($cnode, $artist); + break; + case "tag-list": + $pTag = $artist->getTags(); + $this->parseList($cnode, $pTag, 'createTag'); + break; + } } - private function createDisc( XMLNode $node ) { - $disc = $this->factory->newDisc(); - $disc->setId($node->getAttribute("id")); - return $disc; - } + return $artist; + } - private function createReleaseEvent( XMLNode $node ) { - $relEvent = $this->factory->newReleaseEvent(); - $relEvent->setCountry($node->getAttribute("country")); - $relEvent->setDate($node->getAttribute("date")); - $relEvent->setCatalogNumber($node->getAttribute("catalog-number")); - $relEvent->setBarcode($node->getAttribute("barcode")); - for ( $i = 0; $i < $node->nChildNodes(); $i++ ) { - $cnode = $node->getChildNode($i); - switch ( strtolower($cnode->getName()) ) { - case "label": - $relEvent->setLabel($this->createLabel($cnode)); - break; - } + private function createTrack(XMLNode $node) { + $track = $this->factory->newTrack(); + $track->setId($node->getAttribute("id")); + for ($i = 0; $i < $node->nChildNodes(); $i++) { + $cnode = $node->getChildNode($i); + switch(strtolower($cnode->getName())) { + case "title": + $track->setTitle($cnode->getText()); + break; + case "artist": + $track->setArtist($this->createArtist($cnode)); + break; + case "duration": + $track->setDuration($cnode->getText()); + break; + case "release-list": + $pRel = $track->getReleases(); + $track->setReleasesOffset($cnode->getAttribute("offset")); + $track->setReleasesCount($cnode->getAttribute("count")); + $this->parseList($cnode, $pRel, 'createRelease'); + break; + case "relation-list": + $this->parseRelations($cnode, $track); + break; + case "tag-list": + $pTag = $track->getTags(); + $this->parseList($cnode, $pTag, 'createTag'); + break; } - return $relEvent; } - private function createLabel( XMLNode $node ) { - $label = $this->factory->newLabel(); - $label->setId($node->getAttribute("id")); - $label->setType($node->getAttribute("type")); - for ( $i = 0; $i < $node->nChildNodes(); $i++ ) { - $cnode = $node->getChildNode($i); - switch ( strtolower($cnode->getName()) ) { - case "name": - case "sort-name": - $label->setSortName($cnode->getText()); - break; - case "disambiguation": - $label->setDisambiguation($cnode->getText()); - break; - case "life-span": - $begin = $cnode->getAttribute("begin"); - $end = $cnode->getAttribute("end"); - if ( $begin != "" ) - $label->setBeginDate($begin); - if ( $end != "" ) - $label->setEndDate($end); - break; - case "alias-list": - $pAlias = &$label->getAliases(); - $this->parseList( $cnode, $pAlias, 'createAlias' ); - break; - case "release-list": - $pRel = &$label->getReleases(); - $label->setReleasesCount($cnode->getAttribute("count")); - $label->setReleasesOffset($cnode->getAttribute("offset")); - $this->parseList( $cnode, $pRel, 'createRelease' ); - break; - case "relation-list": - $this->parseRelations( $cnode, $label ); - break; - case "tag-list": - $pTag = &$label->getTags(); - $this->parseList( $cnode, $pTag, 'createTag' ); - break; - } - } + return $track; + } - return $label; + private function createRelease(XMLNode $node) { + $release = $this->factory->newRelease(); + $release->setId($node->getAttribute("id")); + $array = array(); + $types = $node->getAttribute("type"); + $types = explode(' ', $types); + foreach ($types as $one_type) { + $array[] = $one_type; } - - private function createArtist( XMLNode $node ) { - $artist = $this->factory->newArtist(); - $artist->setId($node->getAttribute("id")); - $artist->setType($node->getAttribute("type")); - for ( $i = 0; $i < $node->nChildNodes(); $i++ ) { - $cnode = $node->getChildNode($i); - switch ( strtolower($cnode->getName()) ) { - case "name": - $artist->setName($cnode->getText()); - break; - case "sort-name": - $artist->setSortName($cnode->getText()); - break; - case "disambiguation": - $artist->setDisambiguation($cnode->getText()); - break; - case "life-span": - $begin = $cnode->getAttribute("begin"); - $end = $cnode->getAttribute("end"); - if ( $begin != "" ) - $artist->setBeginDate( $begin ); - if ( $end != "" ) - $artist->setEndDate( $end ); - break; - case "alias-list": - $pAli = &$artist->getAliases(); - $this->parseList( $cnode, $pAli, 'createArtistAlias' ); - break; - case "release-list": - $pRel = &$artist->getReleases(); - $artist->setReleasesCount($cnode->getAttribute("count")); - $artist->setReleasesOffset($cnode->getAttribute("offset")); - $this->parseList( $cnode, $pRel, 'createRelease' ); - break; - case "relation-list": - $this->parseRelations( $cnode, $artist ); - break; - case "tag-list": - $pTag = &$artist->getTags(); - $this->parseList( $cnode, $pTag, 'createTag' ); - break; - } + $release->setTypes($array); + + for ($i = 0; $i < $node->nChildNodes(); $i++) { + $cnode = $node->getChildNode($i); + switch (strtolower($cnode->getName())) { + case "title": + $release->setTitle($cnode->getText()); + break; + case "text-representation": + $release->setTextLanguage($cnode->getAttribute("language")); + $release->setTextScript($cnode->getAttribute("script")); + break; + case "asin": + $release->setAsin($cnode->getText()); + break; + case "artist": + $release->setArtist($this->createArtist($cnode)); + break; + case "release-event-list": + $pRelEv = $release->getReleaseEvents(); + $this->parseList($cnode, $pRelEv, 'createReleaseEvent'); + break; + case "disc-list": + $pDisc = $release->getDiscs(); + $this->parseList($cnode, $pDisc, 'createDisc'); + break; + case "track-list": + $pTrack = $release->getTracks(); + $release->setTracksOffset($cnode->getAttribute("offset")); + $release->setTracksCount($cnode->getAttribute("count")); + $this->parseList($cnode, $pTrack, 'createTrack'); + break; + case "relation-list": + $this->parseRelations($cnode, $release); + break; + case "tag-list": + $pTag = $release->getTags(); + $this->parseList($cnode, $pTag, 'createTag'); + break; } - - return $artist; } - private function createTrack( XMLNode $node ) { - $track = $this->factory->newTrack(); - $track->setId($node->getAttribute("id")); - for ( $i = 0; $i < $node->nChildNodes(); $i++ ) { - $cnode = $node->getChildNode($i); - switch( strtolower($cnode->getName()) ) { - case "title": - $track->setTitle($cnode->getText()); - break; - case "artist": - $track->setArtist($this->createArtist($cnode)); - break; - case "duration": - $track->setDuration($cnode->getText()); - break; - case "release-list": - $pRel = &$track->getReleases(); - $track->setReleasesOffset($cnode->getAttribute("offset")); - $track->setReleasesCount($cnode->getAttribute("count")); - $this->parseList( $cnode, $pRel, 'createRelease' ); - break; - case "relation-list": - $this->parseRelations( $cnode, $track ); - break; - case "tag-list": - $pTag = &$track->getTags(); - $this->parseList( $cnode, $pTag, 'createTag' ); - break; - } - } + return $release; + } - return $track; + private function createRelation(XMLNode $node, $type) { + $relation = $this->factory->newRelation(); + $relation->setType(extractFragment($node->getAttribute("type"))); // TODO: fixme + $relation->setTargetType($type); + $relation->setTargetId($node->getAttribute("target")); + + $dir = mbRelation::DIR_BOTH; + switch(strtolower($node->getAttribute("direction"))) { + case "forward": + $dir = mbRelation::DIR_FORWARD; + break; + case "backward": + $dir = mbRelation::DIR_BACKWARD; + break; } - - private function createRelease( XMLNode $node ) { - $release = $this->factory->newRelease(); - $release->setId($node->getAttribute("id")); - $array = array(); - $types = $node->getAttribute( "type" ); - $types = explode( ' ', $types ); - foreach ( $types as $one_type ) - $array[] = $one_type; - $release->setTypes( $array ); - - for ( $i = 0; $i < $node->nChildNodes(); $i++ ) { - $cnode = $node->getChildNode($i); - switch ( strtolower($cnode->getName()) ) { - case "title": - $release->setTitle($cnode->getText()); - break; - case "text-representation": - $release->setTextLanguage($cnode->getAttribute("language")); - $release->setTextScript($cnode->getAttribute("script")); - break; - case "asin": - $release->setAsin($cnode->getText()); - break; - case "artist": - $release->setArtist($this->createArtist($cnode)); - break; - case "release-event-list": - $pRelEv = &$release->getReleaseEvents(); - $this->parseList( $cnode, $pRelEv, 'createReleaseEvent' ); - break; - case "disc-list": - $pDisc = &$release->getDiscs(); - $this->parseList( $cnode, $pDisc, 'createDisc' ); - break; - case "track-list": - $pTrack = &$release->getTracks(); - $release->setTracksOffset($cnode->getAttribute("offset")); - $release->setTracksCount($cnode->getAttribute("count")); - $this->parseList( $cnode, $pTrack, 'createTrack' ); - break; - case "relation-list": - $this->parseRelations( $cnode, $release ); - break; - case "tag-list": - $pTag = &$release->getTags(); - $this->parseList( $cnode, $pTag, 'createTag' ); - break; - } + $relation->setDirection($dir); + + $relation->setBeginDate($node->getAttribute("begin")); + $relation->setEndDate($node->getAttribute("end")); + + // TODO: ns + $attrs = $node->getAttribute("attributes"); + $attributes = explode(' ', $attrs); + foreach ($attributes as $attr) + $relation->addAttribute($attr); + + $target = null; + if ($node->nChildNodes() > 0 ) { + $cnode = $node->getChildNode(0); + switch (strtolower($cnode->getName())) { + case "artist": + $target = $this->createArtist($cnode); + break; + case "release": + $target = $this->createRelease($cnode); + break; + case "track": + $target = $this->createTrack($cnode); + break; } - - return $release; } + $relation->setTarget($target); - private function createRelation( XMLNode $node, $type ) { - $relation = $this->factory->newRelation(); - $relation->setType(extractFragment($node->getAttribute("type"))); // TODO: fixme - $relation->setTargetType($type); - $relation->setTargetId($node->getAttribute("target")); + return $relation; + } - $dir = mbRelation::DIR_BOTH; - switch( strtolower($node->getAttribute("direction")) ) { - case "forward": - $dir = mbRelation::DIR_FORWARD; - break; - case "backward": - $dir = mbRelation::DIR_BACKWARD; - break; - } - $relation->setDirection($dir); - - $relation->setBeginDate($node->getAttribute("begin")); - $relation->setEndDate($node->getAttribute("end")); - - // TODO: ns - $attrs = $node->getAttribute("attributes"); - $attributes = explode( ' ', $attrs ); - foreach ( $attributes as $attr ) - $relation->addAttribute( $attr ); - - $target = null; - if ( $node->nChildNodes() > 0 ) { - $cnode = $node->getChildNode(0); - switch ( strtolower($cnode->getName()) ) { - case "artist": - $target = $this->createArtist($cnode); - break; - case "release": - $target = $this->createRelease($cnode); - break; - case "track": - $target = $this->createTrack($cnode); - break; - } - } - $relation->setTarget($target); + function parse($data) { + $nodes = $this->xml_parser->parse($data); - return $relation; + if ($nodes == false) { + throw new mbParseError($this->xml_parser->lastError(), 1); } - function parse( $data ) { - $nodes = $this->xml_parser->parse( $data ); - - if ( $nodes == false ) - throw new mbParseError( $this->xml_parser->lastError(), 1 ); - - $md = new mbMetadata(); - - - for ( $i = 0; $i < $nodes->nChildNodes(); $i++ ) { - $node = $nodes->getChildNode($i); - $name = strtolower($node->getName()); - - switch ( $name ) { - case "artist": - $md->setArtist($this->createArtist($node)); - break; - case "track": - $md->setTrack($this->createTrack($node)); - break; - case "release": - $md->setRelease($this->createRelease($node)); - break; - case "label": - $md->setLabel($this->createLabel($node)); - break; - case "artist-list": - $pArt = &$md->getArtistResults(); - $this->parseResults($node, $pArt, 'createArtist', 'Artist' ); - break; - case "track-list": - $pTrack = &$md->getTrackResults(); - $this->parseResults($node, $pTrack, 'createTrack', 'Track' ); - break; - case "release-list": - $pRel = &$md->getReleaseResults(); - $this->parseResults($node, $pRel, 'createRelease', 'Release' ); - break; - case "ext:user-list": - $list = &$md->getUserList(); - $this->parseUserResults( $node, $list ); - break; - } + $md = new mbMetadata(); + + for ($i = 0; $i < $nodes->nChildNodes(); $i++) { + $node = $nodes->getChildNode($i); + $name = strtolower($node->getName()); + + switch ($name) { + case "artist": + $md->setArtist($this->createArtist($node)); + break; + case "track": + $md->setTrack($this->createTrack($node)); + break; + case "release": + $md->setRelease($this->createRelease($node)); + break; + case "label": + $md->setLabel($this->createLabel($node)); + break; + case "user-rating": + $md->setRating($this->createRating($node)); + break; + case "artist-list": + $pArt = $md->getArtistResults(); + $this->parseResults($node, $pArt, 'createArtist', 'Artist'); + break; + case "track-list": + $pTrack = $md->getTrackResults(); + $this->parseResults($node, $pTrack, 'createTrack', 'Track'); + break; + case "release-list": + $pRel = $md->getReleaseResults(); + $this->parseResults($node, $pRel, 'createRelease', 'Release'); + break; + case "ext:user-list": + $list = $md->getUserList(); + $this->parseUserResults($node, $list); + break; } - return $md; } + return $md; } +} ?> |