Rev 31 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#! /usr/bin/perl -w
# Converts a standard MySQL dump file to a PHP file ready to be used by library.php.
# NOTE: don't use comma's or parentheses in your comments
#use Data::Dumper;
my $secondary = 0;
my $indentationChar = ' ';
my $inputFileName = '';
sub getFieldInfo {
my $fieldName = shift;
my $fieldInfo = shift;
my $primary_key = shift;
my @output;
my $comment = '';
push @output, "'display name' => '$fieldName'";
# For a lookup table, we will just build it
if ( $fieldInfo =~ m/references\s+([a-z0-9-_]+)\s*\(\s*([a-z0-9-_]+)\s*\)/i ) { # create a lookup
push @output, "'type' => 'lookup', 'table' => '$1', 'keyfield' => '$2', 'display_field' => 'name'";
} else { # this is just a standard field
push @output, "'keyfield' => true" if ( $primary_key);
if ($fieldInfo =~ m/not null/i ) {
push @output, "'required' => true";
$fieldInfo =~ s/not null//i; # remove the not null from the string
}
if ($fieldInfo =~ m/comment *\'([^']+)\'/i ) {
$comment = $1;
$fieldInfo =~ s/comment *\'([^']+)\'//i
} else {
$comment = '';
}
$fieldInfo =~ s/default +null//i; # we just ignore default null
if ($fieldInfo =~ m/default +(\'.*\')/i ) {
$default = $1;
$fieldInfo =~ s/default +(\'.*\')//i;
push @output, "'default' => $default";
} elsif ($fieldInfo =~ /default +([^ ]+)/i ) {
$default = $1;
$fieldInfo =~ s/default +([^ ]+)//;
push @output, "'default' => $default";
}
if ($fieldInfo =~ m/auto_increment/i ) {
push @output, "'readonly' => true";
$fieldInfo =~ s/auto_increment//i;
}
# at this point, we should only have the data type left
if ( $fieldInfo =~ m/ *([a-z]+)( *\(\d+\))?/i ) {
$type = $1;
$size = $2;
$type =~ s/(varchar)|(char)/string/i;
push @output, "'type' => '$type'";
if ($size) {
$size =~ s/[^0-9]//gi;
push @output, "'width' => $size";
} # if
}
}
#return "\n//$original\n\n" . join(" , ", @output);
return ($comment, join(" , ", @output));
}
sub getTableDef {
my $table = shift;
# print "[$table]\n";
my $unique_key_regex = 'unique key [a-z0-9]+ \(([^)]+)\),?';
my $primary_key_regex = 'primary key \(([^)]+)\),?';
my $comment_regex = "comment *'([^']+)'";
my %tableInformation;
$table =~ s/ +/ /gi; # remove duplicate spaces
$table =~ s/ ,/,/gi; # remove all spaces before comma's
#print "[$table]\n\n";
$table =~ m/create table ([a-z0-9_]+) *\((.+)\)([^\(\)]*)$/i; #
$tableInformation{'table name'} = $1;
$tableInformation{'display name'} = $tableInformation{'table name'};
print STDERR "[$tableInformation{'table name'}]\n";
$table = $2;
$tableCommentStuff = $3;
if ( $tableCommentStuff and $tableCommentStuff =~ m/comment.*\'(.*)\'/i ) {
$tableInformation{'comment'} = $1;
}
if ( $table =~ m/$primary_key_regex/i ) {
$tableInformation{'primary keys'} = $1;
$table =~ s/$primary_key_regex//i;
}
if ( $table =~ m/$unique_key_regex/i ) {
$tableInformation{'unique keys'} = $1;
$table =~ s/$unique_key_regex//i;
}
my @columnInformation = split(',',$table);
my %fieldInfo;
for ( $i = 0; $i < @columnInformation; $i++) {
$columnInformation[$i] =~ s/^ +//;
if ( $columnInformation[$i] =~ m/ *([a-z0-9_]+) +(.*)/i ) {
$fieldName = $1;
$fieldDef = $2;
($fieldInfo{$fieldName}{'comment'},$fieldInfo{$fieldName}{'data'}) = &getFieldInfo($fieldName, $fieldDef, $tableInformation{'primary keys'} ? ($tableInformation{'primary keys'} =~ m/$fieldName/) : 0 );
}
} # for
#$tableInformation{'fields'} = \%fieldInfo;
#print Dumper(\%fieldInfo );
# create the output
my $output = '';
if ( $tableInformation{'comment'} ) {
$output .= "/* $tableInformation{'comment'} */\n";
}
if ( $secondary ) {
$output = "\$DATABASE_DEFINITION['" . $tableInformation{'table name'} . "'] = array(\n";
} else {
$output .= "'" . $tableInformation{'table name'} . "' => array( \n";
}
$output .= "\t'table name' => '" . $tableInformation{'table name'} . "',\n";
if ($tableInformation{'primary keys'}) {
if ( $tableInformation{'primary keys'} =~ m/,/ ) {
#following line takes all items in a comma delimited list and quotes them
$output .= "\t'key field' => array('" . join("','", split( ',',$tableInformation{'primary keys'})) . "'),\n";
} else {
$output .= "\t'key field' => '" . $tableInformation{'primary keys'} . "',\n";
}
}
# do the display columns, basically all columns
@fields = keys %fieldInfo;
#$fieldNames = join( ',',@fields );
$output .= "\t'display columns' => array('" . join( "','",@fields ) . "'),\n";
$output .= "\t'display query' => 'select " . join( ',',@fields ) . " from " . $tableInformation{'table name'} . "',\n";
my @fieldInfo;
foreach $thisField (keys %fieldInfo ) {
push @fieldInfo, ($fieldInfo{$thisField}{'comment'} ? "\t\t/* " . $fieldInfo{$thisField}{'comment'} . " */\n" : '') .
"\t\t'$thisField' => array(" . $fieldInfo{$thisField}{'data'} . ")";
}
$output .= "\t'field info' => array(\n" . join( ",\n", @fieldInfo) . "\n\t)\n)";
return $output;
}
sub processCommandLine {
while ( $parameter = shift ) {
if ($parameter =~ m/-f(.*)/i) {
$inputFileName = $1;
} elsif ( $parameter =~ m/-s/i) {
$secondary = 1;
}
}
}
&processCommandLine( @ARGV );
# die "Input Filename == $inputFileName, secondary = $secondary\n";
if ($inputFileName) { # they want us to read from this file
open DATA, "$inputFileName" or die "could not open $inputFileName";
@input = <DATA>;
close DATA;
} else { # no filename entered, so read STDIN
@input = <STDIN>;
}
@input = grep( !/^(--)|(\/\*\!)/, @input ); # remove all comments
chomp @input; # get rid of all line endings
# next line read all input, joins it with null,then splits it based on a semi-colon
# this places all sql statements into one array entry
# It then filters for only those statements that start with create table
my @tables = grep(/^create table/i, split( ';', join( '', @input) ));
#foreach my $thisTable (@tables) {
# print "$thisTable\n";
#}
#exit 1;
my @results;
#push @results, &getTableDef( shift @tables );
foreach $thisTable( @tables ) {
$thisTable =~ s/\`//gi;
push @results, &getTableDef( $thisTable ) if $thisTable;
}
# apply proper indentation. The code inserts a tab char and does a decent job of indenting
# we now replace the tab chars with $indentationChar, and add one more to the beginning
for (my $line = 0; $line < @results; $line++ ) {
$results[$line] = "\t" . $results[$line];
$results[$line] =~ s/\n/\n\t/g;
$results[$line] =~ s/\t/$indentationChar/g;
}
# now, actually print out the results
print "<?php\nglobal \$DATABASE_DEFINITION;\n";
if ( $secondary ) { # this is an auxilary file
print join( ";\n", @results) ;
} else { # this is the main, or only database.php file
print "define ( MAX_INPUT_FIELD_DISPLAY, 40 ); // this is the maximum input field size
define ( IMAGE_DIRECTORY, '/pictures/' ); // relative URL where pictures are stored
define ( EDIT_IMAGE_HEIGHT, 100 ); // height for thumbnail of pictuers
define ( MAX_UPLOAD_FILE_SIZE, 1024*1024*10 ); // 10 meg
define (DEFAULT_TEXTAREA_HEIGHT, 5 );
define ( DEFAULT_TABLE, 'FILL ME IN');
";
print "\$DATABASE_DEFINITION = array(\n";
print join( ",\n", @results) ;
print "\n);";
}
print "\n?>\n";
1;