1 #!/usr/bin/perl -w # please develop with -w
11 use strict; # please develop with the strict pragma
16 my $step = $query->param('step');
18 my $language = $query->param('language');
19 my ( $template, $loggedinuser, $cookie );
21 my $all_languages = getAllLanguages();
23 if ( defined($language) ) {
24 setlanguagecookie( $query, $language, "install.pl?step=1" );
26 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
28 template_name => "installer/step" . ( $step ? $step : 1 ) . ".tmpl",
37 $info{'dbname'} = C4::Context->config("database");
39 ( C4::Context->config("db_scheme")
40 ? C4::Context->config("db_scheme")
42 $info{'hostname'} = C4::Context->config("hostname");
43 $info{'port'} = C4::Context->config("port");
44 $info{'user'} = C4::Context->config("user");
45 $info{'password'} = C4::Context->config("pass");
46 my $dbh = DBI->connect(
47 "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
48 . ( $info{port} ? ";port=$info{port}" : "" ),
49 $info{'user'}, $info{'password'}
52 if ( $step && $step == 1 ) {
54 #Checking ALL perl Modules and services needed are installed.
55 #Whenever there is an error, adding a report to the page
56 $template->param( language => 1 );
59 unless ( $] >= 5.006001 ) { # Bug 179
60 $template->param( "problems" => 1, "perlversion" => 1 );
64 # We could here use a special find
66 unless ( eval { require ZOOM } ) {
67 push @missing, { name => "ZOOM" };
69 unless ( eval { require LWP::Simple } ) {
70 push @missing, { name => "LWP::Simple" };
72 unless ( eval { require XML::Simple } ) {
73 push @missing, { name => "XML::Simple" };
75 unless ( eval { require MARC::File::XML } ) {
76 push @missing, { name => "MARC::File::XML" };
78 unless ( eval { require MARC::File::USMARC } ) {
79 push @missing, { name => "MARC::File::USMARC" };
81 unless ( eval { require DBI } ) {
82 push @missing, { name => "DBI" };
84 unless ( eval { require Date::Manip } ) {
85 push @missing, { name => "Date::Manip" };
87 unless ( eval { require DBD::mysql } ) {
88 push @missing, { name => "DBD::mysql" };
90 unless ( eval { require HTML::Template::Pro } ) {
91 push @missing, { name => "HTML::Template::Pro" };
93 unless ( eval { require Date::Calc } ) {
94 push @missing, { name => "Date::Calc" };
96 unless ( eval { require Digest::MD5 } ) {
97 push @missing, { name => "Digest::MD5" };
99 unless ( eval { require MARC::Record } ) {
100 push @missing, { name => "MARC::Record" };
102 unless ( eval { require Mail::Sendmail } ) {
103 push @missing, { name => "Mail::Sendmail", usagemail => 1 };
105 unless ( eval { require List::MoreUtils } ) {
106 push @missing, { name => "List::MoreUtils" };
108 unless ( eval { require XML::RSS } ) {
109 push @missing, { name => "XML::RSS" };
111 unless ( eval { require CGI::Carp } ) {
112 push @missing, { name => "CGI::Carp" };
116 # The following modules are not mandatory, depends on how the library want to use Koha
117 unless ( eval { require PDF::API2 } ) {
118 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
119 push @missing, { name => "PDF::API2", usagebarcode => 1 };
122 unless ( eval { require GD::Barcorde } ) {
123 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
125 { name => "GD::Barcode", usagebarcode => 1, usagespine => 1 };
128 unless ( eval { require Data::Random } ) {
129 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
130 push @missing, { name => "Data::Random", usagebarcode => 1 };
133 unless ( eval { require PDF::Reuse::Barcode } ) {
134 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
135 push @missing, { name => "PDF::Reuse::Barcode", usagebarcode => 1 };
138 unless ( eval { require PDF::Report } ) {
139 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
140 push @missing, { name => "PDF::Report", usagebarcode => 1 };
143 unless ( eval { require Algorithm::CheckDigits } ) {
144 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
145 push @missing, { name => "Algorithm::CheckDigits", usagebarcode => 1 };
148 unless ( eval { require GD::Barcode::UPCE } ) {
149 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
150 push @missing, { name => "GD::Barcode::UPCE", usagepine => 1 };
153 unless ( eval { require Net::LDAP } ) {
154 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
155 push @missing, { name => "Net::LDAP", usageLDAP => 1 };
158 $template->param( missings => \@missing ) if ( scalar(@missing) > 0 );
159 $template->param( 'checkmodule' => 1 )
160 unless ( scalar(@missing) && $problem );
163 elsif ( $step && $step == 2 ) {
165 #STEP 2 Check Database connection and access
167 $template->param(%info);
168 my $checkdb = $query->param("checkdb");
169 $template->param( 'dbconnection' => $checkdb );
173 # Can connect to the mysql
174 $template->param( "checkdatabaseaccess" => 1 );
175 if ( $info{dbms} eq "mysql" ) {
177 #Check if database created
178 my $rv = $dbh->do("SHOW DATABASES LIKE \'$info{dbname}\'");
180 $template->param( 'checkdatabasecreated' => 1 );
183 #Check if user have all necessary grants on this database.
186 "SHOW GRANTS FOR \'$info{user}\'\@'$info{hostname}'");
189 while ( my ($line) = $rq->fetchrow ) {
190 my $dbname = $info{dbname};
191 if ( $line =~ m/^GRANT (.*?) ON `$dbname`\.\*/ || index( $line, '*.*' ) > 0 ) {
194 index( $line, 'ALL PRIVILEGES' ) > 0
195 || ( ( index( $line, 'SELECT' ) > 0 )
196 && ( index( $line, 'INSERT' ) > 0 )
197 && ( index( $line, 'UPDATE' ) > 0 )
198 && ( index( $line, 'DELETE' ) > 0 )
199 && ( index( $line, 'CREATE' ) > 0 )
200 && ( index( $line, 'DROP' ) > 0 ) )
204 unless ($grantaccess) {
206 $dbh->prepare("SHOW GRANTS FOR \'$info{user}\'\@'\%'");
208 while ( my ($line) = $rq->fetchrow ) {
209 my $dbname = $info{dbname};
210 if ( $line =~ m/$dbname/ || index( $line, '*.*' ) > 0 )
214 index( $line, 'ALL PRIVILEGES' ) > 0
215 || ( ( index( $line, 'SELECT' ) > 0 )
216 && ( index( $line, 'INSERT' ) > 0 )
217 && ( index( $line, 'UPDATE' ) > 0 )
218 && ( index( $line, 'DELETE' ) > 0 )
219 && ( index( $line, 'CREATE' ) > 0 )
220 && ( index( $line, 'DROP' ) > 0 ) )
225 $template->param( "checkgrantaccess" => $grantaccess );
226 } # End mysql connect check...
228 elsif ( $info{dbms} eq "Pg" ) {
229 # Check if database has been created...
230 my $rv = $dbh->do( "SELECT * FROM pg_catalog.pg_database WHERE datname = \'$info{dbname}\';" );
232 $template->param( 'checkdatabasecreated' => 1 );
235 # Check if user has all necessary grants on this database...
236 my $rq = $dbh->do( "SELECT u.usesuper
237 FROM pg_catalog.pg_user as u
238 WHERE u.usename = \'$info{user}\';" );
240 $template->param( "checkgrantaccess" => 1 );
242 } # End Pg connect check...
245 $template->param( "error" => DBI::err, "message" => DBI::errstr );
249 elsif ( $step && $step == 3 ) {
252 # STEP 3 : database setup
255 my $op = $query->param('op');
256 if ( $op && $op eq 'finished' ) {
258 # we have finished, just redirect to mainpage.
260 print $query->redirect("/cgi-bin/koha/mainpage.pl");
263 elsif ( $op && $op eq 'finish' ) {
264 my $kohaversion=C4::Context::KOHAVERSION;
265 # remove the 3 last . to have a Perl number
266 $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
267 if (C4::Context->preference('Version')) {
268 warn "UPDATE Version";
269 my $finish=$dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='Version'");
270 $finish->execute($kohaversion);
272 warn "INSERT Version";
273 my $finish=$dbh->prepare("INSERT into systempreferences (variable,value,explanation) values ('Version',?,'The Koha database version. Don t change this value manually, it s holded by the webinstaller')");
274 $finish->execute($kohaversion);
277 # Installation is finished.
278 # We just deny anybody access to install
279 # And we redirect people to mainpage.
280 # The installer will have to relogin since we do not pass cookie to redirection.
281 $template->param( "$op" => 1 );
283 elsif ( $op && $op eq 'Nozebra' ) {
285 if ($query->param('NoZebra')) {
287 $dbh->do("UPDATE systempreferences SET value=1 WHERE variable='NoZebra'");
288 $dbh->do("UPDATE systempreferences SET value=0 WHERE variable in ('QueryFuzzy','QueryWeightFields','QueryStemming')");
291 $dbh->do("UPDATE systempreferences SET value=0 WHERE variable='NoZebra'");
293 $template->param( "$op" => 1 );
295 elsif ( $op && $op eq 'addframeworks' ) {
297 # 1ST install, 3rd sub-step : insert the SQL files the user has selected
300 #Framework importing and reports
304 # sort by filename -> prepend with numbers to specify order of insertion.
306 my @aa = split /\/|\\/, ($a);
307 my @bb = split /\/|\\/, ($b);
309 } $query->param('framework');
310 if ( $info{dbms} eq 'mysql' ) { $dbh->do('SET FOREIGN_KEY_CHECKS=0'); }
311 elsif ( $info{dbms} eq 'Pg' ) { $dbh->do('SET CONSTRAINTS ALL DEFERRED;'); }
314 "SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'"
317 my ($systempreference) = $request->fetchrow;
318 foreach my $file (@fnames) {
323 if ( $info{dbms} eq 'mysql' ) {
324 my $strcmd = "mysql "
325 . ( $info{hostname} ? " -h $info{hostname} " : "" )
326 . ( $info{port} ? " -P $info{port} " : "" )
327 . ( $info{user} ? " -u $info{user} " : "" )
328 . ( $info{password} ? " -p$info{password}" : "" )
330 $error = qx($strcmd < $file 2>&1 1>/dev/null); # We want to send stdout to null and return only stderr... -fbcit
332 elsif ( $info{dbms} eq 'Pg' ) {
334 . ( $info{hostname} ? " -h $info{hostname} " : "" )
335 . ( $info{port} ? " -p $info{port} " : "" )
336 . ( $info{user} ? " -U $info{user} " : "" )
337 # . ( $info{password} ? " -W $info{password}" : "" )
339 $error = qx($strcmd -f $file 2>&1 1>/dev/null); # ...even more so with psql...
341 my @file = split qr(\/|\\), $file;
342 $lang = $file[ scalar(@file) - 3 ] unless ($lang);
343 my $level = $file[ scalar(@file) - 2 ];
345 $systempreference .= "$file[scalar(@file)-1]|"
347 index( $systempreference, $file[ scalar(@file) - 1 ] ) >=
351 #Bulding here a hierarchy to display files by level.
352 push @{ $hashlevel{$level} },
353 { "fwkname" => $file[ scalar(@file) - 1 ], "error" => $error };
356 #systempreference contains an ending |
357 chop $systempreference;
359 map { push @list, { "level" => $_, "fwklist" => $hashlevel{$_} } }
362 for my $each_language (@$all_languages) {
364 # warn "CODE".$each_language->{'language_code'};
365 # warn "LANG:".$lang;
366 if ( $lang eq $each_language->{'language_code'} ) {
367 $fwk_language = $each_language->{language_locale_name};
372 "UPDATE systempreferences set value=\"$systempreference\" where variable='FrameworksLoaded'"
374 unless ( $updateflag == 1 ) {
376 "INSERT INTO systempreferences (value, variable, explanation, type) VALUES (\"$systempreference\",'FrameworksLoaded','Frameworks loaded through webinstaller','choice')";
377 my $rq = $dbh->prepare($string);
381 "fwklanguage" => $fwk_language,
384 $template->param( "$op" => 1 );
385 if ( $info{dbms} eq 'mysql' ) { $dbh->do('SET FOREIGN_KEY_CHECKS=1'); }
386 elsif ( $info{dbms} eq 'Pg' ) { $dbh->do('SET CONSTRAINTS ALL IMMEDIATE;'); }
388 elsif ( $op && $op eq 'selectframeworks' ) {
391 # 1ST install, 2nd sub-step : show the user the sql datas he can insert in the database.
394 # (note that the term "selectframeworks is not correct. The user can select various files, not only frameworks)
397 #sql data for import are supposed to be located in installer/data/<language>/<level>
398 # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
399 # Where <level> is a category of requirement : required, recommended optional
400 # level should contain :
401 # SQL File for import With a readable name.
402 # txt File taht explains what this SQL File is meant for.
403 # Could be VERY useful to have A Big file for a kind of library.
404 # But could also be useful to have some Authorised values data set prepared here.
405 # Framework Selection is achieved through checking boxes.
406 my $langchoice = $query->param('fwklanguage');
407 $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
408 my $marcflavour = $query->param('marcflavour');
410 # we can have some variants of marc flavour, by having different directories, like : unimarc_small and unimarc_full, for small and complete unimarc frameworks.
411 # marc_cleaned finds the marcflavour, without the variant.
412 my $marc_cleaned = 'MARC21';
413 $marc_cleaned = 'UNIMARC' if $marcflavour =~ /unimarc/;
416 "INSERT IGNORE INTO `systempreferences` (variable,value,explanation,options,type) VALUES('marcflavour','$marc_cleaned','Define global MARC flavor (MARC21 or UNIMARC) used for character encoding','MARC21|UNIMARC','Choice');"
420 $marcflavour = C4::Context->preference('marcflavour') unless ($marcflavour);
421 #Insert into database the selected marcflavour
425 C4::Context->config('intranetdir') . "/installer/data/$info{dbms}/$langchoice/marcflavour/".lc($marcflavour);
426 opendir( MYDIR, $dir ) || warn "no open $dir";
427 my @listdir = sort grep { !/^\.|marcflavour/ && -d "$dir/$_" } readdir(MYDIR);
433 "SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'"
436 my ($frameworksloaded) = $request->fetchrow;
437 my %frameworksloaded;
438 foreach ( split( /\|/, $frameworksloaded ) ) {
439 $frameworksloaded{$_} = 1;
442 foreach my $requirelevel (@listdir) {
443 opendir( MYDIR, "$dir/$requirelevel" );
445 grep { !/^\./ && -f "$dir/$requirelevel/$_" && $_ =~ m/\.sql$/ }
451 my $name = substr( $_, 0, -4 );
452 open FILE, "<:utf8","$dir/$requirelevel/$name.txt";
454 $lines =~ s/\n|\r/<br \/>/g;
456 utf8::encode($lines) unless ( utf8::is_utf8($lines) );
460 'fwkfile' => "$dir/$requirelevel/$_",
461 'fwkdescription' => $lines,
464 $frameworksloaded{$_}
465 || ( $requirelevel =~
466 /(mandatory|requi|oblig|necess)/i )
472 sort { $a->{'fwkname'} lt $b->{'fwkname'} } @frameworklist;
474 # $cell{"mandatory"}=($requirelevel=~/(mandatory|requi|oblig|necess)/i);
475 $cell{"frameworks"} = \@fwks;
476 $cell{"label"} = ucfirst($requirelevel);
477 $cell{"code"} = lc($requirelevel);
478 push @fwklist, \%cell;
480 $template->param( "frameworksloop" => \@fwklist );
481 $template->param( "marcflavour" => ucfirst($marcflavour));
484 C4::Context->config('intranetdir') . "/installer/data/$info{dbms}/$langchoice";
485 opendir( MYDIR, $dir ) || warn "no open $dir";
486 @listdir = sort grep { !/^\.|marcflavour/ && -d "$dir/$_" } readdir(MYDIR);
489 foreach my $requirelevel (@listdir) {
490 opendir( MYDIR, "$dir/$requirelevel" );
492 grep { !/^\./ && -f "$dir/$requirelevel/$_" && $_ =~ m/\.sql$/ }
498 my $name = substr( $_, 0, -4 );
499 open FILE, "<:utf8","$dir/$requirelevel/$name.txt";
501 $lines =~ s/\n|\r/<br \/>/g;
503 utf8::encode($lines) unless ( utf8::is_utf8($lines) );
507 'fwkfile' => "$dir/$requirelevel/$_",
508 'fwkdescription' => $lines,
511 $frameworksloaded{$_}
512 || ( $requirelevel =~
513 /(mandatory|requi|oblig|necess)/i )
519 sort { $a->{'fwkname'} lt $b->{'fwkname'} } @frameworklist;
521 # $cell{"mandatory"}=($requirelevel=~/(mandatory|requi|oblig|necess)/i);
522 $cell{"frameworks"} = \@fwks;
523 $cell{"label"} = ucfirst($requirelevel);
524 $cell{"code"} = lc($requirelevel);
525 push @levellist, \%cell;
527 $template->param( "levelloop" => \@levellist );
528 $template->param( "$op" => 1 );
530 elsif ( $op && $op eq 'choosemarc' ) {
533 # 1ST install, 2nd sub-step : show the user the marcflavour available.
538 #sql data are supposed to be located in installer/data/<dbms>/<language>/marcflavour/marcflavourname
539 # Where <dbms> is database type according to DBD syntax
540 # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
541 # Where <level> is a category of requirement : required, recommended optional
542 # level should contain :
543 # SQL File for import With a readable name.
544 # txt File taht explains what this SQL File is meant for.
545 # Could be VERY useful to have A Big file for a kind of library.
546 # But could also be useful to have some Authorised values data set prepared here.
547 # Marcflavour Selection is achieved through radiobuttons.
548 my $langchoice = $query->param('fwklanguage');
549 $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
551 C4::Context->config('intranetdir') . "/installer/data/$info{dbms}/$langchoice/marcflavour";
552 opendir( MYDIR, $dir ) || warn "no open $dir";
553 my @listdir = grep { !/^\./ && -d "$dir/$_" } readdir(MYDIR);
555 my $marcflavour=C4::Context->preference("marcflavour");
557 foreach my $marc (@listdir) {
559 "label"=> ucfirst($marc),
561 "checked"=>uc($marc) eq $marcflavour);
562 # $cell{"description"}= do { local $/ = undef; open INPUT "<$dir/$marc.txt"||"";<INPUT> };
563 push @flavourlist, \%cell;
565 $template->param( "flavourloop" => \@flavourlist );
566 $template->param( "$op" => 1 );
568 elsif ( $op && $op eq 'importdatastructure' ) {
571 # 1st install, 1st "sub-step" : import kohastructure
574 my $datadir = C4::Context->config('intranetdir') . "/installer/data/$info{dbms}";
576 if ( $info{dbms} eq 'mysql' ) {
577 my $strcmd = "mysql "
578 . ( $info{hostname} ? " -h $info{hostname} " : "" )
579 . ( $info{port} ? " -P $info{port} " : "" )
580 . ( $info{user} ? " -u $info{user} " : "" )
581 . ( $info{password} ? " -p$info{password}" : "" )
583 $error = qx($strcmd <$datadir/kohastructure.sql 2>&1 1>/dev/null);
585 elsif ( $info{dbms} eq 'Pg' ) {
587 . ( $info{hostname} ? " -h $info{hostname} " : "" )
588 . ( $info{port} ? " -p $info{port} " : "" )
589 . ( $info{user} ? " -U $info{user} " : "" )
590 # . ( $info{password} ? " -W $info{password}" : "" ) # psql will NOT accept a password, but prompts...
591 . " $info{dbname} "; # Therefore, be sure to run 'trust' on localhost in pg_hba.conf -fbcit
592 $error = qx($strcmd -f $datadir/kohastructure.sql 2>&1 1>/dev/null);# Be sure to set 'client_min_messages = error' in postgresql.conf
593 # so that only true errors are returned to stderr or else the installer will
594 # report the import a failure although it really succeded -fbcit
601 elsif ( $op && $op eq 'updatestructure' ) {
603 # Not 1st install, the only sub-step : update database
605 #Do updatedatabase And report
607 C4::Context->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl";
609 my $string = qx($execstring 2>&1 1>/dev/null); # added '1>/dev/null' to return only stderr in $string. Needs testing here. -fbcit
611 $string =~ s/\n|\r/<br \/>/g;
613 s/(DBD::mysql.*? failed: .*? line [0-9]*.|=================.*?====================)/<font color=red>$1<\/font>/g;
614 $template->param( "updatereport" => $string );
616 $template->param( $op => 1 );
620 # check wether it's a 1st install or an update
622 #Check if there are enough tables.
623 # Paul has cleaned up tables so reduced the count
624 #I put it there because it implied a data import if condition was not satisfied.
625 my $dbh = DBI->connect(
626 "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
627 . ( $info{port} ? ";port=$info{port}" : "" ),
628 $info{'user'}, $info{'password'}
631 if ( $info{dbms} eq 'mysql' ) { $rq = $dbh->prepare( "SHOW TABLES FROM " . $info{'dbname'} ); }
632 elsif ( $info{dbms} eq 'Pg' ) { $rq = $dbh->prepare( "SELECT *
633 FROM information_schema.tables
634 WHERE table_schema='public' and table_type='BASE TABLE';" ); }
636 my $data = $rq->fetchall_arrayref( {} );
637 my $count = scalar(@$data);
639 # we don't have tables, propose DB import
642 $template->param( "count" => $count, "proposeimport" => 1 );
646 # we have tables, propose to select files to upload or updatedatabase
648 $template->param( "count" => $count, "default" => 1 );
650 # 1st part of step 3 : check if there is a databaseversion systempreference
651 # if there is, then we just need to upgrade
652 # if there is none, then we need to install the database
654 if (C4::Context->preference('Version')) {
655 my $dbversion = C4::Context->preference('Version');
656 $dbversion =~ /(.*)\.(..)(..)(...)/;
657 $dbversion = "$1.$2.$3.$4";
658 $template->param("upgrading" => 1,
659 "dbversion" => $dbversion,
660 "kohaversion" => C4::Context->KOHAVERSION,
670 # LANGUAGE SELECTION page by default
671 # using opendir + language Hash
673 my $langavail = getTranslatedLanguages();
676 foreach (@$langavail) {
679 'value' => $_->{'language_code'},
680 'description' => $_->{'language_name'}
682 if ( $_->{'language_code'} );
684 $template->param( languages => \@languages );
688 "SELECT * from systempreferences WHERE variable='Version'");
689 if ( $rq->execute ) {
690 my ($version) = $rq->fetchrow;
692 $query->redirect("install.pl?step=3");
697 output_html_with_http_headers $query, $cookie, $template->output;