<?php
/* Include ARC2 classes. */
include_once("arc/ARC2.php");

/* Configure the app to use DBPedia. */
$dbpconfig = array(
  "remote_store_endpoint" => "http://dbpedia.org/sparql",
);

/* Create the 'remote store' */
$dbpedia = ARC2::getRemoteStore($dbpconfig);

/* Configure the app to use DBTune. */
$dbtconfig = array(
  "remote_store_endpoint" => "http://dbtune.org/musicbrainz/sparql",
);

/* Create the 'remote store' */
$dbtune = ARC2::getRemoteStore($dbtconfig);

/* Let's start with some artist (Brian Eno). */
$mbid = "ff95eb47-41c4-4f7f-a104-cdc30f02e872";
if ($_GET['mbid']) {
  $mbid = $_GET['mbid'];
}
$uri = "http://dbtune.org/musicbrainz/resource/artist/$mbid";

/* Let's get some MusicBrainz data about the artist. */
/* Note that we now pull out the DBPedia URI. */
$q = "
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX vocab: <http://dbtune.org/musicbrainz/resource/vocab/>
PREFIX mo: <http://purl.org/ontology/mo/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>

SELECT ?name ?sortname ?musicbrainz ?dbpuri
WHERE
 { <$uri> rdfs:label ?name ;
          vocab:sortname ?sortname ;
          mo:musicbrainz ?musicbrainz ;
          owl:sameAs ?dbpuri .
   FILTER ( regex(str(?dbpuri), \"http://dbpedia.org/\") ) }
";
$artist = $dbtune->query($q, 'row');
$dbpuri = $artist['dbpuri'];

/* And let's pull up his records. */
$q = "
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX mo: <http://purl.org/ontology/mo/>
PREFIX vocab: <http://dbtune.org/musicbrainz/resource/vocab/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>

SELECT DISTINCT ?name ?amazon ?coverarturl ?date ?trackcount ?musicbrainz
WHERE
 { ?record foaf:maker <$uri> ;
           dc:title ?name ;
           mo:amazon_asin ?amazon ;
           vocab:albummeta_coverarturl ?coverarturl ;
           dc:date ?date ;
           vocab:tracks ?trackcount ;
           mo:musicbrainz ?musicbrainz . } 
ORDER BY ?date
";
$albums = $dbtune->query($q, 'rows');

/* Now let's pull in some DBPedia data. */
$q = "
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?birthDate ?birthPlace ?deathDate ?deathPlace ?abstract ?img ?yearsActive ?wikipedia
WHERE
 { <$dbpuri> rdfs:comment ?abstract ;
             foaf:page ?wikipedia .
   OPTIONAL { <$dbpuri> dbpprop:yearsActive ?yearsActive . }
   OPTIONAL { <$dbpuri> dbpedia-owl:thumbnail ?img . }
   OPTIONAL { <$dbpuri> dbpedia-owl:birthDate ?birthDate ;
                        dbpedia-owl:birthPlace ?birthPlace . }
   OPTIONAL { <$dbpuri> dbpedia-owl:deathDate ?deathDate ;
                        dbpedia-owl:deathPlace ?deathPlace . }
   FILTER ( langMatches(lang(?abstract), \"EN\") ) }
";
$dbpArtist = $dbpedia->query($q, 'row');

/* Let's also grab genres and instruments. */
$q = "
PREFIX dbpedia-owl-artist: <http://dbpedia.org/ontology/Artist/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?genre ?genreuri
WHERE
 { <$dbpuri> dbpedia-owl-artist:genre ?genreuri .
   ?genreuri rdfs:label ?genre . 
   FILTER ( langMatches(lang(?genre), \"EN\") ) }
";
$genres = $dbpedia->query($q, 'rows');

$q = "
PREFIX dbpedia-owl-artist: <http://dbpedia.org/ontology/Artist/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?instrument
WHERE
 { <$dbpuri> dbpedia-owl-artist:instrument ?instrumenturi .
   ?instrumenturi rdfs:label ?instrument .
   FILTER ( langMatches(lang(?instrument), \"EN\") ) }
";
$instruments = $dbpedia->query($q, 'rows');

?>
<html>
<head>
<title>Artist: <?=$artist['name']?></title>
</head>
<body>
<h1><?=$artist['name']?></h1>
<p><img src="<?=$dbpArtist['img']?>"/></p>
<p>(<?=$artist['sortname']?>)</p>
<p>(<?=$dbpArtist['birthDate']?>-<?=$dbpArtist['deathDate']?>)</p>
<p>(<?=$dbpArtist['yearsActive']?>)</p>
<p>(<a href="<?=$artist['musicbrainz']?>">MusicBrainz</a>, <a href="<?=$dbpArtist['wikipedia']?>">Wikipedia</a>)</p>
<p>Born: <?=$dbpArtist['birthDate']?> in <?=$dbpArtist['birthPlace']?></p>
<p>Died: <?=$dbpArtist['deathDate']?> in <?=$dbpArtist['deathPlace']?></p>
  <p>Genres: <?php foreach ($genres as $genre) { print "<a href=\"genre1.php?genre=" . substr($genre['genreuri'], 28) . "\">" . $genre['genre'] . "</a>"; } ?></p>
<p>Instruments: <?php foreach ($instruments as $instrument) { print $instrument['instrument']; } ?></p>
<p><?=$dbpArtist['abstract']?></p>
<table>
<thead>
<tr>
<th></th>
<th>Album</th>
<th>Release</th>
<th>Tracks</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
foreach ($albums as $album) {
?>
<tr>
<td><img src="<?=$album['coverarturl']?>" style="width: 72px; height: 72px;"/></td>
<td><?=$album['name']?></td>
<td><?=$album['date']?></td>
<td><?=$album['trackcount']?></td>
<td><a href="<?=$album['musicbrainz']?>">MusicBrainz</a></td>
<td><a href="<?=$album['amazon']?>">Amazon</a></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>