Bug 28489: Modify sessions.a_session from longtext to longblob
[koha.git] / Koha / Config.pm
1 package Koha::Config;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use XML::LibXML ':libxml';
21
22 # Default config file, if none is specified
23 use constant CONFIG_FNAME => "/etc/koha/koha-conf.xml";
24
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';
32
33 # Should not be called outside of C4::Context
34 # use C4::Context->config instead
35 sub read_from_file {
36     my ( $class, $file ) = @_;
37
38     return if not defined $file;
39
40     my $config = {};
41     eval {
42         my $dom = XML::LibXML->load_xml(location => $file);
43         foreach my $childNode ($dom->documentElement->nonBlankChildNodes) {
44             $class->_read_from_dom_node($childNode, $config);
45         }
46     };
47
48     if ($@) {
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";
50     }
51
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     }
104 }
105
106 # Koha's main configuration file koha-conf.xml
107 # is searched for according to this priority list:
108 #
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'
115 #    mode.
116 # 4. Path supplied in CONFIG_FNAME.
117 #
118 # The first entry that refers to a readable file is used.
119
120 sub guess_koha_conf {
121
122     # If the $KOHA_CONF environment variable is set, use
123     # that. Otherwise, use the built-in default.
124     my $conf_fname;
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;
133     }
134     return $conf_fname;
135 }
136
137 1;