Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

SPARQL: add ARC2 parsing

  • Loading branch information...
commit fd8f258f467b1250819332796531228cba2542db 1 parent 5398a9e
@presbrey presbrey authored
Showing with 49 additions and 0 deletions.
  1. +2 −0  www/wildcard/POST.php
  2. +47 −0 www/wildcard/SPARQL.php
View
2  www/wildcard/POST.php
@@ -41,6 +41,8 @@
} elseif (!empty($_input) && ($g->append($_input, $_data) || 1)) {
librdf_php_last_log_level() && httpStatusExit(400, 'Bad Request', null, librdf_php_last_log_message());
$g->save();
+} elseif ($_content_type == 'application/sparql-query') {
+ require_once('SPARQL.php');
} else {
httpStatusExit(406, 'Content-Type Not Acceptable');
}
View
47 www/wildcard/SPARQL.php
@@ -0,0 +1,47 @@
+<?php
+
+function queryError($msg) {
+ header('HTTP/1.1 400 Bad Request');
+ echo "$msg\n";
+ exit;
+}
+
+require_once('arc2/ARC2.php');
+
+$query = $_data;
+$parser = ARC2::getSPARQLPlusParser();
+$parser->parse($query);
+if (isset($parser->errors) && count($parser->errors))
+ queryError(implode("\n",$parser->errors));
+
+$info = $parser->getQueryInfos();
+$query = $info['query'];
+
+$assure_strings = array('type', 'target_graph');
+$assure_arrays = array('dataset', 'target_graphs', 'construct_triples');
+
+foreach ($assure_strings as $k=>$v)
+ if (!isset($query[$v]))
+ $query[$v] = '';
+foreach ($assure_arrays as $k=>$v)
+ if (!isset($query[$v]))
+ $query[$v] = array();
+foreach ($query as $k=>$v)
+ if (!in_array($k, $assure_arrays) && !in_array($k, $assure_strings))
+ queryError('unsupported query feature: '.$k);
+
+if (!in_array($query['type'], array('insert')))
+ queryError('valid query types: insert');
+
+if ($query['target_graph'] != $_base)
+ queryError('query must target request URI graph (only)');
+if (count($query['target_graphs']) && $query['target_graphs'][0] != $_base)
+ queryError('query must target request URI graph (only)');
+
+foreach ($query['construct_triples'] as $elt)
+ foreach (array('s', 'p', 'o') as $k)
+ if (!in_array($elt["{$k}_type"], array('uri', 'literal')))
+ queryError('unsupported node type: '.$elt[$k].' ('.$elt["{$k}_type"].')');
+
+header('HTTP/1.1 202 Accepted');
+print_r($query);
Please sign in to comment.
Something went wrong with that request. Please try again.