<?php
// 2007-06-04 Written by Hugh Glaser (hg@ecs.soton.ac.uk), with some help, esp. from Ian Millard.
// This is a 404 error script.
// We catch a URI such as http://dblp.rkbexplorer.com/rdf/people-e3509fdeb9dbe0d49774283361797993-08a1ef037db7e1e9459f600262eb94c4
// and look it up as both subject and object in the appropriate threestore, returning the rdf we get.
// Very rough in general, especially the rdf lookup and post-processing.
// If the URI is not found, we return a proper 404.
// If CACHING, then a file is also created at the exact point that apache will look for the file next time.
// Thus this does not need to check if the lookup is cached, as the web server will simply serve the file if it is there.

        define("CACHING", "TRUE");

        $PREFIX = "PREFIX akt:  <http://www.aktors.org/ontology/portal#> PREFIX akts: <http://www.aktors.org/ontology/support#>";

        $threestore = $_SERVER['SERVER_NAME'];
        $resource =  basename($_SERVER['REQUEST_URI']);
        $uri = "http://$threestore/rdf/$resource";
        // For safety, here you should check your threestore existence
        // Build the RDF - could use DESCRIBE or whatever else you want instead of the following
        $q = "$PREFIX CONSTRUCT { <$uri> ?p ?o . } WHERE { <$uri> ?p ?o . }";
        $cmd = '/usr/local/bin/ts-query -d '.$threestore.' ' . escapeshellarg($q);
        $results = "";
        exec($cmd, $results);
        $r1 = array_slice($results, 0, -1);
        $q = "$PREFIX CONSTRUCT { ?s ?p <$uri> . } WHERE { ?s ?p <$uri> . }";
        $cmd = '/usr/local/bin/ts-query -d '.$threestore.' ' . escapeshellarg($q);
        $results = "";
        exec($cmd, $results);
        $r2 = array_slice($results, 2);
        $results = array_merge($r1, $r2);

        if ((empty($results)) || ($results[2] == '</rdf:RDF>')) {
                // threestore did not exist or query was empty, so issue real 404
                // could do the multilanguage bit here
                header('HTTP/1.1 404 Not Found');
                echo "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>404 Not Found</title>\n</head>";
                echo "<body>\n<h1>Not Found</h1>\n<p>The requested URL ".$_SERVER['REQUEST_URI']." was not found on this server.</p>\n";
                echo "<hr>\n".$_SERVER['SERVER_SIGNATURE']."\n</body></html>\n";
                exit;
        } else {
                header('HTTP/1.1 200 OK');
                header('Content-type: text/xml');
                if (CACHING) { # Could probably have more error checking for caching
                        # Uses tmp file to avoid concurrent access to partially built file, by using rename (thanks Steve Harris!)
                        if (!file_exists("$threestore")) system("mkdir ".escapeshellarg("$threestore"));
                        $tmp = "tmp-".rand();
                        $handle = fopen("$threestore/$tmp", "w");
                };
                foreach ($results as $key => $value) {
                        echo $value."\n";
                        if (CACHING) fwrite($handle, $value."\n");
                };
                if (CACHING) {
                        rename("$threestore/$tmp", "$threestore/$resource");
                        fclose($handle); 
                        };
        }
?>
