From 96d3e421d21b15ec978a86da51a4611d17441e9b Mon Sep 17 00:00:00 2001
From: Alvaro Graves <alvaro@graves.cl>
Date: Thu, 26 Jan 2012 16:44:21 -0800
Subject: [PATCH] Adding named graph when searching if URI exists (some
 implementations will make a difference)

Using ASK instead of select
using [] instead of variables in query
---
 classes/Endpoint.php |  4 ++++
 classes/Queries.php  | 29 +++++++++++++++++++----------
 classes/Utils.php    |  2 +-
 3 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/classes/Endpoint.php b/classes/Endpoint.php
index aa2cff11..16a7fbe2 100644
--- a/classes/Endpoint.php
+++ b/classes/Endpoint.php
@@ -45,6 +45,10 @@ class Endpoint{
         if(preg_match("/construct/i", $q)){
           return $aux;
         }
+        if(preg_match("/ask/i", $q)){
+          $r = json_decode($aux, true);
+          return $r;
+        }
   }
   
   public function queryPost($q){
diff --git a/classes/Queries.php b/classes/Queries.php
index 3e2451ef..9e650c72 100644
--- a/classes/Queries.php
+++ b/classes/Queries.php
@@ -2,16 +2,25 @@
 
 class Queries{
   public static function uriExist($uri, $e){
-  	$q = "SELECT * WHERE{
-  	{<$uri> ?p1 ?o1}
-  	UNION
-  	{?s1 <$uri> ?o2}
-  	UNION
-  	{?s2 ?p2 <$uri>}
-  	}LIMIT 1";
-  	
-  	$r = $e->query($q);
-  	if(sizeof($r['results']['bindings'])>0){
+  	$q = "ASK WHERE{
+  	{
+  	GRAPH ?g{
+    	{<$uri> ?p1 []}
+    	UNION
+    	{[] <$uri> []}
+    	UNION
+    	{[] ?p2 <$uri>}
+    	}
+    	}UNION{
+    	    	{<$uri> ?p1 []}
+    	UNION
+    	{[] <$uri> []}
+    	UNION
+    	{[] ?p2 <$uri>}
+    	}
+    }";
+  	$r = $e->query($q); 
+  	if($r['boolean'] && strtolower($r['boolean']) !== false){
   	  return true;
   	}
   	return false;
diff --git a/classes/Utils.php b/classes/Utils.php
index 8175d512..871998d8 100644
--- a/classes/Utils.php
+++ b/classes/Utils.php
@@ -12,7 +12,7 @@ class Utils{
   
   public static function send404($uri){
   	header("HTTP/1.0 404 Not Found");
-  	echo "LODSPeaKr could not find ".$uri." or information about it.\n\n";
+  	echo "LODSPeaKr could not find ".$uri." or information about it.\nNo URIs in the triple store, or services configured with that URI\n";
   	exit(0);
   }
   
-- 
GitLab