3 # Copyright (C) 2008 LibLime
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>.
22 use Encode
qw( encode is_utf8 );
24 use YAML
::Syck
qw( LoadFile );
29 use vars
qw(@ISA @EXPORT);
32 @ISA = qw( Exporter );
33 push @EXPORT, qw( primary_key_exists foreign_key_exists index_exists column_exists TableExists);
43 my $installer = C4::Installer->new();
44 my $all_languages = getAllLanguages();
45 my $error = $installer->load_db_schema();
47 #fill $list with list of sql files
48 my ($fwk_language, $error_list) = $installer->load_sql_in_order($all_languages, @$list);
49 $installer->set_version_syspref();
50 $installer->set_marcflavour_syspref('MARC21');
60 my $installer = C4::Installer->new();
62 Creates a new installer.
71 # get basic information from context
72 $self->{'dbname'} = C4
::Context
->config("database");
73 $self->{'dbms'} = C4
::Context
->config("db_scheme") ? C4
::Context
->config("db_scheme") : "mysql";
74 $self->{'hostname'} = C4
::Context
->config("hostname");
75 $self->{'port'} = C4
::Context
->config("port");
76 $self->{'user'} = C4
::Context
->config("user");
77 $self->{'password'} = C4
::Context
->config("pass");
78 $self->{'tls'} = C4
::Context
->config("tls");
79 if( $self->{'tls'} && $self->{'tls'} eq 'yes' ) {
80 $self->{'ca'} = C4
::Context
->config('ca');
81 $self->{'cert'} = C4
::Context
->config('cert');
82 $self->{'key'} = C4
::Context
->config('key');
83 $self->{'tlsoptions'} = ";mysql_ssl=1;mysql_ssl_client_key=".$self->{key
}.";mysql_ssl_client_cert=".$self->{cert
}.";mysql_ssl_ca_file=".$self->{ca
};
84 $self->{'tlscmdline'} = " --ssl-cert ". $self->{cert
} . " --ssl-key " . $self->{key
} . " --ssl-ca ".$self->{ca
}." "
86 $self->{'dbh'} = DBI
->connect("DBI:$self->{dbms}:dbname=$self->{dbname};host=$self->{hostname}" .
87 ( $self->{port
} ?
";port=$self->{port}" : "" ).
88 ( $self->{tlsoptions
} ?
$self->{tlsoptions
} : ""),
89 $self->{'user'}, $self->{'password'});
90 $self->{'language'} = undef;
91 $self->{'marcflavour'} = undef;
92 $self->{'dbh'}->do('set NAMES "utf8"');
93 $self->{'dbh'}->{'mysql_enable_utf8'}=1;
99 =head2 marc_framework_sql_list
101 my ($defaulted_to_en, $list) =
102 $installer->marc_framework_sql_list($lang, $marcflavour);
104 Returns in C<$list> a structure listing the filename, description, section,
105 and mandatory/optional status of MARC framework scripts available for C<$lang>
108 If the C<$defaulted_to_en> return value is true, no scripts are available
109 for language C<$lang> and the 'en' ones are returned.
113 sub marc_framework_sql_list
{
116 my $marcflavour = shift;
118 my $defaulted_to_en = 0;
121 my $dir = C4
::Context
->config('intranetdir') . "/installer/data/$self->{dbms}/$lang/marcflavour/".lc($marcflavour);
122 unless (opendir( MYDIR
, $dir )) {
124 warn "cannot open MARC frameworks directory $dir";
126 # if no translated MARC framework is available,
128 $dir = C4
::Context
->config('intranetdir') . "/installer/data/$self->{dbms}/en/marcflavour/".lc($marcflavour);
129 opendir(MYDIR
, $dir) or warn "cannot open English MARC frameworks directory $dir";
130 $defaulted_to_en = 1;
133 my @listdir = sort grep { !/^\.|marcflavour/ && -d
"$dir/$_" } readdir(MYDIR
);
137 my $request = $self->{'dbh'}->prepare("SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'");
139 my ($frameworksloaded) = $request->fetchrow;
140 $frameworksloaded = '' unless defined $frameworksloaded; # avoid warning
141 my %frameworksloaded;
142 foreach ( split( /\|/, $frameworksloaded ) ) {
143 $frameworksloaded{$_} = 1;
146 foreach my $requirelevel (@listdir) {
147 opendir( MYDIR
, "$dir/$requirelevel" );
148 my @listname = grep { !/^\./ && -f
"$dir/$requirelevel/$_" && $_ =~ m/\.(sql|yml)$/ } readdir(MYDIR
);
153 my ( $name, $ext ) = split /\./, $_;
155 if ( $ext =~ /yml/ ) {
156 my $yaml = LoadFile
("$dir/$requirelevel/$name\.$ext");
157 @lines = map { Encode
::decode
('UTF-8', $_) } @
{ $yaml->{'description'} };
159 open my $fh, "<:encoding(UTF-8)", "$dir/$requirelevel/$name.txt";
161 $line = Encode
::encode
('UTF-8', $line) unless ( Encode
::is_utf8
($line) );
162 @lines = split /\n/, $line;
164 my $mandatory = ($requirelevel =~ /(mandatory|requi|oblig|necess)/i);
168 'fwkfile' => "$dir/$requirelevel/$_",
169 'fwkdescription' => \
@lines,
170 'checked' => ( ( $frameworksloaded{$_} || $mandatory ) ?
1 : 0 ),
171 'mandatory' => $mandatory,
175 sort { $a->{'fwkname'} cmp $b->{'fwkname'} } @frameworklist;
177 $cell{"frameworks"} = \
@fwks;
178 $cell{"label"} = ($requirelevel =~ /(mandatory|requi|oblig|necess)/i)?
'mandatory':'optional';
179 $cell{"code"} = lc($requirelevel);
180 push @fwklist, \
%cell;
183 return ($defaulted_to_en, \
@fwklist);
186 =head2 sample_data_sql_list
188 my ($defaulted_to_en, $list) = $installer->sample_data_sql_list($lang);
190 Returns in C<$list> a structure listing the filename, description, section,
191 and mandatory/optional status of sample data scripts available for C<$lang>.
192 If the C<$defaulted_to_en> return value is true, no scripts are available
193 for language C<$lang> and the 'en' ones are returned.
197 sub sample_data_sql_list
{
201 my $defaulted_to_en = 0;
204 my $dir = C4
::Context
->config('intranetdir') . "/installer/data/$self->{dbms}/$lang";
205 unless (opendir( MYDIR
, $dir )) {
207 warn "cannot open sample data directory $dir";
209 # if no sample data is available,
211 $dir = C4
::Context
->config('intranetdir') . "/installer/data/$self->{dbms}/en";
212 opendir(MYDIR
, $dir) or warn "cannot open English sample data directory $dir";
213 $defaulted_to_en = 1;
216 my @listdir = sort grep { !/^\.|marcflavour/ && -d
"$dir/$_" } readdir(MYDIR
);
220 my $request = $self->{'dbh'}->prepare("SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'");
222 my ($frameworksloaded) = $request->fetchrow;
223 $frameworksloaded = '' unless defined $frameworksloaded; # avoid warning
224 my %frameworksloaded;
225 foreach ( split( /\|/, $frameworksloaded ) ) {
226 $frameworksloaded{$_} = 1;
229 foreach my $requirelevel (@listdir) {
230 opendir( MYDIR
, "$dir/$requirelevel" );
231 my @listname = grep { !/^\./ && -f
"$dir/$requirelevel/$_" && $_ =~ m/\.(sql|yml)$/ } readdir(MYDIR
);
236 my ( $name, $ext ) = split /\./, $_;
238 if ( $ext =~ /yml/ ) {
239 my $yaml = LoadFile
("$dir/$requirelevel/$name\.$ext");
240 @lines = map { Encode
::decode
('UTF-8', $_) } @
{ $yaml->{'description'} };
242 open my $fh, "<:encoding(UTF-8)", "$dir/$requirelevel/$name.txt";
244 $line = Encode
::encode
('UTF-8', $line) unless ( Encode
::is_utf8
($line) );
245 @lines = split /\n/, $line;
247 my $mandatory = ($requirelevel =~ /(mandatory|requi|oblig|necess)/i);
251 'fwkfile' => "$dir/$requirelevel/$_",
252 'fwkdescription' => \
@lines,
253 'checked' => ( ( $frameworksloaded{$_} || $mandatory ) ?
1 : 0 ),
254 'mandatory' => $mandatory,
257 my @fwks = sort { $a->{'fwkname'} cmp $b->{'fwkname'} } @frameworklist;
259 $cell{"frameworks"} = \
@fwks;
260 $cell{"label"} = ($requirelevel =~ /(mandatory|requi|oblig|necess)/i)?
'mandatory':'optional';
261 $cell{"code"} = lc($requirelevel);
262 push @levellist, \
%cell;
265 return ($defaulted_to_en, \
@levellist);
268 =head2 load_db_schema
270 my $error = $installer->load_db_schema();
272 Loads the SQL script that creates Koha's tables and indexes. The
273 return value is a string containing error messages reported by the
281 my $datadir = C4
::Context
->config('intranetdir') . "/installer/data/$self->{dbms}";
282 my $error = $self->load_sql("$datadir/kohastructure.sql");
287 =head2 load_sql_in_order
289 my ($fwk_language, $list) = $installer->load_sql_in_order($all_languages, @sql_list);
291 Given a list of SQL scripts supplied in C<@sql_list>, loads each of them
292 into the database and sets the FrameworksLoaded system preference to names
293 of the scripts that were loaded.
295 The SQL files are loaded in alphabetical order by filename (not including
296 directory path). This means that dependencies among the scripts are to
297 be resolved by carefully naming them, keeping in mind that the directory name
298 does *not* currently count.
300 B<FIXME:> this is a rather delicate way of dealing with dependencies between
303 The return value C<$list> is an arrayref containing a hashref for each
304 "level" or directory containing SQL scripts; the hashref in turns contains
305 a list of hashrefs containing a list of each script load and any error
306 messages associated with the loading of each script.
308 B<FIXME:> The C<$fwk_language> code probably doesn't belong and needs to be
309 moved to a different method.
313 sub load_sql_in_order
{
315 my $langchoice = shift;
316 my $all_languages = shift;
322 my @aa = split /\/|\\/, ($a);
323 my @bb = split /\/|\\/, ($b);
326 my $request = $self->{'dbh'}->prepare( "SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'" );
328 my ($systempreference) = $request->fetchrow;
329 $systempreference = '' unless defined $systempreference; # avoid warning
331 my $global_mandatory_dir = C4
::Context
->config('intranetdir') . "/installer/data/$self->{dbms}/mandatory";
333 # Make sure some stuffs are loaded first
335 "$global_mandatory_dir/sysprefs.sql",
336 "$global_mandatory_dir/subtag_registry.sql",
337 "$global_mandatory_dir/auth_val_cat.sql",
338 "$global_mandatory_dir/message_transport_types.sql",
339 "$global_mandatory_dir/sample_notices_message_attributes.sql",
340 "$global_mandatory_dir/sample_notices_message_transports.sql",
341 "$global_mandatory_dir/keyboard_shortcuts.sql",
344 push @fnames, "$global_mandatory_dir/userflags.sql",
345 "$global_mandatory_dir/userpermissions.sql",
346 "$global_mandatory_dir/audio_alerts.sql",
347 "$global_mandatory_dir/account_offset_types.sql",
348 "$global_mandatory_dir/account_credit_types.sql",
349 "$global_mandatory_dir/account_debit_types.sql",
351 my $localization_file = C4
::Context
->config('intranetdir') .
352 "/installer/data/$self->{dbms}/localization/$langchoice/custom.sql";
353 if ( $langchoice ne 'en' and -f
$localization_file ) {
354 push @fnames, $localization_file;
356 foreach my $file (@fnames) {
359 my $error = $self->load_sql($file);
360 my @file = split qr
(\
/|\\), $file;
361 $lang = $file[ scalar(@file) - 3 ] unless ($lang);
362 my $level = ( $file =~ /(localization)/ ) ?
$1 : $file[ scalar(@file) - 2 ];
364 $systempreference .= "$file[scalar(@file)-1]|"
365 unless ( index( $systempreference, $file[ scalar(@file) - 1 ] ) >= 0 );
368 #Bulding here a hierarchy to display files by level.
369 push @
{ $hashlevel{$level} },
370 { "fwkname" => $file[ scalar(@file) - 1 ], "error" => $error };
373 #systempreference contains an ending |
374 chop $systempreference;
376 map { push @list, { "level" => $_, "fwklist" => $hashlevel{$_} } } keys %hashlevel;
378 for my $each_language (@
$all_languages) {
380 # warn "CODE".$each_language->{'language_code'};
381 # warn "LANG:".$lang;
382 if ( $lang eq $each_language->{'language_code'} ) {
383 $fwk_language = $each_language->{language_locale_name
};
388 "UPDATE systempreferences set value=\"$systempreference\" where variable='FrameworksLoaded'"
391 unless ( $updateflag == 1 ) {
393 "INSERT INTO systempreferences (value, variable, explanation, type) VALUES (\"$systempreference\",'FrameworksLoaded','Frameworks loaded through webinstaller','choice')";
394 my $rq = $self->{'dbh'}->prepare($string);
397 return ($fwk_language, \
@list);
400 =head2 set_marcflavour_syspref
402 $installer->set_marcflavour_syspref($marcflavour);
404 Set the 'marcflavour' system preference. The incoming
405 C<$marcflavour> references to a subdirectory of
406 installer/data/$dbms/$lang/marcflavour, and is
407 normalized to MARC21, UNIMARC or NORMARC.
409 FIXME: this method assumes that the MARC flavour will be either
410 MARC21, UNIMARC or NORMARC.
414 sub set_marcflavour_syspref
{
416 my $marcflavour = shift;
418 # we can have some variants of marc flavour, by having different directories, like : unimarc_small and unimarc_full, for small and complete unimarc frameworks.
419 # marc_cleaned finds the marcflavour, without the variant.
420 my $marc_cleaned = 'MARC21';
421 $marc_cleaned = 'UNIMARC' if $marcflavour =~ /unimarc/i;
422 $marc_cleaned = 'NORMARC' if $marcflavour =~ /normarc/i;
424 $self->{'dbh'}->prepare(
425 "INSERT IGNORE INTO `systempreferences` (variable,value,explanation,options,type) VALUES('marcflavour','$marc_cleaned','Define global MARC flavor (MARC21, UNIMARC or NORMARC) used for character encoding','MARC21|UNIMARC|NORMARC','Choice');"
430 =head2 set_version_syspref
432 $installer->set_version_syspref();
434 Set or update the 'Version' system preference to the current
435 Koha software version.
439 sub set_version_syspref
{
442 my $kohaversion = Koha
::version
();
443 # remove the 3 last . to have a Perl number
444 $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
445 if (C4
::Context
->preference('Version')) {
446 warn "UPDATE Version";
447 my $finish=$self->{'dbh'}->prepare("UPDATE systempreferences SET value=? WHERE variable='Version'");
448 $finish->execute($kohaversion);
450 warn "INSERT Version";
451 my $finish=$self->{'dbh'}->prepare("INSERT into systempreferences (variable,value,explanation) values ('Version',?,'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller')");
452 $finish->execute($kohaversion);
454 C4
::Context
->clear_syspref_cache();
457 =head2 set_languages_syspref
459 $installer->set_languages_syspref();
461 Add the installation language to 'language' and 'opaclanguages' system preferences
462 if different from 'en'
466 sub set_languages_syspref
{
468 my $language = shift;
470 return if ( not $language or $language eq 'en' );
472 warn "UPDATE Languages";
474 my $pref = $self->{'dbh'}->prepare("UPDATE systempreferences SET value=? WHERE variable='language'");
475 $pref->execute("en,$language");
477 $pref = $self->{'dbh'}->prepare("UPDATE systempreferences SET value=? WHERE variable='opaclanguages'");
478 $pref->execute("en,$language");
480 C4
::Context
->clear_syspref_cache();
483 =head2 process_yml_table
485 my $query_info = $installer->process_yml_table($table);
487 Analyzes a table loaded in YAML format.
488 Returns the values required to build an insert statement.
492 sub process_yml_table
{
494 my $table_name = ( keys %$table )[0]; # table name
495 my @rows = @
{ $table->{$table_name}->{rows
} }; #
496 my @columns = ( sort keys %{$rows[0]} ); # column names
497 my $fields = join ",", map{sprintf("`%s`", $_)} @columns; # idem, joined
498 my $query = "INSERT INTO $table_name ( $fields ) VALUES ";
499 my @multiline = @
{ $table->{$table_name}->{'multiline'} }; # to check multiline values;
500 my $placeholders = '(' . join ( ",", map { "?" } @columns ) . ')'; # '(?,..,?)' string
502 foreach my $row ( @rows ) {
503 push @values, [ map {
505 ( @multiline and grep { $_ eq $col } @multiline )
506 ?
join "\r\n", @
{$row->{$col}} # join multiline values
510 return { query
=> $query, placeholders
=> $placeholders, values => \
@values };
515 my $error = $installer->load_sql($filename);
517 Runs the specified input file using a sql loader DBIx::RunSQL, or a yaml loader
518 Returns any strings sent to STDERR
520 # FIXME This should be improved: sometimes the caller and load_sql warn the same
527 my $filename = shift;
530 my $dbh = $self->{ dbh
};
535 open STDERR
, ">>", \
$dup_stderr;
537 if ( $filename =~ /sql$/ ) { # SQL files
539 DBIx
::RunSQL
->run_sql_file(
547 my $yaml = LoadFile
( $filename ); # Load YAML
548 for my $table ( @
{ $yaml->{'tables'} } ) {
549 my $query_info = process_yml_table
($table);
550 my $query = $query_info->{query
};
551 my $placeholders = $query_info->{placeholders
};
552 my $values = $query_info->{values};
553 # Doing only 1 INSERT query for the whole table
554 my @all_rows_values = map { @
$_ } @
$values;
555 $query .= join ', ', ( $placeholders ) x
scalar @
$values;
556 $dbh->do( $query, undef, @all_rows_values );
558 for my $statement ( @
{ $yaml->{'sql_statements'} } ) { # extra SQL statements
559 $dbh->do($statement);
564 warn "Something went wrong loading file $filename ($@)";
567 # errors thrown while loading installer data should be logged
569 warn "C4::Installer::load_sql returned the following errors while attempting to load $filename:\n";
570 $error = $dup_stderr;
576 =head2 get_file_path_from_name
578 my $filename = $installer->get_file_path_from_name('script_name');
580 searches through the set of known SQL scripts and finds the fully
581 qualified path name for the script that mathches the input.
583 returns undef if no match was found.
588 sub get_file_path_from_name
{
590 my $partialname = shift;
592 my $lang = 'en'; # FIXME: how do I know what language I want?
594 my ($defaulted_to_en, $list) = $self->sample_data_sql_list($lang);
595 # warn( Data::Dumper->Dump( [ $list ], [ 'list' ] ) );
598 foreach my $frameworklist ( @
$list ) {
599 push @found, grep { $_->{'fwkfile'} =~ /$partialname$/ } @
{$frameworklist->{'frameworks'}};
602 # warn( Data::Dumper->Dump( [ \@found ], [ 'found' ] ) );
603 if ( 0 == scalar @found ) {
605 } elsif ( 1 < scalar @found ) {
606 warn "multiple results found for $partialname";
609 return $found[0]->{'fwkfile'};
614 sub primary_key_exists
{
615 my ( $table_name, $key_name ) = @_;
616 my $dbh = C4
::Context
->dbh;
617 my ($exists) = $dbh->selectrow_array(
619 SHOW INDEX FROM
$table_name
620 WHERE key_name
= 'PRIMARY' AND column_name
= ?
626 sub foreign_key_exists
{
627 my ( $table_name, $constraint_name ) = @_;
628 my $dbh = C4
::Context
->dbh;
629 my (undef, $infos) = $dbh->selectrow_array(qq|SHOW CREATE TABLE
$table_name|);
630 return $infos =~ m
|CONSTRAINT
`$constraint_name` FOREIGN KEY
|;
634 my ( $table_name, $key_name ) = @_;
635 my $dbh = C4
::Context
->dbh;
636 my ($exists) = $dbh->selectrow_array(
638 SHOW INDEX FROM
$table_name
646 my ( $table_name, $column_name ) = @_;
647 return unless TableExists
($table_name);
648 my $dbh = C4
::Context
->dbh;
649 my ($exists) = $dbh->selectrow_array(
651 SHOW COLUMNS FROM
$table_name
653 |, undef, $column_name
658 sub TableExists
{ # Could be renamed table_exists for consistency
661 my $dbh = C4
::Context
->dbh;
662 local $dbh->{PrintError
} = 0;
663 local $dbh->{RaiseError
} = 1;
664 $dbh->do(qq{SELECT
* FROM
$table WHERE
1 = 0 });
673 C4::Installer is a refactoring of logic originally from installer/installer.pl, which was
674 originally written by Henri-Damien Laurant.
676 Koha Development Team <http://koha-community.org/>
678 Galen Charlton <galen.charlton@liblime.com>