From 48d5112d15c5bfbb3b37a6862a040a5ef97ff1ea Mon Sep 17 00:00:00 2001
From: Alvaro Graves <alvaro@graves.cl>
Date: Mon, 22 Aug 2011 11:50:39 -0300
Subject: [PATCH] LODSPeaKr works with sqlite3 instead of using ARC's endpoint

Removed functions related to extract  metadata from a SPARQL endpoint,
it stores it on the sqlite database. This will ease the installation
process
---
 classes/MetaDb.php  |  46 +++++++++++++++++++
 classes/Queries.php | 105 ++++++++++++++++++--------------------------
 common.inc.php      |   6 +--
 endpoint.php        |  41 -----------------
 index.php           |  44 +++++++++----------
 install.sh          |  75 -------------------------------
 meta/db.sqlite      | Bin 2048 -> 36864 bytes
 7 files changed, 112 insertions(+), 205 deletions(-)
 create mode 100644 classes/MetaDb.php
 delete mode 100644 endpoint.php

diff --git a/classes/MetaDb.php b/classes/MetaDb.php
new file mode 100644
index 00000000..305cd0b8
--- /dev/null
+++ b/classes/MetaDb.php
@@ -0,0 +1,46 @@
+<?
+
+
+class MetaDb extends SQLite3{
+  private $dbLocation;
+  
+  public function __construct($location){
+  	$this->dbLocation = $location;
+  }
+  
+   public function query($q){
+        global $conf;
+        try{
+	        $db = new PDO('sqlite:'.$this->dbLocation);
+	        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
+
+	        $results = array();
+	        foreach($db->query($q) as $entry) {
+	        	array_push($results, $entry);
+	        }
+			$db = NULL;
+		}catch(PDOException $e){
+				print 'Exception query : '.$e->getMessage()."\n".$this->dbLocation."\n";
+				exit(10);
+		}
+        return $results;
+  }
+  
+   public function write($q){
+        global $conf;
+        try{
+	    	$db = new PDO('sqlite:'.$this->dbLocation);
+	    	$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
+
+	        $results = $db->exec($q);
+			$db = NULL;
+		}catch(PDOException $e){
+				print 'Exception exec: '.$e->getMessage()."\n\n";
+				exit(10);
+		}
+        return $results;
+  }
+  
+}
+
+?>
diff --git a/classes/Queries.php b/classes/Queries.php
index e4f26253..94de8418 100644
--- a/classes/Queries.php
+++ b/classes/Queries.php
@@ -28,70 +28,51 @@ class Queries{
   	return NULL;
   }
   
-  public static function getMetaData($uri, $format, $e){
-  	global $conf;
-  	$named_graph = $conf['metaendpoint']['config']['named_graph'];
-  	$q = <<<QUERY
-  	PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
-  	PREFIX dcterms: <http://purl.org/dc/terms/>
-  	SELECT ?page ?uri ?format WHERE{
-  	GRAPH <$named_graph>{
-  	?s ?p ?o . #Stupid dummy triple to make it work with ARC2
-  	OPTIONAL{
-  	  ?page foaf:primaryTopic <$uri> ;
-  	        dcterms:format "$format" .
-  	  } .
-  	OPTIONAL{
-  	  <$uri> foaf:primaryTopic ?uri;
-  	         dcterms:format ?format
-  	 } .
-  	}
-  	}LIMIT 1
+	public static function getMetadata($uri, $format, $e){
+		global $conf;
+		$q = <<<QUERY
+		SELECT uri, doc, format FROM document WHERE 
+			(uri = "$uri" AND format = "$format") OR doc = "$uri"
+		LIMIT 1
 QUERY;
-$r = $e->query($q);
-if(sizeof($r['results']['bindings'])>0){
-  $u = (isset($r['results']['bindings'][0]['uri']))?$r['results']['bindings'][0]['uri']['value']:NULL;
-  $p = (isset($r['results']['bindings'][0]['page']))?$r['results']['bindings'][0]['page']['value']:NULL;
-  $f = (isset($r['results']['bindings'][0]['format']))?$r['results']['bindings'][0]['format']['value']:NULL;
-  return array($u, $p, $f);
-}
-return NULL;
-  }
+	   $r = $e->query($q);
+		if(sizeof($r) > 0){
+		 $u = $r[0]['uri'];
+		 $p = $r[0]['doc'];
+		 $f = $r[0]['format'];
+		  return array($u, $p, $f);
+		}else{
+		  return NULL;
+		}
+	}
   
