3 # koha-functions.sh -- shared library of helper functions for koha-* scripts
4 # Copyright 2014 - Tomas Cohen Arazi
5 # Universidad Nacional de Cordoba
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
32 get_apache_config_for()
35 local sitefile="/etc/apache2/sites-available/$site"
37 if is_instance $site; then
38 if [ -f "$sitefile.conf" ]; then
40 elif [ -f "$sitefile" ]; then
50 if [ -e /etc/koha/koha-sites.conf ]; then
51 . /etc/koha/koha-sites.conf
53 echo "Error: /etc/koha/koha-sites.conf not present." 1>&2
56 local opacdomain="$OPACPREFIX$site$OPACSUFFIX$DOMAIN"
64 if [ -e /etc/koha/koha-sites.conf ]; then
65 . /etc/koha/koha-sites.conf
67 echo "Error: /etc/koha/koha-sites.conf not present." 1>&2
70 local intradomain="$INTRAPREFIX$site$INTRASUFFIX$DOMAIN"
74 letsencrypt_get_opacdomain_for()
78 if [ -e /var/lib/koha/$site/letsencrypt.enabled ]; then
79 . /var/lib/koha/$site/letsencrypt.enabled
81 local opacdomain=$(get_opacdomain_for $site)
89 local instancefile=$(get_apache_config_for $site)
91 if [ "$instancefile" = "" ]; then
95 if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-disable.conf' \
96 "$instancefile" ; then
105 local instancename=$1
107 if find -L /etc/koha/sites -mindepth 1 -maxdepth 1 \
108 -type d -printf '%f\n'\
109 | grep -q -x "$instancename" ; then
118 local instancename=$1
120 if [ -e /var/lib/koha/$instancename/email.enabled ]; then
127 is_letsencrypt_enabled()
129 local instancename=$1
131 if [ -e /var/lib/koha/$instancename/letsencrypt.enabled ]; then
140 local instancename=$1
142 if [ -e /var/lib/koha/$instancename/sip.enabled ]; then
151 local instancename=$1
153 if [ -e /var/lib/koha/$instancename/sitemap.enabled ]; then
162 local instancename=$1
164 if daemon --name="$instancename-koha-sip" \
165 --pidfiles="/var/run/koha/$instancename/" \
166 --user="$instancename-koha.$instancename-koha" \
176 local instancename=$1
178 if daemon --name="$instancename-koha-zebra" \
179 --pidfiles="/var/run/koha/$instancename/" \
180 --user="$instancename-koha.$instancename-koha" \
190 local instancename=$1
192 if daemon --name="$instancename-koha-indexer" \
193 --pidfiles="/var/run/koha/$instancename/" \
194 --user="$instancename-koha.$instancename-koha" \
202 is_es_indexer_running()
204 local instancename=$1
206 if daemon --name="$instancename-koha-es-indexer" \
207 --pidfiles="/var/run/koha/$instancename/" \
208 --user="$instancename-koha.$instancename-koha" \
218 local instancename=$1
221 local name=`get_worker_name $instancename $queue`
223 if daemon --name="$name" \
224 --pidfiles="/var/run/koha/$instancename/" \
225 --user="$instancename-koha.$instancename-koha" \
238 if [ "$queue" = "" ] || [ "$queue" = "default" ]; then
239 echo "${name}-koha-worker"
241 echo "${name}-koha-worker-${queue}"
245 is_plack_enabled_opac()
247 local instancefile=$1
249 if [ "$instancefile" = "" ]; then
253 # remember 0 means success/true in bash.
254 if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-opac-plack.conf' \
255 "$instancefile" ; then
262 is_plack_enabled_intranet()
264 local instancefile=$1
266 if [ "$instancefile" = "" ]; then
270 # remember 0 means success/true in bash.
271 if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-intranet-plack.conf' \
272 "$instancefile" ; then
282 local instancefile=$(get_apache_config_for $site)
284 if [ "$instancefile" = "" ]; then
288 if is_plack_enabled_opac $instancefile ; then
293 if is_plack_enabled_intranet $instancefile ; then
299 # remember 0 means success/true in bash.
300 if [ "$enabledopac" != "$enabledintra" ] ; then
301 echo "$site has a plack configuration error. Enable or disable plack to correct this."
303 elif [ "$enabledopac" = "1" ] ; then
312 local instancename=$1
314 if start-stop-daemon --pidfile "/var/run/koha/${instancename}/plack.pid" \
315 --user="$instancename-koha" \
325 local instancename=$1
327 if [ -e /etc/koha/sites/$instancename/z3950/config.xml ]; then
336 local instancename=$1
338 if start-stop-daemon --pidfile "/var/run/koha/${instancename}/z3950-responder.pid" \
339 --user="$instancename-koha" \
347 is_elasticsearch_enabled()
349 local instancename=$1
351 # is querying the DB fails then $searching won't match 'Elasticsearch'. Ditching STDERR
352 search_engine=$(koha-shell $instancename -c "/usr/share/koha/bin/admin/koha-preferences get SearchEngine 2> /dev/null")
354 if [ "$search_engine" = "Elasticsearch" ]; then
361 adjust_paths_dev_install()
363 # Adjust KOHA_HOME, PERL5LIB for dev installs, as indicated by
364 # corresponding tag in koha-conf.xml
366 local instancename=$1
369 if [ "$instancename" != "" ] && is_instance $instancename; then
370 dev_install=$(run_safe_xmlstarlet $instancename dev_install)
373 if [ "$dev_install" != "" ] && [ "$dev_install" != "0" ]; then
375 KOHA_HOME=$(run_safe_xmlstarlet $instancename intranetdir)
376 PERL5LIB="$KOHA_HOME:$KOHA_HOME/lib"
384 find -L /etc/koha/sites -mindepth 1 -maxdepth 1\
385 -type d -printf '%f\n' | sort
390 local instancename=$1
391 local retval=$(run_safe_xmlstarlet $instancename zebra_loglevels)
392 if [ "$retval" != "" ]; then
395 echo "none,fatal,warn"
399 get_max_record_size()
401 local instancename=$1
402 local retval=$(xmlstarlet sel -t -v 'yazgfs/config/zebra_max_record_size' /etc/koha/sites/$instancename/koha-conf.xml)
403 if [ "$retval" != "" ]; then
412 local instancename=$1
413 local retval=$(run_safe_xmlstarlet $instancename tmp_path)
414 if [ "$retval" != "" ]; then
422 local instancename=$1
423 local retval=$(run_safe_xmlstarlet $instancename upload_path)
424 if [ "$retval" != "" ]; then
432 if [ "$TMPDIR" != "" ]; then
433 if [ -d "$TMPDIR" ]; then
437 # We will not unset TMPDIR but just default to /tmp here
438 # Note that mktemp (used later) would look at TMPDIR
442 local retval=$(mktemp -u)
443 if [ "$retval" = "" ]; then
447 echo $(dirname $retval)
450 run_safe_xmlstarlet()
452 # When a bash script sets -e (errexit), calling xmlstarlet on an
453 # unexisting key would halt the script. This is resolved by calling
454 # this function in a subshell. It will always returns true, while not
455 # affecting the exec env of the caller. (Otherwise, errexit is cleared.)
456 local instancename=$1
458 set +e; # stay on the safe side
459 echo $(xmlstarlet sel -t -v "yazgfs/config/$myexpr" /etc/koha/sites/$instancename/koha-conf.xml)