Koha/debian/scripts/koha-create
Tomas Cohen Arazi aba3db2628 Bug 8507: koha-create now supports using DOM indexing for bibs
This patch makes the koha-create script install the file zebra-biblios-dom.cfg
with the proper string substitutions inside on the new instance koha-conf.xml file.

It also adds two option switches that control the indexing mode for the instance:

 --biblio-idx {dom|grs1}
 --auth-idx {dom|grs1}

DOM indexing is set as the default for both authorities and bibliographic records.

Following drojf (thanks!) advice I arranged stuff like explained here:

  http://wiki.koha-community.org/wiki/Switching_to_dom_indexing

To test:
- Apply the patch
- Build your own packages and install them on a test server
a) Create a new instance without using the new switches like:
 $ koha-create --create-db domtest
 - Check there's a file /etc/koha/sites/domtest/zebra-biblios-dom.cfg
 - Check that /etc/koha/sites/domtest/koha-conf.xml points to:
   * zebra-biblios-dom.cfg (biblioserver section)
   * zebra-biblios-dom.cfg (publicserver section)
   * zebra-authorities-dom.cfg (authorityserver section)
 - Success means the new default is DOM
b) Play with the 4 possible combination of option switches
 $ koha-create --create-db --auth-idx grs1 --biblio-idx grs1 domtest
 $ koha-create --create-db --auth-idx grs1 --biblio-idx dom domtest
 $ koha-create --create-db --auth-idx dom --biblio-idx grs1 domtest
 $ koha-create --create-db --auth-idx dom --biblio-idx dom domtest
 - Check the koha-conf.xml file reflects the chosen options.
c) Run
 $ koha-create --help
 - It should advertise this addition accordingly.
d) Run
 $ man koha-create
 - Man page for koha-create should provide good information on the new switches behaviour

Sponsored-by: Universidad Nacional de Cordoba
Signed-off-by: Mirko Tietgen <mirko@abunchofthings.net>
Signed-off-by: Chris Cormack <chris@bigballofwax.co.nz>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
2013-08-13 14:25:53 +00:00

465 lines
14 KiB
Bash
Executable file

