public function sparqlQuery($query, $options = array()) { $resultform = isset($options[Erfurt_Store::RESULTFORMAT]) ? $options[Erfurt_Store::RESULTFORMAT] : Erfurt_Store::RESULTFORMAT_PLAIN; // Support for FOAF+SSL only when user is authenticated via WebID. $identity = Erfurt_App::getInstance()->getAuth()->getIdentity(); if (!$identity->isWebId()) { return parent::sparqlQuery($query, $resultform); } $url = $this->_serviceUrl . '?query=' . urlencode((string) $query); $accept = 'application/sparql-results+xml'; $response = $this->_handleRequest($url, $accept); $result = $this->_parseSparqlXmlResults($response); switch ($resultform) { case 'plain': $newResult = array(); foreach ($result['results']['bindings'] as $row) { $newRow = array(); foreach ($row as $var => $value) { // TODO datatype and lang support $newRow[$var] = $value['value']; } $newResult[] = $newRow; } return $newResult; case 'extended': return $result; case 'json': return json_encode($result); break; default: throw new Exception('Result form not supported yet.'); } }
/** * Test if the Erfurt_Store_Adapter_Sparql resolves the graph URI to the correct service. */ public function testSparqlWithDbPediaEndpoint() { $options = array('serviceUrl' => 'http://dbpedia.org/sparql', 'graphs' => array('http://dbpedia.org')); $adapter = new Erfurt_Store_Adapter_Sparql($options); // Use HTTP Client test adapter $httpAdapter = new Erfurt_TestHelper_Http_ClientAdapter(); $httpAdapter->setResponse(new Zend_Http_Response(200, array('Content-type' => 'application/sparql-results+xml'), file_get_contents($this->_dataDir . 'sparqlDBpediaLeipzig.srx'))); $adapter->setHttpAdapter($httpAdapter); $sparql = 'SELECT ?p ?o FROM <http://dbpedia.org> WHERE {<http://dbpedia.org/resource/Leipzig> ?p ?o} LIMIT 10'; $result = $adapter->sparqlQuery($sparql); $serviceUrl = Zend_Uri_Http::fromString('http://dbpedia.org/sparql'); $requestUrl = $httpAdapter->getLastRequestUri(); $this->assertTrue(is_array($result)); $this->assertEquals($serviceUrl->getHost(), $requestUrl->getHost()); $this->assertEquals($serviceUrl->getPath(), $requestUrl->getPath()); $this->assertEquals(10, count($result)); }