3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use XML::LibXML qw( XML_ELEMENT_NODE XML_TEXT_NODE );
22 # Default config file, if none is specified
23 use constant CONFIG_FNAME => "/etc/koha/koha-conf.xml";
25 # path to config file set by installer
26 # __KOHA_CONF_DIR__ is set by rewrite-confg.PL
27 # when Koha is installed in 'standard' or 'single'
28 # mode. If Koha was installed in 'dev' mode,
29 # __KOHA_CONF_DIR__ is *not* rewritten; instead
30 # developers should set the KOHA_CONF environment variable
31 my $INSTALLED_CONFIG_FNAME = '__KOHA_CONF_DIR__/koha-conf.xml';
33 # Should not be called outside of C4::Context
34 # use C4::Context->config instead
36 my ( $class, $file ) = @_;
38 return if not defined $file;
42 my $dom = XML::LibXML->load_xml(location => $file);
43 foreach my $childNode ($dom->documentElement->nonBlankChildNodes) {
44 $class->_read_from_dom_node($childNode, $config);
49 die "\nError reading file $file.\nTry running this again as the koha instance user (or use the koha-shell command in debian)\n\n";
55 sub _read_from_dom_node {
56 my ($class, $node, $config) = @_;
58 if ($node->nodeType == XML_TEXT_NODE) {
59 $config->{content} = $node->textContent;
60 } elsif ($node->nodeType == XML_ELEMENT_NODE) {
63 foreach my $attribute ($node->attributes) {
64 my $key = $attribute->nodeName;
65 my $value = $attribute->value;
66 $subconfig->{$key} = $value;
69 foreach my $childNode ($node->nonBlankChildNodes) {
70 $class->_read_from_dom_node($childNode, $subconfig);
73 my $key = $node->nodeName;
74 if ($node->hasAttribute('id')) {
75 my $id = $node->getAttribute('id');
76 $config->{$key} //= {};
77 $config->{$key}->{$id} = $subconfig;
78 delete $subconfig->{id};
80 my @keys = keys %$subconfig;
81 if (1 == scalar @keys && $keys[0] eq 'content') {
82 # An element with no attributes and no child elements becomes its text content
83 $subconfig = $subconfig->{content};
84 } elsif (0 == scalar @keys) {
85 # An empty element becomes an empty string
89 if (exists $config->{$key}) {
90 unless (ref $config->{$key} eq 'ARRAY') {
91 $config->{$key} = [$config->{$key}];
93 push @{ $config->{$key} }, $subconfig;
95 if (grep { $_ eq $key } (qw(listen server serverinfo))) {
96 # <listen>, <server> and <serverinfo> are always arrays
97 $config->{$key} = [$subconfig];
99 $config->{$key} = $subconfig;
106 # Koha's main configuration file koha-conf.xml
107 # is searched for according to this priority list:
109 # 1. Path supplied via use C4::Context '/path/to/koha-conf.xml'
110 # 2. Path supplied in KOHA_CONF environment variable.
111 # 3. Path supplied in INSTALLED_CONFIG_FNAME, as long
112 # as value has changed from its default of
113 # '__KOHA_CONF_DIR__/koha-conf.xml', as happens
114 # when Koha is installed in 'standard' or 'single'
116 # 4. Path supplied in CONFIG_FNAME.
118 # The first entry that refers to a readable file is used.
120 sub guess_koha_conf {
122 # If the $KOHA_CONF environment variable is set, use
123 # that. Otherwise, use the built-in default.
125 if ( exists $ENV{"KOHA_CONF"} and $ENV{'KOHA_CONF'} and -s $ENV{"KOHA_CONF"} ) {
126 $conf_fname = $ENV{"KOHA_CONF"};
127 } elsif ( $INSTALLED_CONFIG_FNAME !~ /__KOHA_CONF_DIR/ and -s $INSTALLED_CONFIG_FNAME ) {
128 # NOTE: be careful -- don't change __KOHA_CONF_DIR in the above
129 # regex to anything else -- don't want installer to rewrite it
130 $conf_fname = $INSTALLED_CONFIG_FNAME;
131 } elsif ( -s CONFIG_FNAME ) {
132 $conf_fname = CONFIG_FNAME;