Skip to content
This repository was archived by the owner on Dec 3, 2024. It is now read-only.

Commit 857ffed

Browse files
committed
Resolved conflicts
2 parents f993262 + 77f55e4 commit 857ffed

File tree

56 files changed

+4413
-3900
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+4413
-3900
lines changed

Diff for: app/BatchUpload/BatchUploader.php renamed to app/BatchImport/BatchUploader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\BatchUpload;
3+
namespace App\BatchImport;
44

55
use App\Models\Collection;
66
use App\Models\CollectionTaxonomy;

Diff for: app/BatchImport/OpenActiveTaxonomyImporter.php

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
3+
namespace App\BatchImport;
4+
5+
use App\Models\Taxonomy;
6+
use Carbon\Exceptions\UnitException;
7+
use Exception;
8+
use GuzzleHttp\Client;
9+
use GuzzleHttp\Exception\GuzzleException;
10+
use Illuminate\Database\Eloquent\MassAssignmentException;
11+
use Illuminate\Database\Eloquent\ModelNotFoundException;
12+
use Illuminate\Support\Carbon;
13+
use Illuminate\Support\Facades\App;
14+
use Illuminate\Support\Facades\DB;
15+
use Illuminate\Support\Facades\Log;
16+
use Illuminate\Support\Facades\Schema;
17+
use RuntimeException;
18+
19+
class OpenActiveTaxonomyImporter
20+
{
21+
/**
22+
* The URL for the Open Active taxonomy in jsonld format.
23+
*
24+
* @var string
25+
*/
26+
protected $openActiveDirectoryUrl = 'https://openactive.io/activity-list/activity-list.jsonld';
27+
28+
/**
29+
* Dry run: no taxonomies will be created
30+
*
31+
* @var bool
32+
**/
33+
protected $dryRun = false;
34+
35+
public function __construct($directoryUrl = null, $dryRun = false)
36+
{
37+
if ($directoryUrl) {
38+
$this->openActiveDirectoryUrl = $directoryUrl;
39+
}
40+
$this->dryRun = $dryRun;
41+
}
42+
43+
/**
44+
* Fetch the Open Active taxonomy data and store it as a collection.
45+
*
46+
* @param mixed $openActiveDirectoryUrl
47+
* @return array
48+
*/
49+
public function fetchTaxonomies($openActiveDirectoryUrl)
50+
{
51+
$client = new Client();
52+
try {
53+
$response = $client->get($openActiveDirectoryUrl);
54+
if (200 === $response->getStatusCode() && $response->getBody()->isReadable()) {
55+
$data = json_decode((string)$response->getBody(), true);
56+
57+
return $data['concept'];
58+
}
59+
} catch (\Exception $e) {
60+
Log::error($e->getMessage());
61+
62+
throw $e;
63+
}
64+
}
65+
66+
/**
67+
* Get the root Open Active category.
68+
*
69+
* @return \App\Models\Taxonomy
70+
*/
71+
public function getOpenActiveCategory()
72+
{
73+
return Taxonomy::category()
74+
->children()
75+
->where('name', 'OpenActive')
76+
->firstOrFail();
77+
}
78+
79+
/**
80+
* Runs the import process
81+
*
82+
* @return void
83+
* @throws ModelNotFoundException
84+
* @throws Exception
85+
* @throws GuzzleException
86+
* @throws UnitException
87+
* @throws RuntimeException
88+
* @throws MassAssignmentException
89+
* @return int
90+
*/
91+
public function runImport():int
92+
{
93+
// Get the root Open Active category
94+
$openActiveCategory = $this->getOpenActiveCategory();
95+
96+
// Fetch the remote Open Active data
97+
$openActiveTaxonomies = $this->fetchTaxonomies($this->openActiveDirectoryUrl);
98+
99+
// Correctly format the remote data ready for import
100+
$taxonomyImports = $this->mapOpenActiveTaxonomyImport($openActiveCategory, $openActiveTaxonomies);
101+
102+
return $this->importTaxonomies($openActiveCategory, $taxonomyImports);
103+
}
104+
105+
/**
106+
* Import the formatted taxonomies into the database.
107+
*
108+
* @param \App\Models\Taxonomy $rootTaxonomy
109+
* @param array $taxonomyImports
110+
* @return int
111+
*/
112+
public function importTaxonomies(Taxonomy $openActiveCategory, array $taxonomyImports): int
113+
{
114+
// Import the data
115+
if (App::environment() != 'testing') {
116+
DB::beginTransaction();
117+
}
118+
119+
Schema::disableForeignKeyConstraints();
120+
121+
DB::table((new Taxonomy())->getTable())->insertOrIgnore($taxonomyImports);
122+
123+
if (!$this->dryRun && App::environment() != 'testing') {
124+
DB::commit();
125+
}
126+
127+
Schema::enableForeignKeyConstraints();
128+
129+
$openActiveCategory->refresh();
130+
131+
$openActiveCategory->updateDepth();
132+
133+
return count($taxonomyImports);
134+
}
135+
136+
/**
137+
* Map the imported data into an import friendly format.
138+
*
139+
* @param \App\Models\Taxonomy $rootTaxonomy
140+
* @param array openActiveTaxonomyData
141+
* @param mixed $openActiveTaxonomyData
142+
* @return array
143+
*/
144+
public function mapOpenActiveTaxonomyImport(Taxonomy $rootTaxonomy, $openActiveTaxonomyData)
145+
{
146+
$nowDateTimeString = Carbon::now()->toDateTimeString();
147+
148+
return array_map(function ($taxonomy) use ($rootTaxonomy, $nowDateTimeString) {
149+
return array_merge(
150+
$this->mapOpenActiveTaxonomyToTaxonomyModelSchema($rootTaxonomy, $taxonomy),
151+
[
152+
'created_at' => $nowDateTimeString,
153+
'updated_at' => $nowDateTimeString,
154+
]
155+
);
156+
}, $openActiveTaxonomyData);
157+
}
158+
159+
/**
160+
* Return the uuid component from an Open Active directory url.
161+
*
162+
* @param string $identifierUrl
163+
* @return string
164+
*/
165+
private function parseIdentifier(string $identifierUrl)
166+
{
167+
return mb_substr($identifierUrl, mb_strpos($identifierUrl, '#') + 1);
168+
}
169+
170+
/**
171+
* Convert Open Active taxonomies into Taxonomy model data.
172+
*
173+
* @param \App\Models\Taxonomy $rootTaxonomy
174+
* @param array openActiveTaxonomyData
175+
* @return array
176+
*/
177+
private function mapOpenActiveTaxonomyToTaxonomyModelSchema(Taxonomy $rootTaxonomy, array $taxonomyData)
178+
{
179+
return [
180+
'id' => $taxonomyData['identifier'],
181+
'name' => $taxonomyData['prefLabel'],
182+
'parent_id' => array_key_exists('broader', $taxonomyData) ? $this->parseIdentifier($taxonomyData['broader'][0]) : $rootTaxonomy->id,
183+
'order' => 0,
184+
'depth' => 2,
185+
];
186+
}
187+
}

