3 # query.sh - Set of Bash functions to work with wdef files.
4 # Copyright (C) 2022 Pierre Choffet
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of version 3 of the GNU General Public License as
8 # published by the Free Software Foundation.
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.
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/>.
20 readonly WIKIDATA_SPARQL_ENDPOINT
='https://query.wikidata.org/bigdata/namespace/wdq/sparql'
22 readonly CACHE_DIR
=${CACHE_DIR:-"${HOME}/.cache/wdef_tools/"}
23 readonly QUERY_CACHE_DIR
="${CACHE_DIR}/queries/"
25 # Send query and return a path to the result into cache dir
27 # $1: Path to file containing query
29 # Path to the file containing result, in cache dir
31 local -r query_path
="${1}"
33 if [ ! -s "${query_path}" ]
35 echo "Query not found at path ${query_path}. Exiting." >&2
39 local -r query_checksum
=$(md5sum "${query_path}" | cut -d ' ' -f 1)
40 local -r result_path
="${QUERY_CACHE_DIR}/${query_checksum}"
43 local -r query
=$(cat "${query_path}" | jq -sRr @uri)
45 # Send query, cache result
46 mkdir -p "${QUERY_CACHE_DIR}"
47 wget
-O "${result_path}" "${WIKIDATA_SPARQL_ENDPOINT}?query=${query}"
52 # Build query from template and a set of variables
54 # $1: Path to file containing query template
55 # $2: Associative array with variables values, indexed on their names
57 # Path to the file containing result, in cache dir
58 function queryVariables
() {
59 local -r template_path
="${1}"
60 local -nr variables
=${2}
63 if [ ! -s "${template_path}" ]
65 echo "Query template not found at path ${template_path}. Exiting." >&2
68 if [ ${#variables[@]} -eq 0 ]
70 echo "Missing query variables. Exiting." >&2
74 local -r template_checksum
=$(md5sum "${template_path}" | cut -d ' ' -f 1)
75 local -r variables_checksum
=$(echo "$(typeset -p ${2})" | md5sum - | cut -d ' ' -f 1)
78 local query=$(cat "${template_path}")
79 for var_name in ${!variables[@]}
81 query=${query//\%${var_name}\%/${variables[${var_name}]}}
84 # Save query into a temporary file
85 local -r query_path="$(mktemp)"
86 echo "${query}" > "${query_path}"
88 # Check at least one variable has been substituted (file name collision otherwise)
89 if [ "$(md5sum "${query_path}" | cut -d ' ' -f 1)" == "${template_checksum}" ]
91 echo "No variable substituted. Exiting.
" >&2
96 local -r query_result_path=$(query "${query_path}")
98 # Generate query result cache path, create dir if needed
99 local -r query_cache_path="${QUERY_CACHE_DIR}/${template_checksum}/${variables_checksum}"
100 mkdir -p "$(dirname "${query_cache_path}")"
103 if [ ! -f "${query_cache_path}" ]
105 ln "${query_result_path}" "${query_cache_path}"
112 echo "${query_cache_path}"