public function testParseEmpty() { $count = $this->parser->parse($this->graph, '{}', 'jsonld', null); $this->assertSame(0, $count); // Should be empty but no exception thrown $this->assertSame(0, $this->graph->countTriples()); }
/** * Return all triples with a subject that equals the base uri * * @param string $base_uri * @param integer $limit * @param integer $offset * @param boolean $deference If dereferenced, use the depth of the configured semantic source, if not use 1 as depth * * @return EasyRdf_Graph */ public function getTriples($base_uri, $limit = 100, $offset = 0, $dereference = false) { // Check if hash variants should be used in the grah patterns to fetch matching triples $this->checkHashVariants(); $depth = null; if (!$dereference) { $depth = 1; } $original_limit = $limit; $original_offset = $offset; // Fetch the total amount of matching triples, over all of the configured semantic sources $total_triples_count = $this->getCount($base_uri, $depth); // Fetch the local amount of matching triples $count_local_triples = $this->local_store->getCount($base_uri, $depth); // Create the resulting graph $graph = new \EasyRdf_Graph(); if ($count_local_triples < $offset) { $offset -= $count_local_triples; } else { // Add triples from the local store $start_amount = $graph->countTriples(); $graph = $this->local_store->addTriples($base_uri, $graph, $limit, $offset, $depth); $offset -= $graph->countTriples() - $start_amount; } if ($offset < 0) { $offset = 0; } $sparql_triples_count = $this->sparql_handler->getCount($base_uri, $depth); // If there's more room for triples, and the sparql can provide sufficient triples // according to the paging parameters, add triples from the sparql if ($graph->countTriples() < $limit && $offset < $sparql_triples_count) { // For every semantic source, count the triples we'll get out of them (sparql and ldf for the moment) $graph = $this->sparql_handler->addTriples($base_uri, $graph, $limit, $offset, $depth); } $ldf_triples_count = $this->ldf_handler->getCount($base_uri, $depth); if ($graph->countTriples() < $limit && $offset < $ldf_triples_count) { $graph = $this->ldf_handler->addTriples($base_uri, $graph, $limit, $offset, $depth); } // If the graph doesn't contain any triples, then the resource can't be resolved if ($graph->countTriples() == 0) { \App::abort(404, 'The resource could not be found.'); } // Add the void and hydra meta-data triples to the resulting graph $graph = $this->addMetaTriples($graph, $original_limit, $original_offset, $total_triples_count); return $graph; }
/** * Add triples to the graph and return it based on limit, offset and the SPARQL query * * @param string $base_uri * @param EasyRdf_Graph $graph * @param int $limit * @param int $offset * @param integer $depth The depth the queries should have, handlers should not override this if given * * @return EasyRdf_Graph */ public function addTriples($base_uri, $graph, $limit, $offset, $depth = null) { $total_triples = $graph->countTriples(); // Iterate the sparql endpoints foreach ($this->sparql_repo->getAll() as $sparql_source) { $endpoint = $sparql_source['endpoint']; $pw = $sparql_source['endpoint_password']; $user = $sparql_source['endpoint_user']; $endpoint = rtrim($endpoint, '/'); if (is_null($depth)) { $depth = $sparql_source['depth']; } $count_query = $this->query_builder->createCountQuery($base_uri, \Request::root(), $sparql_source['named_graph'], $depth); \Log::info("sparql : " . $count_query . " base uri : " . $base_uri); // Check if the response has been cached yet $cache_string = $this->buildCacheString($sparql_source['id'], $count_query); if (Cache::has($cache_string)) { $result = Cache::get($cache_string); } else { $count_query = urlencode($count_query); $count_query = str_replace("+", "%20", $count_query); $query_uri = $endpoint . '?query=' . $count_query . '&format=' . urlencode("application/sparql-results+json"); // Make a request with the count query to the SPARQL endpoint $result = $this->executeUri($query_uri, array(), $user, $pw); Cache::put($cache_string, $result, 5); } $response = json_decode($result); if (!empty($response)) { $count = $response->results->bindings[0]->count->value; // If the amount of matching triples is higher than the offset // add them and update the offset, if not higher, then only update the offset if ($count > $offset) { // Read the triples from the sparql endpoint $query_limit = $limit - $total_triples; $query = $this->query_builder->createFetchQuery($base_uri, \Request::root(), $sparql_source['named_graph'], $query_limit, $offset, $depth); $query = urlencode($query); $query = str_replace("+", "%20", $query); $query_uri = $endpoint . '?query=' . $query . '&format=' . urlencode("application/rdf+xml"); // Check for caching $cache_string = $this->buildCacheString($sparql_source['id'], $query_uri); if (Cache::has($cache_string)) { $result = Cache::get($cache_string); } else { $result = $this->executeUri($query_uri, array(), $user, $pw); } if (!empty($result) && $result[0] == '<') { // Parse the triple response and retrieve the triples from them $result_graph = new \EasyRdf_Graph(); $parser = new \EasyRdf_Parser_RdfXml(); $parser->parse($result_graph, $result, 'rdfxml', null); $graph = $this->mergeGraph($graph, $result_graph); $total_triples += $count - $offset; } else { $sparql_id = $sparql_source['id']; \Log::error("Something went wrong while fetching the triples from the sparql source with id {$sparql_id}. The error was " . $result . ". The query was : " . $query_uri); } } else { // Update the offset $offset -= $count; } if ($offset < 0) { $offset = 0; } } } return $graph; }
private function render_rdf($body) { global $iso_countries; require_once 'vendor/autoload.php'; try { $rdf_doc = new EasyRdf_Graph($this->uriCache->uri, $body); } catch (Exception $e) { return false; } // the RDF may be rubbish if ($rdf_doc->countTriples() == 0) { return false; } // easier to work with it as a php array $rdf_array = $rdf_doc->toRdfPhp(); // we can get RDF that isn't about the specimen! if (!array_key_exists($this->uriCache->uri, $rdf_array)) { return false; } $rdf_array = $rdf_array[$this->uriCache->uri]; // image - comes first to make floating easy if (isset($rdf_array['http://rs.tdwg.org/dwc/terms/associatedMedia'][0]['value'])) { $image_uri = $rdf_array['http://rs.tdwg.org/dwc/terms/associatedMedia'][0]['value']; echo "<a href=\"{$image_uri}\"><img class=\"cetaf-image\" src=\"{$image_uri}\" /></a>"; } // title if (isset($rdf_array['http://purl.org/dc/terms/title'][0]['value'])) { echo '<p class="cetaf-title"><a href="' . $this->uriCache->uri . '">' . $rdf_array['http://purl.org/dc/terms/title'][0]['value'] . '</a></p>'; } echo '<div class="cetaf-taxonomy">'; // scientificName if (isset($rdf_array['http://rs.tdwg.org/dwc/terms/scientificName'][0]['value'])) { echo '<p class="cetaf-scientificName">' . $rdf_array['http://rs.tdwg.org/dwc/terms/scientificName'][0]['value'] . '</p>'; } // family if (isset($rdf_array['http://rs.tdwg.org/dwc/terms/family'][0]['value'])) { $family = $rdf_array['http://rs.tdwg.org/dwc/terms/family'][0]['value']; $family = ucfirst(strtolower($family)); echo '<p class="cetaf-family"><a href="https://en.wikipedia.org/wiki/' . $family . '" >' . $family . '</a></p>'; } echo '</div>'; echo '<div class="cetaf-collection">'; // recordedBy if (isset($rdf_array['http://rs.tdwg.org/dwc/terms/recordedBy'][0]['value'])) { echo '<p class="cetaf-recordedBy">' . $rdf_array['http://rs.tdwg.org/dwc/terms/recordedBy'][0]['value'] . '</p>'; } // recordedBy if (isset($rdf_array['http://rs.tdwg.org/dwc/terms/recordNumber'][0]['value'])) { echo '<p class="cetaf-recordNumber">' . $rdf_array['http://rs.tdwg.org/dwc/terms/recordNumber'][0]['value'] . '</p>'; } // recorded date if (isset($rdf_array['http://purl.org/dc/terms/created'][0]['value'])) { echo '<p class="cetaf-created">' . $rdf_array['http://purl.org/dc/terms/created'][0]['value'] . '</p>'; } echo '</div>'; echo '<div class="cetaf-geography">'; // country if (isset($rdf_array['http://rs.tdwg.org/dwc/terms/countryCode'][0]['value'])) { $country_iso = strtoupper($rdf_array['http://rs.tdwg.org/dwc/terms/countryCode'][0]['value']); require_once 'inc/iso_countries.php'; if (array_key_exists($country_iso, $iso_countries)) { $country_name = $iso_countries[$country_iso]; echo "<p class=\"cetaf-country cetaf-country-{$country_iso}\"><a href=\"https://en.wikipedia.org/wiki/{$country_name}\">{$country_name}</a></p>"; } else { $country_name = $country_iso; echo "<p class=\"cetaf-country cetaf-country-{$country_iso}\">{$country_iso}</p>"; } } // lon lat if (isset($rdf_array['http://rs.tdwg.org/dwc/terms/decimalLongitude'][0]['value']) && isset($rdf_array['http://rs.tdwg.org/dwc/terms/decimalLatitude'][0]['value'])) { $lat = $rdf_array['http://rs.tdwg.org/dwc/terms/decimalLatitude'][0]['value']; $lon = $rdf_array['http://rs.tdwg.org/dwc/terms/decimalLongitude'][0]['value']; $lat_lon = "{$lat},{$lon}"; echo '<p class="cetaf-lat-lon"><a href="http://maps.google.com?q=' . $lat_lon . '">' . $lat_lon . '</a></p>'; } echo '</div>'; echo '<div class="cetaf-meta">'; // source if (isset($rdf_array['http://purl.org/dc/terms/publisher'][0]['value'])) { $publisher = $rdf_array['http://purl.org/dc/terms/publisher'][0]['value']; if (filter_var($publisher, FILTER_VALIDATE_URL)) { // fixme - is this a known publisher? if so render their logo echo "<p class=\"cetaf-publisher\"><a href=\"{$publisher}\">{$publisher}</a></p>"; } else { echo '<p class="cetaf-publisher">' . $publisher . '</p>'; } } echo '</div>'; return true; }
public function testLoadRedirect() { // Check that loading the same URL as a redirected request // doesn't result in multiple HTTP GETs $this->_client->addMockRedirect('GET', 'http://www.example.org/', 'http://www.example.com/', 301); $this->_client->addMockRedirect('GET', 'http://www.example.com/', 'http://www.example.com/foaf.rdf', 303); $this->_client->addMockOnce('GET', 'http://www.example.com/foaf.rdf', readFixture('foaf.json')); $graph = new EasyRdf_Graph(); $this->assertSame(0, $graph->countTriples()); $this->assertSame(14, $graph->load('http://www.example.org/', 'json')); $this->assertSame(14, $graph->countTriples()); $this->assertSame(0, $graph->load('http://www.example.com/foaf.rdf', 'json')); $this->assertSame(14, $graph->countTriples()); $this->assertStringEquals('Joe Bloggs', $graph->get('http://www.example.com/joe#me', 'foaf:name')); }
<?php include_once "./header.php"; require 'vendor/autoload.php'; require_once "html_tag_helpers.php"; $graph = new EasyRdf_Graph("http://localhost/lod/tcmdemoen.rdf"); $graph->load(); echo "The count is: " . $graph->countTriples(); $data = $graph->serialise('ntriples'); if (!is_scalar($data)) { $data = var_export($data, true); } //print htmlspecialchars($data); //$me = $graph->resource('http://www.example.com/Huperzia_Serrata'); $me = $graph->resource('http://www.example.com/fourGentlemenDecoction'); echo "<p>http://www.example.com/Huperzia_Serrata 的标签是: " . $me->get('rdfs:label') . "</p>"; echo "<p>http://www.example.com/Huperzia_Serrata 的中文标签是: " . $me->label('zh') . "</p>"; echo "<p>http://www.example.com/Huperzia_Serrata 的英文标签是: " . $me->label('en') . "</p>"; $hasMinister = $graph->resource('http://www.example.com/hasMinister'); echo "<p>http://www.example.com/hasMinister 的标签是: " . $hasMinister->get('rdfs:label') . "</p>"; $graph->allResources($me, $hasMinister); foreach ($me->all($hasMinister) as $type) { echo 'start'; $label = $type->label('zh'); if (!$label) { $label = $type->getUri(); } if ($type->isBnode()) { echo "<li>{$label}</li>"; } else { echo "<li>" . link_to_self($label, 'uri=' . urlencode($friend)) . "</li>";
/** * Add triples to the graph and return it based on limit, offset and the SPARQL query * * @param string $base_uri * @param EasyRdf_Graph $graph * @param int $limit * @param int $offset * * @return EasyRdf_Graph */ public function addTriples($base_uri, $graph, $limit, $offset) { $total_triples = $graph->countTriples(); // Iterate the LDF end points, note that ldf servers don't necessarily have page size's as a parameter // But rather have a fixed page size foreach ($this->ldf_repo->getAll() as $ldf_conf) { // Build the query string (raw) $query_string = $_SERVER['QUERY_STRING']; $q_string_raw = ''; $query_parts = explode('&', $query_string); // Don't let paging parameters in the re-constructed query string $invalid_q_string = array('page', 'page_size', 'limit', 'offset'); foreach ($query_parts as $part) { if (!empty($part)) { $couple = explode('=', $part); if (!in_array($couple[0], $invalid_q_string)) { $q_string_raw .= $couple[0] . '=' . $couple[1] . '&'; } } } $q_string_raw = rtrim($q_string_raw, '&'); $start_fragment = $ldf_conf['startfragment']; $entire_fragment = $start_fragment . '?' . $q_string_raw; $entire_fragment = rtrim($entire_fragment, '?'); // Make the LDF query (basic GET to the endpoint, should provide us with a hydra:totalItems or void:triples entry) $accept = array("Accept: text/turtle,*/*;q=0.0"); $response = ''; if (Cache::has($entire_fragment)) { $response = Cache::get($entire_fragment); } else { $response = $this->executeUri($entire_fragment, $accept); } if ($response) { // Try decoding it into turtle, if not something is wrong with the response body try { $tmp_graph = new \EasyRdf_Graph(); $parser = new \EasyRdf_Parser_Turtle(); \EasyRdf_Namespace::set('hydra', 'http://www.w3.org/ns/hydra/core#'); $parser->parse($tmp_graph, $response, 'turtle', null); // Fetch the count (hydra:totalItems or void:triples) $count = $tmp_graph->getLiteral($entire_fragment, 'hydra:totalItems'); $page_size = $tmp_graph->getLiteral($entire_fragment, 'hydra:itemsPerPage'); if (is_null($count)) { $count = $tmp_graph->getLiteral($entire_fragment, 'void:triples'); } if (is_null($count) || is_null($page_size)) { // Skip, the count has not been found on this endpoint $count = -1; \Log::warning("An LDF endpoint's count could not be retrieved from the uri: {$entire_fragment}"); } else { $count = $count->getValue(); $page_size = $page_size->getValue(); Cache::put($entire_fragment, $response, 5); } // If the amount of matching triples is higher than the offset // add them and update the offset, if not higher, then only update the offset if ($count > $offset) { // Read the triples from the LDF $query_limit = $limit - $total_triples; // There's no way of giving along the page size (not that we can presume) // So we have to make a numer of requests $amount_of_requests = ceil($query_limit / $page_size); // Calculate the page offset from the offset parameter $page_offset = ceil($offset / $page_size); for ($i = $page_offset; $i < $amount_of_requests + $page_offset; $i++) { $paged_fragment = $entire_fragment; if (!empty($q_string_raw)) { $paged_fragment .= '&page=' . $i; } else { $paged_fragment .= '?page=' . $i; } // Ask for turtle $accept = array('Accept: text/turtle'); $response = ''; if (Cache::has($paged_fragment)) { $response = Cache::get($paged_fragment); } else { $response = $this->executeUri($paged_fragment, $accept); } if ($response) { // Try decoding it into turtle, if not something is wrong with the response body try { $tmp_graph = new \EasyRdf_Graph(); $parser = new \EasyRdf_Parser_Turtle(); $parser->parse($tmp_graph, $response, 'turtle', $start_fragment); // Fetch the count (hydra:totalItems or void:triples) $total_items = $tmp_graph->getLiteral($paged_fragment, 'hydra:totalItems'); if (is_null($total_items)) { $total_items = $tmp_graph->getLiteral($paged_fragment, 'void:triples'); } if (!is_null($total_items)) { Cache::put($paged_fragment, $tmp_graph, 5); // This needs to be a function of a different helper class for LDF endpoints $tmp_graph = $this->rebaseGraph($start_fragment, $tmp_graph); $graph = $this->mergeGraph($graph, $tmp_graph); $total_triples += $page_size; } } catch (\EasyRdf_Parser_Exception $ex) { \Log::error("Failed to parse turtle content from the LDF endpoint: {$start_fragment}"); } } else { \Log::error("Something went wrong while fetching the triples from a LDF source. The error was " . $response . ". The query was : " . $paged_fragment); } } } else { // Update the offset $offset -= $count; } if ($offset < 0) { $offset = 0; } } catch (\EasyRdf_Parser_Exception $ex) { \Log::error("Failed to parse turtle content from the LDF endpoint: {$endpoint}"); } } else { \Log::warning("Couldn't fetch a proper response for the fragment: {$entire_fragment}."); } } return $graph; }