bug 2258 - do not duplicate embedded items
[koha.git] / installer / install.pl
blob088d0f7681c8e4eb5b5af1f332ffdd05f5f4ed12
1 #!/usr/bin/perl -w # please develop with -w
3 use diagnostics;
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;
12 use strict; # please develop with the strict pragma
14 use CGI;
16 my $query = new CGI;
17 my $step = $query->param('step');
19 my $language = $query->param('language');
20 my ( $template, $loggedinuser, $cookie );
22 my $all_languages = getAllLanguages();
24 if ( defined($language) ) {
25 setlanguagecookie( $query, $language, "install.pl?step=1" );
27 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
29 template_name => "installer/step" . ( $step ? $step : 1 ) . ".tmpl",
30 query => $query,
31 type => "intranet",
32 authnotrequired => 0,
33 debug => 1,
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'}
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;
61 unless ( $] >= 5.006001 ) { # Bug 179
62 $template->param( "problems" => 1, "perlversion" => 1 );
63 $problem = 1;
66 # We could here use a special find
67 my @missing = ();
68 unless ( eval { require ZOOM } ) {
69 push @missing, { name => "ZOOM" };
71 unless ( eval { require YAML::Syck } ) {
72 push @missing, { name => "YAML::Syck" };
74 unless ( eval { require LWP::Simple } ) {
75 push @missing, { name => "LWP::Simple" };
77 unless ( eval { require XML::Simple } ) {
78 push @missing, { name => "XML::Simple" };
80 unless ( eval { require MARC::File::XML } ) {
81 push @missing, { name => "MARC::File::XML" };
83 unless ( eval { require MARC::File::USMARC } ) {
84 push @missing, { name => "MARC::File::USMARC" };
86 unless ( eval { require DBI } ) {
87 push @missing, { name => "DBI" };
89 unless ( eval { require Date::Manip } ) {
90 push @missing, { name => "Date::Manip" };
92 unless ( eval { require DBD::mysql } ) {
93 push @missing, { name => "DBD::mysql" };
95 unless ( eval { require HTML::Template::Pro } ) {
96 push @missing, { name => "HTML::Template::Pro" };
98 unless ( eval { require Date::Calc } ) {
99 push @missing, { name => "Date::Calc" };
101 unless ( eval { require Digest::MD5 } ) {
102 push @missing, { name => "Digest::MD5" };
104 unless ( eval { require MARC::Record } ) {
105 push @missing, { name => "MARC::Record" };
107 unless ( eval { require Mail::Sendmail } ) {
108 push @missing, { name => "Mail::Sendmail", usagemail => 1 };
110 unless ( eval { require List::MoreUtils } ) {
111 push @missing, { name => "List::MoreUtils" };
113 unless ( eval { require XML::RSS } ) {
114 push @missing, { name => "XML::RSS" };
116 unless ( eval { require CGI::Carp } ) {
117 push @missing, { name => "CGI::Carp" };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
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 };
163 $template->param( missings => \@missing ) if ( scalar(@missing) > 0 );
164 $template->param( 'checkmodule' => 1 )
165 unless ( scalar(@missing) && $problem );
168 elsif ( $step && $step == 2 ) {
170 #STEP 2 Check Database connection and access
172 $template->param(%info);
173 my $checkdb = $query->param("checkdb");
174 $template->param( 'dbconnection' => $checkdb );
175 if ($checkdb) {
176 if ($dbh) {
178 # Can connect to the mysql
179 $template->param( "checkdatabaseaccess" => 1 );
180 if ( $info{dbms} eq "mysql" ) {
182 #Check if database created
183 my $rv = $dbh->do("SHOW DATABASES LIKE \'$info{dbname}\'");
184 if ( $rv == 1 ) {
185 $template->param( 'checkdatabasecreated' => 1 );
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 ) )
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 )
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 ) )
230 $template->param( "checkgrantaccess" => $grantaccess );
231 } # End mysql connect check...
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 );
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 );
247 } # End Pg connect check...
249 else {
250 $template->param( "error" => DBI::err, "message" => DBI::errstr );
254 elsif ( $step && $step == 3 ) {
257 # STEP 3 : database setup
260 my $op = $query->param('op');
261 if ( $op && $op eq 'finished' ) {
263 # we have finished, just redirect to mainpage.
265 print $query->redirect("/cgi-bin/koha/mainpage.pl");
266 exit 1;
268 elsif ( $op && $op eq 'finish' ) {
269 $installer->set_version_syspref();
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 );
277 elsif ( $op && $op eq 'SetIndexingEngine' ) {
278 $installer->set_indexing_engine($query->param('NoZebra'));
279 $template->param( "$op" => 1 );
281 elsif ( $op && $op eq 'addframeworks' ) {
283 # 1ST install, 3rd sub-step : insert the SQL files the user has selected
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
291 $template->param( "$op" => 1 );
293 elsif ( $op && $op eq 'selectframeworks' ) {
296 # 1ST install, 2nd sub-step : show the user the sql datas he can insert in the database.
299 # (note that the term "selectframeworks is not correct. The user can select various files, not only frameworks)
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 taht 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);
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));
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 );
330 elsif ( $op && $op eq 'choosemarc' ) {
333 # 1ST install, 2nd sub-step : show the user the marcflavour available.
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";
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;
374 $template->param( "flavourloop" => \@flavourlist );
375 $template->param( "$op" => 1 );
377 elsif ( $op && $op eq 'importdatastructure' ) {
380 # 1st install, 1st "sub-step" : import kohastructure
383 my $error = $installer->load_db_schema();
384 $template->param(
385 "error" => $error,
386 "$op" => 1,
389 elsif ( $op && $op eq 'updatestructure' ) {
391 # Not 1st install, the only sub-step : update database
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 );
404 $template->param( $op => 1 );
406 else {
408 # check wether it's a 1st install or an update
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'}
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);
427 # we don't have tables, propose DB import
429 if ( $count < 70 ) {
430 $template->param( "count" => $count, "proposeimport" => 1 );
432 else {
434 # we have tables, propose to select files to upload or updatedatabase
436 $template->param( "count" => $count, "default" => 1 );
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
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,
453 $dbh->disconnect;
456 else {
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;
475 output_html_with_http_headers $query, $cookie, $template->output;