Line 1... |
Line 1... |
1 |
#! /usr/bin/perl -w
|
1 |
#! /usr/bin/perl -w
|
- |
|
2 |
|
- |
|
3 |
# Converts a standard MySQL dump file to a PHP file ready to be used by library.php.
|
- |
|
4 |
# NOTE: don't use comma's or parentheses in your comments
|
- |
|
5 |
|
- |
|
6 |
|
2 |
#use Data::Dumper;
|
7 |
#use Data::Dumper;
|
3 |
|
8 |
|
4 |
|
9 |
|
5 |
sub getFieldInfo {
|
10 |
sub getFieldInfo {
|
6 |
my $fieldInfo = shift;
|
11 |
my $fieldInfo = shift;
|
7 |
my $primary_key = shift;
|
12 |
my $primary_key = shift;
|
8 |
my @output;
|
13 |
my @output;
|
- |
|
14 |
my $comment = '';
|
9 |
|
15 |
|
- |
|
16 |
# For a lookup table, we will just build it
|
- |
|
17 |
if ( $fieldInfo =~ m/references\s+([a-z0-9-_]+)\s*\(\s*([a-z0-9-_]+)\s*\)/i ) { # create a lookup
|
- |
|
18 |
push @output, "'type' => 'lookup', 'table' => '$1', 'keyfield' => '$2', 'display_field' => 'name'";
|
10 |
|
19 |
} else { # this is just a standard field
|
11 |
push @output, "'keyfield' => 1" if ( $primary_key);
|
20 |
push @output, "'keyfield' => true" if ( $primary_key);
|
12 |
if ($fieldInfo =~ m/not null/i ) {
|
21 |
if ($fieldInfo =~ m/not null/i ) {
|
13 |
push @output, "'required' => true";
|
22 |
push @output, "'required' => true";
|
14 |
$fieldInfo =~ s/not null//i; # remove the not null from the string
|
23 |
$fieldInfo =~ s/not null//i; # remove the not null from the string
|
15 |
}
|
24 |
}
|
16 |
|
25 |
|
17 |
if ($fieldInfo =~ m/comment *\'([^']+)\'/i ) {
|
26 |
if ($fieldInfo =~ m/comment *\'([^']+)\'/i ) {
|
18 |
$comment = $1;
|
27 |
$comment = $1;
|
19 |
$fieldInfo =~ s/comment *\'([^']+)\'//i
|
28 |
$fieldInfo =~ s/comment *\'([^']+)\'//i
|
20 |
} else {
|
29 |
} else {
|
21 |
$comment = '';
|
30 |
$comment = '';
|
22 |
}
|
31 |
}
|
23 |
|
32 |
|
24 |
$fieldInfo =~ s/default +null//i; # we just ignore default null
|
33 |
$fieldInfo =~ s/default +null//i; # we just ignore default null
|
25 |
if ($fieldInfo =~ m/default +(\'.*\')/i ) {
|
34 |
if ($fieldInfo =~ m/default +(\'.*\')/i ) {
|
26 |
$default = $1;
|
35 |
$default = $1;
|
27 |
$fieldInfo =~ s/default +(\'.*\')//i;
|
36 |
$fieldInfo =~ s/default +(\'.*\')//i;
|
28 |
push @output, "'default' => $default";
|
37 |
push @output, "'default' => $default";
|
29 |
} elsif ($fieldInfo =~ /default +([^ ]+)/i ) {
|
38 |
} elsif ($fieldInfo =~ /default +([^ ]+)/i ) {
|
30 |
$default = $1;
|
39 |
$default = $1;
|
31 |
$fieldInfo =~ s/default +([^ ]+)//;
|
40 |
$fieldInfo =~ s/default +([^ ]+)//;
|
32 |
push @output, "'default' => $default";
|
41 |
push @output, "'default' => $default";
|
33 |
}
|
42 |
}
|
34 |
if ($fieldInfo =~ m/auto_increment/i ) {
|
43 |
if ($fieldInfo =~ m/auto_increment/i ) {
|
35 |
push @output, "'readonly' => true";
|
44 |
push @output, "'readonly' => true";
|
36 |
$fieldInfo =~ s/auto_increment//i;
|
45 |
$fieldInfo =~ s/auto_increment//i;
|
37 |
}
|
46 |
}
|
38 |
# at this point, we should only have the data type left
|
47 |
# at this point, we should only have the data type left
|
39 |
if ( $fieldInfo =~ m/ *([a-z]+)( *\(\d+\))?/i ) {
|
48 |
if ( $fieldInfo =~ m/ *([a-z]+)( *\(\d+\))?/i ) {
|
40 |
$type = $1;
|
49 |
$type = $1;
|
41 |
$size = $2;
|
50 |
$size = $2;
|
42 |
$type =~ s/(varchar)|(char)/string/i;
|
51 |
$type =~ s/(varchar)|(char)/string/i;
|
43 |
push @output, "'type' => '$type'";
|
52 |
push @output, "'type' => '$type'";
|
44 |
if ($size) {
|
53 |
if ($size) {
|
45 |
$size =~ s/[^0-9]//gi;
|
54 |
$size =~ s/[^0-9]//gi;
|
46 |
push @output, "'width' => $size";
|
55 |
push @output, "'width' => $size";
|
47 |
} # if
|
56 |
} # if
|
- |
|
57 |
}
|
48 |
}
|
58 |
}
|
49 |
#return "\n//$original\n\n" . join(" , ", @output);
|
59 |
#return "\n//$original\n\n" . join(" , ", @output);
|
50 |
return ($comment, join(" , ", @output));
|
60 |
return ($comment, join(" , ", @output));
|
51 |
}
|
61 |
}
|
52 |
|
62 |
|
53 |
|
63 |
|
54 |
|
64 |
|
55 |
sub getTableDef {
|
65 |
sub getTableDef {
|
56 |
my $table = shift;
|
66 |
my $table = shift;
|
57 |
print "$table\n";
|
67 |
# print "[$table]\n";
|
58 |
my $unique_key_regex = 'unique key [a-z0-9]+ \(([^)]+)\),?';
|
68 |
my $unique_key_regex = 'unique key [a-z0-9]+ \(([^)]+)\),?';
|
59 |
my $primary_key_regex = 'primary key \(([^)]+)\),?';
|
69 |
my $primary_key_regex = 'primary key \(([^)]+)\),?';
|
60 |
my $comment_regex = "comment *'([^']+)'";
|
70 |
my $comment_regex = "comment *'([^']+)'";
|
61 |
my %tableInformation;
|
71 |
my %tableInformation;
|
62 |
$table =~ s/ +/ /gi; # remove duplicate spaces
|
72 |
$table =~ s/ +/ /gi; # remove duplicate spaces
|
63 |
$table =~ s/ ,/,/gi; # remove all spaces before comma's
|
73 |
$table =~ s/ ,/,/gi; # remove all spaces before comma's
|
64 |
print "$table\n";
|
74 |
#print "[$table]\n\n";
|
65 |
$table =~ m/create table ([a-z0-9_]+) *\((.+)\)([^\(\)]*)$/i; #
|
75 |
$table =~ m/create table ([a-z0-9_]+) *\((.+)\)([^\(\)]*)$/i; #
|
66 |
$tableInformation{'table name'} = $1;
|
76 |
$tableInformation{'table name'} = $1;
|
- |
|
77 |
print STDERR "[$tableInformation{'table name'}]\n";
|
67 |
$table = $2;
|
78 |
$table = $2;
|
68 |
$tableCommentStuff = $3;
|
79 |
$tableCommentStuff = $3;
|
69 |
if ( $tableCommentStuff and $tableCommentStuff =~ m/comment.*\'(.*)\'/i ) {
|
80 |
if ( $tableCommentStuff and $tableCommentStuff =~ m/comment.*\'(.*)\'/i ) {
|
70 |
$tableInformation{'comment'} = $1;
|
81 |
$tableInformation{'comment'} = $1;
|
71 |
}
|
82 |
}
|
Line 87... |
Line 98... |
87 |
($fieldInfo{$fieldName}{'comment'},$fieldInfo{$fieldName}{'data'}) = &getFieldInfo($fieldDef, $tableInformation{'primary keys'} ? ($tableInformation{'primary keys'} =~ m/$fieldName/) : 0 );
|
98 |
($fieldInfo{$fieldName}{'comment'},$fieldInfo{$fieldName}{'data'}) = &getFieldInfo($fieldDef, $tableInformation{'primary keys'} ? ($tableInformation{'primary keys'} =~ m/$fieldName/) : 0 );
|
88 |
}
|
99 |
}
|
89 |
} # for
|
100 |
} # for
|
90 |
#$tableInformation{'fields'} = \%fieldInfo;
|
101 |
#$tableInformation{'fields'} = \%fieldInfo;
|
91 |
#print Dumper(\%fieldInfo );
|
102 |
#print Dumper(\%fieldInfo );
|
- |
|
103 |
|
- |
|
104 |
# create the output
|
92 |
my $output = "'" . $tableInformation{'table name'} . "' => array( \n";
|
105 |
my $output = '';
|
93 |
if ( $tableInformation{'comment'} ) {
|
106 |
if ( $tableInformation{'comment'} ) {
|
94 |
$output .= "/* $tableInformation{'comment'} */\n";
|
107 |
$output .= "/* $tableInformation{'comment'} */\n";
|
95 |
}
|
108 |
}
|
- |
|
109 |
$output .= "'" . $tableInformation{'table name'} . "' => array( \n";
|
96 |
$output .= "\t'table name' => '" . $tableInformation{'table name'} . "',\n";
|
110 |
$output .= "\t'table name' => '" . $tableInformation{'table name'} . "',\n";
|
97 |
if ($tableInformation{'primary keys'}) {
|
111 |
if ($tableInformation{'primary keys'}) {
|
98 |
if ( $tableInformation{'primary keys'} =~ m/,/ ) {
|
112 |
if ( $tableInformation{'primary keys'} =~ m/,/ ) {
|
99 |
#following line takes all items in a comma delimited list and quotes them
|
113 |
#following line takes all items in a comma delimited list and quotes them
|
100 |
$output .= "\t'key field' => array('" . join("','", split( ',',$tableInformation{'primary keys'})) . "'),\n";
|
114 |
$output .= "\t'key field' => array('" . join("','", split( ',',$tableInformation{'primary keys'})) . "'),\n";
|
Line 107... |
Line 121... |
107 |
#$fieldNames = join( ',',@fields );
|
121 |
#$fieldNames = join( ',',@fields );
|
108 |
$output .= "\t'display columns' => array('" . join( "','",@fields ) . "'),\n";
|
122 |
$output .= "\t'display columns' => array('" . join( "','",@fields ) . "'),\n";
|
109 |
$output .= "\t'display query' => 'select " . join( ',',@fields ) . " from " . $tableInformation{'table name'} . "',\n";
|
123 |
$output .= "\t'display query' => 'select " . join( ',',@fields ) . " from " . $tableInformation{'table name'} . "',\n";
|
110 |
my @fieldInfo;
|
124 |
my @fieldInfo;
|
111 |
foreach $thisField (keys %fieldInfo ) {
|
125 |
foreach $thisField (keys %fieldInfo ) {
|
- |
|
126 |
push @fieldInfo, ;
|
- |
|
127 |
|
112 |
push @fieldInfo, "/* " . $fieldInfo{$thisField}{'comment'} . " */" if $fieldInfo{$thisField}{'comment'};
|
128 |
push @fieldInfo, ($fieldInfo{$thisField}{'comment'} ? "\t\t/* " . $fieldInfo{$thisField}{'comment'} . " */\n" : '') .
|
113 |
push @fieldInfo, "'$thisField' => array(" . $fieldInfo{$thisField}{'data'} . ")";
|
129 |
"\t\t'$thisField' => array(" . $fieldInfo{$thisField}{'data'} . ")";
|
114 |
}
|
130 |
}
|
115 |
$output .= "\t'field info' => array(\n\t\t" . join( ",\n\t\t", @fieldInfo) . "\n\t)\n)";
|
131 |
$output .= "\t'field info' => array(\n" . join( ",\n", @fieldInfo) . "\n\t)\n)";
|
116 |
|
132 |
|
117 |
return $output;
|
133 |
return $output;
|
118 |
}
|
134 |
}
|
119 |
|
135 |
|
- |
|
136 |
my $indentationChar = ' ';
|
- |
|
137 |
|
120 |
my $inputFileName = shift;
|
138 |
my $inputFileName = shift;
|
121 |
if ($inputFileName) {
|
139 |
if ($inputFileName) {
|
122 |
open DATA, "$inputFileName" or die "could not open $inputFileName";
|
140 |
open DATA, "$inputFileName" or die "could not open $inputFileName";
|
123 |
@input = <DATA>;
|
141 |
@input = <DATA>;
|
124 |
close DATA;
|
142 |
close DATA;
|
Line 127... |
Line 145... |
127 |
}
|
145 |
}
|
128 |
@input = grep( !/^(--)|(\/\*\!)/, @input );
|
146 |
@input = grep( !/^(--)|(\/\*\!)/, @input );
|
129 |
|
147 |
|
130 |
|
148 |
|
131 |
chomp @input;
|
149 |
chomp @input;
|
- |
|
150 |
# next line read all input, joins it with null,then splits it based on a semi-colon
|
- |
|
151 |
# this places all sql statements into one array entry
|
- |
|
152 |
# It then filters for only those statements that start with create table
|
132 |
my @tables = split( ';', join( '', @input) );
|
153 |
my @tables = grep(/^create table/i, split( ';', join( '', @input) ));
|
- |
|
154 |
#foreach my $thisTable (@tables) {
|
- |
|
155 |
# print "$thisTable\n";
|
- |
|
156 |
#}
|
- |
|
157 |
#exit 1;
|
- |
|
158 |
|
133 |
my @results;
|
159 |
my @results;
|
134 |
#push @results, &getTableDef( shift @tables );
|
160 |
#push @results, &getTableDef( shift @tables );
|
135 |
foreach $thisTable( @tables ) {
|
161 |
foreach $thisTable( @tables ) {
|
136 |
$thisTable =~ s/\`//gi;
|
162 |
$thisTable =~ s/\`//gi;
|
137 |
push @results, &getTableDef( $thisTable );
|
163 |
push @results, &getTableDef( $thisTable ) if $thisTable;
|
- |
|
164 |
}
|
- |
|
165 |
|
- |
|
166 |
# apply proper indentation. The code inserts a tab char and does a decent job of indenting
|
- |
|
167 |
# we now replace the tab chars with $indentationChar, and add one more to the beginning
|
- |
|
168 |
for (my $line = 0; $line < @results; $line++ ) {
|
- |
|
169 |
$results[$line] = "\t" . $results[$line];
|
- |
|
170 |
$results[$line] =~ s/\n/\n\t/g;
|
- |
|
171 |
$results[$line] =~ s/\t/$indentationChar/g;
|
138 |
}
|
172 |
}
|
139 |
|
173 |
|
140 |
print "<?php\n";
|
174 |
print "<?php\n";
|
141 |
print "define ( MAX_INPUT_FIELD_DISPLAY, 40 ); // this is the maximum input field size
|
175 |
print "define ( MAX_INPUT_FIELD_DISPLAY, 40 ); // this is the maximum input field size
|
142 |
define ( IMAGE_DIRECTORY, '/pictures/' ); // relative URL where pictures are stored
|
176 |
define ( IMAGE_DIRECTORY, '/pictures/' ); // relative URL where pictures are stored
|
143 |
define ( EDIT_IMAGE_HEIGHT, 100 ); // height for thumbnail of pictuers
|
177 |
define ( EDIT_IMAGE_HEIGHT, 100 ); // height for thumbnail of pictuers
|
144 |
define ( MAX_UPLOAD_FILE_SIZE, 1024*1024*10 ); // 10 meg
|
178 |
define ( MAX_UPLOAD_FILE_SIZE, 1024*1024*10 ); // 10 meg
|
145 |
define (DEFAULT_TEXTAREA_HEIGHT, 5 );
|
179 |
define (DEFAULT_TEXTAREA_HEIGHT, 5 );
|
146 |
define define ( DEFAULT_TABLE, 'FILL ME IN');
|
180 |
define ( DEFAULT_TABLE, 'FILL ME IN');
|
147 |
global \$DATABASE_DEFINITION;
|
181 |
global \$DATABASE_DEFINITION;
|
148 |
|
182 |
|
149 |
";
|
183 |
";
|
150 |
print "\$DATABASE_DEFINITION = array(\n" . join( ",\n", @results) . "\n);";
|
184 |
print "\$DATABASE_DEFINITION = array(\n" . join( ",\n", @results) . "\n);";
|
151 |
print "\n?>\n";
|
185 |
print "\n?>\n";
|