@@ -200,13 +200,13 @@ public function getCatalogItems($catalogId, $baseUrl)
200
200
* !! THIS FUNCTION IS THREAD SAFE !!
201
201
*
202
202
* @param array $catalogs
203
- * @param string $baseUrl
203
+ * @param RestoContext $context
204
204
* @param string $userid
205
205
* @param RestoCollection $collection
206
206
* @param string $featureId
207
207
* @param boolean addBeginCommit // True means that call is already within a BEGIN/COMMIT block
208
208
*/
209
- public function storeCatalogs ($ catalogs , $ baseUrl , $ userid , $ collection , $ featureId , $ addBeginCommit )
209
+ public function storeCatalogs ($ catalogs , $ context , $ userid , $ collection , $ featureId , $ addBeginCommit )
210
210
{
211
211
212
212
// Empty catalogs - do nothing
@@ -223,7 +223,7 @@ public function storeCatalogs($catalogs, $baseUrl, $userid, $collection, $featur
223
223
}
224
224
225
225
for ($ i = count ($ catalogs ); $ i --;) {
226
- $ this ->storeCatalog ($ catalogs [$ i ], $ userid , $ baseUrl , $ collectionId , $ featureId );
226
+ $ this ->storeCatalog ($ catalogs [$ i ], $ userid , $ context , $ collectionId , $ featureId );
227
227
}
228
228
229
229
// Update all counters at the same time for a given featureId
@@ -251,10 +251,10 @@ public function storeCatalogs($catalogs, $baseUrl, $userid, $collection, $featur
251
251
*
252
252
* @param array $catalog
253
253
* @param string $userid
254
- * @param string $baseUrl
254
+ * @param RestoContext $context
255
255
* @return boolean
256
256
*/
257
- public function updateCatalog ($ catalog , $ userid , $ baseUrl )
257
+ public function updateCatalog ($ catalog , $ userid , $ context )
258
258
{
259
259
260
260
if ( !isset ($ catalog ['id ' ]) ) {
@@ -274,7 +274,7 @@ public function updateCatalog($catalog, $userid, $baseUrl)
274
274
);
275
275
276
276
$ set = array ();
277
- $ cleanLinks = $ this ->getCleanLinks ($ catalog , $ userid , $ baseUrl );
277
+ $ cleanLinks = $ this ->getCleanLinks ($ catalog , $ userid , $ context );
278
278
$ catalog ['links ' ] = $ cleanLinks ['links ' ];
279
279
280
280
foreach (array_keys ($ catalog ) as $ key ) {
@@ -402,11 +402,11 @@ public function removeCatalog($catalogId)
402
402
*
403
403
* @param array $catalog
404
404
* @param string $userid
405
- * @param string $baseUrl
405
+ * @param RestoContext $context
406
406
* @param string $collectionId
407
407
* @param string $featureId
408
408
*/
409
- private function storeCatalog ($ catalog , $ userid , $ baseUrl , $ collectionId , $ featureId )
409
+ private function storeCatalog ($ catalog , $ userid , $ context , $ collectionId , $ featureId )
410
410
{
411
411
// Empty catalog - do nothing
412
412
if (!isset ($ catalog )) {
@@ -418,7 +418,7 @@ private function storeCatalog($catalog, $userid, $baseUrl, $collectionId, $featu
418
418
$ catalog ['id ' ] = rtrim ($ catalog ['id ' ], '/ ' );
419
419
}
420
420
421
- $ cleanLinks = $ this ->getCleanLinks ($ catalog , $ userid , $ baseUrl );
421
+ $ cleanLinks = $ this ->getCleanLinks ($ catalog , $ userid , $ context );
422
422
423
423
$ insert = 'INSERT INTO ' . $ this ->dbDriver ->targetSchema . '.catalog (id, title, description, level, counters, owner, links, visibility, rtype, created) SELECT $1,$2,$3,$4,$5,$6,$7,$8,$9,now() ON CONFLICT (id) DO NOTHING ' ;
424
424
$ this ->dbDriver ->pQuery ($ insert , array (
@@ -478,21 +478,6 @@ private function storeCatalog($catalog, $userid, $baseUrl, $collectionId, $featu
478
478
*/
479
479
$ this ->addInternalItems ($ cleanLinks ['internalItems ' ], $ catalog ['id ' ]);
480
480
481
- /*
482
- * Now the tricky part - change catalogs level
483
- */
484
- for ($ i = 0 , $ ii = count ($ cleanLinks ['updateCatalogs ' ]); $ i < $ ii ; $ i ++) {
485
- $ updateCatalogs = $ cleanLinks ['updateCatalogs ' ][$ i ];
486
- $ this ->dbDriver ->pQuery ('UPDATE ' . $ this ->dbDriver ->targetSchema . '.catalog SET id=$2, level=level + 1 WHERE lower(id)=lower($1) ' , array (
487
- $ updateCatalogs ['id ' ],
488
- $ catalog ['id ' ] . '/ ' . $ updateCatalogs ['id ' ]
489
- ), 500 , 'Cannot update child link ' . $ updateCatalogs ['id ' ]);
490
- $ this ->dbDriver ->pQuery ('UPDATE ' . $ this ->dbDriver ->targetSchema . '.catalog_feature SET path=$2 WHERE path=$1 ' , array (
491
- RestoUtil::path2ltree ($ updateCatalogs ['id ' ]),
492
- RestoUtil::path2ltree ($ catalog ['id ' ] . '/ ' . $ updateCatalogs ['id ' ])
493
- ), 500 , 'Cannot update catalog feature association for child link ' . $ updateCatalogs ['id ' ]);
494
- }
495
-
496
481
return $ catalog ;
497
482
498
483
}
@@ -691,20 +676,19 @@ private function insertIntoCatalogFeature($featureId, $path, $catalogId, $collec
691
676
*
692
677
* @param array $catalog
693
678
* @param string userid
694
- * @param string $baseUrl
679
+ * @param string $context
695
680
* @return array
696
681
*/
697
- private function getCleanLinks ($ catalog , $ userid , $ baseUrl ) {
682
+ private function getCleanLinks ($ catalog , $ userid , $ context ) {
698
683
699
684
$ output = array (
700
685
'links ' => array (),
701
- 'updateCatalogs ' => array (),
702
686
'internalItems ' => array ()
703
687
);
704
688
705
689
if ( !isset ($ catalog ['links ' ]) ) {
706
690
return $ output ;
707
- }
691
+ };
708
692
709
693
for ($ i = 0 , $ ii = count ($ catalog ['links ' ]); $ i < $ ii ; $ i ++) {
710
694
$ link = $ catalog ['links ' ][$ i ];
@@ -723,18 +707,21 @@ private function getCleanLinks($catalog, $userid, $baseUrl) {
723
707
*/
724
708
if ( in_array ($ link ['rel ' ], array ('item ' , 'items ' )) ) {
725
709
726
- if ( !str_starts_with ($ link ['href ' ], $ baseUrl . RestoRouter::ROUTE_TO_COLLECTIONS ) ) {
710
+ if ( !str_starts_with ($ link ['href ' ], $ context -> core [ ' baseUrl ' ] . RestoRouter::ROUTE_TO_COLLECTIONS ) ) {
727
711
$ output ['links ' ][] = $ link ;
728
712
continue ;
729
713
}
730
714
731
- $ exploded = explode ('/ ' , substr ($ link ['href ' ], strlen ($ baseUrl . RestoRouter::ROUTE_TO_COLLECTIONS ) + 1 ));
715
+ $ exploded = explode ('/ ' , substr ($ link ['href ' ], strlen ($ context -> core [ ' baseUrl ' ] . RestoRouter::ROUTE_TO_COLLECTIONS ) + 1 ));
732
716
// A item endpoint is /collections/{collectionId}/items/{featureId}
733
717
if (count ($ exploded ) === 3 ) {
718
+
719
+ // Eventually convert collection alias to real collection id
720
+ $ collectionId = (new CollectionsFunctions ($ this ->dbDriver ))->aliasToCollectionId ($ exploded [0 ]) ?? $ exploded [0 ];
734
721
$ internalItem = array (
735
722
'id ' => RestoUtil::isValidUUID ($ exploded [2 ]) ? $ exploded [2 ] : RestoUtil::toUUID ($ exploded [2 ]),
736
723
'href ' => $ link ['href ' ],
737
- 'collection ' => $ exploded [ 0 ]
724
+ 'collection ' => $ collectionId
738
725
);
739
726
$ output ['internalItems ' ][] = $ internalItem ;
740
727
@@ -754,8 +741,8 @@ private function getCleanLinks($catalog, $userid, $baseUrl) {
754
741
/*
755
742
* Avoid cycling (i.e. catalog self referencing one of its parent)
756
743
*/
757
- if (str_starts_with ($ link ['href ' ], $ baseUrl . RestoRouter::ROUTE_TO_CATALOGS )) {
758
- $ exploded = explode ('/ ' , substr ($ link ['href ' ], strlen ($ baseUrl . RestoRouter::ROUTE_TO_CATALOGS ) + 1 ));
744
+ if (str_starts_with ($ link ['href ' ], $ context -> core [ ' baseUrl ' ] . RestoRouter::ROUTE_TO_CATALOGS )) {
745
+ $ exploded = explode ('/ ' , substr ($ link ['href ' ], strlen ($ context -> core [ ' baseUrl ' ] . RestoRouter::ROUTE_TO_CATALOGS ) + 1 ));
759
746
if ( count ($ exploded ) <= count (explode ('/ ' , $ catalog ['id ' ])) ) {
760
747
return RestoLogUtil::httpError (400 , 'Child ' . $ link ['href ' ] . ' is invalid because it references a parent resource ' );
761
748
}
@@ -764,29 +751,24 @@ private function getCleanLinks($catalog, $userid, $baseUrl) {
764
751
/*
765
752
* Store local collection within links
766
753
*/
767
- if (str_starts_with ($ link ['href ' ], $ baseUrl . RestoRouter::ROUTE_TO_COLLECTIONS )) {
754
+ if (str_starts_with ($ link ['href ' ], $ context -> core [ ' baseUrl ' ] . RestoRouter::ROUTE_TO_COLLECTIONS )) {
768
755
$ output ['links ' ][] = $ link ;
769
756
continue ;
770
757
}
771
758
772
759
}
773
760
774
- $ exploded = explode ($ baseUrl . RestoRouter::ROUTE_TO_CATALOGS . '/ ' , $ link ['href ' ]);
761
+ $ exploded = explode ($ context -> core [ ' baseUrl ' ] . RestoRouter::ROUTE_TO_CATALOGS . '/ ' , $ link ['href ' ]);
775
762
if ( count ($ exploded ) !== 2 ) {
776
- return RestoLogUtil::httpError (400 , 'One link child has an external href i.e. not starting with ' . $ baseUrl . RestoRouter::ROUTE_TO_CATALOGS );
763
+ return RestoLogUtil::httpError (400 , 'One link child has an external href i.e. not starting with ' . $ context -> core [ ' baseUrl ' ] . RestoRouter::ROUTE_TO_CATALOGS );
777
764
}
778
765
779
- $ childCatalog = $ this ->getCatalog ($ exploded [1 ], $ baseUrl );
766
+ $ childCatalog = $ this ->getCatalog ($ exploded [1 ], $ context -> core [ ' baseUrl ' ] );
780
767
if ( $ childCatalog === null ) {
781
- return RestoLogUtil::httpError (400 , 'Catalog child ' . $ link ['href ' ] . ' does not exist in database ' );
768
+ return RestoLogUtil::httpError (400 , 'Catalog child ' . $ link ['href ' ] . ' does not exist ' );
782
769
}
783
770
784
- if ($ childCatalog ['level ' ] === 1 && $ childCatalog ['owner ' ] === $ userid ) {
785
- array_push ($ output ['updateCatalogs ' ], ...$ this ->getCatalogs (array ('id ' => $ childCatalog ['id ' ]), $ baseUrl , true ));
786
- }
787
- else {
788
- $ output ['links ' ][] = $ link ;
789
- }
771
+ $ output ['links ' ][] = $ link ;
790
772
791
773
}
792
774
0 commit comments