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>.
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 );
59 my ( $preference, $value ) = @_;
61 _debug( "Setting $preference to $value" );
63 if ( $preference->{type} eq 'YesNo'
67 print STDERR sprintf "System preference %s is YesNo and expects 1 or 0. '%s' was given, using '0'\n",
68 $preference->{variable}, $value;
72 C4::Context->set_preference( $preference->{variable}, $value );
76 my $dbh = C4::Context->dbh;
79 map { $_->{variable}, $_->{value} }
80 @{ $dbh->selectall_arrayref( "
83 FROM systempreferences
84 ", { Slice => {} } ) }
89 my ( %preferences ) = @_;
91 my $dbh = C4::Context->dbh;
93 # First, a quick check to make sure all of the system preferences exist
94 my $current_state = $dbh->selectall_arrayref( "
97 FROM systempreferences
98 WHERE variable IN (" . join( ', ', map( "?", keys %preferences ) ) . ")
99 ", { Slice => {} }, keys %preferences );
101 exit 2 if ( scalar( @$current_state ) != scalar( keys %preferences ) );
103 foreach my $row ( @$current_state ) {
104 my $new_value = $preferences{ $row->{variable} };
105 next if $new_value && $row->{value} && $new_value eq $row->{value};
107 _set_preference( $row, $new_value );
110 # FIXME This may be not needed
111 C4::Context->clear_syspref_cache();
114 sub _fetch_preference {
115 my ( $preference ) = @_;
117 my $dbh = C4::Context->dbh;
119 my $row = $dbh->selectrow_hashref( "
121 variable, value, type
122 FROM systempreferences
125 ", {}, $preference );
127 exit 2 unless ( $row );
133 my ( $preference ) = @_;
135 return _fetch_preference( $preference );
139 my ( $preference, $value ) = @_;
141 my $row = _fetch_preference( $preference );
143 exit 3 if ( $value eq $row->{'value'} ); #FIXME exit??
145 _set_preference( $row, $value );
148 sub ClearPreference {
149 my ( $preference ) = @_;
153 my $row = _fetch_preference( $preference );
155 $value = 0 if ( $row->{'type'} eq 'YesNo' );
157 exit 3 if ( $value eq $row->{'value'} );
159 _set_preference( $row, $value );
164 COMMAND can be any of the following:
168 =item B<dump> [ -o I<OUTFILE> ]
170 Dump all of Koha's system preferences as a simple YAML mapping into OUTFILE or
173 =item B<load> [ -i I<INFILE> ] [ -f|--force ]
175 Reads system preferences specified in YAML in INFILE or STDIN. Will exit with a
176 status of 2, without setting any sysprefs, if any of the sysprefs do not exist.
177 Will also exit if any of the sysprefs are YesNo and have an invalid value.
179 If there is a Version syspref in the input, it will not be set in the database,
180 but it will be checked to make sure the running Koha version is equal or higher.
181 The script will exit with a status of 4 if this is not true. Pass the -f option
184 =item B<get> I<PREFERENCE>
186 Print the value of the system preference PREFERENCE, followed by a newline. If
187 no such syspref exists, will exit with a status of 2.
189 =item B<set> I<PREFERENCE> I<VALUE>
191 Set the system preference PREFERENCE to the value VALUE. If no such syspref
192 exists, will exit with a status of 2. If the syspref already has that value,
193 will exit with a status of 3.
195 If the syspref is YesNo, will accept only a boolean value. The syntax is *not*
196 lax and must be 1 or 0.
198 =item B<clear> I<PREFERENCE>
200 Clears the value of the system preference PREFERENCE. If no such syspref exists,
201 will exit with a status of 2. Will set YesNo sysprefs to 'false'.
205 Print a longer, more detailed manual.
215 ) || _print_usage( 1 );
218 YAML::XS::DumpFile( $outfile, GetPreferences() );
220 print Encode::decode_utf8(YAML::XS::Dump(GetPreferences()));
224 my ( $infile, $force_version );
228 'f' => \$force_version,
231 my $preferences = YAML::XS::LoadFile($infile || \*STDIN);
233 die "Expected a YAML mapping" if ( ref($preferences) ne 'HASH' );
235 die "Tried to load preferences for version " . $preferences->{'Version'} . ", we are " . C4::Context->preference( 'Version' ) if ( $preferences->{'Version'} && C4::Context->preference( 'Version' ) < $preferences->{'Version'} );
238 map { $_, $preferences->{$_} }
239 grep { !$NOT_SET_PREFS{$_} }
243 SetPreferences( %prefs_to_set );
246 my ( $preference ) = @_;
248 print_usage() unless ( $preference );
250 print GetPreference( $preference ) . "\n";
253 my ( $preference, $value ) = @_;
255 print_usage() unless ( $preference && defined($value) );
257 SetPreference( $preference, $value );
260 my ( $preference ) = @_;
262 print_usage() unless ( $preference );
264 ClearPreference( $preference );
267 pod2usage( -verbose => 2 );
271 print_usage() if ( $ARGV[0] =~ /^(-h|--help|-help|help)$/ );
273 print_usage( 1 ) if ( !$ARGV[0] || ref($commands{$ARGV[0]}) ne 'CODE' );
275 my $command = $commands{$ARGV[0]};
281 Print a short usage message.
287 $ export KOHA_DEBUG=1 # Used here to show what is being stored
288 $ misc/admin/koha-preferences get viewISBD
290 $ misc/admin/koha-preferences set viewISBD on
291 Setting viewISBD to 1
292 $ misc/admin/koha-preferences dump -o preferences.yaml
293 $ [ edit preferences.yaml ]
294 $ misc/admin/koha-preferences load -i preferences.yaml
295 $ misc/admin/koha-preferences load -i preferences-too-new.yaml
296 Tried to load preferences for version 3.0500012, we are 3.0300009 at misc/admin/koha-preferences line 255
297 $ misc/admin/koha-preferences load # Can also work from STDIN
301 Setting viewMARC to 1