Metadata validation
This commit is contained in:
parent
3719c4b20c
commit
eac4b246d7
|
@ -34,6 +34,11 @@ public function __construct(protected string $filename, protected Bundle $bundle
|
|||
$this->load();
|
||||
}
|
||||
|
||||
public function getFilename(): string
|
||||
{
|
||||
return $this->filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads metadata from disk using the disk, with caching to improve performance.
|
||||
*/
|
||||
|
@ -151,7 +156,7 @@ public function all()
|
|||
*/
|
||||
public function remove($key)
|
||||
{
|
||||
collect($this->content)->forget($key);
|
||||
$this->content = collect($this->content)->forget($key)->all();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -34,60 +34,49 @@ public function handle()
|
|||
foreach ($bundles as $bundle) {
|
||||
$this->output->write(sprintf('Upgrading %s... ', $bundle->getPath()));
|
||||
|
||||
$disk->move($bundle->getPath() . 'images', $bundle->getDataDir() . 'attachments/images');
|
||||
$disk->move($bundle->getPath() . 'sounds', $bundle->getDataDir() . 'attachments/sounds');
|
||||
$disk->move($bundle->getPath() . 'videos', $bundle->getDataDir() . 'attachments/videos');
|
||||
$disk->move($bundle->getPath() . 'index.json', $bundle->getDataDir() . 'index.json');
|
||||
$disk->move($bundle->getPath() . 'metadata.json', $bundle->getDataDir() . 'metadata.json');
|
||||
$currentKeywords = $bundle->metadata('metadata')->get('miscKeywords');
|
||||
|
||||
if ($disk->exists($bundle->getPath() . 'attachments.json')) {
|
||||
$attachments = json_decode($disk->get($bundle->getPath() . 'attachments.json'), true);
|
||||
$images = [];
|
||||
$sounds = [];
|
||||
$videos = [];
|
||||
|
||||
foreach ($attachments as $ref => $data) {
|
||||
$kind = $data['kind'];
|
||||
|
||||
$data['filename'] = 'attachments/' . $data['url'];
|
||||
|
||||
unset($data['url']);
|
||||
unset($data['kind']);
|
||||
|
||||
switch ($kind) {
|
||||
case 'images':
|
||||
$images['files'][$ref] = $data;
|
||||
break;
|
||||
case 'sounds':
|
||||
$sounds['files'][$ref] = $data;
|
||||
break;
|
||||
case 'videos':
|
||||
$videos['files'][$ref] = $data;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($images)) {
|
||||
$disk->put($bundle->getDataDir() . 'images.json', json_encode($images, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
if (!empty($sounds)) {
|
||||
$disk->put($bundle->getDataDir() . 'sounds.json', json_encode($sounds, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
if (!empty($videos)) {
|
||||
$disk->put($bundle->getDataDir() . 'videos.json', json_encode($videos, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
if (!empty($currentKeywords)) {
|
||||
$bundle->metadata('metadata')->set('Mots-clé', $currentKeywords);
|
||||
$bundle->metadata('metadata')->remove('miscKeywords');
|
||||
}
|
||||
|
||||
$filesToDelete = [
|
||||
'links.json',
|
||||
'attachments.json',
|
||||
];
|
||||
$currentPeople = $bundle->metadata('metadata')->get('miscPeople');
|
||||
|
||||
$filesToDelete = array_map(fn ($filename) => $bundle->getPath() . $filename, $filesToDelete);
|
||||
if (!empty($currentPeople)) {
|
||||
$bundle->metadata('metadata')->set('Personnalités', $currentPeople);
|
||||
$bundle->metadata('metadata')->remove('miscPeople');
|
||||
}
|
||||
|
||||
$disk->delete($filesToDelete);
|
||||
$currentLinks = $bundle->metadata('metadata')->get('links');
|
||||
|
||||
if (!empty($currentLinks)) {
|
||||
$bundle->metadata('metadata')->set('Liens', $currentLinks);
|
||||
$bundle->metadata('metadata')->remove('links');
|
||||
}
|
||||
|
||||
$currentDetails = $bundle->metadata('metadata')->get('details');
|
||||
|
||||
if (!empty($currentDetails)) {
|
||||
$bundle->metadata('metadata')->set('Détails', $currentDetails);
|
||||
$bundle->metadata('metadata')->remove('details');
|
||||
}
|
||||
|
||||
$currentTheme = $bundle->metadata('metadata')->get('theme');
|
||||
|
||||
if (!empty($currentTheme)) {
|
||||
$bundle->metadata('metadata')->set('Thème', $currentTheme);
|
||||
$bundle->metadata('metadata')->remove('theme');
|
||||
}
|
||||
|
||||
$currentSagas = $bundle->metadata('metadata')->get('sagas');
|
||||
|
||||
if (!empty($currentSagas)) {
|
||||
$bundle->metadata('metadata')->set('Sagas.Fait partie de', $currentSagas);
|
||||
$bundle->metadata('metadata')->remove('sagas');
|
||||
}
|
||||
|
||||
$bundle->save();
|
||||
|
||||
$this->info('OK');
|
||||
}
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands\Bundle;
|
||||
|
||||
use App\Classes\Bundle;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use JsonSchema\Constraints\Constraint;
|
||||
use JsonSchema\Validator;
|
||||
|
||||
class Validate extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'bundle:validate { path? : Specific bundle to repair }';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Validate bundles metadata';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$path = $this->argument('path') ?? '/';
|
||||
$bundles = Bundle::findBundles(Storage::disk(env('CONTENT_DISK')), $path, true);
|
||||
|
||||
$validators = [
|
||||
'index' => json_decode(file_get_contents(resource_path('schemas/index.json'))),
|
||||
'metadata' => json_decode(file_get_contents(resource_path('schemas/metadata.json'))),
|
||||
];
|
||||
|
||||
$allValid = true;
|
||||
|
||||
foreach ($bundles as $bundle) {
|
||||
$this->output->write(sprintf('Validating %s... ', $bundle->getPath()));
|
||||
|
||||
$isValid = false;
|
||||
|
||||
foreach ($validators as $file => $schema) {
|
||||
$data = json_decode(Storage::disk(env('CONTENT_DISK'))->get($bundle->metadata($file)->getFilename()) ?? '{}');
|
||||
$validator = new Validator();
|
||||
|
||||
$validator->validate($data, $schema, Constraint::CHECK_MODE_APPLY_DEFAULTS);
|
||||
|
||||
if ($validator->isValid()) {
|
||||
$isValid = true;
|
||||
} else {
|
||||
$isValid = false;
|
||||
$allValid = false;
|
||||
|
||||
$this->error(sprintf('%s INVALID', $file));
|
||||
|
||||
foreach ($validator->getErrors() as $error) {
|
||||
$this->line(" $error[message]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($isValid) {
|
||||
$this->info('OK');
|
||||
}
|
||||
}
|
||||
|
||||
$this->newLine();
|
||||
|
||||
if ($allValid) {
|
||||
$this->info('All bundles are valid');
|
||||
} else {
|
||||
$this->comment('Errors have been reported');
|
||||
}
|
||||
}
|
||||
}
|
|
@ -60,12 +60,12 @@ public function createBundle(): string
|
|||
]);
|
||||
|
||||
$bundle->metadata('metadata')->setMany([
|
||||
'details' => [
|
||||
'Détails' => [
|
||||
'Identifiant' => $this->data['product_id'],
|
||||
'Date de sortie' => $setData['year'],
|
||||
'Nombre de pièces' => $setData['num_parts'],
|
||||
],
|
||||
'theme' => [
|
||||
'Thème' => [
|
||||
$theme['name'],
|
||||
],
|
||||
]);
|
||||
|
|
|
@ -55,7 +55,7 @@ public function createBundle(): string
|
|||
'cover' => $ref,
|
||||
]);
|
||||
|
||||
$bundle->metadata('metadata')->set('links.Page originale', [$url]);
|
||||
$bundle->metadata('metadata')->set('Liens.Page originale', [$url]);
|
||||
|
||||
$bundle->markdown()->set('> ' . $description . "\n\n");
|
||||
|
||||
|
|
|
@ -56,12 +56,12 @@ public function update()
|
|||
]);
|
||||
|
||||
$this->bundle->metadata('metadata')->setMany([
|
||||
'details' => [
|
||||
'Détails' => [
|
||||
'Identifiant' => $productId,
|
||||
'Date de sortie' => $setData['year'],
|
||||
'Nombre de pièces' => $setData['num_parts'],
|
||||
],
|
||||
'theme' => [
|
||||
'Thème' => [
|
||||
$theme['name'],
|
||||
],
|
||||
]);
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
"require": {
|
||||
"php": "^8.2",
|
||||
"intervention/image-laravel": "^1.2",
|
||||
"justinrainbow/json-schema": "^5.2",
|
||||
"laravel/framework": "^11.0",
|
||||
"laravel/telescope": "^5.0",
|
||||
"laravel/tinker": "^2.9",
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "06eb655331a9c10a307b3302e9fbcfc2",
|
||||
"content-hash": "119e9381a7ab60f03e5267f48b27c1e5",
|
||||
"packages": [
|
||||
{
|
||||
"name": "brick/math",
|
||||
|
@ -1258,6 +1258,76 @@
|
|||
],
|
||||
"time": "2024-03-03T09:14:35+00:00"
|
||||
},
|
||||
{
|
||||
"name": "justinrainbow/json-schema",
|
||||
"version": "v5.2.13",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/justinrainbow/json-schema.git",
|
||||
"reference": "fbbe7e5d79f618997bc3332a6f49246036c45793"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/justinrainbow/json-schema/zipball/fbbe7e5d79f618997bc3332a6f49246036c45793",
|
||||
"reference": "fbbe7e5d79f618997bc3332a6f49246036c45793",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.3.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "~2.2.20||~2.15.1",
|
||||
"json-schema/json-schema-test-suite": "1.2.0",
|
||||
"phpunit/phpunit": "^4.8.35"
|
||||
},
|
||||
"bin": [
|
||||
"bin/validate-json"
|
||||
],
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "5.0.x-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"JsonSchema\\": "src/JsonSchema/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Bruno Prieto Reis",
|
||||
"email": "bruno.p.reis@gmail.com"
|
||||
},
|
||||
{
|
||||
"name": "Justin Rainbow",
|
||||
"email": "justin.rainbow@gmail.com"
|
||||
},
|
||||
{
|
||||
"name": "Igor Wiedler",
|
||||
"email": "igor@wiedler.ch"
|
||||
},
|
||||
{
|
||||
"name": "Robert Schönthal",
|
||||
"email": "seroscho@googlemail.com"
|
||||
}
|
||||
],
|
||||
"description": "A library to validate a json schema.",
|
||||
"homepage": "https://github.com/justinrainbow/json-schema",
|
||||
"keywords": [
|
||||
"json",
|
||||
"schema"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/justinrainbow/json-schema/issues",
|
||||
"source": "https://github.com/justinrainbow/json-schema/tree/v5.2.13"
|
||||
},
|
||||
"time": "2023-09-26T02:20:38+00:00"
|
||||
},
|
||||
{
|
||||
"name": "laravel/framework",
|
||||
"version": "v11.3.1",
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "Bundle metadata",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {
|
||||
"type": "string"
|
||||
},
|
||||
"hideTitle": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"subTitle": {
|
||||
"type": "string"
|
||||
},
|
||||
"date": {
|
||||
"type": "string",
|
||||
"pattern": "^(\\d{4}-\\d{2}-\\d{2}(T\\d{2}:\\d{2}:\\d{2}(\\+|-)\\d{2}:\\d{2})?)$"
|
||||
},
|
||||
"coauthor": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"name"
|
||||
]
|
||||
}
|
||||
},
|
||||
"cover": {
|
||||
"type": "string"
|
||||
},
|
||||
"logo": {
|
||||
"type": "string"
|
||||
},
|
||||
"virtualMetadata": {
|
||||
"type": "array"
|
||||
},
|
||||
"wikidataEntityId": {
|
||||
"type": "string"
|
||||
},
|
||||
"rebrickableId": {
|
||||
"type": "string"
|
||||
},
|
||||
"productId": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"title"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "Bundle extended metadata",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Liens": {
|
||||
"type": "object",
|
||||
"items": {
|
||||
"type": "array"
|
||||
}
|
||||
},
|
||||
"Mots-clé": {},
|
||||
"Personnalités": {},
|
||||
"Détails": {},
|
||||
"Sagas": {},
|
||||
"Thème": {}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
Loading…
Reference in New Issue