#!/bin/bash
#
# koha-create -- Create a new Koha instance.
# Copyright 2010 Catalyst IT, Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 <http://www.gnu.org/licenses/>.
set -e
usage="Usage: $0 [--create-db|--request-db|--populate-db|--use-db] \
[--marcflavor marc21|normarc|unimarc] \
[--zebralang en|es|fr|nb|ru|uk] \
[--auth-idx dom|grs1] [--biblio-idx dom|grs1] \
[--defaultsql /path/to/some.sql] \
[--configfile /path/to/config] [--passwdfile /path/to/passwd] \
[--database database] [--adminuser n] instancename"
die() {
echo "$@" 1>&2
exit 1
}
# UPPER CASE VARIABLES - from configfile or default value
# lower case variables - generated within this script
generate_config_file() {
touch "$2"
chown "root:$username" "$2"
chmod 0640 "$2"
sed -e "s/__KOHASITE__/$name/g" \
-e "s/__OPACPORT__/$OPACPORT/g" \
-e "s/__INTRAPORT__/$INTRAPORT/g" \
-e "s/__OPACSERVER__/$opacdomain/g" \
-e "s/__INTRASERVER__/$intradomain/g" \
-e "s/__ZEBRA_PASS__/$zebrapwd/g" \
-e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
-e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
-e "s/__BIBLIOS_INDEXING_MODE__/$BIBLIOS_INDEXING_MODE/g" \
-e "s/__AUTHORITIES_INDEXING_MODE__/$AUTHORITIES_INDEXING_MODE/g" \
-e "s/__ZEBRA_BIBLIOS_CFG__/$ZEBRA_BIBLIOS_CFG/g" \
-e "s/__ZEBRA_AUTHORITIES_CFG__/$ZEBRA_AUTHORITIES_CFG/g" \
-e "s/__START_BIBLIOS_RETRIEVAL_INFO__/`echo $START_BIBLIOS_RETRIEVAL_INFO`/g" \
-e "s/__END_BIBLIOS_RETRIEVAL_INFO__/`echo $END_BIBLIOS_RETRIEVAL_INFO`/g" \
-e "s/__START_AUTHORITIES_RETRIEVAL_INFO__/`echo $START_AUTHORITIES_RETRIEVAL_INFO`/g" \
-e "s/__END_AUTHORITIES_RETRIEVAL_INFO__/`echo $END_AUTHORITIES_RETRIEVAL_INFO`/g" \
-e "s/__DB_NAME__/$mysqldb/g" \
-e "s/__DB_HOST__/$mysqlhost/g" \
-e "s/__DB_USER__/$mysqluser/g" \
-e "s/__DB_PASS__/$mysqlpwd/g" \
-e "s/__UNIXUSER__/$username/g" \
-e "s/__UNIXGROUP__/$username/g" \
-e "s/__PLUGINS_DIR__/\/var\/lib\/koha\/$name\/plugins/g" \
"/etc/koha/$1" > "$2"
}
getmysqlhost() {
awk '
/^\[/ { inclient = 0 }
/^\[client\]/ { inclient = 1 }
inclient && /^ *host *=/ { print $3 }' \
/etc/mysql/koha-common.cnf
}
getinstancemysqlpassword() {
xmlstarlet sel -t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml"
}
getinstancemysqluser() {
xmlstarlet sel -t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml"
}
getinstancemysqldatabase() {
xmlstarlet sel -t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
}
set_biblios_indexing_mode()
{
local indexing_mode=$1
local marc_format=$2
case $indexing_mode in
"dom")
START_BIBLIOS_RETRIEVAL_INFO=`cat <<EOF
<xi:include href="\/etc\/koha\/$marc_format-retrieval-info-bib-dom.xml"\n
xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
<xi:fallback>\n
<retrievalinfo>
EOF`
END_BIBLIOS_RETRIEVAL_INFO=`cat <<EOF
<\/retrievalinfo>\n
<\/xi:fallback>\n
<\/xi:include>
EOF`
BIBLIOS_INDEXING_MODE="dom"
ZEBRA_BIBLIOS_CFG="zebra-biblios-dom.cfg"
;;
"grs1")
START_BIBLIOS_RETRIEVAL_INFO=" <retrievalinfo>"
END_BIBLIOS_RETRIEVAL_INFO=" <\/retrievalinfo>"
BIBLIOS_INDEXING_MODE="grs1"
ZEBRA_BIBLIOS_CFG="zebra-biblios.cfg"
;;
*)
die "Error: '$indexing_mode' is not a valid indexing mode for bibliographic records."
;;
esac
}
set_authorities_indexing_mode()
{
local indexing_mode=$1
local marc_format=$2
case $indexing_mode in
"dom")
START_AUTHORITIES_RETRIEVAL_INFO=`cat <<EOF
<xi:include href="\/etc\/koha\/$marc_format-retrieval-info-auth-dom.xml"\n
xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
<xi:fallback>\n
<retrievalinfo>
EOF`
END_AUTHORITIES_RETRIEVAL_INFO=`cat <<EOF
<\/retrievalinfo>\n
<\/xi:fallback>\n
<\/xi:include>\n
EOF`
AUTHORITIES_INDEXING_MODE="dom"
ZEBRA_AUTHORITIES_CFG="zebra-authorities-dom.cfg"
;;
"grs1")
START_AUTHORITIES_RETRIEVAL_INFO=" <retrievalinfo>"
END_AUTHORITIES_RETRIEVAL_INFO=" <\/retrievalinfo>"
AUTHORITIES_INDEXING_MODE="grs1"
ZEBRA_AUTHORITIES_CFG="zebra-authorities.cfg"
;;
*)
die "Error: '$indexing_mode' is not a valid indexing mode for authority records."
;;
esac
}
# Set defaults and read config file, if it exists.
DOMAIN=""
OPACPORT="80"
OPACPREFIX=""
OPACSUFFIX=""
INTRAPORT="8080"
INTRAPREFIX=""
INTRASUFFIX=""
DEFAULTSQL=""
ZEBRA_MARC_FORMAT="marc21"
ZEBRA_LANGUAGE="en"
ADMINUSER="1"
PASSWDFILE="/etc/koha/passwd"
# Indexing mode variables (default is DOM)
BIBLIOS_INDEXING_MODE="dom"
AUTHORITIES_INDEXING_MODE="dom"
START_BIBLIOS_RETRIEVAL_INFO=""
END_BIBLIOS_RETRIEVAL_INFO=""
START_AUTHORITIES_RETRIEVAL_INFO=""
END_AUTHORITIES_RETRIEVAL_INFO=""
if [ -e /etc/koha/koha-sites.conf ]
then
. /etc/koha/koha-sites.conf
fi
[ $# -ge 2 ] && [ $# -le 16 ] || die $usage
TEMP=`getopt -o crpm:l:d:f:b:a: -l create-db,request-db,populate-db,use-db,marcflavor:,auth-idx:,biblio-idx:,zebralang:,defaultsql:,configfile:,passwdfile:,database:,adminuser: \
-n "$0" -- "$@"`
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
# Temporary variables for the command line options
CLO_ZEBRA_MARC_FORMAT=""
CLO_ZEBRA_LANGUAGE=""
CLO_DEFAULTSQL=""
CLO_ADMINUSER=""
CLO_BIBLIOS_INDEXING_MODE=""
CLO_AUTHORITIES_INDEXING_MODE=""
while true ; do
case "$1" in
-c|--create-db) op=create ; shift ;;
-r|--request-db) op=request ; shift ;;
-p|--populate-db) op=populate ; shift ;;
-u|--use-db) op=use ; shift ;;
-m|--marcflavor) CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
-l|--zebralang) CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
--auth-idx) CLO_AUTHORITIES_INDEXING_MODE="$2" ; shift 2 ;;
--biblio-idx) CLO_BIBLIOS_INDEXING_MODE="$2" ; shift 2 ;;
-d|--defaultsql) CLO_DEFAULTSQL="$2" ; shift 2 ;;
-f|--configfile) configfile="$2" ; shift 2 ;;
-s|--passwdfile) CLO_PASSWDFILE="$2" ; shift 2 ;;
-b|--database) CLO_DATABASE="$2" ; shift 2 ;;
-a|--adminuser) CLO_ADMINUSER="$2" ; shift 2 ;;
--) shift ; break ;;
*) die "Internal error processing command line arguments" ;;
esac
done
# Load the configfile given on the command line
if [ "$configfile" != "" ]
then
if [ -e "$configfile" ]
then
. "$configfile"
else
die "$configfile does not exist.";
fi
fi
# Make sure options from the command line get the highest precedence
if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
then
ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT"
fi
if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
then
ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE"
fi
if [ "$CLO_DEFAULTSQL" != "" ]
then
DEFAULTSQL="$CLO_DEFAULTSQL"
fi
if [ "$CLO_ADMINUSER" != "" ]
then
ADMINUSER="$CLO_ADMINUSER"
fi
if [ "$CLO_PASSWDFILE" != "" ]
then
PASSWDFILE="$CLO_PASSWDFILE"
fi
if [ "$CLO_BIBLIOS_INDEXING_MODE" != "" ]; then
BIBLIOS_INDEXING_MODE=$CLO_BIBLIOS_INDEXING_MODE
fi
set_biblios_indexing_mode $BIBLIOS_INDEXING_MODE $ZEBRA_MARC_FORMAT
if [ "$CLO_AUTHORITIES_INDEXING_MODE" != "" ]; then
AUTHORITIES_INDEXING_MODE=$CLO_AUTHORITIES_INDEXING_MODE
fi
set_authorities_indexing_mode $AUTHORITIES_INDEXING_MODE $ZEBRA_MARC_FORMAT
name="$1"
opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
then
passwdline=`cat $PASSWDFILE | grep "^$name:"`
mysqluser=`echo $passwdline | cut -d ":" -f 2`
mysqlpwd=`echo $passwdline | cut -d ":" -f 3`
mysqldb=`echo $passwdline | cut -d ":" -f 4`
fi
# The order of precedence for MySQL database name is:
# default < passwd file < command line
if [ "$mysqldb" = "" ]
then
mysqldb="koha_$name"
fi
if [ "$CLO_DATABASE" != "" ]
then
mysqldb="$CLO_DATABASE"
fi
if [ "$mysqluser" = "" ]
then
mysqluser="koha_$name"
fi
mysqlhost="$(getmysqlhost)"
if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
then
if [ "$mysqlpwd" = "" ]
then
mysqlpwd="$(pwgen -s 16 1)"
fi
else
mysqlpwd="$(getinstancemysqlpassword $name)"
fi
if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
then
# Create new user and group.
username="$name-koha"
if getent passwd "$username" > /dev/null
then
die "User $username already exists."
fi
if getent group "$username" > /dev/null
then
die "Group $username already exists."
fi
adduser --no-create-home --disabled-login \
--gecos "Koha instance $username" \
--home "/var/lib/koha/$name" \
--quiet "$username"
# Create the site-specific directories.
koha-create-dirs "$name"
# Generate Zebra database password.
zebrapwd="$(pwgen -s 16 1)"
# Future enhancement: make this configurable for when your db is on
# another server.
mysql_hostname="localhost"
# Set up MySQL database for this instance.
if [ "$op" = create ]
then
mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
CREATE DATABASE \`$mysqldb\`;
CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
FLUSH PRIVILEGES;
eof
fi #`
if [ "$op" = use ]
then
mysql --defaults-extra-file=/etc/mysql/koha-common.cnf --force <<eof
CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
FLUSH PRIVILEGES;
eof
fi #`
# Generate and install Apache site-available file and log dir.
generate_config_file apache-site.conf.in \
"/etc/apache2/sites-available/$name"
mkdir "/var/log/koha/$name"
chown "$username:$username" "/var/log/koha/$name"
# Generate and install main Koha config file.
generate_config_file koha-conf-site.xml.in \
"/etc/koha/sites/$name/koha-conf.xml"
# Generate and install Zebra config files.
generate_config_file zebra-biblios-site.cfg.in \
"/etc/koha/sites/$name/zebra-biblios.cfg"
generate_config_file zebra-biblios-dom-site.cfg.in \
"/etc/koha/sites/$name/zebra-biblios-dom.cfg"
generate_config_file zebra-authorities-site.cfg.in \
"/etc/koha/sites/$name/zebra-authorities.cfg"
generate_config_file zebra-authorities-dom-site.cfg.in \
"/etc/koha/sites/$name/zebra-authorities-dom.cfg"
generate_config_file zebra.passwd.in \
"/etc/koha/sites/$name/zebra.passwd"
# Create a GPG-encrypted file for requesting a DB to be set up.
if [ "$op" = request ]
then
touch "$name-db-request.txt"
chmod 0600 "$name-db-request.txt"
cat > "$name-db-request.txt" << eof
Please create a MySQL database and user on $mysqlhost as follows:
database name: $mysqldb
database user: $mysqluser
password: $mysqlpwd
Thank you.
eof
echo "See $name-db-request.txt for database creation request."
echo "Please forward it to the right person, and then run"
echo "$0 --populate-db $name"
echo "Thanks."
fi
fi
if [ "$op" = create ] || [ "$op" = populate ]
then
# Re-fetch the passwords from the config we've generated, allows it
# to be different from what we set, in case the user had to change
# something.
mysqluser=$(getinstancemysqluser $name)
mysqldb=$(getinstancemysqldatabase $name)
# Use the default database content if that exists.
if [ -e "$DEFAULTSQL" ]
then
# Populate the database with default content.
zcat "$DEFAULTSQL" |
sed "s/__KOHASITE__/$name/g" |
mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
# Change the default user's password.
staffpass="$(pwgen 12 1)"
staffdigest=$(echo -n "$staffpass" |
perl -e '
use Digest::MD5 qw(md5_base64);
while (<>) { print md5_base64($_), "\n"; }')
mysql --host="$mysqlhost" --user="$mysqluser" \
--password="$mysqlpwd" <<eof
USE \`$mysqldb\`;
UPDATE borrowers
SET password = '$staffdigest'
WHERE borrowernumber = $ADMINUSER;
eof
#`
echo "staff user password is '$staffpass' but keep that secret"
# Upgrade the database schema, just in case the dump was from an
# old version.
koha-upgrade-schema "$name"
else
echo "Koha instance is empty, no staff user created."
fi
fi
if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
then
# Reconfigure Apache.
a2ensite "$name"
service apache2 restart
# Start Zebra.
koha-start-zebra "$name"
fi
if [ "$op" = request ]
then
koha-disable "$name"
fi
echo <<eoh
Email for this instance is disabled. When you're ready to enable it, use:
koha-email-enable $name
eoh