Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions bin/imports/create-query.sh → bin/add-construct.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,24 @@ print_usage()
{
printf "Creates a SPARQL CONSTRUCT query.\n"
printf "\n"
printf "Usage: %s options\n" "$0"
printf "Usage: %s options TARGET_URI\n" "$0"
printf "\n"
printf "Options:\n"
printf " -f, --cert-pem-file CERT_FILE .pem file with the WebID certificate of the agent\n"
printf " -p, --cert-password CERT_PASSWORD Password of the WebID certificate\n"
printf " -b, --base BASE_URI Base URI of the application\n"
printf " --proxy PROXY_URL The host this request will be proxied through (optional)\n"
printf "\n"
printf " --title TITLE Title of the chart\n"
printf " --description DESCRIPTION Description of the chart (optional)\n"
printf " --slug STRING String that will be used as URI path segment (optional)\n"
printf " --title TITLE Title of the query\n"
printf " --description DESCRIPTION Description of the query (optional)\n"
printf " --uri URI URI of the query (optional)\n"
printf "\n"
printf " --query-file ABS_PATH Absolute path to the text file with the SPARQL query string\n"
printf " --service SERVICE_URI URI of the SPARQL service specific to this query (optional)\n"
}

hash turtle 2>/dev/null || { echo >&2 "turtle not on \$PATH. Aborting."; exit 1; }

urlencode() {
python -c 'import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1], sys.argv[2]))' \
"$1" "$urlencode_safe"
}

args=()
while [[ $# -gt 0 ]]
do
Expand Down Expand Up @@ -63,8 +59,8 @@ do
shift # past argument
shift # past value
;;
--slug)
slug="$2"
--uri)
uri="$2"
shift # past argument
shift # past value
;;
Expand All @@ -73,6 +69,11 @@ do
shift # past argument
shift # past value
;;
--service)
service="$2"
shift # past argument
shift # past value
;;
*) # unknown arguments
args+=("$1") # save it in an array for later
shift # past argument
Expand All @@ -81,6 +82,8 @@ do
done
set -- "${args[@]}" # restore args

target="$1"

if [ -z "$cert_pem_file" ] ; then
print_usage
exit 1
Expand All @@ -102,43 +105,38 @@ if [ -z "$query_file" ] ; then
exit 1
fi

if [ -z "$slug" ] ; then
slug=$(uuidgen | tr '[:upper:]' '[:lower:]') # lowercase
fi
encoded_slug=$(urlencode "$slug")

container="${base}queries/"
query=$(<"$query_file") # read query string from file

target="${container}${encoded_slug}/"

args+=("-f")
args+=("$cert_pem_file")
args+=("-p")
args+=("$cert_password")
args+=("-t")
args+=("text/turtle") # content type
args+=("$target")
if [ -n "$proxy" ]; then
args+=("--proxy")
args+=("$proxy")
fi

if [ -n "$uri" ] ; then
subject="<${uri}>"
else
subject="_:subject"
fi

turtle+="@prefix ldh: <https://w3id.org/atomgraph/linkeddatahub#> .\n"
turtle+="@prefix dh: <https://www.w3.org/ns/ldt/document-hierarchy#> .\n"
turtle+="@prefix dct: <http://purl.org/dc/terms/> .\n"
turtle+="@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n"
turtle+="@prefix sp: <http://spinrdf.org/sp#> .\n"
turtle+="_:query a sp:Construct .\n"
turtle+="_:query dct:title \"${title}\" .\n"
turtle+="_:query sp:text \"\"\"${query}\"\"\" .\n"
turtle+="<${target}> a dh:Item .\n"
turtle+="<${target}> foaf:primaryTopic _:query .\n"
turtle+="<${target}> dct:title \"${title}\" .\n"
turtle+="${subject} a sp:Construct .\n"
turtle+="${subject} dct:title \"${title}\" .\n"
turtle+="${subject} sp:text \"\"\"${query}\"\"\" .\n"

if [ -n "$service" ] ; then
turtle+="${subject} ldh:service <${service}> .\n"
fi
if [ -n "$description" ] ; then
turtle+="_:query dct:description \"${description}\" .\n"
turtle+="${subject} dct:description \"${description}\" .\n"
fi

