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>.
31 our %NOT_SET_PREFS = map { $_, 1 } qw( Version );
35 koha-preferences - Get, set, dump and load Koha system preferences
39 misc/admin/koha-preferences COMMAND ...
47 pod2usage( -verbose => 1, -exitval => 1, -output => \*STDERR );
49 pod2usage( -verbose => 1, -exitval => 0 );
54 my ( $preference, $value ) = @_;
56 Koha::Logger->get->debug("Setting $preference to $value");
58 if ( $preference->{type} eq 'YesNo'
62 print STDERR sprintf "System preference %s is YesNo and expects 1 or 0. '%s' was given, using '0'\n",
63 $preference->{variable}, $value;
67 C4::Context->set_preference( $preference->{variable}, $value );
71 my $dbh = C4::Context->dbh;
74 map { $_->{variable}, $_->{value} }
75 @{ $dbh->selectall_arrayref( "
78 FROM systempreferences
79 ", { Slice => {} } ) }
84 my ( %preferences ) = @_;
86 my $dbh = C4::Context->dbh;
88 # First, a quick check to make sure all of the system preferences exist
89 my $current_state = $dbh->selectall_arrayref( "
92 FROM systempreferences
93 WHERE variable IN (" . join( ', ', map( "?", keys %preferences ) ) . ")
94 ", { Slice => {} }, keys %preferences );
96 exit 2 if ( scalar( @$current_state ) != scalar( keys %preferences ) );
98 foreach my $row ( @$current_state ) {
99 my $new_value = $preferences{ $row->{variable} };
100 next if $new_value && $row->{value} && $new_value eq $row->{value};
102 _set_preference( $row, $new_value );
105 # FIXME This may be not needed
106 C4::Context->clear_syspref_cache();
109 sub _fetch_preference {
110 my ( $preference ) = @_;
112 my $dbh = C4::Context->dbh;
114 my $row = $dbh->selectrow_hashref( "
116 variable, value, type
117 FROM systempreferences
120 ", {}, $preference );
122 exit 2 unless ( $row );
128 my ( $preference ) = @_;
130 return _fetch_preference( $preference );
134 my ( $preference, $value ) = @_;
136 my $row = _fetch_preference( $preference );
138 exit 3 if ( $value eq $row->{'value'} ); #FIXME exit??
140 _set_preference( $row, $value );
143 sub ClearPreference {
144 my ( $preference ) = @_;
148 my $row = _fetch_preference( $preference );
150 $value = 0 if ( $row->{'type'} eq 'YesNo' );
152 exit 3 if ( $value eq $row->{'value'} );
154 _set_preference( $row, $value );
159 COMMAND can be any of the following:
163 =item B<dump> [ -o I<OUTFILE> ]
165 Dump all of Koha's system preferences as a simple YAML mapping into OUTFILE or
168 =item B<load> [ -i I<INFILE> ] [ -f|--force ]
170 Reads system preferences specified in YAML in INFILE or STDIN. Will exit with a
171 status of 2, without setting any sysprefs, if any of the sysprefs do not exist.
172 Will also exit if any of the sysprefs are YesNo and have an invalid value.
174 If there is a Version syspref in the input, it will not be set in the database,
175 but it will be checked to make sure the running Koha version is equal or higher.
176 The script will exit with a status of 4 if this is not true. Pass the -f option
179 =item B<get> I<PREFERENCE>
181 Print the value of the system preference PREFERENCE, followed by a newline. If
182 no such syspref exists, will exit with a status of 2.
184 =item B<set> I<PREFERENCE> I<VALUE>
186 Set the system preference PREFERENCE to the value VALUE. If no such syspref
187 exists, will exit with a status of 2. If the syspref already has that value,
188 will exit with a status of 3.
190 If the syspref is YesNo, will accept only a boolean value. The syntax is *not*
191 lax and must be 1 or 0.
193 =item B<clear> I<PREFERENCE>
195 Clears the value of the system preference PREFERENCE. If no such syspref exists,
196 will exit with a status of 2. Will set YesNo sysprefs to 'false'.
200 Print a longer, more detailed manual.
210 ) || _print_usage( 1 );
212 YAML::XS::DumpFile( $outfile || \*STDOUT, GetPreferences() );
215 my ( $infile, $force_version );
219 'f' => \$force_version,
222 my $preferences = YAML::XS::LoadFile($infile || \*STDIN);
224 die "Expected a YAML mapping" if ( ref($preferences) ne 'HASH' );
226 die "Tried to load preferences for version " . $preferences->{'Version'} . ", we are " . C4::Context->preference( 'Version' ) if ( $preferences->{'Version'} && C4::Context->preference( 'Version' ) < $preferences->{'Version'} );
229 map { $_, $preferences->{$_} }
230 grep { !$NOT_SET_PREFS{$_} }
234 SetPreferences( %prefs_to_set );
237 my ( $preference ) = @_;
239 print_usage() unless ( $preference );
241 my $pref = GetPreference( $preference );
243 say $pref->{'value'};
246 my ( $preference, $value ) = @_;
248 print_usage() unless ( $preference && defined($value) );
250 SetPreference( $preference, $value );
253 my ( $preference ) = @_;
255 print_usage() unless ( $preference );
257 ClearPreference( $preference );
260 pod2usage( -verbose => 2 );
264 print_usage() if ( $ARGV[0] =~ /^(-h|--help|-help|help)$/ );
266 print_usage( 1 ) if ( !$ARGV[0] || ref($commands{$ARGV[0]}) ne 'CODE' );
268 my $command = $commands{$ARGV[0]};
274 Print a short usage message.
280 $ export KOHA_DEBUG=1 # Used here to show what is being stored
281 $ misc/admin/koha-preferences get viewISBD
283 $ misc/admin/koha-preferences set viewISBD on
284 Setting viewISBD to 1
285 $ misc/admin/koha-preferences dump -o preferences.yaml
286 $ [ edit preferences.yaml ]
287 $ misc/admin/koha-preferences load -i preferences.yaml
288 $ misc/admin/koha-preferences load -i preferences-too-new.yaml
289 Tried to load preferences for version 3.0500012, we are 3.0300009 at misc/admin/koha-preferences line 255
290 $ misc/admin/koha-preferences load # Can also work from STDIN
294 Setting viewMARC to 1