Skip to content
Merged
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
8 changes: 0 additions & 8 deletions src/app/Console/Commands/AlgoliaImportWorldHeritages.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,6 @@ public function handle(): int
return;
}

if ((int) $row->id === 1133) {
dd([
'state_party_codes' => $statePartyCodes,
'country_names_jp' => $countryNamesJp,
'object' => end($objects),
]);
}

$res = $client->saveObjects(
indexName: $indexName,
objects: $objects
Expand Down
45 changes: 0 additions & 45 deletions src/app/Console/Commands/DumpUnescoWorldHeritageJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,49 +487,4 @@ private function normalizeRow(array $row): array
'components_count' => $toInt($row['components_count'] ?? null),
];
}

private function buildCriteriaFromDumpRow(array $row): array
{
$raw = $row['criteria_txt'] ?? null;

if (!is_string($raw)) {
return [];
}
$raw = trim($raw);
if ($raw === '') {
return [];
}

preg_match_all('/\(\s*([ivxlcdm]+)\s*\)/i', $raw, $m1);
$vals = $m1[1] ?? [];

if (!is_array($vals) || $vals === []) {
preg_match_all('/\b([ivxlcdm]{1,6})\b/i', $raw, $m2);
$vals = $m2[1] ?? [];
}

if (!is_array($vals) || $vals === []) {
return [];
}

$out = [];
$seen = [];

foreach ($vals as $v) {
$v = strtolower(trim((string) $v));
if ($v === '') {
continue;
}
if (!preg_match('/^[ivxlcdm]+$/', $v)) {
continue;
}

if (!isset($seen[$v])) {
$seen[$v] = true;
$out[] = $v;
}
}

return $out;
}
}
80 changes: 9 additions & 71 deletions src/app/Console/Commands/ImportCountriesFromSplitFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use App\Console\Concerns\LoadsJsonRows;

