From 294a46ae5e4b155eedf45dc0c56572866ec0c10b Mon Sep 17 00:00:00 2001
From: alvaro <alvaro@alia.(none)>
Date: Thu, 19 Apr 2012 15:28:06 -0700
Subject: [PATCH] LODSPeaKr adds prefixes to sparql queries automatically

Solves #106
---
 README            |  2 +-
 classes/Utils.php | 26 ++++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/README b/README
index 839586ed..6c4706e7 100644
--- a/README
+++ b/README
@@ -1,6 +1,6 @@
 lodspeakr - Linked Open Data Simple Publishing Kit
 author: Alvaro Graves (alvaro@graves.cl)
-version: 20120411 
+version: 20120419 
 
 Simplest Installation
 ---------------------
diff --git a/classes/Utils.php b/classes/Utils.php
index 29028d5f..e3821c55 100644
--- a/classes/Utils.php
+++ b/classes/Utils.php
@@ -421,6 +421,7 @@ class Utils{
 	  	  HTTPStatus::send500("invalid query: " . $parser->getErrors());
 	  	}
 	  }
+  	  $query = Utils::addPrefixes($query);
   	  if($conf['debug']){
   	  	echo "\n-------------------------------------------------\nIn ".getcwd()."\n";
   	    echo "$modelFile (against ".$e->getSparqlUrl().")\n-------------------------------------------------\n\n";
@@ -544,7 +545,15 @@ class Utils{
 	if(is_string($data)){
 	  echo($data);
 	}elseif(is_file($conf['home'].$view)){
+         try{
 	  Haanga::Load($viewFile, $vars);
+          }catch(Exception $e){
+echo '<pre>';
+           echo $e->getMessage();
+var_dump($vars);
+echo($e->getMessage()."' in ".$e->getFile().":".$e->getLine()."\nStack trace:\n".$e->getTraceAsString());
+echo '</pre>';
+         }
 	}elseif($view == null){
 	  $fnc = Haanga::compile('{{models|safe}}');
 	  $fnc($vars, TRUE);
@@ -575,6 +584,23 @@ class Utils{
   	return $triples;
   }
   
+  private static function addPrefixes($q){
+    global $conf;
+    $matches = array();
+    $visited = array();
+    $newQuery = $q;
+    if(preg_match_all("|\s(\w+):\w+|", $q, $matches) > 0){
+      foreach($matches[1] as $v){
+        if(!isset($visited[$v]) && isset($conf['ns'][$v])){
+          $newQuery = "PREFIX ".$v.": <".$conf['ns'][$v].">\n".$newQuery;
+          $visited[$v] = true;
+        }
+      }
+    }
+    
+    return $newQuery;
+  }
+  
 }
 
 ?>
-- 
GitLab