You are here

public function EasyRdf_Graph::load in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/easyrdf/easyrdf/lib/EasyRdf/Graph.php \EasyRdf_Graph::load()

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.

Parameters

string $uri The URI of the data to load:

string $format Optional format of the data (eg. rdfxml):

Return value

integer The number of triples added to the graph

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Graph.php, line 270

Class

EasyRdf_Graph
Container for collection of EasyRdf_Resources.

Code

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);
}