]> Pierre Choffet | Git repositories - wdef_tools.git/blob - scripts/get_qid_from_property.sh
Add script to compare with Wikidata’s RDF
[wdef_tools.git] / scripts / get_qid_from_property.sh
1 #!/bin/bash
2
3 # get_equivalent.sh - Search equivalent element on Wikidata.
4 # Copyright (C) 2022 Pierre Choffet
5 #
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.
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 source "$(dirname "$0")/query.sh"
19
20 set -euo pipefail
21
22 function buildQuery() {
23 local -r pid="${1}"
24 shift
25 local -r values=( "${@}" )
26
27 echo 'SELECT DISTINCT ?element WHERE {'
28
29 for index in ${!values[@]}
30 do
31 if [ "${index}" -ne 0 ]
32 then
33 echo 'UNION'
34 fi
35 echo "{ ?element p:${pid} ?id${index}. ?id${index} (ps:${pid}) ${values[$index]@Q}. }"
36 done
37
38 echo '} LIMIT 2'
39 }
40
41 function usage() {
42 cat << EOF
43 USAGE: get_qid_from_property.sh <wdef_path> <element_id> <element_pid>
44
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.
48 EOF
49 }
50
51 if [ "$#" -ne 3 ]
52 then
53 usage >&2
54 exit 1
55 fi
56
57 readonly WDEF_PATH="${1}"
58 readonly ELEMENT_ID="${2}"
59 readonly ELEMENT_PID="${3}"
60
61 # Get wdef
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}")
63
64 # Build query
65 readonly QUERY_PATH=$(mktemp)
66 buildQuery "${ELEMENT_PID}" ${VALUES[@]} > "${QUERY_PATH}"
67 readonly RESULT_PATH=$(query "${QUERY_PATH}")
68
69 # Cleanup
70 rm "${QUERY_PATH}"
71
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}")
74
75 if [ "${QID}" != '' ]
76 then
77 echo "${QID##*/}"
78 else
79 exit 1
80 fi