/** * @see https://github.com/njh/easyrdf/issues/140 */ public function testIssue140() { $this->markTestIncomplete('fix for bug #140 is not implemented yet'); $filename = 'turtle/gh140-freebase.ttl'; $graph = new EasyRdf_Graph(); $triple_count = $this->turtleParser->parse($graph, readFixture($filename), 'turtle', $this->baseUri . basename($filename)); $this->assertEquals(14, $triple_count); }
/** * 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) { // Build the query if (is_null($depth)) { $depth = 3; } $query = $this->query_builder->createFetchQuery($base_uri, \Request::root(), null, $limit, $offset, $depth); $store = $this->setUpArc2Store(); $result = $store->query($query); if (!empty($result['result'])) { $result = $result['result']; $ttl_string = $store->toNTriples($result); $parser = new \EasyRdf_Parser_Turtle(); $parser->parse($graph, $ttl_string, 'turtle', ''); } if (!$result) { $errors = $store->getErrors(); $message = array_shift($errors); \Log::error("Error occured in the triples package, while trying to retrieve triples: " . $message); } return $graph; }
private function processDcat() { $controller = \App::make('Tdt\\Core\\Definitions\\DcatController'); $response = $controller->handle(''); // Get the semantic (turtle) content $turtle = $response->getOriginalContent(); $graph = new \EasyRdf_Graph(); $parser = new \EasyRdf_Parser_Turtle(); $total_triples = $parser->parse($graph, $turtle, 'turtle', ''); $datasets = $graph->allOfType('dcat:Dataset'); // Make sure triples are created and inserted into the graph $this->assertEquals(2, count($datasets)); foreach ($datasets as $dataset) { $literal_properties = ['dc:title', 'dc:description', 'dc:identifier', 'dc:issued', 'dc:modified']; foreach ($literal_properties as $property) { $literal = $dataset->getLiteral($property); $this->assertNotEmpty($literal); } // Get the distribution $distribution = $dataset->getResource('dcat:distribution'); $this->assertNotEmpty($distribution); } $catalog = $graph->allOfType('dcat:Catalog'); $this->assertEquals(1, count($catalog)); $this->processLinkHeader($response); }
/** * 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; }