-    
-  public static function createPage($uri, $contentType, $e){
-        global $conf;
-        $ext = 'html';
-        $named_graph = $conf['metaendpoint']['config']['named_graph'];
-        //TODO: More flexible page creation method
-        $inserts = "";
-        foreach($conf['http_accept'] as $extension => $f){
-          $page = $uri.".".$extension;
-            foreach($f as $v){
-              if($contentType == $v){
-                $returnPage = $uri.".".$extension;
-              }
-              $inserts .= "<$page> foaf:primaryTopic <$uri>;
-                  dcterms:format '".$v."'.";
-              if($v == $contentType){
-                $ext = $extension;
-              }
-            }
-    	  }
 
-        $q = <<<QUERY
-        PREFIX foaf: <http://xmlns.com/foaf/0.1/>
-        PREFIX dcterms: <http://purl.org/dc/terms/>
-        INSERT INTO <$named_graph> {
-          $inserts
-        }
+	public static function createPage($uri, $contentType, $e){
+	 global $conf;
+		$ext = 'html';
+		$inserts = "";
+		foreach($conf['http_accept'] as $extension => $f){
+		  $page = $uri.".".$extension;
+			foreach($f as $v){
+			  if($contentType == $v){
+				$returnPage = $uri.".".$extension;
+			  }
+			  if($inserts != ""){
+				$inserts .= "UNION ";
+			  }
+			  $inserts .= "SELECT '$uri', '$page', '$v' \n";
+			  if($v == $contentType){
+				$ext = $extension;
+			  }
+			}
+		  }
+		  $q = <<<QUERY
+		  INSERT INTO document (uri, doc, format) $inserts
 QUERY;
-    $r = $e->queryPost($q);
-
-    if($r == null){
-      return null;
-    }
-    return $returnPage;
-  }
+	$r = $e->write($q);
+	
+		return $returnPage;
+	}
 }
-  	?>
+
+?>
diff --git a/common.inc.php b/common.inc.php
index aef553de..4777e4c1 100644
--- a/common.inc.php
+++ b/common.inc.php
@@ -7,11 +7,7 @@ $conf['endpoint']['config']['output'] = $conf['endpoint']['select']['output'];
 $conf['endpoint']['config']['named_graph'] = '';
 $conf['endpoint']['config']['show_inline'] = 0;
 
-$conf['metaendpoint']['select']['output'] = 'json';
-$conf['metaendpoint']['describe']['output'] = 'rdf';
-$conf['metaendpoint']['config']['output'] = $conf['metaendpoint']['select']['output'];
-$conf['metaendpoint']['config']['show_inline'] = 0;
-$conf['metaendpoint']['config']['named_graph'] = 'http://lodspeakr.org/metadata';
+$conf['metadata']['db']['location'] = 'meta/db.sqlite';
 
 $conf['ns']['rdf']     = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
 $conf['ns']['rdfs']    = 'http://www.w3.org/2000/01/rdf-schema#';
diff --git a/endpoint.php b/endpoint.php
deleted file mode 100644
index a2d33b7e..00000000
--- a/endpoint.php
+++ /dev/null
@@ -1,41 +0,0 @@
-<?php
-/* ARC2 static class inclusion */ 
-include_once('lib/arc2/ARC2.php');
-include_once('settings.inc.php');
-set_time_limit(0);
-
-/* MySQL and endpoint configuration */ 
-$config = array(
-  /* db */
-  'db_host' => $conf['metaendpoint']['db']['host'],
-  'db_name' => $conf['metaendpoint']['db']['dbname'],
-  'db_user' => $conf['metaendpoint']['db']['user'], 
-  'db_pwd' =>  $conf['metaendpoint']['db']['pass'],
-
-  /* store name */
-  'store_name' => 'my_endpoint_store',
-
-  /* endpoint */
-  'endpoint_features' => array(
-    'select', 'construct', 'ask', 'describe', 
-     'insert','load', 'delete', 
-    'dump' /* dump is a special command for streaming SPOG export */
-  ),
-  'endpoint_timeout' => 60, /* not implemented in ARC2 preview */
-  'endpoint_read_key' => '', 
-  'endpoint_write_key' => $conf['metaendpoint']['config']['key'], 
-  'endpoint_max_limit' => 0 
-);
-
-/* instantiation */
-$ep = ARC2::getStoreEndpoint($config);
-
-if (!$ep->isSetUp()) {
-  $ep->setUp(); /* create MySQL tables */
-}
-
-/* request handling */
-$ep->go();
-
-?>
-
diff --git a/index.php b/index.php
index f56172d2..b700be51 100755
--- a/index.php
+++ b/index.php
@@ -8,11 +8,11 @@ include_once('common.inc.php');
 include_once('classes/Utils.php');
 include_once('classes/Queries.php');
 include_once('classes/Endpoint.php');
