Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use catalog to discover if the listing exists already #153

Merged
merged 2 commits into from
Dec 9, 2024
Merged
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
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