<?php
function formatDate($date, $emptyIsPresent=false) {
  if ($emptyIsPresent && $date == '') {
    return 'present';
  }
  
  $dateArray = explode('-', $date);
  $obj = new DateTime($date);
  if ($dateArray[1] == '00') {
    $obj->modify('+1 year');
    return $obj->format('Y');
  } elseif ($dateArray[2] == '00') {
    $obj->modify('+1 month');
    return $obj->format('F Y');
  } else {
    return $obj->format('F j, Y');
  }
}

/* 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 ?birthPlaceName ?deathPlaceName ?birthPlaceWiki ?deathPlaceWiki
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 { ?birthPlace rdfs:label ?birthPlaceName ;
                          foaf:page ?birthPlaceWiki . }
   OPTIONAL { <$dbpuri> dbpedia-owl:deathDate ?deathDate ;
                        dbpedia-owl:deathPlace ?deathPlace . }
   OPTIONAL { ?deathPlace rdfs:label ?deathPlaceName ;
                          foaf:page ?deathPlaceWiki . }
   FILTER ( langMatches(lang(?abstract), \"EN\") ) }
";
$dbpArtist = $dbpedia->query($q, 'row');

if ($dbpArtist['birthPlaceName']) {
  $dbpArtist['birthPlace'] = '<a href="' . $dbpArtist['birthPlaceWiki'] . '">' . $dbpArtist['birthPlaceName'] . '</a>';
}
if ($dbpArtist['deathPlaceName']) {
  $dbpArtist['deathPlace'] = '<a href="' . $dbpArtist['deathPlaceWiki'] . '">' . $dbpArtist['deathPlaceName'] . '</a>';
}

/* 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>
<link rel="stylesheet" href="music.css"/>
</head>
<body>
<h1 id="name"><?=$artist['name']?></h1>
<?php
if ($artist['sortname']) {
?>
<span id="sortname">(<?=$artist['sortname']?>)</span>
<?php
}
?>
<?php
if ($dbpArtist['img']) {
?>
<div id="image">
<img src="<?=$dbpArtist['img']?>" alt="<?=$artist['name']?>"/>
<?php
if ($dbpArtist['birthDate']) {
?>
<p id="imageCaption"><?=$artist['name']?><br />(<?=formatDate($dbpArtist['birthDate'])?>&ndash;<?=formatDate($dbpArtist['deathDate'], true)?>)</p>
</div>
<?php
}
}
if ($dbpArtist['birthDate']) {
?>
<p id="birth" class="eventline"><strong>Born:</strong> <?=formatDate($dbpArtist['birthDate'])?> in <?=$dbpArtist['birthPlace']?></p>
<?php
}
if ($dbpArtist['deathDate']) {
?>
<p id="death" class="eventline"><strong>Died:</strong> <?=$dbpArtist['deathDate']?> in <?=$dbpArtist['deathPlace']?></p>
<?php
}
if ($dbpArtist['yearsActive']) {
?>
<p id="yearsactive" class="eventline"><strong>Years Active:</strong> <?=$dbpArtist['yearsActive']?></p>
<?php
}
?>
<p id="genres" class="dataline"><strong>Genres:</strong> <?php $i = 0; foreach ($genres as $genre) { print (($i++ != 0) ? ", " : "") . "<a href=\"genre.php?genre=" . substr($genre['genreuri'], 28) . "\">" . $genre['genre'] . "</a>"; } ?></p>
<p id="instruments" class="dataline"><strong>Instruments:</strong> <?php $i = 0; foreach ($instruments as $instrument) { print (($i++ != 0) ? ", " : "") . $instrument['instrument']; } ?></p>
<p id="abstract"><?=$dbpArtist['abstract']?></p>
<?php
if ($dbpArtist['wikipedia'] || $artist['musicbrainz']) {
?>
<p id="links">
<?php
if ($artist['musicbrainz']) {
?>
<a href="<?=$artist['musicbrainz']?>">Musicbrainz</a>
<?php
}
if ($dbpArtist['wikipedia']) {
?>
<a href="<?=$dbpArtist['wikipedia']?>">Wikipedia</a></p>
<?php
}
?>
</p>
<?php
}
?>
<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><?=formatDate($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>