public static function getBody($dataObj) { if ($dataObj->is_semantic) { // Check if a configuration is given $conf = array(); if (!empty($dataObj->semantic->conf)) { $conf = $dataObj->semantic->conf; foreach ($conf['ns'] as $prefix => $uri) { RdfNamespace::set($prefix, $uri); } } // Add the configured ontology prefixes $ontologies = \App::make('Tdt\\Core\\Repositories\\Interfaces\\OntologyRepositoryInterface'); $context = array(); // Only add the common namespaces $namespaces = array('hydra', 'rdf', 'rdfs', 'foaf', 'void', 'xsd', 'skos', 'xs'); foreach ($namespaces as $ns) { $namespace = $ontologies->getByPrefix($ns); if (!empty($namespace)) { $context[$ns] = $namespace['uri']; } } $output = $dataObj->data->serialise('jsonld'); // Next, encode the context as JSON $jsonContext = json_encode($context); // Compact the JsonLD by using @context -> Needs tweaking can only return the // URI spaces that are used in the document. $compacted = JsonLD::compact($output, $jsonContext); // Print the resulting JSON-LD! return JsonLD::toString($compacted, true); } else { \App::abort(400, "The data is not a semantically linked document, a linked data JSON representation is not possible."); } }
protected function open() { $this->graph = new Graph(); $rdfa_parser = new RdfaParser(); $json_parser = new JsonLd(); \EasyRdf\RdfNamespace::set('dcat', 'http://www.w3.org/ns/dcat#'); \EasyRdf\RdfNamespace::set('lbld', 'http://decisions.data.vlaanderen.be/ns#'); $data = file_get_contents($this->extractor->uri); // Assume we have an rdfa document to begin with $rdfa_parser->parse($this->graph, $data, 'rdfa', $this->extractor->base_uri); // Check if we have properties we need to include into the current graph $properties = explode(',', $this->extractor->follow_properties); foreach ($this->graph->resources() as $resource) { foreach ($properties as $property) { $resolve_resources = $resource->allResources($property); if (!empty($resolve_resources)) { foreach ($resolve_resources as $resolve_resource) { $data = file_get_contents($resolve_resource->getUri()); // Parse any rdfa data in the document into the existing graph $this->graph->parse($data, 'rdfa', $resolve_resource->getUri()); $json = $this->parseJsonldSnippet($data); if (!empty($json)) { try { $this->graph->parse($json, 'jsonld', $resolve_resource->getUri()); } catch (\Exception $ex) { \Log::error("We could not parse json from the data, the data was:" . $json); } } } } } } // We only return the graph once as a full datastructure $this->has_next = true; }
public function __construct($endpoint, $namespaces) { $this->endpoint = $endpoint; $this->namespaces = $namespaces; foreach ($namespaces as $namespace => $uri) { \EasyRdf\RdfNamespace::set($namespace, $uri); } }
/** * Create the DCAT document of the published (non-draft) resources * * @return mixed \Data object with a graph of DCAT information */ private function createDcat() { $ns = $this->dcat->getNamespaces(); foreach ($ns as $prefix => $uri) { RdfNamespace::set($prefix, $uri); } // If limit is empty, provide a custom page size for the DCAT document $limit = \Input::get('limit'); if (empty($limit)) { \Input::merge(array('limit' => 100)); } // Apply paging when fetching the definitions list($limit, $offset) = Pager::calculateLimitAndOffset(); $definition_count = $this->definitions->countPublished(); $definitions = $this->definitions->getAllPublished($limit, $offset); $oldest = $this->definitions->getOldest(); $describedDefinitions = array(); // Add the source type description to the definition foreach ($definitions as $definition) { $definition = array_merge($definition, $this->definitions->getFullDescription($definition['collection_uri'] . '/' . $definition['resource_name'])); array_push($describedDefinitions, $definition); } $graph = $this->dcat->getDcatDocument($describedDefinitions, $oldest); // Return the dcat feed in our internal data object $data_result = new Data(); $data_result->data = $graph; $data_result->is_semantic = true; $data_result->paging = Pager::calculatePagingHeaders($limit, $offset, $definition_count); // Add the semantic configuration for the ARC graph $data_result->semantic = new \stdClass(); $data_result->semantic->conf = array('ns' => $ns); $data_result->definition = []; $data_result->definition['resource_name'] = 'dcat'; $data_result->definition['collection_uri'] = 'info'; $data_result->definition['source_type'] = 'DCAT'; return $data_result; }
protected function open() { // Perform XLST $xsl_url = "https://webgate.ec.europa.eu/CITnet/stash/projects/ODCKAN/repos/iso-19139-to-dcat-ap/browse/iso-19139-to-dcat-ap.xsl?raw"; $proc = null; try { $xml = new DOMDocument(); $xml->load($this->extractor->uri); $xsl = new DOMDocument(); $xsl->load($xsl_url); $proc = new XSLTProcessor(); $proc->importStyleSheet($xsl); } catch (\ErrorException $ex) { $this->log('Something went wrong: ' . $ex->getMessage()); die; } $dcat_document = $proc->transformToXML($xml); \EasyRdf\RdfNamespace::set('locn', 'http://www.w3.org/ns/locn#'); // Parse the dcat graph $graph = new Graph(); $rdf_parser = new RdfXml(); $rdf_parser->parse($graph, $dcat_document, 'rdfxml', 'http://foo'); $this->datasets = $graph->allOfType('dcat:Dataset'); }
private function sparqlRetrieveTriplesForNotation($notation) { // alternative query (the fuseki's query is the same except that # must be encoded as %23 or it will be understood as eof //$query = "prefix skos: <http://www.w3.org/2004/02/skos/core#> select ?s ?p ?o where {?s skos:notation '" . $notation . "' . }"; \EasyRdf\RdfNamespace::set('skos', 'http://www.w3.org/2004/02/skos/core#'); $query = "select ?s ?p ?o where {?s skos:notation '" . $notation . "' . }"; $sparqlClient = new \EasyRdf\Sparql\Client(BASE_URI_ . ':3030/openskos/query'); $result = $sparqlClient->query($query); return $result; }
/** * Retrieve a Data object identified by $uri * * @param string $uri The identifier that identifies a resource * * @return \Response */ public function get($uri) { // Check permissions Auth::requirePermissions('dataset.view'); // Split for an (optional) extension list($uri, $extension) = self::processURI($uri); $extension = strtolower($extension); // Check for caching // Based on: URI / Rest parameters / Query parameters / Paging headers $cache_string = $uri; list($limit, $offset) = Pager::calculateLimitAndOffset(); $cache_string .= '/limit=' . $limit . 'offset=' . $offset; $omit = ['limit', 'offset', 'page', 'page_size']; $query_string_params = \Input::get(); foreach ($query_string_params as $key => $val) { if (in_array($key, $omit)) { unset($query_string_params[$key]); } } $cache_string .= http_build_query($query_string_params); $cache_string = sha1($cache_string); if (Cache::has($cache_string)) { return ContentNegotiator::getResponse(Cache::get($cache_string), $extension); } else { // Get definition $definition = $this->definition->getByIdentifier($uri); if ($definition) { // Get source definition $source_definition = $this->definition->getDefinitionSource($definition['source_id'], $definition['source_type']); if ($source_definition) { $source_type = $source_definition['type']; // KML can be formatted into different formats if ($source_definition['type'] == 'XML' && $source_definition['geo_formatted'] == 1) { $source_type == 'kml'; $source_definition['type'] = 'KML'; } // Create the right datacontroller $controller_class = 'Tdt\\Core\\DataControllers\\' . $source_type . 'Controller'; $data_controller = \App::make($controller_class); // Get REST parameters $uri_segments = explode('/', $uri); $definition_segments = explode('/', $definition['collection_uri']); array_push($definition_segments, $definition['resource_name']); $rest_parameters = array_diff($uri_segments, $definition_segments); $rest_parameters = array_values($rest_parameters); $throttle_response = $this->applyThrottle($definition); if (!empty($throttle_response)) { return $throttle_response; } // Retrieve dataobject from datacontroller $data = $data_controller->readData($source_definition, $rest_parameters); // If the source type is XML, just return the XML contents, don't transform if (strtolower($source_type) == 'xml' && $extension == 'xml') { return $this->createXMLResponse($data->data); } elseif (strtolower($source_type) == 'xml' && $extension == 'kml' && $data->geo_formatted) { return $this->createXMLResponse($data->data); } elseif (!$data->is_semantic && $extension == 'xml' && $source_type != 'xml') { \App::abort(406, "The requested format for the datasource is not available."); } elseif (strtolower($source_type) == 'xml' && !$data->geo_formatted && !empty($extension) && $extension != 'xml') { \App::abort(406, "The requested format for the datasource is not available."); } elseif (strtolower($source_type) == 'xml' && $data->geo_formatted && !empty($extension) && !in_array($extension, $data->preferred_formats)) { \App::abort(406, "The requested format for the datasource is not available."); } $data->rest_parameters = $rest_parameters; // REST filtering if ($source_type != 'INSTALLED' && count($data->rest_parameters) > 0) { $data->data = self::applyRestFilter($data->data, $data->rest_parameters); } // Semantic paging with the hydra voc if ($data->is_semantic && !empty($data->paging)) { RdfNamespace::set('hydra', 'http://www.w3.org/ns/hydra/core#'); $graph = $data->data; $url = \URL::to($definition['collection_uri'] . '/' . $definition['resource_name']); $request_url = \Request::url(); $graph->addResource($request_url, 'void:subset', $url); foreach ($data->paging as $key => $val) { $paged_url = $request_url . '?offset=' . $val[0] . '&limit=' . $val[1] . Pager::buildQuerystring(); switch ($key) { case 'next': $graph->addResource($request_url, 'hydra:nextPage', $paged_url); break; case 'previous': $graph->addResource($request_url, 'hydra:previousPage', $paged_url); break; case 'last': $graph->addResource($request_url, 'hydra:lastPage', $paged_url); break; case 'first': $graph->addResource($request_url, 'hydra:firstPage', $paged_url); break; } } $graph->addResource($url, 'a', 'dcat:Dataset'); $title = null; if (!empty($definition['title'])) { $title = $definition['title']; } else { $title = $definition['collection_uri'] . '/' . $definition['resource_name']; } $graph->addLiteral($url, 'dc:title', $title); $graph->addLiteral($url, 'dc:description', $source_definition['description']); $graph->addResource($url, 'dcat:distribution', $url . '.json'); $data->data = $graph; } // Add definition to the object $data->definition = $definition; // Add source definition to the object $data->source_definition = $source_definition; // Add the available, supported formats to the object $format_helper = new FormatHelper(); $data->formats = $format_helper->getAvailableFormats($data); // Store in cache if (!empty($definition['cache_minutes'])) { Cache::put($cache_string, $data, $definition['cache_minutes']); } // Return the formatted response with content negotiation return ContentNegotiator::getResponse($data, $extension); } else { \App::abort(404, "Source for the definition could not be found."); } } else { // Coulnd't find a definition, but it might be a collection $resources = $this->definition->getByCollection($uri); if (count($resources) > 0) { $data = new Data(); $data->data = new \stdClass(); $data->data->datasets = array(); $data->data->collections = array(); foreach ($resources as $res) { // Check if it's a subcollection or a dataset $collection_uri = rtrim($res['collection_uri'], '/'); if ($collection_uri == $uri) { array_push($data->data->datasets, \URL::to($collection_uri . '/' . $res['resource_name'])); } else { // Push the subcollection if it's not already in the array if (!in_array(\URL::to($collection_uri), $data->data->collections)) { array_push($data->data->collections, \URL::to($collection_uri)); } } } // Fake a definition $data->definition = new \Definition(); $uri_array = explode('/', $uri); $last_chunk = array_pop($uri_array); $data->definition->collection_uri = join('/', $uri_array); $data->definition->resource_name = $last_chunk; // Return the formatted response with content negotiation return ContentNegotiator::getResponse($data, $extension); } else { \App::abort(404, "The dataset or collection you were looking for could not be found (URI: {$uri})."); } } } }
/** * @param $prefix * @param $long */ public function set($prefix, $long) { RdfNamespace::set($prefix, $long); }
protected function open() { \EasyRdf\RdfNamespace::set('locn', 'http://www.w3.org/ns/locn#'); $graph = Graph::newAndLoad($this->extractor->uri, $this->extractor->format); $this->datasets = $graph->allOfType('dcat:Dataset'); }