# submit Turtle doc to the server
echo -e "$turtle" | turtle --base="$target" | put.sh "${args[@]}"
echo -e "$turtle" | turtle --base="$target" | post.sh "${args[@]}"
67 changes: 5 additions & 62 deletions bin/imports/create-file.sh → bin/add-file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ print_usage()
{
printf "Uploads a file.\n"
printf "\n"
printf "Usage: %s options\n" "$0"
printf "Usage: %s options TARGET_URI\n" "$0"
printf "\n"
printf "Options:\n"
printf " -f, --cert-pem-file CERT_FILE .pem file with the WebID certificate of the agent\n"
Expand All @@ -14,22 +14,14 @@ print_usage()
printf " --proxy PROXY_URL The host this request will be proxied through (optional)\n"
printf "\n"
printf " --title TITLE Title of the file\n"
printf " --container CONTAINER_URI URI of the parent container (optional)\n"
printf " --description DESCRIPTION Description of the file (optional)\n"
printf " --slug STRING String that will be used as URI path segment (optional)\n"
printf "\n"
printf " --file ABS_PATH Absolute path to the file\n"
printf " --file-content-type MEDIA_TYPE Media type of the file (optional)\n"
#printf " --file-slug STRING String that will be used as the file's URI path segment (optional)\n"
}

hash curl 2>/dev/null || { echo >&2 "curl not on \$PATH. Aborting."; exit 1; }

urlencode() {
python -c 'import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1], sys.argv[2]))' \
"$1" "$urlencode_safe"
}

args=()
while [[ $# -gt 0 ]]
do
Expand Down Expand Up @@ -66,16 +58,6 @@ do
shift # past argument
shift # past value
;;
--slug)
slug="$2"
shift # past argument
shift # past value
;;
--container)
container="$2"
shift # past argument
shift # past value
;;
--file)
file="$2"
shift # past argument
Expand All @@ -86,11 +68,6 @@ do
shift # past argument
shift # past value
;;
--file-slug)
file_slug="$2"
shift # past argument
shift # past value
;;
*) # unknown arguments
args+=("$1") # save it in an array for later
shift # past argument
Expand All @@ -99,6 +76,8 @@ do
done
set -- "${args[@]}" # restore args

target="$1"

if [ -z "$cert_pem_file" ] ; then
print_usage
exit 1
Expand All @@ -124,23 +103,6 @@ if [ -z "$file_content_type" ] ; then
file_content_type=$(file -b --mime-type "$file")
fi

if [ -z "$slug" ] ; then
slug=$(uuidgen | tr '[:upper:]' '[:lower:]') # lowercase
fi
encoded_slug=$(urlencode "$slug")

# need to create explicit file URI since that is what this script returns (not the graph URI)

#if [ -z "$file_slug" ] ; then
# file_slug=$(uuidgen | tr '[:upper:]' '[:lower:]') # lowercase
#fi

if [ -z "$container" ] ; then
container="${base}files/"
fi

target="${container}${encoded_slug}/"

# https://stackoverflow.com/questions/19116016/what-is-the-right-way-to-post-multipart-form-data-using-curl

rdf_post+="-F \"rdf=\"\n"
Expand All @@ -151,18 +113,8 @@ rdf_post+="-F \"pu=http://purl.org/dc/terms/title\"\n"
rdf_post+="-F \"ol=${title}\"\n"
rdf_post+="-F \"pu=http://www.w3.org/1999/02/22-rdf-syntax-ns#type\"\n"
rdf_post+="-F \"ou=http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#FileDataObject\"\n"
rdf_post+="-F \"su=${target}\"\n"
rdf_post+="-F \"pu=http://purl.org/dc/terms/title\"\n"
rdf_post+="-F \"ol=${title}\"\n"
rdf_post+="-F \"pu=http://www.w3.org/1999/02/22-rdf-syntax-ns#type\"\n"
rdf_post+="-F \"ou=https://www.w3.org/ns/ldt/document-hierarchy#Item\"\n"
rdf_post+="-F \"pu=http://xmlns.com/foaf/0.1/primaryTopic\"\n"
rdf_post+="-F \"ob=file\"\n"
rdf_post+="-F \"pu=http://rdfs.org/sioc/ns#has_container\"\n"
rdf_post+="-F \"ou=${container}\"\n"

if [ -n "$description" ] ; then
rdf_post+="-F \"sb=file\"\n"
rdf_post+="-F \"pu=http://purl.org/dc/terms/description\"\n"
rdf_post+="-F \"ol=${description}\"\n"
fi
Expand All @@ -176,14 +128,5 @@ if [ -n "$proxy" ]; then
target="${target/$target_host/$proxy_host}"
fi

# POST RDF/POST multipart form and capture the effective URL
effective_url=$(echo -e "$rdf_post" | curl -w '%{url_effective}' -f -v -s -k -X PUT -H "Accept: text/turtle" -E "$cert_pem_file":"$cert_password" -o /dev/null --config - "$target")

