From 0ef685aa960f97c964e35e64af74068d4cd43ebb Mon Sep 17 00:00:00 2001 From: Vlad Byndych Date: Fri, 30 Jun 2023 17:54:14 +0200 Subject: [PATCH 1/2] feat: RDF migration from sql to neo4j - add extra parameter in order to be able to control format of the output - used specific node type to make sure resources added properly, as general add handle them incorrectly --- core/kernel/api/class.ModelExporter.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/core/kernel/api/class.ModelExporter.php b/core/kernel/api/class.ModelExporter.php index f2a1ebb32..3a6fe5efb 100755 --- a/core/kernel/api/class.ModelExporter.php +++ b/core/kernel/api/class.ModelExporter.php @@ -29,29 +29,33 @@ class core_kernel_api_ModelExporter /** * Export the entire ontology * + * @param string $format (optional) which format resulted ontology will be. + * * @return string */ - public static function exportAll() + public static function exportAll($format = 'rdfxml') { $dbWrapper = core_kernel_classes_DbWrapper::singleton(); $result = $dbWrapper->query('SELECT DISTINCT "subject", "predicate", "object", "l_language" FROM "statements"'); - return self::statement2rdf($result); + return self::statement2rdf($result, $format); } /** * Export models by id * * @param array $modelIds + * @param string $format (optional) which format resulted models ontology will be. + * * @return string */ - public static function exportModels($modelIds) + public static function exportModels($modelIds, $format = 'rdfxml') { $dbWrapper = core_kernel_classes_DbWrapper::singleton(); $result = $dbWrapper->query('SELECT DISTINCT "subject", "predicate", "object", "l_language" FROM "statements" WHERE "modelid" IN (\'' . implode('\',\'', $modelIds) . '\')'); common_Logger::i('Found ' . $result->rowCount() . ' entries for models ' . implode(',', $modelIds)); - return self::statement2rdf($result); + return self::statement2rdf($result, $format); } /** @@ -68,20 +72,20 @@ public static function exportModelByUri() * @throws \EasyRdf\Exception * @ignore */ - private static function statement2rdf(PDOStatement $statement) + private static function statement2rdf($statement, $format = 'rdfxml') { $graph = new Graph(); while ($r = $statement->fetch()) { if (isset($r['l_language']) && !empty($r['l_language'])) { $graph->addLiteral($r['subject'], $r['predicate'], $r['object'], $r['l_language']); } elseif (common_Utils::isUri($r['object'])) { - $graph->add($r['subject'], $r['predicate'], $r['object']); + $graph->addResource($r['subject'], $r['predicate'], $r['object']); } else { $graph->addLiteral($r['subject'], $r['predicate'], $r['object']); } } - $format = Format::getFormat('rdfxml'); + $format = Format::getFormat($format); return $graph->serialise($format); } From 14793d7e2ab9d3507a96c848eceade03bd76404a Mon Sep 17 00:00:00 2001 From: Vlad Byndych Date: Thu, 13 Jul 2023 15:03:52 +0200 Subject: [PATCH 2/2] feat: RDF migration from sql to neo4j - introduced constant for code flexibility. --- core/kernel/api/class.ModelExporter.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/core/kernel/api/class.ModelExporter.php b/core/kernel/api/class.ModelExporter.php index 3a6fe5efb..fa27657ff 100755 --- a/core/kernel/api/class.ModelExporter.php +++ b/core/kernel/api/class.ModelExporter.php @@ -26,6 +26,8 @@ class core_kernel_api_ModelExporter { + private const DEFAULT_FORMAT = 'rdfxml'; + /** * Export the entire ontology * @@ -33,7 +35,7 @@ class core_kernel_api_ModelExporter * * @return string */ - public static function exportAll($format = 'rdfxml') + public static function exportAll($format = self::DEFAULT_FORMAT) { $dbWrapper = core_kernel_classes_DbWrapper::singleton(); $result = $dbWrapper->query('SELECT DISTINCT "subject", "predicate", "object", "l_language" FROM "statements"'); @@ -48,7 +50,7 @@ public static function exportAll($format = 'rdfxml') * * @return string */ - public static function exportModels($modelIds, $format = 'rdfxml') + public static function exportModels($modelIds, $format = self::DEFAULT_FORMAT) { $dbWrapper = core_kernel_classes_DbWrapper::singleton(); $result = $dbWrapper->query('SELECT DISTINCT "subject", "predicate", "object", "l_language" FROM "statements" @@ -72,7 +74,7 @@ public static function exportModelByUri() * @throws \EasyRdf\Exception * @ignore */ - private static function statement2rdf($statement, $format = 'rdfxml') + private static function statement2rdf($statement, $format = self::DEFAULT_FORMAT) { $graph = new Graph(); while ($r = $statement->fetch()) {