Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UPM importer #1043

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/Enums/FrontendEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ enum FrontendEnum: string
{
case WEBUMENIA = 'webumenia';
case MORAVSKA_GALERIE = 'moravska-galerie';
case UPM = 'upm';
}
2 changes: 1 addition & 1 deletion app/Helpers/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ function convertEmptyStringsToNull($array) {
}

function formatName($name) {
return preg_replace('/^([^,]*),\s*(.*)$/', '$2 $1', $name);
return preg_replace('/^([^,]*),\s*(.*)$/', '$2 $1', $name);
}

function starts_with_upper($str) {
Expand Down
8 changes: 8 additions & 0 deletions app/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ class Import extends Model
'completed_at' => 'datetime',
];

protected $fillable = [
'name',
'dir_path',
'iip_dir_path',
'class_name',
'disk',
];

public function user()
{
return $this->belongsTo(User::class);
Expand Down
227 changes: 227 additions & 0 deletions app/Importers/UpmImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
<?php

namespace App\Importers;

use App\Enums\FrontendEnum;

class UpmImporter extends AbstractImporter
{
protected $mapping = [
'identifier' => 'Inventárníčíslo',
'title:cs' => 'Název',
'title:sk' => 'Název',
'title:en' => 'Název EN',
'dating:cs' => 'Datace',
'dating:sk' => 'Datace',
'date_earliest' => 'Od',
'date_latest' => 'Do',
'inscription:cs' => 'Značení',
'inscription:sk' => 'Značení',
'related_work:sk' => 'Sbírka',
'related_work:cs' => 'Sbírka',
'acquisition_date' => 'Datum akvizice',
];

protected $defaults = [
'relationship_type:sk' => 'zo súboru',
'relationship_type:cs' => 'ze souboru',
'relationship_type:en' => 'collection',
'gallery:cs' => 'Uměleckoprůmyslové museum v Praze, UPM',
'gallery:sk' => 'Umeleckopriemyselné múzeum v Prahe, UPM',
'frontends' => [
FrontendEnum::UPM,
FrontendEnum::WEBUMENIA,
],
];

protected static $options = [
'delimiter' => ';',
'enclosure' => '"',
'escape' => '\\',
'newline' => "\n",
];

private array $workTypeTranslationKeys;
private array $techniqueTranslationKeys;
private array $mediumTranslationKeys;
private array $objectTypeTranslationKeys;
private array $topicTranslationKeys;


protected function init()
{
$this->sanitizers[] = function ($value) {
return empty_to_null(trim($value));
};

$this->workTypeTranslationKeys = array_flip(trans('item.work_types', locale: 'cs'));
$this->techniqueTranslationKeys = array_flip(trans('item.techniques', locale: 'cs'));
$this->mediumTranslationKeys = array_flip(trans('item.media', locale: 'cs'));
$this->objectTypeTranslationKeys = array_flip(trans('item.object_types', locale: 'cs'));
$this->topicTranslationKeys = array_flip(trans('item.topics', locale: 'cs'));
}

protected function getItemId(array $record)
{
return 'CZE:UPM.' . str($record['ID'])
->explode('_')
->transform(fn ($part) => str($part)
->replaceMatches('/\W/', '-')
->trim('-')
)
->join('_');
}

protected function getItemImageFilenameFormat(array $record): string
{
return str($record['ID'])
->explode('_')
->transform(fn ($part) => '0*' . preg_quote($part))
->join('_') . '(_.*)?';
}

protected function hydrateAuthor(array $record): string
{
$authors = str($record['Autor']);
if ($authors->isEmpty()) {
return 'Neznámý autor';
}

return $authors
->split('/\s*;\s*/')
->map(function (string $author) {
preg_match('/^(?<name>[^–]*)(\s–\s(?<role>.*))?$/', $author, $matches);

if (!isset($matches['name'], $matches['role'])) {
return $author;
}

return sprintf('%s – %s', formatName($matches['name']), $matches['role']);
})
->join('; ');
}

protected function hydrateWorkType(array $record, string $locale): ?string
{
if ($locale === 'cs') {
return $record['Výtvarný druh'];
}

return str($record['Výtvarný druh'])
->split('/\s*;\s*/')
->map(function (string $workType) use ($locale) {
$key = $this->workTypeTranslationKeys[$workType] ?? null;
return $key ? trans("item.work_types.$key", locale: $locale) : null;
})
->filter()
->join('; ') ?: null;
}

protected function hydrateTechnique(array $record, string $locale): ?string
{
if ($locale === 'cs') {
return $record['Technika'];
}

return str($record['Technika'])
->split('/\s*;\s*/')
->map(function (string $technique) use ($locale) {
$key = $this->techniqueTranslationKeys[$technique] ?? null;
return $key ? trans("item.techniques.$key", locale: $locale) : null;
})
->filter()
->join('; ') ?: null;
}

protected function hydrateMedium(array $record, string $locale): ?string
{
if ($locale === 'cs') {
return $record['Materiál'];
}

return str($record['Materiál'])
->split('/\s*;\s*/')
->map(function (string $medium) use ($locale) {
$key = $this->mediumTranslationKeys[$medium] ?? null;
return $key ? trans("item.media.$key", locale: $locale) : null;
})
->filter()
->join('; ') ?: null;
}

protected function hydrateObjectType(array $record, string $locale): ?string
{
if ($locale === 'cs') {
return $record['Typ'];
}

return str($record['Typ'])
->split('/\s*;\s*/')
->map(function (string $objectType) use ($locale) {
$key = $this->objectTypeTranslationKeys[$objectType] ?? null;
return $key ? trans("item.object_types.$key", locale: $locale) : null;
})
->filter()
->join('; ') ?: null;
}

protected function hydrateTopic(array $record, string $locale): ?string
{
if ($locale === 'cs') {
return $record['Námět'];
}

return str($record['Námět'])
->split('/\s*;\s*/')
->map(function (string $topic) use ($locale) {
$key = $this->topicTranslationKeys[$topic] ?? null;
return $key ? trans("item.topics.$key", locale: $locale) : null;
})
->filter()
->join('; ') ?: null;
}

protected function hydratePlace(array $record, string $locale): ?string
{
if (!in_array($locale, ['cs', 'sk'])) {
return null;
}

$place = str($record['Vznik'])->match('/^([^;]+)/');
return $place->isNotEmpty() ? $place->toString() : null;
}

protected function hydrateAdditionals(array $record, string $locale): ?array
{
if ($locale !== 'cs') {
return null;
}

$additionals = [];

if ($record['Způsob akvizice'] !== null) {
$additionals['acquisition'] = $record['Způsob akvizice'];
}

if ($record['Výstava'] !== null) {
$additionals['exhibition'] = $record['Výstava'];
}

$producer = str($record['Vznik'])->match('/;(.+)/');
if ($producer->isNotEmpty()) {
$additionals['producer'] = $producer->toString();
}

return $additionals ?: null;
}

protected function hydrateMeasurement(array $record, $locale): ?string
{
if (empty($record['Rozměry'])) {
return null;
}

$replacements = trans('item.measurement_replacements', [], $locale);
return strtr($record['Rozměry'], $replacements);
}
}
1 change: 1 addition & 0 deletions app/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class Item extends Model implements IndexableModel, TranslatableContract
'exhibition',
'box',
'location',
'object_type',
];

