3 # Copyright 2010 Jesse Weaver, Koha Dev Team
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # 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 # Koha is distributed in the hope that it will be useful, but
13 # 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 Koha; if not, see <http://www.gnu.org/licenses>.
28 $YAML::Syck
::ImplicitTyping
= 1;
29 $YAML::Syck
::SortKeys
= 1;
30 our %NOT_SET_PREFS = map { $_, 1 } qw( Version );
34 koha-preferences - Get, set, dump and load Koha system preferences
38 misc/admin/koha-preferences COMMAND ...
46 pod2usage
( -verbose
=> 1, -exitval
=> 1, -output
=> \
*STDERR
);
48 pod2usage
( -verbose
=> 1, -exitval
=> 0 );
55 print STDERR
$message . "\n" if ( $C4::Debug
::debug
);
58 sub _normalize_value
{
61 # AFAIK, there are no sysprefs where NULL has a different meaning than ''.
62 return ( $row->{'value'} || '' ) unless ( $row->{'type'} eq 'YesNo' );
65 # In theory, YesNo sysprefs should always contain 1/0.
66 # In practice, however, they don't.
69 my $bool_value = eval {
70 return C4
::Boolean
::true_p
( $row->{'value'} ) ?
1 : 0;
80 my ( $preference, $value ) = @_;
82 _debug
( "Setting $preference to $value" );
84 C4
::Context
->set_preference( $preference, $value );
88 my $dbh = C4
::Context
->dbh;
91 map { $_->{'variable'}, _normalize_value
($_) }
92 @
{ $dbh->selectall_arrayref( "
95 FROM systempreferences
96 ", { Slice
=> {} } ) }
101 my ( %preferences ) = @_;
103 my $dbh = C4
::Context
->dbh;
105 # First, a quick check to make sure all of the system preferences exist
106 my $current_state = $dbh->selectall_arrayref( "
108 variable, type, value
109 FROM systempreferences
110 WHERE variable IN (" . join( ', ', map( "?", keys %preferences ) ) . ")
111 ", { Slice
=> {} }, keys %preferences );
113 exit 2 if ( scalar( @
$current_state ) != scalar( keys %preferences ) );
115 foreach my $row ( @
$current_state ) {
116 # YAML::Syck encodes no as '', so deal with that
117 $preferences{$row->{'variable'}} = $preferences{$row->{'variable'}} eq '' ?
0 : C4
::Boolean
::true_p
( $preferences{$row->{'variable'}} ) if ( $row->{'type'} eq 'YesNo' );
120 # Iterate through again, now that we've checked all of the YesNo sysprefs
122 foreach my $row ( @
$current_state ) {
123 next if ( $preferences{$row->{'variable'}} eq $row->{'value'} );
125 _set_preference
( $row->{'variable'}, $preferences{$row->{'variable'}} );
128 # FIXME This may be not needed
129 C4
::Context
->clear_syspref_cache();
132 sub _fetch_preference
{
133 my ( $preference ) = @_;
135 my $dbh = C4
::Context
->dbh;
137 my $row = $dbh->selectrow_hashref( "
139 variable, value, type
140 FROM systempreferences
143 ", {}, $preference );
145 exit 2 unless ( $row );
151 my ( $preference ) = @_;
153 my $row = _fetch_preference
( $preference );
155 return _normalize_value
( $row );
159 my ( $preference, $value ) = @_;
161 my $row = _fetch_preference
( $preference );
163 $value = C4
::Boolean
::true_p
($value) ?
1 : 0 if ( $row->{'type'} eq 'YesNo' );
165 exit 3 if ( $value eq $row->{'value'} );
167 _set_preference
( $preference, $value );
170 sub ClearPreference
{
171 my ( $preference ) = @_;
175 my $row = _fetch_preference
( $preference );
177 $value = 0 if ( $row->{'type'} eq 'YesNo' );
179 exit 3 if ( $value eq $row->{'value'} );
181 _set_preference
( $preference, $value );
186 COMMAND can be any of the following:
190 =item B<dump> [ -o I<OUTFILE> ]
192 Dump all of Koha's system preferences as a simple YAML mapping into OUTFILE or
195 =item B<load> [ -i I<INFILE> ] [ -f|--force ]
197 Reads system preferences specified in YAML in INFILE or STDIN. Will exit with a
198 status of 2, without setting any sysprefs, if any of the sysprefs do not exist.
199 Will also exit if any of the sysprefs are YesNo and have an invalid value.
201 If there is a Version syspref in the input, it will not be set in the database,
202 but it will be checked to make sure the running Koha version is equal or higher.
203 The script will exit with a status of 4 if this is not true. Pass the -f option
206 =item B<get> I<PREFERENCE>
208 Print the value of the system preference PREFERENCE, followed by a newline. If
209 no such syspref exists, will exit with a status of 2.
211 =item B<set> I<PREFERENCE> I<VALUE>
213 Set the system preference PREFERENCE to the value VALUE. If no such syspref
214 exists, will exit with a status of 2. If the syspref already has that value,
215 will exit with a status of 3.
217 If the syspref is YesNo, will accept only a boolean value, but the syntax for
218 these is fairly lax (yes/no, on/off, 1/0, n/y, true/false are all accepted).
220 =item B<clear> I<PREFERENCE>
222 Clears the value of the system preference PREFERENCE. If no such syspref exists,
223 will exit with a status of 2. Will set YesNo sysprefs to 'false'.
227 Print a longer, more detailed manual.
237 ) || _print_usage
( 1 );
240 YAML
::Syck
::DumpFile
( $outfile, GetPreferences
() );
242 print YAML
::Syck
::Dump
( GetPreferences
() );
246 my ( $infile, $force_version );
250 'f' => \
$force_version,
253 my $preferences = YAML
::Syck
::LoadFile
($infile || \
*STDIN
);
255 die "Expected a YAML mapping" if ( ref($preferences) ne 'HASH' );
257 die "Tried to load preferences for version " . $preferences->{'Version'} . ", we are " . C4
::Context
->preference( 'Version' ) if ( $preferences->{'Version'} && C4
::Context
->preference( 'Version' ) < $preferences->{'Version'} );
260 map { $_, $preferences->{$_} }
261 grep { !$NOT_SET_PREFS{$_} }
265 SetPreferences
( %prefs_to_set );
268 my ( $preference ) = @_;
270 print_usage
() unless ( $preference );
272 print GetPreference
( $preference ) . "\n";
275 my ( $preference, $value ) = @_;
277 print_usage
() unless ( $preference && defined($value) );
279 SetPreference
( $preference, $value );
282 my ( $preference ) = @_;
284 print_usage
() unless ( $preference );
286 ClearPreference
( $preference );
289 pod2usage
( -verbose
=> 2 );
293 print_usage
() if ( $ARGV[0] =~ /^(-h|--help|-help|help)$/ );
295 print_usage
( 1 ) if ( !$ARGV[0] || ref($commands{$ARGV[0]}) ne 'CODE' );
297 $command = $commands{$ARGV[0]};
303 Print a short usage message.
309 $ export KOHA_DEBUG=1 # Used here to show what is being stored
310 $ misc/admin/koha-preferences get viewISBD
312 $ misc/admin/koha-preferences set viewISBD on
313 Setting viewISBD to 1
314 $ misc/admin/koha-preferences dump -o preferences.yaml
315 $ [ edit preferences.yaml ]
316 $ misc/admin/koha-preferences load -i preferences.yaml
317 $ misc/admin/koha-preferences load -i preferences-too-new.yaml
318 Tried to load preferences for version 3.0500012, we are 3.0300009 at misc/admin/koha-preferences line 255
319 $ misc/admin/koha-preferences load # Can also work from STDIN
323 Setting viewMARC to 1