<?php 
  
/* build the SPARQL query*/
  
$sparql '
    PREFIX awm:<http://muse.aucklandmuseum.com/databases/cenotaph#>

    SELECT *
    WHERE
    {
       {
          SELECT ?namePosition (COUNT(?namePosition) AS ?numberDied)
          WHERE {
            ?person awm:diedAt_country ?diedAt .
            ?diedAt awm:addressConstitutedOf ?addressConstitutedOf .
            ?addressConstitutedOf awm:namePosition ?namePosition .
          }
          GROUP BY ?namePosition
          HAVING (COUNT(?namePosition) > 3)
          ORDER BY DESC(?numberDied) ?namePosition
       }
    UNION
       {
          SELECT ?namePosition (COUNT(?numberDied) as ?numberDied)
          WHERE
          {
             SELECT ?namePosition (COUNT(?namePosition) AS ?numberDied)
             WHERE {
               ?person awm:diedAt_country ?diedAt .
               ?diedAt awm:addressConstitutedOf ?addressConstitutedOf .
               ?addressConstitutedOf awm:namePosition ?nameOfPosition .
               BIND ( "Other" AS ?namePosition)
             }
             GROUP BY ?nameOfPosition ?namePosition
             HAVING (COUNT(?namePosition) <= 3)
             ORDER BY DESC(?numberDied) ?namePosition
          }
          GROUP BY ?namePosition
       }
    }'
;

  
/* retrieve the results of the SPARQL query */

  
$url 'http://cenotaph.mebooks.co.nz/endpoint.php';
  
$ch curl_init$url );

  
curl_setopt$chCURLOPT_POSTtrue );
  
// 'format' can either be 'json' or 'xml' (defaults to 'xml' if not specified).
  
curl_setopt$chCURLOPT_POSTFIELDS, array('query' => urlencode($sparql), 'format' => $_GET['format'], 'user' => '1234') );
  
curl_setopt$chCURLOPT_FOLLOWLOCATIONtrue );
  
curl_setopt$chCURLOPT_HEADERtrue );
  
curl_setopt$chCURLOPT_RETURNTRANSFERtrue );          
  
curl_setopt$chCURLOPT_USERAGENT$_SERVER['HTTP_USER_AGENT'] );
  
// supress the "100 Continue" header.
  
curl_setopt$chCURLOPT_HTTPHEADER, array( 'Expect:' ) );

  
$response curl_exec$ch );
  list( 
$header$contents ) = preg_split'/([\r\n][\r\n])\\1/'$response);
  
$status curl_getinfo$ch );
  
curl_close$ch );
  
  
// spit out the results.
  
echo "$contents;\n";