class ImportCountriesFromSplitFile extends Command
{
use LoadsJsonRows;

/**
* The name and signature of the console command.
*
Expand Down Expand Up @@ -69,27 +71,16 @@ public function handle(): int
if ($code === '' || strlen($code) !== 3) {
$skipped++;
if ($strict) {
$this->error(
'Strict: invalid state_party_code: ' .
json_encode($row, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
);
$this->error('Strict: invalid state_party_code: ' . json_encode($row, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
return self::FAILURE;
}
continue;
}

$nameEn = $this->toNullableString($row['name_en'] ?? null);
$nameJp = $this->toNullableString($row['name_jp'] ?? null);
$nameEn = $this->toNullableString($row['name_en'] ?? null) ?? $code;
$nameJp = $this->toNullableString($row['name_jp'] ?? null) ?? $this->resolveCountryNameJapanese($code);
$region = $this->toNullableString($row['region'] ?? null);

if ($nameEn === null) {
$nameEn = $code;
}

if ($nameJp === null) {
$nameJp = $this->resolveCountryNameJapanese($code);
}

if ($strict && $nameJp === null) {
$this->error("Strict: name_jp could not be resolved for state_party_code [{$code}]");
return self::FAILURE;
Expand Down Expand Up @@ -122,75 +113,22 @@ private function flush(array $rows, bool $dryRun): int
return count($rows);
}

DB::table('countries')->upsert(
$rows,
['state_party_code'],
['name_en', 'name_jp', 'region']
);

DB::table('countries')->upsert($rows, ['state_party_code'], ['name_en', 'name_jp', 'region']);
return count($rows);
}

private function loadRows(string $path): ?array
{
$raw = @file_get_contents($path);
if ($raw === false) {
return null;
}

$json = json_decode($raw, true);
if (!is_array($json)) {
return null;
}

if (array_key_exists('results', $json)) {
return is_array($json['results']) ? $json['results'] : null;
}

return array_is_list($json) ? $json : null;
}

private function resolvePath(string $path): string
{
$path = trim($path);
if ($path === '') {
return $path;
}

if (str_starts_with($path, '/') || preg_match('/^[A-Za-z]:\\\\/', $path) === 1) {
return $path;
}

$path = ltrim($path, '/');

if (str_starts_with($path, 'storage/app/')) {
$path = substr($path, strlen('storage/app/'));
}

if (str_starts_with($path, 'private/')) {
$path = substr($path, strlen('private/'));
}

return Storage::disk('local')->path($path);
}

private function toNullableString(mixed $v): ?string
{
if (!is_string($v)) {
return null;
}

$s = trim($v);

return $s === '' ? null : $s;
}

private function resolveCountryNameJapanese(string $iso3): ?string
{
$countryNameJa = Config::get('country_ja.alpha3_to_country.' . strtoupper(trim($iso3)));

return is_string($countryNameJa) && $countryNameJa !== ''
? $countryNameJa
: null;
$name = Config::get('country_ja.alpha3_to_country.' . strtoupper(trim($iso3)));
return is_string($name) && $name !== '' ? $name : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Throwable;

class ImportWorldHeritageJapaneseNameFromJson extends Command
{
Expand Down Expand Up @@ -94,38 +95,52 @@ public function handle(): int
->whereIn('id', $chunkIds)
->get();

DB::beginTransaction();
$upsertRows = [];

try {
foreach ($rows as $row) {
$id = (int) $row->id;
foreach ($rows as $row) {
$id = (int) $row->id;

if ($onlyEmpty) {
$current = is_string($row->name_jp) ? trim($row->name_jp) : '';
if ($current !== '') {
$skippedAlreadySet++;
continue;
}
if ($onlyEmpty) {
$current = is_string($row->name_jp) ? trim($row->name_jp) : '';
if ($current !== '') {
$skippedAlreadySet++;
continue;
}
}

DB::table('world_heritage_sites')
->where('id', $id)
->update([
'name_jp' => $map[$id],
'updated_at' => $now,
]);
$upsertRows[] = [
'id' => $id,
'name_jp' => $map[$id],
'updated_at' => $now,
];
}

$updated++;
if ($upsertRows !== []) {
DB::beginTransaction();
try {
$chunkIdList = array_column($upsertRows, 'id');
$cases = implode(' ', array_map(
fn ($row) => "WHEN {$row['id']} THEN ?",
$upsertRows
));
$bindings = array_column($upsertRows, 'name_jp');
$bindings[] = $now;
$placeholders = implode(',', $chunkIdList);

DB::statement(
"UPDATE world_heritage_sites SET name_jp = CASE id {$cases} END, updated_at = ? WHERE id IN ({$placeholders})",
$bindings
);

DB::commit();
$updated += count($upsertRows);
} catch (Throwable $e) {
DB::rollBack();
$this->error('Failed while updating chunk: ' . $e->getMessage());
return self::FAILURE;
}

DB::commit();
} catch (\Throwable $e) {
DB::rollBack();
$this->error('Failed while updating chunk: ' . $e->getMessage());
return self::FAILURE;
}
}

$this->info(
"Done: updated={$updated}, missing=" . count($missing) .
", skipped_already_set={$skippedAlreadySet}, invalid/skipped={$invalid}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace App\Console\Commands;

use App\Console\Concerns\LoadsJsonRows;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;

class ImportWorldHeritageSiteCountryExceptionsFromSplitFile extends Command
{

use LoadsJsonRows;

/**
* The name and signature of the console command.
*
Expand Down Expand Up @@ -60,13 +63,12 @@ public function handle(): int
if ($max > 0 && $imported >= $max) {
break;
}
if (!is_array($row)) { $skipped++; continue; }

$idNo = $row['id_no']
?? $row['world_heritage_site_id']
?? $row['site_id']
?? null;
if (!is_array($row)) {
$skipped++;
continue;
}

$idNo = $row['id_no'] ?? $row['world_heritage_site_id'] ?? $row['site_id'] ?? null;
if (!is_int($idNo) && !(is_string($idNo) && is_numeric($idNo))) {
$skipped++;
if ($strict) {
Expand All @@ -87,12 +89,9 @@ public function handle(): int
continue;
}

if ($strict) {
$existsSite = DB::table('world_heritage_sites')->where('id', $siteId)->exists();
if (!$existsSite) {
$this->error("Strict: FK missing. world_heritage_sites.id={$siteId} not found");
return self::FAILURE;
}
if ($strict && !DB::table('world_heritage_sites')->where('id', $siteId)->exists()) {
$this->error("Strict: FK missing. world_heritage_sites.id={$siteId} not found");
return self::FAILURE;
}

$batch[] = [
Expand Down Expand Up @@ -128,50 +127,6 @@ private function flush(array $rows, bool $dryRun): int
['world_heritage_site_id', 'reason'],
['raw', 'updated_at']
);

return count($rows);
}

private function loadRows(string $path): ?array
{
$raw = @file_get_contents($path);
if ($raw === false) {
return null;
}

$json = json_decode($raw, true);
if (!is_array($json)) {
return null;
}

if (array_key_exists('results', $json)) {
return is_array($json['results']) ? $json['results'] : null;
}

return array_is_list($json) ? $json : null;
}

private function resolvePath(string $path): string
{
$path = trim($path);
if ($path === '') {
return $path;
}

if (str_starts_with($path, '/') || preg_match('/^[A-Za-z]:\\\\/', $path) === 1) {
return $path;
}

$path = ltrim($path, '/');

if (str_starts_with($path, 'storage/app/')) {
$path = substr($path, strlen('storage/app/'));
}

if (str_starts_with($path, 'private/')) {
$path = substr($path, strlen('private/'));
}

return Storage::disk('local')->path($path);
}
}
Loading
Loading