diff --git a/common.inc.php b/common.inc.php index a50f94ce25e43661e2ffe21ed1e074fb79ff39e0..c4e9256411a473b05500a84eedff6e2af9b59a57 100644 --- a/common.inc.php +++ b/common.inc.php @@ -36,6 +36,7 @@ $conf['service']['prefix'] = 'service.'; $conf['service']['class'] = 'classes/BasicSpecialFunction.php'; $conf['class']['prefix'] = 'class.'; +$conf['uri']['prefix'] = 'uri.'; //Frontpage when user goes to http://example.org/ $conf['root'] = 'index.html'; @@ -51,7 +52,7 @@ $conf['debug'] = false; //using the modules in the following order $conf['modules'] = array(); $conf['modules']['directory'] = 'modules/'; -$conf['modules']['available'] = array('static','class', 'service'); +$conf['modules']['available'] = array('static','uri', 'class', 'service'); include_once('settings.inc.php'); $conf['view']['standard']['baseUrl'] = $conf['basedir']; diff --git a/modules/uriModule.php b/modules/uriModule.php new file mode 100644 index 0000000000000000000000000000000000000000..492306c7160feca3ead2f6a080d653d5ff711188 --- /dev/null +++ b/modules/uriModule.php @@ -0,0 +1,119 @@ +<? +require_once('abstractModule.php'); +class UriModule extends abstractModule{ + //Uri module + + public function match($uri){ + global $conf; + global $localUri; + global $uri; + global $acceptContentType; + global $endpoints; + global $base; + + require_once('classes/MetaDb.php'); + $metaDb = new MetaDb($conf['metadata']['db']['location']); + + $pair = Queries::getMetadata($localUri, $acceptContentType, $metaDb); + + if($pair == NULL){ // Original URI is not in metadata + if(Queries::uriExist($uri, $endpoints['local'])){ + $page = Queries::createPage($uri, $localUri, $acceptContentType, $metaDb); + if($page == NULL){ + Utils::send500("Can't write sqlite database."); + } + Utils::send303($page, $acceptContentType); + exit(0); + }else{ + return false; //Utils::send404($uri); + } + } + $extension = Utils::getExtension($pair[2]); + $curie = Utils::uri2curie($pair[0]); + list($modelFile, $viewFile) = $this::getModelandView($curie, $extension); + + if($modelFile == NULL){ + return FALSE; + } + return $pair; + } + + public function execute($pair){ + global $conf; + global $localUri; + global $uri; + global $acceptContentType; + global $endpoints; + global $base; + global $results; + global $first; + list($res, $page, $format) = $pair; + $uri = $res; + $curie = Utils::uri2curie($res); + + //If resource is not the page, send a 303 to the document + if($res == $localUri){ + Utils::send303($page, $acceptContentType); + } + + $uri = $res; + if($conf['mirror_external_uris']){ + $localUri = preg_replace("|^".$conf['ns']['local']."|", $conf['basedir'], $res); + } + + $extension = Utils::getExtension($format); + + /*Redefine Content type based on the + * dcterms:format for this page + */ + $acceptContentType = $format; + + $curie = Utils::uri2curie($uri); + list($modelFile, $viewFile) = $this::getModelandView($curie, $extension); + if($modelFile == NULL){ + return; + } + + $base = $conf['view']['standard']; + $base['type'] = $modelFile; + $base['this']['value'] = $uri; + $base['this']['curie'] = Utils::uri2curie($uri); + $base['thislocal']['value'] = $localUri; + $base['thislocal']['curie'] = Utils::uri2curie($localUri); + + $base['this']['contentType'] = $acceptContentType; + $base['model']['directory'] = $conf['model']['directory']; + $base['view']['directory'] = $conf['view']['directory']; + $base['ns'] = $conf['ns']; + + + chdir($conf['home'].$conf['model']['directory']); + + Utils::queryFile($modelFile, $endpoints['local'], $results, $first); + $results = Utils::internalize($results); + + $base['first'] = Utils::getFirsts($results); + chdir($conf['home']); + if(is_array($results)){ + $resultsObj = Convert::array_to_object($results); + }else{ + $resultsObj = $results; + } + Utils::processDocument($viewFile, $base, $resultsObj); + + } + + private static function getModelandView($uri, $extension){ + global $conf; + $auxViewFile = $conf['view']['directory'].$conf['uri']['prefix'].$uri.'/'.$extension.'.template'; + $auxModelFile = $conf['model']['directory'].$conf['uri']['prefix'].$uri.'/'.$extension.'.queries'; + if(file_exists($auxModelFile) && file_exists($auxViewFile) ){ + $viewFile = $conf['uri']['prefix'].$uri.'/'.$extension.'.template'; + $modelFile = $conf['uri']['prefix'].$uri.'/'.$extension.'.queries'; + return array($modelFile, $viewFile); + } + return array(NULL, NULL); + } + +} +?> diff --git a/utils/modules/create-uri.sh b/utils/modules/create-uri.sh new file mode 100755 index 0000000000000000000000000000000000000000..8562599817abccfb3d50a3ac03c6f37c99066946 --- /dev/null +++ b/utils/modules/create-uri.sh @@ -0,0 +1,153 @@ +#!/bin/bash + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +initToken='uri' + +modelHtml=$(cat <<QUERY +SELECT ?s2 ?p2 ?s1 ?p1 WHERE { + { + <{{uri}}> ?s1 ?p1 . + }UNION{ + ?s2 ?p2 <{{uri}}> . + } +} +QUERY +) + + +viewHtml=$(cat <<VIEW +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" + "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"> + <html xmlns="http://www.w3.org/1999/xhtml" {% for i, ns in base.ns %}xmlns:{{i}}="{{ns}}" + {%endfor%}version="XHTML+RDFa 1.0" xml:lang="en"> + <head> + <title>Page about {{base.this.value}}</title> + <link href="{{base.baseUrl}}/lodspeakr/css/basic.css" rel="stylesheet" type="text/css" media="screen" /> + <link rel="alternate" type="application/rdf+xml" title="RDF/XML Version" href="{{base.this.value}}.rdf" /> + <link rel="alternate" type="text/turtle" title="Turtle Version" href="{{base.this.value}}.ttl" /> + <link rel="alternate" type="text/plain" title="N-Triples Version" href="{{base.this.value}}.nt" /> + <link rel="alternate" type="application/json" title="RDFJSON Version" href="{{base.this.value}}.json" /> + </head> + <body about="{{base.this.value}}"> + <h1>Page about <a href='{{base.this.value}}'>{{base.this.curie}}</a></h1> + <div> + <h2>Information from {{base.this.curie}}</h2> + <table> + {% for row in r.main %} + + {% if row.s1%} + <tr> + <td><a href='{{row.s1.value}}'>{{row.s1.curie}}</a></td> + + {% if row.p1.uri == 1 %} + <td><a rel='{{row.s1.curie}}' href='{{row.p1.value}}'>{{row.p1.curie}}</a></td> + {% else %} + <td><span property='{{row.s1.curie}}'>{{row.p1.value}}</span></td> + {% endif %} + + </tr> + {% endif %} + {% endfor %} + </table> + + <br/><br/> + <h2>Information pointing to {{base.this.curie}}</h2> + <table> + {% for row in r.main %} + {% if row.s2%} + <tr> + <td><a href='{{row.s2.value}}'>{{row.s2.curie}}</a></td> + <td><a rev='[{{row.p2.curie}}]' href='{{row.s2.value}}'>{{row.p2.curie}}</a></td> + </tr> + {%endif %} + {% endfor %} + </table> + </div> + </body> +</html> +VIEW) + +modelRdf=$(cat <<QUERY +DESCRIBE ?resource WHERE { + [] a ?resource . +} +QUERY) + +viewRdf=$(cat <<VIEW +{{r|safe}} +VIEW) + +modelTtl=$modelRdf +viewTtl=$viewRdf +modelNt=$modelRdf +viewNt=$viewRdf +modelJson=$modelRdf +viewJson=$viewJson + +#Check models +mainModelDir=$DIR/../../models/$initToken.$1 + +if [ -e "$mainModelDir" ] +then + echo "WARNING: At least one model for $1 exists." >&2 +else + mkdir $mainModelDir +fi + +obj=( ) +if [ "$2" == "all" ] +then + obj=( html rdf ttl nt json ) +else + obj=( $2 ) +fi + +for i in ${obj[@]} +do + if [ -e $mainModelDir/$i.queries ] + then + echo ERROR: $initToken.$1/$i.queries exists in models. Operation aborted >&2 + exit 1 + fi +done + + +#Check views +mainViewDir=$DIR/../../views/$initToken.$1 + +if [ -e "$mainViewDir" ] +then + echo "WARNING: At least one view for $1 exists." >&2 +else + mkdir $mainViewDir +fi + + +for i in ${obj[@]} +do + if [ -e $mainViewDir/$i.template ] + then + echo ERROR: $initToken.$1/$i already exist in views. Operation aborted >&2 + exit 1 + fi +done + + +#Create file structure + +for i in ${obj[@]} +do + mkdir $mainModelDir/$i.queries + if [ "$i" == "html" ] + then + echo "$modelHtml" > $mainModelDir/$i.queries/main.query + echo "$viewHtml" > $mainViewDir/$i.template + else + echo "$modelRdf" > $mainModelDir/$i.queries/main.query + echo "$viewRdf" > $mainViewDir/$i.template + fi +done + +echo $initToken.$1 created/modified successfully! >&2 diff --git a/utils/modules/delete-uri.sh b/utils/modules/delete-uri.sh new file mode 100755 index 0000000000000000000000000000000000000000..eaf82c3c06e3c62cacd542536575d2cbaa19d530 --- /dev/null +++ b/utils/modules/delete-uri.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +initToken='uri' + + +#Check models +mainModelDir=$DIR/../../models/$initToken.$1 + +if [ ! -e "$mainModelDir" ] +then + echo "ERROR: $initToken.$1 doesn't exist in models. Operation aborted" >&2 + exit 1 +fi + +obj=( ) +if [ "$2" == "all" ] +then + obj=( html rdf ttl nt json ) +else + obj=( $2 ) +fi + +for i in ${obj[@]} +do + if [ ! -e $mainModelDir/$i.queries ] + then + echo "WARNING: $initToken.$1/$i.query does not exists in models." >&2 + fi +done + + +#Check views +mainViewDir=$DIR/../../views/$initToken.$1 + +if [ ! -e "$mainViewDir" ] +then + echo "ERROR: $initToken.$1 doesn't exist in views. Operation aborted." >&2 +fi + + +for i in ${obj[@]} +do + if [ ! -e $mainViewDir/$i.template ] + then + echo "WARNING: $mainViewDir/$i.template doesn't exist in views." >&2 + fi +done + + +#Delete file structure + +if [ "$2" == "all" ] +then + rm -rf $mainModelDir + rm -rf $mainViewDir +else + for i in ${obj[@]} + do + rm -rf $mainModelDir/$i.queries + rm -rf $mainViewDir/$i.template + done +fi +echo $initToken.$1 deleted successfully! >&2