@@ -142,19 +142,17 @@ public function getCatalogs($params, $baseUrl, $withChilds)
142
142
try {
143
143
$ results = $ this ->dbDriver ->pQuery ('SELECT id, title, description, level, counters, owner, links, visibility, rtype, to_iso8601(created) as created FROM ' . $ this ->dbDriver ->targetSchema . '.catalog ' . ( empty ($ where ) ? '' : ' WHERE ' . join (' AND ' , $ where ) . ' ORDER BY id ASC ' ), $ values );
144
144
while ($ result = pg_fetch_assoc ($ results )) {
145
-
146
- /*
147
- * Recursively add child collection counters to catalog counters
148
- */
149
- $ result = $ this ->onTheFlyUpdateCountersWithCollection ($ result , $ baseUrl );
150
-
151
145
$ catalogs [] = CatalogsFunctions::format ($ result );
152
146
}
153
147
} catch (Exception $ e ) {
154
148
RestoLogUtil::httpError (500 , $ e ->getMessage ());
155
149
}
156
150
157
- return $ catalogs ;
151
+ /*
152
+ * Recursively add child collection counters to catalog counters
153
+ */
154
+ return $ this ->onTheFlyUpdateCountersWithCollection ($ catalogs , $ params ['id ' ] ?? null , $ baseUrl );
155
+
158
156
}
159
157
160
158
/**
@@ -591,7 +589,7 @@ public function getSummaries($types, $baseUrl)
591
589
'collection ' => array (
592
590
array (
593
591
'const ' => $ _collectionId ,
594
- 'count ' => $ catalogs [$ i ]['counters ' ]['collections ' ][$ _collectionId ]
592
+ 'count ' => $ catalogs [$ i ]['counters ' ]['collections ' ][$ _collectionId ] ?? 0
595
593
)
596
594
)
597
595
);
@@ -789,72 +787,91 @@ private function getCleanLinks($catalog, $userid, $context) {
789
787
* Return an update links array and counters object by adding child collection counters
790
788
* to the input catalog
791
789
*
792
- * @param Object $catalog
790
+ * @param array $catalogs
791
+ * @param string $catalogId
792
+ * @param string $baseUrl
793
793
*/
794
- private function onTheFlyUpdateCountersWithCollection ($ catalog , $ baseUrl )
794
+ private function onTheFlyUpdateCountersWithCollection ($ catalogs , $ catalogId , $ baseUrl )
795
795
{
796
796
797
- $ counters = json_decode ($ catalog ['counters ' ], true );
798
- $ originalLinks = json_decode ($ catalog ['links ' ], true );
799
-
800
797
$ collections = array ();
801
-
802
- $ 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 (
803
- $ catalog ['id ' ],
804
- $ catalog ['id ' ] . '/% '
805
- ));
806
- while ($ result = pg_fetch_assoc ($ results )) {
807
798
808
- if ($ catalog ['id ' ] !== $ result ['id ' ]) {
809
- $ catalogCounters = json_decode ($ result ['counters ' ], true );
810
- $ counters ['total ' ] = $ counters ['total ' ] + $ catalogCounters ['total ' ];
799
+ // First get collections counts
800
+ try {
801
+ $ results = $ this ->dbDriver ->query ('SELECT id, counters, title, description FROM ' . $ this ->dbDriver ->targetSchema . '.catalog WHERE lower(id) LIKE lower( \'collections/% \') ' );
802
+ while ($ result = pg_fetch_assoc ($ results )) {
803
+ $ collections [$ result ['id ' ]] = array (
804
+ 'counters ' => json_decode ($ result ['counters ' ], true ),
805
+ 'title ' => $ result ['title ' ] ?? null ,
806
+ 'description ' => $ result ['description ' ] ?? null ,
807
+
808
+ );
811
809
}
810
+ } catch (Exception $ e ) {
811
+ RestoLogUtil::httpError (500 , $ e ->getMessage ());
812
+ }
813
+
814
+ $ catalogsUpdated = array ();
815
+ for ($ i = 0 , $ ii = count ($ catalogs ); $ i < $ ii ; $ i ++)
816
+ {
817
+ $ catalogsUpdated [] = $ this ->computeCountersSum ($ catalogs [$ i ], $ catalogs , $ collections );
818
+ }
819
+
820
+ return $ catalogsUpdated ;
821
+ }
822
+
823
+ /**
824
+ * Calculate the total counter for a given path and its children
825
+ *
826
+ * @param array $parentCatalog
827
+ * @param array $catalogs
828
+ * @param array $collections
829
+ */
830
+ private function computeCountersSum ($ parentCatalog , $ catalogs , $ collections ) {
831
+
832
+ $ parentCatalogId = $ parentCatalog ['id ' ] . '/ ' ;
833
+
834
+ // Iterate over all catalog entries
835
+ foreach ($ catalogs as $ catalog ) {
812
836
813
- // Process collection
814
- if ( isset ($ result ['links ' ]) ) {
815
- $ links = json_decode ($ result ['links ' ], true );
816
- for ($ i = 0 , $ ii = count ($ links ); $ i < $ ii ; $ i ++) {
817
- if ($ links [$ i ]['rel ' ] === 'child ' ) {
818
- $ exploded = explode ('/ ' , substr ($ links [$ i ]['href ' ], strlen ($ baseUrl . RestoRouter::ROUTE_TO_COLLECTIONS ) + 1 ));
819
- if (count ($ exploded ) === 1 ) {
820
- $ collections [$ exploded [0 ]] = $ links [$ i ]['href ' ];
821
- }
822
- }
823
- }
837
+ // Check if the catalog's path starts with the parent path
838
+ if ( !str_starts_with ($ catalog ['id ' ], $ parentCatalogId ) ) {
839
+ continue ;
824
840
}
825
- }
826
841
827
- // Now process links
828
- foreach (array_keys ($ collections ) as $ collectionId ) {
829
- $ results = $ this ->dbDriver ->pQuery ('SELECT title, description, counters FROM ' . $ this ->dbDriver ->targetSchema . '.catalog WHERE lower(id) = lower($1) ORDER BY id ASC ' , array (
830
- 'collections/ ' . $ collectionId
831
- ));
832
- while ($ result = pg_fetch_assoc ($ results )) {
833
- $ collectionCounters = json_decode ($ result ['counters ' ], true );
834
- $ counters ['total ' ] = $ counters ['total ' ] + $ collectionCounters ['total ' ];
835
- $ counters ['collections ' ][$ collectionId ] = $ collectionCounters ['total ' ];
836
- for ($ i = 0 , $ ii = count ($ originalLinks ); $ i < $ ii ; $ i ++) {
837
- if ($ originalLinks [$ i ]['rel ' ] === 'child ' ) {
838
- $ exploded = explode ('/ ' , substr ($ originalLinks [$ i ]['href ' ], strlen ($ baseUrl . RestoRouter::ROUTE_TO_COLLECTIONS ) + 1 ));
839
- if (count ($ exploded ) === 1 && $ exploded [0 ] === $ collectionId ) {
840
- $ originalLinks [$ i ]['matched ' ] = $ collectionCounters ['total ' ];
841
- if ( isset ($ result ['title ' ]) ) {
842
- $ originalLinks [$ i ]['title ' ] = $ result ['title ' ];
843
- }
844
- if ( isset ($ result ['description ' ]) ) {
845
- $ originalLinks [$ i ]['description ' ] = $ result ['description ' ];
842
+ $ parentCatalog ['counters ' ]['total ' ] = $ parentCatalog ['counters ' ]['total ' ] + $ catalog ['counters ' ]['total ' ];
843
+
844
+ // Process collection
845
+ if ( isset ($ catalog ['links ' ]) ) {
846
+ for ($ i = 0 , $ ii = count ($ catalog ['links ' ]); $ i < $ ii ; $ i ++) {
847
+ if ($ catalog ['links ' ][$ i ]['rel ' ] === 'child ' ) {
848
+ $ exploded = explode ('/ ' , substr ($ catalog ['links ' ][$ i ]['href ' ], strlen ($ baseUrl . RestoRouter::ROUTE_TO_COLLECTIONS ) + 1 ));
849
+ if ( count ($ exploded ) === 1 && isset ($ collections [$ exploded [0 ]]) ) {
850
+ $ total = $ total + $ collections [$ exploded [0 ]]['counters ' ]['total ' ];
851
+ $ parentCatalog ['counters ' ]['collections ' ][$ exploded [0 ]] = $ collections [$ exploded [0 ]]['counters ' ]['total ' ];
852
+
853
+ for ($ j = 0 , $ jj = count ($ parentCatalog ['links ' ]); $ j < $ jj ; $ j ++) {
854
+ if ($ parentCatalog ['links ' ][$ j ]['rel ' ] === 'child ' ) {
855
+ $ exploded2 = explode ('/ ' , substr ($ parentCatalog ['links ' ][$ j ]['href ' ], strlen ($ baseUrl . RestoRouter::ROUTE_TO_COLLECTIONS ) + 1 ));
856
+ if (count ($ exploded2 ) === 1 && $ exploded2 [0 ] === $ exploded [0 ]) {
857
+ $ parentCatalog ['links ' ][$ j ]['matched ' ] = $ collectionCounters [$ exploded [0 ]];
858
+ if ( isset ($ collections [$ exploded [0 ]]['title ' ]) ) {
859
+ $ parentCatalog ['links ' ][$ i ]['title ' ] = $ collections [$ exploded [0 ]]['title ' ];
860
+ }
861
+ if ( isset ($ collections [$ exploded [0 ]]['description ' ]) ) {
862
+ $ parentCatalog ['links ' ][$ i ]['description ' ] = $ collections [$ exploded [0 ]]['description ' ];
863
+ }
864
+ }
865
+ }
846
866
}
847
867
}
848
868
}
849
- }
869
+ }
850
870
}
851
- }
852
871
853
- $ catalog ['counters ' ] = json_encode ($ counters , JSON_UNESCAPED_SLASHES );
854
- $ catalog ['links ' ] = json_encode ($ originalLinks , JSON_UNESCAPED_SLASHES );
855
-
856
- return $ catalog ;
872
+ }
857
873
874
+ return $ parentCatalog ;
858
875
}
859
876
860
877
}
0 commit comments