Skip to content
Open
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
2 changes: 1 addition & 1 deletion app/Console/Commands/RegisterFungiOccurences.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private function fetchIUCNData(string $genus, string $species): int
return RedListClassification::NA->value;
}
}

private function buildFungiData(array $row): array
{
return [
Expand Down
3 changes: 2 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console;

use App\Jobs\UpdateTaxonomyFromMycoBank;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

Expand All @@ -12,7 +13,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule): void
{
// $schedule->command('inspire')->hourly();
$schedule->job(UpdateTaxonomyFromMycoBank::class)->monthlyOn(1);
}

/**
Expand Down
92 changes: 92 additions & 0 deletions app/Jobs/UpdateTaxonomyFromMycoBank.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace App\Jobs;

use App\Models\Taxonomy;
use App\Services\Platforms\MycoBank\MycoBank;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class UpdateTaxonomyFromMycoBank implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function handle(MycoBank $service): void
{
$filepath = $service->downloadFungiList();

$reader = ReaderEntityFactory::createXLSXReader();

try {
$reader->open($filepath);
$localTaxa = $this->getLocalTaxonomies();

foreach ($reader->getSheetIterator() as $sheet) {
$header = null;

foreach ($sheet->getRowIterator() as $rowIndex => $row) {
$cells = $row->toArray();

// Map header columns on first row
if ($rowIndex === 1) {
$header = array_flip($cells);
continue;
}

if (!$header || !isset($header['Taxon name'])) {
Log::warning('Invalid sheet format: missing "Taxon name" column.');
break;
}

$taxonName = trim($cells[$header['Taxon name']] ?? '');
if ($taxonName === '') {
continue;
}

$fungi = $localTaxa[$taxonName] ?? null;
if (!$fungi) {
continue;
}

$this->updateFungiRecord($fungi, $cells, $header);
}
}
} catch (\Throwable $e) {
Log::error('MycoBank import failed: ' . $e->getMessage(), ['trace' => $e->getTraceAsString()]);
} finally {
$reader->close();
}
}
private function getLocalTaxonomies(): array
{
return Taxonomy::all()
->mapWithKeys(fn($t) => [$t->genus . ' ' . $t->specie => $t])
->toArray();
}

private function updateFungiRecord(array $fungiData, array $cells, array $header): void
{
$fungi = Taxonomy::find($fungiData['id']);
$mycoBankId = $cells[$header['MycoBank #']] ?? null;

$updates = [];

if (!$fungi) {
Log::warning("Taxonomy not found for ID: {$fungiData['id']}");
return;
}

if ($mycoBankId && $mycoBankId !== $fungi->external_id) {
$updates['external_id'] = $mycoBankId;
}

if (!empty($updates)) {
$fungi->fill($updates)->save();
}
}
}
78 changes: 78 additions & 0 deletions app/Services/Platforms/MycoBank/MycoBank.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace App\Services\Platforms\MycoBank;

use Exception;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Shuchkin\SimpleXLSX;
use ZipArchive;

class MycoBank
{
public function __construct(protected ZipArchive $zipArchive) {}

public function downloadFungiList(): string
{
$name = storage_path('app/MycoBank/fungi_list_' . date('Y_m_d_H_i') . '.zip');

$client = new Client();
$client->get('https://www.mycobank.org/Images/MBList.zip', [
'sink' => $name
]);

return $this->extractXlsxFromZip($name);
}

public function extractXlsxFromZip($zipPath)
{
$destinationPath = str_replace('.zip', '', $zipPath);
if (!file_exists($destinationPath)) {
mkdir($destinationPath, 0777, true);
}

$zip = new \ZipArchive;
if ($zip->open($zipPath) === true) {
$tempPath = storage_path('app/temp_unzip_' . uniqid());
mkdir($tempPath, 7777, true);

$zip->extractTo($tempPath);
$zip->close();

$xlsxFile = collect(scandir($tempPath))
->first(fn($file) => str_ends_with($file, '.xlsx'));

if ($xlsxFile) {
$xlsxSource = $tempPath . '/' . $xlsxFile;
$xlsxTarget = $destinationPath . '/' . $xlsxFile;

rename($xlsxSource, $xlsxTarget);
$this->deleteDirectory($tempPath);

return $xlsxTarget;
} else {
$this->deleteDirectory($tempPath);
throw new \Exception("Nenhum arquivo XLSX encontrado no zip.");
}
} else {
throw new \Exception("Não foi possível abrir o arquivo ZIP.");
}
}

private function deleteDirectory($dir)
{
if (!file_exists($dir)) return true;
if (!is_dir($dir)) return unlink($dir);

foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') continue;
$this->deleteDirectory($dir . DIRECTORY_SEPARATOR . $item);
}
return rmdir($dir);
}
}
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@
"name": "laravel/laravel",
"type": "project",
"description": "The skeleton application for the Laravel framework.",
"keywords": ["laravel", "framework"],
"keywords": [
"laravel",
"framework"
],
"license": "MIT",
"require": {
"php": "^8.1",
"box/spout": "^3.3",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.3",
"laravel/tinker": "^2.8",
"phpoffice/phpspreadsheet": "^2.0",
"phpoffice/phpspreadsheet": "^5.1",
"shuchkin/simplexlsx": "^1.1",
"tymon/jwt-auth": "^2.1"
},
"require-dev": {
Expand Down
Loading