#!/bin/bash # get_merged_element.sh - Return an element in wdef:knowledge after it's been # merged with a RDF. # Copyright (C) 2022 Pierre Choffet # # This program is free software: you can redistribute it and/or modify # it under the terms of version 3 of the GNU General Public License as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . readonly SCRIPT_DIR="$(dirname "$0")" source "${SCRIPT_DIR}/rdf.sh" set -euo pipefail # Any rdf cache older than this (in minutes) will be updated RDFS_MAX_AGE=${RDFS_MAX_AGE:=1440} readonly CANONICALIZE_XSLT_PATH="${SCRIPT_DIR}/../xslts/canonicalize.xslt" readonly MERGE_XSLT_PATH="${SCRIPT_DIR}/../xslts/merge_rdf.xslt" function usage() { cat << EOF USAGE: get_merged_element.sh Merge a RDF into an element inside a wdef file. A wdef file containing this single element is returned. EOF } if [ "$#" -ne 2 ] then usage >&2 exit 1 fi readonly WDEF_PATH="${1}" readonly ELEMENT_QID="${2}" # Check wdef exists if [ ! -s "${WDEF_PATH}" ] then echo "WDEF file doesn't exist. Exiting" >&2 exit fi # Export element from wdef readonly ELEMENT_PATH="$(mktemp)" xmlstarlet sel -D -t -e 'wdef:knowledge' -m '/wdef:knowledge' -c "wdef:element[@wdef:id = '${ELEMENT_QID}']" "${WDEF_PATH}" | xmlstarlet fo - > "${ELEMENT_PATH}" # Check element is in temp file if [ "$(xmlstarlet sel -t -i "/wdef:knowledge/wdef:element[@wdef:id = '${ELEMENT_QID}']" -v "'true'" "${ELEMENT_PATH}")" != 'true' ] then echo "Element not available in wdef. Exiting." >&2 exit fi # Cache RDF readonly RDF_PATH=$(cacheRDFMaxAge "${ELEMENT_QID}" "${RDFS_MAX_AGE}") # Merge and return canonicalized result xmlstarlet tr "${MERGE_XSLT_PATH}" -s action=reduce \ -s "rdf-path=${RDF_PATH}" \ "${WDEF_PATH}" | xmlstarlet tr ${CANONICALIZE_XSLT_PATH} - rm "${ELEMENT_PATH}"