<?
#
# SPARQL Timeline - A SPARQL/Timeline demo
#
# (c) 2003-2006 Morten Frederiksen
# License: http://www.gnu.org/licenses/gpl
#

define('REDLANDURI','mysql://mysql/redland');
include_once(
'service.php');
include_once(
'curl.php');

# Initialization.
$service=new Service(array(
#        'xslt'=>'morten-service.xsl'
        
));
$source='';
$date=gmdate('Y-m-d');

# Feed?
if ($service->match('SPARQL Feed Timeline Service Profile')) {
    
# Get input values.
    
$source=$feed=$service->input('SPARQL Feed Timeline Service Profile','feed');

    
# Get (or update) feed.
    
list($code,$feedfile,)=curl_cache_get($feed, 'SPARQL Timeline; '.$service->uri);
    if (
$code!=200 && $code!=304 || !file_exists($feedfile))
        
$service->error('500 Internal Server Error [Oops, unable to retrieve feed ('.htmlspecialchars($feed).'), got code '.$code.'.]', true);
    
$results=$resultfile=$feedfile.'.srx';
    if (!
file_exists($resultfile) || filemtime($feedfile)>filemtime($resultfile)) {
        
$data=@file($feedfile);
        
array_shift($data);
        
array_shift($data);
        
array_shift($data);
        
$data=join('',$data);
        
$tmp=tempnam('/tmp','sparql-timeline');
        if (!(
$fp=@fopen($tmp,'w')) || !@fwrite($fp,$data) || !@fclose($fp))
            
$service->error('500 Internal Server Error [Oops, unable to save feed ('.htmlspecialchars($feed).').]', true);
        
$data='file:'.$tmp;
        
$uri=librdf_new_uri($World, $data);

        
# Create Redland objects and process query.
        
$nulluri=librdf_new_uri($World, '-');
        
$dawgxmluri=librdf_new_uri($World, 'http://www.w3.org/2005/sparql-results#');
        
$query='
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rss: <http://purl.org/rss/1.0/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>

SELECT DISTINCT ?date ?title ?description ?link
WHERE {
  ?x rdf:type rss:item .
  ?x rss:title ?title .
  ?x rss:description ?description .
  ?x rss:link ?link .
  ?x dc:date ?date
}
ORDER BY ?date
'
;
        
$Storage=0;
        
$Model=0;
        
$Query=0;
        
$Results=0;
        
$result='';
        if (!(
$Storage=librdf_new_storage($World,
                
'hashes','temp',"hash-type='memory',contexts='yes'")))
            
$service->error('500 Internal Server Error [Unable to create temporary storage for model'.redland_error().']');
        elseif (!(
$Model=librdf_new_model($World, $Storage, '')))
            
$service->error('500 Internal Server Error [Unable to create model'.redland_error().']');
        elseif (
librdf_model_load($Model,$uri,'','',$nulluri))
            
$service->error('500 Internal Server Error [Unable to load content'.redland_error().']');
        elseif (!(
$Query=librdf_new_query($World, 'sparql', $nulluri, $query, $nulluri)))
            
$service->error('500 Internal Server Error [Unable to create query'.redland_error().']');
        elseif (!(
$Results=librdf_query_execute($Query, $Model)))
            
$service->error('500 Internal Server Error [Unable to execute query'.redland_error().']');
        elseif (
librdf_query_results_is_bindings($Results)
                && !(
$result=librdfutil_query_results_to_string($Results,$dawgxmluri,$nulluri,preg_match('|select\s+distinct|i',$query))))
            
$service->error('500 Internal Server Error [Unable to serialise query results from bindings'.redland_error().']');
        elseif (!(
$fp=@fopen($resultfile,'w')) || !@fwrite($fp,$result) || !@fclose($fp))
            
$service->error('500 Internal Server Error [Oops, unable to save results from query.]');
        
$results=$resultfile;
        
unlink($tmp);
        
librdf_free_uri($uri);
        
librdf_free_uri($nulluri);
        
librdf_free_uri($dawgxmluri);
        if (
$Query)
            
librdf_free_query($Query);
        if (
$Model)
            
librdf_free_model($Model);
        if (
$Storage)
            
librdf_free_storage($Storage);
    }
}

# Results
elseif ($service->match('SPARQL Results Timeline Service Profile')) {
    
# Get input values.
    
$source=$service->input('SPARQL Results Timeline Service Profile','results');
    list(
$code,$sourcefile,)=curl_cache_get($source, 'SPARQL Timeline; '.$service->uri);
    if (
$code!=200 && $code!=304 || !file_exists($sourcefile))
        
$service->error('500 Internal Server Error [Oops, unable to retrieve results file ('.htmlspecialchars($source).'), got code '.$code.'.]', true);
    
$results=$sourcefile.'.srx';
    if (!
file_exists($results) || filemtime($sourcefile)>filemtime($results)) {
        
$data=@file($sourcefile);
        
array_shift($data);
        
array_shift($data);
        
array_shift($data);
        
$data=join('',$data);
        if (!(
$fp=@fopen($results,'w')) || !@fwrite($fp,$data) || !@fclose($fp))
            
$service->error('500 Internal Server Error [Oops, unable to save results file ('.htmlspecialchars($source).').]', true);
    }
}

# Generate output.
if (!$service->errors && ''!=$source) {
    
$html=join('',@file('timeline.html'));
    
$html=str_replace('<<<SPARQL-BEGIN>>>',$date,$html);
    
$html=str_replace('<<<SPARQL-SOURCE>>>',$source,$html);
    
$html=str_replace('<<<SPARQL-RESULTS>>>',$results,$html);
    
header('Content-Type: text/html; charset=UTF-8');
    print
$html;
    
$service->free();
}

# Finish.
$service->finish();

# Handle Redland error.
function redland_error() {
    global
$World;
    
$nulluri=librdf_new_uri($World,'-');
    
$log=librdf_world_get_feature($World,$nulluri);
    
librdf_free_uri($nulluri);
    if (
$log)
        return
': <strong>'.librdf_log_message_message($log).'</strong>';
    else
        return
'.';
}

?>