Handle hostnames with upper-case letters
[webmin.git] / postgresql / import.cgi
1 #!/usr/local/bin/perl
2 # import.cgi
3 # Import data from a text file
4
5 require './postgresql-lib.pl';
6 &ReadParseMime();
7 &can_edit_db($in{'db'}) || &error($text{'dbase_ecannot'});
8 &error_setup($text{'import_err'});
9
10 if ($in{'mode'}) {
11         # From uploaded file
12         $in{'upload'} || &error($text{'import_eupload'});
13         $in{'upload_filename'} =~ /([^\/\\\s]+)$/ ||
14                 &error($text{'import_eupload'});
15         $file = &transname($1);
16         open(TEMP, ">$file");
17         print TEMP $in{'upload'};
18         close(TEMP);
19         $need_unlink = 1;
20         &ui_print_header(undef, $text{'import_title'}, "");
21         print "$text{'import_uploadout'}<p>\n";
22         }
23 else {
24         # From local file
25         -r $in{'file'} || &error($text{'import_efile'});
26         $file = $in{'file'};
27         &ui_print_header(undef, $text{'import_title'}, "");
28         print &text('import_fileout', "<tt>$in{'file'}</tt>"),"<p>\n";
29         }
30
31 if (!$in{'delete'}) {
32         $data = &execute_sql($in{'db'},
33                 "select * from ".&quote_table($in{'table'}));
34         foreach $r (@{$data->{'data'}}) {
35                 $done{join("/", @$r)}++;
36                 }
37         }
38
39 # Read the file
40 $skip = 0;
41 open(FILE, $file);
42 while(<FILE>) {
43         s/\r|\n//g;
44         next if (!/\S/);
45         local @row;
46         if ($in{'format'} == 0) {
47                 # Quoted and comma-separated
48                 s/\\\"/\0/g;
49                 while(/^,?"([^"]*)"(.*)/) {
50                         $field = $1;
51                         $_ = $2;
52                         $field =~ s/\0/"/g;
53                         push(@row, $field);
54                         }
55                 }
56         elsif ($in{'format'} == 1) {
57                 # Just comma-separated
58                 s/\\,/\0/g;
59                 @row = split(/,/, $_);
60                 foreach my $r (@row) {
61                         $r =~ s/\0/,/g;
62                         }
63                 }
64         elsif ($in{'format'} == 2) {
65                 # Tab separated
66                 @row = split(/\t/, $_);
67                 }
68         if (!@row) {
69                 print &text('import_erow', "<tt>$_</tt>"),"<p>\n";
70                 goto failed;
71                 }
72         if ($in{'ignore'} && $done{join("/", @row)}++) {
73                 # Skip duplicate
74                 $skip++;
75                 next;
76                 }
77         push(@rv, \@row);
78         }
79
80 # Empty the table, if requested
81 if ($in{'delete'}) {
82         &execute_sql_logged($in{'db'}, "delete from ".
83                                        &quote_table($in{'table'}));
84         }
85
86 # Add the rows
87 @str = &table_structure($in{'db'}, $in{'table'});
88 foreach $r (@rv) {
89         @sets = map { "?" } @str;
90         $cmd = "insert into ".&quote_table($in{'table'})." values (".
91                join(",", @sets).")";
92         &execute_sql_logged($in{'db'}, $cmd, @$r);
93         }
94 print &text('import_done', scalar(@rv), $skip),"<p>\n";
95
96 &webmin_log("import", undef, $in{'db'}, { 'mode' => $in{'mode'},
97                                           'file' => $in{'file'} });
98
99 failed:
100 unlink($file) if ($need_unlink);
101
102 &ui_print_footer("exec_form.cgi?db=$in{'db'}&mode=import", $text{'exec_return'},
103                  "edit_dbase.cgi?db=$in{'db'}", $text{'dbase_return'},
104                  "", $text{'index_return'});
105