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 'finish' ) {
242 my $kohaversion=C4::Context::KOHAVERSION;
243 # remove the 3 last . to have a Perl number
244 $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
245 if (C4::Context->preference('Version')) {
246 warn "UPDATE Version";
247 my $finish=$dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='Version'");
248 $finish->execute($kohaversion);
250 warn "INSERT Version";
251 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')");
252 $finish->execute($kohaversion);
255 # Installation is finished.
256 # We just deny anybody acess to install
257 # And we redirect people to mainpage.
258 # The installer wil have to relogin since we donot pass cookie to redirection.
259 $template->param( "$op" => 1 );
261 elsif ( $op && $op eq 'finished' ) {
264 # we have finished, just redirect to mainpage.
267 print $query->redirect("/cgi-bin/koha/mainpage.pl");
270 elsif ( $op && $op eq 'addframeworks' ) {
272 # 1ST install : insert the SQL files the user has selected
275 #Framework importing and reports
279 # sort by filename -> prepend with numbers to specify order of insertion.
281 my @aa = split /\/|\\/, ($a);
282 my @bb = split /\/|\\/, ($b);
284 } $query->param('framework');
285 $dbh->do('SET FOREIGN_KEY_CHECKS=0');
288 "SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'"
291 my ($systempreference) = $request->fetchrow;
292 foreach my $file (@fnames) {
296 my $strcmd = "mysql "
297 . ( $info{hostname} ? " -h $info{hostname} " : "" )
298 . ( $info{port} ? " -P $info{port} " : "" )
299 . ( $info{user} ? " -u $info{user} " : "" )
300 . ( $info{password} ? " -p$info{password}" : "" )
302 my $error = qx($strcmd < $file 2>&1);
303 my @file = split qr(\/|\\), $file;
304 $lang = $file[ scalar(@file) - 3 ] unless ($lang);
305 my $level = $file[ scalar(@file) - 2 ];
307 $systempreference .= "$file[scalar(@file)-1]|"
309 index( $systempreference, $file[ scalar(@file) - 1 ] ) >=
313 #Bulding here a hierarchy to display files by level.
314 push @{ $hashlevel{$level} },
315 { "fwkname" => $file[ scalar(@file) - 1 ], "error" => $error };
318 #systempreference contains an ending |
319 chop $systempreference;
321 map { push @list, { "level" => $_, "fwklist" => $hashlevel{$_} } }
324 for my $each_language (@$all_languages) {
326 # warn "CODE".$each_language->{'language_code'};
327 # warn "LANG:".$lang;
328 if ( $lang eq $each_language->{'language_code'} ) {
329 $fwk_language = $each_language->{language_locale_name};
334 "UPDATE systempreferences set value=\"$systempreference\" where variable='FrameworksLoaded'"
336 unless ( $updateflag == 1 ) {
338 "INSERT INTO systempreferences (value, variable, explanation, type) VALUES (\"$systempreference\",'FrameworksLoaded','Frameworks loaded through webinstaller','choice')";
339 my $rq = $dbh->prepare($string);
343 "fwklanguage" => $fwk_language,
346 $template->param( "$op" => 1 );
347 $dbh->do('SET FOREIGN_KEY_CHECKS=1');
349 elsif ( $op && $op eq 'selectframeworks' ) {
352 # 1ST install : show the user the sql datas he can insert in the database.
355 # (note that the term "selectframeworks is not correct. The user can select various files, not only frameworks)
358 #sql data for import are supposed to be located in installer/data/<language>/<level>
359 # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
360 # Where <level> is a category of requirement : required, recommended optional
361 # level should contain :
362 # SQL File for import With a readable name.
363 # txt File taht explains what this SQL File is meant for.
364 # Could be VERY useful to have A Big file for a kind of library.
365 # But could also be useful to have some Authorised values data set prepared here.
366 # Framework Selection is achieved through checking boxes.
367 my $langchoice = $query->param('fwklanguage');
368 $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
369 my $dir = C4::Context->config('intranetdir') . "/installer/data/";
370 opendir( MYDIR, $dir );
371 my @listdir = grep { !/^\.|CVS/ && -d "$dir/$_" } readdir(MYDIR);
373 my $frmwklangs = getFrameworkLanguages();
378 'dirname' => $_->{'language_code'},
379 'languagedescription' => $_->{'language_name'},
380 'checked' => ( $_->{'language_code'} eq $langchoice )
382 if ( $_->{'language_code'} );
384 $template->param( "languagelist" => \@languages );
387 C4::Context->config('intranetdir') . "/installer/data/$langchoice";
388 opendir( MYDIR, $dir ) || warn "no open $dir";
389 @listdir = grep { !/^\.|CVS/ && -d "$dir/$_" } readdir(MYDIR);
394 "SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'"
397 my ($frameworksloaded) = $request->fetchrow;
398 my %frameworksloaded;
400 foreach ( split( /\|/, $frameworksloaded ) ) {
401 $frameworksloaded{$_} = 1;
403 foreach my $requirelevel (@listdir) {
405 C4::Context->config('intranetdir')
406 . "/installer/data/$langchoice/$requirelevel";
407 opendir( MYDIR, $dir );
409 grep { !/^\.|CVS/ && -f "$dir/$_" && $_ =~ m/\.sql$/ }
415 my $name = substr( $_, 0, -4 );
416 open FILE, "< $dir/$name.txt";
418 $lines =~ s/\n|\r/<br \/>/g;
420 utf8::encode($lines) unless ( utf8::is_utf8($lines) );
424 'fwkfile' => "$dir/$_",
425 'fwkdescription' => $lines,
428 $frameworksloaded{$_}
429 || ( $requirelevel =~
430 /(mandatory|requi|oblig|necess)/i )
436 sort { $a->{'fwkname'} lt $b->{'fwkname'} } @frameworklist;
438 # $cell{"mandatory"}=($requirelevel=~/(mandatory|requi|oblig|necess)/i);
439 $cell{"frameworks"} = \@fwks;
440 $cell{"label"} = ucfirst($requirelevel);
441 $cell{"code"} = lc($requirelevel);
442 push @levellist, \%cell;
444 $template->param( "levelloop" => \@levellist );
445 $template->param( "$op" => 1 );
447 elsif ( $op && $op eq 'updatestructure' ) {
449 #Do updatedatabase And report
451 C4::Context->config("intranetdir") . "/updater/updatedatabase";
453 my $string = qx|$execstring 2>&1|;
455 $string =~ s/\n|\r/<br \/>/g;
457 s/(DBD::mysql.*? failed: .*? line [0-9]*.|=================.*?====================)/<font color=red>$1<\/font>/g;
458 $template->param( "updatereport" => $string );
460 $template->param( $op => 1 );
462 elsif ( $op && $op eq 'importdatastructure' ) {
465 # UPDATE (not 1st install) run updatedatabase
469 #Import data structure and show errors if any
470 #Uses DBI to read the file [MJR 2007-07-01]
471 my $dbh = DBI->connect(
472 "DBI:$info{dbms}:$info{dbname}:$info{hostname}"
473 . ( $info{port} ? ":$info{port}" : "" ),
474 $info{'user'}, $info{'password'}
476 open( INPUT, "<kohastructure.sql" );
477 my $file = do { local $/ = undef; <INPUT> };
478 my @commands = split( /;/, $file );
480 map { $dbh->do($_) } @commands;
483 "error" => $dbh->errstr,
490 #Check if there are enough tables.
491 # Paul has cleaned up tables so reduced the count
492 #I put it there because it implied a data import if condition was not satisfied.
493 my $dbh = DBI->connect(
494 "DBI:$info{dbms}:$info{dbname}:$info{hostname}"
495 . ( $info{port} ? ":$info{port}" : "" ),
496 $info{'user'}, $info{'password'}
498 my $rq = $dbh->prepare( "SHOW TABLES FROM " . $info{'dbname'} );
500 my $data = $rq->fetchall_arrayref( {} );
501 my $count = scalar(@$data);
503 # we don't have tables, propose DB import
506 $template->param( "count" => $count, "proposeimport" => 1 );
510 # we have tables, propose to select files to upload or updatedatabase
512 $template->param( "count" => $count, "default" => 1 );
514 # 1st part of step 3 : check if there is a databaseversion systempreference
515 # if there is, then we just need to upgrade
516 # if there is none, then we need to install the database
518 my $dbversion = C4::Context->preference('Version');
519 $dbversion =~ /(.*)\.(..)(..)(...)/;
520 $dbversion = "$1.$2.$3.$4";
521 if (C4::Context->preference('Version')) {
522 $template->param("upgrading" => 1,
523 "dbversion" => $dbversion,
524 "kohaversion" => C4::Context->KOHAVERSION,
534 # LANGUAGE SELECTION page by default
535 # using opendir + language Hash
537 my $langavail = getTranslatedLanguages();
540 foreach (@$langavail) {
543 'value' => $_->{'language_code'},
544 'description' => $_->{'language_name'}
546 if ( $_->{'language_code'} );
548 $template->param( languages => \@languages );
552 "SELECT * from systempreferences WHERE variable='Version'");
553 if ( $rq->execute ) {
554 my ($version) = $rq->fetchrow;
556 $query->redirect("install.pl?step=3");
561 output_html_with_http_headers $query, $cookie, $template->output;