diff --git a/core/kernel/persistence/smoothsql/class.Class.php b/core/kernel/persistence/smoothsql/class.Class.php index 3be8f14f1..9a3b09a4b 100644 --- a/core/kernel/persistence/smoothsql/class.Class.php +++ b/core/kernel/persistence/smoothsql/class.Class.php @@ -204,6 +204,16 @@ public function getInstances(core_kernel_classes_Class $resource, $recursive = f { $returnValue = []; + if (empty($params)) { + $resourceIds = $this->getInstanceUris($resource->getUri(), $recursive); + + foreach ($resourceIds as $resourceId) { + $returnValue[$resourceId] = $this->getModel()->getResource($resourceId); + } + + return $returnValue; + } + $params = array_merge($params, ['like' => false, 'recursive' => $recursive]); $query = $this->getFilteredQuery($resource, [], $params); @@ -630,4 +640,47 @@ public function updateUri(core_kernel_classes_Class $resource, string $newUri): ] ); } + + public function getInstanceUris(string $classUri, bool $recursive = false): array + { + if (!$recursive) { + $query = 'SELECT subject FROM statements WHERE predicate = ? AND object = ?'; + $params = [ + OntologyRdf::RDF_TYPE, + $classUri, + ]; + } else { + $query = <<<'SQL' +WITH RECURSIVE statements_tree AS ( + SELECT + r.subject, + r.predicate + FROM statements r + WHERE r.subject = ? + AND r.predicate IN (?, ?) + UNION ALL + SELECT + s.subject, + s.predicate + FROM statements s + JOIN statements_tree st + ON s.object = st.subject + WHERE s.predicate IN (?, ?) +) +SELECT subject FROM statements_tree WHERE predicate = ?; +SQL; + $params = [ + $classUri, + OntologyRdfs::RDFS_SUBCLASSOF, + OntologyRdf::RDF_TYPE, + OntologyRdfs::RDFS_SUBCLASSOF, + OntologyRdf::RDF_TYPE, + OntologyRdf::RDF_TYPE, + ]; + } + + $statement = $this->getPersistence()->query($query, $params); + + return array_column($statement->fetchAll(), 'subject'); + } }