|
Lines 17-23
package Koha::Config;
Link Here
|
| 17 |
|
17 |
|
| 18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
| 19 |
|
19 |
|
| 20 |
use XML::Simple; |
20 |
use XML::LibXML ':libxml'; |
| 21 |
|
21 |
|
| 22 |
# Default config file, if none is specified |
22 |
# Default config file, if none is specified |
| 23 |
use constant CONFIG_FNAME => "/etc/koha/koha-conf.xml"; |
23 |
use constant CONFIG_FNAME => "/etc/koha/koha-conf.xml"; |
|
Lines 37-57
sub read_from_file {
Link Here
|
| 37 |
|
37 |
|
| 38 |
return if not defined $file; |
38 |
return if not defined $file; |
| 39 |
|
39 |
|
| 40 |
my $xml; |
40 |
my $config = {}; |
| 41 |
eval { |
41 |
eval { |
| 42 |
$xml = XMLin( |
42 |
my $dom = XML::LibXML->load_xml(location => $file); |
| 43 |
$file, |
43 |
foreach my $childNode ($dom->documentElement->nonBlankChildNodes) { |
| 44 |
keyattr => ['id'], |
44 |
$class->_read_from_dom_node($childNode, $config); |
| 45 |
forcearray => ['listen', 'server', 'serverinfo'], |
45 |
} |
| 46 |
suppressempty => '' |
|
|
| 47 |
); |
| 48 |
}; |
46 |
}; |
| 49 |
|
47 |
|
| 50 |
if ($@) { |
48 |
if ($@) { |
| 51 |
die "\nError reading file $file.\nTry running this again as the koha instance user (or use the koha-shell command in debian)\n\n"; |
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"; |
| 52 |
} |
50 |
} |
| 53 |
|
51 |
|
| 54 |
return $xml; |
52 |
return $config; |
|
|
53 |
} |
| 54 |
|
| 55 |
sub _read_from_dom_node { |
| 56 |
my ($class, $node, $config) = @_; |
| 57 |
|
| 58 |
if ($node->nodeType == XML_TEXT_NODE) { |
| 59 |
$config->{content} = $node->textContent; |
| 60 |
} elsif ($node->nodeType == XML_ELEMENT_NODE) { |
| 61 |
my $subconfig = {}; |
| 62 |
|
| 63 |
foreach my $attribute ($node->attributes) { |
| 64 |
my $key = $attribute->nodeName; |
| 65 |
my $value = $attribute->value; |
| 66 |
$subconfig->{$key} = $value; |
| 67 |
} |
| 68 |
|
| 69 |
foreach my $childNode ($node->nonBlankChildNodes) { |
| 70 |
$class->_read_from_dom_node($childNode, $subconfig); |
| 71 |
} |
| 72 |
|
| 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}; |
| 79 |
} else { |
| 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 |
| 86 |
$subconfig = ''; |
| 87 |
} |
| 88 |
|
| 89 |
if (exists $config->{$key}) { |
| 90 |
unless (ref $config->{$key} eq 'ARRAY') { |
| 91 |
$config->{$key} = [$config->{$key}]; |
| 92 |
} |
| 93 |
push @{ $config->{$key} }, $subconfig; |
| 94 |
} else { |
| 95 |
if (grep { $_ eq $key } (qw(listen server serverinfo))) { |
| 96 |
# <listen>, <server> and <serverinfo> are always arrays |
| 97 |
$config->{$key} = [$subconfig]; |
| 98 |
} else { |
| 99 |
$config->{$key} = $subconfig; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 55 |
} |
104 |
} |
| 56 |
|
105 |
|
| 57 |
# Koha's main configuration file koha-conf.xml |
106 |
# Koha's main configuration file koha-conf.xml |
| 58 |
- |
|
|