class EasyRdf_Sparql_Client

Class for making SPARQL queries using the SPARQL 1.1 Protocol

@package EasyRdf @copyright Copyright (c) 2009-2012 Nicholas J Humfrey @license http://www.opensource.org/licenses/bsd-license.php

Hierarchy

Expanded class hierarchy of EasyRdf_Sparql_Client

File

drupal/core/vendor/easyrdf/easyrdf/lib/EasyRdf/Sparql/Client.php, line 46

View source
class EasyRdf_Sparql_Client {

  /** The address of the SPARQL Endpoint */
  private $uri = null;

  /** Configuration settings */
  private $config = array();

  /** Create a new SPARQL endpoint client
   *
   * @param string $uri The address of the SPARQL Endpoint
   */
  public function __construct($uri) {
    $this->uri = $uri;
  }

  /** Get the URI of the SPARQL endpoint
   *
   * @return string The URI of the SPARQL endpoint
   */
  public function getUri() {
    return $this->uri;
  }

  /** 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());
    }
  }

  /** Magic method to return URI of the SPARQL endpoint when casted to string
   *
   * @return string The URI of the SPARQL endpoint
   */
  public function __toString() {
    return $this->uri == null ? '' : $this->uri;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EasyRdf_Sparql_Client::$config private property Configuration settings
EasyRdf_Sparql_Client::$uri private property The address of the SPARQL Endpoint
EasyRdf_Sparql_Client::getUri public function Get the URI of the SPARQL endpoint
EasyRdf_Sparql_Client::query public function Make a query to the SPARQL endpoint
EasyRdf_Sparql_Client::__construct public function Create a new SPARQL endpoint client
EasyRdf_Sparql_Client::__toString public function Magic method to return URI of the SPARQL endpoint when casted to string