Strabon

view scripts/endpoint @ 1412:8dc244d8f18a

corrected some tests that were expecting the default SRID (4326) for WKT literals; from now, we do not include such URI, since this is the default and is assumed
author Babis Nikolaou <charnik@di.uoa.gr>
date Mon Sep 22 13:12:18 2014 +0300 (2014-09-22)
parents 93f5d7b2d3cb
children da14a303bf2c
line source
1 #!/bin/bash
3 #
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 #
8 # Copyright (C) 2010, 2011, 2012, Pyravlos Team
9 #
10 # http://www.strabon.di.uoa.gr/
11 #
13 #
14 # Script for executing SPARQL queries and SPARQL Update queries
15 # as well as storing RDF triples on a Strabon Endpoint.
16 #
17 # Author: Charalampos (Babis) Nikolaou <charnik@di.uoa.gr>
18 # Author: Panayiotis Smeros <psmeros@di.uoa.gr>
19 #
21 # absolute directory name of this command
22 LOC="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
24 # this command
25 CMD="$(basename ${0})"
27 # Filename containing the prefixes to be included in SPARQL queries
28 PREFIXES_FILE="${LOC}/prefixes.sparql"
30 function help() {
31 echo "Usage: ${CMD} [OPTIONS] COMMAND ENDPOINT ARGS"
32 echo
33 echo "Execute SPARQL and SPARQL Update queries as well as store RDF triples on a Strabon endpoint."
34 echo
35 echo " COMMAND : one of query, queryfile, update, store, describe, or help"
36 echo " ENDPOINT : the URL of the Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint)"
37 echo " ARGS : arguments according to selected command"
38 echo
39 echo "OPTIONS can be any of the following"
40 echo " -d : don't run, just print what shall be executed"
41 echo " -i : include URI prefixes in the SPARQL query. Prefixes are taken from file"
42 echo " \`prefixes.sparql'"
43 }
45 function help_query() {
46 echo "Usage: ${CMD} query ENDPOINT SPARQL_QUERY [RESULT_FORMAT]"
47 echo
48 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
49 echo " SPARQL_QUERY : the SPARQL query to execute or the alias name corresponding to a"
50 echo " predefined query such as:"
51 echo " \`size': SELECT (count(*) as ?c) WHERE {?s ?p ?o}"
52 echo " RESULT_FORMAT : the format of the result. Possible values are \`XML' (default), \`KML', \`KMZ', \`GeoJSON', \`HTML', or \`TSV'"
53 }
55 function help_queryfile() {
56 echo "Usage: ${CMD} queryfile ENDPOINT SPARQL_QUERY_FILE [RESULT_FORMAT]"
57 echo
58 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
59 echo " SPARQL_QUERY_FILE : the file that contain the SPARQL query to execute"
60 echo " RESULT_FORMAT : the format of the result. Possible values are \`KMLMAP\, \`GEOJSON', "
61 echo " \`HTML', \`KMZMAP', \`XML' (default), or \`KML'."
62 }
64 function help_update() {
65 echo "Usage: ${CMD} update ENDPOINT SPARQL_QUERY"
66 echo
67 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
68 echo " SPARQL_QUERY : the SPARQL update query to execute or the alias name corresponding to a"
69 echo " predefined query such as:"
70 echo " \`clear': DELETE {?s ?p ?o} WHERE {?s ?p ?o}"
71 }
73 function help_store() {
74 echo "Usage: ${CMD} store ENDPOINT FORMAT [-g NAMED_GRAPH] [--inference] -t TRIPLES|-u TRIPLES_URL"
75 echo
76 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
77 echo " FORMAT : the RDF format of the input (one of RDF/XML, N-Triples, Turtle, N3, TriX, TriG, or BinaryRDF)"
78 echo " TRIPLES : the RDF triples to store"
79 echo " TRIPLES_URL : the URL containing the RDF triples to store"
80 echo " NAMED_GRAPH : the URI of the named graph into which the RDF files shall be stored"
81 echo " (defaults to the default graph)."
82 echo " --inference : enables inference."
83 }
85 function help_describe() {
86 echo "Usage: ${CMD} describe ENDPOINT DESCRIBE_QUERY [RESULT_FORMAT]"
87 echo
88 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
89 echo " DESCRIBE_QUERY : the SPARQL DESCRIBE query to execute"
90 echo " RESULT_FORMAT : the format of the result. Possible values are \`N-Triples' (default)"
91 echo " \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'"
92 }
94 CURL_OPTS="-w HTTP_CODE='%{http_code}\n' -H \"Content-Type:application/x-www-form-urlencoded\" -H \"Accept:text/xml\""
96 # if set to 1, then only the command to be executed is printed
97 DEBUG=0
98 # set default limit
99 MAXLIMIT=0
100 while test $# -gt 0 -a "X${1:0:1}" == "X-"; do
101 case "${1}" in
102 -d)
103 shift
104 DEBUG=1
105 ;;
106 -i)
107 shift
108 PREFIXES="$(cat ${PREFIXES_FILE})"
109 ;;
110 -l)
111 MAXLIMIT="${2}"
112 shift
113 shift
114 ;;
115 esac
116 done
118 case "${1}" in
119 help)
120 shift
121 if test $# -eq 1; then
122 case "${1}" in
123 query)
124 help_query
125 ;;
126 queryfile)
127 help_queryfile
128 ;;
129 update)
130 help_update
131 ;;
132 store)
133 help_store
134 ;;
135 describe)
136 help_describe
137 ;;
138 *)
139 help
140 ;;
141 esac
142 exit 1
143 fi
144 help
145 exit 0
146 ;;
147 query)
148 shift
149 if ! test $# -ge 2; then
150 help_query
151 exit 1
152 fi
153 URL="${1}/Query"
154 QUERY="${2}"
156 shift
157 shift
158 # predefined queries
159 case "${QUERY}" in
160 size)
161 QUERY="SELECT (count(*) as ?c) WHERE {?s ?p ?o}"
162 ;;
163 esac
165 # set default format
166 FORMAT="XML"
167 if test $# -eq 1; then
168 FORMAT="${1}"
169 fi
171 case "${FORMAT}" in
172 [Xx][Mm][Ll])
173 MIME_TYPE="application/sparql-results+xml"
174 ;;
175 [Tt][sS][vV])
176 MIME_TYPE="text/tab-separated-values"
177 ;;
178 [Kk][Mm][Ll])
179 MIME_TYPE="application/vnd.google-earth.kml+xml"
180 ;;
181 [Kk][mM][Zz])
182 MIME_TYPE="application/vnd.google-earth.kmz"
183 ;;
184 [Hh][Tt][Mm][Ll])
185 MIME_TYPE="text/html"
186 ;;
187 [Gg][Ee][Oo][Jj][Ss][Oo][Nn])
188 MIME_TYPE="application/json"
189 ;;
190 *)
191 echo "${CMD}: unknown format \"${FORMAT}\"."
192 echo "${CMD}: possible values are \`KML', \`KMZ', \`GeoJSON', \`XML' (default), \`HTML', or \`TSV'"
193 exit 2
194 ;;
195 esac
197 EXEC="curl -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode query='${PREFIXES}${QUERY}' --data maxLimit='${MAXLIMIT}' ${URL}"
198 ;;
199 queryfile)
200 shift
201 if ! test $# -ge 2; then
202 help_queryfile
203 exit 1
204 fi
206 if ! test -f ${2}; then
207 echo "${CMD}: File not found."
208 exit 2
209 fi
211 URL="${1}/Query"
212 QUERY=`cat ${2} | sed 's/\"/\\\"/g'`
214 shift
215 shift
217 # set default format
218 FORMAT="XML"
219 if test $# -eq 1; then
220 FORMAT="${1}"
221 fi
223 case "${FORMAT}" in
224 [Xx][Mm][Ll])
225 MIME_TYPE="application/sparql-results+xml"
226 ;;
227 [Tt][sS][vV])
228 MIME_TYPE="text/tab-separated-values"
229 ;;
230 [Kk][Mm][Ll])
231 MIME_TYPE="application/vnd.google-earth.kml+xml"
232 ;;
233 [Kk][mM][Zz])
234 MIME_TYPE="application/vnd.google-earth.kmz"
235 ;;
236 [Hh][Tt][Mm][Ll])
237 MIME_TYPE="text/html"
238 ;;
239 [Gg][Ee][Oo][Jj][Ss][Oo][Nn])
240 MIME_TYPE="application/json"
241 ;;
242 *)
243 echo "${CMD}: unknown format \"${FORMAT}\"."
244 echo "${CMD}: possible values are \`KML', \`KMZ', \`GeoJSON', \`XML' (default), \`HTML', or \`TSV'"
245 exit 2
246 ;;
247 esac
249 EXEC="curl -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode query='${PREFIXES}${QUERY}' ${URL}"
250 ;;
252 update)
253 shift
254 if ! test $# -eq 2; then
255 help_update
256 exit 1
257 fi
258 URL="${1}/Update"
259 QUERY="${2}"
261 # predefined queries
262 case "${QUERY}" in
263 clear)
264 QUERY="DELETE {?s ?p ?o} WHERE {?s ?p ?o}"
265 ;;
266 esac
268 EXEC="curl -u endpoint:3ndpo1nt ${CURL_OPTS} --data-urlencode query='${PREFIXES}${QUERY}' ${URL}"
269 ;;
270 store)
271 shift
272 if ! test $# -ge 4; then
273 help_store
274 exit 1
275 fi
276 URL="${1}/Store"
277 shift
278 FORMAT="${1}"
279 shift
280 MIME_TYPE=
281 case "${FORMAT}" in
282 N-Triples)
283 MIME_TYPE="text/plain"
284 ;;
285 RDF/XML)
286 MIME_TYPE="application/rdf+xml"
287 ;;
288 N3)
289 MIME_TYPE="text/rdf+n3"
290 ;;
291 TURTLE)
292 MIME_TYPE="text/turtle"
293 ;;
294 TRIG)
295 MIME_TYPE="application/x-trig"
296 ;;
297 TRIX)
298 MIME_TYPE="application/trix"
299 ;;
300 BinaryRDF)
301 MIME_TYPE="application/x-binary-rdf"
302 ;;
303 *)
304 echo "${CMD}: unknown format \"${FORMAT}\"."
305 echo "${CMD}: possible values are \`N-Triples' (default), \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'"
306 exit 2
307 ;;
308 esac
310 GRAPH=
311 case "${1}" in
312 -g)
313 shift
314 if [ "${1}" == "--inference" ]; then
315 help_store
316 exit 1
317 fi
318 if ! test $# -ge 3; then
319 help_store
320 exit 1
321 fi
322 GRAPH="-d graph=${1}"
323 shift
324 esac
326 INFERENCE=
327 case "${1}" in
328 --inference)
329 shift
330 if ! test $# -ge 2; then
331 help_store
332 exit 1
333 fi
334 INFERENCE="-d inference=true"
335 esac
337 case "${1}" in
338 -t)
339 shift
340 if ! test $# -eq 1; then
341 help_store
342 exit 1
343 fi
344 TRIPLES="${1}"
345 EXEC="curl -u endpoint:3ndpo1nt -w '\nHTTP_CODE=%{http_code}\n' -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode data='${TRIPLES}' ${GRAPH} ${INFERENCE} ${URL}"
346 ;;
347 -u)
348 shift
349 if ! test $# -eq 1; then
350 help_store
351 exit 1
352 fi
353 URL_TRIPLES="${1}"
354 EXEC="curl -u endpoint:3ndpo1nt -w '\nHTTP_CODE=%{http_code}\n' -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode url='${URL_TRIPLES}' -d fromurl='' ${GRAPH} ${INFERENCE} ${URL}"
355 ;;
356 *)
357 help_store
358 exit 1
359 ;;
360 esac
361 ;;
362 describe)
363 shift
364 if ! test $# -ge 2; then
365 help_describe
366 exit 1
367 fi
368 URL="${1}/Describe"
369 QUERY="${2}"
371 shift
372 shift
374 # set default format
375 FORMAT="N-Triples"
376 if test $# -eq 1; then
377 FORMAT="${1}"
378 fi
380 MIME_TYPE=
381 case "${FORMAT}" in
382 N-Triples)
383 MIME_TYPE="text/plain"
384 ;;
385 RDF/XML)
386 MIME_TYPE="application/rdf+xml"
387 ;;
388 N3)
389 MIME_TYPE="text/rdf+n3"
390 ;;
391 TURTLE)
392 MIME_TYPE="text/turtle"
393 ;;
394 TRIG)
395 MIME_TYPE="application/x-trig"
396 ;;
397 TRIX)
398 MIME_TYPE="application/trix"
399 ;;
400 BinaryRDF)
401 MIME_TYPE="application/x-binary-rdf"
402 ;;
403 *)
404 echo "${CMD}: unknown format \"${FORMAT}\"."
405 echo "${CMD}: possible values are \`N-Triples' (default), \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'"
406 exit 2
407 ;;
408 esac
410 EXEC="curl -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode query='${PREFIXES}${QUERY}' ${URL}"
411 ;;
412 *)
413 help
414 echo
415 echo "${CMD}: unknown command \"${1}\"."
416 exit 1
417 ;;
418 esac
420 # execute or debug
421 if test $DEBUG -eq 1; then
422 echo "${CMD}: Debug is ON"
423 echo "${CMD}: Printing command for execution"
424 echo " $EXEC"
425 else
426 eval "${EXEC}"
427 fi