diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..88900c3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Babibubebon + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/app/Http/Controllers/ResourceController.php b/app/Http/Controllers/ResourceController.php index 8d9a855..2e116d5 100644 --- a/app/Http/Controllers/ResourceController.php +++ b/app/Http/Controllers/ResourceController.php @@ -16,12 +16,11 @@ class ResourceController extends Controller ]; /** - * @param Request $request - * @return mixed + * ResourceController constructor. */ - protected function getCurrentDatasetConfig($request) { - $datasetName = explode('.', $request->route()[1]['as'])[1]; - return config('datasets.' . $datasetName); + public function __construct() + { + $this->middleware('dataset'); } /** @@ -31,28 +30,45 @@ class ResourceController extends Controller */ protected function querySparql($request, $id) { - $datasetConfig = $this->getCurrentDatasetConfig($request); - $client = new \EasyRdf_Sparql_Client($datasetConfig['endpoint']); - - $resourceUri = str_replace('{id}', $id, $datasetConfig['resource_uri']); - $query = 'DESCRIBE <' . $resourceUri . '>'; + $client = new \EasyRdf_Sparql_Client($request->datasetConfig['endpoint']); + $query = <<resourceUri}> ?p ?o . + ?s ?ip <{$request->resourceUri}> . + } + WHERE { + <{$request->resourceUri}> ?p ?o . + ?s ?ip <{$request->resourceUri}> . + } +EOT; return $client->query($query); } + /** + * @param Request $request + * @param $id + * @return \Illuminate\View\View + */ public function html(Request $request, $id) { - $graph = $this->querySparql($request, $id); - $subject = key($graph->toRdfPhp()); - $datasetConfig = $this->getCurrentDatasetConfig($request); - $dataUri = str_replace('{id}', $id, $datasetConfig['data_uri']); + $graph = $this->querySparql($request, urldecode($id)); + if ($graph->isEmpty()) { + abort(404); + } return view('resource')->with([ 'graph' => $graph, - 'subject' => $subject, - 'dataUri' => $dataUri, + 'primaryTopic' => $request->resourceUri, + 'dataUri' => $request->dataUri, ]); } + /** + * @param Request $request + * @param $id + * @param $ext + * @return \Illuminate\Http\Response|\Laravel\Lumen\Http\ResponseFactory + */ public function data(Request $request, $id, $ext) { if (!in_array($ext, $this->acceptableFileExtensions)) { diff --git a/app/Http/Middleware/DatasetMiddleware.php b/app/Http/Middleware/DatasetMiddleware.php new file mode 100644 index 0000000..537a4ef --- /dev/null +++ b/app/Http/Middleware/DatasetMiddleware.php @@ -0,0 +1,37 @@ +route()) { + return $next($request); + } + + $datasetName = explode('.', $request->route()[1]['as'])[1]; + $currentDatasetConfig = config('datasets.' . $datasetName); + + $id = $request->route('id'); + $resourceUri = str_replace('{id}', $id, $currentDatasetConfig['resource_uri']); + $dataUri = str_replace('{id}', $id, $currentDatasetConfig['data_uri']); + + $request->merge([ + 'datasetConfig' => $currentDatasetConfig, + 'resourceUri' => $resourceUri, + 'dataUri' => $dataUri, + ]); + + return $next($request); + } +} diff --git a/bootstrap/app.php b/bootstrap/app.php index c669c62..543e6bc 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -72,6 +72,7 @@ $app->singleton( $app->routeMiddleware([ 'content_negotiation' => App\Http\Middleware\ContentNegotiatorMiddleware::class, + 'dataset' => App\Http\Middleware\DatasetMiddleware::class, ]); /* diff --git a/config/app.php b/config/app.php index f6b34cc..e3dc66a 100644 --- a/config/app.php +++ b/config/app.php @@ -2,7 +2,7 @@ return [ - 'version' => '1.1.0', + 'version' => '1.2.0', /* |-------------------------------------------------------------------------- diff --git a/resources/views/parts/row.blade.php b/resources/views/parts/row.blade.php new file mode 100644 index 0000000..4b3a602 --- /dev/null +++ b/resources/views/parts/row.blade.php @@ -0,0 +1,34 @@ + + + @if($subject !== $primaryTopic) + is + @endif + {{ \EasyRdf_Namespace::shorten($predicate) ?? $predicate }} + @if($subject !== $primaryTopic) + of + @endif + + + + + diff --git a/resources/views/resource.blade.php b/resources/views/resource.blade.php index 2dbacdb..7df10be 100644 --- a/resources/views/resource.blade.php +++ b/resources/views/resource.blade.php @@ -1,12 +1,12 @@ @extends('base') @section('title') - About: {{ $graph->label($subject) ?? $graph->getLiteral($subject, 'schema:name') }} + About: {{ $graph->label($primaryTopic) ?? $graph->getLiteral($primaryTopic, 'schema:name') }} @endsection @section('content')

@yield('title')

- {{ $subject }} + {{ $primaryTopic }}