Skip to content

Commit

Permalink
Merge branch 'release-15.38.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Oct 4, 2024
2 parents e9cd24f + e5480a8 commit 5bbe529
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions core/kernel/persistence/smoothsql/class.Class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
}
}

0 comments on commit 5bbe529

Please sign in to comment.