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{'hostname'}, $info{'port'} ) = ( $1, $2 )
44 if $info{'hostname'} =~ /([^:]*):([0-9]+)/;
45 $info{'user'} = C4::Context->config("user");
46 $info{'password'} = C4::Context->config("pass");
47 my $dbh = DBI->connect(
48 "DBI:$info{dbms}:$info{dbname}:$info{hostname}"
49 . ( $info{port} ? ":$info{port}" : "" ),
50 $info{'user'}, $info{'password'}
53 if ( $step && $step == 1 ) {
55 #Checking ALL perl Modules and services needed are installed.
56 #Whenever there is an error, adding a report to the page
57 $template->param( language => 1 );
60 unless ( $] >= 5.006001 ) { # Bug 179
61 $template->param( "problems" => 1, "perlversion" => 1 );
65 # We could here use a special find
67 unless ( eval { require ZOOM } ) {
68 push @missing, { name => "ZOOM" };
70 unless ( eval { require LWP::Simple } ) {
71 push @missing, { name => "LWP::Simple" };
73 unless ( eval { require XML::Simple } ) {
74 push @missing, { name => "XML::Simple" };
76 unless ( eval { require MARC::File::XML } ) {
77 push @missing, { name => "MARC::File::XML" };
79 unless ( eval { require MARC::File::USMARC } ) {
80 push @missing, { name => "MARC::File::USMARC" };
82 unless ( eval { require DBI } ) {
83 push @missing, { name => "DBI" };
85 unless ( eval { require Date::Manip } ) {
86 push @missing, { name => "Date::Manip" };
88 unless ( eval { require DBD::mysql } ) {
89 push @missing, { name => "DBD::mysql" };
91 unless ( eval { require HTML::Template } ) {
92 push @missing, { name => "HTML::Template::Pro" };
94 unless ( eval { require HTML::Template } ) {
95 push @missing, { name => "Date::Calc" };
97 unless ( eval { require Digest::MD5 } ) {
98 push @missing, { name => "Digest::MD5" };
100 unless ( eval { require MARC::Record } ) {
101 push @missing, { name => "MARC::Record" };
103 unless ( eval { require Mail::Sendmail } ) {
104 push @missing, { name => "Mail::Sendmail", usagemail => 1 };
106 unless ( eval { require List::MoreUtils } ) {
107 push @missing, { name => "List::MoreUtils" };
109 unless ( eval { require XML::RSS } ) {
110 push @missing, { name => "XML::RSS" };
112 unless ( eval { require CGI::Carp } ) {
113 push @missing, { name => "CGI::Carp" };
117 # The following modules are not mandatory, depends on how the library want to use Koha
118 unless ( eval { require PDF::API2 } ) {
119 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
120 push @missing, { name => "PDF::API2", usagebarcode => 1 };
123 unless ( eval { require GD::Barcorde } ) {
124 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
126 { name => "GD::Barcode", usagebarcode => 1, usagespine => 1 };
129 unless ( eval { require Data::Random } ) {
130 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
131 push @missing, { name => "Data::Random", usagebarcode => 1 };
134 unless ( eval { require PDF::Reuse::Barcode } ) {
135 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
136 push @missing, { name => "PDF::Reuse::Barcode", usagebarcode => 1 };
139 unless ( eval { require PDF::Report } ) {
140 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
141 push @missing, { name => "PDF::Report", usagebarcode => 1 };
144 unless ( eval { require Net::LDAP } ) {
145 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
146 push @missing, { name => "Algorithm::CheckDigits", usagebarcode => 1 };
149 unless ( eval { require GD::Barcode::UPCE } ) {
150 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
151 push @missing, { name => "GD::Barcode::UPCE", usagepine => 1 };
154 unless ( eval { require Net::LDAP } ) {
155 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
156 push @missing, { name => "Net::LDAP", usageLDAP => 1 };
159 $template->param( missings => \@missing ) if ( scalar(@missing) > 0 );
160 $template->param( 'checkmodule' => 1 )
161 unless ( scalar(@missing) && $problem );
164 elsif ( $step && $step == 2 ) {
166 #STEP 2 Check Database conn~ection and access
168 $template->param(%info);
169 my $checkmysql = $query->param("checkmysql");
170 $template->param( 'mysqlconnection' => $checkmysql );
174 # Can connect to the mysql
175 $template->param( "checkdatabaseaccess" => 1 );
176 if ( $info{dbms} eq "mysql" ) {
178 #Check if database created
179 my $rv = $dbh->do("SHOW DATABASES LIKE \'$info{dbname}\'");
181 $template->param( 'checkdatabasecreated' => 1 );
184 #Check if user have all necessary grants on this database.
187 "SHOW GRANTS FOR \'$info{user}\'\@'$info{hostname}'");
190 while ( my ($line) = $rq->fetchrow ) {
191 my $dbname = $info{dbname};
192 if ( $line =~ m/$dbname/ || index( $line, '*.*' ) > 0 ) {
195 index( $line, 'ALL PRIVILEGES' ) > 0
196 || ( ( index( $line, 'SELECT' ) > 0 )
197 && ( index( $line, 'INSERT' ) > 0 )
198 && ( index( $line, 'UPDATE' ) > 0 )
199 && ( index( $line, 'DELETE' ) > 0 )
200 && ( index( $line, 'CREATE' ) > 0 )
201 && ( index( $line, 'DROP' ) > 0 ) )
205 unless ($grantaccess) {
207 $dbh->prepare("SHOW GRANTS FOR \'$info{user}\'\@'\%'");
209 while ( my ($line) = $rq->fetchrow ) {
210 my $dbname = $info{dbname};
211 if ( $line =~ m/$dbname/ || index( $line, '*.*' ) > 0 )
215 index( $line, 'ALL PRIVILEGES' ) > 0
216 || ( ( index( $line, 'SELECT' ) > 0 )
217 && ( index( $line, 'INSERT' ) > 0 )
218 && ( index( $line, 'UPDATE' ) > 0 )
219 && ( index( $line, 'DELETE' ) > 0 )
220 && ( index( $line, 'CREATE' ) > 0 )
221 && ( index( $line, 'DROP' ) > 0 ) )
226 $template->param( "checkgrantaccess" => $grantaccess );
230 $template->param( "error" => DBI::err, "message" => DBI::errstr );
234 elsif ( $step && $step == 3 ) {
237 # STEP 3 : database setup
240 my $op = $query->param('op');
241 if ( $op && $op eq 'finished' ) {
243 # we have finished, just redirect to mainpage.
245 print $query->redirect("/cgi-bin/koha/mainpage.pl");
248 elsif ( $op && $op eq 'finish' ) {
249 my $kohaversion=C4::Context::KOHAVERSION;
250 # remove the 3 last . to have a Perl number
251 $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
252 if (C4::Context->preference('Version')) {
253 warn "UPDATE Version";
254 my $finish=$dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='Version'");
255 $finish->execute($kohaversion);
257 warn "INSERT Version";
258 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')");
259 $finish->execute($kohaversion);
262 # Installation is finished.
263 # We just deny anybody acess to install
264 # And we redirect people to mainpage.
265 # The installer wil have to relogin since we donot pass cookie to redirection.
266 $template->param( "$op" => 1 );
268 elsif ( $op && $op eq 'Nozebra' ) {
269 if ($query->param('Nozebra')) {
270 $dbh->do("UPDATE systempreferences SET value=1 WHERE variable='NoZebra'");
272 $dbh->do("UPDATE systempreferences SET value=0 WHERE variable='NoZebra'");
274 $template->param( "$op" => 1 );
276 elsif ( $op && $op eq 'addframeworks' ) {
278 # 1ST install, 3rd sub-step : insert the SQL files the user has selected
281 #Framework importing and reports
285 # sort by filename -> prepend with numbers to specify order of insertion.
287 my @aa = split /\/|\\/, ($a);
288 my @bb = split /\/|\\/, ($b);
290 } $query->param('framework');
291 $dbh->do('SET FOREIGN_KEY_CHECKS=0');
294 "SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'"
297 my ($systempreference) = $request->fetchrow;
298 foreach my $file (@fnames) {
302 my $strcmd = "mysql "
303 . ( $info{hostname} ? " -h $info{hostname} " : "" )
304 . ( $info{port} ? " -P $info{port} " : "" )
305 . ( $info{user} ? " -u $info{user} " : "" )
306 . ( $info{password} ? " -p$info{password}" : "" )
308 my $error = qx($strcmd < $file 2>&1);
309 my @file = split qr(\/|\\), $file;
310 $lang = $file[ scalar(@file) - 3 ] unless ($lang);
311 my $level = $file[ scalar(@file) - 2 ];
313 $systempreference .= "$file[scalar(@file)-1]|"
315 index( $systempreference, $file[ scalar(@file) - 1 ] ) >=
319 #Bulding here a hierarchy to display files by level.
320 push @{ $hashlevel{$level} },
321 { "fwkname" => $file[ scalar(@file) - 1 ], "error" => $error };
324 #systempreference contains an ending |
325 chop $systempreference;
327 map { push @list, { "level" => $_, "fwklist" => $hashlevel{$_} } }
330 for my $each_language (@$all_languages) {
332 # warn "CODE".$each_language->{'language_code'};
333 # warn "LANG:".$lang;
334 if ( $lang eq $each_language->{'language_code'} ) {
335 $fwk_language = $each_language->{language_locale_name};
340 "UPDATE systempreferences set value=\"$systempreference\" where variable='FrameworksLoaded'"
342 unless ( $updateflag == 1 ) {
344 "INSERT INTO systempreferences (value, variable, explanation, type) VALUES (\"$systempreference\",'FrameworksLoaded','Frameworks loaded through webinstaller','choice')";
345 my $rq = $dbh->prepare($string);
349 "fwklanguage" => $fwk_language,
352 $template->param( "$op" => 1 );
353 $dbh->do('SET FOREIGN_KEY_CHECKS=1');
355 elsif ( $op && $op eq 'selectframeworks' ) {
358 # 1ST install, 2nd sub-step : show the user the sql datas he can insert in the database.
361 # (note that the term "selectframeworks is not correct. The user can select various files, not only frameworks)
364 #sql data for import are supposed to be located in installer/data/<language>/<level>
365 # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
366 # Where <level> is a category of requirement : required, recommended optional
367 # level should contain :
368 # SQL File for import With a readable name.
369 # txt File taht explains what this SQL File is meant for.
370 # Could be VERY useful to have A Big file for a kind of library.
371 # But could also be useful to have some Authorised values data set prepared here.
372 # Framework Selection is achieved through checking boxes.
373 my $langchoice = $query->param('fwklanguage');
374 $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
375 my $dir = C4::Context->config('intranetdir') . "/installer/data/";
376 opendir( MYDIR, $dir );
377 my @listdir = grep { !/^\.|CVS/ && -d "$dir/$_" } readdir(MYDIR);
379 my $frmwklangs = getFrameworkLanguages();
384 'dirname' => $_->{'language_code'},
385 'languagedescription' => $_->{'language_name'},
386 'checked' => ( $_->{'language_code'} eq $langchoice )
388 if ( $_->{'language_code'} );
390 $template->param( "languagelist" => \@languages );
393 C4::Context->config('intranetdir') . "/installer/data/$langchoice";
394 opendir( MYDIR, $dir ) || warn "no open $dir";
395 @listdir = sort grep { !/^\.|CVS/ && -d "$dir/$_" } readdir(MYDIR);
400 "SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'"
403 my ($frameworksloaded) = $request->fetchrow;
404 my %frameworksloaded;
406 foreach ( split( /\|/, $frameworksloaded ) ) {
407 $frameworksloaded{$_} = 1;
409 foreach my $requirelevel (@listdir) {
411 C4::Context->config('intranetdir')
412 . "/installer/data/$langchoice/$requirelevel";
413 opendir( MYDIR, $dir );
415 grep { !/^\.|CVS/ && -f "$dir/$_" && $_ =~ m/\.sql$/ }
421 my $name = substr( $_, 0, -4 );
422 open FILE, "< $dir/$name.txt";
424 $lines =~ s/\n|\r/<br \/>/g;
426 utf8::encode($lines) unless ( utf8::is_utf8($lines) );
430 'fwkfile' => "$dir/$_",
431 'fwkdescription' => $lines,
434 $frameworksloaded{$_}
435 || ( $requirelevel =~
436 /(mandatory|requi|oblig|necess)/i )
442 sort { $a->{'fwkname'} lt $b->{'fwkname'} } @frameworklist;
444 # $cell{"mandatory"}=($requirelevel=~/(mandatory|requi|oblig|necess)/i);
445 $cell{"frameworks"} = \@fwks;
446 $cell{"label"} = ucfirst($requirelevel);
447 $cell{"code"} = lc($requirelevel);
448 push @levellist, \%cell;
450 $template->param( "levelloop" => \@levellist );
451 $template->param( "$op" => 1 );
453 elsif ( $op && $op eq 'importdatastructure' ) {
456 # 1st install, 1st "sub-step" : import kohastructure
459 my $dbh = DBI->connect(
460 "DBI:$info{dbms}:$info{dbname}:$info{hostname}"
461 . ( $info{port} ? ":$info{port}" : "" ),
462 $info{'user'}, $info{'password'}
464 open( INPUT, "<kohastructure.sql" );
465 my $file = do { local $/ = undef; <INPUT> };
466 my @commands = split( /;/, $file );
468 map { $dbh->do($_) } @commands;
471 "error" => $dbh->errstr,
476 elsif ( $op && $op eq 'updatestructure' ) {
478 # Not 1st install, the only sub-step : update database
480 #Do updatedatabase And report
482 C4::Context->config("intranetdir") . "/updater/updatedatabase";
484 my $string = qx|$execstring 2>&1|;
486 $string =~ s/\n|\r/<br \/>/g;
488 s/(DBD::mysql.*? failed: .*? line [0-9]*.|=================.*?====================)/<font color=red>$1<\/font>/g;
489 $template->param( "updatereport" => $string );
491 $template->param( $op => 1 );
495 # check wether it's a 1st install or an update
497 #Check if there are enough tables.
498 # Paul has cleaned up tables so reduced the count
499 #I put it there because it implied a data import if condition was not satisfied.
500 my $dbh = DBI->connect(
501 "DBI:$info{dbms}:$info{dbname}:$info{hostname}"
502 . ( $info{port} ? ":$info{port}" : "" ),
503 $info{'user'}, $info{'password'}
505 my $rq = $dbh->prepare( "SHOW TABLES FROM " . $info{'dbname'} );
507 my $data = $rq->fetchall_arrayref( {} );
508 my $count = scalar(@$data);
510 # we don't have tables, propose DB import
513 $template->param( "count" => $count, "proposeimport" => 1 );
517 # we have tables, propose to select files to upload or updatedatabase
519 $template->param( "count" => $count, "default" => 1 );
521 # 1st part of step 3 : check if there is a databaseversion systempreference
522 # if there is, then we just need to upgrade
523 # if there is none, then we need to install the database
525 my $dbversion = C4::Context->preference('Version');
526 $dbversion =~ /(.*)\.(..)(..)(...)/;
527 $dbversion = "$1.$2.$3.$4";
528 if (C4::Context->preference('Version')) {
529 $template->param("upgrading" => 1,
530 "dbversion" => $dbversion,
531 "kohaversion" => C4::Context->KOHAVERSION,
541 # LANGUAGE SELECTION page by default
542 # using opendir + language Hash
544 my $langavail = getTranslatedLanguages();
547 foreach (@$langavail) {
550 'value' => $_->{'language_code'},
551 'description' => $_->{'language_name'}
553 if ( $_->{'language_code'} );
555 $template->param( languages => \@languages );
559 "SELECT * from systempreferences WHERE variable='Version'");
560 if ( $rq->execute ) {
561 my ($version) = $rq->fetchrow;
563 $query->redirect("install.pl?step=3");
568 output_html_with_http_headers $query, $cookie, $template->output;