uri = $uri; $this->credentials = $credentials; } /** * Obtain a bounded description of a given resource * @param mixed uri the URI of the resource to be described or an array of URIs * @return HttpResponse */ function describe( $uri ) { if ( is_array( $uri ) ) { $query="DESCRIBE <" . implode('> <' , $uri) . ">"; } else { $query="DESCRIBE <$uri>"; } return $this->graph($query); } /** * @deprecated triple lists are deprecated */ function describe_to_triple_list( $uri ) { $triples = array(); $response = $this->describe( $uri ); $parser_args=array( "bnode_prefix"=>"genid", "base"=> $this->uri ); $parser = ARC2::getRDFXMLParser($parser_args); if ( $response->body ) { $parser->parse($this->uri, $response->body ); $triples = $parser->getTriples(); } return $triples; } /** * Obtain a bounded description of a given resource as a SimpleGraph. An empty SimpleGraph is returned if any HTTP errors occur. * @param mixed uri the URI of the resource to be described or an array of URIs * @return SimpleGraph */ function describe_to_simple_graph( $uri ) { $graph = new SimpleGraph(); $response = $this->describe( $uri ); if ( $response->is_success() ) { $graph->from_rdfxml( $response->body ); } return $graph; } /** * Execute an arbitrary query on the sparql service * @param string query the query to execute * @param string mime the media type of the expected response (optional, defaults to RDF/XML and SPARQL results XML) * @return HttpResponse */ function query($query, $mime=''){ if (! isset( $this->request_factory) ) { $this->request_factory = new HttpRequestFactory(); } $request = $this->request_factory->make( 'POST', $this->uri, $this->credentials ); if(empty($mime)) $mime = MIME_RDFXML.','.MIME_SPARQLRESULTS; $request->set_accept($mime); $request->set_content_type(MIME_FORMENCODED); $request->set_body( "query=" . urlencode($query) ); return $request->execute(); } /** * Execute a graph type sparql query, i.e. a describe or a construct * @param string query the describe or construct query to execute * @return HttpResponse */ function graph( $query ) { if (! isset( $this->request_factory) ) { $this->request_factory = new HttpRequestFactory(); } $request = $this->request_factory->make( 'POST', $this->uri, $this->credentials ); $request->set_accept(MIME_RDFXML); $request->set_content_type(MIME_FORMENCODED); $request->set_body( "query=" . urlencode($query) ); return $request->execute(); } /** * @deprecated triple lists are deprecated */ function graph_to_triple_list($query ) { $triples = array(); $response = $this->graph( $query ); $parser_args=array( "bnode_prefix"=>"genid", "base"=> $this->uri ); $parser = ARC2::getRDFXMLParser($parser_args); if ( $response->body ) { $parser->parse($this->uri, $response->body ); $triples = $parser->getTriples(); } return $triples; } /** * @deprecated triple lists are deprecated */ function construct_to_triple_list($query ) { return $this->graph_to_triple_list($query ); } /** * Execute a graph type sparql query and obtain the result as a SimpleGraph. An empty SimpleGraph is returned if any HTTP errors occur. * @param string query the describe or construct query to execute * @return SimpleGraph */ function graph_to_simple_graph( $query ) { $graph = new SimpleGraph(); $response = $this->graph( $query ); if ( $response->is_success() ) { $graph->from_rdfxml( $response->body ); } return $graph; } /** * @deprecated use graph_to_simple_graph */ function construct_to_simple_graph( $query ) { return $this->graph_to_simple_graph($query); } /** * Execute a select sparql query * @param string query the select query to execute * @return HttpResponse */ function select( $query ) { if (! isset( $this->request_factory) ) { $this->request_factory = new HttpRequestFactory(); } $request = $this->request_factory->make( 'POST', $this->uri, $this->credentials ); $request->set_accept(MIME_SPARQLRESULTS); $request->set_content_type(MIME_FORMENCODED); $request->set_body( "query=" . urlencode($query) ); return $request->execute(); } /** * Execute a select sparql query and return the results as an array. An empty array is returned if any HTTP errors occur. * @param string query the select query to execute * @return array parsed results in format returned by parse_select_results method */ function select_to_array( $query ) { $results = array(); $response = $this->select( $query ); if ( $response->is_success() ) { $results = $this->parse_select_results( $response->body ); } return $results; } /** * Parse the SPARQL XML results format into an array. The array consist of one element per result. * Each element is an associative array where the keys correspond to the variable name and the values are * another associative array with the following keys: *