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