forked from ontoportal/ontoportal_web_ui
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: add SPARQL endpoint UI (#427)
* add yasgui dependency to have a SPARQL editor * add sparql ednpoint proxy action * add SPARQL section to the admin page * add SPARQL section to the ontology viewer
- Loading branch information
1 parent
b498460
commit b5369a6
Showing
14 changed files
with
730 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
module SparqlHelper | ||
def change_from_clause(query, graph) | ||
unless graph.blank? | ||
graph = graph.gsub($REST_URL, 'http://data.bioontology.org') | ||
|
||
if query.match?(/FROM <[^>]+>/i) | ||
# Use a regular expression to replace all instances of FROM <uri> | ||
query = query.gsub(/FROM <[^>]+>/i, "") | ||
else | ||
query = query.gsub("WHERE", "FROM <#{graph}> WHERE") | ||
end | ||
|
||
query = query.gsub(/GRAPH <[^>]+>/i, "") | ||
end | ||
query | ||
end | ||
def ontology_sparql_query(query, graph = '') | ||
query = change_from_clause(query, graph) | ||
sparql_query(query) | ||
end | ||
def is_allowed_query?(sparql_query) | ||
forbidden_operations = [ | ||
'INSERT DATA', | ||
'DELETE DATA', | ||
'DELETE/INSERT', | ||
'DELETE', | ||
'INSERT', | ||
'DELETE WHERE', | ||
'LOAD', | ||
'CLEAR', | ||
'CREATE', | ||
'DROP', | ||
'COPY', | ||
'MOVE', | ||
'ADD' | ||
] | ||
|
||
# Define a regular expression to match SELECT queries | ||
select_query_regex = /\A\s*SELECT\b/m | ||
|
||
# Check if the query contains any forbidden operations outside SELECT queries | ||
return false if forbidden_operations.any? { |op| sparql_query.upcase.include?(op) && !sparql_query.match(select_query_regex) } | ||
|
||
true | ||
end | ||
|
||
def sparql_query(query) | ||
return 'No SPARQL endpoint configured' if $SPARQL_URL.blank? | ||
return 'INSERT Queries not permitted' unless is_allowed_query?(query) | ||
endpoint = $SPARQL_URL.gsub('test', 'sparql') | ||
begin | ||
conn = Faraday.new do |conn| | ||
conn.options.timeout = 60 | ||
end | ||
response = conn.get("#{endpoint}?query=#{encode_param(query)}") | ||
response.body.force_encoding('ISO-8859-1').encode('UTF-8') | ||
rescue | ||
"Query timeout" | ||
end | ||
end | ||
def sparql_query_container(graph: nil) | ||
content_tag(:div, '', data: {controller: 'sparql', | ||
'sparql-proxy-value': '/sparql_proxy/', | ||
'sparql-graph-value': graph}) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Controller } from '@hotwired/stimulus' | ||
import { getYasgui } from '../mixins/useYasgui' | ||
|
||
// Connects to data-controller="sparql" | ||
export default class extends Controller { | ||
static values = { | ||
proxy: String, | ||
graph: String, | ||
} | ||
connect () { | ||
localStorage.removeItem('yagui__config'); | ||
this.yasgui = getYasgui(this.element, | ||
{ | ||
corsProxy: this.proxyValue, | ||
copyEndpointOnNewTab: true, | ||
requestConfig: { | ||
endpoint: this.proxyValue, | ||
acceptHeaderGraph: false, | ||
acceptHeaderUpdate: false, | ||
namedGraphs: [this.graphValue], | ||
} | ||
}) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Yasgui from "@triply/yasgui"; | ||
|
||
export const getYasgui = (elem, config) => { | ||
return new Yasgui(elem, config) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
= render TurboFrameComponent.new(id: "sparql", data: {"turbo-frame-target": "frame"} ) do | ||
= sparql_query_container(graph: @submission_latest.id ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.