3 # Import data from a text file
5 require './postgresql-lib.pl';
7 &can_edit_db($in{'db'}) || &error($text{'dbase_ecannot'});
8 &error_setup($text{'import_err'});
12 $in{'upload'} || &error($text{'import_eupload'});
13 $in{'upload_filename'} =~ /([^\/\\\s]+)$/ ||
14 &error($text{'import_eupload'});
15 $file = &transname($1);
17 print TEMP $in{'upload'};
20 &ui_print_header(undef, $text{'import_title'}, "");
21 print "$text{'import_uploadout'}<p>\n";
25 -r $in{'file'} || &error($text{'import_efile'});
27 &ui_print_header(undef, $text{'import_title'}, "");
28 print &text('import_fileout', "<tt>$in{'file'}</tt>"),"<p>\n";
32 $data = &execute_sql($in{'db'},
33 "select * from "."e_table($in{'table'}));
34 foreach $r (@{$data->{'data'}}) {
35 $done{join("/", @$r)}++;
46 if ($in{'format'} == 0) {
47 # Quoted and comma-separated
49 while(/^,?"([^"]*)"(.*)/) {
56 elsif ($in{'format'} == 1) {
57 # Just comma-separated
59 @row = split(/,/, $_);
60 foreach my $r (@row) {
64 elsif ($in{'format'} == 2) {
66 @row = split(/\t/, $_);
69 print &text('import_erow', "<tt>$_</tt>"),"<p>\n";
72 if ($in{'ignore'} && $done{join("/", @row)}++) {
80 # Empty the table, if requested
82 &execute_sql_logged($in{'db'}, "delete from ".
83 "e_table($in{'table'}));
87 @str = &table_structure($in{'db'}, $in{'table'});
89 @sets = map { "?" } @str;
90 $cmd = "insert into "."e_table($in{'table'})." values (".
92 &execute_sql_logged($in{'db'}, $cmd, @$r);
94 print &text('import_done', scalar(@rv), $skip),"<p>\n";
96 &webmin_log("import", undef, $in{'db'}, { 'mode' => $in{'mode'},
97 'file' => $in{'file'} });
100 unlink($file) if ($need_unlink);
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'});