Skip to content

Commit

Permalink
add ror version (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentauger authored Oct 4, 2023
1 parent 86ba2db commit 57e6d04
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
17 changes: 14 additions & 3 deletions app/Actions/ROR/DownloadLatestRORData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class DownloadLatestRORData
{
public static function handle(callable $progressCallable = null): string | null
public static function handle(callable $progressCallable = null): array | null
{
$url = 'https://zenodo.org/api/records';

Expand All @@ -21,6 +21,7 @@ public static function handle(callable $progressCallable = null): string | null
$data = $response->json();

$url = $data['hits']['hits'][0]['files'][0]['links']['self'] ?? null;
$version = $data['hits']['hits'][0]['metadata']['version'] ?? null;

// Download files to temporary storage
if(!$url) return null;
Expand Down Expand Up @@ -58,7 +59,12 @@ public static function handle(callable $progressCallable = null): string | null
// is Json file already extracted?
$jsonFile = $base_path . '/' . Str::beforeLast($fileName, '.') .'.json';

if(file_exists($jsonFile)) return $jsonFile;
if(file_exists($jsonFile)){
return [
'jsonFile' => $jsonFile,
'version' => $version,
];
}

$command = "unzip -o $fileName";

Expand All @@ -70,7 +76,12 @@ public static function handle(callable $progressCallable = null): string | null
}
});

if($result->wait()->successful()) return $jsonFile;
if($result->wait()->successful()){
return [
'jsonFile' => $jsonFile,
'version' => $version,
];
}

return null;
}
Expand Down
9 changes: 5 additions & 4 deletions app/Actions/ROR/SyncRORData.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class SyncRORData
{

public static function handle(string $jsonFilePath, callable $progressCallback = null): void
public static function handle(string $jsonFilePath, string $version, callable $progressCallback = null): void
{
if(!file_exists($jsonFilePath)) {
throw new \Exception('File not found: ' . $jsonFilePath);
Expand All @@ -35,7 +35,7 @@ public static function handle(string $jsonFilePath, callable $progressCallback =
if(!$countryCodesToImport->contains($record['country']['country_code'])) continue;

// update or create the organization
self::updateOrCreateOrganization($record);
self::updateOrCreateOrganization($record, $version);

if($update) $progressCallback($json->progress()->percentage());

Expand All @@ -51,7 +51,7 @@ public static function handle(string $jsonFilePath, callable $progressCallback =
* @param array $record
* @return void
*/
private static function updateOrCreateOrganization(array $record)
private static function updateOrCreateOrganization(array $record, string $version)
{
$ror_identifier = $record['id'];

Expand Down Expand Up @@ -97,7 +97,8 @@ private static function updateOrCreateOrganization(array $record)
'abbr_fr' => $abbr_fr,
'ror_names' => $ror_names,
'country_code' => $country_code,
'is_validated' => true
'is_validated' => true,
'ror_version' => $version,
]
);

Expand Down
9 changes: 5 additions & 4 deletions app/Console/Commands/UpdateROROrganizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ class UpdateROROrganizations extends Command
public function handle()
{
$this->info("Downloading or looking for ROR data dump...");
$path = DownloadLatestRORData::handle(function ($message) {
$rorPath = DownloadLatestRORData::handle(function ($message) {
$this->output->write($message);
});
if(!$path){
if(!$rorPath){
$this->error("Unable to download ROR data dump");
return;
}
$this->info("Found and uncompressed here: " . $path);
$this->info("Found and uncompressed here: " . $rorPath['jsonFile']);
$this->info("Synchronizing ROR data with Organizations...");
$progressBar = $this->output->createProgressBar(100);
SyncRORData::handle(
$path,
$rorPath['jsonFile'],
$rorPath['version'],
function ($percentage) use ($progressBar) {
$progressBar->setProgress($percentage);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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('organizations', function (Blueprint $table) {
$table->string('ror_version')->nullable()->after('ror_identifier');
});

}

};

0 comments on commit 57e6d04

Please sign in to comment.