/** * Load RDF data into the graph from a URI. * * If no URI is given, then the URI of the graph will be used. * * The document type is optional but should be specified if it * can't be guessed or got from the HTTP headers. * * @param string $uri The URI of the data to load * @param string $format Optional format of the data (eg. rdfxml) * @return integer The number of triples added to the graph */ public function load($uri = null, $format = null) { $this->checkResourceParam($uri, true); if (!$uri) { throw new EasyRdf_Exception("No URI given to load() and the graph does not have a URI."); } // Setup the HTTP client $client = EasyRdf_Http::getDefaultHttpClient(); $client->resetParameters(true); $client->setConfig(array('maxredirects' => 0)); $client->setMethod('GET'); $client->setHeaders('Accept', EasyRdf_Format::getHttpAcceptHeader()); $requestUrl = $uri; $response = null; $redirectCounter = 0; do { // Have we already loaded it into the graph? $requestUrl = EasyRdf_Utils::removeFragmentFromUri($requestUrl); if (in_array($requestUrl, $this->loaded)) { return 0; } // Make the HTTP request $client->setHeaders('host', null); $client->setUri($requestUrl); $response = $client->request(); // Add the URL to the list of URLs loaded $this->loaded[] = $requestUrl; if ($response->isRedirect() and $location = $response->getHeader('location')) { // Avoid problems with buggy servers that add whitespace $location = trim($location); // Some servers return relative URLs in the location header // resolve it in relation to previous request $baseUri = new EasyRdf_ParsedUri($requestUrl); $requestUrl = $baseUri->resolve($location)->toString(); $requestUrl = EasyRdf_Utils::removeFragmentFromUri($requestUrl); // If it is a 303 then drop the parameters if ($response->getStatus() == 303) { $client->resetParameters(); } ++$redirectCounter; } elseif ($response->isSuccessful()) { // If we didn't get any location, stop redirecting break; } else { throw new EasyRdf_Http_Exception("HTTP request for {$requestUrl} failed: " . $response->getMessage(), $response->getStatus(), null, $response->getBody()); } } while ($redirectCounter < $this->maxRedirects); if (!$format or $format == 'guess') { list($format, $params) = EasyRdf_Utils::parseMimeType($response->getHeader('Content-Type')); } // Parse the data return $this->parse($response->getBody(), $format, $uri); }
public function testParseMimeTypeBasicWithCharsetAndWhitespace() { list($type, $params) = EasyRdf_Utils::parseMimeType(' text/plain ; charset = utf8 '); $this->assertSame('text/plain', $type); $this->assertSame('utf8', $params['charset']); }
/** * Load RDF data into the graph. * * If a URI is supplied, but no data then the data will * be fetched from the URI. * * The document type is optional and can be specified if it * can't be guessed or got from the HTTP headers. * * @param string $uri The URI of the data to load * @param string $data Optional data for the graph * @param string $format Optional format of the data */ public function load($uri = null, $data = null, $format = null) { $this->checkResourceParam($uri, true); if (!$uri) { throw new EasyRdf_Exception("No URI given to load() and the graph does not have a URI."); } if (!$data) { # No data was given - try and fetch data from URI # FIXME: prevent loading the same URI multiple times $client = EasyRdf_Http::getDefaultHttpClient(); $client->resetParameters(true); $client->setUri($uri); $client->setMethod('GET'); $client->setHeaders('Accept', EasyRdf_Format::getHttpAcceptHeader()); $response = $client->request(); if (!$response->isSuccessful()) { throw new EasyRdf_Exception("HTTP request for {$uri} failed: " . $response->getMessage()); } $data = $response->getBody(); if (!$format) { list($format, $params) = EasyRdf_Utils::parseMimeType($response->getHeader('Content-Type')); } } // Parse the data return $this->parse($data, $format, $uri); }
protected function request($type, $query) { // Check for undefined prefixes $prefixes = ''; foreach (EasyRdf_Namespace::namespaces() as $prefix => $uri) { if (strpos($query, "{$prefix}:") !== false and strpos($query, "PREFIX {$prefix}:") === false) { $prefixes .= "PREFIX {$prefix}: <{$uri}>\n"; } } $client = EasyRdf_Http::getDefaultHttpClient(); $client->resetParameters(); // Tell the server which response formats we can parse $accept = EasyRdf_Format::getHttpAcceptHeader(array('application/sparql-results+json' => 1.0, 'application/sparql-results+xml' => 0.8)); $client->setHeaders('Accept', $accept); if ($type == 'update') { $client->setMethod('POST'); $client->setUri($this->updateUri); $client->setRawData($prefixes . $query); $client->setHeaders('Content-Type', 'application/sparql-update'); } elseif ($type == 'query') { // Use GET if the query is less than 2kB // 2046 = 2kB minus 1 for '?' and 1 for NULL-terminated string on server $encodedQuery = 'query=' . urlencode($prefixes . $query); if (strlen($encodedQuery) + strlen($this->queryUri) <= 2046) { $delimiter = $this->queryUri_has_params ? '&' : '?'; $client->setMethod('GET'); $client->setUri($this->queryUri . $delimiter . $encodedQuery); } else { // Fall back to POST instead (which is un-cacheable) $client->setMethod('POST'); $client->setUri($this->queryUri); $client->setRawData($encodedQuery); $client->setHeaders('Content-Type', 'application/x-www-form-urlencoded'); } } $response = $client->request(); if ($response->getStatus() == 204) { // No content return $response; } elseif ($response->isSuccessful()) { list($type, $params) = EasyRdf_Utils::parseMimeType($response->getHeader('Content-Type')); if (strpos($type, 'application/sparql-results') === 0) { return new EasyRdf_Sparql_Result($response->getBody(), $type); } else { return new EasyRdf_Graph($this->queryUri, $response->getBody(), $type); } } else { throw new EasyRdf_Exception("HTTP request for SPARQL query failed: " . $response->getBody()); } }
/** Make a query to the SPARQL endpoint * * SELECT and ASK queries will return an object of type * EasyRdf_Sparql_Result. * * CONSTRUCT and DESCRIBE queries will return an object * of type EasyRdf_Graph. * * @param string $query The query string to be executed * @return object EasyRdf_Sparql_Result|EasyRdf_Graph Result of the query. */ public function query($query) { # Add namespaces to the queryString $prefixes = ''; foreach (EasyRdf_Namespace::namespaces() as $prefix => $uri) { if (strpos($query, "{$prefix}:") !== false and strpos($query, "PREFIX {$prefix}:") === false) { $prefixes .= "PREFIX {$prefix}: <{$uri}>\n"; } } $client = EasyRdf_Http::getDefaultHttpClient(); $client->resetParameters(); $client->setUri($this->_uri); $client->setMethod('GET'); $accept = EasyRdf_Format::getHttpAcceptHeader(array('application/sparql-results+json' => 1.0, 'application/sparql-results+xml' => 0.8)); $client->setHeaders('Accept', $accept); $client->setParameterGet('query', $prefixes . $query); $response = $client->request(); if ($response->isSuccessful()) { list($type, $params) = EasyRdf_Utils::parseMimeType($response->getHeader('Content-Type')); if (strpos($type, 'application/sparql-results') === 0) { return new EasyRdf_Sparql_Result($response->getBody(), $type); } else { return new EasyRdf_Graph($this->_uri, $response->getBody(), $type); } } else { throw new EasyRdf_Exception("HTTP request for SPARQL query failed: " . $response->getBody()); } }