From 471de9ace138eba6eeeceff018f729491e650212 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Wed, 11 Sep 2024 13:16:22 +0300 Subject: Fetch the next comment version in RDF. * gn3/db/rdf/wiki.py (get_next_comment_version): New function. Signed-off-by: Munyoki Kilyungi --- gn3/db/rdf/wiki.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/gn3/db/rdf/wiki.py b/gn3/db/rdf/wiki.py index feda5bd..2864b2b 100644 --- a/gn3/db/rdf/wiki.py +++ b/gn3/db/rdf/wiki.py @@ -10,7 +10,8 @@ NOTE: In the CONSTRUCT queries below, we manually sort the arrays from """ from string import Template -from gn3.db.rdf import BASE_CONTEXT, RDF_PREFIXES, query_frame_and_compact +from gn3.db.rdf import BASE_CONTEXT, RDF_PREFIXES, query_frame_and_compact, sparql_query +from gn3.db.wiki import MissingDBDataException WIKI_CONTEXT = BASE_CONTEXT | { @@ -180,3 +181,25 @@ CONSTRUCT { # See note above in the doc-string results["data"] = sorted(data, key=lambda d: d["version"], reverse=True) return results + + +def get_next_comment_version( + comment_id: int, sparql_uri: str, graph: str = "" +) -> int: + "Find the next version to add" + query = Template( + """ +$prefix + +SELECT MAX(?version) as ?max_version FROM $graph WHERE { + ?comment rdf:type gnc:GNWikiEntry ; + dct:identifier "$comment_id"^^xsd:integer ; + dct:hasVersion ?version . +} +""" + ).substitute(prefix=RDF_PREFIXES, graph=graph, comment_id=comment_id) + results = sparql_query( + query=query, endpoint=sparql_uri, format_type="json")[0] + if not results: + raise MissingDBDataException + return int(results["max_version"]["value"]) + 1 -- cgit v1.2.3