8
foreach my $php_file (@php_files) {
9
open (FILE, $php_file) or die $!;
10
read (FILE, my $php_code, -s $php_file) or die $!;
13
my @file_selects = $php_code =~ m{(SELECT.*?)'}sog;
14
foreach my $file_select (@file_selects) {
15
$file_select =~ s{\{db_prefix\}}{smf_}sgio;
16
my ($trial_select , $grammar_select) = ($file_select , $file_select);
18
$trial_select =~ s{(\{.*?\})}{1}sgio;
20
$grammar_select =~ s{\{([a-z_]*?):[a-z_]*?\}}{_\1}sgio;
21
$grammar_select =~ s{\{STEP_LOW\}}{_step_low}sgio;
22
$grammar_select =~ s{\{STEP_HIGH\}}{_step_high}sgio;
24
push @{$queries{'select'}} , [ $trial_select , $grammar_select ];
27
my @file_updates = $php_code =~ m{(UPDATE.*?)'}sog;
28
foreach my $file_update (@file_updates) {
29
$file_update =~ s{\{db_prefix\}}{smf_}sgio;
30
my ($trial_update , $grammar_update) = ($file_update, $file_update);
32
$trial_update =~ s{(\{.*?\})}{1}sgio;
33
$grammar_update =~ s{\{([a-z_]*?):[a-z_]*?\}}{_\1}sgio;
34
push @{$queries{'update'}} , [ $trial_update , $grammar_update ];
37
my @file_deletes = $php_code =~ m{(DELETE.*?)'}sog;
38
foreach my $file_delete (@file_deletes) {
39
$file_delete =~ s{\{db_prefix\}}{smf_}sgio;
40
my ($trial_delete , $grammar_delete) = ($file_delete, $file_delete);
42
$trial_delete =~ s{(\{.*?\})}{1}sgio;
43
$grammar_delete =~ s{\{([a-z_]*?):[a-z_]*?\}}{_\1}sgio;
44
push @{$queries{'delete'}} , [ $trial_delete , $grammar_delete ];
47
my @file_insertsphp = $php_code =~ m{'db_insert']\((.*?)\);}sgio;
50
foreach my $file_insertphp (@file_insertsphp) {
51
$file_insertphp =~ s{[\r\n]}{}sgio;
52
my ($insert_replace, $table) = $file_insertphp =~ m{'(.*?)'.*?'(.*?)'}sio;
54
$insert_replace = 'insert '.$insert_replace if $insert_replace !~ m{^(insert|replace)}sgio;
56
$table =~ s{\{db_prefix\}}{smf_}sgio;
58
die $file_insertphp if $table eq '';
59
my ($array) = $file_insertphp =~ m{array\((.*?)\)}sio;
60
my @array_elements = split (',', $array);
65
foreach my $array_element (@array_elements) {
66
next if $array_element =~ m{^\s+$}sgio;
67
my ($col_name, $col_type) = $array_element =~ m{'(.*?)'\s+=>\s+'(.*?)'}sio;
69
$col_type =~ s{\-}{_}sgio;
70
$col_type = "_".$col_type;
71
if (defined $col_name) {
72
$col_type eq 'unknown_type' if $col_type eq '';
73
push @col_names, $col_name;
75
push @col_types, $col_type;
79
if ($#col_names > -1) {
80
push @{$queries{'insert_replace'}} , [
81
uc($insert_replace)." INTO $table (".join(',', @col_names).") VALUES (".join(',', @col_values).")",
82
uc($insert_replace)." INTO $table (".join(',', map {'`'.$_.'`' }@col_names).") VALUES (".join(',', @col_types).")"
88
my $dbh = DBI->connect('dbi:mysql:database=smf2:user=root', undef , undef , { PrintError => 1});
93
insert_replace | insert_replace | insert_replace | insert_replace | insert_replace | insert_replace |
94
update | update | update | update | update |
96
select | select | select ;
98
_int: _mediumint_unsigned | _tinyint_unsigned | _digit ;
99
_string: _english | _varchar(1) | _varchar(2) | _varchar(255) | REPEAT ( _varchar(2) , _tinyint_unsigned );
100
_string_16: _english | _varchar(1) | _varchar(2) | _varchar(16) | REPEAT ( _varchar(2) , _digit );
101
_string_40: _english | _varchar(1) | _varchar(2) | _varchar(40) | REPEAT ( _varchar(2) , _digit );
102
_string_255: _english | _varchar(1) | _varchar(2) | _varchar(255) | REPEAT ( _varchar(2) , _tinyint_unsigned );
103
_string_65534: _english | _varchar(1) | _varchar(2) | _varchar(255) | REPEAT ( _varchar(2) , _smallint_unsigned );
104
_array_int: _int | _array_int , _int ;
105
_array_string: _string | _string , _string ;
107
_step_low: _digit | _tinyint_unsigned ;
108
_step_high: _tinyint_unsigned | _smallint_unsigned ;
111
foreach my $query_type ('select', 'insert_replace','update','delete') {
112
my $type_queries = $queries{$query_type};
114
foreach my $type_query (@$type_queries) {
115
my ($trial_query, $grammar_rule) = ($type_query->[0], $type_query->[1]);
116
my $sth = $dbh->prepare($trial_query);
118
$errors{$sth->err()}++;
120
if (($sth->err() == 0) || ($sth->err() == 1062)) {
121
$grammar_rule =~ s{(data|next_time|time_offset|time_regularity|time_unit|time_updated|time_removed)}{`$1`}sgio;
122
$grammar_rule =~ s{``}{`}sgio;
123
push @type_rules, $grammar_rule;
126
print "\n\n$query_type:\n".join("|\n\t", @type_rules).";\n\n";
129
print STDERR Dumper \%errors;