relpipe-data/examples/relpipe-in-sparql.sh
branchv_0
changeset 310 aeda3cb4528d
child 328 cc60c8dd7924
equal deleted inserted replaced
309:71a627e72815 310:aeda3cb4528d
       
     1 #!/bin/bash
       
     2 
       
     3 # Relational pipes
       
     4 # Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
       
     5 # 
       
     6 # This program is free software: you can redistribute it and/or modify
       
     7 # it under the terms of the GNU General Public License as published by
       
     8 # the Free Software Foundation, version 3 of the License.
       
     9 # 
       
    10 # This program is distributed in the hope that it will be useful,
       
    11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13 # GNU General Public License for more details.
       
    14 # 
       
    15 # You should have received a copy of the GNU General Public License
       
    16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17 
       
    18 relpipe_in_sparql_help() {
       
    19 	cat <<-EOF
       
    20 		SPARQL query is expected on the STDIN. Exapmple:
       
    21 		  echo 'SELECT * WHERE { ?subject ?predicate ?object . } LIMIT 3' \
       
    22 | relpipe-in-sparql | relpipe-out-tabular
       
    23 		
       
    24 		Namespace prefixes are part of the query.
       
    25 		But because they are usually constant, they can be set as the 
       
    26 		RELPIPE_IN_SPARQL_PREFIXES environmental variable.
       
    27 		Then they are prepended to the query.
       
    28 		
       
    29 		SPARQL endpoint can be set ad-hoc by the --endpoint option.
       
    30 		
       
    31 		To configure default SPARQL endpoint, add something like this to your environment:
       
    32 		  export RELPIPE_IN_SPARQL_ENDPOINT="https://query.wikidata.org/sparql"
       
    33 		  export RELPIPE_IN_SPARQL_ENDPOINT="https://dbpedia.org/sparql"
       
    34 		  export RELPIPE_IN_SPARQL_ENDPOINT="https://data.gov.cz/sparql"
       
    35 		  export RELPIPE_IN_SPARQL_ENDPOINT="https://data.cssz.cz/sparql"
       
    36 
       
    37 		The relation name defaults to "rdf". Custom name can be set using the --relation option.
       
    38 EOF
       
    39 }
       
    40 
       
    41 relation="rdf";
       
    42 endpoint="${RELPIPE_IN_SPARQL_ENDPOINT:-https://dbpedia.org/sparql}";
       
    43 
       
    44 while [[ $# -gt 0 ]]; do
       
    45 	argument="$1";
       
    46 	case "$argument" in
       
    47 		"--relation") relation="$2"; shift; shift; ;;
       
    48 		"--endpoint") endpoint="$2"; shift; shift; ;;
       
    49 		"--help") relpipe_in_sparql_help; exit; ;;
       
    50 	esac
       
    51 done
       
    52 
       
    53 [[ -n "$RELPIPE_IN_SPARQL_PREFIXES" ]] && query="$RELPIPE_IN_SPARQL_PREFIXES"; query+=$'\n\n';
       
    54 
       
    55 query+="$(</dev/stdin)";
       
    56 
       
    57 
       
    58 # Simple implementation that utilizes the CSV output of the SPARQL endpoint.
       
    59 relpipe_in_sparql_implementation_csv() {
       
    60 	curl \
       
    61 		--header "Accept: text/csv" \
       
    62 		--data-urlencode query="$query" \
       
    63 		--fail \
       
    64 		--silent \
       
    65 		--show-error \
       
    66 		"$endpoint" \
       
    67 		| relpipe-in-csv "$relation"
       
    68 }
       
    69 
       
    70 # More powerful implementation based on XML.
       
    71 # Can be customized through XSLT.
       
    72 # But: has more dependencies and avoids streaming.
       
    73 relpipe_in_sparql_implementation_xml() {
       
    74 	DIR="$(dirname $(realpath "$0"))";
       
    75 	curl \
       
    76 		--header "Accept: application/sparql-results+xml" \
       
    77 		--data-urlencode query="$query" \
       
    78 		--fail \
       
    79 		--silent \
       
    80 		--show-error \
       
    81 		"$endpoint" \
       
    82 		| xsltproc --stringparam "relation" "$relation" "$DIR/relpipe-in-sparql.xsl" - \
       
    83 		| relpipe-in-xml
       
    84 }
       
    85 
       
    86 relpipe_in_sparql_implementation_${RELPIPE_IN_SPARQL_IMPLEMENTATION:-csv}