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" };
113 # The following modules are not mandatory, depends on how the library want to use Koha
114 unless ( eval { require PDF::API2 } ) {
115 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
116 push @missing, { name => "PDF::API2", usagebarcode => 1 };
119 unless ( eval { require GD::Barcorde } ) {
120 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
122 { name => "GD::Barcode", usagebarcode => 1, usagespine => 1 };
125 unless ( eval { require Data::Random } ) {
126 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
127 push @missing, { name => "Data::Random", usagebarcode => 1 };
130 unless ( eval { require PDF::Reuse::Barcode } ) {
131 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
132 push @missing, { name => "PDF::Reuse::Barcode", usagebarcode => 1 };
135 unless ( eval { require PDF::Report } ) {
136 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
137 push @missing, { name => "PDF::Report", usagebarcode => 1 };
140 unless ( eval { require Net::LDAP } ) {
141 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
142 push @missing, { name => "Algorithm::CheckDigits", usagebarcode => 1 };
145 unless ( eval { require GD::Barcode::UPCE } ) {
146 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
147 push @missing, { name => "GD::Barcode::UPCE", usagepine => 1 };
150 unless ( eval { require Net::LDAP } ) {
151 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
152 push @missing, { name => "Net::LDAP", usageLDAP => 1 };
155 $template->param( missings => \@missing ) if ( scalar(@missing) > 0 );
156 $template->param( 'checkmodule' => 1 )
157 unless ( scalar(@missing) && $problem );
160 elsif ( $step && $step == 2 ) {
162 #STEP 2 Check Database conn~ection and access
164 $template->param(%info);
165 my $checkmysql = $query->param("checkmysql");
166 $template->param( 'mysqlconnection' => $checkmysql );
170 # Can connect to the mysql
171 $template->param( "checkdatabaseaccess" => 1 );
172 if ( $info{dbms} eq "mysql" ) {
174 #Check if database created
175 my $rv = $dbh->do("SHOW DATABASES LIKE \'$info{dbname}\'");
177 $template->param( 'checkdatabasecreated' => 1 );
180 #Check if user have all necessary grants on this database.
183 "SHOW GRANTS FOR \'$info{user}\'\@'$info{hostname}'");
186 while ( my ($line) = $rq->fetchrow ) {
187 my $dbname = $info{dbname};
188 if ( $line =~ m/$dbname/ || index( $line, '*.*' ) > 0 ) {
191 index( $line, 'ALL PRIVILEGES' ) > 0
192 || ( ( index( $line, 'SELECT' ) > 0 )
193 && ( index( $line, 'INSERT' ) > 0 )
194 && ( index( $line, 'UPDATE' ) > 0 )
195 && ( index( $line, 'DELETE' ) > 0 )
196 && ( index( $line, 'CREATE' ) > 0 )
197 && ( index( $line, 'DROP' ) > 0 ) )
201 unless ($grantaccess) {
203 $dbh->prepare("SHOW GRANTS FOR \'$info{user}\'\@'\%'");
205 while ( my ($line) = $rq->fetchrow ) {
206 my $dbname = $info{dbname};
207 if ( $line =~ m/$dbname/ || index( $line, '*.*' ) > 0 )
211 index( $line, 'ALL PRIVILEGES' ) > 0
212 || ( ( index( $line, 'SELECT' ) > 0 )
213 && ( index( $line, 'INSERT' ) > 0 )
214 && ( index( $line, 'UPDATE' ) > 0 )
215 && ( index( $line, 'DELETE' ) > 0 )
216 && ( index( $line, 'CREATE' ) > 0 )
217 && ( index( $line, 'DROP' ) > 0 ) )
222 $template->param( "checkgrantaccess" => $grantaccess );
226 $template->param( "error" => DBI::err, "message" => DBI::errstr );
230 elsif ( $step && $step == 3 ) {
233 # STEP 3 : database setup
236 my $op = $query->param('op');
237 if ( $op && $op eq 'finish' ) {
238 my $kohaversion=C4::Context::KOHAVERSION;
239 # remove the 3 last . to have a Perl number
240 $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
241 if (C4::Context->preference('Version')) {
242 warn "UPDATE Version";
243 my $finish=$dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='Version'");
244 $finish->execute($kohaversion);
246 warn "INSERT Version";
247 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')");
248 $finish->execute($kohaversion);
251 # Installation is finished.
252 # We just deny anybody acess to install
253 # And we redirect people to mainpage.
254 # The installer wil have to relogin since we donot pass cookie to redirection.
255 $template->param( "$op" => 1 );
257 elsif ( $op && $op eq 'finished' ) {
260 # we have finished, just redirect to mainpage.
263 print $query->redirect("/cgi-bin/koha/mainpage.pl");
266 elsif ( $op && $op eq 'addframeworks' ) {
268 # 1ST install : insert the SQL files the user has selected
271 #Framework importing and reports
275 # sort by filename -> prepend with numbers to specify order of insertion.
277 my @aa = split /\/|\\/, ($a);
278 my @bb = split /\/|\\/, ($b);
280 } $query->param('framework');
281 $dbh->do('SET FOREIGN_KEY_CHECKS=0');
284 "SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'"
287 my ($systempreference) = $request->fetchrow;
288 foreach my $file (@fnames) {
292 my $strcmd = "mysql "
293 . ( $info{hostname} ? " -h $info{hostname} " : "" )
294 . ( $info{port} ? " -P $info{port} " : "" )
295 . ( $info{user} ? " -u $info{user} " : "" )
296 . ( $info{password} ? " -p$info{password}" : "" )
298 my $error = qx($strcmd < $file 2>&1);
299 my @file = split qr(\/|\\), $file;
300 $lang = $file[ scalar(@file) - 3 ] unless ($lang);
301 my $level = $file[ scalar(@file) - 2 ];
303 $systempreference .= "$file[scalar(@file)-1]|"
305 index( $systempreference, $file[ scalar(@file) - 1 ] ) >=
309 #Bulding here a hierarchy to display files by level.
310 push @{ $hashlevel{$level} },
311 { "fwkname" => $file[ scalar(@file) - 1 ], "error" => $error };
314 #systempreference contains an ending |
315 chop $systempreference;
317 map { push @list, { "level" => $_, "fwklist" => $hashlevel{$_} } }
320 for my $each_language (@$all_languages) {
322 # warn "CODE".$each_language->{'language_code'};
323 # warn "LANG:".$lang;
324 if ( $lang eq $each_language->{'language_code'} ) {
325 $fwk_language = $each_language->{language_locale_name};
330 "UPDATE systempreferences set value=\"$systempreference\" where variable='FrameworksLoaded'"
332 unless ( $updateflag == 1 ) {
334 "INSERT INTO systempreferences (value, variable, explanation, type) VALUES (\"$systempreference\",'FrameworksLoaded','Frameworks loaded through webinstaller','choice')";
335 my $rq = $dbh->prepare($string);
339 "fwklanguage" => $fwk_language,
342 $template->param( "$op" => 1 );
343 $dbh->do('SET FOREIGN_KEY_CHECKS=1');
345 elsif ( $op && $op eq 'selectframeworks' ) {
348 # 1ST install : show the user the sql datas he can insert in the database.
351 # (note that the term "selectframeworks is not correct. The user can select various files, not only frameworks)
354 #sql data for import are supposed to be located in installer/data/<language>/<level>
355 # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
356 # Where <level> is a category of requirement : required, recommended optional
357 # level should contain :
358 # SQL File for import With a readable name.
359 # txt File taht explains what this SQL File is meant for.
360 # Could be VERY useful to have A Big file for a kind of library.
361 # But could also be useful to have some Authorised values data set prepared here.
362 # Framework Selection is achieved through checking boxes.
363 my $langchoice = $query->param('fwklanguage');
364 $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
365 my $dir = C4::Context->config('intranetdir') . "/installer/data/";
366 opendir( MYDIR, $dir );
367 my @listdir = grep { !/^\.|CVS/ && -d "$dir/$_" } readdir(MYDIR);
369 my $frmwklangs = getFrameworkLanguages();
374 'dirname' => $_->{'language_code'},
375 'languagedescription' => $_->{'language_name'},
376 'checked' => ( $_->{'language_code'} eq $langchoice )
378 if ( $_->{'language_code'} );
380 $template->param( "languagelist" => \@languages );
383 C4::Context->config('intranetdir') . "/installer/data/$langchoice";
384 opendir( MYDIR, $dir ) || warn "no open $dir";
385 @listdir = grep { !/^\.|CVS/ && -d "$dir/$_" } readdir(MYDIR);
390 "SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'"
393 my ($frameworksloaded) = $request->fetchrow;
394 my %frameworksloaded;
396 foreach ( split( /\|/, $frameworksloaded ) ) {
397 $frameworksloaded{$_} = 1;
399 foreach my $requirelevel (@listdir) {
401 C4::Context->config('intranetdir')
402 . "/installer/data/$langchoice/$requirelevel";
403 opendir( MYDIR, $dir );
405 grep { !/^\.|CVS/ && -f "$dir/$_" && $_ =~ m/\.sql$/ }
411 my $name = substr( $_, 0, -4 );
412 open FILE, "< $dir/$name.txt";
414 $lines =~ s/\n|\r/<br \/>/g;
416 utf8::encode($lines) unless ( utf8::is_utf8($lines) );
420 'fwkfile' => "$dir/$_",
421 'fwkdescription' => $lines,
424 $frameworksloaded{$_}
425 || ( $requirelevel =~
426 /(mandatory|requi|oblig|necess)/i )
432 sort { $a->{'fwkname'} lt $b->{'fwkname'} } @frameworklist;
434 # $cell{"mandatory"}=($requirelevel=~/(mandatory|requi|oblig|necess)/i);
435 $cell{"frameworks"} = \@fwks;
436 $cell{"label"} = ucfirst($requirelevel);
437 $cell{"code"} = lc($requirelevel);
438 push @levellist, \%cell;
440 $template->param( "levelloop" => \@levellist );
441 $template->param( "$op" => 1 );
443 elsif ( $op && $op eq 'updatestructure' ) {
445 #Do updatedatabase And report
447 C4::Context->config("intranetdir") . "/updater/updatedatabase";
449 my $string = qx|$execstring 2>&1|;
451 $string =~ s/\n|\r/<br \/>/g;
453 s/(DBD::mysql.*? failed: .*? line [0-9]*.|=================.*?====================)/<font color=red>$1<\/font>/g;
454 $template->param( "updatereport" => $string );
456 $template->param( $op => 1 );
458 elsif ( $op && $op eq 'importdatastructure' ) {
461 # UPDATE (not 1st install) run updatedatabase
465 #Import data structure and show errors if any
466 #Uses DBI to read the file [MJR 2007-07-01]
467 my $dbh = DBI->connect(
468 "DBI:$info{dbms}:$info{dbname}:$info{hostname}"
469 . ( $info{port} ? ":$info{port}" : "" ),
470 $info{'user'}, $info{'password'}
472 open( INPUT, "<kohastructure.sql" );
473 my $file = do { local $/ = undef; <INPUT> };
474 my @commands = split( /;/, $file );
476 map { $dbh->do($_) } @commands;
479 "error" => $dbh->errstr,
486 #Check if there are enough tables.
487 # Paul has cleaned up tables so reduced the count
488 #I put it there because it implied a data import if condition was not satisfied.
489 my $dbh = DBI->connect(
490 "DBI:$info{dbms}:$info{dbname}:$info{hostname}"
491 . ( $info{port} ? ":$info{port}" : "" ),
492 $info{'user'}, $info{'password'}
494 my $rq = $dbh->prepare( "SHOW TABLES FROM " . $info{'dbname'} );
496 my $data = $rq->fetchall_arrayref( {} );
497 my $count = scalar(@$data);
499 # we don't have tables, propose DB import
502 $template->param( "count" => $count, "proposeimport" => 1 );
506 # we have tables, propose to select files to upload or updatedatabase
508 $template->param( "count" => $count, "default" => 1 );
510 # 1st part of step 3 : check if there is a databaseversion systempreference
511 # if there is, then we just need to upgrade
512 # if there is none, then we need to install the database
514 my $dbversion = C4::Context->preference('Version');
515 $dbversion =~ /(.*)\.(..)(..)(...)/;
516 $dbversion = "$1.$2.$3.$4";
517 if (C4::Context->preference('Version')) {
518 $template->param("upgrading" => 1,
519 "dbversion" => $dbversion,
520 "kohaversion" => C4::Context->KOHAVERSION,
530 # LANGUAGE SELECTION page by default
531 # using opendir + language Hash
533 my $langavail = getTranslatedLanguages();
536 foreach (@$langavail) {
539 'value' => $_->{'language_code'},
540 'description' => $_->{'language_name'}
542 if ( $_->{'language_code'} );
544 $template->param( languages => \@languages );
548 "SELECT * from systempreferences WHERE variable='Version'");
549 if ( $rq->execute ) {
550 my ($version) = $rq->fetchrow;
552 $query->redirect("install.pl?step=3");
557 output_html_with_http_headers $query, $cookie, $template->output;