public static $rules = array(
Expand Down
4 changes: 2 additions & 2 deletions app/Matchers/AuthorityMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ protected function findByFullname($fullname)

public static function parse($author)
{
if (!preg_match('/^((?<surname>.*?)\s*(?<alt_surname>\(.*\))?(?<role>\s+-\s+.*)?,\s+)?(?<name>.*?)\s*(?<alt_name>\(.*\)|\/.*)?$/', $author, $matches)) {
if (!preg_match('/^((?<surname>.*?)\s*(?<alt_surname>\(.*\))?(?<role>\s+[-–]\s+.*)?,\s+)?(?<name>.*?)\s*(?<alt_name>\(.*\)|\/.*)?$/', $author, $matches)) {
return null;
}

return [
'surname' => $matches['surname'] ?: null,
'alt_surname' => trim($matches['alt_surname'], '()') ?: null,
'role' => ltrim($matches['role'], ' -') ?: null,
'role' => ltrim($matches['role'], ' -') ?: null,
'name' => $matches['name'] ?: null,
'alt_name' => trim($matches['alt_name'] ?? '', '()/') ?: null,
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('item_translations', function (Blueprint $table) {
$table->string('title', 512)->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('item_translations', function (Blueprint $table) {
$table->string('title')->change();
});
}
};
1 change: 1 addition & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function run()
$this->call(CollectionsTableSeeder::class);
$this->call(CategoriesTableSeeder::class);
$this->call(ArticlesTableSeeder::class);
$this->call(ImportsTableSeeder::class);

// $this->call(SketchbooksTableSeeder::class);
}
Expand Down
21 changes: 21 additions & 0 deletions database/seeders/ImportsTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Database\Seeders;

use App\Import;
use App\Importers\UpmImporter;
use Illuminate\Database\Seeder;

class ImportsTableSeeder extends Seeder
{
public function run()
{
Import::create([
'name' => 'UPM',
'dir_path' => 'UPM',
'iip_dir_path' => 'UPM',
'class_name' => UpmImporter::class,
'disk' => 'import_iip',
]);
}
}
Loading