]> Pierre Choffet | Git repositories - wdef_tools.git/blob - scripts/get_merged_element.sh
Add script to compare with Wikidata’s RDF
[wdef_tools.git] / scripts / get_merged_element.sh
1 #!/bin/bash
2
3 # get_merged_element.sh - Return an element in wdef:knowledge after it's been
4 # merged with a RDF.
5 # Copyright (C) 2022 Pierre Choffet
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of version 3 of the GNU General Public License as
9 # published by the Free Software Foundation.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 readonly SCRIPT_DIR="$(dirname "$0")"
20
21 source "${SCRIPT_DIR}/rdf.sh"
22
23 set -euo pipefail
24
25 # Any rdf cache older than this (in minutes) will be updated
26 RDFS_MAX_AGE=${RDFS_MAX_AGE:=1440}
27
28 readonly CANONICALIZE_XSLT_PATH="${SCRIPT_DIR}/../xslts/canonicalize.xslt"
29 readonly MERGE_XSLT_PATH="${SCRIPT_DIR}/../xslts/merge_rdf.xslt"
30
31 function usage() {
32 cat << EOF
33 USAGE: get_merged_element.sh <wdef_path> <QID>
34
35 Merge a RDF into an element inside a wdef file. A wdef file containing this
36 single element is returned.
37 EOF
38 }
39
40 if [ "$#" -ne 2 ]
41 then
42 usage >&2
43 exit 1
44 fi
45
46 readonly WDEF_PATH="${1}"
47 readonly ELEMENT_QID="${2}"
48
49 # Check wdef exists
50 if [ ! -s "${WDEF_PATH}" ]
51 then
52 echo "WDEF file doesn't exist. Exiting" >&2
53 exit
54 fi
55
56 # Export element from wdef
57 readonly ELEMENT_PATH="$(mktemp)"
58 xmlstarlet sel -D -t -e 'wdef:knowledge' -m '/wdef:knowledge' -c "wdef:element[@wdef:id = '${ELEMENT_QID}']" "${WDEF_PATH}" | xmlstarlet fo - > "${ELEMENT_PATH}"
59
60 # Check element is in temp file
61 if [ "$(xmlstarlet sel -t -i "/wdef:knowledge/wdef:element[@wdef:id = '${ELEMENT_QID}']" -v "'true'" "${ELEMENT_PATH}")" != 'true' ]
62 then
63 echo "Element not available in wdef. Exiting." >&2
64 exit
65 fi
66
67 # Cache RDF
68 readonly RDF_PATH=$(cacheRDFMaxAge "${ELEMENT_QID}" "${RDFS_MAX_AGE}")
69
70 # Merge and return canonicalized result
71 xmlstarlet tr "${MERGE_XSLT_PATH}" -s action=reduce \
72 -s "rdf-path=${RDF_PATH}" \
73 "${WDEF_PATH}" | xmlstarlet tr ${CANONICALIZE_XSLT_PATH} -
74
75 rm "${ELEMENT_PATH}"