# If using proxy, rewrite the effective URL back to original hostname
if [ -n "$proxy" ]; then
# Replace proxy host with original host in the effective URL
rewritten_url="${effective_url/$proxy_host/$target_host}"
echo "$rewritten_url"
else
echo "$effective_url"
fi
# POST RDF/POST multipart form
echo -e "$rdf_post" | curl -f -v -s -k -X POST -H "Accept: text/turtle" -E "$cert_pem_file":"$cert_password" -o /dev/null --config - "$target"
50 changes: 19 additions & 31 deletions bin/imports/create-csv-import.sh → bin/imports/add-csv-import.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ print_usage()
{
printf "Transforms CSV data into RDF using a SPARQL query and imports it.\n"
printf "\n"
printf "Usage: %s options\n" "$0"
printf "Usage: %s options TARGET_URI\n" "$0"
printf "\n"
printf "Options:\n"
printf " -f, --cert-pem-file CERT_FILE .pem file with the WebID certificate of the agent\n"
printf " -p, --cert-password CERT_PASSWORD Password of the WebID certificate\n"
printf " -b, --base BASE_URI Base URI of the application\n"
printf " --proxy PROXY_URL The host this request will be proxied through (optional)\n"
printf "\n"
printf " --title TITLE Title of the container\n"
printf " --description DESCRIPTION Description of the container (optional)\n"
printf " --slug STRING String that will be used as URI path segment (optional)\n"
printf " --title TITLE Title of the import\n"
printf " --description DESCRIPTION Description of the import (optional)\n"
printf " --uri URI URI of the import resource (optional)\n"
printf "\n"
printf " --query QUERY_URI URI of the CONSTRUCT mapping query\n"
printf " --file FILE_URI URI of the CSV file\n"
Expand All @@ -24,11 +24,6 @@ print_usage()

hash turtle 2>/dev/null || { echo >&2 "turtle not on \$PATH. Aborting."; exit 1; }

urlencode() {
python -c 'import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1], sys.argv[2]))' \
"$1" "$urlencode_safe"
}

args=()
while [[ $# -gt 0 ]]
do
Expand Down Expand Up @@ -65,8 +60,8 @@ do
shift # past argument
shift # past value
;;
--slug)
slug="$2"
--uri)
uri="$2"
shift # past argument
shift # past value
;;
Expand All @@ -93,6 +88,8 @@ do
done
set -- "${args[@]}" # restore args

target="$1"

if [ -z "$cert_pem_file" ] ; then
print_usage
exit 1
Expand Down Expand Up @@ -122,44 +119,35 @@ if [ -z "$delimiter" ] ; then
exit 1
fi

if [ -z "$slug" ] ; then
slug=$(uuidgen | tr '[:upper:]' '[:lower:]') # lowercase
if [ -n "$uri" ] ; then
subject="<${uri}>"
else
subject="_:import"
fi
encoded_slug=$(urlencode "$slug")

container="${base}imports/"

target="${container}${encoded_slug}/"

args+=("-f")
args+=("$cert_pem_file")
args+=("-p")
args+=("$cert_password")
args+=("-t")
args+=("text/turtle") # content type
args+=("$target")
if [ -n "$proxy" ]; then
args+=("--proxy")
args+=("$proxy")
fi

turtle+="@prefix ldh: <https://w3id.org/atomgraph/linkeddatahub#> .\n"
turtle+="@prefix dh: <https://www.w3.org/ns/ldt/document-hierarchy#> .\n"
turtle+="@prefix dct: <http://purl.org/dc/terms/> .\n"
turtle+="@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n"
turtle+="@prefix spin: <http://spinrdf.org/spin#> .\n"
turtle+="_:import a ldh:CSVImport .\n"
turtle+="_:import dct:title \"${title}\" .\n"
turtle+="_:import spin:query <${query}> .\n"
turtle+="_:import ldh:file <${file}> .\n"
turtle+="_:import ldh:delimiter \"${delimiter}\" .\n"
turtle+="<${target}> a dh:Item .\n"
turtle+="<${target}> foaf:primaryTopic _:import .\n"
turtle+="<${target}> dct:title \"${title}\" .\n"
turtle+="${subject} a ldh:CSVImport .\n"
turtle+="${subject} dct:title \"${title}\" .\n"
turtle+="${subject} spin:query <${query}> .\n"
turtle+="${subject} ldh:file <${file}> .\n"
turtle+="${subject} ldh:delimiter \"${delimiter}\" .\n"

if [ -n "$description" ] ; then
turtle+="_:import dct:description \"${description}\" .\n"
turtle+="${subject} dct:description \"${description}\" .\n"
fi

# submit Turtle doc to the server
echo -e "$turtle" | turtle --base="$target" | put.sh "${args[@]}"
echo -e "$turtle" | turtle --base="$target" | post.sh "${args[@]}"
Loading