Diff for: app/Console/Commands/Ck/BatchUploadCommand.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Console\Commands\Ck;
44

5-
use App\BatchUpload\BatchUploader;
5+
use App\BatchImport\BatchUploader;
66
use Illuminate\Console\Command;
77

88
class BatchUploadCommand extends Command
@@ -22,7 +22,7 @@ class BatchUploadCommand extends Command
2222
protected $description = 'Uploads an xlsx spreadsheet to the database';
2323

2424
/**
25-
* @var \App\BatchUpload\BatchUploader
25+
* @var \App\BatchImport\BatchUploader
2626
*/
2727
protected $batchUploader;
2828

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace App\Console\Commands\Ck;
4+
5+
use App\BatchImport\OpenActiveTaxonomyImporter;
6+
use Illuminate\Console\Command;
7+
8+
class ImportOpenActiveTaxonomiesCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'ck:import-openactive-taxonomies
16+
{--url : The url of the Open Active taxonomies .jsonld file (e.g. http://...)}
17+
{--dry-run : Parse the import but do not commit the changes}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Imports a .jsonld file of Taxonomies under the OpenActive category';
25+
26+
/**
27+
* Execute the console command.
28+
*
29+
* @throws \Exception
30+
* @return mixed
31+
*/
32+
public function handle()
33+
{
34+
$jsonUrl = null;
35+
if ($this->option('url')) {
36+
if (!preg_match("/\b(?:(?:https?):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $this->option('url'))) {
37+
$this->error('The .jsonld file URL is invalid. Exiting');
38+
39+
return false;
40+
}
41+
$jsonUrl = $this->option('url');
42+
}
43+
44+
$dryRun = $this->option('dry-run');
45+
46+
$openActiveImporter = new OpenActiveTaxonomyImporter($jsonUrl, $dryRun);
47+
48+
if ($dryRun) {
49+
$this->warn('Dry Run, no data will be committed');
50+
}
51+
52+
try {
53+
$importCount = $openActiveImporter->runImport();
54+
} catch (\Exception $e) {
55+
$this->error($e->getMessage());
56+
}
57+
58+
$this->info('All records imported. Total records imported: ' . $importCount);
59+
}
60+
}

0 commit comments

Comments
 (0)