]>
Pierre Choffet | Git repositories - wdef_tools.git/blob - scripts/get_qid_from_property.sh
3 # get_equivalent.sh - Search equivalent element on Wikidata.
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/>.
18 source "$(dirname "$0")/query.sh"
22 function buildQuery
() {
25 local -r values
=( "${@}" )
27 echo 'SELECT DISTINCT ?element WHERE {'
29 for index
in ${!values[@]}
31 if [ "${index}" -ne 0 ]
35 echo "{ ?element p:${pid} ?id${index}. ?id${index} (ps:${pid}) ${values[$index]@Q}. }"
43 USAGE: get_qid_from_property.sh <wdef_path> <element_id> <element_pid>
45 This script will get Wikidata's equivalent element to an entry in a wdef. If
46 uniqueness can be ensured from a literal value (typically an external identifier),
47 it's an easy way to merge your wdef with Wikidata.
57 readonly WDEF_PATH
="${1}"
58 readonly ELEMENT_ID
="${2}"
59 readonly ELEMENT_PID
="${3}"
62 readarray
-t VALUES
<<<$(xmlstarlet sel -t -m "/wdef:knowledge/wdef:element[@wdef:id = \"${ELEMENT_ID}\"]/wdef:property[@wdef:pid = \"${ELEMENT_PID}\"]/wdef:value/wdef:literal" -v '.' -n "${WDEF_PATH}")
65 readonly QUERY_PATH
=$(mktemp)
66 buildQuery
"${ELEMENT_PID}" ${VALUES[@]} > "${QUERY_PATH}"
67 readonly RESULT_PATH
=$(query "${QUERY_PATH}")
72 # Print potential result in stdout, cleanup, exit
73 readonly QID
=$(xmlstarlet sel -t -m '/_:sparql/_:results[count(_:result) = 1]' -v '_
:result
/_
:binding
[@name
= "element"]/_
:uri
' "${RESULT_PATH}")