Skip to content

Commit

Permalink
Merge pull request #153 from ConductionNL/fix/federatie-catalog
Browse files Browse the repository at this point in the history
Use catalog to discover if the listing exists already
  • Loading branch information
rjzondervan authored Dec 9, 2024
2 parents 7ee7dca + fe9b9b0 commit 172ee0a
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions lib/Service/DirectoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private function getDirectoryFromListing(Listing|array $listing): array
// $listing['id'] = $listing['uuid'];

// Remove unneeded fields
unset($listing['status'], $listing['lastSync'], $listing['default'], $listing['available'], $listing['catalog'], $listing['statusCode'],
unset($listing['status'], $listing['lastSync'], $listing['default'], $listing['available'], $listing['statusCode'],
// $listing['uuid'], //@todo this breaks stuff when trying to find and update a listing
$listing['hash']);

Expand Down Expand Up @@ -159,6 +159,7 @@ private function getDirectoryFromCatalog(Catalog|array $catalog): array
// Add the search and directory urls
$catalog['search'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute("opencatalogi.search.index"));
$catalog['directory'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute("opencatalogi.directory.index"));
$catalog['catalog'] = $catalog['id'];

// Process publication types
if (isset($catalog['publicationTypes']) && is_array($catalog['publicationTypes'])) {
Expand Down Expand Up @@ -262,7 +263,7 @@ public function doCronSync(): array {
*/
public function validateExternalListing(array $listing): bool
{
if (empty($listing['id']) || !Uuid::isValid($listing['id'])) {
if (empty($listing['catalog']) === true || Uuid::isValid($listing['catalog']) === false) {
return false;
}

Expand Down Expand Up @@ -368,14 +369,9 @@ public function syncExternalDirectory(string $url): array

// Get all current listings for this directory
$currentListings = $this->objectService->getObjects(
objectType: 'listing',
filters: [
'directory'=>$checkUrls,
]
objectType: 'listing'
);



// Remove any listings without a catalog ID from the database
foreach ($currentListings as $listing) {
if (empty($listing['catalog'])) {
Expand All @@ -395,12 +391,15 @@ public function syncExternalDirectory(string $url): array
'catalog' // Index by catalog ID
);

$oldListingDirectories = array_unique(array: array_column(array: $currentListings, column_key: 'directory'));

// Initialize arrays to store results
$addedListings = [];
$updatedListings = [];
$invalidListings = [];
$foundDirectories = [];
$removedListings = [];
$discoveredDirectories = [];

// Process each new listing
foreach ($newListings as $listing) {
Expand All @@ -410,13 +409,22 @@ public function syncExternalDirectory(string $url): array
continue;
}

if (in_array(needle: $listing['directory'], haystack: $checkUrls) === false
&& in_array(needle: $listing['directory'], haystack: $oldListingDirectories) === false
) {
$discoveredDirectories[] = $listing['directory'];

continue;
} else if (in_array(needle: $listing['directory'], haystack: $checkUrls) === false) {
continue;
}

// Check if we already have this listing by looking up its catalog ID in the oldListings array
$oldListing = $oldListings[$listing['id']] ?? null;
$oldListing = $oldListings[$listing['catalog']] ?? null;

// If no existing listing found, prepare the new listing data
if ($oldListing === null) {
$listing['hash'] = hash('sha256', json_encode($listing));
$listing['catalog'] = $listing['id'];
unset($listing['id']);
} else {
// Update existing listing
Expand All @@ -442,10 +450,14 @@ public function syncExternalDirectory(string $url): array
}

// Lets inform our new friends that we exist
foreach($foundDirectories as $foundDirectory){
foreach ($foundDirectories as $foundDirectory){
$this->broadcastService->broadcast($foundDirectory);
}

foreach ($discoveredDirectories as $discoveredDirectory) {
$this->syncExternalDirectory($discoveredDirectory);
}

// Return the results
return [
'invalidListings' => $invalidListings,
Expand Down

0 comments on commit 172ee0a

Please sign in to comment.