This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-vocabularies.sh
executable file
·155 lines (134 loc) · 4.46 KB
/
import-vocabularies.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/bash
# TODOs:
# - Support URLs and local files for reimport
# - Improve vocabulary filters (currently doesn't support scheme data only)
# - Add better error handling
# - Support other data like concordances?
function usage {
echo "
Usage: $0 [-d vocabularies.txt] [-f] [-r] [-g <vocabulary filter>]
Offers easy importing of vocabularies into jskos-server instances via a list in vocabularies.txt.
NOTE: This is still experimental. Use with caution.
Options:
-d Specify the data which is used for import; defaults to vocabularies.txt
-f Force import even if vocabulary data already exists
-r Reset a vocabulary's concept data before importing
-g A grep filter used on vocabularies.txt
Note that -r currently only works if the scheme is given as a BARTOC URI.
Examples:
$0 # Imports all vocabularies in vocabularies.txt (ignores concepts if already exist)
$0 -g node/18797 # Imports IxTheo (ignores concepts if already exist)
$0 -g node/18797 -f # Imports IxTheo, including concepts
$0 -g node/18797 -r # Imports IxTheo, resets its concepts, then reimports concepts
"
}
DATAFILE="vocabularies.txt"
FORCE="false"
RESET="false"
FILTER=""
while getopts 'd:frg:h' flag; do
case "${flag}" in
d) DATAFILE="${OPTARG}" ;;
f) FORCE='true' ;;
r) RESET='true' ;;
g) FILTER="${OPTARG}" ;;
h) usage
exit 0 ;;
*) usage
exit 1 ;;
esac
done
# Ask for confirmation to reimport all vocabularies
if [[ "$RESET" == "true" ]] && [[ -z "$FILTER" ]]; then
read -p "Are you sure you want to reset all vocabularies? " -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 0
fi
fi
function handler {
# TODO: How can I remove these functions from handler? If they are defined outside of it, they won't be available due to how we call it (see last line).
ok() {
echo "📗 $@"
}
warn() {
echo "📙 $@"
}
error() {
echo "📕 $@"
}
# Logs based on condition
# Parameter 1: Condition (usually $?)
# Parameter 2: Success string
# Parameter 3: Failure string
didSucceed () {
echo
if [[ $1 -eq 0 ]]; then
ok $2
else
warn $3
fi
}
# baseUrl will be in BASE_URL
function getBaseUrl {
if ! command -v jq &> /dev/null
then
BASE_URL=""
echo
warn "!!! WARNING: jq could not be found; will not import vocabulary. Run script with -f to import anyway. !!!"
exit
fi
JSKOS_SERVER=$0
BASE_URL=$(jq '.baseUrl' $JSKOS_SERVER/config/config.json)
if [[ "$BASE_URL" == "null" ]]; then
PORT=$(jq '.port' $JSKOS_SERVER/config/config.json)
if [[ "$PORT" == "null" ]]; then
PORT=3000
fi
BASE_URL="http://localhost:$PORT"
fi
# Remove leading/trailing " if necessary
BASE_URL="${BASE_URL%\"}"
BASE_URL="${BASE_URL#\"}"
# Remove trailing slash
BASE_URL=$(echo ${BASE_URL%/})
}
JSKOS_SERVER=$0
SCHEME=$1
echo
echo "==================== importing $SCHEME (force=$FORCE, reset=$RESET) ===================="
echo
# TODO: This works only if scheme is given as BARTOC URI, but it should work for all cases.
if [[ "$RESET" == "true" ]]; then
yes | $JSKOS_SERVER/bin/reset.js -s $SCHEME
didSucceed $? "Concept data was removed from jskos-server." "Concept data could not be removed from jskos-server."
echo
fi
# Load scheme data from BARTOC if a BARTOC URI is given
if [[ "$SCHEME" =~ ^http://bartoc.org ]]; then
SCHEME_URI=$SCHEME
SCHEME=https://bartoc.org/api/data?uri=$SCHEME
fi
$JSKOS_SERVER/bin/import.js scheme $SCHEME
didSucceed $? "Vocabulary metadata was imported into jskos-server." "Import of vocabulary metadata into jskos-server failed."
echo
# TODO: Check if concept data exists already; if yes, stop here.
if [[ "$FORCE" == "false" ]] && [[ "$RESET" == "false" ]] && [[ ! -z $SCHEME_URI ]]
then
getBaseUrl $JSKOS_SERVER
CONCEPTS_LENGTH=$(curl -s "$BASE_URL/voc?uri=$SCHEME_URI" | jq '.[0] | .concepts | length')
if [[ "$CONCEPTS_LENGTH" == "1" ]]; then
warn "Concept data for $SCHEME_URI already exists. Run script with -f to import anyway."
exit
fi
fi
# Supports multiple concept files
for CONCEPTS in "${@:2}"
do
$JSKOS_SERVER/bin/import.js concepts $CONCEPTS
didSucceed $? "Concept data was imported into jskos-server." "Import of concept data into jskos-server failed."
echo
done
}
export -f handler
grep -o '^[^#]*' $DATAFILE | grep "$FILTER" | FORCE=$FORCE RESET=$RESET xargs -L 1 bash -c 'handler "$@"'