diff --git a/README.md b/README.md index 8f92602..6c980ff 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,10 @@ lodfe ===== +SPARQLエンドポイントを用いたLinked Open Dataフロントエンド([Pubby](https://github.com/cygri/pubby)みたいなやつ) -SPARQLエンドポイントを用いたLinked Open Dataフロントエンド +## Installation +``` +$ composer install +$ cp .env{.example,} +$ cp config/datasets.php{.example,} +``` diff --git a/app/Http/Controllers/ResourceController.php b/app/Http/Controllers/ResourceController.php index 5c3182f..8d9a855 100644 --- a/app/Http/Controllers/ResourceController.php +++ b/app/Http/Controllers/ResourceController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Http\Middleware\ContentNegotiatorMiddleware; use Illuminate\Http\Request; class ResourceController extends Controller @@ -64,6 +65,8 @@ class ResourceController extends Controller } catch (\EasyRdf_Exception $e) { abort(400); } - return $data; + $type = ContentNegotiatorMiddleware::mimetypeFromExtension($ext); + return response($data) + ->header('Content-Type', $type); } } diff --git a/app/Http/Middleware/ContentNegotiatorMiddleware.php b/app/Http/Middleware/ContentNegotiatorMiddleware.php index 733d5fe..de481bf 100644 --- a/app/Http/Middleware/ContentNegotiatorMiddleware.php +++ b/app/Http/Middleware/ContentNegotiatorMiddleware.php @@ -9,23 +9,34 @@ class ContentNegotiatorMiddleware { public $defaultType = 'text/html'; - public $acceptableTypes = [ + public static $acceptableTypes = [ 'text/html' => 'html', 'application/xhtml+xml' => 'html', 'text/n3' => 'data', 'text/turtle' => 'data', 'application/n-triples' => 'data', 'application/rdf+xml' => 'data', + 'application/json' => 'data', + 'application/ld+json' => 'data', ]; - public $fileExtensions = [ + public static $fileExtensions = [ 'text/html' => '.html', 'application/xhtml+xml' => '.html', 'text/turtle' => '.ttl', 'application/n-triples' => '.nt', 'application/rdf+xml' => '.rdf', + 'application/json' => '.json', + 'application/ld+json' => '.jsonld', ]; + public static function mimetypeFromExtension($ext) + { + $extensionToMimetype = array_flip(self::$fileExtensions); + return array_key_exists($ext, $extensionToMimetype) + ? $extensionToMimetype[$ext] : null; + } + /** * Handle an incoming request. * @@ -53,15 +64,15 @@ class ContentNegotiatorMiddleware $id = $request->route('id'); $negotiatedType = $this->defaultType; foreach ($accepts as $mime => $q) { - if (array_key_exists($mime, $this->acceptableTypes)) { + if (array_key_exists($mime, self::$acceptableTypes)) { $negotiatedType = $mime; } } - $redirectTo = $this->acceptableTypes[$negotiatedType] . '.' . $datasetName; + $redirectTo = self::$acceptableTypes[$negotiatedType] . '.' . $datasetName; $params = ['id' => $id]; if (substr($redirectTo, 0, 4) === 'data') { - $params['ext'] = $this->fileExtensions[$negotiatedType]; + $params['ext'] = self::$fileExtensions[$negotiatedType]; } return redirect()->route($redirectTo, $params, 303); } diff --git a/bootstrap/app.php b/bootstrap/app.php index 4dbb9ae..c669c62 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -27,6 +27,12 @@ $app->withFacades(); $app->configure('app'); $app->configure('datasets'); +$app->configure('namespaces'); + +// Register namespaces +foreach (config('namespaces') as $prefix => $uri) { + EasyRdf_Namespace::set($prefix, $uri); +} /* |-------------------------------------------------------------------------- diff --git a/config/app.php b/config/app.php index 472b18c..f6b34cc 100644 --- a/config/app.php +++ b/config/app.php @@ -2,7 +2,7 @@ return [ - 'version' => '1.0.0', + 'version' => '1.1.0', /* |-------------------------------------------------------------------------- diff --git a/config/namespaces.php b/config/namespaces.php new file mode 100644 index 0000000..2d4638a --- /dev/null +++ b/config/namespaces.php @@ -0,0 +1,5 @@ + 'https://metadata.moe/ns/madb/ma#', + 'madbdata' => 'https://metadata.moe/ns/madb/madbdata#', +]; diff --git a/resources/views/resource.blade.php b/resources/views/resource.blade.php index 70181e4..2dbacdb 100644 --- a/resources/views/resource.blade.php +++ b/resources/views/resource.blade.php @@ -65,4 +65,8 @@ @endforeach @endforeach + + @endsection diff --git a/routes/web.php b/routes/web.php index a8ae9bc..dbd4253 100644 --- a/routes/web.php +++ b/routes/web.php @@ -10,7 +10,9 @@ | and give it the Closure to call when that URI is requested. | */ - +if ($_SERVER['PHP_SELF'] === 'artisan') { + return; +} function joinHostPort($components) {