Bug: Importing sample data removes marcflavour variable (ru-RU)
[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 qw(getAllLanguages getTranslatedLanguages);
10 use C4::Installer;
11
12 use strict;    # please develop with the strict pragma
13
14 use CGI;
15
16 my $query = new CGI;
17 my $step  = $query->param('step');
18
19 my $language = $query->param('language');
20 my ( $template, $loggedinuser, $cookie );
21
22 my $all_languages = getAllLanguages();
23
24 if ( defined($language) ) {
25     setlanguagecookie( $query, $language, "install.pl?step=1" );
26 }
27 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
28     {
29         template_name => "installer/step" . ( $step ? $step : 1 ) . ".tmpl",
30         query         => $query,
31         type          => "intranet",
32         authnotrequired => 0,
33         debug           => 1,
34     }
35 );
36
37 my $installer = C4::Installer->new();
38 my %info;
39 $info{'dbname'} = C4::Context->config("database");
40 $info{'dbms'} =
41   (   C4::Context->config("db_scheme")
42     ? C4::Context->config("db_scheme")
43     : "mysql" );
44 $info{'hostname'} = C4::Context->config("hostname");
45 $info{'port'}     = C4::Context->config("port");
46 $info{'user'}     = C4::Context->config("user");
47 $info{'password'} = C4::Context->config("pass");
48 my $dbh = DBI->connect(
49     "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
50       . ( $info{port} ? ";port=$info{port}" : "" ),
51     $info{'user'}, $info{'password'}
52 );
53
54 if ( $step && $step == 1 ) {
55     #First Step
56     #Checking ALL perl Modules and services needed are installed.
57     #Whenever there is an error, adding a report to the page
58     $template->param( language => 1 );
59     my $problem;
60
61     unless ( $] >= 5.006001 ) {    # Bug 179
62         $template->param( "problems" => 1, "perlversion" => 1 );
63         $problem = 1;
64     }
65
66     # We could here use a special find
67     my @missing = ();
68     unless ( eval { require ZOOM } ) {
69         push @missing, { name => "ZOOM" };
70     }
71     unless ( eval { require YAML::Syck } ) {
72         push @missing, { name => "YAML::Syck" };
73     }
74     unless ( eval { require LWP::Simple } ) {
75         push @missing, { name => "LWP::Simple" };
76     }
77     unless ( eval { require XML::Simple } ) {
78         push @missing, { name => "XML::Simple" };
79     }
80     unless ( eval { require MARC::File::XML } ) {
81         push @missing, { name => "MARC::File::XML" };
82     }
83     unless ( eval { require MARC::File::USMARC } ) {
84         push @missing, { name => "MARC::File::USMARC" };
85     }
86     unless ( eval { require DBI } ) {
87         push @missing, { name => "DBI" };
88     }
89     unless ( eval { require Date::Manip } ) {
90         push @missing, { name => "Date::Manip" };
91     }
92     unless ( eval { require DBD::mysql } ) {
93         push @missing, { name => "DBD::mysql" };
94     }
95     unless ( eval { require HTML::Template::Pro } ) {
96         push @missing, { name => "HTML::Template::Pro" };
97     }
98     unless ( eval { require Date::Calc } ) {
99         push @missing, { name => "Date::Calc" };
100     }
101     unless ( eval { require Digest::MD5 } ) {
102         push @missing, { name => "Digest::MD5" };
103     }
104     unless ( eval { require MARC::Record } ) {
105         push @missing, { name => "MARC::Record" };
106     }
107     unless ( eval { require Mail::Sendmail } ) {
108         push @missing, { name => "Mail::Sendmail", usagemail => 1 };
109     }
110     unless ( eval { require List::MoreUtils } ) {
111         push @missing, { name => "List::MoreUtils" };
112     }
113     unless ( eval { require XML::RSS } ) {
114         push @missing, { name => "XML::RSS" };
115     }
116     unless ( eval { require CGI::Carp } ) {
117         push @missing, { name => "CGI::Carp" };
118     }
119
120
121 # The following modules are not mandatory, depends on how the library want to use Koha
122     unless ( eval { require PDF::API2 } ) {
123         if ( $#missing >= 0 ) {   # only when $#missing >= 0 so this isn't fatal
124             push @missing, { name => "PDF::API2", usagebarcode => 1 };
125         }
126     }
127     unless ( eval { require GD::Barcorde } ) {
128         if ( $#missing >= 0 ) {   # only when $#missing >= 0 so this isn't fatal
129             push @missing,
130               { name => "GD::Barcode", usagebarcode => 1, usagespine => 1 };
131         }
132     }
133     unless ( eval { require Data::Random } ) {
134         if ( $#missing >= 0 ) {   # only when $#missing >= 0 so this isn't fatal
135             push @missing, { name => "Data::Random", usagebarcode => 1 };
136         }
137     }
138     unless ( eval { require PDF::Reuse::Barcode } ) {
139         if ( $#missing >= 0 ) {   # only when $#missing >= 0 so this isn't fatal
140             push @missing, { name => "PDF::Reuse::Barcode", usagebarcode => 1 };
141         }
142     }
143     unless ( eval { require PDF::Report } ) {
144         if ( $#missing >= 0 ) {   # only when $#missing >= 0 so this isn't fatal
145             push @missing, { name => "PDF::Report", usagebarcode => 1 };
146         }
147     }
148     unless ( eval { require Algorithm::CheckDigits } ) {
149         if ( $#missing >= 0 ) {   # only when $#missing >= 0 so this isn't fatal
150             push @missing, { name => "Algorithm::CheckDigits", usagebarcode => 1 };
151         }
152     }
153     unless ( eval { require GD::Barcode::UPCE } ) {
154         if ( $#missing >= 0 ) {   # only when $#missing >= 0 so this isn't fatal
155             push @missing, { name => "GD::Barcode::UPCE", usagepine => 1 };
156         }
157     }
158     unless ( eval { require Net::LDAP } ) {
159         if ( $#missing >= 0 ) {   # only when $#missing >= 0 so this isn't fatal
160             push @missing, { name => "Net::LDAP", usageLDAP => 1 };
161         }
162     }
163     $template->param( missings => \@missing ) if ( scalar(@missing) > 0 );
164     $template->param( 'checkmodule' => 1 )
165       unless ( scalar(@missing) && $problem );
166
167 }
168 elsif ( $step && $step == 2 ) {
169 #
170 #STEP 2 Check Database connection and access
171 #
172     $template->param(%info);
173     my $checkdb = $query->param("checkdb");
174     $template->param( 'dbconnection' => $checkdb );
175     if ($checkdb) {
176         if ($dbh) {
177
178             # Can connect to the mysql
179             $template->param( "checkdatabaseaccess" => 1 );
180             if ( $info{dbms} eq "mysql" ) {
181
182                 #Check if database created
183                 my $rv = $dbh->do("SHOW DATABASES LIKE \'$info{dbname}\'");
184                 if ( $rv == 1 ) {
185                     $template->param( 'checkdatabasecreated' => 1 );
186                 }
187
188                 #Check if user have all necessary grants on this database.
189                 my $rq =
190                   $dbh->prepare(
191                     "SHOW GRANTS FOR \'$info{user}\'\@'$info{hostname}'");
192                 $rq->execute;
193                 my $grantaccess;
194                 while ( my ($line) = $rq->fetchrow ) {
195                     my $dbname = $info{dbname};
196                     if ( $line =~ m/^GRANT (.*?) ON `$dbname`\.\*/ || index( $line, '*.*' ) > 0 ) {
197                         $grantaccess = 1
198                           if (
199                             index( $line, 'ALL PRIVILEGES' ) > 0
200                             || (   ( index( $line, 'SELECT' ) > 0 )
201                                 && ( index( $line, 'INSERT' ) > 0 )
202                                 && ( index( $line, 'UPDATE' ) > 0 )
203                                 && ( index( $line, 'DELETE' ) > 0 )
204                                 && ( index( $line, 'CREATE' ) > 0 )
205                                 && ( index( $line, 'DROP' ) > 0 ) )
206                           );
207                     }
208                 }
209                 unless ($grantaccess) {
210                     $rq =
211                       $dbh->prepare("SHOW GRANTS FOR \'$info{user}\'\@'\%'");
212                     $rq->execute;
213                     while ( my ($line) = $rq->fetchrow ) {
214                         my $dbname = $info{dbname};
215                         if ( $line =~ m/$dbname/ || index( $line, '*.*' ) > 0 )
216                         {
217                             $grantaccess = 1
218                               if (
219                                 index( $line, 'ALL PRIVILEGES' ) > 0
220                                 || (   ( index( $line, 'SELECT' ) > 0 )
221                                     && ( index( $line, 'INSERT' ) > 0 )
222                                     && ( index( $line, 'UPDATE' ) > 0 )
223                                     && ( index( $line, 'DELETE' ) > 0 )
224                                     && ( index( $line, 'CREATE' ) > 0 )
225                                     && ( index( $line, 'DROP' ) > 0 ) )
226                               );
227                         }
228                     }
229                 }
230                 $template->param( "checkgrantaccess" => $grantaccess );
231             }   # End mysql connect check...
232             
233             elsif ( $info{dbms} eq "Pg" ) {
234                 # Check if database has been created...
235                 my $rv = $dbh->do( "SELECT * FROM pg_catalog.pg_database WHERE datname = \'$info{dbname}\';" );
236                 if ( $rv == 1 ) {
237                         $template->param( 'checkdatabasecreated' => 1 );
238                 }
239
240                 # Check if user has all necessary grants on this database...
241                 my $rq = $dbh->do( "SELECT u.usesuper
242                                     FROM pg_catalog.pg_user as u
243                                     WHERE u.usename = \'$info{user}\';" );
244                 if ( $rq == 1 ) {
245                         $template->param( "checkgrantaccess" => 1 );
246                 }
247             }   # End Pg connect check...
248         }
249         else {
250             $template->param( "error" => DBI::err, "message" => DBI::errstr );
251         }
252     }
253 }
254 elsif ( $step && $step == 3 ) {
255 #
256 #
257 # STEP 3 : database setup
258 #
259
260     my $op = $query->param('op');
261     if ( $op && $op eq 'finished' ) {
262         #
263         # we have finished, just redirect to mainpage.
264         #
265         print $query->redirect("/cgi-bin/koha/mainpage.pl");
266         exit 1;
267     }
268     elsif ( $op && $op eq 'finish' ) {
269         $installer->set_version_syspref();
270
271         # Installation is finished.
272         # We just deny anybody access to install
273         # And we redirect people to mainpage.
274         # The installer will have to relogin since we do not pass cookie to redirection.
275         $template->param( "$op" => 1 );
276     }
277     elsif ( $op && $op eq 'SetIndexingEngine' ) {
278         $installer->set_indexing_engine($query->param('NoZebra'));
279         $template->param( "$op" => 1 );
280     }
281     elsif ( $op && $op eq 'addframeworks' ) {
282     #
283     # 1ST install, 3rd sub-step : insert the SQL files the user has selected
284     #
285
286         my ($fwk_language, $list) = $installer->load_sql_in_order($all_languages, $query->param('framework'));
287         $template->param(
288             "fwklanguage" => $fwk_language,
289             "list"        => $list
290         );
291         $template->param( "$op" => 1 );
292     }
293     elsif ( $op && $op eq 'selectframeworks' ) {
294         #
295         #
296         # 1ST install, 2nd sub-step : show the user the sql datas he can insert in the database.
297         #
298         #
299         # (note that the term "selectframeworks is not correct. The user can select various files, not only frameworks)
300         
301         #Framework Selection
302         #sql data for import are supposed to be located in installer/data/<language>/<level>
303         # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
304         # Where <level> is a category of requirement : required, recommended optional
305         # level should contain :
306         #   SQL File for import With a readable name.
307         #   txt File that explains what this SQL File is meant for.
308         # Could be VERY useful to have A Big file for a kind of library.
309         # But could also be useful to have some Authorised values data set prepared here.
310         # Framework Selection is achieved through checking boxes.
311         my $langchoice = $query->param('fwklanguage');
312         $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
313         my $marcflavour = $query->param('marcflavour');
314         if ($marcflavour){
315             $installer->set_marcflavour_syspref($marcflavour);
316         };    
317         $marcflavour = C4::Context->preference('marcflavour') unless ($marcflavour);
318         #Insert into database the selected marcflavour
319         undef $/; 
320         my ($marc_defaulted_to_en, $fwklist) = $installer->marc_framework_sql_list($langchoice, $marcflavour);
321         $template->param('en_marc_frameworks' => $marc_defaulted_to_en);
322         $template->param( "frameworksloop" => $fwklist );
323         $template->param( "marcflavour" => ucfirst($marcflavour));
324        
325         my ($sample_defaulted_to_en, $levellist) = $installer->sample_data_sql_list($langchoice, $marcflavour);
326         $template->param( "en_sample_data" => $sample_defaulted_to_en);
327         $template->param( "levelloop" => $levellist );
328         $template->param( "$op"       => 1 );
329     }
330     elsif ( $op && $op eq 'choosemarc' ) {
331         #
332         #
333         # 1ST install, 2nd sub-step : show the user the marcflavour available.
334         #
335         #
336         
337         #Choose Marc Flavour
338         #sql data are supposed to be located in installer/data/<dbms>/<language>/marcflavour/marcflavourname
339         # Where <dbms> is database type according to DBD syntax
340         # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
341         # Where <level> is a category of requirement : required, recommended optional
342         # level should contain :
343         #   SQL File for import With a readable name.
344         #   txt File taht explains what this SQL File is meant for.
345         # Could be VERY useful to have A Big file for a kind of library.
346         # But could also be useful to have some Authorised values data set prepared here.
347         # Marcflavour Selection is achieved through radiobuttons.
348         my $langchoice = $query->param('fwklanguage');
349         $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
350         my $dir =
351           C4::Context->config('intranetdir') . "/installer/data/$info{dbms}/$langchoice/marcflavour";
352         unless (opendir( MYDIR, $dir )) {
353             if ($langchoice eq 'en') {
354                 warn "cannot open MARC frameworks directory $dir";
355             } else {
356                 # if no translated MARC framework is available,
357                 # default to English
358                 $dir = C4::Context->config('intranetdir') . "/installer/data/$info{dbms}/en/marcflavour";
359                 opendir(MYDIR, $dir) or warn "cannot open English MARC frameworks directory $dir";
360             }
361         }
362         my @listdir = grep { !/^\./ && -d "$dir/$_" } readdir(MYDIR);
363         closedir MYDIR;
364         my $marcflavour=C4::Context->preference("marcflavour");    
365         my @flavourlist;
366         foreach my $marc (@listdir) {
367             my %cell=(    
368             "label"=> ucfirst($marc),
369             "code"=>uc($marc),
370             "checked"=> defined($marcflavour) ? uc($marc) eq $marcflavour : 0);      
371 #             $cell{"description"}= do { local $/ = undef; open INPUT "<$dir/$marc.txt"||"";<INPUT> };
372             push @flavourlist, \%cell;
373         }
374         $template->param( "flavourloop" => \@flavourlist );
375         $template->param( "$op"       => 1 );
376     }
377     elsif ( $op && $op eq 'importdatastructure' ) {
378         #
379         #
380         # 1st install, 1st "sub-step" : import kohastructure
381         #
382         #
383         my $error = $installer->load_db_schema();
384         $template->param(
385             "error" => $error,
386             "$op"   => 1,
387         );
388     }
389     elsif ( $op && $op eq 'updatestructure' ) {
390         #
391         # Not 1st install, the only sub-step : update database
392         #
393         #Do updatedatabase And report
394         my $execstring =
395           C4::Context->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl";
396         undef $/;
397         my $string = qx($execstring 2>&1 1>/dev/null);                          # added '1>/dev/null' to return only stderr in $string. Needs testing here. -fbcit
398         if ($string) {
399             $string =~ s/\n|\r/<br \/>/g;
400             $string =~
401                 s/(DBD::mysql.*? failed: .*? line [0-9]*.|=================.*?====================)/<font color=red>$1<\/font>/g;
402             $template->param( "updatereport" => $string );
403         }
404         $template->param( $op => 1 );
405     }
406     else {
407         #
408         # check wether it's a 1st install or an update
409         #
410         #Check if there are enough tables.
411         # Paul has cleaned up tables so reduced the count
412         #I put it there because it implied a data import if condition was not satisfied.
413         my $dbh = DBI->connect(
414                 "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
415                 . ( $info{port} ? ";port=$info{port}" : "" ),
416                 $info{'user'}, $info{'password'}
417         );
418         my $rq;
419         if ( $info{dbms} eq 'mysql' ) { $rq = $dbh->prepare( "SHOW TABLES FROM " . $info{'dbname'} ); }
420         elsif ( $info{dbms} eq 'Pg' ) { $rq = $dbh->prepare( "SELECT *
421                                                                 FROM information_schema.tables
422                                                                 WHERE table_schema='public' and table_type='BASE TABLE';" ); }
423         $rq->execute;
424         my $data = $rq->fetchall_arrayref( {} );
425         my $count = scalar(@$data);
426         #
427         # we don't have tables, propose DB import
428         #
429         if ( $count < 70 ) {
430             $template->param( "count" => $count, "proposeimport" => 1 );
431         }
432         else {
433             #
434             # we have tables, propose to select files to upload or updatedatabase
435             #
436             $template->param( "count" => $count, "default" => 1 );
437             #
438             # 1st part of step 3 : check if there is a databaseversion systempreference
439             # if there is, then we just need to upgrade
440             # if there is none, then we need to install the database
441             #
442             if (C4::Context->preference('Version')) {
443                 my $dbversion = C4::Context->preference('Version');
444                 $dbversion =~ /(.*)\.(..)(..)(...)/;
445                 $dbversion = "$1.$2.$3.$4";
446                 $template->param("upgrading" => 1,
447                                 "dbversion" => $dbversion,
448                                 "kohaversion" => C4::Context->KOHAVERSION,
449                                 );
450             }
451         }
452
453         $dbh->disconnect;
454     }
455 }
456 else {
457
458     # LANGUAGE SELECTION page by default
459     # using opendir + language Hash
460     my $languages_loop = getTranslatedLanguages('intranet');
461     $template->param( installer_languages_loop => $languages_loop );
462     if ($dbh) {
463         my $rq =
464           $dbh->prepare(
465             "SELECT * from systempreferences WHERE variable='Version'");
466         if ( $rq->execute ) {
467             my ($version) = $rq->fetchrow;
468             if ($version) {
469                 $query->redirect("install.pl?step=3");
470                                 exit;
471             }
472         }
473     }
474 }
475 output_html_with_http_headers $query, $cookie, $template->output;