Exemple #1
0
 /** 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()) {
         $type = $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());
     }
 }
 public function testNamespaces()
 {
     $ns = EasyRdf_Namespace::namespaces();
     $this->assertSame('http://purl.org/dc/terms/', $ns['dc']);
     $this->assertSame('http://xmlns.com/foaf/0.1/', $ns['foaf']);
 }
Exemple #3
0
 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());
     }
 }
      padding: 1em;
      margin: 0.5em;
      background-color: #E6E6E6;
    }
  </style>
</head>
<body>
<h1>EasyRdf SPARQL Query Form</h1>

<div style="margin: 0.5em">
  <?php 
print form_tag();
print label_tag('endpoint');
print text_field_tag('endpoint', "http://dbpedia.org/sparql", array('size' => 80)) . '<br />';
print "<code>";
foreach (EasyRdf_Namespace::namespaces() as $prefix => $uri) {
    print "PREFIX {$prefix}: &lt;" . htmlspecialchars($uri) . "&gt;<br />\n";
}
print "</code>";
print text_area_tag('query', "SELECT * WHERE {\n  ?s ?p ?o\n}\nLIMIT 10", array('rows' => 10, 'cols' => 80)) . '<br />';
print check_box_tag('text') . label_tag('text', 'Plain text results') . '<br />';
print reset_tag() . submit_tag();
print form_end_tag();
?>
</div>

<?php 
if (isset($_REQUEST['endpoint']) and isset($_REQUEST['query'])) {
    $sparql = new EasyRdf_Sparql_Client($_REQUEST['endpoint']);
    try {
        $results = $sparql->query($_REQUEST['query']);
Exemple #5
0
 /**
  * Register a new namespace.
  *
  * @param string $prefix The namespace prefix (eg 'foaf')
  * @param string $long The namespace URI (eg 'http://xmlns.com/foaf/0.1/')
  */
 public static function set($prefix, $long)
 {
     if (!is_string($prefix) or $prefix === null) {
         throw new InvalidArgumentException("\$prefix should be a string and cannot be null or empty");
     }
     if ($prefix !== '') {
         // prefix        ::= Name minus ":"                   // see: http://www.w3.org/TR/REC-xml-names/#NT-NCName
         // Name          ::= NameStartChar (NameChar)*        // see: http://www.w3.org/TR/REC-xml/#NT-Name
         // NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] |
         //                   [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] |
         //                   [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
         // NameChar      ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
         $_name_start_char = 'A-Z_a-z\\xc0-\\xD6\\xd8-\\xf6\\xf8-\\xff\\x{0100}-\\x{02ff}\\x{0370}-\\x{037d}' . '\\x{037F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{2070}-\\x{218F}\\x{2C00}-\\x{2FEF}\\x{3001}-\\x{D7FF}' . '\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}';
         $_name_char = $_name_start_char . '\\-.0-9\\xb7\\x{0300}-\\x{036f}\\x{203f}-\\x{2040}';
         $regex = "#^[{$_name_start_char}]{1}[{$_name_char}]{0,}\$#u";
         $match_result = preg_match($regex, $prefix);
         if ($match_result === false) {
             throw new LogicException('regexp error');
         }
         if ($match_result === 0) {
             throw new InvalidArgumentException("\$prefix should match RDFXML-QName specification. got: {$prefix}");
         }
     }
     if (!is_string($long) or $long === null or $long === '') {
         throw new InvalidArgumentException("\$long should be a string and cannot be null or empty");
     }
     $prefix = strtolower($prefix);
     $namespaces = self::namespaces();
     $namespaces[$prefix] = $long;
     self::$namespaces = $namespaces;
 }