+include_once('classes/MetaDb.php');
 
 
 $endpoint = new Endpoint($conf['endpoint']['host'], $conf['endpoint']['config']);
-$metaEndpoint = new Endpoint($conf['metaendpoint']['host'], $conf['metaendpoint']['config']);
-
+$metaDb = new MetaDb($conf['metadata']['db']['location']);
 
 $uri =  $conf['basedir'].$_GET['q'];
 if($uri == $conf['basedir']){
@@ -26,29 +26,29 @@ $extension = Utils::getExtension($acceptContentType);
 if($acceptContentType == NULL){
   Utils::send406($uri);
 }
-$pair = Queries::getMetadata($uri, $acceptContentType, $metaEndpoint);
-if($pair == NULL){ // Original URI is not in metadata
-  if(Queries::uriExist($uri, $endpoint)){
-  	$page = Queries::createPage($uri, $acceptContentType, $metaEndpoint);
-  	if($page == null){
-  	  Utils::send500(null);
-  	}
-  	Utils::send303($page, $acceptContentType);
-  }else{
-  	Utils::send404($uri);
-  }
-}
-list($res, $page, $format) = $pair;
+$pair = Queries::getMetadata($uri, $acceptContentType, $metaDb);
 
-if($page != NULL && $res == NULL){ 
-  Utils::send303($page, $acceptContentType);
-}
+	if($pair == NULL){ // Original URI is not in metadata
+		if(Queries::uriExist($uri, $endpoint)){
+			$page = Queries::createPage($uri, $acceptContentType, $metaDb);
+			if($page == NULL){
+			  Utils::send500(NULL);
+			}
+			Utils::send303($page, $acceptContentType);
+		}else{
+			Utils::send404($uri);
+		}
+	}
+  list($res, $page, $format) = $pair;
+
+	//If resource is not the page, send a 303 to the document
+  if($res == $uri){
+    Utils::send303($page, $acceptContentType);
+  }
 
-if($res != NULL && $page == NULL){ // Original URI is a page
   $uri = $res;
   $curieType = Utils::uri2curie(Queries::getClass($uri, $endpoint));
   
-    
   /*Redefine Content type based on the
    * dcterms:format for this page
    */
@@ -62,9 +62,9 @@ if($res != NULL && $page == NULL){ // Original URI is a page
   	$modelFile = $conf['model']['directory'].$conf['model']['default'].$conf['model']['extension'].".".$extension;
   	$viewFile = $conf['view']['directory'].$conf['view']['default'].$conf['view']['extension'].".".$extension;
   }
+  
   $query = file_get_contents($modelFile);
   $query = preg_replace("|".$conf['resource']['url_delimiter']."|", "<".$uri.">", $query);
-  
   header('Content-Type: '.$acceptContentType);
   if(preg_match("/describe/i", $query)){
   	$results = $endpoint->query($query, $conf['endpoint']['describe']['output']);
@@ -97,6 +97,6 @@ if($res != NULL && $page == NULL){ // Original URI is a page
   Utils::showView($uri, $results, $viewFile);
   
   exit(0);
-}
+//}
 
 ?>
diff --git a/install.sh b/install.sh
index f5317180..03abcb5a 100755
--- a/install.sh
+++ b/install.sh
@@ -4,13 +4,6 @@ root_htaccess="root.htaccess"
 parent_htaccess="../.htaccess"
 settings_file="settings.inc.php"
 
-mysql_port="3306"
-mysql_host="localhost"
-mysql_dbname="lodspeakr"
-mysql_user="root"
-mysql_pass=""
-sparql_key=$RANDOM
-
 basedir="http://localhost/my/data/"
 lodspeakrdir="lodspeakr"
 ns=$basedir
@@ -56,70 +49,12 @@ do
   	endpoint=$aux_endpoint
   fi
 
-  echo "==Internal SPARQL endpoint=="
-  echo "lodspeakr read -u 1s from your SPARQL endpoint, however it needs to add metadata to its own SPARQL endpoint"
-  echo ""
-  echo "Ok, to create lodspeakr' endpoint I need a MySQL database"
-  echo -n "What is host where the database server is located (default $mysql_host)? "
-  read -u 1 aux_mysql_host
-  echo ""
-  if [ "$aux_mysql_host" != "" ] 
-  then
-  	mysql_host=$aux_mysql_host
-  fi
-
-  echo -n "What is port the database is listening to (default $mysql_port)? "
-  read -u 1 aux_mysql_port  
-  echo ""
-  if [ "$aux_mysql_port" != "" ] 
-  then
-  	mysql_port=$aux_mysql_port
-  fi
-  	
-  echo -n "What is name of the database (default $mysql_dbname)? "
-  read -u 1 aux_mysql_dbname
-  echo ""
-  if [ "$aux_mysql_dbname" != "" ] 
-  then
-  	mysql_dbname=$aux_mysql_dbname
-  fi
-  
-  echo -n "What is user for this database (default $mysql_user)? "
-  read -u 1 mysql_user
-  echo ""
-  if [ "$aux_mysql_user" != "" ] 
-  then
-  	mysql_user=$aux_mysql_user
-  fi
-  
-  echo -n "What is password for $mysql_user? "
-  read -u 1 mysql_pass
-  echo ""
-  if [ "$aux_mysql_pass" != "" ] 
-  then
-  	mysql_pass=$aux_mysql_pass
-  fi
-
-  echo -n "Create a key for lodspeakr' SPARQL endpoint (default: $sparql_key): "
-  read -u 1 aux_sparql_key
-  echo ""
-  if [ "$aux_sparql_key" != "" ] 
-  then
-  	sparql_key=$aux_sparql_key
-  fi
   echo "==Configuration=="
   echo "Ok, so I have the following configuration:"
   echo "Base URL is $basedir"
   echo "lodspeakr is installed at $basedir$lodspeakrdir"
   echo "The local namespace is $ns"
   echo "Your SPARQL endpoint is located at $endpoint"
-  echo "For lodspeakr internal sparql endpoint the configuration is as follows:"
-  echo "Host: $mysql_host"
-  echo "Database name: $mysql_dbname"
-  echo "Port: $mysql_port"
-  echo "User: $mysql_user"
-  echo "Pass: $mysql_pass"
-  echo "Key for the SPARQL key: $sparql_key"
 
   
   echo -n "Is everything ok (y/n)?"
@@ -141,16 +76,6 @@ done
 \$conf['endpoint']['host'] = '$endpoint';
 \$conf['basedir'] = '$basedir';
 
-\$conf['metaendpoint']['host'] = '$basedir$lodspeakrdir/endpoint.php';
-\$conf['metaendpoint']['config']['key'] = '$sparql_key';
-\$conf['metaendpoint']['config']['named_graph'] = 'http://lodspeakr.org/metadata';
-\$conf['metaendpoint']['db']['host'] = '$mysql_host';
-\$conf['metaendpoint']['db']['port'] = '$mysql_port';
-\$conf['metaendpoint']['db']['dbname'] = '$mysql_dbname';
-\$conf['metaendpoint']['db']['user'] = '$mysql_user';
-\$conf['metaendpoint']['db']['pass'] = '$mysql_pass';
-\$conf['metaendpoint']['config']['key'] = '$sparql_key';
-
 \$conf['ns']['local']   = '$ns';
 
 ?>" 
diff --git a/meta/db.sqlite b/meta/db.sqlite
index 5f1877165eea797b174fc4d6969c5494d2bd6027..b8671961c6dd59aef4cb2d4b0d59d5a041a184ec 100644
GIT binary patch
delta 343
zcmd7MyAgsw6oui{T|rRfp@IquA~TQ@*^ZsK3AuDY3l0s@0WI)j6$am#GdH<Md%9bG
zVy6~D6i~?9>ba=Dug>f<HON+%?>pxQRxn^<4GT6l*uudME<E@Mu*U&Mgotp$85hJz
ZaK#NNGR%LOWe7>hdtZ(6x9Pv5krSi@MnnJr

delta 30
gcmZozz|<fxL7JD5fq{Vuh{0f@hB+hS#)Jjz08gU@7ytkO

-- 
GitLab