Skip to content

Instantly share code, notes, and snippets.

@lawlesst
Last active August 30, 2018 20:05
Show Gist options
  • Save lawlesst/6300573 to your computer and use it in GitHub Desktop.
Save lawlesst/6300573 to your computer and use it in GitHub Desktop.
Example of using SPARQLWrapper to update data in VIVO .
Sample scripts for using the VIVO SPARQL Update API in PHP, Python.
https://wiki.duraspace.org/display/VIVO/The+SPARQL+Update+API
<?php
/**
Simple PHP example using the PHP Requests library - http://requests.ryanmccue.info/
*/
// Include Requests
include('vendor/autoload.php');
Requests::register_autoloader();
$q = 'PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>'.
'INSERT DATA {'.
'GRAPH <http://vitro.mannlib.cornell.edu/default/vitro-kb-2>'.
'{ <http://vivo.school.edu/individual/n0001> rdfs:label "New Title" . }'.
'}';
$request = Requests::post(
'http://localhost:8080/vivo/api/sparqlUpdate',
array(),
array(
'update' => $q,
'email' => '[email protected]',
'password' => 'XXXX'
)
);
// Dump response
var_dump($request);
"""
Examples of using VIVO's SPARQL update API.
https://wiki.duraspace.org/display/VIVO/SPARQL+Update+API
For SPARQL update doucmentation, see:
http://www.w3.org/Submission/SPARQL-Update/
"""
import urllib
import time
email = '[email protected]'
password = '----'
endpoint = 'http://localhost:8080/vivo/api/sparqlUpdate'
def do_update(query):
print "Query:\n" + query
payload = {
'email': email,
'password': password,
'update': query
}
data = urllib.urlencode(payload)
response = urllib.urlopen(endpoint, data)
#This will raise an expection if something goes wrong
if response.code != 200:
raise Exception("SPARQL update failed. Status code: {}".format(str(response.code)))
print "Status code:", response.code
return True
#Insert data.
q = """
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
INSERT DATA {
GRAPH <http://localhost/api-test>
{ <http://vivo.school.edu/individual/n1234> rdfs:label "New Title" . }
}
"""
do_update(q)
#For testing purposes only - delay to check data in web app
time.sleep(3)
#Modify it
q = """
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bibo: <http://purl.org/ontology/bibo/>
DELETE DATA {
GRAPH <http://localhost/api-test>
{ <http://vivo.school.edu/individual/n1234> rdfs:label "New Title" . }
} ;
INSERT DATA {
GRAPH <http://localhost/api-test>
{ <http://vivo.school.edu/individual/n1234> rdfs:label "Second Title" . }
}
"""
do_update(q)
time.sleep(3)
#Delete it
q = """
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
DELETE DATA {
GRAPH <http://localhost/api-test>
{ <http://vivo.school.edu/individual/n1234> rdfs:label "Second Title" . }
}
"""
do_update(q)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment