@@ -246,7 +246,7 @@ public function storeCatalog($catalog, $userid, $baseUrl, $collectionId, $featur
246
246
$ catalog ['description ' ] ?? null ,
247
247
isset ($ catalog ['id ' ]) ? count (explode ('/ ' , $ catalog ['id ' ])) : 0 ,
248
248
// If no input counter is specified - set to 1
249
- json_encode ($ counters , JSON_UNESCAPED_SLASHES ),
249
+ str_replace ( ' [] ' , ' {} ' , json_encode ($ counters , JSON_UNESCAPED_SLASHES ) ),
250
250
$ catalog ['owner ' ] ?? $ userid ,
251
251
json_encode ($ cleanLinks ['links ' ], JSON_UNESCAPED_SLASHES ),
252
252
RestoConstants::GROUP_DEFAULT_ID ,
@@ -426,8 +426,10 @@ public function updateCatalog($catalog, $userid, $baseUrl)
426
426
$ path = RestoUtil::path2ltree ($ catalog ['id ' ]);
427
427
428
428
/*
429
- * Add an entry in catalog_feature for each interalItems
429
+ * Add an entry in catalog_feature for each interalItems but first remove all items !
430
430
*/
431
+ $ this ->removeCatalogFeatures ($ catalog ['id ' ]);
432
+
431
433
for ($ i = 0 , $ ii = count ($ cleanLinks ['internalItems ' ]); $ i < $ ii ; $ i ++) {
432
434
$ this ->insertIntoCatalogFeature ($ cleanLinks ['internalItems ' ][$ i ]['id ' ], $ path , $ catalog ['id ' ], $ cleanLinks ['internalItems ' ][$ i ]['collection ' ]);
433
435
}
@@ -443,20 +445,19 @@ public function updateCatalog($catalog, $userid, $baseUrl)
443
445
}
444
446
445
447
/**
446
- * Increment all catalogs relied to feature
448
+ * Increment catalog counters
447
449
*
448
450
* @param string $featureId
449
- * @param string $collectionId
450
451
* @param integer $increment
451
452
*/
452
- public function updateFeatureCatalogsCounters ($ featureId , $ collectionId , $ increment )
453
+ public function updateFeatureCatalogsCounters ($ featureId , $ increment )
453
454
{
454
455
455
456
$ query = join (' ' , array (
456
- 'WITH path_hierarchy AS (SELECT distinct featureid, subpath(path, 0, generate_series(1, nlevel(path))) AS p FROM ' . $ this ->dbDriver ->targetSchema . '.catalog_feature ' ,
457
+ 'WITH path_hierarchy AS (SELECT collection, catalogid FROM ' . $ this ->dbDriver ->targetSchema . '.catalog_feature ' ,
457
458
'WHERE featureid = \'' . pg_escape_string ($ this ->dbDriver ->getConnection (), $ featureId ) . '\') ' ,
458
- 'UPDATE ' . $ this ->dbDriver ->targetSchema . '.catalog SET counters=public.increment_counters(counters, ' . $ increment . ', ' . ( isset ( $ collectionId ) ? '\'' . $ collectionId . '\'' : ' NULL ' ) . ' ) ' ,
459
- 'WHERE lower(id) IN (SELECT LOWER(REPLACE(REPLACE( path_hierarchy.p::text, \' _ \' , \' . \' ), \' . \' , \' / \' ) ) FROM path_hierarchy) '
459
+ 'UPDATE ' . $ this ->dbDriver ->targetSchema . '.catalog SET counters=public.increment_counters(counters, ' . $ increment . ', (SELECT path_hierarchy.collection FROM path_hierarchy LIMIT 1) ) ' ,
460
+ 'WHERE lower(id) IN (SELECT LOWER(path_hierarchy.catalogid ) FROM path_hierarchy) '
460
461
));
461
462
462
463
$ results = $ this ->dbDriver ->fetch ($ this ->dbDriver ->query ($ query ));
@@ -524,6 +525,19 @@ public function removeCatalog($catalogId, $inTransaction = true)
524
525
525
526
}
526
527
528
+ /**
529
+ * Remove features from a catalog i.e. unassociate feature from a catalog
530
+ *
531
+ * [WARNING] This DOES NOT REMOVE FEATURE IN TABLE feature
532
+ *
533
+ * @param string $catalogId
534
+ */
535
+ private function removeCatalogFeatures ($ catalogId )
536
+ {
537
+ $ this ->dbDriver ->query ('UPDATE ' . $ this ->dbDriver ->targetSchema . '.catalog SET counters= \'{"total":0, "collections":{}} \' WHERE lower(id) = lower( \'' . pg_escape_string ($ this ->dbDriver ->getConnection (), $ catalogId ) . '\') ' );
538
+ $ this ->dbDriver ->fetch ($ this ->dbDriver ->pQuery ('DELETE FROM ' . $ this ->dbDriver ->targetSchema . '.catalog_feature WHERE path = $1 ' , array (RestoUtil::path2ltree ($ catalogId )), 500 , 'Cannot delete catalog_feature association for catalog ' . $ catalogId ));
539
+ }
540
+
527
541
/**
528
542
* Return STAC Summaries from catalogs elements from a type for a given collection
529
543
*
@@ -665,15 +679,17 @@ private function insertIntoCatalogFeature($featureId, $path, $catalogId, $collec
665
679
$ path ,
666
680
$ catalogId ,
667
681
$ collectionId
668
- ), 500 , 'Cannot create association for ' . $ featureId . ' intp catalog ' . $ catalogId );
682
+ ), 500 , 'Cannot create association for ' . $ featureId . ' in catalog ' . $ catalogId );
669
683
670
684
$ this ->dbDriver ->pQuery ('INSERT INTO ' . $ this ->dbDriver ->targetSchema . '.catalog_feature (featureid, path, catalogid, collection) SELECT $1, $2::ltree, $3, $4 WHERE NOT EXISTS (SELECT 1 FROM ' . $ this ->dbDriver ->targetSchema . '.catalog_feature WHERE featureid = $1 AND (path <@ $2::ltree OR path @> $2::ltree)) ' , array (
671
685
$ featureId ,
672
686
$ path ,
673
687
$ catalogId ,
674
688
$ collectionId
675
- ), 500 , 'Cannot create association for ' . $ featureId . ' intp catalog ' . $ catalogId );
689
+ ), 500 , 'Cannot create association for ' . $ featureId . ' in catalog ' . $ catalogId );
676
690
691
+ $ this ->updateFeatureCatalogsCounters ($ featureId , 1 );
692
+
677
693
}
678
694
679
695
/**
@@ -781,12 +797,17 @@ private function onTheFlyUpdateCountersWithCollection($catalog, $baseUrl)
781
797
$ originalLinks = json_decode ($ catalog ['links ' ], true );
782
798
783
799
$ collections = array ();
784
-
800
+
785
801
$ results = $ this ->dbDriver ->pQuery ('SELECT id, counters, links FROM ' . $ this ->dbDriver ->targetSchema . '.catalog WHERE lower(id) = lower($1) OR lower(id) LIKE lower($2) ORDER BY id ASC ' , array (
786
802
$ catalog ['id ' ],
787
803
$ catalog ['id ' ] . '/% '
788
804
));
789
805
while ($ result = pg_fetch_assoc ($ results )) {
806
+
807
+ if ($ catalog ['id ' ] !== $ result ['id ' ]) {
808
+ $ catalogCounters = json_decode ($ result ['counters ' ], true );
809
+ $ counters ['total ' ] = $ counters ['total ' ] + $ catalogCounters ['total ' ];
810
+ }
790
811
791
812
// Process collection
792
813
if ( isset ($ result ['links ' ]) ) {
@@ -808,14 +829,14 @@ private function onTheFlyUpdateCountersWithCollection($catalog, $baseUrl)
808
829
'collections/ ' . $ collectionId
809
830
));
810
831
while ($ result = pg_fetch_assoc ($ results )) {
811
- $ collectionCounter = json_decode ($ result ['counters ' ], true );
812
- $ counters ['total ' ] = $ counters ['total ' ] + $ collectionCounter ['total ' ];
813
- $ counters ['collections ' ][$ collectionId ] = $ collectionCounter ['total ' ];
832
+ $ collectionCounters = json_decode ($ result ['counters ' ], true );
833
+ $ counters ['total ' ] = $ counters ['total ' ] + $ collectionCounters ['total ' ];
834
+ $ counters ['collections ' ][$ collectionId ] = $ collectionCounters ['total ' ];
814
835
for ($ i = 0 , $ ii = count ($ originalLinks ); $ i < $ ii ; $ i ++) {
815
836
if ($ originalLinks [$ i ]['rel ' ] === 'child ' ) {
816
837
$ exploded = explode ('/ ' , substr ($ originalLinks [$ i ]['href ' ], strlen ($ baseUrl . RestoRouter::ROUTE_TO_COLLECTIONS ) + 1 ));
817
838
if (count ($ exploded ) === 1 && $ exploded [0 ] === $ collectionId ) {
818
- $ originalLinks [$ i ]['matched ' ] = $ collectionCounter ['total ' ];
839
+ $ originalLinks [$ i ]['matched ' ] = $ collectionCounters ['total ' ];
819
840
if ( isset ($ result ['title ' ]) ) {
820
841
$ originalLinks [$ i ]['title ' ] = $ result ['title ' ];
821
842
}
0 commit comments