From 8828f4f3d3449363b40cd4f63410eee97c795444 Mon Sep 17 00:00:00 2001 From: Emerson Rocha Date: Thu, 29 Sep 2022 19:06:12 -0300 Subject: [PATCH] (#2), (#3): move actual schemas to /_/meta folder (not indented for direct access) --- README.md | 6 ++ app/.htaccess | 8 +- app/lib/urnresolver.php | 101 +++++++++++++----- app/public/{ => _}/meta/README.md | 4 +- app/public/{ => _}/meta/test-api-online.json | 0 app/public/{ => _}/meta/test-api.json | 0 .../meta/urnresolver-api.context.jsonld} | 16 ++- app/public/_/meta/urnresolver-api.schema.json | 33 ++++++ .../{ => _}/meta/urnresolver-urnr.schema.json | 0 app/public/index.php | 2 +- app/public/meta/urnresolver.schema.json | 5 - 11 files changed, 138 insertions(+), 37 deletions(-) rename app/public/{ => _}/meta/README.md (83%) rename app/public/{ => _}/meta/test-api-online.json (100%) rename app/public/{ => _}/meta/test-api.json (100%) rename app/public/{meta/urnresolver.context.jsonld => _/meta/urnresolver-api.context.jsonld} (66%) create mode 100644 app/public/_/meta/urnresolver-api.schema.json rename app/public/{ => _}/meta/urnresolver-urnr.schema.json (100%) delete mode 100644 app/public/meta/urnresolver.schema.json diff --git a/README.md b/README.md index bd4d6ee..896d575 100644 --- a/README.md +++ b/README.md @@ -289,6 +289,12 @@ Check [resolvers](resolvers/) folder. - https://json-ld.org/playground/ - https://json-ld.org/spec/latest/json-ld/#nested-properties - https://www.jsonschemavalidator.net/ + +### Logos +- https://github.com/json-api/json-api/issues/1655 +- https://json-ld.org/images/ +- https://www.w3.org/RDF/icons/ +- https://www.hydra-cg.com/spec/latest/core/ (maybe) --> ## License diff --git a/app/.htaccess b/app/.htaccess index ce9d1fa..7e09592 100644 --- a/app/.htaccess +++ b/app/.htaccess @@ -16,11 +16,17 @@ Header add Access-Control-Allow-Methods: "GET,POST,HEAD,OPTIONS,DELETE,PUT" Options +FollowSymlinks Options -Indexes +# Alias /urn:resolver:meta:context:api public/_/meta/urnresolver-api.context.jsonld +# Alias /urn:resolver:meta:context:urnr public/_/meta/urnresolver-urnr.schema.json + RewriteEngine On # RewriteBase / -RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] +# RewriteRule ^urn:resolver:meta:context:urnr$ public/_/meta/urnresolver-urnr.schema.json [L,NC] +# RewriteRule ^urnresolver-urnr$ public/_/meta/urnresolver-urnr.schema.json [NC] + +# RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # # If the requested path and file is not /index.php and the request # has not already been internally rewritten to the index.php script diff --git a/app/lib/urnresolver.php b/app/lib/urnresolver.php index 18338a2..9f252d2 100644 --- a/app/lib/urnresolver.php +++ b/app/lib/urnresolver.php @@ -8,6 +8,10 @@ define("ROOT_PATH", dirname(dirname(__FILE__))); define("RESOLVER_RULE_PATH", ROOT_PATH . '/public/.well-known/urn'); +$global_conf = new Config(); + +define("URNRESOLVER_BASE", $global_conf->base_iri); + // https://www.php-fig.org/psr/psr-12/ function debug() @@ -28,6 +32,25 @@ function debug() return $info; } +/** + * Pretty print JSON (2 spaces and newline) + * + * @param object $data + * @return string + */ +function to_json($data) +{ + $json_string_4spaces = json_encode( + $data, + JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE + ); + // https://stackoverflow.com/a/31689850/894546 + $json_string = preg_replace_callback('/^ +/m', function ($m) { + return str_repeat(' ', strlen($m[0]) / 2); + }, $json_string_4spaces); + return $json_string . "\n"; +} + class Config { public $global_conf; @@ -130,9 +153,13 @@ public function execute_output_2xx( // header("Access-Control-Allow-Origin: *"); $result = [ - '$schema' => 'https://jsonapi.org/schema', - '$id' => $base, - '@context' => 'https://urn.etica.ai/urnresolver-context.jsonld', + // '$schema' => 'https://jsonapi.org/schema', + // @TODO make this also an URN (with htaccess rewirte for performance reason) + '$schema' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.schema.json', + // '$id' => $base, + // '@context' => 'https://urn.etica.ai/urnresolver-context.jsonld', + '@context' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.context.jsonld', + '@id' => $base, 'data' => $data, 'meta' => [ '@type' => 'schema:Message', @@ -146,7 +173,8 @@ public function execute_output_2xx( ] ]; - echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + // echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + echo to_json($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); die(); } @@ -165,9 +193,13 @@ public function execute_output_4xx( // header("Access-Control-Allow-Origin: *"); $result = [ - '$schema' => 'https://jsonapi.org/schema', - '$id' => $base, - '@context' => 'https://urn.etica.ai/urnresolver-context.jsonld', + // '$schema' => 'https://jsonapi.org/schema', + // @TODO make this also an URN (with htaccess rewirte for performance reason) + '$schema' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.schema.json', + // '$id' => $base, + // '@context' => 'https://urn.etica.ai/urnresolver-context.jsonld', + '@context' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.context.jsonld', + '@id' => $base, 'error' => [ 'status' => $http_status_code, 'title' => $http_status_msg, @@ -182,7 +214,8 @@ public function execute_output_4xx( ] ]; - echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + // echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + echo to_json($result); die(); } @@ -204,9 +237,13 @@ public function execute_output_5xx( // header("Access-Control-Allow-Origin: *"); $result = [ - '$schema' => 'https://jsonapi.org/schema', - '$id' => $base, - '@context' => 'https://urn.etica.ai/urnresolver-context.jsonld', + // '$schema' => 'https://jsonapi.org/schema', + // @TODO make this also an URN (with htaccess rewirte for performance reason) + '$schema' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.schema.json', + // '$id' => $base, + // '@context' => 'https://urn.etica.ai/urnresolver-context.jsonld', + '@context' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.context.jsonld', + '@id' => $base, 'error' => [ 'status' => $http_status_code, 'title' => $http_status_msg, @@ -224,7 +261,8 @@ public function execute_output_5xx( ] ]; - echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + // echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + echo to_json($result); die(); } @@ -578,13 +616,18 @@ public function execute_welcome() } $result = [ - '$schema' => 'https://jsonapi.org/schema', - '$id' => $this->active_base . $this->active_uri, - '@context' => 'https://urn.etica.ai/urnresolver-context.jsonld', + // '$schema' => 'https://jsonapi.org/schema', + // @TODO make this also an URN (with htaccess rewirte for performance reason) + '$schema' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.schema.json', + // '$id' => $base, + // '@context' => 'https://urn.etica.ai/urnresolver-context.jsonld', + // '@id' => $base, + '@id' => URNRESOLVER_BASE, // home page, display the site URL + '@context' => URNRESOLVER_BASE . '/_/meta/urnresolver-api.context.jsonld', '@type' => 'schema:Action', - 'schema:endTime' => date("c"), + // 'schema:endTime' => date("c"), 'data' => [ - 'type' => "schema:Action", + // 'type' => "schema:Action", 'id' => $this->config->base_iri, 'relationships' => [ 'vurn:resolver:index' => [ @@ -610,21 +653,23 @@ public function execute_welcome() // 'links' => [ // 'uptime' => 'https://stats.uptimerobot.com/jYDZlFY8jq' // ], - // 'meta' => [ - // '@type' => 'schema:Message', - // 'schema:name' => 'URN Resolver', - // 'schema:dateCreated' => date("c"), - // // 'schema:mainEntityOfPage' => 'https://github.com/EticaAI/urn-resolver', - // "schema:potentialAction" => [ - // "schema:name" => "uptime", - // "schema:url" => "https://stats.uptimerobot.com/jYDZlFY8jq" - // ] - // ] + 'meta' => [ + '@type' => 'schema:Message', + 'schema:name' => 'URN Resolver', + // 'schema:dateCreated' => date("c"), + 'schema:endTime' => date("c"), + // 'schema:mainEntityOfPage' => 'https://github.com/EticaAI/urn-resolver', + "schema:potentialAction" => [ + "schema:name" => "uptime", + "schema:url" => "https://stats.uptimerobot.com/jYDZlFY8jq" + ] + ] ]; http_response_code(200); // $result->_debug['_router'] = $this->meta(); - echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + // echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + echo to_json($result); die(); } diff --git a/app/public/meta/README.md b/app/public/_/meta/README.md similarity index 83% rename from app/public/meta/README.md rename to app/public/_/meta/README.md index 5de7595..def66b5 100644 --- a/app/public/meta/README.md +++ b/app/public/_/meta/README.md @@ -1,13 +1,15 @@ -# URN Resolver / Meta +# URN Resolver (app) / _ / meta ## Files ### urnresolver.context.jsonld > - [urnresolver.context.jsonld](urnresolver.context.jsonld) +> - Ideal path to serve: `{resolver}/urn:resolver:meta:context:api` Prefixes uses https://github.com/w3c/json-ld-rc/ (https://w3c.github.io/json-ld-rc/context.jsonld) as reference ### urnresolver.schema.json > - [urnresolver.schema.json](urnresolver.schema.json) +> - Ideal path to serve: `{resolver}/urn:resolver:meta:context:urnr` ## Relation to other APIs diff --git a/app/public/meta/test-api-online.json b/app/public/_/meta/test-api-online.json similarity index 100% rename from app/public/meta/test-api-online.json rename to app/public/_/meta/test-api-online.json diff --git a/app/public/meta/test-api.json b/app/public/_/meta/test-api.json similarity index 100% rename from app/public/meta/test-api.json rename to app/public/_/meta/test-api.json diff --git a/app/public/meta/urnresolver.context.jsonld b/app/public/_/meta/urnresolver-api.context.jsonld similarity index 66% rename from app/public/meta/urnresolver.context.jsonld rename to app/public/_/meta/urnresolver-api.context.jsonld index 32472dd..4ce4d0b 100644 --- a/app/public/meta/urnresolver.context.jsonld +++ b/app/public/_/meta/urnresolver-api.context.jsonld @@ -15,6 +15,20 @@ "schema": "http://schema.org/", "skos": "http://www.w3.org/2004/02/skos/core#", "xml": "http://www.w3.org/XML/1998/namespace", - "xsd": "http://www.w3.org/2001/XMLSchema#" + "xsd": "http://www.w3.org/2001/XMLSchema#", + "hydra": "http://www.w3.org/ns/hydra/core#", + "data": "@nest", + "error": { + "@id": "hydra:Error" + }, + "status": { + "@id": "hydra:statusCode", + "@nest": "error" + }, + "title": { + "@id": "hydra:Status", + "@nest": "error" + }, + "$schema": null } } diff --git a/app/public/_/meta/urnresolver-api.schema.json b/app/public/_/meta/urnresolver-api.schema.json new file mode 100644 index 0000000..feec757 --- /dev/null +++ b/app/public/_/meta/urnresolver-api.schema.json @@ -0,0 +1,33 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "urn:resolver:meta:schema:api", + "title": "URN Resolver JSON Schema", + "description": "Early draft. See https://github.com/EticaAI/urn-resolver/issues/2", + "type": "object", + "additionalProperties": false, + "patternProperties": { + "^@": { + "type": [ + "string", + "object" + ] + } + }, + "properties": { + "$id": { + "type": "string" + }, + "$schema": { + "type": "string" + }, + "data": { + "type": ["array", "object"] + }, + "meta": { + "type": "object" + }, + "error": { + "type": "object" + } + } +} diff --git a/app/public/meta/urnresolver-urnr.schema.json b/app/public/_/meta/urnresolver-urnr.schema.json similarity index 100% rename from app/public/meta/urnresolver-urnr.schema.json rename to app/public/_/meta/urnresolver-urnr.schema.json diff --git a/app/public/index.php b/app/public/index.php index d57f65f..b38d43c 100644 --- a/app/public/index.php +++ b/app/public/index.php @@ -55,7 +55,7 @@ // echo $t->getMessage(), " at ", $t->getFile(), ":", $t->getLine(), "\n"; http_response_code(500); header("Content-type: application/json; charset=utf-8"); - echo json_encode( $data, JSON_PRETTY_PRINT); + echo json_encode($data); die; } diff --git a/app/public/meta/urnresolver.schema.json b/app/public/meta/urnresolver.schema.json deleted file mode 100644 index 891d96c..0000000 --- a/app/public/meta/urnresolver.schema.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "title": "URN Resolver JSON Schema", - "description": "Early draft. See https://github.com/EticaAI/urn-resolver/issues/2" -}