Compact css tweak
[openemr.git] / contrib / util / undelete_from_log / convert_logcomments.pl
blob76f3e7f9c2cb82d69fc3b253045ada353ecc228a
1 #!/opt/local/bin/perl
3 #######################################################################
4 # Copyright (C) 2010 - Medical Information Integration, LLC
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This loads the FDA Orange Book data for drugs into the "drugs"
12 # table of OpenEMR. If a row already exists with the same drug name
13 # it is ignored, otherwise a new row is created with the drug
14 # trade name provided from the Orange Book data.
18 # Converts openemr log ouput containing deleted records and
19 # produces the necessary "insert into <table> values ... on duplicate key
20 # update... "
22 # the log table's comments contains all of the deleted records in
23 # "tablename: field1='value1' field2='value2'" form.
25 # there are embedded newlines from the original web forms. They are
26 # encoded to allow the split operation to work correctly, then restored.
28 # there is one mysql reserved word used as a column name in use that I
29 # have discovered. the word 'interval' must be enclosed in backticks in
30 # order to be accepted by mysql.
32 # Use like this:
33 # This procedure will restore all records deleted on a given date/time) that are in the log table
35 # First, you must look into the admin/logs screen and determine the date that the delete occured on.
37 # Adjust the time accordingly
39 # use the mysql command or similar tool to produce the restore file
40 # ========================================
41 # mysql <database>
43 # tee <outfile>;
44 # select distinct comments from log where event like 'delete' and date >='<date> 00:00:00' and date <= '<date> 23:59:59';
45 # ========================================
46 # exit mysql monitor
48 # Verify data is correct
50 # Run: perl convert_logcomments.pl < outfile > restore.sql
52 # verify insert statements
54 # To import data:
56 # mysql <database> < restore.sql
59 use strict;
61 # %deleted is a hash that holds the read in values from the file on the
62 # command line.
63 my %deleted;
65 # %tablefields is simply a hash that holds the tablenames and all of the
66 # fields found for each table. it makes it easier to construct the
67 # insert lines.
68 my %tablefields;
70 my @temp;
71 my $i;
72 my $cols;
73 my $x;
74 my $value;
75 my @values;
76 my $input;
77 my @records;
78 my @fields;
79 my $field;
80 my $table;
81 my $entry;
82 my $sql;
83 my $sqlpost;
86 # engage slurp mode aka pull the entire file into the $input variable
88 local $/;
89 $input=<>;
93 # encode embedded newlines (they are of the form "0x0d 0x0a"
94 # without spaces) these have been entered in at the web browser and must
95 # be preserved as-is.
97 $input =~s/\r\n/jasonnewline/g;
99 # Double backslashes need to be relegated to single backslashes
100 # leaning toothpick syndrome
101 $input=~s/\\\\/\\/g;
104 # fix reserved word issues
105 $input=~s/(interval)/`$1`/g;
107 @records = split /\n/, $input;
110 foreach (@records)
113 # these next lines are skipping over the mysql monitor formatting
114 next if ( m/^mysql/i );
115 next if ( m/^\+/ );
116 next if ( m/\|\s+comments\s+\|\s*$/ );
117 next if ( m/^\s*$/ );
118 next if ( m/^\d+\s+rows in/ );
119 s/^\|\s*//;
120 s/\s+\|\s*$//;
122 # split on the FIRST colon in the line.
123 ($table,$entry) = split /:\s*/,$_,2;
125 $i=$#{ $deleted{$table}}+1;
127 # nuke extra white space between quotes
128 $entry =~ s/'(\s+)'/''/g;
129 #debugging output
130 if ( 0 )
132 print "tablename: $table\n";
133 print "$table entry #$i\n";
134 print "\t\tentry: $entry\n\n";
137 { # for a moment, override input record separator and chomp:
138 # lopping off the last single quote, if present.
139 # otherwise, this last quote screws up the output
140 local $/=q/'/;
141 chomp $entry;
144 #split the line on single quotes followed by a space
145 @temp = split /' /, $entry;
146 foreach (@temp)
148 # $field is the table's field name
149 # $value is the value.
150 ($field,$value) = split /=/;
151 $value .= "'";
152 #un-encode embedded newlines
153 $value =~ s/jasonnewline/\r\n/g;
154 #debugging
155 if (0)
157 print "\t$field = $value\n";
159 $deleted{$table}[$i]{$field}=$value;
160 $tablefields{$table}{$field} = 1;
166 #normalized data
167 foreach $table (keys %deleted)
169 #get the fields that should be present
170 @fields=keys %{ $tablefields{$table} };
171 for $i (0 .. $#{ $deleted{$table} } )
173 foreach $entry (@fields)
175 if (!exists ( $deleted{$table}[$i]{$entry} ) )
177 $deleted{$table}[$i]{$entry} = "";
184 # now to display properly:
186 foreach $table (keys %deleted)
188 # This is the beginning of the statement
189 $sql = "insert into $table ( ";
190 @fields=keys %{ $tablefields{$table} };
191 $sql .= join ",", @fields;
192 # chop $sql;
193 $sql .= ") values \n";
195 # and this is the middle
197 for $i (0 .. $#{$deleted{$table}} )
199 $values[$i]="(";
200 foreach (@fields)
202 $x=$deleted{$table}[$i]{$_};
203 if ( $x=~m/^\s*$/ )
205 $values[$i] .= qq/'',/;
207 else
209 $values[$i] .= $deleted{$table}[$i]{$_} . ",";
212 chop $values[$i];
213 $values[$i] .= "),\n";
217 #this is the end of the statement
218 # don't forget the "on duplicate key replace" part
219 $sqlpost ="on duplicate key update ";
220 # this composes the rest of $sqlpost
221 foreach (@fields)
224 $sqlpost .= "$_=values($_), ";
226 chop $sqlpost;
227 chop $sqlpost;
228 $sqlpost .=";\n";
230 #now put them together: beginning, middle, end
231 print "$sql";
232 $x="";
233 for $i (0 .. $#values)
235 $x.= $values[$i] ;
238 $x =~s/,(\s)$/$1/;
239 print $x;
240 @values="";
241 print "$sqlpost";
242 print "\n";