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

Webumenia 2108 with upm 3 #1046

Closed
wants to merge 14 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
74 changes: 74 additions & 0 deletions app/Console/Commands/DumpUntranslatedAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace App\Console\Commands;

use App\ItemTranslation;
use Illuminate\Console\Command;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Str;
use League\Csv\Writer;

class DumpUntranslatedAttributes extends Command
{
protected $signature = 'items:dump-untranslated-attributes
{--prefix= : Filter by prefix}
{--output= : Output to CSV file}';

private array $attributes = [
'work_type' => 'item.work_types',
'technique' => 'item.techniques',
'medium' => 'item.media',
'object_type' => 'item.object_types',
'topic' => 'item.topics',
];

public function handle(): int
{
$attribute = $this->choice('Select attribute', array_keys($this->attributes));
$locale = $this->choice('Select locale', config('translatable.locales'));
$prefix = $this->option('prefix');
$output = $this->option('output');

$rows = $this->getUntranslatedAttributes($attribute, $locale, $prefix)
->map(fn ($count, $value) => [$value, $count]);

$header = [
sprintf('%s (%s)', Str::ucfirst(trans("item.$attribute")), Str::upper($locale)),
'Count',
];

if ($output) {
$writer = Writer::createFromPath($output, 'w+');
$writer->insertOne($header);
$writer->insertAll($rows);

$this->info("Data has been exported to $output");
} else {
$this->table($header, $rows);
}

return self::SUCCESS;
}

private function getUntranslatedAttributes(string $attribute, string $locale, ?string $prefix): LazyCollection
{
$translations = trans($this->attributes[$attribute], locale: $locale);

return $this->getValuesWithCounts($attribute, $locale, $prefix)
->reject(fn ($count, $value) => in_array($value, $translations))
->sortDesc();
}

private function getValuesWithCounts(string $attribute, string $locale, ?string $prefix): LazyCollection
{
return ItemTranslation::query()
->select($attribute)
->where('locale', $locale)
->when($prefix, fn ($query) => $query->where('item_id', 'like', "$prefix%"))
->lazy()
->pluck($attribute)
->flatMap(fn ($value) => str($value)->split('/\s*;\s*/'))
->filter()
->countBy();
}
}
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';
}
18 changes: 18 additions & 0 deletions app/Filesystem/WebDAVAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Filesystem;

use League\Flysystem\FileAttributes;
use League\Flysystem\WebDAV\WebDAVAdapter as BaseWebDAVAdapter;

/**
* This class is a workaround for the issue of handling spaces in paths
*/
class WebDAVAdapter extends BaseWebDAVAdapter
{
public function lastModified(string $path): FileAttributes
{
$path = $this->encodePath($path);
return parent::lastModified($path);
}
}
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
2 changes: 1 addition & 1 deletion app/Importers/AbstractImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ protected function getJp2Files(
return $this->iipFilesMap[$import_record]
->filter(
fn(SplFileInfo $file) => preg_match(
sprintf('#^%s\.jp2$#', $image_filename_format),
sprintf('#^%s\.jp[2f]$#', $image_filename_format),
$file->getBasename()
)
)
Expand Down
150 changes: 150 additions & 0 deletions app/Importers/MmpImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

namespace App\Importers;

use Illuminate\Support\Facades\Date;

class MmpImporter extends AbstractImporter
{
protected $mapping = [
'identifier' => 'Inventární číslo',
'title' => 'Titul',
'dating' => 'Datace vzniku',
'inscription' => 'Signatura',
];

protected $defaults = [
'gallery:sk' => 'Múzeum Prahy, MMP',
'gallery:cs' => 'Muzeum Prahy, MMP',
];

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

protected function init(): void
{
$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->topicTranslationKeys = array_flip(trans('item.topics', locale: 'cs'));
}

protected function getItemId(array $record): string
{
return str($record['Inventární číslo'])
->swap([
' ' => '_',
'/' => '-',
])
->prepend('CZE:MP.');
}

protected function getItemImageFilenameFormat(array $record): string
{
return str($record['Inventární číslo'])
->replace('/', '_')
->append('(-.*)?');
}

protected function hydrateAuthor(array $record): string
{
if ($record['Autor'] === 'Anonym') {
return 'Neznámy autor';
}

return str($record['Autor'])
->trim()
->replaceMatches('/(.*?) /', '$1, ', 1);
}

protected function hydrateWorkType(array $record, string $locale): ?string
{
$replacements = [
'malba' => 'malířství',
];

return str($record['Výtvarný druh'])
->split('/\s*;\s*/')
->map(fn (string $workType) => $replacements[$workType] ?? $workType)
->when($locale !== 'cs', fn ($workTypes) => $workTypes->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 hydrateTopic(array $record, string $locale): ?string
{
if ($locale === 'cs') {
return $record['Námět/téma'];
}

return str($record['Námět/téma'])
->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 hydrateMeasurement(array $record, $locale): ?string
{
if (empty($record['Rozměr'])) {
return null;
}

$replacements = trans('item.measurement_replacements', [], $locale);
return strtr($record['Rozměr'], $replacements);
}

protected function hydrateDateEarliest(array $record): int
{
return Date::createFromFormat('d.m.Y', $record['(n) Datace OD'])->year;
}

protected function hydrateDateLatest(array $record): int
{
return Date::createFromFormat('d.m.Y', $record['(n) Datace DO'])->year;
}
}
Loading