diff --git a/app/Tdt/Core/DataControllers/SPARQLController.php b/app/Tdt/Core/DataControllers/SPARQLController.php index 986db8ab..9d935a13 100644 --- a/app/Tdt/Core/DataControllers/SPARQLController.php +++ b/app/Tdt/Core/DataControllers/SPARQLController.php @@ -142,33 +142,17 @@ public function readData($source_definition, $rest_parameters = array()) $q = urlencode($query); $q = str_replace("+", "%20", $q); - if ($keyword == 'select') { - - $query_uri = $endpoint . '?query=' . $q . '&format=' . urlencode("application/sparql-results+json"); - - $response = $this->executeUri($query_uri, $endpoint_user, $endpoint_password); - $result = json_decode($response); - - if (!$result) { - \App::abort(500, 'The query has been executed, but the endpoint failed to return sparql results in JSON.'); - } + $query_uri = $endpoint . '?query=' . $q . '&format=' . urlencode("text/rdf+n3"); - $is_semantic = false; - - } else { - - $query_uri = $endpoint . '?query=' . $q . '&format=' . urlencode("application/rdf+xml"); - - $response = $this->executeUri($query_uri, $endpoint_user, $endpoint_password); + $response = $this->executeUri($query_uri, $endpoint_user, $endpoint_password); // Parse the triple response and retrieve the triples from them - $result = new \EasyRdf_Graph(); - $parser = new \EasyRdf_Parser_RdfXml(); + $result = new \EasyRdf_Graph(); + $parser = new \EasyRdf_Parser_Turtle(); - $parser->parse($result, $response, 'rdfxml', null); + $parser->parse($result, $response, 'turtle', null); - $is_semantic = true; - } + $is_semantic = true; // Create the data object to return $data = new Data(); diff --git a/app/Tdt/Core/Formatters/JSONFormatter.php b/app/Tdt/Core/Formatters/JSONFormatter.php index 8615f6f7..b35a3ae4 100644 --- a/app/Tdt/Core/Formatters/JSONFormatter.php +++ b/app/Tdt/Core/Formatters/JSONFormatter.php @@ -27,21 +27,18 @@ public static function createResponse($dataObj) public static function getBody($dataObj) { - // Build the body - $body = $dataObj->data; - if (is_object($dataObj->data)) { - $body = get_object_vars($dataObj->data); - } - + // If the data is semantic return the data in a json-ld format if ($dataObj->is_semantic) { - // Check if a configuration is given - $conf = array(); - if (!empty($dataObj->semantic->conf)) { - $conf = $dataObj->semantic->conf; - } + $jsonld_formatter = new JSONLDFormatter(); - return $dataObj->data->serialise('json'); + return $jsonld_formatter->getBody($dataObj); + } + + // If not semantic, build the json body + $body = $dataObj->data; + if (is_object($dataObj->data)) { + $body = get_object_vars($dataObj->data); } // Unescape slashes