updatedatabase changes to reflect C4::Matcher serialization tables
[koha.git] / installer / install.pl
1 #!/usr/bin/perl -w # please develop with -w
2
3 #use diagnostics;
4
5 # use Install;
6 use InstallAuth;
7 use C4::Context;
8 use C4::Output;
9 use C4::Languages;
10
11 use strict;    # please develop with the strict pragma
12
13 use CGI;
14
15 my $query = new CGI;
16 my $step  = $query->param('step');
17
18 my $language = $query->param('language');
19 my ( $template, $loggedinuser, $cookie );
20
21 my $all_languages = getAllLanguages();
22
23 if ( defined($language) ) {
24     setlanguagecookie( $query, $language, "install.pl?step=1" );
25 }
26 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
27     {
28         template_name => "installer/step" . ( $step ? $step : 1 ) . ".tmpl",
29         query         => $query,
30         type          => "intranet",
31         authnotrequired => 0,
32         debug           => 1,
33     }
34 );
35
36 my %info;
37 $info{'dbname'} = C4::Context->config("database");
38 $info{'dbms'} =
39   (   C4::Context->config("db_scheme")
40     ? C4::Context->config("db_scheme")
41     : "mysql" );
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'}
50 );
51
52 if ( $step && $step == 1 ) {
53     #First Step
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 );
57     my $problem;
58
59     unless ( $] >= 5.006001 ) {    # Bug 179
60         $template->param( "problems" => 1, "perlversion" => 1 );
61         $problem = 1;
62     }
63
64     # We could here use a special find
65     my @missing = ();
66     unless ( eval { require ZOOM } ) {
67         push @missing, { name => "ZOOM" };
68     }
69     unless ( eval { require LWP::Simple } ) {
70         push @missing, { name => "LWP::Simple" };
71     }
72     unless ( eval { require XML::Simple } ) {
73         push @missing, { name => "XML::Simple" };
74     }
75     unless ( eval { require MARC::File::XML } ) {
76         push @missing, { name => "MARC::File::XML" };
77     }
78     unless ( eval { require MARC::File::USMARC } ) {
79         push @missing, { name => "MARC::File::USMARC" };
80     }
81     unless ( eval { require DBI } ) {
82         push @missing, { name => "DBI" };
83     }
84     unless ( eval { require Date::Manip } ) {
85         push @missing, { name => "Date::Manip" };
86     }
87     unless ( eval { require DBD::mysql } ) {
88         push @missing, { name => "DBD::mysql" };
89     }
90     unless ( eval { require HTML::Template } ) {
91         push @missing, { name => "HTML::Template::Pro" };
92     }
93     unless ( eval { require HTML::Template } ) {
94         push @missing, { name => "Date::Calc" };
95     }
96     unless ( eval { require Digest::MD5 } ) {
97         push @missing, { name => "Digest::MD5" };
98     }
99     unless ( eval { require MARC::Record } ) {
100         push @missing, { name => "MARC::Record" };
101     }
102     unless ( eval { require Mail::Sendmail } ) {
103         push @missing, { name => "Mail::Sendmail", usagemail => 1 };
104     }
105     unless ( eval { require List::MoreUtils } ) {
106         push @missing, { name => "List::MoreUtils" };
107     }
108     unless ( eval { require XML::RSS } ) {
109         push @missing, { name => "XML::RSS" };
110     }
111     unless ( eval { require CGI::Carp } ) {
112         push @missing, { name => "CGI::Carp" };
113     }
114
115
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 };
120         }
121     }
122     unless ( eval { require GD::Barcorde } ) {
123         if ( $#missing >= 0 ) {   # only when $#missing >= 0 so this isn't fatal
124             push @missing,
125               { name => "GD::Barcode", usagebarcode => 1, usagespine => 1 };
126         }
127     }
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 };
131         }
132     }
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 };
136         }
137     }
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 };
141         }
142     }
143     unless ( eval { require Net::LDAP } ) {
144         if ( $#missing >= 0 ) {   # only when $#missing >= 0 so this isn't fatal
145             push @missing, { name => "Algorithm::CheckDigits", usagebarcode => 1 };
146         }
147     }
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 };
151         }
152     }
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 };
156         }
157     }
158     $template->param( missings => \@missing ) if ( scalar(@missing) > 0 );
159     $template->param( 'checkmodule' => 1 )
160       unless ( scalar(@missing) && $problem );
161
162 }
163 elsif ( $step && $step == 2 ) {
164 #
165 #STEP 2 Check Database connection and access
166 #
167     $template->param(%info);
168     my $checkdb = $query->param("checkdb");
169     $template->param( 'dbconnection' => $checkdb );
170     if ($checkdb) {
171         if ($dbh) {
172
173             # Can connect to the mysql
174             $template->param( "checkdatabaseaccess" => 1 );
175             if ( $info{dbms} eq "mysql" ) {
176
177                 #Check if database created
178                 my $rv = $dbh->do("SHOW DATABASES LIKE \'$info{dbname}\'");
179                 if ( $rv == 1 ) {
180                     $template->param( 'checkdatabasecreated' => 1 );
181                 }
182
183                 #Check if user have all necessary grants on this database.
184                 my $rq =
185                   $dbh->prepare(
186                     "SHOW GRANTS FOR \'$info{user}\'\@'$info{hostname}'");
187                 $rq->execute;
188                 my $grantaccess;
189                 while ( my ($line) = $rq->fetchrow ) {
190                     my $dbname = $info{dbname};
191                     if ( $line =~ m/$dbname/ || index( $line, '*.*' ) > 0 ) {
192                         $grantaccess = 1
193                           if (
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 ) )
201                           );
202                     }
203                 }
204                 unless ($grantaccess) {
205                     $rq =
206                       $dbh->prepare("SHOW GRANTS FOR \'$info{user}\'\@'\%'");
207                     $rq->execute;
208                     while ( my ($line) = $rq->fetchrow ) {
209                         my $dbname = $info{dbname};
210                         if ( $line =~ m/$dbname/ || index( $line, '*.*' ) > 0 )
211                         {
212                             $grantaccess = 1
213                               if (
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 ) )
221                               );
222                         }
223                     }
224                 }
225                 $template->param( "checkgrantaccess" => $grantaccess );
226             }   # End mysql connect check...
227             
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}\';" );
231                 if ( $rv == 1 ) {
232                         $template->param( 'checkdatabasecreated' => 1 );
233                 }
234
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}\';" );
239                 if ( $rq == 1 ) {
240                         $template->param( "checkgrantaccess" => 1 );
241                 }
242             }   # End Pg connect check...
243         }
244         else {
245             $template->param( "error" => DBI::err, "message" => DBI::errstr );
246         }
247     }
248 }
249 elsif ( $step && $step == 3 ) {
250 #
251 #
252 # STEP 3 : database setup
253 #
254
255     my $op = $query->param('op');
256     if ( $op && $op eq 'finished' ) {
257         #
258         # we have finished, just redirect to mainpage.
259         #
260         print $query->redirect("/cgi-bin/koha/mainpage.pl");
261         exit 1;
262     }
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);
271         } else {
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);
275         }
276
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 );
282     }
283     elsif ( $op && $op eq 'Nozebra' ) {
284         if ($query->param('Nozebra')) {
285             $dbh->do("UPDATE systempreferences SET value=1 WHERE variable='NoZebra'");
286         } else {
287             $dbh->do("UPDATE systempreferences SET value=0 WHERE variable='NoZebra'");
288         }
289         $template->param( "$op" => 1 );
290     }
291     elsif ( $op && $op eq 'addframeworks' ) {
292     #
293     # 1ST install, 3rd sub-step : insert the SQL files the user has selected
294     #
295
296         #Framework importing and reports
297         my $lang;
298         my %hashlevel;
299
300        # sort by filename -> prepend with numbers to specify order of insertion.
301         my @fnames = sort {
302             my @aa = split /\/|\\/, ($a);
303             my @bb = split /\/|\\/, ($b);
304             $aa[-1] lt $bb[-1]
305         } $query->param('framework');
306         if ( $info{dbms} eq 'mysql' ) { $dbh->do('SET FOREIGN_KEY_CHECKS=0'); }
307         elsif ( $info{dbms} eq 'Pg' ) { $dbh->do('SET CONSTRAINTS ALL DEFERRED;'); }
308         my $request =
309           $dbh->prepare(
310             "SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'"
311           );
312         $request->execute;
313         my ($systempreference) = $request->fetchrow;
314         foreach my $file (@fnames) {
315
316             #      warn $file;
317             undef $/;
318             my $error;
319             if ( $info{dbms} eq 'mysql' ) {
320                 my $strcmd = "mysql "
321                         . ( $info{hostname} ? " -h $info{hostname} " : "" )
322                         . ( $info{port}     ? " -P $info{port} "     : "" )
323                         . ( $info{user}     ? " -u $info{user} "     : "" )
324                         . ( $info{password} ? " -p$info{password}"   : "" )
325                         . " $info{dbname} ";
326                 $error = qx($strcmd < $file 2>&1 1>/dev/null);                  # We want to send stdout to null and return only stderr... -fbcit
327             }
328             elsif ( $info{dbms} eq 'Pg' ) { 
329                 my $strcmd = "psql "
330                         . ( $info{hostname} ? " -h $info{hostname} " : "" )
331                         . ( $info{port}     ? " -p $info{port} "     : "" )
332                         . ( $info{user}     ? " -U $info{user} "     : "" )
333 #                        . ( $info{password} ? " -W $info{password}"   : "" )
334                         . " $info{dbname} ";
335                 $error = qx($strcmd -f $file 2>&1 1>/dev/null);                 # ...even more so with psql...
336             }
337             my @file = split qr(\/|\\), $file;
338             $lang = $file[ scalar(@file) - 3 ] unless ($lang);
339             my $level = $file[ scalar(@file) - 2 ];
340             unless ($error) {
341                 $systempreference .= "$file[scalar(@file)-1]|"
342                   unless (
343                     index( $systempreference, $file[ scalar(@file) - 1 ] ) >=
344                     0 );
345             }
346
347             #Bulding here a hierarchy to display files by level.
348             push @{ $hashlevel{$level} },
349               { "fwkname" => $file[ scalar(@file) - 1 ], "error" => $error };
350         }
351
352         #systempreference contains an ending |
353         chop $systempreference;
354         my @list;
355         map { push @list, { "level" => $_, "fwklist" => $hashlevel{$_} } }
356           keys %hashlevel;
357         my $fwk_language;
358         for my $each_language (@$all_languages) {
359
360             #           warn "CODE".$each_language->{'language_code'};
361             #           warn "LANG:".$lang;
362             if ( $lang eq $each_language->{'language_code'} ) {
363                 $fwk_language = $each_language->{language_locale_name};
364             }
365         }
366         my $updateflag =
367           $dbh->do(
368             "UPDATE systempreferences set value=\"$systempreference\" where variable='FrameworksLoaded'"
369           );
370         unless ( $updateflag == 1 ) {
371             my $string =
372                 "INSERT INTO systempreferences (value, variable, explanation, type) VALUES (\"$systempreference\",'FrameworksLoaded','Frameworks loaded through webinstaller','choice')";
373             my $rq = $dbh->prepare($string);
374             $rq->execute;
375         }
376         $template->param(
377             "fwklanguage" => $fwk_language,
378             "list"        => \@list
379         );
380         $template->param( "$op" => 1 );
381         if ( $info{dbms} eq 'mysql' ) { $dbh->do('SET FOREIGN_KEY_CHECKS=1'); }
382         elsif ( $info{dbms} eq 'Pg' ) { $dbh->do('SET CONSTRAINTS ALL IMMEDIATE;'); }
383     }
384     elsif ( $op && $op eq 'selectframeworks' ) {
385         #
386         #
387         # 1ST install, 2nd sub-step : show the user the sql datas he can insert in the database.
388         #
389         #
390         # (note that the term "selectframeworks is not correct. The user can select various files, not only frameworks)
391         
392         #Framework Selection
393         #sql data for import are supposed to be located in installer/data/<language>/<level>
394         # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
395         # Where <level> is a category of requirement : required, recommended optional
396         # level should contain :
397         #   SQL File for import With a readable name.
398         #   txt File taht explains what this SQL File is meant for.
399         # Could be VERY useful to have A Big file for a kind of library.
400         # But could also be useful to have some Authorised values data set prepared here.
401         # Framework Selection is achieved through checking boxes.
402         my $langchoice = $query->param('fwklanguage');
403         $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
404         my $marcflavour = $query->param('marcflavour');
405         if ($marcflavour){    
406           my $request =
407             $dbh->prepare(
408               "INSERT IGNORE INTO `systempreferences` (variable,value,explanation,options,type) VALUES('marcflavour','$marcflavour','Define global MARC flavor (MARC21 or UNIMARC) used for character encoding','MARC21|UNIMARC','Choice');"
409             );     
410           $request->execute;
411         };    
412         $marcflavour = C4::Context->preference('marcflavour') unless ($marcflavour);
413         #Insert into database the selected marcflavour
414     
415         undef $/;
416         my $dir =
417           C4::Context->config('intranetdir') . "/installer/data/$info{dbms}/$langchoice/marcflavour/".lc($marcflavour);
418         opendir( MYDIR, $dir ) || warn "no open $dir";
419         my @listdir = sort grep { !/^\.|marcflavour/ && -d "$dir/$_" } readdir(MYDIR);
420         closedir MYDIR;
421                   
422         my @fwklist;
423         my $request =
424           $dbh->prepare(
425             "SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'"
426           );
427         $request->execute;
428         my ($frameworksloaded) = $request->fetchrow;
429         my %frameworksloaded;
430         foreach ( split( /\|/, $frameworksloaded ) ) {
431             $frameworksloaded{$_} = 1;
432         }
433         
434         foreach my $requirelevel (@listdir) {
435             opendir( MYDIR, "$dir/$requirelevel" );
436             my @listname =
437               grep { !/^\./ && -f "$dir/$requirelevel/$_" && $_ =~ m/\.sql$/ }
438               readdir(MYDIR);
439             closedir MYDIR;
440             my %cell;
441             my @frameworklist;
442             map {
443                 my $name = substr( $_, 0, -4 );
444                 open FILE, "< $dir/$requirelevel/$name.txt";
445                 my $lines = <FILE>;
446                 $lines =~ s/\n|\r/<br \/>/g;
447                 use utf8;
448                 utf8::encode($lines) unless ( utf8::is_utf8($lines) );
449                 push @frameworklist,
450                   {
451                     'fwkname'        => $name,
452                     'fwkfile'        => "$dir/$requirelevel/$_",
453                     'fwkdescription' => $lines,
454                     'checked'        => (
455                         (
456                             $frameworksloaded{$_}
457                               || ( $requirelevel =~
458                                 /(mandatory|requi|oblig|necess)/i )
459                         ) ? 1 : 0
460                     )
461                   };
462             } @listname;
463             my @fwks =
464               sort { $a->{'fwkname'} lt $b->{'fwkname'} } @frameworklist;
465
466 #             $cell{"mandatory"}=($requirelevel=~/(mandatory|requi|oblig|necess)/i);
467             $cell{"frameworks"} = \@fwks;
468             $cell{"label"}      = ucfirst($requirelevel);
469             $cell{"code"}       = lc($requirelevel);
470             push @fwklist, \%cell;
471         }
472         $template->param( "frameworksloop" => \@fwklist );
473         $template->param( "marcflavour" => ucfirst($marcflavour));
474         
475         $dir =
476           C4::Context->config('intranetdir') . "/installer/data/$info{dbms}/$langchoice";
477         opendir( MYDIR, $dir ) || warn "no open $dir";
478         @listdir = sort grep { !/^\.|marcflavour/ && -d "$dir/$_" } readdir(MYDIR);
479         closedir MYDIR;
480         my @levellist;
481         foreach my $requirelevel (@listdir) {
482             opendir( MYDIR, "$dir/$requirelevel" );
483             my @listname =
484               grep { !/^\./ && -f "$dir/$requirelevel/$_" && $_ =~ m/\.sql$/ }
485               readdir(MYDIR);
486             closedir MYDIR;
487             my %cell;
488             my @frameworklist;
489             map {
490                 my $name = substr( $_, 0, -4 );
491                 open FILE, "< $dir/$requirelevel/$name.txt";
492                 my $lines = <FILE>;
493                 $lines =~ s/\n|\r/<br \/>/g;
494                 use utf8;
495                 utf8::encode($lines) unless ( utf8::is_utf8($lines) );
496                 push @frameworklist,
497                   {
498                     'fwkname'        => $name,
499                     'fwkfile'        => "$dir/$requirelevel/$_",
500                     'fwkdescription' => $lines,
501                     'checked'        => (
502                         (
503                             $frameworksloaded{$_}
504                               || ( $requirelevel =~
505                                 /(mandatory|requi|oblig|necess)/i )
506                         ) ? 1 : 0
507                     )
508                   };
509             } @listname;
510             my @fwks =
511               sort { $a->{'fwkname'} lt $b->{'fwkname'} } @frameworklist;
512
513 #             $cell{"mandatory"}=($requirelevel=~/(mandatory|requi|oblig|necess)/i);
514             $cell{"frameworks"} = \@fwks;
515             $cell{"label"}      = ucfirst($requirelevel);
516             $cell{"code"}       = lc($requirelevel);
517             push @levellist, \%cell;
518         }
519         $template->param( "levelloop" => \@levellist );
520         $template->param( "$op"       => 1 );
521     }
522     elsif ( $op && $op eq 'choosemarc' ) {
523         #
524         #
525         # 1ST install, 2nd sub-step : show the user the marcflavour available.
526         #
527         #
528         
529         #Choose Marc Flavour
530         #sql data are supposed to be located in installer/data/<dbms>/<language>/marcflavour/marcflavourname
531         # Where <dbms> is database type according to DBD syntax
532         # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
533         # Where <level> is a category of requirement : required, recommended optional
534         # level should contain :
535         #   SQL File for import With a readable name.
536         #   txt File taht explains what this SQL File is meant for.
537         # Could be VERY useful to have A Big file for a kind of library.
538         # But could also be useful to have some Authorised values data set prepared here.
539         # Marcflavour Selection is achieved through radiobuttons.
540         my $langchoice = $query->param('fwklanguage');
541         $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
542         my $dir =
543           C4::Context->config('intranetdir') . "/installer/data/$info{dbms}/$langchoice/marcflavour";
544         opendir( MYDIR, $dir ) || warn "no open $dir";
545         my @listdir = grep { !/^\./ && -d "$dir/$_" } readdir(MYDIR);
546         closedir MYDIR;
547         my $marcflavour=C4::Context->preference("marcflavour");    
548         my @flavourlist;
549         foreach my $marc (@listdir) {
550             my %cell=(    
551             "label"=> ucfirst($marc),
552             "code"=>uc($marc),
553             "checked"=>uc($marc) eq $marcflavour);      
554 #             $cell{"description"}= do { local $/ = undef; open INPUT "<$dir/$marc.txt"||"";<INPUT> };
555             push @flavourlist, \%cell;
556         }
557         $template->param( "flavourloop" => \@flavourlist );
558         $template->param( "$op"       => 1 );
559     }
560     elsif ( $op && $op eq 'importdatastructure' ) {
561         #
562         #
563         # 1st install, 1st "sub-step" : import kohastructure
564         #
565         #
566         my $datadir = C4::Context->config('intranetdir') . "/installer/data/$info{dbms}";
567         my $error;
568         if ( $info{dbms} eq 'mysql' ) {
569             my $strcmd = "mysql "
570                 . ( $info{hostname} ? " -h $info{hostname} " : "" )
571                 . ( $info{port}     ? " -P $info{port} "     : "" )
572                 . ( $info{user}     ? " -u $info{user} "     : "" )
573                 . ( $info{password} ? " -p$info{password}"   : "" )
574                 . " $info{dbname} ";
575             $error = qx($strcmd <$datadir/kohastructure.sql 2>&1 1>/dev/null);
576         }
577         elsif ( $info{dbms} eq 'Pg' ) { 
578             my $strcmd = "psql "
579                 . ( $info{hostname} ? " -h $info{hostname} " : "" )
580                 . ( $info{port}     ? " -p $info{port} "     : "" )
581                 . ( $info{user}     ? " -U $info{user} "     : "" )
582 #                . ( $info{password} ? " -W $info{password}"   : "" )           # psql will NOT accept a password, but prompts...
583                 . " $info{dbname} ";                                            # Therefore, be sure to run 'trust' on localhost in pg_hba.conf -fbcit
584             $error = qx($strcmd -f $datadir/kohastructure.sql 2>&1 1>/dev/null);# Be sure to set 'client_min_messages = error' in postgresql.conf
585                                                                                 # so that only true errors are returned to stderr or else the installer will
586                                                                                 # report the import a failure although it really succeded -fbcit
587         }
588         $template->param(
589             "error" => $error,
590             "$op"   => 1,
591         );
592     }
593     elsif ( $op && $op eq 'updatestructure' ) {
594         #
595         # Not 1st install, the only sub-step : update database
596         #
597         #Do updatedatabase And report
598         my $execstring =
599           C4::Context->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl";
600         undef $/;
601         my $string = qx($execstring 2>&1 1>/dev/null);                          # added '1>/dev/null' to return only stderr in $string. Needs testing here. -fbcit
602         if ($string) {
603             $string =~ s/\n|\r/<br \/>/g;
604             $string =~
605                 s/(DBD::mysql.*? failed: .*? line [0-9]*.|=================.*?====================)/<font color=red>$1<\/font>/g;
606             $template->param( "updatereport" => $string );
607         }
608         $template->param( $op => 1 );
609     }
610     else {
611         #
612         # check wether it's a 1st install or an update
613         #
614         #Check if there are enough tables.
615         # Paul has cleaned up tables so reduced the count
616         #I put it there because it implied a data import if condition was not satisfied.
617         my $dbh = DBI->connect(
618                 "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
619                 . ( $info{port} ? ";port=$info{port}" : "" ),
620                 $info{'user'}, $info{'password'}
621         );
622         my $rq;
623         if ( $info{dbms} eq 'mysql' ) { $rq = $dbh->prepare( "SHOW TABLES FROM " . $info{'dbname'} ); }
624         elsif ( $info{dbms} eq 'Pg' ) { $rq = $dbh->prepare( "SELECT *
625                                                                 FROM information_schema.tables
626                                                                 WHERE table_schema='public' and table_type='BASE TABLE';" ); }
627         $rq->execute;
628         my $data = $rq->fetchall_arrayref( {} );
629         my $count = scalar(@$data);
630         #
631         # we don't have tables, propose DB import
632         #
633         if ( $count < 70 ) {
634             $template->param( "count" => $count, "proposeimport" => 1 );
635         }
636         else {
637             #
638             # we have tables, propose to select files to upload or updatedatabase
639             #
640             $template->param( "count" => $count, "default" => 1 );
641             #
642             # 1st part of step 3 : check if there is a databaseversion systempreference
643             # if there is, then we just need to upgrade
644             # if there is none, then we need to install the database
645             #
646             if (C4::Context->preference('Version')) {
647                 my $dbversion = C4::Context->preference('Version');
648                 $dbversion =~ /(.*)\.(..)(..)(...)/;
649                 $dbversion = "$1.$2.$3.$4";
650                 $template->param("upgrading" => 1,
651                                 "dbversion" => $dbversion,
652                                 "kohaversion" => C4::Context->KOHAVERSION,
653                                 );
654             }
655         }
656
657         $dbh->disconnect;
658     }
659 }
660 else {
661
662     # LANGUAGE SELECTION page by default
663     # using opendir + language Hash
664
665     my $langavail = getTranslatedLanguages();
666
667     my @languages;
668     foreach (@$langavail) {
669         push @languages,
670           {
671             'value'       => $_->{'language_code'},
672             'description' => $_->{'language_name'}
673           }
674           if ( $_->{'language_code'} );
675     }
676     $template->param( languages => \@languages );
677     if ($dbh) {
678         my $rq =
679           $dbh->prepare(
680             "SELECT * from systempreferences WHERE variable='Version'");
681         if ( $rq->execute ) {
682             my ($version) = $rq->fetchrow;
683             if ($version) {
684                 $query->redirect("install.pl?step=3");
685             }
686         }
687     }
688 }
689 output_html_with_http_headers $query, $cookie, $template->output;