/** * 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) = $this->processURI($uri); // 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; $cache_string .= http_build_query(\Input::except('limit', 'offset', 'page', 'page_size')); $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']; // Create the right datacontroller $controller_class = 'Tdt\\Core\\DataControllers\\' . $source_type . 'Controller'; $data_controller = \App::make($controller_class); // Get REST parameters $rest_parameters = str_replace($definition['collection_uri'] . '/' . $definition['resource_name'], '', $uri); $rest_parameters = ltrim($rest_parameters, '/'); $rest_parameters = explode('/', $rest_parameters); if (empty($rest_parameters[0]) && !is_numeric($rest_parameters[0])) { $rest_parameters = array(); } // Retrieve dataobject from datacontroller $data = $data_controller->readData($source_definition, $rest_parameters); $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); } // 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 Cache::put($cache_string, $data, $source_definition['cache']); // 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})."); } } } }
public function getDescriptionInfo($identifier) { $definition = $this->getEloquentDefinition($identifier); $properties = array(); $source_definition = $definition->source()->first(); foreach ($definition->getFillable() as $key) { $properties[$key] = $definition->{$key}; } $properties['type'] = strtolower($source_definition->type); $properties['description'] = @$source_definition->description; // Get the formats based on the source definition meta-data $format_helper = new FormatHelper(); $formats = $format_helper->getFormatsForType($source_definition->toArray()); $properties['formats'] = $formats; $properties['identifier'] = $identifier; unset($properties['map_property']); return $properties; }
/** * 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) = $this->processURI($uri); // 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']; // 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); $rest_parameters = array_diff($uri_segments, array($definition['collection_uri'], $definition['resource_name'])); $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); } $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)) { \EasyRdf_Namespace::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 Cache::put($cache_string, $data, $source_definition['cache']); // 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})."); } } } }
/** * Format using requested formatter (via extension, Accept-header or default) * * @param Tdt\Core\Datasets\Data $data The data object on which the response will be based * @param string $extension The preferred format in which the data should be returned * * @return \Response */ public static function getResponse($data, $extension = null) { // Check Accept-header $accept_header = \Request::header('Accept'); // Safety first $extension = strtoupper($extension); // Formatter class $formatter_class = 'Tdt\\Core\\Formatters\\' . $extension . 'Formatter'; if (empty($extension)) { $negotiator = new FormatNegotiator(); foreach (self::$mime_types_map as $format_name => $mime_types) { $negotiator->registerFormat($format_name, $mime_types, true); } // Create a priority list of formats $priorities = array(); $format_helper = new FormatHelper(); if (empty($data->preferred_formats)) { // Still nothing? Use default formatter if (empty($extension) && !$data->is_semantic) { // Default formatter for non semantic data $data->preferred_formats = array('json'); } elseif (empty($extension) && $data->is_semantic) { // Default formatter for semantic data is turtle $data->preferred_formats = array('ttl'); } } if (!in_array('html', $priorities)) { array_push($priorities, 'html'); } $priorities = array_merge($data->preferred_formats, $priorities); // Add support for our other formatters as well, if they're not already in the priorities foreach ($format_helper->getAvailableFormats($data) as $format) { if (!in_array($format, $priorities)) { array_push($priorities, $format); } } array_push($priorities, '*/*'); $format = $negotiator->getBestFormat($accept_header, $priorities); $format_object = $negotiator->getBest($accept_header, $priorities); if (!$format || $format_object->getQuality() == 0) { $format_helper = new FormatHelper(); $available_formats = implode(', ', array_values($format_helper->getAvailableFormats($data))); \App::abort(406, "The requested Content-Type is not supported, or the best quality we found was 0. The supported formats for this resource are: " . $available_formats); } // Safety first $extension = strtoupper($format); // Formatter class $formatter_class = 'Tdt\\Core\\Formatters\\' . $extension . 'Formatter'; } elseif (!class_exists($formatter_class)) { $format_helper = new FormatHelper(); $available_formats = implode(', ', array_values($format_helper->getAvailableFormats($data))); \App::abort(406, "The requested Content-Type is not supported, or the best quality we found was 0. The supported formats for this resource are: " . $available_formats); } // Create the response from the designated formatter $response = $formatter_class::createResponse($data); // Set the paging header if (!empty($data->paging)) { $response->header('Link', self::getLinkHeader($data->paging)); } // Set the URI template header if (!empty($data->optional_parameters) || !empty($data->rest_parameters)) { // http://www.mnot.net/blog/2006/10/04/uri_templating $link_template = self::fetchUrl($extension); if (substr($link_template, -1, 1) == '/') { $link_template = substr($link_template, 0, -1); } // Add the required parameters foreach ($data->rest_parameters as $required_parameter) { $link_template .= '/{' . $required_parameter . '}'; } // Add the extension if given if (!empty($extension)) { $link_template .= '.' . strtolower($extension); } // Add the optional parameters if (!empty($data->optional_parameters)) { $link_template .= '{?'; foreach ($data->optional_parameters as $optional_parameter) { $link_template .= $optional_parameter . ', '; } $link_template = rtrim($link_template, ', '); $link_template .= '}'; } $response->header('Link-Template', $link_template); } // Cache settings $cache_minutes = -1; if (\Config::get('cache.enabled', true)) { $cache_minutes = 1; // Cache per resource if (!empty($data->source_definition['cache'])) { $cache_minutes = $data->source_definition['cache']; } } // Cache headers $response->header('Cache-Control', 'public, max-age=' . $cache_minutes * 60 . ', pre-check=' . $cache_minutes * 60 . ''); $response->header('Pragma', 'public'); $response->header('Expires', date(DATE_RFC822, strtotime("{$cache_minutes} minute"))); $response->header('Vary', 'Accept, Accept-encoding'); // Return formatted response return $response; }
/** * Resolve a graph pattern query (/all route) * * @param string $format The format of the request * * @return \Response */ public function solveQuery($format = null) { $data; if (!empty($format)) { $format = ltrim($format, '.'); } // Ignore the rest of the uri after /all and work with the request parameters as they were given $cache_string = sha1($this->getRawRequestURI(\Request::root())); // Check if the response to the query has been cached already if (Cache::has($cache_string)) { $data = Cache::get($cache_string); } else { // Get the graph pattern query string parameters from the request list($s, $p, $o) = $this->getTemplateParameters(); // Pass them to our sparql query builder SparqlQueryBuilder::setParameters(array($s, $p, $o)); $base_uri = null; // If no parameter has been filled in, the URI we have to match triples with is the root of our application if ($s == '?s' && $p == '?p' && $o == '?o') { $base_uri = \Request::root(); } // Fetch matching triples $result = $this->triples->getTriples($base_uri, \Request::get('limit', 100), \Request::get('offset', 0)); // If the graph contains no triples, then the graph pattern couldn't resolve to anything, 404 it is if ($result->countTriples() == 0) { \App::abort(404, "The resource couldn't be found, nor dereferenced."); } $definition = array('resource_name' => "all", 'collection_uri' => ""); $source_definition = array('description' => 'Semantic data collected out the configured semantic data sources.', 'type' => 'Semantic'); $data = new Data(); $data->definition = $definition; $data->source_definition = $source_definition; $data->data = $result; $data->is_semantic = true; // Add the available, supported formats to the object $format_helper = new FormatHelper(); $data->formats = $format_helper->getAvailableFormats($data); // Store in cache for a default of 5 minutes Cache::put($cache_string, $data, 5); } // Add the hydra namespace, it's not present in the easy rdf namespaces by default \EasyRdf_Namespace::set('hydra', 'http://www.w3.org/ns/hydra/core#'); // Return the formatted response with content negotiation $response = ContentNegotiator::getResponse($data, $format); // Pass a Vary header so that browsers know they have to take the accept header // into consideration when they apply caching client side $response->header('Vary', 'Accept'); // Allow CORS $response->header('Access-Control-Allow-Origin', '*'); return $response; }