Пример #1
0
 /**
  * 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;
 }