From 23d6bdadca012e762712f65d0af515dbbd53ba94 Mon Sep 17 00:00:00 2001 From: Babibubebon Date: Mon, 20 Jan 2020 01:48:11 +0900 Subject: [PATCH 1/4] create LICENSE --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE 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. From 8a5de215dd579d0a874fd6933dba10fb3d0e9416 Mon Sep 17 00:00:00 2001 From: Babibubebon Date: Mon, 20 Jan 2020 11:54:13 +0900 Subject: [PATCH 2/4] fix query including percent-encoded id --- app/Http/Controllers/ResourceController.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/ResourceController.php b/app/Http/Controllers/ResourceController.php index 8d9a855..5932c7f 100644 --- a/app/Http/Controllers/ResourceController.php +++ b/app/Http/Controllers/ResourceController.php @@ -41,7 +41,10 @@ class ResourceController extends Controller public function html(Request $request, $id) { - $graph = $this->querySparql($request, $id); + $graph = $this->querySparql($request, urldecode($id)); + if ($graph->isEmpty()) { + abort(404); + } $subject = key($graph->toRdfPhp()); $datasetConfig = $this->getCurrentDatasetConfig($request); $dataUri = str_replace('{id}', $id, $datasetConfig['data_uri']); From d4aaf6ae00174f6b42d1b5fb7071712be3d70c56 Mon Sep 17 00:00:00 2001 From: Babibubebon Date: Wed, 22 Jan 2020 02:21:48 +0900 Subject: [PATCH 3/4] Improve view and query for describing resource --- app/Http/Controllers/ResourceController.php | 43 +++++++++++------- app/Http/Middleware/DatasetMiddleware.php | 37 +++++++++++++++ bootstrap/app.php | 1 + resources/views/parts/row.blade.php | 34 ++++++++++++++ resources/views/resource.blade.php | 50 ++++++++------------- 5 files changed, 118 insertions(+), 47 deletions(-) create mode 100644 app/Http/Middleware/DatasetMiddleware.php create mode 100644 resources/views/parts/row.blade.php diff --git a/app/Http/Controllers/ResourceController.php b/app/Http/Controllers/ResourceController.php index 5932c7f..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,31 +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, urldecode($id)); if ($graph->isEmpty()) { abort(404); } - $subject = key($graph->toRdfPhp()); - $datasetConfig = $this->getCurrentDatasetConfig($request); - $dataUri = str_replace('{id}', $id, $datasetConfig['data_uri']); 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/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 + + +
    + @if($subject === $primaryTopic) + @foreach($objects as $object) +
  • + @if($object['type'] === 'uri') + {{ $object['value'] }} + @elseif($object['type'] === 'literal') + {{ $object['value'] }} + @if(isset($object['lang'])) + {{ '@'.$object['lang'] }} + @endif + @if(isset($object['datatype'])) + ^^{{ \EasyRdf_Namespace::shorten($object['datatype']) ?? $object['datatype'] }} + @endif + @endif +
  • + @endforeach + @else + {{ $subject }} + @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 }}