-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1042 from SlovakNationalGallery/feat/dump-untrans…
…lated-attributes [item] Add command for dumping untranslated attributes
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |